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

[PATCH 2/2] add--interactive: quote commentChar regex

From
Jeff King <peff@peff.net>
Date
Jun 21, 2017, 19:28 UTC
Message-ID
<20170621192859.udejhkl66ylsbg24@sigill.intra.peff.net>
In-Reply-To
<20170621192315.3umx4lszvzwjffnb@sigill.intra.peff.net>

Since c9d961647 (i18n: add--interactive: mark edit_hunk_manually message for translation, 2016-12-14), when the user asks to edit a hunk manually, we respect core.commentChar in generating the edit instructions. However, when we then strip out comment lines, we use a simple regex like:

  /^$commentChar/

If your chosen comment character is a regex metacharacter, then that will behave in a confusing manner ("$", for instance, would only eliminate blank lines, not actual comment lines).

We can fix that by telling perl not to respect metacharacters.

Reported-by: Christian Rösch <christian@croesch.de>
Signed-off-by: Jeff King <peff@peff.net>
---
I did this on the current master. It also applies cleanly on top of
'maint'. The fix applies back as far as c9d961647, but t3701 grew some
new tests in the interim, so there's a minor conflict applying it there.
 git-add--interactive.perl  | 2 +-
 t/t3701-add-interactive.sh | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index fc722fe8a..0e8543c86 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1081,7 +1081,7 @@ EOF2
 
 	open $fh, '<', $hunkfile
 		or die sprintf(__("failed to open hunk edit file for reading: %s"), $!);
-	my @newtext = grep { !/^$comment_line_char/ } <$fh>;
+	my @newtext = grep { !/^\Q$comment_line_char\E/ } <$fh>;
 	close $fh;
 	unlink $hunkfile;
 
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 2ecb43a61..2f3e7cea6 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -477,4 +477,12 @@ test_expect_success 'add -p does not expand argument lists' '
 	! grep not-changed trace.out
 '
 
+test_expect_success 'hunk-editing handles custom comment char' '
+	git reset --hard &&
+	echo change >>file &&
+	test_config core.commentChar "\$" &&
+	echo e | GIT_EDITOR=true git add -p &&
+	git diff --exit-code
+'
+
 test_done
-- 
2.13.1.792.g159074dab
Previous: Jeff KingNext: Christian Rösch
Message 5 of 6 in “git add -p does not work with custom comment char currently”
  1. Christian RöschJun 21, 2017
  2. Jeff KingJun 21, 2017
  3. 0/2 Re: git add -p does not work with custom comment char currentlyJeff King, Jun 21, 2017
  4. 1/2 add--interactive: handle EOF in prompt_yesnoJeff King, Jun 21, 2017
  5. 2/2 add--interactive: quote commentChar regexJeff King, Jun 21, 2017
  6. Christian RöschJun 21, 2017

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.