From 7c759d4d293412d23de180e5fd54a4192ac4382f Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Mon, 28 Sep 2015 01:45:04 -0700 Subject: [PATCH] git-linguist: Do not write cache if repo is gone --- bin/git-linguist | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bin/git-linguist b/bin/git-linguist index 170406de..9541cf82 100755 --- a/bin/git-linguist +++ b/bin/git-linguist @@ -71,17 +71,20 @@ class GitLinguist end def write_cache(object) - tmp_path = Dir::Tmpname.make_tmpname(cache_file, nil) + return unless File.directory? @repo_path - File.open(tmp_path, "wb") do |f| - marshal = Marshal.dump(object) - f.write(Zlib::Deflate.deflate(marshal)) + begin + tmp_path = Dir::Tmpname.make_tmpname(cache_file, nil) + File.open(tmp_path, "wb") do |f| + marshal = Marshal.dump(object) + f.write(Zlib::Deflate.deflate(marshal)) + end + + File.rename(tmp_path, cache_file) + rescue => e + (File.unlink(tmp_path) rescue nil) + raise e end - - File.rename(tmp_path, cache_file) - tmp_path = nil - ensure - (File.unlink(tmp_path) rescue nil) if tmp_path end def load_cache