Skip to content

Instantly share code, notes, and snippets.

@marcusbuffett
Created June 19, 2019 19:19
Show Gist options
  • Save marcusbuffett/67c5cd0365c03d4740874bf0b3ac2aae to your computer and use it in GitHub Desktop.
Save marcusbuffett/67c5cd0365c03d4740874bf0b3ac2aae to your computer and use it in GitHub Desktop.
Finds branches which are ahead of their remote tracking branch
import os
from subprocess import Popen, PIPE, check_output, STDOUT, CalledProcessError
import shlex
list_branches_command = "git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short)'"
pipe = Popen(list_branches_command, shell=True, stdout=PIPE)
for branch in pipe.stdout:
branch = branch.strip().decode("utf-8")
get_remote_command = f"git rev-parse --abbrev-ref --symbolic-full-name {branch}@{{u}}"
try:
remote_branch = check_output(shlex.split(get_remote_command), stderr=STDOUT).decode().strip()
commit_diff_command = f"git log --pretty=oneline {remote_branch}..{branch}"
commits = check_output(shlex.split(commit_diff_command), stderr=STDOUT) \
.decode().strip().split("\n")
if commits == ['']:
# No difference between local and remote
continue
print(f"{branch} is ahead of its remote, {remote_branch}")
if len(commits) > 3:
length = len(commits)
commits = commits[:3]
commits.append(f"{length - 3} more...")
print("\n".join(commits))
print("")
except CalledProcessError:
# This will happen if there is no remote branch
# print(f"There is no upstream tracking branch for {branch}")
pass
redacted is ahead of its remote, origin/redacted
36b36038fcb11b9022b4e673c646c03bc7575514 <Redacted>
f3c381213f49dd000dfdcbec3a9b65e8b3304e7e <Redacted>
redacted is ahead of its remote, origin/redacted
b56d6a7bcaead9fb0fe74d74f1fb246fe4d5d298 <Redacted>
f26a0ac781e991719073015f60f71f9055310cd4 <Redacted>
7b0c1a6ff87b781c76744ba7d404106fd7751db5 <Redacted>
2 more...
redacted is ahead of its remote, origin/redacted
87b6f2301ece52d855b0cbce01460d7a8f99b1e1 <Redacted>
4b3af3005177116037f3198b50a812d5942613a2 <Redacted>
615a021c0a9a3a6afa6ab71094c3bc766ae29d80 <Redacted>
14 more...
redacted is ahead of its remote, origin/redacted
7c5f37915492e9da244d05d8eb5a0c5bbdf4565c <Redacted>
redacted is ahead of its remote, origin/redacted
d4ac86ac5d343ba0e20d6d03a02d4744a57bf8e5 <Redacted>
redacted is ahead of its remote, origin/redacted
a74266cbcfdb8beabe8b88b055409e72b5f0e91b <Redacted>
c8398fee18351072088d5eaecabfaad00749f750 <Redacted>
f358d2991c53580feeb505b022aa59492293adcb <Redacted>
11 more...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment