status: Show ahead/behind info for local branches

When viewing `repo status`, it is difficult to distinguish between branches that have active unpushed changes and stale branches that are fully synced. Previously, developers had to run commands like `repo forall -c "git status"` to see their ahead/behind counts.

This change updates Project.PrintWorkTreeStatus to automatically calculate and display the number of commits a branch is ahead and/or behind its upstream tracking branch. We use `git rev-list --left-right --count` to fetch this information natively and efficiently.

If the branch is completely synced with upstream, no extra text is shown.

Added tests for ahead-only, behind-only, diverged, no-tracking, and fully-synced branch states.

Bug: 319412954
Change-Id: I23879b2d472c7a7e11d01b565428a84b1b4f09c1
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/602423
Reviewed-by: Gavin Mak <gavinmak@google.com>
Tested-by: Brian Gan <brgan@google.com>
Commit-Queue: Brian Gan <brgan@google.com>
This commit is contained in:
Brian Gan
2026-06-29 20:15:53 +00:00
committed by gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 91986011b0
commit e7cac4bca6
4 changed files with 272 additions and 3 deletions
+6
View File
@@ -54,6 +54,12 @@ project 'repo' on branch 'devwork':
project repo/ branch devwork
-m subcmds/status.py
If the branch is tracking an upstream branch, the number of commits
ahead and/or behind is also shown:
project repo/ branch devwork [ahead 1, behind 2]
-m subcmds/status.py
The first column explains how the staging area (index) differs from
the last commit (HEAD). Its values are always displayed in upper
case and have the following meanings: