summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2020-09-06 15:51:21 -0400
committerMike Frysinger <vapier@google.com>2020-09-09 05:46:07 +0000
commit50a81de2bc9f2074d56c368f651cf9f50c8d8a87 (patch)
treeb72dbba8f5915686c8c10f6de501949003685164
parent0501b29e7ae072e0b10ea9ddd913ec6d5975f690 (diff)
downloadgit-repo-50a81de2bc9f2074d56c368f651cf9f50c8d8a87.tar.gz
init: use the remote default manifest branch
Instead of hardcoding "master" as our default, use the remote server's default branch instead. For most people, this should be the same as "master" already. For projects moving to "main", it means we'll use the new name automatically rather than forcing people to use -b main. For repositories that never set up a default HEAD, we should still use the historical "master" default. Bug: https://crbug.com/gerrit/13339 Change-Id: I4117c81a760c9495f98dbb1111a3e6c127f45eba Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/280799 Reviewed-by: Michael Mortensen <mmortensen@google.com> Tested-by: Mike Frysinger <vapier@google.com>
-rw-r--r--project.py21
-rw-r--r--subcmds/init.py26
2 files changed, 36 insertions, 11 deletions
diff --git a/project.py b/project.py
index fdbf9e4a..80b0cf8d 100644
--- a/project.py
+++ b/project.py
@@ -2311,6 +2311,27 @@ class Project(object):
2311 # Enable the extension! 2311 # Enable the extension!
2312 self.config.SetString('extensions.%s' % (key,), value) 2312 self.config.SetString('extensions.%s' % (key,), value)
2313 2313
2314 def ResolveRemoteHead(self, name=None):
2315 """Find out what the default branch (HEAD) points to.
2316
2317 Normally this points to refs/heads/master, but projects are moving to main.
2318 Support whatever the server uses rather than hardcoding "master" ourselves.
2319 """
2320 if name is None:
2321 name = self.remote.name
2322
2323 # The output will look like (NB: tabs are separators):
2324 # ref: refs/heads/master HEAD
2325 # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44 HEAD
2326 output = self.bare_git.ls_remote('-q', '--symref', '--exit-code', name, 'HEAD')
2327
2328 for line in output.splitlines():
2329 lhs, rhs = line.split('\t', 1)
2330 if rhs == 'HEAD' and lhs.startswith('ref:'):
2331 return lhs[4:].strip()
2332
2333 return None
2334
2314 def _CheckForImmutableRevision(self): 2335 def _CheckForImmutableRevision(self):
2315 try: 2336 try:
2316 # if revision (sha or tag) is not present then following function 2337 # if revision (sha or tag) is not present then following function
diff --git a/subcmds/init.py b/subcmds/init.py
index 41578076..5ba0d074 100644
--- a/subcmds/init.py
+++ b/subcmds/init.py
@@ -54,7 +54,8 @@ from the server and is installed in the .repo/ directory in the
54current working directory. 54current working directory.
55 55
56The optional -b argument can be used to select the manifest branch 56The optional -b argument can be used to select the manifest branch
57to checkout and use. If no branch is specified, master is assumed. 57to checkout and use. If no branch is specified, the remote's default
58branch is used.
58 59
59The optional -m argument can be used to specify an alternate manifest 60The optional -m argument can be used to specify an alternate manifest
60to be used. If no manifest is specified, the manifest default.xml 61to be used. If no manifest is specified, the manifest default.xml
@@ -215,24 +216,27 @@ to update the working directory files.
215 216
216 m._InitGitDir(mirror_git=mirrored_manifest_git) 217 m._InitGitDir(mirror_git=mirrored_manifest_git)
217 218
218 if opt.manifest_branch:
219 m.revisionExpr = opt.manifest_branch
220 else:
221 m.revisionExpr = 'refs/heads/master'
222 else:
223 if opt.manifest_branch:
224 m.revisionExpr = opt.manifest_branch
225 else:
226 m.PreSync()
227
228 self._ConfigureDepth(opt) 219 self._ConfigureDepth(opt)
229 220
221 # Set the remote URL before the remote branch as we might need it below.
230 if opt.manifest_url: 222 if opt.manifest_url:
231 r = m.GetRemote(m.remote.name) 223 r = m.GetRemote(m.remote.name)
232 r.url = opt.manifest_url 224 r.url = opt.manifest_url
233 r.ResetFetch() 225 r.ResetFetch()
234 r.Save() 226 r.Save()
235 227
228 if opt.manifest_branch:
229 m.revisionExpr = opt.manifest_branch
230 else:
231 if is_new:
232 default_branch = m.ResolveRemoteHead()
233 if default_branch is None:
234 # If the remote doesn't have HEAD configured, default to master.
235 default_branch = 'refs/heads/master'
236 m.revisionExpr = default_branch
237 else:
238 m.PreSync()
239
236 groups = re.split(r'[,\s]+', opt.groups) 240 groups = re.split(r'[,\s]+', opt.groups)
237 all_platforms = ['linux', 'darwin', 'windows'] 241 all_platforms = ['linux', 'darwin', 'windows']
238 platformize = lambda x: 'platform-' + x 242 platformize = lambda x: 'platform-' + x