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

Re: (Some?) control codes not escaped when using `git add -p`

From
Jeff King <peff@peff.net>
Date
Oct 12, 2017, 18:47 UTC
Message-ID
<20171012184736.rglkbyryauwuvn2a@sigill.intra.peff.net>
In-Reply-To
<CACcTrKctqAWeWWrc9Q+Y7ewXGc_o+uJoeHS83LDw5O_s1-3Nug@mail.gmail.com>
On Thu, Oct 12, 2017 at 12:59:06PM -0500, Mahmoud Al-Qudsi wrote:
Show 11 quoted lines
> Hello list,
> 
> Running git 2.7.4, I’ve run into an issue where control codes that would
> normally be escaped when using `git diff` are not similarly escaped when using
> `git add -p`.
> 
> As a concrete example, I have a text file including the following "text":
> 
> :map ^[[H <Home>
> :map ^[[5~ ^B "page up
> :map ^[[6~ ^F "page down

The diffs generated for "git diff" and "git add -p" are done by the exact same code. And that code doesn't do any escaping or cleverness; it will output the raw bytes of the difference.

What is likely causing the different in what you see is that "git diff". outputs through a pager, and the snippets of "add -p" do not. The default pager, "less", does escape some control codes (but with the -R option, which git uses by default, it passes through colors).

Try:
  git --no-pager diff
or:
  GIT_PAGER=cat git diff
and you'll likely see output similar to what you get with "add -p".

The reason that "add -p" doesn't go through a pager by default is simply that it would be annoying, since we show snippets and then ask for user input.

However, since Git v2.9.0, "add -p" (and all of the interactive commands like "checkout -p", etc) know about the interactive.diffFilter config option. You could use that to munge the results however you like. E.g.:

  # we can't use [:cntrl:] here because we want to keep newlines.
  # likewise, we want to keep ESC for color codes
  git config interactive.diffFilter "tr '\000-\011\013-\032\034-\037' '?'"
-Peff
Previous: Mahmoud Al-Qudsi
Message 2 of 2 in “(Some?) control codes not escaped when using `git add -p`”
  1. Mahmoud Al-QudsiOct 12, 2017
  2. Jeff KingOct 12, 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.