Skip to content

Instantly share code, notes, and snippets.

@dansimau
Last active April 22, 2019 04:52
Show Gist options
  • Save dansimau/7232737 to your computer and use it in GitHub Desktop.
Save dansimau/7232737 to your computer and use it in GitHub Desktop.
Report branches or commits that are not yet merged into master.
#!/usr/bin/python
"""
Report branches or commits that are not yet merged into master.
"""
import subprocess
from os.path import basename
# Merge/environment branches. These will be excluded from the
# "unmerged branches" list.
EXCLUDE_BRANCHES = ['staging', 'uat', 'production', 'master']
def run(args):
"""
Run command and capture output into list of lines.
"""
# Run command and split output
lines = subprocess.check_output(args).strip().split('\n')
# Strip spaces from each line
lines = [line.strip() for line in lines if line.strip()]
return lines
unmerged_branches = []
unmerged_singular_commits = []
# Get list of unmerged commits
unmerged_refs = run(['git', 'log', 'production', '^master', '--no-merges',
'--format=%h'])
print 'You have %r unmerged commits.' % len(unmerged_refs)
for ref in unmerged_refs:
# Get list of branches that contain this commit
branch_refs = run(['git', 'branch', '-a', '--contains', ref])
for branch in set(branch_refs):
# If this branch name is in the list of excludes, remove it
if basename(branch) in EXCLUDE_BRANCHES:
branch_refs.remove(branch)
# If this commit is in a non-deploy branch somewhere, add that branch to
# the list, otherwise this is a singular commit with no branch
if branch_refs:
for branch in branch_refs:
unmerged_branches.append(basename(branch))
else:
unmerged_singular_commits.append(ref)
# Show unmerged branches
if unmerged_branches:
print '\nUnmerged branches:'
for branch in set(unmerged_branches):
print '* %s' % basename(branch)
# Show unmerged singular commits
if unmerged_singular_commits:
print '\nUnmerged singular commits (with no branch):'
for commit in set(unmerged_singular_commits):
print(run(['git', 'log', '-1', '--format=* %h %an -- %s', commit])[0])
@dansimau
Copy link
Author

Install this by running:

sudo wget -O /usr/local/bin/git-unmerged https://gist.github.com/dansimau/7232737/raw/git-unmerged.py
sudo chmod +x /usr/local/bin/git-unmerged

Then from within a git repo simply run:

git unmerged

Et voila!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment