threads / rfc / 18826

RFC patchgit add -p: new "quit" command at the prompt.

Subject: [RFC PATCH] git add -p: new "quit" command at the prompt.

## tl;dr

15 messages between Apr 10, 2009 and Apr 16, 2009. Diffs are folded; open one to read it.

replies: 14people: 7as markdown or json

Matthieu Moy· Apr 10, 2009, 14:57 UTC · lore

There's already 'd' to stop staging hunks in a file, but no command to stop the interactive staging (for the current files and the remaining ones). This patch implements this functionality, and binds it to 'q'. ---

I'm not familiar at all with the code in git-add--interactive.perl, so my code is mostly cut-and-pasted+adapted from the 'd' command. Probably suboptimal.

 git-add--interactive.perl |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
Show changes to git-add--interactive.perl +19 −1
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index def062a..210d230 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -894,6 +894,7 @@ sub help_patch_cmd {
 	print colored $help_color, <<\EOF ;
 y - stage this hunk
 n - do not stage this hunk
+q - quit, do not stage this hunk nor any of the remaining ones
 a - stage this and all the remaining hunks in the file
 d - do not stage this hunk nor any of the remaining hunks in the file
 g - select a hunk to go to
@@ -930,7 +931,7 @@ sub patch_update_cmd {
 					@mods);
 	}
 	for (@them) {
-		patch_update_file($_->{VALUE});
+		return 0 if patch_update_file($_->{VALUE});
 	}
 }
 
@@ -976,6 +977,7 @@ sub display_hunks {
 }
 
 sub patch_update_file {
+	my $quit = 0;
 	my ($ix, $num);
 	my $path = shift;
 	my ($head, @hunk) = parse_diff($path);
@@ -1006,6 +1008,11 @@ sub patch_update_file {
 				$_->{USE} = 0 foreach ($mode, @hunk);
 				last;
 			}
+			elsif ($line =~ /^q/i) {
+				$_->{USE} = 0 foreach ($mode, @hunk);
+				$quit = 1;
+				last;
+			}
 			else {
 				help_patch_cmd('');
 				next;
@@ -1113,6 +1120,16 @@ sub patch_update_file {
 				}
 				next;
 			}
+			elsif ($line =~ /^q/i) {
+				while ($ix < $num) {
+					if (!defined $hunk[$ix]{USE}) {
+						$hunk[$ix]{USE} = 0;
+					}
+					$ix++;
+				}
+				$quit = 1;
+				next;
+			}
 			elsif ($line =~ m|^/(.*)|) {
 				my $regex = $1;
 				if ($1 eq "") {
@@ -1239,6 +1256,7 @@ sub patch_update_file {
 	}
 
 	print "\n";
+	return $quit;
 }
 
 sub diff_cmd {
-- 
1.6.2.2.449.g92961.dirty
Junio C Hamano· Apr 11, 2009, 19:22 UTC · re: Matthieu Moy · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 7 quoted lines
> There's already 'd' to stop staging hunks in a file, but no command to
> stop the interactive staging (for the current files and the remaining
> ones). This patch implements this functionality, and binds it to 'q'.
> ---
>
> I'm not familiar at all with the code in git-add--interactive.perl, so
> my code is mostly cut-and-pasted+adapted from the 'd' command.
You can say 'd' and then ^C, I think.
Matthieu Moy· Apr 12, 2009, 12:45 UTC · re: Junio C Hamano · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Junio C Hamano <gitster@pobox.com> writes:
Show 11 quoted lines
> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
>
>> There's already 'd' to stop staging hunks in a file, but no command to
>> stop the interactive staging (for the current files and the remaining
>> ones). This patch implements this functionality, and binds it to 'q'.
>> ---
>>
>> I'm not familiar at all with the code in git-add--interactive.perl, so
>> my code is mostly cut-and-pasted+adapted from the 'd' command.
>
> You can say 'd' and then ^C, I think.

Yes, you /can/, and that's what I'm doing right now in this situation. But that's undocumented, not so intuitive (I found out I could do that after trying ^C alone, which doesn't work, staged content is recorded on disk at the end of the file only, not after each prompt), ...

I thought the situation was common enough to deserve an explicit command. The 'd' command is natural for "git add -i" + patch subcommand, but for "git add -p", I found 'd' mostly useless, and I really want a "quit" command.

Sure, I can live without it, but if other people would like to have it, please speak now ;-).

-- 
Matthieu
Sverre Rabbelier· Apr 12, 2009, 12:54 UTC · re: Matthieu Moy · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Heya,
On Sunday, April 12, 2009, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
> Sure, I can live without it, but if other people would like to have
> it, please speak now ;-).
Yes please, I would like a q command to git add -p too!

