diff_parser 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/diff_parser/inline_diff.rb +76 -0
- data/lib/diff_parser/parser.rb +101 -0
- data/lib/diff_parser/regex.rb +48 -0
- data/lib/diff_parser/version.rb +3 -0
- data/lib/diff_parser.rb +3 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTQwOTNiZThmMzU2M2Q1MTIyM2IzMDIyZDIxOGU4N2M5ODVkZWQzNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmQwY2VkZmNmYTYxZWJkZTY1MDBjZGIxMDQ5MzJmZDEwZjU4MmUzOQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzQ5ODU5MGZlY2E0Y2U1NzRiOWQ4MDNiMjEzY2ViOGZkZDhkYzgzODUxNjI3
|
10
|
+
NjEyNGY3NGNlZjMxOWI1NDJiNjMwYTBhOTg0YmZiYzY1NzQ1MzYyYjJiNGU2
|
11
|
+
ZjgzMWZmZDdhYjQ1MDdkODIzM2ExZmViYzRlNWNjMjQ1NGRiN2Q=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTRjYmE3ODBiZWZhNTE5MTlkZjA5ZDY5OGVhMjIwMzJhYjllN2ViZDBmM2E2
|
14
|
+
NGVlNzYwNDk0MWNiYTE2MTQ3NDg5OGQ3OTc5ZjQ5NDdhYzU1YjZlZmU5MGQ3
|
15
|
+
ZDhlM2NkZTgzZmFmYTFlMWRhNjMyZTY5NDU0ODE3MWFjNDA5MTE=
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class DiffParser::InlineDiff
|
2
|
+
class << self
|
3
|
+
|
4
|
+
START = "#!idiff-start!#"
|
5
|
+
FINISH = "#!idiff-finish!#"
|
6
|
+
|
7
|
+
def processing diff_arr
|
8
|
+
indexes = _indexes_of_changed_lines diff_arr
|
9
|
+
|
10
|
+
indexes.each do |index|
|
11
|
+
first_line = diff_arr[index+1]
|
12
|
+
second_line = diff_arr[index+2]
|
13
|
+
max_length = [first_line.size, second_line.size].max
|
14
|
+
|
15
|
+
# Skip inline diff if empty line was replaced with content
|
16
|
+
next if first_line == "-\n"
|
17
|
+
|
18
|
+
first_the_same_symbols = 0
|
19
|
+
(0..max_length + 1).each do |i|
|
20
|
+
first_the_same_symbols = i - 1
|
21
|
+
if first_line[i] != second_line[i] && i > 0
|
22
|
+
break
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
first_token = first_line[0..first_the_same_symbols][1..-1]
|
27
|
+
start = first_token + START
|
28
|
+
|
29
|
+
if first_token.empty?
|
30
|
+
# In case if we remove string of spaces in commit
|
31
|
+
diff_arr[index+1].sub!("-", "-" => "-#{START}")
|
32
|
+
diff_arr[index+2].sub!("+", "+" => "+#{START}")
|
33
|
+
else
|
34
|
+
diff_arr[index+1].sub!(first_token, first_token => start)
|
35
|
+
diff_arr[index+2].sub!(first_token, first_token => start)
|
36
|
+
end
|
37
|
+
|
38
|
+
last_the_same_symbols = 0
|
39
|
+
(1..max_length + 1).each do |i|
|
40
|
+
last_the_same_symbols = -i
|
41
|
+
shortest_line = second_line.size > first_line.size ? first_line : second_line
|
42
|
+
if (first_line[-i] != second_line[-i]) || "#{first_token}#{START}".size == shortest_line[1..-i].size
|
43
|
+
break
|
44
|
+
end
|
45
|
+
end
|
46
|
+
last_the_same_symbols += 1
|
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)
|
50
|
+
end
|
51
|
+
diff_arr
|
52
|
+
end
|
53
|
+
|
54
|
+
def _indexes_of_changed_lines diff_arr
|
55
|
+
chain_of_first_symbols = ""
|
56
|
+
diff_arr.each_with_index do |line, i|
|
57
|
+
chain_of_first_symbols += line[0]
|
58
|
+
end
|
59
|
+
chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
|
60
|
+
|
61
|
+
offset = 0
|
62
|
+
indexes = []
|
63
|
+
while index = chain_of_first_symbols.index("#-+#", offset)
|
64
|
+
indexes << index
|
65
|
+
offset = index + 1
|
66
|
+
end
|
67
|
+
indexes
|
68
|
+
end
|
69
|
+
|
70
|
+
def replace_markers line
|
71
|
+
line.gsub!(START, "<span class='idiff'>")
|
72
|
+
line.gsub!(FINISH, "</span>")
|
73
|
+
line
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
class DiffParser::Parser
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
GIT_NEW_FILE = "new file"
|
5
|
+
GIT_DELETED_FILE = "deleted file"
|
6
|
+
GIT_DIFF_START = "+#!idiff-start!#++"
|
7
|
+
GIT_DIFF_FINISH = "-#!idiff-start!#--"
|
8
|
+
GIT_INDEX = "index "
|
9
|
+
GIT_DIFF = "diff --git "
|
10
|
+
GIT_WARN_NEW_LINE = " No newline at end of file"
|
11
|
+
GIT_CODE_ADDED = "+"
|
12
|
+
GIT_CODE_REMOVED = "-"
|
13
|
+
|
14
|
+
attr_reader :lines, :new_path
|
15
|
+
|
16
|
+
def initialize(diff)
|
17
|
+
@commit_id = diff.commit_id
|
18
|
+
@lines = diff.lines.to_a
|
19
|
+
@new_path = ""
|
20
|
+
#@new_path = diff.new_path
|
21
|
+
end
|
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
|
44
|
+
|
45
|
+
yield(full_line, type, nil, nil, nil)
|
46
|
+
next
|
47
|
+
else
|
48
|
+
type = identification_type(line)
|
49
|
+
line_code = generate_line_code(new_path, line_new, line_old, type, idx)
|
50
|
+
next if ["index"].include? type
|
51
|
+
yield(full_line, type, line_code, line_new, line_old, raw_line)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
if line[0] == "+"
|
56
|
+
line_new += 1
|
57
|
+
elsif line[0] == "-"
|
58
|
+
line_old += 1
|
59
|
+
else
|
60
|
+
line_new += 1
|
61
|
+
line_old += 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
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"
|
90
|
+
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
|
+
def html_escape str
|
98
|
+
replacements = {'&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => '''}
|
99
|
+
str.gsub(/[&"'><]/, replacements)
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module DiffParser::Regex
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def username_regex
|
5
|
+
default_regex
|
6
|
+
end
|
7
|
+
|
8
|
+
def project_name_regex
|
9
|
+
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
|
10
|
+
end
|
11
|
+
|
12
|
+
def name_regex
|
13
|
+
/\A[a-zA-Z0-9_\-\. ]*\z/
|
14
|
+
end
|
15
|
+
|
16
|
+
def path_regex
|
17
|
+
default_regex
|
18
|
+
end
|
19
|
+
|
20
|
+
def git_reference_regex
|
21
|
+
# Valid git ref regex, see:
|
22
|
+
# https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
23
|
+
|
24
|
+
%r{
|
25
|
+
(?!
|
26
|
+
# doesn't begins with
|
27
|
+
\/| # (rule #6)
|
28
|
+
# doesn't contain
|
29
|
+
.*(?:
|
30
|
+
[\/.]\.| # (rule #1,3)
|
31
|
+
\/\/| # (rule #6)
|
32
|
+
@\{| # (rule #8)
|
33
|
+
\\ # (rule #9)
|
34
|
+
)
|
35
|
+
)
|
36
|
+
[^\000-\040\177~^:?*\[]+ # (rule #4-5)
|
37
|
+
# doesn't end with
|
38
|
+
(?<!\.lock) # (rule #1)
|
39
|
+
(?<![\/.]) # (rule #6-7)
|
40
|
+
}x
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def default_regex
|
46
|
+
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
|
47
|
+
end
|
48
|
+
end
|
data/lib/diff_parser.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: diff_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- stevo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allows parsing diffs for presentation purposes - extracted from marvelous
|
14
|
+
GitLab project
|
15
|
+
email:
|
16
|
+
- blazejek@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/diff_parser.rb
|
22
|
+
- lib/diff_parser/inline_diff.rb
|
23
|
+
- lib/diff_parser/parser.rb
|
24
|
+
- lib/diff_parser/regex.rb
|
25
|
+
- lib/diff_parser/version.rb
|
26
|
+
homepage: https://github.com/Selleo/diff_parser
|
27
|
+
licenses: []
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project: ! '[none]'
|
45
|
+
rubygems_version: 2.1.10
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: GitLab Diff Parser
|
49
|
+
test_files: []
|