Skip to content

Commit ae9cf9a

Browse files
committed
Allow disabling chomping of #command output
This allows the #show method to return the actual content of a file when this file ends by a line feed.
1 parent 81f9e2d commit ae9cf9a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/git/lib.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def show(objectish=nil, path=nil)
492492

493493
arr_opts << (path ? "#{objectish}:#{path}" : objectish)
494494

495-
command('show', arr_opts.compact)
495+
command('show', arr_opts.compact, chomp: false)
496496
end
497497

498498
## WRITE COMMANDS ##
@@ -923,12 +923,12 @@ def with_custom_env_variables(&block)
923923
end
924924

925925
def command(cmd, *opts, &block)
926-
command_opts = { redirect: '' }
926+
command_opts = { chomp: true, redirect: '' }
927927
if opts.last.is_a?(Hash)
928928
command_opts.merge!(opts.pop)
929929
end
930930
command_opts.keys.each do |k|
931-
raise ArgumentError.new("Unsupported option: #{k}") unless [:redirect].include?(k)
931+
raise ArgumentError.new("Unsupported option: #{k}") unless [:chomp, :redirect].include?(k)
932932
end
933933

934934
default_command_opts = { redirect: '' }
@@ -967,6 +967,10 @@ def command(cmd, *opts, &block)
967967
raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s)
968968
end
969969

970+
if command_opts[:chomp]
971+
output.chomp! if output
972+
end
973+
970974
return output
971975
end
972976

@@ -1026,7 +1030,7 @@ def log_path_options(opts)
10261030
def run_command(git_cmd, &block)
10271031
return IO.popen(git_cmd, &block) if block_given?
10281032

1029-
`#{git_cmd}`.chomp
1033+
`#{git_cmd}`
10301034
end
10311035

10321036
def escape(s)

tests/units/test_lib.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def test_show
226226
assert(/^commit 5e53019b3238362144c2766f02a2c00d91fcc023.+\+replace with new text - diff test$/m.match(@lib.show))
227227
assert(/^commit 935badc874edd62a8629aaf103418092c73f0a56.+\+nothing!$/m.match(@lib.show('gitsearch1')))
228228
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
229+
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
229230
end
230231

231232
end

0 commit comments

Comments
 (0)