diff_parser 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjM4OTllYjZjOGY1ZTAyN2M3NDVjZWRlZDA5MjBkZDMxYjQwYThiYg==
4
+ ODhkM2ZjNDJhMjJkZjg3N2ZiN2YzYWU0MjA3YTEwMDM3NTVmZDlkYQ==
5
5
  data.tar.gz: !binary |-
6
- N2JjYTUyM2M1OGE1ZmFkMzIyMjI3Zjc1Mzk2YzRkMGVkYjliYWQ5Zg==
6
+ ZDA4ZGRiMmYzODM2NTZlNzE2MDNjY2ExYWQ2ZDIxNzBkMDUzM2MwNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YzM5Y2I2ZjA0MmJmNzlmZTZlZmZiN2MwYTcxMWE4NDNmMDJmOTUyNGM0ZWRk
10
- YjhkOWE5MTU3YTczNDg4Zjg2YzA4ZjliZjQzMGI3NGI5OWQyNGEyYmU4NTlk
11
- M2U4OGQ4NjhlYWViMTlkOTZjNWRhNmY4NmNjMGJhMDJjNzViZGU=
9
+ MGNlY2E0YWI2Yzk1N2VjYjcwNjFjN2FkOGM5YzU3NDhlMGVkMjk0OTNmODgz
10
+ ODFmZjJkYzA0YTY1MzljZTE0MzcyYzhmMDc5YzMxMmVmM2RiMmJjNGNiOGVk
11
+ MWMyZjUzMjQ1NDRmZTc5Yzc3Yzc3ZGVkOWNhMDk5ZmNiYmE1M2I=
12
12
  data.tar.gz: !binary |-
13
- M2U2ZGE5NmY2YzMxNWQwNzNmODc2ZDM4N2Y5MzNkOTdmMzRjNWIwYTQ5Y2Uz
14
- MGRlN2ZjYzk5MmQ5MTZhZmEwN2JiZGY1MmZhNDNhM2JjZGYyYmYxNjZlNTQw
15
- MjA3YTRhZGI3NGJjNTUwM2IwZWZmNWE4MDFjODM0N2NlOWQ4Yzc=
13
+ NWY5ODI1NjY2OWJlYzFlNTQ2MmIyMjJmYTBkYThhMWEzYjYxNTRjZWYwNmY3
14
+ ZGVlNDFlNTExMjk4MTEyZWNlZjM2MTIwZDE5MWQxN2NjZmM2MDFlYTA1NGE4
15
+ OTlhZmI3Y2MyNDRjZjMzMjEzNTgzNTg3MjJkNjdlM2U5OTI4MTY=
data/lib/diff_parser.rb CHANGED
@@ -4,18 +4,11 @@ require 'diff_parser/file'
4
4
  require 'diff_parser/diff_files_collection'
5
5
  require 'diff_parser/raw_line'
6
6
  require 'diff_parser/diff_line'
7
- require 'diff_parser/inline_diff'
8
7
 
9
8
  module DiffParser
10
9
  mattr_accessor :max_changes_to_render
11
10
  @@max_changes_to_render = 500
12
11
 
13
- mattr_accessor :reduced_max_changes_to_render
14
- @@reduced_max_changes_to_render = 10
15
-
16
- mattr_accessor :changes_reduction_threshold
17
- @@changes_reduction_threshold = 20
18
-
19
12
  def self.setup
20
13
  yield self
21
14
  end
@@ -11,8 +11,8 @@ module DiffParser
11
11
  @sha = sha
12
12
  end
13
13
 
14
- def diff
15
- DiffParser::Diff.new(self)
14
+ def diff(options={})
15
+ DiffParser::Diff.new(self, options)
16
16
  end
17
17
  end
18
18
  end
@@ -1,16 +1,24 @@
1
1
  module DiffParser
2
2
  class Diff
3
- attr_reader :raw, :info, :commit
3
+ attr_reader :raw, :info, :commit, :options
4
4
  delegate :github, :repo, :sha, to: :commit
5
5
 
6
- def initialize(commit)
6
+ def initialize(commit, options)
7
7
  @commit = commit
8
- @info = github.commit(commit.repo, sha)
9
- @raw = github.commit(repo, sha, accept: 'application/vnd.github.VERSION.diff')
8
+ @options = options
9
+ @info = github.commit(repo, sha)
10
+
11
+ @raw = Rails.cache.fetch(diff_cache_key) do
12
+ github.commit(repo, sha, accept: 'application/vnd.github.VERSION.diff')
13
+ end
14
+ end
15
+
16
+ def diff_cache_key
17
+ "diff-#{repo}-#{sha}"
10
18
  end
11
19
 
12
20
  def files
13
- DiffParser::DiffFilesCollection.new(self)
21
+ DiffParser::DiffFilesCollection.new(self, options)
14
22
  end
15
23
  end
16
24
  end
@@ -2,15 +2,15 @@ module DiffParser
2
2
  class DiffFilesCollection
3
3
  include Enumerable
4
4
 
5
- attr_reader :diff
5
+ attr_reader :diff, :options
6
6
 
7
- def initialize(diff)
7
+ def initialize(diff, options)
8
+ @options = options
8
9
  @diff = diff
9
10
  end
10
11
 
11
12
  def raw_lines
12
- preprocessed_lines = DiffParser::InlineDiff.processing(diff.raw.lines)
13
- preprocessed_lines.map { |line| RawLine.new(line) }
13
+ diff.raw.lines.map { |line| RawLine.new(line) }
14
14
  end
15
15
 
16
16
  def each
