Created
August 21, 2010 07:04
-
-
Save igal/541915 to your computer and use it in GitHub Desktop.
git-meld
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 | |
# FIXME provide flexible way of choosing tool | |
DIFF = ENV['DIFF'] || 'meld' | |
######################################################################################################## | |
# Usage: # | |
# gitopendiff [-r<left rev>:<right-rev>] [repository] # | |
# # | |
# Example: # | |
# gitopendiff # == gitopendiff -rHEAD^:HEAD . # | |
# gitopendiff -rHEAD^^:HEAD # == gitopendiff -rHEAD^^:HEAD . # | |
# gitopendiff -master:origin/master # == gitopendiff -rmaster:origin/master . # | |
# gitopendiff /path/to/git/repository # == gitopendiff -rHEAD^:HEAD /path/to/git/repository # | |
# gitopendiff -rHEAD^:HEAD /path/to/git/repository # == gitopendiff -rHEAD^:HEAD /path/to/git/repository # | |
# # | |
# Authored by Simon Menke, with minor optimizations by Jonathan del Strother # | |
######################################################################################################## | |
def checkout(origin, rev) | |
tmp = "/tmp/git_opendiff_#{rand}.tmp/" | |
Dir.mkdir(tmp) | |
Dir.chdir(origin) do | |
# The clean, porcelainy way would be "git archive --format=tar '#{rev}' | (cd #{tmp} && tar xf -)" | |
# I've found the plumbing to be marginally faster : | |
system "git read-tree '#{rev}'; git checkout-index --prefix=#{tmp} -a; git read-tree HEAD" | |
end | |
return tmp | |
end | |
def diff(origin, revs={}) | |
revs = {:left=>"HEAD^",:right=>"HEAD"}.merge(revs) | |
left = checkout origin, revs[:left] | |
right = checkout origin, revs[:right] | |
system "#{DIFF} '#{left}' '#{right}'" | |
end | |
revs = {} | |
git_dir = Dir.pwd | |
ARGV.each do |arg| | |
if arg[0,2] == "-r" | |
rev_strings = arg[2..-1].split(':') | |
case rev_strings.size | |
when 1 | |
revs[:left] = rev_strings[0] unless rev_strings[0].empty? | |
when 2 | |
revs[:left] = rev_strings[0] unless rev_strings[0].empty? | |
revs[:right] = rev_strings[1] unless rev_strings[1].empty? | |
else | |
revs = nil | |
end | |
else | |
git_dir = arg unless arg.empty? | |
end | |
end | |
diff(git_dir, revs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment