git_diff 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa01fe77b7f1845c9feb7cb917b4f6ec852a7ea3
4
- data.tar.gz: 9c2a059e515b676f57215214262cdb9fab89724e
3
+ metadata.gz: 880459816cc9946e902a423f2ced95ad11c507a7
4
+ data.tar.gz: 6d9541ae1bd4303750cb9d14d2968053dccad910
5
5
  SHA512:
6
- metadata.gz: 551a65b09a2a618daaa345205f821666612ed6a8ad588fd8ef137b13d57e6ae4c2acfd0ea8b538b8f8f847baeb211779b83f061b1c8f57065c7778b25bb48a48
7
- data.tar.gz: 907132b50e27f720f8d1397988a88258f967ae8bd7aada9403208afe7e3dc4872a55a054d43961cf7053f47e304147ecb1e86b207c8dd9de9b955e5f97adc4db
6
+ metadata.gz: 5357077be7040cba74dd3b16387ff1508120e33109f2e06ab63d719a61da7d9d8dafc8a035ac67187da854d920667759a816564beba776779187213401baf526
7
+ data.tar.gz: 8a4b2b47b2b1a726c4963b5be45f16b0bb40fa190ccbcb063fc42a99256b94ea3d5fb6e691f22845ba6fd1daa9633fdb7286ca0deb7aa19018f392f54846ca24
data/lib/git_diff/file.rb CHANGED
@@ -1,16 +1,21 @@
1
1
  module GitDiff
2
2
  class File
3
3
 
4
- attr_reader :a_path, :a_blob, :b_path, :b_blob, :b_mode, :hunks
4
+ attr_reader :a_path, :a_blob, :b_path, :b_blob, :b_mode, :hunks, :binary
5
5
 
6
6
  def self.from_string(string)
7
- if /^diff --git/.match(string)
8
- File.new
7
+ if path_info = /^diff --git(?: a\/(\S+))?(?: b\/(\S+))?/.match(string)
8
+ File.new(
9
+ a_path: path_info.captures[0] || '/dev/null',
10
+ b_path: path_info.captures[1] || '/dev/null'
11
+ )
9
12
  end
10
13
  end
11
14
 
12
- def initialize
15
+ def initialize(a_path: '/dev/null', b_path: '/dev/null')
13
16
  @hunks = []
17
+ @a_path = a_path
18
+ @b_path = b_path
14
19
  end
15
20
 
16
21
  def <<(string)
@@ -38,6 +43,7 @@ module GitDiff
38
43
  def add_hunk(hunk)
39
44
  self.current_hunk = hunk
40
45
  hunks << current_hunk
46
+ @binary = false
41
47
  end
42
48
 
43
49
  def append_to_current_hunk(string)
@@ -60,6 +66,25 @@ module GitDiff
60
66
  @a_path = "/dev/null"
61
67
  when /^deleted file mode [0-9]{6}$/.match(string)
62
68
  @b_path = "/dev/null"
69
+ when mode_info = /^(old|new) mode ([0-9]{6})$/.match(string)
70
+ if mode_info.captures[0] == "old"
71
+ @a_mode = mode_info.captures[1]
72
+ else
73
+ @b_mode = mode_info.captures[1]
74
+ end
75
+ when rename_info = /^rename (from|to) (.*)$/.match(string)
76
+ if rename_info.captures[0] == "from"
77
+ @a_path = rename_info.captures[1]
78
+ else
79
+ @b_path = rename_info.captures[1]
80
+ end
81
+ when binary_info = /^Binary files (?:\/dev\/null|a\/(.*)) and (?:\/dev\/null|b\/(.*)) differ$/.match(string)
82
+ @binary = true
83
+ @a_path ||= binary_info[1] || "/dev/null"
84
+ @b_path ||= binary_info[2] || "/dev/null"
85
+ when /^similarity/.match(string)
86
+ # trash
87
+ true
63
88
  end
64
89
  end
65
90
  end
@@ -1,3 +1,3 @@
1
1
  module GitDiff
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -128,4 +128,56 @@ index 033b446..0e2d140 100644
128
128
  [186,186]
129
129
  ], second_hunk.lines.map { |line| line.line_number.pair }
130
130
  end
131
+
132
+ def test_binary_file_diff
133
+ diff = GitDiff.from_string 'diff --git a/app/assets/bin.eot b/app/assets/bin.eot
134
+ new file mode 100644
135
+ index 0000000..2cbab9c
136
+ Binary files /dev/null and b/app/assets/bin.eot differ'
137
+
138
+ refute_nil diff
139
+ assert_instance_of GitDiff::Diff, diff
140
+
141
+ assert_equal 1, diff.files.count
142
+ file = diff.files[0]
143
+
144
+ assert file.binary
145
+ assert_equal '/dev/null', file.a_path
146
+ assert_equal "app/assets/bin.eot", file.b_path
147
+ end
148
+
149
+ def test_perm_change_diff
150
+ diff = GitDiff.from_string 'diff --git a/bin/setup b/bin/setup
151
+ old mode 100644
152
+ new mode 100755'
153
+
154
+ refute_nil diff
155
+ assert_instance_of GitDiff::Diff, diff
156
+
157
+ assert_equal 1, diff.files.count
158
+ file = diff.files[0]
159
+
160
+ assert_equal 0, file.hunks.count
161
+
162
+ assert_equal "bin/setup", file.a_path
163
+ assert_equal "bin/setup", file.b_path
164
+ end
165
+
166
+ def test_rename_diff
167
+ diff = GitDiff.from_string 'diff --git a/lib/path1/my_file.rb b/lib/path2/my_file2.rb
168
+ similarity index 100%
169
+ rename from lib/path1/my_file.rb
170
+ rename to lib/path2/my_file2.rb'
171
+
172
+ refute_nil diff
173
+ assert_instance_of GitDiff::Diff, diff
174
+
175
+ assert_equal 1, diff.files.count
176
+ file = diff.files[0]
177
+
178
+ assert_equal 0, file.hunks.count
179
+
180
+ assert_equal "lib/path1/my_file.rb", file.a_path
181
+ assert_equal "lib/path2/my_file2.rb", file.b_path
182
+ end
131
183
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Olson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-30 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.6.8
122
+ rubygems_version: 2.6.13
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: A Ruby library for parsing git diffs.