Skip to content

Commit d4f66ab

Browse files
committedAug 25, 2024·
Sanitize non-option arguments passed to git name-rev
1 parent 0296442 commit d4f66ab

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed
 

Diff for: ‎lib/git/lib.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,22 @@ def rev_parse(revision)
337337
# For backwards compatibility with the old method name
338338
alias :revparse :rev_parse
339339

340-
def namerev(string)
341-
command('name-rev', string).split[1]
340+
# Find the first symbolic name for given commit_ish
341+
#
342+
# @param commit_ish [String] the commit_ish to find the symbolic name of
343+
#
344+
# @return [String, nil] the first symbolic name or nil if the commit_ish isn't found
345+
#
346+
# @raise [ArgumentError] if the commit_ish is a string starting with a hyphen
347+
#
348+
def name_rev(commit_ish)
349+
assert_args_are_not_options('commit_ish', commit_ish)
350+
351+
command('name-rev', commit_ish).split[1]
342352
end
343353

354+
alias :namerev :name_rev
355+
344356
def object_type(sha)
345357
command('cat-file', '-t', sha)
346358
end

Diff for: ‎lib/git/object.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def message
175175
end
176176

177177
def name
178-
@base.lib.namerev(sha)
178+
@base.lib.name_rev(sha)
179179
end
180180

181181
def gtree

Diff for: ‎tests/units/test_lib.rb

+10
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ def test_rev_parse_with_unknown_revision
198198
end
199199
end
200200

201+
def test_name_rev
202+
assert_equal('tags/v2.5~5', @lib.name_rev('00ea60e'))
203+
end
204+
205+
def test_name_rev_with_invalid_commit_ish
206+
assert_raise(ArgumentError) do
207+
@lib.name_rev('-1cc8667014381')
208+
end
209+
end
210+
201211
def test_object_type
202212
assert_equal('commit', @lib.object_type('1cc8667014381')) # commit
203213
assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree

0 commit comments

Comments
 (0)
Please sign in to comment.