-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff3-interactive.rb
More file actions
executable file
·67 lines (52 loc) · 1.88 KB
/
diff3-interactive.rb
File metadata and controls
executable file
·67 lines (52 loc) · 1.88 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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
MINE = ARGV[0]
BASE = ARGV[1]
YOURS = ARGV[2]
OUT = ARGV[3]
merged = `diff3 -m #{MINE} #{BASE} #{YOURS}`.force_encoding("UTF-8").gsub(/\r\n?/,"\n")
#HACK!
#Handle bug where files without newlines at the end confuse diff
merged_tac = merged.lines.to_a.reverse.join("")
merged_tac.sub!(/\A>>>>>>>[^\n]*\n(.*?)\n(.*?)=======\n<<<<<<<[^\n]*\n/m, "\\1\n")
merged_tac.sub!(/\A(.+?)>>>>>>>[^\n]*\n=======\n([^\n]*?)\n<<<<<<<[^\n]*\n/m, "\\2\n")
merged = merged_tac.lines.to_a.reverse.join("").sub(/\n\z/,'')
def get_choice_from_user()
puts "Please choose:\n"
puts "1: #{MINE}"
puts "2: #{YOURS}"
print "\n> "
$stdin.gets.strip
end
# Modifications which only display a 2-way diff get resolved automatically
merged.gsub!(/^<<<<<<<[^\n]*\n(.*?)\n=======[^\n]*\n(.*?)\n>>>>>>>[^\n]*/m) do |match|
match =~ /^\|\|\|\|\|\|\|/ ? match : $2
end
# Modifications resulting in a 3-way diff prompt the user
merged.gsub! /^<<<<<<<[^\n]*\n(.*?)\n\|\|\|\|\|\|\|[^\n]*\n(.*?)\n=======[^\n]*\n(.*?)\n>>>>>>>[^\n]*/m do |match|
mine = $1
old = $2
yours = $3
puts "****************************** CONFLICT DETECTED! ******************************\n\n"
puts "==============================================================================="
puts "= 1: #{MINE + (" " * (72 - MINE.size))} ="
puts "==============================================================================="
puts mine + "\n\n"
puts "==============================================================================="
puts "= 2: #{YOURS + (" " * (72 - YOURS.size))} ="
puts "==============================================================================="
puts yours + "\n\n"
selected = ""
choice = get_choice_from_user
while selected.empty? do
case choice
when "1"
selected = mine
when "2"
selected = yours
else
choice = get_choice_from_user
end
end
selected
end
File.open(OUT, 'w') {|f| f.write(merged) }