From 0642373b276ce883bc91f765966602c5c7867ad7 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Wed, 31 Mar 2021 19:04:42 +0000 Subject: [PATCH] Update origin/HEAD automatically If origin/HEAD resolves to `master`, depot_tools can check if origin/main exists. If so, it's very likely that origin/HEAD should be updated to point to origin/main. In the worst case, this will result in extra git remote call while not changing local state (i.e origin/HEAD remained unchanged). R=gavinmak@google.com Bug: 1190702 Change-Id: I51e69d7a95d3534e1820099b4b6983d38b5e2763 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2795075 Reviewed-by: Gavin Mak Commit-Queue: Josip Sokcevic --- git_common.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/git_common.py b/git_common.py index 446354152..523f62894 100644 --- a/git_common.py +++ b/git_common.py @@ -683,7 +683,16 @@ def repo_root(): def upstream_default(): """Returns the default branch name of the origin repository.""" try: - return run('rev-parse', '--abbrev-ref', 'origin/HEAD') + ret = run('rev-parse', '--abbrev-ref', 'origin/HEAD') + # Detect if the repository migrated to main branch + if ret == 'origin/master': + try: + ret = run('rev-parse', '--abbrev-ref', 'origin/main') + run('remote', 'set-head', '-a', 'origin') + ret = run('rev-parse', '--abbrev-ref', 'origin/HEAD') + except subprocess2.CalledProcessError: + pass + return ret except subprocess2.CalledProcessError: return 'origin/master'