-- Cheers,

Sverre Rabbelier
-- 
Cheers,

Sverre Rabbelier
Miles Bader· Apr 13, 2009, 01:39 UTC · re: Sverre Rabbelier · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Sverre Rabbelier <srabbelier@gmail.com> writes:
> Yes please, I would like a q command to git add -p too!
Me too.

Even though I've used add -p a lot, I _still_ find myself looking for a quit sub-command sometimes and being momentarily confused because there isn't one... it just seems such a natural thing...

-Miles
-- 
Would you like fries with that?
Nicolas Sebrecht· Apr 12, 2009, 17:29 UTC · re: Matthieu Moy · lore

[RFC PATCH] Re: git add -p: new "quit" command at the prompt.

On Sun, Apr 12, 2009 at 02:45:23PM +0200, Matthieu Moy wrote:
Show 7 quoted lines
> I thought the situation was common enough to deserve an explicit
> command. The 'd' command is natural for "git add -i" + patch
> subcommand, but for "git add -p", I found 'd' mostly useless, and I
> really want a "quit" command.
> 
> Sure, I can live without it, but if other people would like to have
> it, please speak now ;-).
Acked-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
-- 
Nicolas Sebrecht
Wincent Colaiuta· Apr 13, 2009, 16:38 UTC · re: Matthieu Moy · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

El 12/4/2009, a las 14:45, Matthieu Moy escribió:
Show 16 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> You can say 'd' and then ^C, I think.
>
> Yes, you /can/, and that's what I'm doing right now in this situation.
> But that's undocumented, not so intuitive (I found out I could do that
> after trying ^C alone, which doesn't work, staged content is recorded
> on disk at the end of the file only, not after each prompt), ...
>
> I thought the situation was common enough to deserve an explicit
> command. The 'd' command is natural for "git add -i" + patch
> subcommand, but for "git add -p", I found 'd' mostly useless, and I
> really want a "quit" command.
>
> Sure, I can live without it, but if other people would like to have
> it, please speak now ;-).

Yes, I'd like it too. I've been using ^C a lot, but I'd never noticed that changes weren't staged except at the end of each file. Thanks for bringing it up; you might have saved some people (including me) from being bitten by it at some point.

Wincent
Matthieu Moy· Apr 14, 2009, 20:44 UTC · re: Matthieu Moy · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 26 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
>>
>>> There's already 'd' to stop staging hunks in a file, but no command to
>>> stop the interactive staging (for the current files and the remaining
>>> ones). This patch implements this functionality, and binds it to 'q'.
>>> ---
>>>
>>> I'm not familiar at all with the code in git-add--interactive.perl, so
>>> my code is mostly cut-and-pasted+adapted from the 'd' command.
>>
>> You can say 'd' and then ^C, I think.
>
> Yes, you /can/, and that's what I'm doing right now in this situation.
> But that's undocumented, not so intuitive (I found out I could do that
> after trying ^C alone, which doesn't work, staged content is recorded
> on disk at the end of the file only, not after each prompt), ...
>
> I thought the situation was common enough to deserve an explicit
> command. The 'd' command is natural for "git add -i" + patch
> subcommand, but for "git add -p", I found 'd' mostly useless, and I
> really want a "quit" command.
>
> Sure, I can live without it, but if other people would like to have
> it, please speak now ;-).

I think everybody agree that the intention of the patch is good (well, several pro, and no real counter-argument). I'll send an updated version with documentation soon, but I'd appreciate review and comments on the code.

