public
Description: A bunch of random scripts I've either written, downloaded or clipped from #git.
Homepage:
Clone URL: git://github.com/jwiegley/git-scripts.git
git-scripts / git-opendiff
100755 55 lines (49 sloc) 2.462 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env ruby
 
########################################################################################################
# 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 "opendiff '#{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)