Clean up temporary git pack files when breaking locks.

When a git-cache operation is interrupted, it can leave behind large
temporary pack files.  Over time, these pack files will fill up
disks.

R=agable@chromium.org,hinoka@chromium.org,iannucci@chromium.org
BUG=352268

Review URL: https://codereview.chromium.org/326203003

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@276208 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cache.py b/git_cache.py
index 086b479..9ade674 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -361,6 +361,18 @@
     gsutil.call('cp', tmp_zipfile, dest_name)
     os.remove(tmp_zipfile)
 
+  @staticmethod
+  def DeleteTmpPackFiles(path):
+    pack_dir = os.path.join(path, 'objects', 'pack')
+    pack_files = [f for f in os.listdir(pack_dir) if
+                  f.startswith('.tmp-') or f.startswith('tmp_pack_')]
+    for f in pack_files:
+      f = os.path.join(pack_dir, f)
+      try:
+        os.remove(f)
+        logging.warn('Deleted stale temporary pack file %s' % f)
+      except OSError:
+        logging.warn('Unable to delete temporary pack file %s' % f)
 
   @staticmethod
   def BreakLocks(path):