Skip to content

Commit 5d46b81

Browse files
authored
Merge branch 'master' into clone_logger
2 parents 9204503 + b2f8845 commit 5d46b81

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/git/diff.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def each(&block) # :yields: each Git::DiffFile in turn
7272
class DiffFile
7373
attr_accessor :patch, :path, :mode, :src, :dst, :type
7474
@base = nil
75+
NIL_BLOB_REGEXP = /\A0{4,40}\z/.freeze
7576

7677
def initialize(base, hash)
7778
@base = base
@@ -89,10 +90,10 @@ def binary?
8990
end
9091

9192
def blob(type = :dst)
92-
if type == :src
93-
@base.object(@src) if @src != '0000000'
94-
else
95-
@base.object(@dst) if @dst != '0000000'
93+
if type == :src && !NIL_BLOB_REGEXP.match(@src)
94+
@base.object(@src)
95+
elsif !NIL_BLOB_REGEXP.match(@dst)
96+
@base.object(@dst)
9697
end
9798
end
9899
end
@@ -132,7 +133,7 @@ def process_full_diff
132133
current_file = m[1]
133134
final[current_file] = defaults.merge({:patch => line, :path => current_file})
134135
else
135-
if m = /^index (.......)\.\.(.......)( ......)*/.match(line)
136+
if m = /^index ([0-9a-f]{4,40})\.\.([0-9a-f]{4,40})( ......)*/.match(line)
136137
final[current_file][:src] = m[1]
137138
final[current_file][:dst] = m[2]
138139
final[current_file][:mode] = m[3].strip if m[3]

tests/units/test_diff.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,31 @@ def test_diff_stats
7676
assert_equal(1, s[:files]["scott/newfile"][:deletions])
7777
end
7878

79-
def test_diff_hashkey
79+
def test_diff_hashkey_default
8080
assert_equal('5d46068', @diff["scott/newfile"].src)
8181
assert_nil(@diff["scott/newfile"].blob(:dst))
8282
assert(@diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
8383
end
84+
85+
def test_diff_hashkey_min
86+
set_file_paths
87+
git = Git.open(@wdir)
88+
git.config('core.abbrev', 4)
89+
diff = git.diff('gitsearch1', 'v2.5')
90+
assert_equal('5d46', diff["scott/newfile"].src)
91+
assert_nil(diff["scott/newfile"].blob(:dst))
92+
assert(diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
93+
end
94+
95+
def test_diff_hashkey_max
96+
set_file_paths
97+
git = Git.open(@wdir)
98+
git.config('core.abbrev', 40)
99+
diff = git.diff('gitsearch1', 'v2.5')
100+
assert_equal('5d4606820736043f9eed2a6336661d6892c820a5', diff["scott/newfile"].src)
101+
assert_nil(diff["scott/newfile"].blob(:dst))
102+
assert(diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
103+
end
84104

85105
def test_patch
86106
p = @git.diff('v2.8^', 'v2.8').patch

0 commit comments

Comments
 (0)