Modify git map-branches to show the number of commits on a branch
Currently, for a given branch, map-branches displays the number of
commits ahead and behind it is. This change replaces ahead with
commits, representing the number of commits a branch has, starting from
its base commit, and more accurately reflects the state of a repo.
Bug:1128716
Change-Id: I7c070b4efd452d82d878e1cfb7c20d1c80f38ec7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2412991
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
diff --git a/git_common.py b/git_common.py
index 9550085..8df16a8 100644
--- a/git_common.py
+++ b/git_common.py
@@ -1036,18 +1036,21 @@
info_map = {}
data = run('for-each-ref', format_string, 'refs/heads')
BranchesInfo = collections.namedtuple(
- 'BranchesInfo', 'hash upstream ahead behind')
+ 'BranchesInfo', 'hash upstream commits behind')
for line in data.splitlines():
(branch, branch_hash, upstream_branch, tracking_status) = line.split(':')
- ahead_match = re.search(r'ahead (\d+)', tracking_status)
- ahead = int(ahead_match.group(1)) if ahead_match else None
+ commits = None
+ base = get_or_create_merge_base(branch)
+ if base:
+ commits = int(run('rev-list', '--count', branch, '^%s' % base)) or None
behind_match = re.search(r'behind (\d+)', tracking_status)
behind = int(behind_match.group(1)) if behind_match else None
info_map[branch] = BranchesInfo(
- hash=branch_hash, upstream=upstream_branch, ahead=ahead, behind=behind)
+ hash=branch_hash, upstream=upstream_branch, commits=commits,
+ behind=behind)
# Set None for upstreams which are not branches (e.g empty upstream, remotes
# and deleted upstream branches).