* [PATCH 1/2] Add / command in add --patch (feature request)
@ 2008-11-26 20:51 William Pursell
2008-11-26 21:55 ` Junio C Hamano
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: William Pursell @ 2008-11-26 20:51 UTC (permalink / raw)
To: git
This sequence of 2 patches adds a '/' command to
add --patch that allows the user to search for
a hunk that matches a regex, and deals with j,k slightly
more gracefully. (Rather than printing the
help menu if k is invalid, it will print
a relevant error message.)
This is naive, and it is easy for an invalid
search string to cause a perl error.
I think it could be useful functionality to make
robust.
(Please CC me in any response)
---
git-add--interactive.perl | 26 ++++++++++++++++++++++----
1 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index b0223c3..7ad4ee0 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -876,12 +876,14 @@ sub patch_update_file {
$num = scalar @hunk;
$ix = 0;
+ my $search_s; # User entered string to match a hunk.
while (1) {
my ($prev, $next, $other, $undecided, $i);
$other = '';
if ($num <= $ix) {
+ $search_s = 0;
$ix = 0;
}
for ($i = 0; $i < $ix; $i++) {
@@ -916,11 +918,24 @@ sub patch_update_file {
$other .= '/s';
}
$other .= '/e';
- for (@{$hunk[$ix]{DISPLAY}}) {
- print;
+
+ my $line;
+ if( $search_s ) {
+ my $text = join( "", @{$hunk[$ix]{DISPLAY}} );
+ if( $text !~ $search_s ) {
+ $line = "n\n";
+ } else {
+ print $text;
+ }
+ } else {
+ for (@{$hunk[$ix]{DISPLAY}}) {
+ print;
+ }
+ }
+ if (!$line) {
+ print colored $prompt_color, "Stage this hunk [y/n/a/d///$other/?]? ";
+ $line = <STDIN>;
}
- print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
- my $line = <STDIN>;
if ($line) {
if ($line =~ /^y/i) {
$hunk[$ix]{USE} = 1;
@@ -946,6 +961,9 @@ sub patch_update_file {
}
next;
}
+ elsif ($line =~ m|^/(.*)|) {
+ $search_s = $1;
+ }
elsif ($other =~ /K/ && $line =~ /^K/) {
$ix--;
next;
--
1.6.0.4.781.gf2070.dirty
--
William Pursell
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-26 20:51 [PATCH 1/2] Add / command in add --patch (feature request) William Pursell
@ 2008-11-26 21:55 ` Junio C Hamano
2008-11-26 22:38 ` Jeff King
2008-11-27 1:46 ` Johannes Schindelin
2 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2008-11-26 21:55 UTC (permalink / raw)
To: William Pursell; +Cc: git
William Pursell <bill.pursell@gmail.com> writes:
> diff --git a/git-add--interactive.perl b/git-add--interactive.perl
> index b0223c3..7ad4ee0 100755
> --- a/git-add--interactive.perl
> +++ b/git-add--interactive.perl
> @@ -876,12 +876,14 @@ sub patch_update_file {
>
> $num = scalar @hunk;
> $ix = 0;
> + my $search_s; # User entered string to match a hunk.
>
> while (1) {
> my ($prev, $next, $other, $undecided, $i);
> $other = '';
>
> if ($num <= $ix) {
> + $search_s = 0;
People cannot look for string "0"?
Instead, set this to 'undef' and check with:
if (defined $search_string) {
...
in the later part of the code.
> @@ -916,11 +918,24 @@ sub patch_update_file {
> $other .= '/s';
> }
> $other .= '/e';
> - for (@{$hunk[$ix]{DISPLAY}}) {
> - print;
> +
> + my $line;
> + if( $search_s ) {
> + my $text = join( "", @{$hunk[$ix]{DISPLAY}} );
> + if( $text !~ $search_s ) {
Style.
(1) SP between language construct and open parenthesis, as opposed to
no extra SP between function name and open parenthesis;
(2) No extra SP around what is enclosed in parentheses.
No help text added to help people discover this new feature?
The interactive help prompt is hard to read because '/' is used to
separate choices. I'd suggest to make this into two patches:
Patch 1/2 would change use of '/' to ',' so that this:
Stage this hunk [y/n/a/d/j/J/e/?]?
becomes
Stage this hunk [y,n,a,d,j,J,e,?]?
Patch 2/2 would be a fix-up of the patch you sent.
Thanks.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-26 20:51 [PATCH 1/2] Add / command in add --patch (feature request) William Pursell
2008-11-26 21:55 ` Junio C Hamano
@ 2008-11-26 22:38 ` Jeff King
2008-11-26 22:54 ` Junio C Hamano
2008-11-27 1:46 ` Johannes Schindelin
2 siblings, 1 reply; 7+ messages in thread
From: Jeff King @ 2008-11-26 22:38 UTC (permalink / raw)
To: William Pursell; +Cc: git
On Wed, Nov 26, 2008 at 08:51:20PM +0000, William Pursell wrote:
> This is naive, and it is easy for an invalid
> search string to cause a perl error.
> [...]
> + if( $text !~ $search_s ) {
Yeah, a bad regex will cause the whole program to barf. Maybe wrap it in
an eval, like this?
my $r = eval { $text !~ $search_s };
if ($@) {
print STDERR "error in search string: $@\n";
next;
}
if ($r) {
...
Or similar (I didn't look at the code closely enough to know if "next"
is the right thing there).
-Peff
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-26 22:38 ` Jeff King
@ 2008-11-26 22:54 ` Junio C Hamano
2008-11-27 6:02 ` William Pursell
0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2008-11-26 22:54 UTC (permalink / raw)
To: Jeff King; +Cc: William Pursell, git
Jeff King <peff@peff.net> writes:
> On Wed, Nov 26, 2008 at 08:51:20PM +0000, William Pursell wrote:
>
>> This is naive, and it is easy for an invalid
>> search string to cause a perl error.
>> [...]
>> + if( $text !~ $search_s ) {
>
> Yeah, a bad regex will cause the whole program to barf. Maybe wrap it in
> an eval, like this?
>
> my $r = eval { $text !~ $search_s };
> if ($@) {
> print STDERR "error in search string: $@\n";
> next;
> }
> if ($r) {
> ...
>
> Or similar (I didn't look at the code closely enough to know if "next"
> is the right thing there).
Use of eval is a good way to protect against this kind of breakage, but it
should be done close to where the string is given by the user, perhaps in
here:
+ elsif ($line =~ m|^/(.*)|) {
+ $search_s = $1;
+ }
Something like...
elsif ($line =~ m|^/(.*)|) {
$search_string = $1;
eval {
$search_string =~ /$search_string/;
};
if ($@) {
print STDERR "Regexp error in $search_string: $@";
next;
}
...
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-26 22:54 ` Junio C Hamano
@ 2008-11-27 6:02 ` William Pursell
2008-11-27 6:41 ` Junio C Hamano
0 siblings, 1 reply; 7+ messages in thread
From: William Pursell @ 2008-11-27 6:02 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Jeff King, git
Junio C Hamano wrote:
>
> Use of eval is a good way to protect against this kind of breakage, but it
> should be done close to where the string is given by the user, perhaps in
> here:
>
>
> + elsif ($line =~ m|^/(.*)|) {
> + $search_s = $1;
> + }
>
> Something like...
>
> elsif ($line =~ m|^/(.*)|) {
> $search_string = $1;
> eval {
> $search_string =~ /$search_string/;
> };
> if ($@) {
> print STDERR "Regexp error in $search_string: $@";
> next;
> }
> ...
Thanks. The second set of patches that I just sent
up is fatally flawed--by changing to skip unmatched
hunks instead of deselecting them, it enters a loop
if no hunks match.
Before working on patches, I'd like some ideas on
functionality:
1) If a hunk doesn't match, should it be as if the user
selected 'n', or 'j'?
2) If no hunks match it is easiest to simply move to
the last hunk and display it, but I'm not sure that
is acceptable. Probably better to return to the
hunk that was being viewed when the search string
is entered, but that seems to require some restructuring
of the code. What would be the preferred behavior?
--
William Pursell
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-27 6:02 ` William Pursell
@ 2008-11-27 6:41 ` Junio C Hamano
0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2008-11-27 6:41 UTC (permalink / raw)
To: William Pursell; +Cc: Jeff King, git
William Pursell <bill.pursell@gmail.com> writes:
> Before working on patches, I'd like some ideas on
> functionality:
>
> 1) If a hunk doesn't match, should it be as if the user
> selected 'n', or 'j'?
Is it an option to tell "nothing matched", stay at the same hunk and ask
the user to make the choice again?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] Add / command in add --patch (feature request)
2008-11-26 20:51 [PATCH 1/2] Add / command in add --patch (feature request) William Pursell
2008-11-26 21:55 ` Junio C Hamano
2008-11-26 22:38 ` Jeff King
@ 2008-11-27 1:46 ` Johannes Schindelin
2 siblings, 0 replies; 7+ messages in thread
From: Johannes Schindelin @ 2008-11-27 1:46 UTC (permalink / raw)
To: William Pursell; +Cc: git
Hi,
On Wed, 26 Nov 2008, William Pursell wrote:
> This sequence of 2 patches adds a '/' command to
> add --patch that allows the user to search for
> a hunk that matches a regex, and deals with j,k slightly
> more gracefully. (Rather than printing the
> help menu if k is invalid, it will print
> a relevant error message.)
I find these references to j and k not only confusing, but slightly
unnerving. Care to be a bit more explicit?
> (Please CC me in any response)
Always on this list; we respect netiquette.
Ciao,
Dscho
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2008-11-27 6:49 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-26 20:51 [PATCH 1/2] Add / command in add --patch (feature request) William Pursell
2008-11-26 21:55 ` Junio C Hamano
2008-11-26 22:38 ` Jeff King
2008-11-26 22:54 ` Junio C Hamano
2008-11-27 6:02 ` William Pursell
2008-11-27 6:41 ` Junio C Hamano
2008-11-27 1:46 ` Johannes Schindelin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox