Skip to content

Instantly share code, notes, and snippets.

@dansimau
Last active April 22, 2019 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

Example output:

$ ./unmerged.py
You have 25 unmerged commits.

Unmerged branches:
* img_width_events
* fcc_25_broken_links
* fcc-510-services-select-fix
* 2836-destinations-spring-titles
* settings_screen_tab_closed_fix
* cookie_banner_position_absolute
* 2656_accordian_fault
* ladda-temp-fix
* develop

Unmerged singular commits (with no branch):
* b3d467c  Daniel Simmons -- Refactor line_updates method.
* 49dc8a4  Ian Taylor -- Removing ticket refunds link
* ffafed1  Ian Taylor -- Define LHMenu option for live train updates
* 7d57d67  Ian Taylor -- Make the list marker appear in the right place when the item copy wraps
* 65271f4  ian.taylor -- Try and make static_management work...
* 02b2211  Ian Taylor -- Fixing datetime issue on engineering drafts
* 9033ad3  Ian Taylor -- Change destination h2 titles to summer
* b3f1548  Ian Taylor -- Adding a manage command to export lost property submissions
* 63264d9  Ian Taylor -- CSS change for small images on events pages
* 1777dda  Ian Taylor -- Make the lost property form send submissions via email
* 0beaf8d  Ian Taylor -- Removing another tickets incident link
* 7faa0f8  Ian Taylor -- Adding in some to-email dubigging for sporadic NRE feed problems
* 82d1f69  Ian Taylor -- Stopping the NRE feed error emails
* a368ccd  Daniel Simmons -- Fix cookie date issue

@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