sync: REPO_AUTO_GC=1 to restore old behavior.

Add an environment variable to restore previous behavior, since the
older version of repo does not support `--auto-gc`.

Change-Id: I874dfb8fc3533a97b8adfd52125eb3d1d75e2f3c
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/351194
Reviewed-by: Sam Saccone <samccone@google.com>
Tested-by: LaMont Jones <lamontjones@google.com>
diff --git a/subcmds/sync.py b/subcmds/sync.py
index b7542cc..862c7e2 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -68,9 +68,12 @@
 _ONE_DAY_S = 24 * 60 * 60
 # Env var to implicitly turn off object backups.
 REPO_BACKUP_OBJECTS = 'REPO_BACKUP_OBJECTS'
-
 _BACKUP_OBJECTS = os.environ.get(REPO_BACKUP_OBJECTS) != '0'
 
+# Env var to implicitly turn auto-gc back on.
+_REPO_AUTO_GC = 'REPO_AUTO_GC'
+_AUTO_GC = os.environ.get(_REPO_AUTO_GC) == '1'
+
 
 class _FetchOneResult(NamedTuple):
   """_FetchOne return value.
@@ -312,7 +315,7 @@
                  help='delete refs that no longer exist on the remote (default)')
     p.add_option('--no-prune', dest='prune', action='store_false',
                  help='do not delete refs that no longer exist on the remote')
-    p.add_option('--auto-gc', action='store_true',
+    p.add_option('--auto-gc', action='store_true', default=None,
                  help='run garbage collection on all synced projects')
     p.add_option('--no-auto-gc', dest='auto_gc', action='store_false',
                  help='do not run garbage collection on any projects (default)')
@@ -1219,6 +1222,11 @@
     if opt.prune is None:
       opt.prune = True
 
+    if opt.auto_gc is None and _AUTO_GC:
+      print(f"Will run `git gc --auto` because {_REPO_AUTO_GC} is set.",
+            file=sys.stderr)
+      opt.auto_gc = True
+
   def Execute(self, opt, args):
     manifest = self.outer_manifest
     if not opt.outer_manifest: