cmd/gitmirror: prune refs that no longer exist on origin

Use the --prune flag when running git fetch origin commands
to delete remote-tracking refs in the local repositories.
This prevents branches and tags that have been deleted on
the origin server from reappearing in the GitHub mirrors.

It will take more work to get refs automatically deleted from
the mirror repositories, because the current Repo.push uses
a custom implementation rather than git push -f --mirror dest,
and it doesn't take the need to delete refs into account yet.

See https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---prune
for information on the --prune flag. The --prune-tags flag isn't
needed because the local repositories are cloned with --mirror.

Updates golang/go#23099

Change-Id: I762b2d1365287d5dac40236310713c3df15d59d8
Reviewed-on: https://go-review.googlesource.com/c/build/+/190879
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
diff --git a/cmd/gitmirror/gitmirror.go b/cmd/gitmirror/gitmirror.go
index 7eb076f..19c367a 100644
--- a/cmd/gitmirror/gitmirror.go
+++ b/cmd/gitmirror/gitmirror.go
@@ -418,7 +418,7 @@
 	needClone := true
 	if r.shouldTryReuseGitDir(dstURL) {
 		r.setStatus("reusing git dir; running git fetch")
-		cmd := exec.Command("git", "fetch", "origin")
+		cmd := exec.Command("git", "fetch", "--prune", "origin")
 		cmd.Dir = r.root
 		r.logf("running git fetch")
 		t0 := time.Now()
@@ -1090,7 +1090,7 @@
 		if n > 1 {
 			r.setStatus(fmt.Sprintf("running git fetch origin, attempt %d", n))
 		}
-		cmd := exec.Command("git", "fetch", "origin")
+		cmd := exec.Command("git", "fetch", "--prune", "origin")
 		cmd.Dir = r.root
 		if out, err := cmd.CombinedOutput(); err != nil {
 			err = fmt.Errorf("%v\n\n%s", err, out)