@@ -22,7 +22,7 @@ module DiffParser
22
22
  if raw_line.info?
23
23
  yield current_file if current_file
24
24
  current_file = DiffParser::File.new(raw_line.full_line, diff)
25
- elsif !current_file.folded?
25
+ elsif options[:full] == current_file.path || options[:full] == true || !current_file.folded?
26
26
  current_file.push_line(raw_line, idx)
27
27
  end
28
28
  end
@@ -35,19 +35,13 @@ module DiffParser
35
35
  end
36
36
  end
37
37
 
38
- def each_line
39
- lines.each do |line|
40
- yield line
41
- end
42
- end
43
-
44
38
  def sha
45
39
  diff.sha
46
40
  end
47
41
 
48
42
  def folded?
49
- @folded ||= if file_info && file_max_changes_exceeded?
50
- lines << DiffLine.new('Changes not shown', 'info', nil, nil, 0, nil)
43
+ @folded ||= if file_max_changes_exceeded?
44
+ lines << DiffLine.new(path, 'fold', nil, nil, 0, nil)
51
45
  true
52
46
  else
53
47
  false
@@ -56,24 +50,8 @@ module DiffParser
56
50
 
57
51
  private
58
52
 
59
- def max_changes_to_render
60
- if diff.info.files.count > DiffParser.changes_reduction_threshold
61
- DiffParser.reduced_max_changes_to_render
62
- else
63
- DiffParser.max_changes_to_render
64
- end
65
- end
66
-
67
53
  def file_max_changes_exceeded?
68
- (file_info.additions > max_changes_to_render && added?) || (file_info.deletions > max_changes_to_render && removed?)
69
- end
70
-
71
- def added?
72
- file_info.status == 'added'
73
- end
74
-
75
- def removed?
76
- file_info.status == 'removed'
54
+ !file_info || (file_info.changes > DiffParser.max_changes_to_render)
77
55
  end
78
56
 
79
57
  def preprocess_path(path)
@@ -1,14 +1,14 @@
1
1
  module DiffParser
2
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 = "-"
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
12
 
13
13
  SUMMARY_REGEXES = [/^\-\-\- \/dev\/null/,
14
14
  /^\+\+\+ \/dev\/null/,
@@ -22,7 +22,7 @@ module DiffParser
22
22
  end
23
23
 
24
24
  def full_line
25
- @full_line ||= DiffParser::InlineDiff.replace_markers(html_escape(line.gsub(/\n/, '')))
25
+ @full_line ||= html_escape(line.gsub(/\n/, ''))
26
26
  end
27
27
 
28
28
  def summary?
@@ -53,27 +53,27 @@ module DiffParser
53
53
 
54
54
  def type
55
55
  if stripped_line.match(/^@@ -/)
56
- "match"
56
+ 'match'
57
57
  elsif stripped_line.start_with? GIT_NEW_FILE
58
- "file_created"
58
+ 'file_created'
59
59
  elsif stripped_line.start_with? GIT_DELETED_FILE
60
- "file_removed"
60
+ 'file_removed'
61
61
  elsif stripped_line.start_with? GIT_DIFF_START
62
- "file_update_created"
62
+ 'file_update_created'
63
63
  elsif stripped_line.start_with? GIT_DIFF_FINISH
64
- "file_update_removed"
64
+ 'file_update_removed'
65
65
  elsif stripped_line.start_with? GIT_INDEX
66
- "index"
66
+ 'index'
67
67
  elsif stripped_line.start_with? GIT_DIFF
68
- "info"
68
+ 'info'
69
69
  elsif stripped_line.start_with? GIT_WARN_NEW_LINE
70
- "warning"
70
+ 'warning'
71
71
  elsif stripped_line.start_with? GIT_CODE_ADDED
72
- "new"
72
+ 'new'
73
73
  elsif stripped_line[0] == GIT_CODE_REMOVED
74
- "old"
74
+ 'old'
75
75
  else
76
- "default"
76
+ 'default'
77
77
  end
78
78
  end
79
79
 
@@ -1,3 +1,3 @@
1
1
  module DiffParser
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
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: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - stevo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-09 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Allows parsing diffs for presentation purposes - extracted from marvelous
14
14
  GitLab project
@@ -24,7 +24,6 @@ files:
24
24
  - lib/diff_parser/diff_files_collection.rb
25
25
  - lib/diff_parser/diff_line.rb
26
26
  - lib/diff_parser/file.rb
27
- - lib/diff_parser/inline_diff.rb
28
27
  - lib/diff_parser/raw_line.rb
29
28
  - lib/diff_parser/version.rb
30
29
  homepage: https://github.com/Selleo/diff_parser
@@ -1,78 +0,0 @@
1
- module DiffParser
2
- module InlineDiff
3
- class << self
4
-
5
- START = "#!idiff-start!#"
6
- FINISH = "#!idiff-finish!#"
7
-
8
- def processing diff_arr
9
- indexes = _indexes_of_changed_lines diff_arr
10
-
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
15
-
16
- # Skip inline diff if empty line was replaced with content
17
- next if first_line == "-\n"
18
-
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
25
- end
26
-
27
- first_token = first_line[0..first_the_same_symbols][1..-1]
28
- start = first_token + START
29
-
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
38
-
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
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)
51
- end
52
- diff_arr
53
- end
54
-
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!(/[^\-\+]/, "#")
61
-
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
69
- end
70
-
71
- def replace_markers line
72
- line.gsub!(START, "<span class='idiff'>")
73
- line.gsub!(FINISH, "</span>")
74
- line
75
- end
76
- end
77
- end
78
- end