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

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

From
Mahmoud Al-Qudsi <mqudsi@neosmart.net>
Date
Oct 12, 2017, 17:59 UTC
Message-ID
<CACcTrKctqAWeWWrc9Q+Y7ewXGc_o+uJoeHS83LDw5O_s1-3Nug@mail.gmail.com>
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

Except each ^x above is the literal ctrl+x (i.e. ctrl+v followed by ctrl+x). These are not lines that have been added or removed from the document, they're just context lines.

Using `git diff`, these special characters are elided from the diff output (though the latter two lines cause special coloring in the diff output so perhaps they're not being entirely escaped?), but when using `git add -p` upon reaching the chunk in question my terminal interprets a literal "page up" input and corrupts the output.

Here's a screenshot of what I see when I use `git diff`: https://i.imgur.com/FOXWEIi.png

And here's what I see when use `git add -p`: https://i.imgur.com/i5hqhFX.png

As you can see, in the second example the cursor is a few lines from the top of the screen, as the text output started midway down and then jumped to the start and continued from there on encountering the literal 'Page Up' sequence in the diff'd text.

I'm not sure _what_ the correct approach would be here (does git faithfully print whatever it finds in the file or does it attempt to escape it instead?) but it seems to me that the lack of consistency between the two commands is a bug as whichever approach is taken, a context line in `git diff` should surely be output to the terminal in the same way as a context line in `git add -p`?

The obvious solution is to embrace isatty(2) religiously, but I'm not sure how the everyone else feels about that (or if it's already used elsewhere).

Anyway, I'm sure I'm not the only one to run into this. Seeking options and interested in the various viewpoints on how this should be correctly handled (or explanations for why it's already correct as-is).

Cheers,

Mahmoud Al-Qudsi NeoSmart Technologies

Next: Jeff King
Message 1 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.