git_cache: on upload, run "git gc" only if necessary.

If the destination cache file already exists, "git gc" isn't useful.

R=karenqian

Bug: 943696
Change-Id: I1fe3ab04713996b962012242edb6cbdb279f4874
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1585707
Reviewed-by: Karen Qian <karenqian@google.com>
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
diff --git a/git_cache.py b/git_cache.py
index a7ac8a7..bd534d0 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -607,9 +607,6 @@
     # The folder is <git number>
     gen_number = subprocess.check_output(
         [self.git_exe, 'number', 'master'], cwd=self.mirror_path).strip()
-    # Run Garbage Collect to compress packfile.
-    self.RunGit(['gc', '--prune=all'])
-
     gsutil = Gsutil(path=self.gsutil_exe, boto_path=None)
 
     src_name = self.mirror_path
@@ -631,6 +628,9 @@
       print('Cache %s already exists' % dest_name)
       return
 
+    # Run Garbage Collect to compress packfile.
+    self.RunGit(['gc', '--prune=all'])
+
     gsutil.call('-m', 'cp', '-r', src_name, dest_name)
 
     #TODO(karenqian): prune old caches