Skip to content

Commit

Permalink
Support arbitrary object name lengths (set with core.abbrev)
Browse files Browse the repository at this point in the history
Support core.abbrev set to non-default values between 4 and 40.

We ran into an issue when we bumped to Ubuntu 18 (bionic) and started seeing failures in process_full_diff because the index lines had 11 character SHA abbreviations. This should allow core.abbrev to be set to anything between 4 (the min) and 40 (the max).

Signed-off-by: Nicholas Calugar <nicholas.calugar@gusto.com>
Co-authored-by: Ngan Pham <ngan@users.noreply.github.com>
  • Loading branch information
SocalNick and ngan committed Dec 22, 2020
1 parent 8b3bd25 commit b2f8845
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/git/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def each(&block) # :yields: each Git::DiffFile in turn
class DiffFile
attr_accessor :patch, :path, :mode, :src, :dst, :type
@base = nil
NIL_BLOB_REGEXP = /\A0{4,40}\z/.freeze

def initialize(base, hash)
@base = base
Expand All @@ -89,10 +90,10 @@ def binary?
end

def blob(type = :dst)
if type == :src
@base.object(@src) if @src != '0000000'
else
@base.object(@dst) if @dst != '0000000'
if type == :src && !NIL_BLOB_REGEXP.match(@src)
@base.object(@src)
elsif !NIL_BLOB_REGEXP.match(@dst)
@base.object(@dst)
end
end
end
Expand Down Expand Up @@ -132,7 +133,7 @@ def process_full_diff
current_file = m[1]
final[current_file] = defaults.merge({:patch => line, :path => current_file})
else
if m = /^index (.......)\.\.(.......)( ......)*/.match(line)
if m = /^index ([0-9a-f]{4,40})\.\.([0-9a-f]{4,40})( ......)*/.match(line)
final[current_file][:src] = m[1]
final[current_file][:dst] = m[2]
final[current_file][:mode] = m[3].strip if m[3]
Expand Down
22 changes: 21 additions & 1 deletion tests/units/test_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,31 @@ def test_diff_stats
assert_equal(1, s[:files]["scott/newfile"][:deletions])
end

def test_diff_hashkey
def test_diff_hashkey_default
assert_equal('5d46068', @diff["scott/newfile"].src)
assert_nil(@diff["scott/newfile"].blob(:dst))
assert(@diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
end

def test_diff_hashkey_min
set_file_paths
git = Git.open(@wdir)
git.config('core.abbrev', 4)
diff = git.diff('gitsearch1', 'v2.5')
assert_equal('5d46', diff["scott/newfile"].src)
assert_nil(diff["scott/newfile"].blob(:dst))
assert(diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
end

def test_diff_hashkey_max
set_file_paths
git = Git.open(@wdir)
git.config('core.abbrev', 40)
diff = git.diff('gitsearch1', 'v2.5')
assert_equal('5d4606820736043f9eed2a6336661d6892c820a5', diff["scott/newfile"].src)
assert_nil(diff["scott/newfile"].blob(:dst))
assert(diff["scott/newfile"].blob(:src).is_a?(Git::Object::Blob))
end

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

0 comments on commit b2f8845

Please sign in to comment.