{"thread":{"id":"46958","subject":"(Some?) control codes not escaped when using `git add -p`","startedAt":"2017-10-12T17:59:35Z","lastAt":"2017-10-12T18:47:43Z","messageCount":2,"participants":["Mahmoud Al-Qudsi","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"330295","messageId":"CACcTrKctqAWeWWrc9Q+Y7ewXGc_o+uJoeHS83LDw5O_s1-3Nug@mail.gmail.com","threadId":"46958","inReplyTo":null,"subject":"(Some?) control codes not escaped when using `git add -p`","fromName":"Mahmoud Al-Qudsi","fromEmail":"mqudsi@neosmart.net","sentAt":"2017-10-12T17:59:06Z","receivedAt":"2017-10-12T17:59:35Z","isPatch":false,"sender":{"key":"mqudsi@neosmart.net","avatar":"https://gravatar.com/avatar/c2643dd7c6df61aed49d9f3d917ac6d61cafbbda5f9b1619f50d3b749dca415a?d=mp&s=160"},"body":"Hello list,\n\nRunning git 2.7.4, I’ve run into an issue where control codes that would\nnormally be escaped when using `git diff` are not similarly escaped when using\n`git add -p`.\n\nAs a concrete example, I have a text file including the following \"text\":\n\n:map ^[[H <Home>\n:map ^[[5~ ^B \"page up\n:map ^[[6~ ^F \"page down\n\nExcept each ^x above is the literal ctrl+x (i.e. ctrl+v followed by ctrl+x).\nThese are not lines that have been added or removed from the document, they're\njust context lines.\n\nUsing `git diff`, these special characters are elided from the diff output\n(though the latter two lines cause special coloring in the diff output so\nperhaps they're not being entirely escaped?), but when using `git add -p` upon\nreaching the chunk in question my terminal interprets a literal \"page up\" input\nand corrupts the output.\n\nHere's a screenshot of what I see when I use `git diff`:\nhttps://i.imgur.com/FOXWEIi.png\n\nAnd here's what I see when use `git add -p`:\nhttps://i.imgur.com/i5hqhFX.png\n\nAs you can see, in the second example the cursor is a few lines from the top of\nthe screen, as the text output started midway down and then jumped to the start\nand continued from there on encountering the literal 'Page Up' sequence in the\ndiff'd text.\n\nI'm not sure _what_ the correct approach would be here (does git faithfully\nprint whatever it finds in the file or does it attempt to escape it instead?)\nbut it seems to me that the lack of consistency between the two commands is a\nbug as whichever approach is taken, a context line in `git diff` should surely\nbe output to the terminal in the same way as a context line in `git add -p`?\n\nThe obvious solution is to embrace isatty(2) religiously, but I'm not\nsure how the\neveryone else feels about that (or if it's already used elsewhere).\n\nAnyway, I'm sure I'm not the only one to run into this. Seeking options and\ninterested in the various viewpoints on how this should be correctly handled\n(or explanations for why it's already correct as-is).\n\nCheers,\n\nMahmoud Al-Qudsi\nNeoSmart Technologies\n"},{"id":"330297","messageId":"20171012184736.rglkbyryauwuvn2a@sigill.intra.peff.net","threadId":"46958","inReplyTo":"CACcTrKctqAWeWWrc9Q+Y7ewXGc_o+uJoeHS83LDw5O_s1-3Nug@mail.gmail.com","subject":"Re: (Some?) control codes not escaped when using `git add -p`","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-10-12T18:47:36Z","receivedAt":"2017-10-12T18:47:43Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Oct 12, 2017 at 12:59:06PM -0500, Mahmoud Al-Qudsi wrote:\n\n> Hello list,\n> \n> Running git 2.7.4, I’ve run into an issue where control codes that would\n> normally be escaped when using `git diff` are not similarly escaped when using\n> `git add -p`.\n> \n> As a concrete example, I have a text file including the following \"text\":\n> \n> :map ^[[H <Home>\n> :map ^[[5~ ^B \"page up\n> :map ^[[6~ ^F \"page down\n\nThe diffs generated for \"git diff\" and \"git add -p\" are done by the\nexact same code. And that code doesn't do any escaping or cleverness; it\nwill output the raw bytes of the difference.\n\nWhat is likely causing the different in what you see is that \"git diff\".\noutputs through a pager, and the snippets of \"add -p\" do not. The\ndefault pager, \"less\", does escape some control codes (but with the -R\noption, which git uses by default, it passes through colors).\n\nTry:\n\n  git --no-pager diff\n\nor:\n\n  GIT_PAGER=cat git diff\n\nand you'll likely see output similar to what you get with \"add -p\".\n\nThe reason that \"add -p\" doesn't go through a pager by default is simply\nthat it would be annoying, since we show snippets and then ask for user\ninput.\n\nHowever, since Git v2.9.0, \"add -p\" (and all of the interactive commands\nlike \"checkout -p\", etc) know about the interactive.diffFilter config\noption. You could use that to munge the results however you like. E.g.:\n\n  # we can't use [:cntrl:] here because we want to keep newlines.\n  # likewise, we want to keep ESC for color codes\n  git config interactive.diffFilter \"tr '\\000-\\011\\013-\\032\\034-\\037' '?'\"\n\n-Peff\n"}]}