Skip to content

Commit b7008aa

Browse files
gnustavobook
authored andcommitted
Add input_record_separator option to Git::Repository::Command::new
This option specifies a string which the $/ special variable is set to during the command execution. This allows one to read line by line the output of Git commands when passing the -z option to them. One only needs to specify the "\0" string as the input_record_separator. This implements RT #134239.
1 parent 34f507e commit b7008aa

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/Git/Repository/Command.pm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ sub final_output {
174174
my ($self, @cb) = @_;
175175

176176
# get output / errput
177+
my $input_record_separator =
178+
exists $self->options->{input_record_separator}
179+
? $self->options->{input_record_separator}
180+
: "\n";
177181
my ( @output, @errput );
178182
$self->loop_on(
179-
input_record_separator => "\n",
183+
input_record_separator => $input_record_separator,
180184
stdout => sub { chomp( my $o = shift ); push @output, $o; },
181185
stderr => sub { chomp( my $e = shift ); push @errput, $e; },
182186
);
@@ -333,6 +337,20 @@ Boolean option to control the output of warnings.
333337
If true, methods such as C<final_output()> will not warn when Git outputs
334338
messages on C<STDERR>.
335339
340+
=item C<input_record_separator>
341+
342+
A string which the C<$/> special variable is set to during the command
343+
execution.
344+
345+
This is mostly useful when passing the C<-z> option to a Git command so that it
346+
output lines separated by the NUL character and you want to get it line by line,
347+
like this:
348+
349+
my @files = $git->run(
350+
qw/ls-tree -r --name-only -z HEAD/,
351+
{ input_record_separator => "\0" },
352+
);
353+
336354
=back
337355
338356
If the L<Git::Repository> object has its own option hash, it will be used

t/20-simple.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,10 @@ ok( $r = eval { Git::Repository->new( work_tree => $dir ) },
371371
$tree = $r->run( mktree => { input => '' } );
372372
is( $tree, '4b825dc642cb6eb9a060e54bf8d69288fbee4904', 'mktree empty tree' );
373373

374+
# use ls-tree with input_record_separator option
375+
BEGIN { $tests += 1 }
376+
my @files = $r->run(
377+
qw/ls-tree --name-only -z HEAD/,
378+
{ input_record_separator => "\0" },
379+
);
380+
ok( @files > 1, 'option input_record_separator in run() worked');

0 commit comments

Comments
 (0)