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
|
# File 'lib/hunkify/diff_parser.rb', line 7
def self.parse(raw_diff)
hunks = []
hunk_id = 0
= nil
file_path = nil
= nil
hunk_lines = []
raw_diff.each_line do |line|
line = line.chomp
line = " " if line.empty? &&
if line.start_with?("diff --git")
if && !hunk_lines.empty?
hunk_id += 1
hunks << Hunk.new(
id: hunk_id,
file_header: ,
file_path: file_path,
hunk_header: ,
lines: hunk_lines.dup
)
end
= nil
hunk_lines = []
= line
file_path = line.split(" b/").last
elsif line.start_with?("--- ", "+++ ", "index ", "new file", "deleted file", "old mode", "new mode", "rename ")
= "#{}\n#{line}"
elsif line.start_with?("@@")
if && !hunk_lines.empty?
hunk_id += 1
hunks << Hunk.new(
id: hunk_id,
file_header: ,
file_path: file_path,
hunk_header: ,
lines: hunk_lines.dup
)
end
= line
hunk_lines = []
elsif
hunk_lines << line
end
end
if && !hunk_lines.empty?
hunk_id += 1
hunks << Hunk.new(
id: hunk_id,
file_header: ,
file_path: file_path,
hunk_header: ,
lines: hunk_lines.dup
)
end
hunks
end
|