From 5fd4faa354ee15502e3aabce61697c3ec1e526a7 Mon Sep 17 00:00:00 2001 From: Kelly Heller Date: Wed, 27 May 2015 14:51:43 -0700 Subject: [PATCH 1/3] workaround for issue 182 (git add -p or git add -i with a subdirectory) inspired by pull request 218 using code from @PhilipDavis --- git-add--interactive.perl | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index 1fadd69f8809f9..d643fcce9d7c1a 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -177,6 +177,48 @@ sub run_cmd_pipe { die "$^O does not support: @invalid\n" if @invalid; my @args = map { m/ /o ? "\"$_\"": $_ } @_; return qx{@args}; + } elsif (($^O eq 'MSWin32' || $^O eq 'msys') && (scalar @_ > 200)) { + + # workaround for https://github.com/msysgit/git/issues/182 + # mostly the work of @PhilipDavis, circa 2014-08-07 + + my @myArgs = @_; + my $cmd = "@myArgs"; + my $path = "$ENV{APPDATA}"; + $path =~ s/\\/\//g; + + use File::Temp qw(tempfile); + # for troubleshooting, you might find it useful to eliminate the UNLINK setting: + my ($fhargs, $filename) = tempfile("$path/git-args-XXXXXX", UNLINK => 1); + + if (grep $_ eq "--", @myArgs) { + + $cmd = ""; + while ($myArgs[0] ne '--') { + $cmd = $cmd . shift(@myArgs) . " "; + } + + $cmd = $cmd . "-- "; + shift(@myArgs); + + foreach (@myArgs) { + # if files have whitespace in the name, quotes around $_ are mandatory: + print $fhargs "\"$_\" \n"; + } + + $fhargs->flush; + + # 2015 may 26: @kkheller using cat to xargs instead of "< $filename" + $cmd = "cat $filename | xargs -s 20000 " . $cmd; + } + + print "$filename\n"; + print "$cmd \n\n"; + + my $fh = undef; + open($fh, '-|', $cmd) or die; + return <$fh>; + } else { my $fh = undef; open($fh, '-|', @_) or die; From bd230485c1458f0dabc249082da7616076979930 Mon Sep 17 00:00:00 2001 From: Kelly Heller Date: Tue, 7 Jul 2015 13:42:36 -0700 Subject: [PATCH 2/3] moving the scope of the tempfile variable to inside the 'if' block, since that is all we need it for. also closing the file handle. --- git-add--interactive.perl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index d643fcce9d7c1a..cc10697fa22ec1 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -187,11 +187,10 @@ sub run_cmd_pipe { my $path = "$ENV{APPDATA}"; $path =~ s/\\/\//g; - use File::Temp qw(tempfile); - # for troubleshooting, you might find it useful to eliminate the UNLINK setting: - my ($fhargs, $filename) = tempfile("$path/git-args-XXXXXX", UNLINK => 1); - if (grep $_ eq "--", @myArgs) { + use File::Temp qw(tempfile); + # for troubleshooting, you might find it useful to eliminate the UNLINK setting: + my ($fhargs, $filename) = tempfile("$path/git-args-XXXXXX", UNLINK => 1); $cmd = ""; while ($myArgs[0] ne '--') { @@ -207,14 +206,12 @@ sub run_cmd_pipe { } $fhargs->flush; + close($fhargs); # 2015 may 26: @kkheller using cat to xargs instead of "< $filename" $cmd = "cat $filename | xargs -s 20000 " . $cmd; } - print "$filename\n"; - print "$cmd \n\n"; - my $fh = undef; open($fh, '-|', $cmd) or die; return <$fh>; From 717b5269f4f8b8846cf4a32410f2169dc94ab064 Mon Sep 17 00:00:00 2001 From: Kelly Heller Date: Tue, 7 Jul 2015 14:20:37 -0700 Subject: [PATCH 3/3] separating input to xargs using NUL, which is more standard. --- git-add--interactive.perl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index cc10697fa22ec1..56ce9591a0eadc 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -201,15 +201,14 @@ sub run_cmd_pipe { shift(@myArgs); foreach (@myArgs) { - # if files have whitespace in the name, quotes around $_ are mandatory: - print $fhargs "\"$_\" \n"; + print $fhargs "$_" . "\0"; } $fhargs->flush; close($fhargs); # 2015 may 26: @kkheller using cat to xargs instead of "< $filename" - $cmd = "cat $filename | xargs -s 20000 " . $cmd; + $cmd = "cat $filename | xargs -0 -s 20000 " . $cmd; } my $fh = undef;