grit-ext 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,3 +12,7 @@ To use grit-ext, after installing, just require 'grit-ext':
12
12
  require 'grit-ext'
13
13
 
14
14
  All of the extensions, additionally to normal grit classes, will be available to you.
15
+
16
+ ## Diff#hunks
17
+
18
+ [Diff#hunks](https://github.com/mmozuras/grit-ext/blob/master/lib/grit/ext/diff.rb) method returns an array of [DiffHunk](https://github.com/mmozuras/grit-ext/blob/master/lib/grit/ext/diff_hunk.rb). These hunks provide structured information about the diff.
data/lib/grit/ext/diff.rb CHANGED
@@ -6,7 +6,7 @@ module Grit
6
6
  .find_all { |line, index| line.start_with?('@@') }
7
7
  .map { |line, index| [DiffHeader.new(line), index] }
8
8
  .map { |header, index|
9
- DiffHunk.new(header, lines[index, header.count])
9
+ DiffHunk.new(header, lines[index, header.count + 1])
10
10
  }
11
11
  end
12
12
  end
@@ -22,5 +22,9 @@ module Grit
22
22
  start, count = s.split(',')
23
23
  [start.to_i, count.to_i]
24
24
  end
25
+
26
+ def to_s
27
+ "@@ -#{removed_start},#{removed_count} +#{added_start},#{added_count} @@"
28
+ end
25
29
  end
26
30
  end
@@ -3,17 +3,20 @@ module Grit
3
3
  attr_reader :lines
4
4
 
5
5
  def initialize(header, diff_hunk)
6
+ @header = header
6
7
  @lines = diff_hunk.each_with_index.map do |line, index|
7
8
  content = line[1..line.length - 1]
8
9
  status = case line[0]
9
10
  when '+' then :added
10
11
  when '-' then :removed
11
12
  when ' ' then :unchanged
13
+ else
14
+ :ignore
12
15
  end
13
16
  position = header.start + index
14
17
 
15
- DiffLine.new(content, status, position)
16
- end
18
+ DiffLine.new(content, status, position) if status != :ignore
19
+ end.compact
17
20
  end
18
21
 
19
22
  def added
@@ -27,5 +30,9 @@ module Grit
27
30
  def unchanged
28
31
  @lines.find_all { |line| line.unchanged? }
29
32
  end
33
+
34
+ def to_s
35
+ "#{@header}\n#{lines.join("\n")}"
36
+ end
30
37
  end
31
38
  end
@@ -11,5 +11,17 @@ module Grit
11
11
  def unchanged?
12
12
  status == :unchanged
13
13
  end
14
+
15
+ def to_s
16
+ symbol = case status
17
+ when :added
18
+ '+'
19
+ when :removed
20
+ '-'
21
+ when :unchanged
22
+ ' '
23
+ end
24
+ "#{symbol}#{content}"
25
+ end
14
26
  end
15
27
  end
@@ -1,5 +1,5 @@
1
1
  module Grit
2
2
  module Ext
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grit-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -66,7 +66,6 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
- - lib/grit/ext/commit.rb
70
69
  - lib/grit/ext/diff.rb
71
70
  - lib/grit/ext/diff_header.rb
72
71
  - lib/grit/ext/diff_hunk.rb
@@ -1,26 +0,0 @@
1
- module Grit
2
- class Commit
3
- def changed_files
4
- diffs.map do |diff|
5
- diff.b_path
6
- end
7
- end
8
-
9
- def added_lines
10
- diffs.map do |diff|
11
- diff.hunks.map do |number, lines|
12
- result = []
13
- lines.each do |line|
14
- if line.start_with?('+')
15
- result << { number: number, content: line }
16
- end
17
-
18
- number += 1
19
- end
20
-
21
- result
22
- end
23
- end
24
- end
25
- end
26
- end