git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* (Some?) control codes not escaped when using `git add -p`
@ 2017-10-12 17:59 Mahmoud Al-Qudsi
  2017-10-12 18:47 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: Mahmoud Al-Qudsi @ 2017-10-12 17:59 UTC (permalink / raw)
  To: git, Mahmoud Al-Qudsi

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: (Some?) control codes not escaped when using `git add -p`
  2017-10-12 17:59 (Some?) control codes not escaped when using `git add -p` Mahmoud Al-Qudsi
@ 2017-10-12 18:47 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2017-10-12 18:47 UTC (permalink / raw)
  To: Mahmoud Al-Qudsi; +Cc: git

On Thu, Oct 12, 2017 at 12:59:06PM -0500, Mahmoud Al-Qudsi wrote:

> 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-10-12 18:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-12 17:59 (Some?) control codes not escaped when using `git add -p` Mahmoud Al-Qudsi
2017-10-12 18:47 ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).