Skip to content

Commit 0296442

Browse files
committed
Refactor Git::Lib#rev_parse
1 parent 9b9b31e commit 0296442

File tree

7 files changed

+60
-24
lines changed

7 files changed

+60
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ tree.blobs
277277
tree.subtrees
278278
tree.children # blobs and subtrees
279279

280-
g.revparse('v2.5:Makefile')
280+
g.rev_parse('v2.0.0:README.md')
281281

282282
g.branches # returns Git::Branch objects
283283
g.branches.local

lib/git/base.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,14 +634,17 @@ def with_temp_working &blk
634634
# runs git rev-parse to convert the objectish to a full sha
635635
#
636636
# @example
637-
# git.revparse("HEAD^^")
638-
# git.revparse('v2.4^{tree}')
639-
# git.revparse('v2.4:/doc/index.html')
637+
# git.rev_parse("HEAD^^")
638+
# git.rev_parse('v2.4^{tree}')
639+
# git.rev_parse('v2.4:/doc/index.html')
640640
#
641-
def revparse(objectish)
642-
self.lib.revparse(objectish)
641+
def rev_parse(objectish)
642+
self.lib.rev_parse(objectish)
643643
end
644644

645+
# For backwards compatibility
646+
alias revparse rev_parse
647+
645648
def ls_tree(objectish, opts = {})
646649
self.lib.ls_tree(objectish, opts)
647650
end

lib/git/lib.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,17 +311,32 @@ def full_log_commits(opts = {})
311311
process_commit_log_data(full_log)
312312
end
313313

314-
def revparse(string)
315-
return string if string =~ /^[A-Fa-f0-9]{40}$/ # passing in a sha - just no-op it
316-
rev = ['head', 'remotes', 'tags'].map do |d|
317-
File.join(@git_dir, 'refs', d, string)
318-
end.find do |path|
319-
File.file?(path)
320-
end
321-
return File.read(rev).chomp if rev
322-
command('rev-parse', string)
314+
# Verify and resolve a Git revision to its full SHA
315+
#
316+
# @see https://git-scm.com/docs/git-rev-parse git-rev-parse
317+
# @see https://git-scm.com/docs/git-rev-parse#_specifying_revisions Valid ways to specify revisions
318+
# @see https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt-emltrefnamegtemegemmasterememheadsmasterememrefsheadsmasterem Ref disambiguation rules
319+
#
320+
# @example
321+
# lib.rev_parse('HEAD') # => '9b9b31e704c0b85ffdd8d2af2ded85170a5af87d'
322+
# lib.rev_parse('9b9b31e') # => '9b9b31e704c0b85ffdd8d2af2ded85170a5af87d'
323+
#
324+
# @param revision [String] the revision to resolve
325+
#
326+
# @return [String] the full commit hash
327+
#
328+
# @raise [Git::FailedError] if the revision cannot be resolved
329+
# @raise [ArgumentError] if the revision is a string starting with a hyphen
330+
#
331+
def rev_parse(revision)
332+
assert_args_are_not_options('rev', revision)
333+
334+
command('rev-parse', revision)
323335
end
324336

337+
# For backwards compatibility with the old method name
338+
alias :revparse :rev_parse
339+
325340
def namerev(string)
326341
command('name-rev', string).split[1]
327342
end

lib/git/object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(base, objectish)
2323
end
2424

2525
def sha
26-
@sha ||= @base.lib.revparse(@objectish)
26+
@sha ||= @base.lib.rev_parse(@objectish)
2727
end
2828

2929
def size

tests/units/test_branch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def test_branch_update_ref
160160
File.write('foo','rev 2')
161161
git.add('foo')
162162
git.commit('rev 2')
163-
git.branch('testing').update_ref(git.revparse('HEAD'))
163+
git.branch('testing').update_ref(git.rev_parse('HEAD'))
164164

165165
# Expect the call to Branch#update_ref to pass the full ref name for the
166166
# of the testing branch to Lib#update_ref
167-
assert_equal(git.revparse('HEAD'), git.revparse('refs/heads/testing'))
167+
assert_equal(git.rev_parse('HEAD'), git.rev_parse('refs/heads/testing'))
168168
end
169169
end
170170
end

tests/units/test_lib.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,28 @@ def test_git_ssh_from_environment_is_passed_to_binary
174174
Git::Base.config.git_ssh = saved_git_ssh
175175
end
176176

177-
def test_revparse
178-
assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.revparse('1cc8667014381')) # commit
179-
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree
180-
assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob
177+
def test_rev_parse_commit
178+
assert_equal('1cc8667014381e2788a94777532a788307f38d26', @lib.rev_parse('1cc8667014381')) # commit
179+
end
180+
181+
def test_rev_parse_tree
182+
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.rev_parse('1cc8667014381^{tree}')) #tree
183+
end
184+
185+
def test_rev_parse_blob
186+
assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.rev_parse('v2.5:example.txt')) #blob
187+
end
188+
189+
def test_rev_parse_with_bad_revision
190+
assert_raise(ArgumentError) do
191+
@lib.rev_parse('--all')
192+
end
193+
end
194+
195+
def test_rev_parse_with_unknown_revision
196+
assert_raise(Git::FailedError) do
197+
@lib.rev_parse('NOTFOUND')
198+
end
181199
end
182200

183201
def test_object_type

tests/units/test_object.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def test_blob_contents
120120
assert(block_called)
121121
end
122122

123-
def test_revparse
124-
sha = @git.revparse('v2.6:example.txt')
123+
def test_rev_parse
124+
sha = @git.rev_parse('v2.6:example.txt')
125125
assert_equal('1f09f2edb9c0d9275d15960771b363ca6940fbe3', sha)
126126
end
127127

0 commit comments

Comments
 (0)