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

[PATCH 1/2] add--interactive: ignore mode change in 'p'atch command

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

When a path is examined in the patch subcommand, any mode changes in the file are given to use in the diff header by git-diff. If no hunks are staged, then we throw out that header. But if _any_ hunks are staged, we use the header, and the mode is changed.

Since the 'p'atch command should just be dealing with hunks that are shown to the user, it makes sense to just ignore mode changes entirely. We do squirrel away the mode, though, since a future patch will allow users to select the mode update.

Signed-off-by: Jeff King <peff@peff.net>
---
Like I said, I don't necessarily like this behavior, but it is a
stepping stone to 1/2.
 git-add--interactive.perl  |   16 ++++++++++++++++
 t/t3701-add-interactive.sh |    9 +++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index a0a81f1..5cdda29 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -550,6 +550,21 @@ sub parse_diff {
 	return @hunk;
 }
 
+sub parse_diff_header {
+	my $src = shift;
+
+	my $head = { TEXT => [], DISPLAY => [] };
+	my $mode = { TEXT => [], DISPLAY => [] };
+
+	for (my $i = 0; $i < @{$src->{TEXT}}; $i++) {
+		my $dest = $src->{TEXT}->[$i] =~ /^(old|new) mode (\d+)$/ ?
+			$mode : $head;
+		push @{$dest->{TEXT}}, $src->{TEXT}->[$i];
+		push @{$dest->{DISPLAY}}, $src->{DISPLAY}->[$i];
+	}
+	return ($head, $mode);
+}
+
 sub hunk_splittable {
 	my ($text) = @_;
 
@@ -795,6 +810,7 @@ sub patch_update_file {
 	my ($ix, $num);
 	my $path = shift;
 	my ($head, @hunk) = parse_diff($path);
+	($head, my $mode) = parse_diff_header($head);
 	for (@{$head->{DISPLAY}}) {
 		print;
 	}
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 77c90f6..d920d06 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -66,4 +66,13 @@ test_expect_success 'revert works (commit)' '
 	grep "unchanged *+3/-0 file" output
 '
 
+test_expect_success 'patch does not affect mode' '
+	git reset --hard &&
+	echo content >>file &&
+	chmod +x file &&
+	printf "y\\n" | git add -p &&
+	git show :file | grep content &&
+	git diff file | grep "new mode"
+'
+
 test_done
-- 
1.5.5.rc1.141.g50ecd.dirty
Next: Junio C Hamano
Message 1 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.