I'm not really happy with the fact that I mainly cut-and-pasted code from the "d" command, but OTOH, that's already how the code is today (huge if/elsif/... with similar elsif blocks for each command).

-- 
Matthieu
Junio C Hamano· Apr 15, 2009, 23:25 UTC · re: Matthieu Moy · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
> I think everybody agree that the intention of the patch is good (well,
> several pro, and no real counter-argument).

Oh, I think we have already passed that state long time ago. It's queued as c9cc8d9 ([NEEDS SIGN OFF, DOC, AND REVIEW] git add -p: new "quit" command at the prompt., 2009-04-10) in 'pu'.

I agree that there is a "Huh?" factor in the repeated and similar hunks to patch_update_file, but the first hunk is dealing with the mode change and the second one is about the patch text.

Currently parse_diff_header() returns the mode line into $mode and the main loop treats $mode differently from @hunk, which is an array of hunks parsed by parse_diff(). Treating $mode as a "fake hunk" by unshifting it at the beginning of @hunk array and teaching the main loop minor details such as $mode "fake hunk" cannot be edited nor split, I suspect we _could_ unify the two.

ca72468 (add--interactive: allow user to choose mode update, 2008-03-27) introduced this duplication. Jeff, what do you think? I am not sure if it is worth it.

Jeff King· Apr 16, 2009, 06:00 UTC · re: Junio C Hamano · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

On Wed, Apr 15, 2009 at 04:25:55PM -0700, Junio C Hamano wrote:
Show 10 quoted lines
> Currently parse_diff_header() returns the mode line into $mode and the
> main loop treats $mode differently from @hunk, which is an array of hunks
> parsed by parse_diff().  Treating $mode as a "fake hunk" by unshifting it
> at the beginning of @hunk array and teaching the main loop minor details
> such as $mode "fake hunk" cannot be edited nor split, I suspect we _could_
> unify the two.
> 
> ca72468 (add--interactive: allow user to choose mode update, 2008-03-27)
> introduced this duplication.  Jeff, what do you think?  I am not sure if
> it is worth it.
Yeah, at least at one point I thought that was possible:
  http://article.gmane.org/gmane.comp.version-control.git/78340
Let me see how painful it would be.
-Peff
Jeff King· Apr 16, 2009, 06:52 UTC · re: Jeff King · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

On Thu, Apr 16, 2009 at 02:00:27AM -0400, Jeff King wrote:
Show 8 quoted lines
> > Currently parse_diff_header() returns the mode line into $mode and the
> > main loop treats $mode differently from @hunk, which is an array of hunks
> > parsed by parse_diff().  Treating $mode as a "fake hunk" by unshifting it
> > at the beginning of @hunk array and teaching the main loop minor details
> > such as $mode "fake hunk" cannot be edited nor split, I suspect we _could_
> > unify the two.
>
> Let me see how painful it would be.

Not too painful at all. We can _almost_ get away with a really minimal "just unshift it" patch, but calling splittable_hunk() generates some perl warnings (because the hunk lacks @@ lines), and doing a hunk edit seems to confuse the code (it duplicates the "old mode" line for some reason). It is not worth trying to fix, though, as editing the mode hunk is not useful in practice (see the commit message below).

It would also be possible to refactor the header parsing to just pull the mode lines out as we are parsing the hunks. However, it is probably not worth the added complexity: you have header lines, then some mode lines, then some more header lines, then some hunks. So you have to keep more state around to do a linear parse.

Anyway, I think this is a nice improvement on its own, and it should make Matthieu's patch a little cleaner.

-- >8 --
Subject: [PATCH] add-interactive: refactor mode hunk handling

The original implementation considered the mode separately from the rest of the hunks, asking about it outside the main hunk-selection loop. This patch instead places a mode change as the first hunk in the loop. This has two advantages:

  1. less duplicated code (since we use the main selection
     loop). This also cleans up an inconsistency, which is
     that the main selection loop separates options with a
     comma, whereas the mode prompt used slashes.
  2. users can now skip the mode change and come back to it,
     search for it (via "/mode"), etc, as they can with other
     hunks.

