diff options
| author | Brian Gan <brgan@google.com> | 2026-07-30 23:21:22 +0000 |
|---|---|---|
| committer | gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2026-07-30 17:00:00 -0700 |
| commit | 54fa31cd844b3de887a1915755b110287a4d2bcb (patch) | |
| tree | ba113b235b162de2fd2a355d022abe822a7d7489 /project.py | |
| parent | 29b6630a6532b20fc26c7b44d8fa69fd4db390a0 (diff) | |
| download | git-repo-54fa31cd844b3de887a1915755b110287a4d2bcb.tar.gz | |
project: derive HEAD fallback from git's own default branch
When the default branch cannot be determined dynamically, repo fell back
to a hardcoded "refs/heads/master". This can point at a branch that does
not exist on the server, since projects increasingly default to "main".
Rather than swap one hardcoded name for another, ask git itself what it
would use.
Bug: 483758905
Change-Id: Ic66712acea98f8e548a2d6d8211865ee5c416b4d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/612845
Tested-by: Brian Gan <brgan@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Brian Gan <brgan@google.com>
Diffstat (limited to 'project.py')
| -rw-r--r-- | project.py | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/project.py b/project.py index 36039321f..45860d228 100644 --- a/project.py +++ b/project.py | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | import datetime | 15 | import datetime |
| 16 | import errno | 16 | import errno |
| 17 | import filecmp | 17 | import filecmp |
| 18 | import functools | ||
| 18 | import glob | 19 | import glob |
| 19 | import os | 20 | import os |
| 20 | import platform | 21 | import platform |
| @@ -4663,6 +4664,26 @@ class SyncBuffer: | |||
| 4663 | self._pending_failures = [] | 4664 | self._pending_failures = [] |
| 4664 | 4665 | ||
| 4665 | 4666 | ||
| 4667 | @functools.lru_cache(maxsize=None) | ||
| 4668 | def _DefaultBranchFallback() -> str: | ||
| 4669 | """Return the ref to use when remote default branch can't be resolved.""" | ||
| 4670 | |||
| 4671 | def _git(args: List[str]) -> str: | ||
| 4672 | p = GitCommand( | ||
| 4673 | None, | ||
| 4674 | args, | ||
| 4675 | capture_stdout=True, | ||
| 4676 | capture_stderr=True, | ||
| 4677 | log_as_error=False, | ||
| 4678 | ) | ||
| 4679 | return p.stdout.strip() if p.Wait() == 0 else "" | ||
| 4680 | |||
| 4681 | branch = _git(["var", "GIT_DEFAULT_BRANCH"]) or _git( | ||
| 4682 | ["config", "--get", "init.defaultBranch"] | ||
| 4683 | ) | ||
| 4684 | return f"refs/heads/{branch or 'master'}" | ||
| 4685 | |||
| 4686 | |||
| 4666 | class MetaProject(Project): | 4687 | class MetaProject(Project): |
| 4667 | """A special project housed under .repo.""" | 4688 | """A special project housed under .repo.""" |
| 4668 | 4689 | ||
| @@ -4676,7 +4697,7 @@ class MetaProject(Project): | |||
| 4676 | worktree=worktree, | 4697 | worktree=worktree, |
| 4677 | remote=RemoteSpec("origin"), | 4698 | remote=RemoteSpec("origin"), |
| 4678 | relpath=".repo/%s" % name, | 4699 | relpath=".repo/%s" % name, |
| 4679 | revisionExpr="refs/heads/master", | 4700 | revisionExpr=_DefaultBranchFallback(), |
| 4680 | revisionId=None, | 4701 | revisionId=None, |
| 4681 | groups=None, | 4702 | groups=None, |
| 4682 | ) | 4703 | ) |
| @@ -5134,9 +5155,9 @@ class ManifestProject(MetaProject): | |||
| 5134 | if is_new: | 5155 | if is_new: |
| 5135 | default_branch = self.ResolveRemoteHead() | 5156 | default_branch = self.ResolveRemoteHead() |
| 5136 | if default_branch is None: | 5157 | if default_branch is None: |
| 5137 | # If the remote doesn't have HEAD configured, default to | 5158 | # If the remote doesn't have HEAD configured, fall back |
| 5138 | # master. | 5159 | # to whatever git uses as its default branch. |
| 5139 | default_branch = "refs/heads/master" | 5160 | default_branch = _DefaultBranchFallback() |
| 5140 | self.revisionExpr = default_branch | 5161 | self.revisionExpr = default_branch |
| 5141 | else: | 5162 | else: |
| 5142 | self.PreSync() | 5163 | self.PreSync() |
