Просмотр исходного кода

Default to origin's default branch instead of always origin/master.

BUG=1023031

Change-Id: I4bf3e33932af40600646f070f057a7c8c0661f33
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1954624
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Auto-Submit: Jeffrey Yasskin <jyasskin@chromium.org>
Jeffrey Yasskin 5 лет назад
Родитель
Сommit
6b52dc21e1
4 измененных файлов с 26 добавлено и 3 удалено
  1. 9 1
      git_common.py
  2. 2 1
      man/src/git-new-branch.txt
  3. 1 1
      man/src/git-rebase-update.txt
  4. 14 0
      tests/git_common_test.py

+ 9 - 1
git_common.py

@@ -665,8 +665,16 @@ def repo_root():
   return run('rev-parse', '--show-toplevel')
 
 
+def upstream_default():
+  """Returns the default branch name of the origin repository."""
+  try:
+    return run('rev-parse', '--abbrev-ref', 'origin/HEAD')
+  except subprocess2.CalledProcessError:
+    return 'origin/master'
+
+
 def root():
-  return get_config('depot-tools.upstream', 'origin/master')
+  return get_config('depot-tools.upstream', upstream_default())
 
 
 @contextlib.contextmanager

+ 2 - 1
man/src/git-new-branch.txt

@@ -52,7 +52,8 @@ depot-tools.upstream
 ~~~~~~~~~~~~~~~~~~~~
 
 This configures the default 'upstream' for all new branches. If it is unset, it
-defaults to 'origin/master'.  This is considered to be the 'root' branch.
+defaults to 'origin''s default branch or 'origin/master' if that can't be found.
+This is considered to be the 'root' branch.
 
 EXAMPLE
 -------

+ 1 - 1
man/src/git-rebase-update.txt

@@ -63,7 +63,7 @@ Restoration::
   `git rebase-update` checks out the branch that you started on, and 'thaws' it,
   if necessary (see linkgit:git-thaw[1]). If the branch you started on got
   cleaned up, `git rebase-update` will checkout the 'root' ref (defaults to
-  'origin/master', as configured by `depot-tools.upstream`, see
+  'origin''s default branch, as configured by `depot-tools.upstream`, see
   linkgit:git-new-branch[1]).
 
 

+ 14 - 0
tests/git_common_test.py

@@ -453,6 +453,20 @@ class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase,
 
     self.assertEqual('catfood', self.repo.run(self.gc.root))
 
+  def testRoot(self):
+    origin_schema = git_test_utils.GitRepoSchema("""
+    A B C
+      B D
+    """, self.getRepoContent)
+    origin = origin_schema.reify()
+    # Set the default branch to branch_D instead of master.
+    origin.git('checkout', 'branch_D')
+
+    self.repo.git('remote', 'add', 'origin', origin.repo_path)
+    self.repo.git('fetch', 'origin')
+    self.repo.git('remote', 'set-head', 'origin', '-a')
+    self.assertEqual('origin/branch_D', self.repo.run(self.gc.root))
+
   def testUpstream(self):
     self.repo.git('commit', '--allow-empty', '-am', 'foooooo')
     self.assertEqual(self.repo.run(self.gc.upstream, 'bobly'), None)