Created
April 21, 2011 16:15
-
-
Save chrisroos/934890 to your computer and use it in GitHub Desktop.
This attempts to show useful differences between two repos (probably your fork and the upstream original)
This file contains 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 | |
# This attempts to show useful differences between an upstream repo and your local copy. | |
# The motivation is that I want to be able to see differences between my forks of other github repos. | |
unless upstream_repo = ARGV.shift | |
puts "Usage: #{File.basename(__FILE__)} upstream-repo" | |
exit 1 | |
end | |
`git status` | |
unless $?.success? | |
puts "Error. The 'git status' command failed. I'm guessing this isn't a git repo. Byeeee." | |
exit 1 | |
end | |
remotes = `git remote -v` | |
remotes = remotes.split("\n").collect { |remote_and_url| remote_and_url.split("\t") } | |
upstream_remotes = remotes.select { |remote, url| remote == 'upstream' } | |
if upstream_remotes.any? | |
if upstream_remotes.detect { |remote, url| url =~ /^#{upstream_repo} / } | |
warn "Warn. You have a remote named upstream that points at the same URL you've just specified. This'll probably be fine. Probably." | |
else | |
puts "Error. You already have a remote named upstream that has a different URL to the one specified. To avoid any Bad Things happening I'm going to abort. Byeeee." | |
exit 1 | |
end | |
else | |
`git remote add upstream #{upstream_repo}` | |
end | |
`git fetch upstream --quiet` | |
branches = `git branch -r` | |
branches = branches.reject { |branch| branch =~ /HEAD/ }.collect { |branch| branch.gsub(/^ */, '').sub("\n", '').split('/') } | |
origin_branches = branches.select { |remote, branch| remote == 'origin' }.collect { |remote, branch| branch } | |
upstream_branches = branches.select { |remote, branch| remote == 'upstream' }.collect { |remote, branch| branch } | |
branches_only_in_origin = origin_branches - upstream_branches | |
branches_only_in_upstream = upstream_branches - origin_branches | |
branches_in_common = origin_branches & upstream_branches | |
branch_diffs = branches_in_common.inject({}) do |hash, branch| | |
commits_only_in_upstream = `git log --pretty=oneline origin/#{branch}..upstream/#{branch}` | |
commits_only_in_origin = `git log --pretty=oneline upstream/#{branch}..origin/#{branch}` | |
hash[branch] = [commits_only_in_upstream.split("\n").length, commits_only_in_origin.split("\n").length] | |
hash | |
end | |
# Print the stats | |
COL_1_LENGTH = 20 | |
COL_2_LENGTH = 10 | |
COL_3_LENGTH = 10 | |
def in_columns(col_1='', col_2='', col_3='') | |
puts [col_1.to_s.ljust(COL_1_LENGTH), col_2.to_s.ljust(COL_2_LENGTH), col_3.to_s.ljust(COL_3_LENGTH)].join | |
end | |
puts '' | |
puts '## Branches only in the upstream repo' | |
branches_only_in_upstream.each do |branch| | |
puts "* #{branch}" | |
end | |
puts '' | |
puts '## Branches only in your repo' | |
puts '' | |
branches_only_in_origin.each do |branch| | |
puts "* #{branch}" | |
end | |
puts '' | |
puts '## Branch differences' | |
puts '' | |
in_columns '', 'Commits in' | |
in_columns 'Branch', 'Upstream', 'Origin' | |
branch_diffs.each do |branch, (commits_only_in_upstream, commits_only_in_origin)| | |
in_columns branch, commits_only_in_upstream, commits_only_in_origin | |
end | |
puts '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment