diff --git a/README.md b/README.md index 3152688a..c3f788ca 100644 --- a/README.md +++ b/README.md @@ -277,7 +277,7 @@ tree.blobs tree.subtrees tree.children # blobs and subtrees -g.revparse('v2.5:Makefile') +g.rev_parse('v2.0.0:README.md') g.branches # returns Git::Branch objects g.branches.local diff --git a/lib/git/base.rb b/lib/git/base.rb index 27de57de..ae909dcc 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -634,14 +634,17 @@ def with_temp_working &blk # runs git rev-parse to convert the objectish to a full sha # # @example - # git.revparse("HEAD^^") - # git.revparse('v2.4^{tree}') - # git.revparse('v2.4:/doc/index.html') + # git.rev_parse("HEAD^^") + # git.rev_parse('v2.4^{tree}') + # git.rev_parse('v2.4:/doc/index.html') # - def revparse(objectish) - self.lib.revparse(objectish) + def rev_parse(objectish) + self.lib.rev_parse(objectish) end + # For backwards compatibility + alias revparse rev_parse + def ls_tree(objectish, opts = {}) self.lib.ls_tree(objectish, opts) end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 84eda5a1..4f607e4f 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -311,17 +311,32 @@ def full_log_commits(opts = {}) process_commit_log_data(full_log) end - def revparse(string) - return string if string =~ /^[A-Fa-f0-9]{40}$/ # passing in a sha - just no-op it - rev = ['head', 'remotes', 'tags'].map do |d| - File.join(@git_dir, 'refs', d, string) - end.find do |path| - File.file?(path) - end - return File.read(rev).chomp if rev - command('rev-parse', string) + # Verify and resolve a Git revision to its full SHA + # + # @see https://git-scm.com/docs/git-rev-parse git-rev-parse + # @see https://git-scm.com/docs/git-rev-parse#_specifying_revisions Valid ways to specify revisions + # @see https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt-emltrefnamegtemegemmasterememheadsmasterememrefsheadsmasterem Ref disambiguation rules + # + # @example + # lib.rev_parse('HEAD') # => '9b9b31e704c0b85ffdd8d2af2ded85170a5af87d' + # lib.rev_parse('9b9b31e') # => '9b9b31e704c0b85ffdd8d2af2ded85170a5af87d' + # + # @param revision [String] the revision to resolve + # + # @return [String] the full commit hash + # + # @raise [Git::FailedError] if the revision cannot be resolved + # @raise [ArgumentError] if the revision is a string starting with a hyphen + # + def rev_parse(revision) + assert_args_are_not_options('rev', revision) + + command('rev-parse', revision) end + # For backwards compatibility with the old method name + alias :revparse :rev_parse + def namerev(string) command('name-rev', string).split[1] end diff --git a/lib/git/object.rb b/lib/git/object.rb index 1ffc1013..083640b6 100644 --- a/lib/git/object.rb +++ b/lib/git/object.rb @@ -23,7 +23,7 @@ def initialize(base, objectish) end def sha - @sha ||= @base.lib.revparse(@objectish) + @sha ||= @base.lib.rev_parse(@objectish) end def size diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb index 08707b63..2256f4cb 100644 --- a/tests/units/test_branch.rb +++ b/tests/units/test_branch.rb @@ -160,11 +160,11 @@ def test_branch_update_ref File.write('foo','rev 2') git.add('foo') git.commit('rev 2') - git.branch('testing').update_ref(git.revparse('HEAD')) + git.branch('testing').update_ref(git.rev_parse('HEAD')) # Expect the call to Branch#update_ref to pass the full ref name for the # of the testing branch to Lib#update_ref - assert_equal(git.revparse('HEAD'), git.revparse('refs/heads/testing')) + assert_equal(git.rev_parse('HEAD'), git.rev_parse('refs/heads/testing')) end end end diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index be049c7b..c8e035ad 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -174,10 +174,28 @@ def test_git_ssh_from_environment_is_passed_to_binary Git::Base.config.git_ssh = saved_git_ssh end - def test_revparse - assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit - assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree - assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob + def test_rev_parse_commit + assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.rev_parse('1cc8667014381')) # commit + end + + def test_rev_parse_tree + assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.rev_parse('1cc8667014381^{tree}')) #tree + end + + def test_rev_parse_blob + assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.rev_parse('v2.5:example.txt')) #blob + end + + def test_rev_parse_with_bad_revision + assert_raise(ArgumentError) do + @lib.rev_parse('--all') + end + end + + def test_rev_parse_with_unknown_revision + assert_raise(Git::FailedError) do + @lib.rev_parse('NOTFOUND') + end end def test_object_type diff --git a/tests/units/test_object.rb b/tests/units/test_object.rb index 784e81bf..3f31b390 100644 --- a/tests/units/test_object.rb +++ b/tests/units/test_object.rb @@ -120,8 +120,8 @@ def test_blob_contents assert(block_called) end - def test_revparse - sha = @git.revparse('v2.6:example.txt') + def test_rev_parse + sha = @git.rev_parse('v2.6:example.txt') assert_equal('1f09f2edb9c0d9275d15960771b363ca6940fbe3', sha) end