Created
March 1, 2013 18:03
-
-
Save rcarlsen/5066510 to your computer and use it in GitHub Desktop.
ruby script for tallying "orphaned" branches in a git-based repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# in this project, the branching convention is that feature development | |
# and fixes are worked on in branches prefixed with feature/ and fix/ respectively. | |
# These branches are intended to be short-lived, only kept on the remote repo long | |
# enough for collaboration and eventual Pull Request approval/merging into the | |
# develop branch. After merging, the remote branch should be removed, ideally by the | |
# repo management software (Stash, github, etc.). If the management software does | |
# not support automatic deletion of the branch then it is the responsibility of | |
# the "owner" of the remote branch to delete it--this is generally the creator | |
# of the branch or the developer who primarily worked on the task. | |
# | |
# This script will display a tally by author of the remote branches in the | |
# feature/* and fix/* namespace. It will then print each branch and the oneline | |
# commit message for the HEAD of each branch, grouped by author. | |
# | |
# There are opportunities for "false postives"; specifically, the last author of | |
# a commit on a branch may not be the "owner"--eg. another developer appends a commit | |
# to an existing branch during the pull request process, or branches which have not yet | |
# been merged and correctly remain on the remote repo. Also, some authors may be listed | |
# by multiple e-mail addresses. This seems to be due to different local and Stash | |
# configurations in user information. Future enhancement of this script | |
# could address these issues. | |
# | |
# usage: | |
# * cd into the local repo, anywhere in the repo. | |
# * be sure to update the remote branch, pruning already deleted branches: | |
# ** $ git fetch --prune origin | |
# $ ruby branch-blame.rb | |
# list all branches then filter just the remote feature/* and fix/* branches (assuming "origin" as canonical): | |
branches = `git branch -a|grep -E "origin/(feature|fix|update)"` | |
# trim any leading whitespace: | |
branches = branches.split( /\r?\n/ ).map(&:lstrip) | |
#puts branches | |
# get the HEAD commit for each branch: | |
commits = branches.map {|b| `git log -n1 --pretty="%h %cd (%an) %s" --date=relative #{b}`} | |
#puts commits | |
# get author for each HEAD commit: | |
perps = branches.map {|b| `git log -n1 --pretty="%an" #{b}`} | |
#puts perps.sort | |
# create an array to hold the hash-based commits: | |
perpsWithMessages = [] | |
for i in 0..perps.length-1 | |
perpsWithMessages.push(Hash[:name, perps[i], | |
:commit, commits[i], | |
:branch, branches[i] ]) | |
end | |
# print the count of remotes: | |
puts "Likely orphaned branches: " + branches.length.to_s + "\n\n" | |
# sort the branches by author name: | |
if Array.method_defined? :sort_by! | |
perpsWithMessages.sort_by! { |hsh| hsh[:name] } # seems only supported on ruby v1.9+ (not Mac OS X default) | |
else | |
perpsWithMessages = perpsWithMessages.sort_by { |hsh| hsh[:name] } # did not seem to sort correctly...non-deterministic. | |
end | |
# print the tally of branches by author name: | |
perpsWithMessages.group_by {|x| x[:name] }.each {|i| | |
puts "(#{i[1].length})\t" + i[0] | |
} | |
puts "\n\n" | |
# print the full list of commits/branches, grouped by author name: | |
perpsWithMessages.group_by {|x| x[:name] }.each {|i| | |
# first, print the author name: | |
puts "(#{i[1].length}) " + i[0] | |
# then HEAD commits for each branch | |
i[1].each {|c| puts c[:branch], c[:commit], "\n" } | |
puts "\n" | |
} | |
# all done: | |
exit 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment