project: carry current state from the ref snapshot

Attach the current marker computed by GetBranches to uploadable and
prunable branches. Overview and prune reporting can then reuse that
result instead of resolving HEAD once per displayed branch.

Bug: 553599402
Change-Id: Ie3e23d08d0caf66c47cc12d7295ec4b6a834560c
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/623184
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Brian Gan <brgan@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Gavin Mak authored and gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com committed 2026-08-31 13:26:22 -07:00
1 parent 5f378458d2
commit 0039e39000
4 files changed
+32 -16

No files matched your search

+13 -11
View File
@@ -195,6 +195,10 @@ class ReviewableBranch:
def name(self):
return self.branch.name
@property
def current(self) -> bool:
return getattr(self.branch, "current", False)
@property
def commits(self):
if self._commit_cache is None:
@@ -1178,29 +1182,26 @@ class Project:
if pub_id and pub_id == head_id:
return []
rb = self.GetUploadableBranch(selected_branch)
return [rb] if rb else []
if rb:
rb.branch.current = selected_branch == self.CurrentBranch
return [rb]
return []
# Optimization: Skip scanning _allrefs (which spawns git processes)
# if no local branches with upstream tracking exist in .git/config.
if not any(self.config.GetSubSections("branch")):
return []
heads = {}
pubed = {}
for name, ref_id in self._allrefs.items():
if name.startswith(R_HEADS):
heads[name[len(R_HEADS) :]] = ref_id
elif name.startswith(R_PUB):
pubed[name[len(R_PUB) :]] = ref_id
branches = self.GetBranches()
ready = []
for branch, ref_id in heads.items():
if branch in pubed and pubed[branch] == ref_id:
for branch, branch_config in branches.items():
if branch_config.published == branch_config.revision:
continue
rb = self.GetUploadableBranch(branch)
if rb:
rb.branch.current = branch_config.current
ready.append(rb)
return ready
@@ -2521,6 +2522,7 @@ class Project:
for branch in kill:
if R_HEADS + branch in left:
branch = self.GetBranch(branch)
branch.current = branch.name == cb
base = branch.LocalMerge
if not base:
base = rev
+9 -4
View File
@@ -59,10 +59,15 @@ are displayed.
for project in self.GetProjects(
args, all_manifests=not opt.this_manifest_only
):
br = [project.GetUploadableBranch(x) for x in project.GetBranches()]
br = [x for x in br if x]
local_branches = project.GetBranches()
br = []
for name, branch in local_branches.items():
uploadable = project.GetUploadableBranch(name)
if uploadable:
uploadable.branch.current = branch.current
br.append(uploadable)
if opt.current_branch:
br = [x for x in br if x.name == project.CurrentBranch]
br = [x for x in br if x.current]
all_branches.extend(br)
if not all_branches:
@@ -97,7 +102,7 @@ are displayed.
print(
"%s %-33s (%2d commit%s, %s)"
% (
branch.name == project.CurrentBranch and "*" or " ",
branch.current and "*" or " ",
branch.name,
len(commits),
len(commits) != 1 and "s" or " ",
+1 -1
View File
@@ -80,7 +80,7 @@ class Prune(PagedCommand):
print(
"%s %-33s "
% (
branch.name == project.CurrentBranch and "*" or " ",
branch.current and "*" or " ",
branch.name,
),
end="",
+9
View File
@@ -111,6 +111,15 @@ class ReviewableBranchTests(unittest.TestCase):
_, kwargs = proj.UploadForReview.call_args
self.assertEqual(kwargs.get("git_event_log"), mock_event_log)
def test_current_comes_from_branch_snapshot(self) -> None:
"""Reviewable branches carry current state into report consumers."""
branch = mock.MagicMock()
branch.current = True
rb = project.ReviewableBranch(mock.MagicMock(), branch, "main")
self.assertTrue(rb.current)
class ProjectTests(unittest.TestCase):
"""Check Project behavior."""