git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] add--interactive: allow user to choose mode update

From
Jeff King <peff@peff.net>
Date
Mar 27, 2008, 07:32 UTC
Message-ID
<20080327073225.GC22444@coredump.intra.peff.net>
In-Reply-To
<cover.1206602393.git.peff@peff.net>

When using the 'p'atch command, instead of just throwing out any mode change, let's present it to the user in the same way that we show hunks.

Signed-off-by: Jeff King <peff@peff.net>
---
This is the "minimal intrusion" implementation. It always asks about the
mode just once at the beginning of the file. I think you could probably
refactor the hunk handling so that the mode change went into the main
hunk loop, and you could skip it, go back to it, etc, like a regular
hunk.
 git-add--interactive.perl  |   33 +++++++++++++++++++++++++++++++++
 t/t3701-add-interactive.sh |   12 +++++++++++-
 2 files changed, 44 insertions(+), 1 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 5cdda29..903953e 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -814,6 +814,36 @@ sub patch_update_file {
 	for (@{$head->{DISPLAY}}) {
 		print;
 	}
+
+	if (@{$mode->{TEXT}}) {
+		while (1) {
+			print @{$mode->{DISPLAY}};
+			print colored $prompt_color,
+				"Stage mode change [y/n/a/d/?]? ";
+			my $line = <STDIN>;
+			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;
+			}
+		}
+	}
+
 	$num = scalar @hunk;
 	$ix = 0;
 
@@ -936,6 +966,9 @@ sub patch_update_file {
 
 	my $n_lofs = 0;
 	my @result = ();
+	if ($mode->{USE}) {
+		push @result, @{$mode->{TEXT}};
+	}
 	for (@hunk) {
 		my $text = $_->{TEXT};
 		my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index d920d06..f15be93 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -70,9 +70,19 @@ test_expect_success 'patch does not affect mode' '
 	git reset --hard &&
 	echo content >>file &&
 	chmod +x file &&
-	printf "y\\n" | git add -p &&
+	printf "n\\ny\\n" | git add -p &&
 	git show :file | grep content &&
 	git diff file | grep "new mode"
 '
 
+test_expect_success 'stage mode but not hunk' '
+	git reset --hard &&
+	echo content >>file &&
+	chmod +x file &&
+	printf "y\\nn\\n" | git add -p &&
+	git diff --cached file | grep "new mode" &&
+	git diff          file | grep "+content"
+'
+
+
 test_done
-- 
1.5.5.rc1.141.g50ecd.dirty
Previous: Jeff KingNext: Jeff King
Message 6 of 7 in “add--interactive: ignore mode change in 'p'atch command”
  1. 1/2 add--interactive: ignore mode change in 'p'atch commandJeff King, Mar 27, 2008
  2. Junio C HamanoMar 27, 2008
  3. Jeff KingMar 27, 2008
  4. Junio C HamanoMar 27, 2008
  5. Jeff KingMar 27, 2008
  6. 2/2 add--interactive: allow user to choose mode updateJeff King, Mar 27, 2008
  7. Jeff KingMar 27, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.