-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.rb
executable file
·166 lines (138 loc) · 5.01 KB
/
helper.rb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Include diff, difffile classes
load 'diff.rb'
load 'difffile.rb'
# Inputs a and b are diffs
# 3rd argument is hash for specific checks
def PRINT_AND_EXIT(a,b,h)
if h[:stats]
if (a.stats.nil? || b.stats.nil? )
PrintDiffInfoAndStats(a,b)
raise "STATS NIL"
end
end
end
def DEBUG_LOGGER diffs_array
diffs_array.each do |diff|
puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
puts "The previous commit is \n" + diff.prev_commit_sha.to_s
puts "The next commit is \n" + diff.next_commit_sha.to_s
puts "The stats for the diff are \n" + diff.stats.to_s
puts "The size of the diff is \n" + diff.num_difffiles.to_s
diff.num_difffiles.times do |i|
difffile = diff.difffiles[i]
puts "Difffile #" + i.to_s + " :- "
puts "The patch/ actual changes are \n" + difffile.patch
end
puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
end
end
def CompareDiffStatsFor2Difffiles( a,b, a_difffile_file_name, b_difffile_file_name, options)
astats = a.stats
bstats = b.stats
# Rudimentary as already checked before
raise "Error" if astats.nil? || bstats.nil?
raise "Error" if astats[:total].nil? || bstats[:total].nil?
a_file_stats = astats[:files][a_difffile_file_name]
b_file_stats = bstats[:files][b_difffile_file_name]
return false if a_file_stats.nil? || b_file_stats.nil?
a_insertions = a_file_stats[:insertions]
a_deletions = a_file_stats[:deletions]
b_insertions = b_file_stats[:insertions]
b_deletions = b_file_stats[:deletions]
return false if ( ((a_insertions != b_deletions) || (a_deletions != b_insertions)) && options["revert"])
return false if ( ((a_insertions != b_insertions) || (a_deletions != b_deletions)) && options["cp"])
true
end
# CompareDiffStats(diff.stats, cmp_diff.stats)
# Compare 2 diffs by their stats
def CompareDiffStats( a, b)
# Rudimentary as already checked before
raise "Error" if a.nil? || b.nil?
raise "Error" if a[:total].nil? || b[:total].nil?
a_num_files = a[:total][:files]
b_num_files = b[:total][:files]
unless a_num_files == b_num_files
raise "Both stats must have the same number of files, \
as we reached here only after checking num_difffiles"
end
i = 0
stats_dont_match = false
a[:files].each do |a_file, a_file_stats|
a_insertions = a_file_stats[:insertions]
a_deletions = a_file_stats[:deletions]
# Find the stats for the same file in stats b
b[:files].each do |b_file, b_file_stats|
next if a_file != b_file
b_insertions = b_file_stats[:insertions]
b_deletions = b_file_stats[:deletions]
if a_insertions != b_deletions || a_deletions != b_insertions
stats_dont_match = true
break
end
end
if stats_dont_match == true
return false
end
end
return true
end
def GetCorrepondingDifffileInB(a_difffile_file_name, b)
is_present = false
b.difffiles.each do |b_difffile|
b_difffile_file_name = b_difffile.file_name
if a_difffile_file_name == b_difffile_file_name
is_present = true
return [is_present, b_difffile]
break
end
end
return [false, nil]
end
def ComparePatchForDifffiles(a, b, options)
a_additions = a.additions
a_deletions = a.deletions
b_additions = b.additions
b_deletions = b.deletions
if options["revert"] == true
return true if ( (a_additions == b_deletions) && (a_deletions == b_additions) )
end
if options["cp"] == true
return true if ( (a_additions == b_additions) && (a_deletions == b_deletions) )
end
return false
end
def CompareDiffPatch(a, b, options)
#PrintDiffInfoAndStats(a,b)
a_numfiles = a.num_difffiles
b_numfiles = b.num_difffiles
num_diffs_correct = 0
return false if a_numfiles == 0 || b_numfiles == 0
raise "The difffiles array can't be nil" if a.difffiles.nil? || b.difffiles.nil?
partial_diff_files = []
a.difffiles.each do |a_difffile|
a_difffile_file_name = a_difffile.file_name
raise "Error !!! The file_name can't be nil" if a_difffile_file_name.nil?
# Checkpoint #1 - The file_name must have a corresponding difffile in diff b
is_present, b_difffile = GetCorrepondingDifffileInB(a_difffile_file_name, b)
return false if is_present == false
# Enhancement for partial reverts/cherrypicks. Check if stats match for the given difffile
b_difffile_file_name = b_difffile.file_name
diff_stats_match = CompareDiffStatsFor2Difffiles(a,b, a_difffile_file_name, b_difffile_file_name, options)
next if diff_stats_match == false
# Checkpoint #2 - Compare the patches for both difffiles a and b
a_patch = a_difffile.patch
b_patch = b_difffile.patch
do_patches_match = ComparePatchForDifffiles(a_patch, b_patch, options)
if do_patches_match == false
return false
else
partial_diff_files << a_difffile_file_name
num_diffs_correct = num_diffs_correct + 1
end
end
return true if ( num_diffs_correct == a_numfiles && num_diffs_correct == b_numfiles )
# Enhancement for partial reverts/cherrypicks
if num_diffs_correct > 0
return true, partial_diff_files
end
end