Skip to content

Commit 9b9b31e

Browse files
committed
Verify that the revision-range passed to git log does not resemble a command-line option
1 parent dc46ede commit 9b9b31e

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

lib/git/lib.rb

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def repository_default_branch(repository)
192192
# @return [String] the tag name
193193
#
194194
# @raise [ArgumentError] if the commit_ish is a string starting with a hyphen
195-
#
195+
#
196196
def describe(commit_ish = nil, opts = {})
197197
assert_args_are_not_options('commit-ish object', commit_ish)
198198

@@ -218,7 +218,37 @@ def describe(commit_ish = nil, opts = {})
218218
return command('describe', *arr_opts)
219219
end
220220

221-
def log_commits(opts={})
221+
# Return the commits that are within the given revision range
222+
#
223+
# @see https://git-scm.com/docs/git-log git-log
224+
#
225+
# @param opts [Hash] the given options
226+
#
227+
# @option opts :count [Integer] the maximum number of commits to return (maps to max-count)
228+
# @option opts :all [Boolean]
229+
# @option opts :cherry [Boolean]
230+
# @option opts :since [String]
231+
# @option opts :until [String]
232+
# @option opts :grep [String]
233+
# @option opts :author [String]
234+
# @option opts :between [Array<String>] an array of two commit-ish strings to specify a revision range
235+
#
236+
# Only :between or :object options can be used, not both.
237+
#
238+
# @option opts :object [String] the revision range for the git log command
239+
#
240+
# Only :between or :object options can be used, not both.
241+
#
242+
# @option opts :path_limiter [Array<String>, String] only include commits that impact files from the specified paths
243+
#
244+
# @return [Array<String>] the log output
245+
#
246+
# @raise [ArgumentError] if the resulting revision range is a string starting with a hyphen
247+
#
248+
def log_commits(opts = {})
249+
assert_args_are_not_options('between', opts[:between]&.first)
250+
assert_args_are_not_options('object', opts[:object])
251+
222252
arr_opts = log_common_options(opts)
223253

224254
arr_opts << '--pretty=oneline'
@@ -228,7 +258,47 @@ def log_commits(opts={})
228258
command_lines('log', *arr_opts).map { |l| l.split.first }
229259
end
230260

231-
def full_log_commits(opts={})
261+
# Return the commits that are within the given revision range
262+
#
263+
# @see https://git-scm.com/docs/git-log git-log
264+
#
265+
# @param opts [Hash] the given options
266+
#
267+
# @option opts :count [Integer] the maximum number of commits to return (maps to max-count)
268+
# @option opts :all [Boolean]
269+
# @option opts :cherry [Boolean]
270+
# @option opts :since [String]
271+
# @option opts :until [String]
272+
# @option opts :grep [String]
273+
# @option opts :author [String]
274+
# @option opts :between [Array<String>] an array of two commit-ish strings to specify a revision range
275+
#
276+
# Only :between or :object options can be used, not both.
277+
#
278+
# @option opts :object [String] the revision range for the git log command
279+
#
280+
# Only :between or :object options can be used, not both.
281+
#
282+
# @option opts :path_limiter [Array<String>, String] only include commits that impact files from the specified paths
283+
# @option opts :skip [Integer]
284+
#
285+
# @return [Array<Hash>] the log output parsed into an array of hashs for each commit
286+
#
287+
# Each hash contains the following keys:
288+
# * 'sha' [String] the commit sha
289+
# * 'author' [String] the author of the commit
290+
# * 'message' [String] the commit message
291+
# * 'parent' [Array<String>] the commit shas of the parent commits
292+
# * 'tree' [String] the tree sha
293+
# * 'author' [String] the author of the commit and timestamp of when the changes were created
294+
# * 'committer' [String] the committer of the commit and timestamp of when the commit was applied
295+
#
296+
# @raise [ArgumentError] if the revision range (specified with :between or :object) is a string starting with a hyphen
297+
#
298+
def full_log_commits(opts = {})
299+
assert_args_are_not_options('between', opts[:between]&.first)
300+
assert_args_are_not_options('object', opts[:object])
301+
232302
arr_opts = log_common_options(opts)
233303

234304
arr_opts << '--pretty=raw'

tests/units/test_lib.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ def test_log_commits
123123
assert_equal(20, a.size)
124124
end
125125

126+
def test_log_commits_invalid_between
127+
# between can not start with a hyphen
128+
assert_raise ArgumentError do
129+
@lib.log_commits :count => 20, :between => ['-v2.5', 'v2.6']
130+
end
131+
end
132+
133+
def test_log_commits_invalid_object
134+
# :object can not start with a hyphen
135+
assert_raise ArgumentError do
136+
@lib.log_commits :count => 20, :object => '--all'
137+
end
138+
end
139+
140+
def test_full_log_commits_invalid_between
141+
# between can not start with a hyphen
142+
assert_raise ArgumentError do
143+
@lib.full_log_commits :count => 20, :between => ['-v2.5', 'v2.6']
144+
end
145+
end
146+
147+
def test_full_log_commits_invalid_object
148+
# :object can not start with a hyphen
149+
assert_raise ArgumentError do
150+
@lib.full_log_commits :count => 20, :object => '--all'
151+
end
152+
end
153+
126154
def test_git_ssh_from_environment_is_passed_to_binary
127155
saved_binary_path = Git::Base.config.binary_path
128156
saved_git_ssh = Git::Base.config.git_ssh

0 commit comments

Comments
 (0)