diff_parser 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/diff_parser/inline_diff.rb +59 -57
- data/lib/diff_parser/parser.rb +92 -90
- data/lib/diff_parser/regex.rb +24 -22
- data/lib/diff_parser/version.rb +1 -1
- data/lib/diff_parser.rb +0 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDIyMjRiMGEwNTJhMWFjN2ZjZDM3OTQzYzdkY2QxN2Y2MTYzNTQyZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTAwZjQyNDM0ZGNjNGM3Y2FlOTE4MTcwNDYzZWM1NjhiYjlmYzg5ZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MjgxY2M2MjI4YzE4MzYxOWQ0OTQzMTVhMWE1ZWMwNzZhMzk4ODA4MGIwN2Ji
|
10
|
+
NDEwNDk5NjkyOTFlYjcxN2JkYzI3NTJkOGI5MmQxZWE5MjRjMzE1MWNkY2U5
|
11
|
+
NWE3YWU4NDgyNDQyMWRkOTY5ZjM2OTRmZmYxNTlhNWExZWJlNzQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDY5MzI1MDlmNTk0MjlmZDg2OWFjZmVkNmFhNTkwMDI0MmI3NzliOWJiYjYw
|
14
|
+
MWU5NmY5MDlhZjg0YjAzMDk4YjA2MjM3NTc4ZTNmZmY3ZDEwYzQ1NzIxM2Vl
|
15
|
+
NmI0Nzk3NDQzZGYzZjg5MGY2ZmY2YTQxZTY5OTUzYmQ2NDBiNzg=
|
@@ -1,76 +1,78 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module DiffParser
|
2
|
+
module InlineDiff
|
3
|
+
class << self
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
START = "#!idiff-start!#"
|
6
|
+
FINISH = "#!idiff-finish!#"
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def processing diff_arr
|
9
|
+
indexes = _indexes_of_changed_lines diff_arr
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
indexes.each do |index|
|
12
|
+
first_line = diff_arr[index+1]
|
13
|
+
second_line = diff_arr[index+2]
|
14
|
+
max_length = [first_line.size, second_line.size].max
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
# Skip inline diff if empty line was replaced with content
|
17
|
+
next if first_line == "-\n"
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
first_the_same_symbols = 0
|
20
|
+
(0..max_length + 1).each do |i|
|
21
|
+
first_the_same_symbols = i - 1
|
22
|
+
if first_line[i] != second_line[i] && i > 0
|
23
|
+
break
|
24
|
+
end
|
23
25
|
end
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
first_token = first_line[0..first_the_same_symbols][1..-1]
|
28
|
+
start = first_token + START
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
if first_token.empty?
|
31
|
+
# In case if we remove string of spaces in commit
|
32
|
+
diff_arr[index+1].sub!("-", "-" => "-#{START}")
|
33
|
+
diff_arr[index+2].sub!("+", "+" => "+#{START}")
|
34
|
+
else
|
35
|
+
diff_arr[index+1].sub!(first_token, first_token => start)
|
36
|
+
diff_arr[index+2].sub!(first_token, first_token => start)
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
last_the_same_symbols = 0
|
40
|
+
(1..max_length + 1).each do |i|
|
41
|
+
last_the_same_symbols = -i
|
42
|
+
shortest_line = second_line.size > first_line.size ? first_line : second_line
|
43
|
+
if (first_line[-i] != second_line[-i]) || "#{first_token}#{START}".size == shortest_line[1..-i].size
|
44
|
+
break
|
45
|
+
end
|
44
46
|
end
|
47
|
+
last_the_same_symbols += 1
|
48
|
+
last_token = first_line[last_the_same_symbols..-1]
|
49
|
+
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
50
|
+
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
45
51
|
end
|
46
|
-
|
47
|
-
last_token = first_line[last_the_same_symbols..-1]
|
48
|
-
diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
49
|
-
diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
|
52
|
+
diff_arr
|
50
53
|
end
|
51
|
-
diff_arr
|
52
|
-
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def _indexes_of_changed_lines diff_arr
|
56
|
+
chain_of_first_symbols = ""
|
57
|
+
diff_arr.each_with_index do |line, i|
|
58
|
+
chain_of_first_symbols += line[0]
|
59
|
+
end
|
60
|
+
chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
62
|
+
offset = 0
|
63
|
+
indexes = []
|
64
|
+
while index = chain_of_first_symbols.index("#-+#", offset)
|
65
|
+
indexes << index
|
66
|
+
offset = index + 1
|
67
|
+
end
|
68
|
+
indexes
|
66
69
|
end
|
67
|
-
indexes
|
68
|
-
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
def replace_markers line
|
72
|
+
line.gsub!(START, "<span class='idiff'>")
|
73
|
+
line.gsub!(FINISH, "</span>")
|
74
|
+
line
|
75
|
+
end
|
74
76
|
end
|
75
77
|
end
|
76
78
|
end
|
data/lib/diff_parser/parser.rb
CHANGED
@@ -1,101 +1,103 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
def each
|
24
|
-
line_old = 1
|
25
|
-
line_new = 1
|
26
|
-
|
27
|
-
lines_arr = DiffParser::InlineDiff.processing lines
|
28
|
-
lines_arr.each_with_index do |line, idx|
|
29
|
-
raw_line = line.dup
|
30
|
-
|
31
|
-
next if line.match(/^\-\-\- \/dev\/null/)
|
32
|
-
next if line.match(/^\+\+\+ \/dev\/null/)
|
33
|
-
next if line.match(/^\-\-\- a/)
|
34
|
-
next if line.match(/^\+\+\+ b/)
|
35
|
-
|
36
|
-
full_line = html_escape(line.gsub(/\n/, ''))
|
37
|
-
full_line = DiffParser::InlineDiff.replace_markers full_line
|
38
|
-
|
39
|
-
if line.match(/^@@ -/)
|
40
|
-
type = "match"
|
41
|
-
|
42
|
-
line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
|
43
|
-
line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
|
1
|
+
module DiffParser
|
2
|
+
class Parser
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
GIT_NEW_FILE = "new file"
|
6
|
+
GIT_DELETED_FILE = "deleted file"
|
7
|
+
GIT_DIFF_START = "+#!idiff-start!#++"
|
8
|
+
GIT_DIFF_FINISH = "-#!idiff-start!#--"
|
9
|
+
GIT_INDEX = "index "
|
10
|
+
GIT_DIFF = "diff --git "
|
11
|
+
GIT_WARN_NEW_LINE = " No newline at end of file"
|
12
|
+
GIT_CODE_ADDED = "+"
|
13
|
+
GIT_CODE_REMOVED = "-"
|
14
|
+
|
15
|
+
attr_reader :lines, :new_path
|
16
|
+
|
17
|
+
def initialize(diff)
|
18
|
+
@commit_id = diff.commit_id
|
19
|
+
@lines = diff.lines.to_a
|
20
|
+
@new_path = ""
|
21
|
+
#@new_path = diff.new_path
|
22
|
+
end
|
44
23
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
24
|
+
def each
|
25
|
+
line_old = 1
|
26
|
+
line_new = 1
|
27
|
+
|
28
|
+
lines_arr = DiffParser::InlineDiff.processing lines
|
29
|
+
lines_arr.each_with_index do |line, idx|
|
30
|
+
raw_line = line.dup
|
31
|
+
|
32
|
+
next if line.match(/^\-\-\- \/dev\/null/)
|
33
|
+
next if line.match(/^\+\+\+ \/dev\/null/)
|
34
|
+
next if line.match(/^\-\-\- a/)
|
35
|
+
next if line.match(/^\+\+\+ b/)
|
36
|
+
|
37
|
+
full_line = html_escape(line.gsub(/\n/, ''))
|
38
|
+
full_line = DiffParser::InlineDiff.replace_markers full_line
|
39
|
+
|
40
|
+
if line.match(/^@@ -/)
|
41
|
+
type = "match"
|
42
|
+
|
43
|
+
line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
|
44
|
+
line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
|
45
|
+
|
46
|
+
yield(full_line, type, nil, nil, nil)
|
47
|
+
next
|
48
|
+
else
|
49
|
+
type = identification_type(line)
|
50
|
+
line_code = generate_line_code(new_path, line_new, line_old, type, idx)
|
51
|
+
next if ["index"].include? type
|
52
|
+
yield(full_line, type, line_code, line_new, line_old, raw_line)
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
if line[0] == "+"
|
57
|
+
line_new += 1
|
58
|
+
elsif line[0] == "-"
|
59
|
+
line_old += 1
|
60
|
+
else
|
61
|
+
line_new += 1
|
62
|
+
line_old += 1
|
63
|
+
end
|
52
64
|
end
|
65
|
+
end
|
53
66
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
67
|
+
private
|
68
|
+
|
69
|
+
def identification_type(line)
|
70
|
+
line = line.strip
|
71
|
+
if line.start_with? GIT_NEW_FILE
|
72
|
+
"file_created"
|
73
|
+
elsif line.start_with? GIT_DELETED_FILE
|
74
|
+
"file_removed"
|
75
|
+
elsif line.start_with? GIT_DIFF_START
|
76
|
+
"file_update_created"
|
77
|
+
elsif line.start_with? GIT_DIFF_FINISH
|
78
|
+
"file_update_removed"
|
79
|
+
elsif line.start_with? GIT_INDEX
|
80
|
+
"index"
|
81
|
+
elsif line.start_with? GIT_DIFF
|
82
|
+
"info"
|
83
|
+
elsif line.start_with? GIT_WARN_NEW_LINE
|
84
|
+
"warning"
|
85
|
+
elsif line.start_with? GIT_CODE_ADDED
|
86
|
+
"new"
|
87
|
+
elsif line.start_with? GIT_CODE_REMOVED
|
88
|
+
"old"
|
59
89
|
else
|
60
|
-
|
61
|
-
line_old += 1
|
90
|
+
"default"
|
62
91
|
end
|
63
92
|
end
|
64
|
-
end
|
65
93
|
|
66
|
-
|
67
|
-
|
68
|
-
def identification_type(line)
|
69
|
-
line = line.strip
|
70
|
-
if line.start_with? GIT_NEW_FILE
|
71
|
-
"file_created"
|
72
|
-
elsif line.start_with? GIT_DELETED_FILE
|
73
|
-
"file_removed"
|
74
|
-
elsif line.start_with? GIT_DIFF_START
|
75
|
-
"file_update_created"
|
76
|
-
elsif line.start_with? GIT_DIFF_FINISH
|
77
|
-
"file_update_removed"
|
78
|
-
elsif line.start_with? GIT_INDEX
|
79
|
-
"index"
|
80
|
-
elsif line.start_with? GIT_DIFF
|
81
|
-
"info"
|
82
|
-
elsif line.start_with? GIT_WARN_NEW_LINE
|
83
|
-
"warning"
|
84
|
-
elsif line.start_with? GIT_CODE_ADDED
|
85
|
-
"new"
|
86
|
-
elsif line.start_with? GIT_CODE_REMOVED
|
87
|
-
"old"
|
88
|
-
else
|
89
|
-
"default"
|
94
|
+
def generate_line_code(path, line_new, line_old, type, idx)
|
95
|
+
"#{@commit_id}_#{line_old}_#{line_new}_#{type}_#{idx}"
|
90
96
|
end
|
91
|
-
end
|
92
|
-
|
93
|
-
def generate_line_code(path, line_new, line_old, type, idx)
|
94
|
-
"#{@commit_id}_#{line_old}_#{line_new}_#{type}_#{idx}"
|
95
|
-
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
def html_escape str
|
99
|
+
replacements = {'&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => '''}
|
100
|
+
str.gsub(/[&"'><]/, replacements)
|
101
|
+
end
|
100
102
|
end
|
101
103
|
end
|
data/lib/diff_parser/regex.rb
CHANGED
@@ -1,27 +1,28 @@
|
|
1
|
-
module DiffParser
|
2
|
-
|
1
|
+
module DiffParser
|
2
|
+
module Regex
|
3
|
+
extend self
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def username_regex
|
6
|
+
default_regex
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def project_name_regex
|
10
|
+
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def name_regex
|
14
|
+
/\A[a-zA-Z0-9_\-\. ]*\z/
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def path_regex
|
18
|
+
default_regex
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def git_reference_regex
|
22
|
+
# Valid git ref regex, see:
|
23
|
+
# https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
23
24
|
|
24
|
-
|
25
|
+
%r{
|
25
26
|
(?!
|
26
27
|
# doesn't begins with
|
27
28
|
\/| # (rule #6)
|
@@ -38,11 +39,12 @@ module DiffParser::Regex
|
|
38
39
|
(?<!\.lock) # (rule #1)
|
39
40
|
(?<![\/.]) # (rule #6-7)
|
40
41
|
}x
|
41
|
-
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
+
protected
|
44
45
|
|
45
|
-
|
46
|
-
|
46
|
+
def default_regex
|
47
|
+
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
|
48
|
+
end
|
47
49
|
end
|
48
50
|
end
|
data/lib/diff_parser/version.rb
CHANGED
data/lib/diff_parser.rb
CHANGED