diff_parser 0.1.0 → 1.0.0
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/commit.rb +25 -0
- data/lib/diff_parser/diff.rb +14 -0
- data/lib/diff_parser/diff_files_collection.rb +34 -0
- data/lib/diff_parser/diff_line.rb +10 -16
- data/lib/diff_parser/file.rb +54 -0
- data/lib/diff_parser/raw_line.rb +91 -0
- data/lib/diff_parser/version.rb +1 -1
- data/lib/diff_parser.rb +5 -2
- metadata +7 -4
- data/lib/diff_parser/parser.rb +0 -104
- data/lib/diff_parser/regex.rb +0 -50
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjkxM2Q3NjAzODMyYmEwYzI2MGVhZTBjZmY1MTc1YTViN2E0ZGE0Yw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2VhNzliYzNkYmFkZThlNGM4ZDQ2OWE1ODdlNjZmZTliNGIxMDI5NA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWFkMjQzNTU1NTJkMTEwYjE2MjU4OGZlNzk2ZWNiMjk0MjdkODVlMzBkMTFh
|
10
|
+
YzE2YmFlNGU3ZDY3MWMxMDZiNzRlZDk0Y2I0YTdkNTlhN2E0NDY4Y2I3ODNi
|
11
|
+
MjhiNmU3NzAzNTRjZjUwNmU0ZmZjZmMwNWMyYmIwODkzMmE2ZTU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MmIxMTdmNDRiMmZkMzgwNTQ2NTA0NTllZDRlMmM0NDc3MzMzYzRkYzNlZDQ4
|
14
|
+
ODM5M2Y5N2NmYjNiYjQzYjI5YTc0NjFhNTU4MzM3NTdjNzhkYzU0YmJhYmM3
|
15
|
+
NzhhOTI5ZWYwMzVhMjVhMTAwOWUzYTJmODIzMWMyYTkxZTllNjY=
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module DiffParser
|
2
|
+
class Commit
|
3
|
+
attr_reader :github, :repo, :sha
|
4
|
+
|
5
|
+
def initialize(github, repo, sha)
|
6
|
+
@github = github
|
7
|
+
@repo = repo
|
8
|
+
@sha = sha
|
9
|
+
end
|
10
|
+
|
11
|
+
def diff
|
12
|
+
DiffParser::Diff.new(raw, info)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def info
|
18
|
+
@info ||= github.commit(repo, sha)
|
19
|
+
end
|
20
|
+
|
21
|
+
def raw
|
22
|
+
@raw ||= github.commit(repo, sha, accept: 'application/vnd.github.VERSION.diff')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module DiffParser
|
2
|
+
class Diff
|
3
|
+
attr_reader :raw_diff, :diff_info
|
4
|
+
|
5
|
+
def initialize(raw_diff, diff_info)
|
6
|
+
@raw_diff = raw_diff
|
7
|
+
@diff_info = diff_info
|
8
|
+
end
|
9
|
+
|
10
|
+
def files
|
11
|
+
DiffParser::DiffFilesCollection.new(raw_diff, diff_info)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module DiffParser
|
2
|
+
class DiffFilesCollection
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :raw_diff, :diff_info
|
6
|
+
|
7
|
+
def initialize(raw_diff, diff_info)
|
8
|
+
@raw_diff = raw_diff
|
9
|
+
@diff_info = diff_info
|
10
|
+
end
|
11
|
+
|
12
|
+
def raw_lines
|
13
|
+
preprocessed_lines = DiffParser::InlineDiff.processing(raw_diff.lines)
|
14
|
+
preprocessed_lines.map { |line| RawLine.new(line) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
current_file = nil
|
19
|
+
|
20
|
+
raw_lines.each_with_index do |raw_line, idx|
|
21
|
+
next if raw_line.skip_line?
|
22
|
+
|
23
|
+
if raw_line.info?
|
24
|
+
yield current_file if current_file
|
25
|
+
current_file = DiffParser::File.new(raw_line.full_line, diff_info)
|
26
|
+
else
|
27
|
+
current_file.push_line(raw_line, idx)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
yield current_file
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -2,29 +2,23 @@ module DiffParser
|
|
2
2
|
class DiffLine
|
3
3
|
CODE_TYPES = %w(old new default match)
|
4
4
|
|
5
|
-
attr_reader :raw_line, :type, :
|
5
|
+
attr_reader :raw_line, :type, :line_new, :line_old, :idx
|
6
6
|
|
7
|
-
def initialize(raw_line, type,
|
8
|
-
@raw_line =
|
9
|
-
raw_line.split(' ').last.tap do |_raw_line|
|
10
|
-
_raw_line[0] = ''
|
11
|
-
end
|
12
|
-
else
|
13
|
-
raw_line
|
14
|
-
end
|
15
|
-
|
16
|
-
@type = if type == 'old' and raw_line[0] != '-'
|
17
|
-
'default'
|
18
|
-
else
|
19
|
-
type
|
20
|
-
end
|
21
|
-
@line_code = line_code
|
7
|
+
def initialize(raw_line, type, line_new, line_old, idx, sha)
|
8
|
+
@raw_line = raw_line
|
22
9
|
@line_new = line_new
|
23
10
|
@line_old = line_old
|
11
|
+
@idx = idx
|
12
|
+
@sha = sha
|
13
|
+
@type = type
|
24
14
|
end
|
25
15
|
|
26
16
|
def line_of_code?
|
27
17
|
CODE_TYPES.include?(type)
|
28
18
|
end
|
19
|
+
|
20
|
+
def line_code
|
21
|
+
"#{sha}_#{line_old}_#{line_new}_#{type}_#{idx}"
|
22
|
+
end
|
29
23
|
end
|
30
24
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module DiffParser
|
2
|
+
class File
|
3
|
+
attr_accessor :line_old, :line_new
|
4
|
+
attr_reader :lines, :sha, :path, :file_info
|
5
|
+
delegate :additions, :deletions, to: :file_info
|
6
|
+
|
7
|
+
def initialize(path, diff_info)
|
8
|
+
@path = preprocess_path(path)
|
9
|
+
@lines = []
|
10
|
+
@line_old = 1
|
11
|
+
@line_new = 1
|
12
|
+
@sha = diff_info.sha
|
13
|
+
@file_info = retrieve_file_info(diff_info)
|
14
|
+
end
|
15
|
+
|
16
|
+
def push_line(raw_line, idx)
|
17
|
+
if raw_line.type == 'match'
|
18
|
+
self.line_old = raw_line.line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
|
19
|
+
self.line_new = raw_line.line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
|
20
|
+
|
21
|
+
lines << DiffLine.new(raw_line.full_line, raw_line.type, nil, nil, idx, sha)
|
22
|
+
else
|
23
|
+
unless raw_line.index?
|
24
|
+
lines << DiffLine.new(raw_line.full_line, raw_line.type, line_new, line_old, idx, sha)
|
25
|
+
|
26
|
+
if raw_line.addition?
|
27
|
+
self.line_new += 1
|
28
|
+
elsif raw_line.deletion?
|
29
|
+
self.line_old += 1
|
30
|
+
else
|
31
|
+
self.line_new += 1
|
32
|
+
self.line_old += 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def each_line
|
39
|
+
lines.each do |line|
|
40
|
+
yield line
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def preprocess_path(path)
|
47
|
+
path.split(' ').last[2..-1]
|
48
|
+
end
|
49
|
+
|
50
|
+
def retrieve_file_info(diff_info)
|
51
|
+
diff_info.files.find { |f| f.filename == path }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module DiffParser
|
2
|
+
class RawLine
|
3
|
+
GIT_NEW_FILE = "new file"
|
4
|
+
GIT_DELETED_FILE = "deleted file"
|
5
|
+
GIT_DIFF_START = "+#!idiff-start!#++"
|
6
|
+
GIT_DIFF_FINISH = "-#!idiff-start!#--"
|
7
|
+
GIT_INDEX = "index "
|
8
|
+
GIT_DIFF = "diff --git "
|
9
|
+
GIT_WARN_NEW_LINE = " No newline at end of file"
|
10
|
+
GIT_CODE_ADDED = "+"
|
11
|
+
GIT_CODE_REMOVED = "-"
|
12
|
+
|
13
|
+
SUMMARY_REGEXES = [/^\-\-\- \/dev\/null/,
|
14
|
+
/^\+\+\+ \/dev\/null/,
|
15
|
+
/^\-\-\- a/,
|
16
|
+
/^\+\+\+ b/]
|
17
|
+
|
18
|
+
attr_reader :line
|
19
|
+
|
20
|
+
def initialize(line)
|
21
|
+
@line = line
|
22
|
+
end
|
23
|
+
|
24
|
+
def full_line
|
25
|
+
@full_line ||= DiffParser::InlineDiff.replace_markers(html_escape(line.gsub(/\n/, '')))
|
26
|
+
end
|
27
|
+
|
28
|
+
def summary?
|
29
|
+
SUMMARY_REGEXES.any? do |regex|
|
30
|
+
line.match(regex)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def info?
|
35
|
+
type == 'info'
|
36
|
+
end
|
37
|
+
|
38
|
+
def index?
|
39
|
+
type == 'index'
|
40
|
+
end
|
41
|
+
|
42
|
+
def addition?
|
43
|
+
line[0] == '+'
|
44
|
+
end
|
45
|
+
|
46
|
+
def deletion?
|
47
|
+
line[0] == '-'
|
48
|
+
end
|
49
|
+
|
50
|
+
def skip_line?
|
51
|
+
summary? or %w(file_update_removed file_update_created).include?(type)
|
52
|
+
end
|
53
|
+
|
54
|
+
def type
|
55
|
+
if stripped_line.match(/^@@ -/)
|
56
|
+
"match"
|
57
|
+
elsif stripped_line.start_with? GIT_NEW_FILE
|
58
|
+
"file_created"
|
59
|
+
elsif stripped_line.start_with? GIT_DELETED_FILE
|
60
|
+
"file_removed"
|
61
|
+
elsif stripped_line.start_with? GIT_DIFF_START
|
62
|
+
"file_update_created"
|
63
|
+
elsif stripped_line.start_with? GIT_DIFF_FINISH
|
64
|
+
"file_update_removed"
|
65
|
+
elsif stripped_line.start_with? GIT_INDEX
|
66
|
+
"index"
|
67
|
+
elsif stripped_line.start_with? GIT_DIFF
|
68
|
+
"info"
|
69
|
+
elsif stripped_line.start_with? GIT_WARN_NEW_LINE
|
70
|
+
"warning"
|
71
|
+
elsif stripped_line.start_with? GIT_CODE_ADDED
|
72
|
+
"new"
|
73
|
+
elsif stripped_line[0] == GIT_CODE_REMOVED
|
74
|
+
"old"
|
75
|
+
else
|
76
|
+
"default"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def html_escape str
|
83
|
+
replacements = {'&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => '''}
|
84
|
+
str.gsub(/[&"'><]/, replacements)
|
85
|
+
end
|
86
|
+
|
87
|
+
def stripped_line
|
88
|
+
@stripped_line ||= line.strip
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/diff_parser/version.rb
CHANGED
data/lib/diff_parser.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
require 'diff_parser/diff'
|
2
|
+
require 'diff_parser/commit'
|
3
|
+
require 'diff_parser/file'
|
4
|
+
require 'diff_parser/diff_files_collection'
|
5
|
+
require 'diff_parser/raw_line'
|
1
6
|
require 'diff_parser/diff_line'
|
2
7
|
require 'diff_parser/inline_diff'
|
3
|
-
require 'diff_parser/regex'
|
4
|
-
require 'diff_parser/parser'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diff_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- stevo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Allows parsing diffs for presentation purposes - extracted from marvelous
|
14
14
|
GitLab project
|
@@ -19,10 +19,13 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/diff_parser.rb
|
22
|
+
- lib/diff_parser/commit.rb
|
23
|
+
- lib/diff_parser/diff.rb
|
24
|
+
- lib/diff_parser/diff_files_collection.rb
|
22
25
|
- lib/diff_parser/diff_line.rb
|
26
|
+
- lib/diff_parser/file.rb
|
23
27
|
- lib/diff_parser/inline_diff.rb
|
24
|
-
- lib/diff_parser/
|
25
|
-
- lib/diff_parser/regex.rb
|
28
|
+
- lib/diff_parser/raw_line.rb
|
26
29
|
- lib/diff_parser/version.rb
|
27
30
|
homepage: https://github.com/Selleo/diff_parser
|
28
31
|
licenses: []
|
data/lib/diff_parser/parser.rb
DELETED
@@ -1,104 +0,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
|
23
|
-
|
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(DiffLine.new(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
|
-
|
53
|
-
yield(DiffLine.new(full_line, type, line_code, line_new, line_old))
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
if line[0] == "+"
|
58
|
-
line_new += 1
|
59
|
-
elsif line[0] == "-"
|
60
|
-
line_old += 1
|
61
|
-
else
|
62
|
-
line_new += 1
|
63
|
-
line_old += 1
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
private
|
69
|
-
|
70
|
-
def identification_type(line)
|
71
|
-
line = line.strip
|
72
|
-
if line.start_with? GIT_NEW_FILE
|
73
|
-
"file_created"
|
74
|
-
elsif line.start_with? GIT_DELETED_FILE
|
75
|
-
"file_removed"
|
76
|
-
elsif line.start_with? GIT_DIFF_START
|
77
|
-
"file_update_created"
|
78
|
-
elsif line.start_with? GIT_DIFF_FINISH
|
79
|
-
"file_update_removed"
|
80
|
-
elsif line.start_with? GIT_INDEX
|
81
|
-
"index"
|
82
|
-
elsif line.start_with? GIT_DIFF
|
83
|
-
"info"
|
84
|
-
elsif line.start_with? GIT_WARN_NEW_LINE
|
85
|
-
"warning"
|
86
|
-
elsif line.start_with? GIT_CODE_ADDED
|
87
|
-
"new"
|
88
|
-
elsif line.start_with? GIT_CODE_REMOVED
|
89
|
-
"old"
|
90
|
-
else
|
91
|
-
"default"
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def generate_line_code(path, line_new, line_old, type, idx)
|
96
|
-
"#{@commit_id}_#{line_old}_#{line_new}_#{type}_#{idx}"
|
97
|
-
end
|
98
|
-
|
99
|
-
def html_escape str
|
100
|
-
replacements = {'&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => '''}
|
101
|
-
str.gsub(/[&"'><]/, replacements)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
data/lib/diff_parser/regex.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
module DiffParser
|
2
|
-
module Regex
|
3
|
-
extend self
|
4
|
-
|
5
|
-
def username_regex
|
6
|
-
default_regex
|
7
|
-
end
|
8
|
-
|
9
|
-
def project_name_regex
|
10
|
-
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
|
11
|
-
end
|
12
|
-
|
13
|
-
def name_regex
|
14
|
-
/\A[a-zA-Z0-9_\-\. ]*\z/
|
15
|
-
end
|
16
|
-
|
17
|
-
def path_regex
|
18
|
-
default_regex
|
19
|
-
end
|
20
|
-
|
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
|
24
|
-
|
25
|
-
%r{
|
26
|
-
(?!
|
27
|
-
# doesn't begins with
|
28
|
-
\/| # (rule #6)
|
29
|
-
# doesn't contain
|
30
|
-
.*(?:
|
31
|
-
[\/.]\.| # (rule #1,3)
|
32
|
-
\/\/| # (rule #6)
|
33
|
-
@\{| # (rule #8)
|
34
|
-
\\ # (rule #9)
|
35
|
-
)
|
36
|
-
)
|
37
|
-
[^\000-\040\177~^:?*\[]+ # (rule #4-5)
|
38
|
-
# doesn't end with
|
39
|
-
(?<!\.lock) # (rule #1)
|
40
|
-
(?<![\/.]) # (rule #6-7)
|
41
|
-
}x
|
42
|
-
end
|
43
|
-
|
44
|
-
protected
|
45
|
-
|
46
|
-
def default_regex
|
47
|
-
/\A[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|