Set HEAD on repo init, not after downloading repo

git_cache incorrectly sets HEAD to main. It should be done during repo
initialization, and not after files are downloaded. Current
implementation overrides HEAD settings instead of keping whatever is on
remote.

R=aravindvasudev@google.com

Fixed: 1475252
Change-Id: Idba87e2774ecbf249630a207d284f91c0f4a8a38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4820461
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com>
diff --git a/git_cache.py b/git_cache.py
index cd67f24..816f415 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -305,7 +305,7 @@
     try:
       # create new temporary directory locally
       tempdir = tempfile.mkdtemp(prefix='_cache_tmp', dir=self.GetCachePath())
-      self.RunGit(['init', '--bare'], cwd=tempdir)
+      self.RunGit(['init', '-b', 'main', '--bare'], cwd=tempdir)
       self.print('Downloading files in %s/* into %s.' %
                  (latest_dir, tempdir))
       with self.print_duration_of('download'):
@@ -314,8 +314,6 @@
                              tempdir)
       if code:
         return False
-      # Set HEAD to main.
-      self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/main'], cwd=tempdir)
       # A quick validation that all references are valid.
       self.RunGit(['for-each-ref'], print_stdout=False, cwd=tempdir)
     except Exception as e: