Prune branches that no longer exist on remote

If a local branch tracks removed remote tracking branch (e.g. foo) and
remote has a branch that starts with such name (e.g. foo/bar), git fetch
will fail. --prune flag removes any remote-tracking branches that no
longer exist.

R=apolito@google.com, ehmaldonado@chromium.org

Bug: 1079483
Change-Id: I9bc31bf961d52a86b6fa2342249971b99a003666
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2190341
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/git_cache.py b/git_cache.py
index 5184c5b..0ca9c7d 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -529,18 +529,26 @@
             'but failed. Continuing with non-optimized repository.'
             % len(pack_files))
 
-  def _fetch(self, rundir, verbose, depth, no_fetch_tags, reset_fetch_config):
+  def _fetch(self,
+             rundir,
+             verbose,
+             depth,
+             no_fetch_tags,
+             reset_fetch_config,
+             prune=True):
     self.config(rundir, reset_fetch_config)
-    v = []
-    d = []
-    t = []
+
+    fetch_cmd = ['fetch']
     if verbose:
-      v = ['-v', '--progress']
+      fetch_cmd.extend(['-v', '--progress'])
     if depth:
-      d = ['--depth', str(depth)]
+      fetch_cmd.extend(['--depth', str(depth)])
     if no_fetch_tags:
-      t = ['--no-tags']
-    fetch_cmd = ['fetch'] + v + d + t + ['origin']
+      fetch_cmd.append('--no-tags')
+    if prune:
+      fetch_cmd.append('--prune')
+    fetch_cmd.append('origin')
+
     fetch_specs = subprocess.check_output(
         [self.git_exe, 'config', '--get-all', 'remote.origin.fetch'],
         cwd=rundir).decode('utf-8', 'ignore').strip().splitlines()
@@ -574,16 +582,16 @@
 
     try:
       self._ensure_bootstrapped(depth, bootstrap, reset_fetch_config)
-      self._fetch(
-          self.mirror_path, verbose, depth, no_fetch_tags, reset_fetch_config)
+      self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
+                  reset_fetch_config)
     except ClobberNeeded:
       # This is a major failure, we need to clean and force a bootstrap.
       gclient_utils.rmtree(self.mirror_path)
       self.print(GIT_CACHE_CORRUPT_MESSAGE)
       self._ensure_bootstrapped(
           depth, bootstrap, reset_fetch_config, force=True)
-      self._fetch(
-          self.mirror_path, verbose, depth, no_fetch_tags, reset_fetch_config)
+      self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
+                  reset_fetch_config)
     finally:
       if not ignore_lock:
         lockfile.unlock()