Skip to content

Commit dc46ede

Browse files
committed
Verify that the commit-ish passed to git describe does not resemble a command-line option
1 parent 00c4939 commit dc46ede

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

lib/git/lib.rb

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,27 +169,33 @@ def repository_default_branch(repository)
169169

170170
## READ COMMANDS ##
171171

172+
# Finds most recent tag that is reachable from a commit
172173
#
173-
# Returns most recent tag that is reachable from a commit
174+
# @see https://git-scm.com/docs/git-describe git-describe
174175
#
175-
# accepts options:
176-
# :all
177-
# :tags
178-
# :contains
179-
# :debug
180-
# :exact_match
181-
# :dirty
182-
# :abbrev
183-
# :candidates
184-
# :long
185-
# :always
186-
# :math
187-
#
188-
# @param [String|NilClass] committish target commit sha or object name
189-
# @param [{Symbol=>Object}] opts the given options
190-
# @return [String] the tag name
191-
#
192-
def describe(committish=nil, opts={})
176+
# @param commit_ish [String, nil] target commit sha or object name
177+
#
178+
# @param opts [Hash] the given options
179+
#
180+
# @option opts :all [Boolean]
181+
# @option opts :tags [Boolean]
182+
# @option opts :contains [Boolean]
183+
# @option opts :debug [Boolean]
184+
# @option opts :long [Boolean]
185+
# @option opts :always [Boolean]
186+
# @option opts :exact_match [Boolean]
187+
# @option opts :dirty [true, String]
188+
# @option opts :abbrev [String]
189+
# @option opts :candidates [String]
190+
# @option opts :match [String]
191+
#
192+
# @return [String] the tag name
193+
#
194+
# @raise [ArgumentError] if the commit_ish is a string starting with a hyphen
195+
#
196+
def describe(commit_ish = nil, opts = {})
197+
assert_args_are_not_options('commit-ish object', commit_ish)
198+
193199
arr_opts = []
194200

195201
arr_opts << '--all' if opts[:all]
@@ -207,7 +213,7 @@ def describe(committish=nil, opts={})
207213
arr_opts << "--candidates=#{opts[:candidates]}" if opts[:candidates]
208214
arr_opts << "--match=#{opts[:match]}" if opts[:match]
209215

210-
arr_opts << committish if committish
216+
arr_opts << commit_ish if commit_ish
211217

212218
return command('describe', *arr_opts)
213219
end
@@ -534,15 +540,15 @@ def grep(string, opts = {})
534540
# @raise [ArgumentError] if any of the parameters are a string starting with a hyphen
535541
# @return [void]
536542
#
537-
def validate_no_options(arg_name, *args)
543+
def assert_args_are_not_options(arg_name, *args)
538544
invalid_args = args.select { |arg| arg&.start_with?('-') }
539545
if invalid_args.any?
540546
raise ArgumentError, "Invalid #{arg_name}: '#{invalid_args.join("', '")}'"
541547
end
542548
end
543549

544550
def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
545-
validate_no_options('commit or commit range', obj1, obj2)
551+
assert_args_are_not_options('commit or commit range', obj1, obj2)
546552

547553
diff_opts = ['-p']
548554
diff_opts << obj1
@@ -553,7 +559,7 @@ def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
553559
end
554560

555561
def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
556-
validate_no_options('commit or commit range', obj1, obj2)
562+
assert_args_are_not_options('commit or commit range', obj1, obj2)
557563

558564
diff_opts = ['--numstat']
559565
diff_opts << obj1
@@ -575,7 +581,7 @@ def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
575581
end
576582

577583
def diff_name_status(reference1 = nil, reference2 = nil, opts = {})
578-
validate_no_options('commit or commit range', reference1, reference2)
584+
assert_args_are_not_options('commit or commit range', reference1, reference2)
579585

580586
opts_arr = ['--name-status']
581587
opts_arr << reference1 if reference1

tests/units/test_describe.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ def test_describe
1313
assert_equal(@git.describe(nil, {:tags => true}), 'grep_colon_numbers')
1414
end
1515

16+
def test_describe_with_invalid_commitish
17+
assert_raise ArgumentError do
18+
@git.describe('--all')
19+
end
20+
end
1621
end

0 commit comments

Comments
 (0)