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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDMwNjc1OWI5NGI5YWZjMjlhMDNmZWM0NTMzZmEzYWI3ZGUyOGM5MA==
4
+ ZDIyMjRiMGEwNTJhMWFjN2ZjZDM3OTQzYzdkY2QxN2Y2MTYzNTQyZA==
5
5
  data.tar.gz: !binary |-
6
- Y2ZhNDcyOWY3NWQ1ZDc3MDUyNzU3MGUwNGI4OWQ3MDg5N2RhZjU5ZA==
6
+ YTAwZjQyNDM0ZGNjNGM3Y2FlOTE4MTcwNDYzZWM1NjhiYjlmYzg5ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjQ4ZWE2ZjY3NWU2YTAzZDJhYmNjM2YzZDU3ZTI5ZDRkZGNkNzU1YmE1Y2Rl
10
- MmMwYTIyODc5NTA2MTZkZTM3ODVjYmE1YTU0ZGNmYjAzMTA4NDMwZjFhMmMw
11
- MmZjN2MxOWUwZGI2ODBlOWRiZGY4ZWZiYTVjMzk1NDk5N2M5ZDM=
9
+ MjgxY2M2MjI4YzE4MzYxOWQ0OTQzMTVhMWE1ZWMwNzZhMzk4ODA4MGIwN2Ji
10
+ NDEwNDk5NjkyOTFlYjcxN2JkYzI3NTJkOGI5MmQxZWE5MjRjMzE1MWNkY2U5
11
+ NWE3YWU4NDgyNDQyMWRkOTY5ZjM2OTRmZmYxNTlhNWExZWJlNzQ=
12
12
  data.tar.gz: !binary |-
13
- MTFlZjNkYjFjYTFmYmU5ODI0NmYwNDcwY2M3Mzc4MTY5YWJhMWYzMWJhMTkx
14
- YzY4YzJmODBjMzdmZTE0ZDdlZWJiZTU0ZjVkYzE1NDRiODk5OWU3NTU4ZmVl
15
- MDgwNDk2Y2ZiOTQzYTYwMTk3NzA1NTAxNzUxN2I2OTRiZDliZjk=
13
+ ZDY5MzI1MDlmNTk0MjlmZDg2OWFjZmVkNmFhNTkwMDI0MmI3NzliOWJiYjYw
14
+ MWU5NmY5MDlhZjg0YjAzMDk4YjA2MjM3NTc4ZTNmZmY3ZDEwYzQ1NzIxM2Vl
15
+ NmI0Nzk3NDQzZGYzZjg5MGY2ZmY2YTQxZTY5OTUzYmQ2NDBiNzg=
@@ -1,76 +1,78 @@
1
- class DiffParser::InlineDiff
2
- class << self
1
+ module DiffParser
2
+ module InlineDiff
3
+ class << self
3
4
 
4
- START = "#!idiff-start!#"
5
- FINISH = "#!idiff-finish!#"
5
+ START = "#!idiff-start!#"
6
+ FINISH = "#!idiff-finish!#"
6
7
 
7
- def processing diff_arr
8
- indexes = _indexes_of_changed_lines diff_arr
8
+ def processing diff_arr
9
+ indexes = _indexes_of_changed_lines diff_arr
9
10
 
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
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
- # Skip inline diff if empty line was replaced with content
16
- next if first_line == "-\n"
16
+ # Skip inline diff if empty line was replaced with content
17
+ next if first_line == "-\n"
17
18
 
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
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
- first_token = first_line[0..first_the_same_symbols][1..-1]
27
- start = first_token + START
27
+ first_token = first_line[0..first_the_same_symbols][1..-1]
28
+ start = first_token + START
28
29
 
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
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
- 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
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
- 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)
52
+ diff_arr
50
53
  end
51
- diff_arr
52
- end
53
54
 
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!(/[^\-\+]/, "#")
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
- offset = 0
62
- indexes = []
63
- while index = chain_of_first_symbols.index("#-+#", offset)
64
- indexes << index
65
- offset = index + 1
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
- def replace_markers line
71
- line.gsub!(START, "<span class='idiff'>")
72
- line.gsub!(FINISH, "</span>")
73
- line
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
@@ -1,101 +1,103 @@
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
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
- 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)
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
- if line[0] == "+"
56
- line_new += 1
57
- elsif line[0] == "-"
58
- line_old += 1
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
- line_new += 1
61
- line_old += 1
90
+ "default"
62
91
  end
63
92
  end
64
- end
65
93
 
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"
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
- def html_escape str
98
- replacements = {'&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;', "'" => '&#39;'}
99
- str.gsub(/[&"'><]/, replacements)
98
+ def html_escape str
99
+ replacements = {'&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;', "'" => '&#39;'}
100
+ str.gsub(/[&"'><]/, replacements)
101
+ end
100
102
  end
101
103
  end
@@ -1,27 +1,28 @@
1
- module DiffParser::Regex
2
- extend self
1
+ module DiffParser
2
+ module Regex
3
+ extend self
3
4
 
4
- def username_regex
5
- default_regex
6
- end
5
+ def username_regex
6
+ default_regex
7
+ end
7
8
 
8
- def project_name_regex
9
- /\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
10
- end
9
+ def project_name_regex
10
+ /\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
11
+ end
11
12
 
12
- def name_regex
13
- /\A[a-zA-Z0-9_\-\. ]*\z/
14
- end
13
+ def name_regex
14
+ /\A[a-zA-Z0-9_\-\. ]*\z/
15
+ end
15
16
 
16
- def path_regex
17
- default_regex
18
- end
17
+ def path_regex
18
+ default_regex
19
+ end
19
20
 
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
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
- %r{
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
- end
42
+ end
42
43
 
43
- protected
44
+ protected
44
45
 
45
- def default_regex
46
- /\A[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
46
+ def default_regex
47
+ /\A[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
48
+ end
47
49
  end
48
50
  end
@@ -1,3 +1,3 @@
1
1
  module DiffParser
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/diff_parser.rb CHANGED
@@ -1,6 +1,3 @@
1
1
  require 'diff_parser/inline_diff'
2
2
  require 'diff_parser/regex'
3
3
  require 'diff_parser/parser'
4
-
5
- module DiffParser
6
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diff_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - stevo