To facilitate this, each hunk is now marked with a "type". Mode hunks are not considered for splitting (which would make no sense, and also confuses the split_hunk function), nor are they editable. In theory, one could edit the mode lines and change to an entirely new mode. In practice, there are only two modes that git cares about (0644 and 0755), so either you want to move from one to the other or not (and you can do that by staging or not staging).

Signed-off-by: Jeff King <peff@peff.net>
---
 git-add--interactive.perl |   59 ++++++++++++++++-----------------------------
 1 files changed, 21 insertions(+), 38 deletions(-)
Show changes to git-add--interactive.perl +21 −38
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index def062a..b895e3b 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -620,11 +620,12 @@ sub parse_diff {
 	if ($diff_use_color) {
 		@colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
 	}
-	my (@hunk) = { TEXT => [], DISPLAY => [] };
+	my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
 
 	for (my $i = 0; $i < @diff; $i++) {
 		if ($diff[$i] =~ /^@@ /) {
-			push @hunk, { TEXT => [], DISPLAY => [] };
+			push @hunk, { TEXT => [], DISPLAY => [],
+				TYPE => 'hunk' };
 		}
 		push @{$hunk[-1]{TEXT}}, $diff[$i];
 		push @{$hunk[-1]{DISPLAY}},
@@ -636,8 +637,8 @@ sub parse_diff {
 sub parse_diff_header {
 	my $src = shift;
 
-	my $head = { TEXT => [], DISPLAY => [] };
-	my $mode = { TEXT => [], DISPLAY => [] };
+	my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
+	my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
 
 	for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
 		my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
@@ -684,6 +685,7 @@ sub split_hunk {
 		my $this = +{
 			TEXT => [],
 			DISPLAY => [],
+			TYPE => 'hunk',
 			OLD => $o_ofs,
 			NEW => $n_ofs,
 			OCNT => 0,
@@ -873,7 +875,11 @@ sub edit_hunk_loop {
 		if (!defined $text) {
 			return undef;
 		}
-		my $newhunk = { TEXT => $text, USE => 1 };
+		my $newhunk = {
+			TEXT => $text,
+			TYPE => $hunk->[$ix]->{TYPE},
+			USE => 1
+		};
 		if (diff_applies($head,
 				 @{$hunk}[0..$ix-1],
 				 $newhunk,
@@ -985,32 +991,7 @@ sub patch_update_file {
 	}
 
 	if (@{$mode->{TEXT}}) {
-		while (1) {
-			print @{$mode->{DISPLAY}};
-			print colored $prompt_color,
-				"Stage mode change [y/n/a/d/?]? ";
-			my $line = prompt_single_character;
-			if ($line =~ /^y/i) {
-				$mode->{USE} = 1;
-				last;
-			}
-			elsif ($line =~ /^n/i) {
-				$mode->{USE} = 0;
-				last;
-			}
-			elsif ($line =~ /^a/i) {
-				$_->{USE} = 1 foreach ($mode, @hunk);
-				last;
-			}
-			elsif ($line =~ /^d/i) {
-				$_->{USE} = 0 foreach ($mode, @hunk);
-				last;
-			}
-			else {
-				help_patch_cmd('');
-				next;
-			}
-		}
+		unshift @hunk, $mode;
 	}
 
 	$num = scalar @hunk;
@@ -1054,14 +1035,19 @@ sub patch_update_file {
 		}
 		last if (!$undecided);
 
-		if (hunk_splittable($hunk[$ix]{TEXT})) {
+		if ($hunk[$ix]{TYPE} eq 'hunk' &&
+		    hunk_splittable($hunk[$ix]{TEXT})) {
 			$other .= ',s';
 		}
-		$other .= ',e';
+		if ($hunk[$ix]{TYPE} eq 'hunk') {
+			$other .= ',e';
+		}
 		for (@{$hunk[$ix]{DISPLAY}}) {
 			print;
 		}
-		print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
+		print colored $prompt_color, 'Stage ',
+		  ($hunk[$ix]{TYPE} eq 'mode' ? 'mode change' : 'this hunk'),
+		  " [y,n,a,d,/$other,?]? ";
 		my $line = prompt_single_character;
 		if ($line) {
 			if ($line =~ /^y/i) {
@@ -1193,7 +1179,7 @@ sub patch_update_file {
 				$num = scalar @hunk;
 				next;
 			}
-			elsif ($line =~ /^e/) {
+			elsif ($other =~ /e/ && $line =~ /^e/) {
 				my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
 				if (defined $newhunk) {
 					splice @hunk, $ix, 1, $newhunk;
@@ -1214,9 +1200,6 @@ sub patch_update_file {
 
 	my $n_lofs = 0;
 	my @result = ();
-	if ($mode->{USE}) {
-		push @result, @{$mode->{TEXT}};
-	}
 	for (@hunk) {
 		if ($_->{USE}) {
 			push @result, @{$_->{TEXT}};
-- 
1.6.3.rc0.157.g02b29.dirty
Jeff King· Apr 16, 2009, 07:14 UTC · re: Jeff King · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

On Thu, Apr 16, 2009 at 02:52:23AM -0400, Jeff King wrote:
> Anyway, I think this is a nice improvement on its own, and it should
> make Matthieu's patch a little cleaner.

Hmm, it looks like you just applied Matthieu's patch to next already. Here is the rebased version of mine (the conflict resolution was pretty trivial, though: just delete the newly added 'q' option from the mode loop, which no longer exists).

-- >8 --
Subject: [PATCH] add-interactive: refactor mode hunk handling

The original implementation considered the mode separately from the rest of the hunks, asking about it outside the main hunk-selection loop. This patch instead places a mode change as the first hunk in the loop. This has two advantages:

  1. less duplicated code (since we use the main selection
     loop). This also cleans up an inconsistency, which is
     that the main selection loop separates options with a
     comma, whereas the mode prompt used slashes.
  2. users can now skip the mode change and come back to it,
     search for it (via "/mode"), etc, as they can with other
     hunks.

To facilitate this, each hunk is now marked with a "type". Mode hunks are not considered for splitting (which would make no sense, and also confuses the split_hunk function), nor are they editable. In theory, one could edit the mode lines and change to a new mode. In practice, there are only two modes that git cares about (0644 and 0755), so either you want to move from one to the other or not (and you can do that by staging or not staging).

Signed-off-by: Jeff King <peff@peff.net>
---
 git-add--interactive.perl |   64 ++++++++++++++------------------------------
 1 files changed, 21 insertions(+), 43 deletions(-)
Show changes to git-add--interactive.perl +21 −43
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 210d230..60dd1b5 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -620,11 +620,12 @@ sub parse_diff {
 	if ($diff_use_color) {
 		@colored = run_cmd_pipe(qw(git diff-files -p --color --), $path);
 	}
-	my (@hunk) = { TEXT => [], DISPLAY => [] };
+	my (@hunk) = { TEXT => [], DISPLAY => [], TYPE => 'header' };
 
 	for (my $i = 0; $i < @diff; $i++) {
 		if ($diff[$i] =~ /^@@ /) {
-			push @hunk, { TEXT => [], DISPLAY => [] };
+			push @hunk, { TEXT => [], DISPLAY => [],
+				TYPE => 'hunk' };
 		}
 		push @{$hunk[-1]{TEXT}}, $diff[$i];
 		push @{$hunk[-1]{DISPLAY}},
@@ -636,8 +637,8 @@ sub parse_diff {
 sub parse_diff_header {
 	my $src = shift;
 
-	my $head = { TEXT => [], DISPLAY => [] };
-	my $mode = { TEXT => [], DISPLAY => [] };
+	my $head = { TEXT => [], DISPLAY => [], TYPE => 'header' };
+	my $mode = { TEXT => [], DISPLAY => [], TYPE => 'mode' };
 
 	for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
 		my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
@@ -684,6 +685,7 @@ sub split_hunk {
 		my $this = +{
 			TEXT => [],
 			DISPLAY => [],
+			TYPE => 'hunk',
 			OLD => $o_ofs,
 			NEW => $n_ofs,
 			OCNT => 0,
@@ -873,7 +875,11 @@ sub edit_hunk_loop {
 		if (!defined $text) {
 			return undef;
 		}
-		my $newhunk = { TEXT => $text, USE => 1 };
+		my $newhunk = {
+			TEXT => $text,
+			TYPE => $hunk->[$ix]->{TYPE},
+			USE => 1
+		};
 		if (diff_applies($head,
 				 @{$hunk}[0..$ix-1],
 				 $newhunk,
@@ -987,37 +993,7 @@ sub patch_update_file {
 	}
 
 	if (@{$mode->{TEXT}}) {
-		while (1) {
-			print @{$mode->{DISPLAY}};
-			print colored $prompt_color,
-				"Stage mode change [y/n/a/d/?]? ";
-			my $line = prompt_single_character;
-			if ($line =~ /^y/i) {
-				$mode->{USE} = 1;
-				last;
-			}
-			elsif ($line =~ /^n/i) {
-				$mode->{USE} = 0;
-				last;
-			}
-			elsif ($line =~ /^a/i) {
-				$_->{USE} = 1 foreach ($mode, @hunk);
-				last;
-			}
-			elsif ($line =~ /^d/i) {
-				$_->{USE} = 0 foreach ($mode, @hunk);
-				last;
-			}
-			elsif ($line =~ /^q/i) {
-				$_->{USE} = 0 foreach ($mode, @hunk);
-				$quit = 1;
-				last;
-			}
-			else {
-				help_patch_cmd('');
-				next;
-			}
-		}
+		unshift @hunk, $mode;
 	}
 
 	$num = scalar @hunk;
@@ -1061,14 +1037,19 @@ sub patch_update_file {
 		}
 		last if (!$undecided);
 
-		if (hunk_splittable($hunk[$ix]{TEXT})) {
+		if ($hunk[$ix]{TYPE} eq 'hunk' &&
+		    hunk_splittable($hunk[$ix]{TEXT})) {
 			$other .= ',s';
 		}
-		$other .= ',e';
+		if ($hunk[$ix]{TYPE} eq 'hunk') {
+			$other .= ',e';
+		}
 		for (@{$hunk[$ix]{DISPLAY}}) {
 			print;
 		}
-		print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
+		print colored $prompt_color, 'Stage ',
+		  ($hunk[$ix]{TYPE} eq 'mode' ? 'mode change' : 'this hunk'),
+		  " [y,n,a,d,/$other,?]? ";
 		my $line = prompt_single_character;
 		if ($line) {
 			if ($line =~ /^y/i) {
@@ -1210,7 +1191,7 @@ sub patch_update_file {
 				$num = scalar @hunk;
 				next;
 			}
-			elsif ($line =~ /^e/) {
+			elsif ($other =~ /e/ && $line =~ /^e/) {
 				my $newhunk = edit_hunk_loop($head, \@hunk, $ix);
 				if (defined $newhunk) {
 					splice @hunk, $ix, 1, $newhunk;
@@ -1231,9 +1212,6 @@ sub patch_update_file {
 
 	my $n_lofs = 0;
 	my @result = ();
-	if ($mode->{USE}) {
-		push @result, @{$mode->{TEXT}};
-	}
 	for (@hunk) {
 		if ($_->{USE}) {
 			push @result, @{$_->{TEXT}};
-- 
1.6.3.rc0.204.g6bb2
Matthieu Moy· Apr 16, 2009, 16:44 UTC · re: Jeff King · lore

Re: [RFC PATCH] git add -p: new "quit" command at the prompt.

Jeff King <peff@peff.net> writes:
Show 7 quoted lines
> On Thu, Apr 16, 2009 at 02:52:23AM -0400, Jeff King wrote:
>
>> Anyway, I think this is a nice improvement on its own, and it should
>> make Matthieu's patch a little cleaner.
>
> Hmm, it looks like you just applied Matthieu's patch to next
> already.

2 things : my patch was lacking a Signed-off-by:, and your (Junio) modification to add the documentation has a typo (quite instead of quit). I'm resending the patch with a Signed-off-by, in case, and another patch fixes the typo (and updates the help with 'g' and '/').

-- 
Matthieu
Matthieu Moy· Apr 16, 2009, 16:46 UTC · re: Matthieu Moy · lore

[PATCH 1/2] git add -p: new "quit" command at the prompt.

There's already 'd' to stop staging hunks in a file, but no explicit command to stop the interactive staging (for the current files and the remaining ones). Of course you can do 'd' and then ^C, but it would be more intuitive to allow 'quit' action.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/git-add.txt |    1 +
 git-add--interactive.perl |   20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletions(-)
Show changes to 2 files +20 −1

Documentation/git-add.txt, git-add--interactive.perl

diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index ce71838..709e04b 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -245,6 +245,7 @@ patch::
 
        y - stage this hunk
        n - do not stage this hunk
+       q - quite, do not stage this hunk nor any of the remaining ones
        a - stage this and all the remaining hunks in the file
        d - do not stage this hunk nor any of the remaining hunks in the file
        j - leave this hunk undecided, see next undecided hunk
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index def062a..210d230 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -894,6 +894,7 @@ sub help_patch_cmd {
 	print colored $help_color, <<\EOF ;
 y - stage this hunk
 n - do not stage this hunk
+q - quit, do not stage this hunk nor any of the remaining ones
 a - stage this and all the remaining hunks in the file
 d - do not stage this hunk nor any of the remaining hunks in the file
 g - select a hunk to go to
@@ -930,7 +931,7 @@ sub patch_update_cmd {
 					@mods);
 	}
 	for (@them) {
-		patch_update_file($_->{VALUE});
+		return 0 if patch_update_file($_->{VALUE});
 	}
 }
 
@@ -976,6 +977,7 @@ sub display_hunks {
 }
 
 sub patch_update_file {
+	my $quit = 0;
 	my ($ix, $num);
 	my $path = shift;
 	my ($head, @hunk) = parse_diff($path);
@@ -1006,6 +1008,11 @@ sub patch_update_file {
 				$_->{USE} = 0 foreach ($mode, @hunk);
 				last;
 			}
+			elsif ($line =~ /^q/i) {
+				$_->{USE} = 0 foreach ($mode, @hunk);
+				$quit = 1;
+				last;
+			}
 			else {
 				help_patch_cmd('');
 				next;
@@ -1113,6 +1120,16 @@ sub patch_update_file {
 				}
 				next;
 			}
+			elsif ($line =~ /^q/i) {
+				while ($ix < $num) {
+					if (!defined $hunk[$ix]{USE}) {
+						$hunk[$ix]{USE} = 0;
+					}
+					$ix++;
+				}
+				$quit = 1;
+				next;
+			}
 			elsif ($line =~ m|^/(.*)|) {
 				my $regex = $1;
 				if ($1 eq "") {
@@ -1239,6 +1256,7 @@ sub patch_update_file {
 	}
 
 	print "\n";
+	return $quit;
 }
 
 sub diff_cmd {
-- 
1.6.2.2.449.g92961.dirty
Matthieu Moy· Apr 16, 2009, 16:46 UTC · re: Matthieu Moy · lore

[PATCH 2/2] Update git-add.txt according to the new possibilities of 'git add -p'.

The text is merely cut-and-pasted from git-add--interactive.perl. The cut-and-paste also fixes a typo.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 Documentation/git-add.txt |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Show changes to Documentation/git-add.txt +3 −1
diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index 709e04b..d938b42 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -245,9 +245,11 @@ patch::
 
        y - stage this hunk
        n - do not stage this hunk
-       q - quite, do not stage this hunk nor any of the remaining ones
+       q - quit, do not stage this hunk nor any of the remaining ones
        a - stage this and all the remaining hunks in the file
        d - do not stage this hunk nor any of the remaining hunks in the file
+       g - select a hunk to go to
+       / - search for a hunk matching the given regex
        j - leave this hunk undecided, see next undecided hunk
        J - leave this hunk undecided, see next hunk
        k - leave this hunk undecided, see previous undecided hunk
-- 
1.6.2.2.449.g92961.dirty

← back to recent threads