Created
January 13, 2011 21:32
-
-
Save chetan/778655 to your computer and use it in GitHub Desktop.
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 | |
# == Synopsis | |
# | |
# guidiff.rb [--no-compress] [file1] [file2] [...] | |
# | |
# == Usage | |
# | |
# -h, --help: | |
# show this help | |
# | |
# --no-compress, -n | |
# disable output compression (automatically disabled when using vimdiff) | |
# | |
# --vimdiff, -v | |
# use vimdiff | |
# | |
# --filemerge, -f | |
# use FileMerge.app (via opendiff) | |
# | |
# file1, file2, ... | |
# list of files to operate on. if blank, assumes current directory is to be | |
# diffed. directories can also be explicit arguments. | |
require 'tempfile' | |
require 'getoptlong' | |
require 'rdoc/usage' | |
VIMDIFF = "vimdiff" | |
OPENDIFF = "opendiff" | |
class GuiDiff | |
def initialize | |
@files = nil | |
@compress = true | |
@diff_tool = "opendiff" | |
end | |
def opts | |
opts = GetoptLong.new( | |
[ '--help', '-h', GetoptLong::NO_ARGUMENT ], | |
[ '--no-compress', '-n', GetoptLong::NO_ARGUMENT ], | |
[ '--vimdiff', '-v', GetoptLong::NO_ARGUMENT ], | |
[ '--filemerge', '-f', GetoptLong::NO_ARGUMENT ] | |
) | |
opts.each do |opt, arg| | |
case opt | |
when '--help' | |
RDoc::usage | |
when '--no-compress' | |
@compress = false | |
when '--vimdiff' | |
@diff_tool = VIMDIFF | |
when '--filemerge' | |
@diff_tool = OPENDIFF | |
end | |
end | |
if @diff_tool == VIMDIFF then | |
# disable this feature when using vimdiff | |
@compress = false | |
end | |
@paths = ARGV | |
if @paths.empty? then | |
@paths = [ File.expand_path(".") ] | |
else | |
@paths.map! { |p| File.expand_path(p) } | |
end | |
end | |
def main | |
self.opts() | |
def do_diff(f) | |
diff = `svn diff #{f}` | |
end | |
def do_header(f, o) | |
o.puts "Index: #{f}" | |
o.puts "===================================================================" | |
end | |
temp1 = Tempfile.new("guidiff-").path | |
temp2 = Tempfile.new("guidiff-").path | |
out1 = File.open(temp1, "w") | |
out2 = File.open(temp2, "w") | |
i = 0 | |
modified_files = [] | |
@paths.each do |p| | |
mp = `svn status #{p}`.split("\n").reject{ |s| s =~ /^\?/ }.map { |s| s =~ /^.\s+(.*?)$/; $1 } | |
modified_files += mp | |
end | |
modified_files.each do |f| | |
i += 1 | |
if i > 1 then | |
# add an extra blank line | |
out1.write "\n" | |
out2.write "\n" | |
end | |
if modified_files.size > 1 then | |
do_header(f, out1) | |
do_header(f, out2) | |
end | |
# include compressed output? | |
if @compress then | |
do_diff(f, out1) | |
else | |
out1.write `svn cat #{f}` | |
end | |
# current [working copy] file | |
out2.write `cat #{f}` | |
end | |
out1.close | |
out2.close | |
puts temp1 | |
puts temp2 | |
system("#{@diff_tool} #{temp1} #{temp2}") | |
end | |
end | |
GuiDiff.new.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment