Skip to content

Commit f9e024f

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 78fd97e commit f9e024f

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
@@ -495,7 +495,7 @@ def show(objectish=nil, path=nil)
495495

496496
arr_opts << (path ? "#{objectish}:#{path}" : objectish)
497497

498-
command('show', arr_opts.compact)
498+
command('show', arr_opts.compact, chomp: false)
499499
end
500500

501501
## WRITE COMMANDS ##
@@ -934,12 +934,12 @@ def with_custom_env_variables(&block)
934934
end
935935

936936
def command(cmd, *opts, &block)
937-
command_opts = { redirect: '' }
937+
command_opts = { chomp: true, redirect: '' }
938938
if opts.last.is_a?(Hash)
939939
command_opts.merge!(opts.pop)
940940
end
941941
command_opts.keys.each do |k|
942-
raise ArgumentError.new("Unsupported option: #{k}") unless [:redirect].include?(k)
942+
raise ArgumentError.new("Unsupported option: #{k}") unless [:chomp, :redirect].include?(k)
943943
end
944944

945945
default_command_opts = { redirect: '' }
@@ -978,6 +978,10 @@ def command(cmd, *opts, &block)
978978
raise Git::GitExecuteError.new(git_cmd + ':' + output.to_s)
979979
end
980980

981+
if command_opts[:chomp]
982+
output.chomp! if output
983+
end
984+
981985
return output
982986
end
983987

@@ -1037,7 +1041,7 @@ def log_path_options(opts)
10371041
def run_command(git_cmd, &block)
10381042
return IO.popen(git_cmd, &block) if block_given?
10391043

1040-
`#{git_cmd}`.chomp
1044+
`#{git_cmd}`
10411045
end
10421046

10431047
def escape(s)

tests/units/test_lib.rb

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

244245
end

0 commit comments

Comments
 (0)