From 0039e390000306a76388d2d2205ed72ab6148b06 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Tue, 25 Aug 2026 08:56:44 -0700 Subject: [PATCH] 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 Reviewed-by: Brian Gan Tested-by: Gavin Mak --- project.py | 24 +++++++++++++----------- subcmds/overview.py | 13 +++++++++---- subcmds/prune.py | 2 +- tests/test_project.py | 9 +++++++++ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/project.py b/project.py index 4853c768d..26f5982da 100644 --- a/project.py +++ b/project.py @@ -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 diff --git a/subcmds/overview.py b/subcmds/overview.py index dff176238..c5fb9d36f 100644 --- a/subcmds/overview.py +++ b/subcmds/overview.py @@ -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 " ", diff --git a/subcmds/prune.py b/subcmds/prune.py index 18bfc6808..1325eceeb 100644 --- a/subcmds/prune.py +++ b/subcmds/prune.py @@ -80,7 +80,7 @@ class Prune(PagedCommand): print( "%s %-33s " % ( - branch.name == project.CurrentBranch and "*" or " ", + branch.current and "*" or " ", branch.name, ), end="", diff --git a/tests/test_project.py b/tests/test_project.py index c28af9a78..57bb8c10c 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -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."""