{"thread":{"id":"60117","subject":"Force usage of pager for diff, show, etc when piping to non-TTY","startedAt":"2023-08-16T00:10:41Z","lastAt":"2023-08-17T20:04:39Z","messageCount":7,"participants":["Patrick","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"480697","messageId":"CAMRL+qYYGJ-LYG8qZpJOq+_=YO_C7JSTH4TPDpbEA4fRdANP7w@mail.gmail.com","threadId":"60117","inReplyTo":null,"subject":"Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Patrick","fromEmail":"patrickf3139@gmail.com","sentAt":"2023-08-16T00:09:13Z","receivedAt":"2023-08-16T00:10:41Z","isPatch":false,"sender":{"key":"patrickf3139@gmail.com","avatar":null},"body":"Hi,\n\nI noticed there is no option I can pass to git to use the pager set in\nmy gitconfig even when piping to non-TTY. Is there a workaround? If\nnot, may I request this as a new feature?\n\nUse case: integrate tools like Delta or diff-so-fancy when building\nwrappers around git commands. See\nhttps://github.com/dandavison/delta/discussions/840 and\nhttps://github.com/PatrickF1/fzf.fish/discussions/202 for examples.\n\nThanks,\nPatrick\n"},{"id":"480703","messageId":"20230816025715.GB2248431@coredump.intra.peff.net","threadId":"60117","inReplyTo":"CAMRL+qYYGJ-LYG8qZpJOq+_=YO_C7JSTH4TPDpbEA4fRdANP7w@mail.gmail.com","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2023-08-16T02:57:15Z","receivedAt":"2023-08-16T02:58:01Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Aug 15, 2023 at 05:09:13PM -0700, Patrick wrote:\n\n> I noticed there is no option I can pass to git to use the pager set in\n> my gitconfig even when piping to non-TTY. Is there a workaround? If\n> not, may I request this as a new feature?\n\nI don't think there is a workaround. We have \"git --paginate\", but it\nreally means \"paginate this command using the default rules, even if it\nis not a command that is usually paginated\".\n\nLooking at the code in setup_pager(), I think the check for \"is stdout a\ntty\" is fed directly into the decision of whether to use a pager.\nThere's no way to override it within Git. You'd have to trick Git by\nopening a pty in the calling program and pumping the data through it\n(which is what our test suite does; see t/test-terminal.perl).\n\nSo I think it would need a new option. But...\n\n> Use case: integrate tools like Delta or diff-so-fancy when building\n> wrappers around git commands. See\n> https://github.com/dandavison/delta/discussions/840 and\n> https://github.com/PatrickF1/fzf.fish/discussions/202 for examples.\n\nI'm not quite sure that's what you want. When a user configures a custom\npager using a prettifier tool like that, they usually further pipe the\noutput to a pager like \"less\". E.g., I have:\n\n  [pager]\n  log = diff-highlight | less\n\nin my config. If you are trying to save output that looks like what the\nuser would see on their tty, you want the first half of that pipeline\n(the diff-highlight here), but not the second (less). Of course it\nmostly works because less is smart enough to behave like a noop \"cat\"\nwhen stdout isn't a tty. So it might be OK in practice.\n\nI think your script might be better off doing the piping itself. In\ntheory you could ask Git what the configured pager is and the run it\nyourself, but:\n\n  1. You can use \"git var GIT_PAGER\" to get the default pager, but not\n     command-specific ones. So you'd have to check \"git config\n     pager.log\", etc, yourself, which means reimplementing some of Git's\n     logic.\n\n  2. You'd get a string like \"diff-highlight | less\", and then you'd\n     have to decide whether to parse off the \"| less\" part yourself,\n     which is obviously error-prone since the string can be an\n     arbitrarily complex shell expression.\n\nWhen we were faced with a similar situation within Git, we ended up\nadding a new config option: interactive.diffFilter. This is used by \"add\n-p\", etc, to filter diffs that are shown to the user. It does mean the\nuser has to repeat themselves (I have to set it to \"diff-highlight\"\nseparately from my settings for pager.log, pager.diff, etc). But it's\nunambiguous, and it gives the user the flexibility of configuring\nvarious outputs differently if they choose.\n\nSo depending on what your script does, you could use a similar config\noption. Or even just use interactive.diffFilter if your use case is\nconceptually the same.\n\n-Peff\n"},{"id":"480717","messageId":"CAMRL+qbGBOiR49A0FDYgZJmMbsfyeTZkzVmEdrCRYEanjv195A@mail.gmail.com","threadId":"60117","inReplyTo":"20230816025715.GB2248431@coredump.intra.peff.net","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Patrick","fromEmail":"patrickf3139@gmail.com","sentAt":"2023-08-16T16:30:38Z","receivedAt":"2023-08-16T16:31:57Z","isPatch":false,"sender":{"key":"patrickf3139@gmail.com","avatar":null},"body":"Hi Jeff, thank you for your expert and thorough response!\nYou make a good point that users usually pipe the output of a prettier\ntool to a pager themselves. In the case of delta, delta can be\nconfigured to selectively pipe to a pager itself, so delta's docs\ndoesn't tell users to pipe: `core.pager = delta` (source:\nhttps://dandavison.github.io/delta/configuration.html)\n\nHowever, for diff-so-fancy, it might not work cleanly because users\nare told to explicitly pipe it to a pager. `core.pager \"diff-so-fancy\n| less --tabs=4 -RFX\"` (source:\nhttps://github.com/so-fancy/diff-so-fancy#with-git).\n\nI haven't looked at other tools but it's clear this approach is not\nguaranteed to work for all configurations so I should look into\nsomething else.\n\nJeff, would you be so kind as to elaborate more on the\ninteractive.diffFilter approach? My understanding is that\ninteractive.diffFilter is only used for git add -p or git reset -p.\nHowever, the limitation for my use case is I need to use the pager\nfor git log and git show so that won't work. So then, you are\nsuggesting that I ask my users to opt in by setting an arbitrary git\nconfig like fzf.pager and then read out the pager from that git var?\n\nOn Tue, Aug 15, 2023 at 7:57 PM Jeff King <peff@peff.net> wrote:\n>\n> On Tue, Aug 15, 2023 at 05:09:13PM -0700, Patrick wrote:\n>\n> > I noticed there is no option I can pass to git to use the pager set in\n> > my gitconfig even when piping to non-TTY. Is there a workaround? If\n> > not, may I request this as a new feature?\n>\n> I don't think there is a workaround. We have \"git --paginate\", but it\n> really means \"paginate this command using the default rules, even if it\n> is not a command that is usually paginated\".\n>\n> Looking at the code in setup_pager(), I think the check for \"is stdout a\n> tty\" is fed directly into the decision of whether to use a pager.\n> There's no way to override it within Git. You'd have to trick Git by\n> opening a pty in the calling program and pumping the data through it\n> (which is what our test suite does; see t/test-terminal.perl).\n>\n> So I think it would need a new option. But...\n>\n> > Use case: integrate tools like Delta or diff-so-fancy when building\n> > wrappers around git commands. See\n> > https://github.com/dandavison/delta/discussions/840 and\n> > https://github.com/PatrickF1/fzf.fish/discussions/202 for examples.\n>\n> I'm not quite sure that's what you want. When a user configures a custom\n> pager using a prettifier tool like that, they usually further pipe the\n> output to a pager like \"less\". E.g., I have:\n>\n>   [pager]\n>   log = diff-highlight | less\n>\n> in my config. If you are trying to save output that looks like what the\n> user would see on their tty, you want the first half of that pipeline\n> (the diff-highlight here), but not the second (less). Of course it\n> mostly works because less is smart enough to behave like a noop \"cat\"\n> when stdout isn't a tty. So it might be OK in practice.\n>\n> I think your script might be better off doing the piping itself. In\n> theory you could ask Git what the configured pager is and the run it\n> yourself, but:\n>\n>   1. You can use \"git var GIT_PAGER\" to get the default pager, but not\n>      command-specific ones. So you'd have to check \"git config\n>      pager.log\", etc, yourself, which means reimplementing some of Git's\n>      logic.\n>\n>   2. You'd get a string like \"diff-highlight | less\", and then you'd\n>      have to decide whether to parse off the \"| less\" part yourself,\n>      which is obviously error-prone since the string can be an\n>      arbitrarily complex shell expression.\n>\n> When we were faced with a similar situation within Git, we ended up\n> adding a new config option: interactive.diffFilter. This is used by \"add\n> -p\", etc, to filter diffs that are shown to the user. It does mean the\n> user has to repeat themselves (I have to set it to \"diff-highlight\"\n> separately from my settings for pager.log, pager.diff, etc). But it's\n> unambiguous, and it gives the user the flexibility of configuring\n> various outputs differently if they choose.\n>\n> So depending on what your script does, you could use a similar config\n> option. Or even just use interactive.diffFilter if your use case is\n> conceptually the same.\n>\n> -Peff\n"},{"id":"480742","messageId":"20230817054405.GD3006160@coredump.intra.peff.net","threadId":"60117","inReplyTo":"CAMRL+qbGBOiR49A0FDYgZJmMbsfyeTZkzVmEdrCRYEanjv195A@mail.gmail.com","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2023-08-17T05:44:05Z","receivedAt":"2023-08-17T05:45:03Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Aug 16, 2023 at 09:30:38AM -0700, Patrick wrote:\n\n> Jeff, would you be so kind as to elaborate more on the\n> interactive.diffFilter approach? My understanding is that\n> interactive.diffFilter is only used for git add -p or git reset -p.\n> However, the limitation for my use case is I need to use the pager\n> for git log and git show so that won't work. So then, you are\n> suggesting that I ask my users to opt in by setting an arbitrary git\n> config like fzf.pager and then read out the pager from that git var?\n\nYes, they'd have to set a new config variable. Though if you are really\njust filtering diffs and the semantics would be the same as\ninteractive.diffFilter, I would probably just use that. You could also\nintroduce a new foo.diffFilter option, and if unset have it default to\nthe value of interactive.diffFilter. That provides flexibility without\nforcing users to repeat themselves.\n\nThat said, I would not be surprised if many users who set pager.log do\nnot even know about interactive.diffFilter. It's a bit more obscure, and\ncame later.\n\nIf you do want to follow that approach, the simplest example is probably\njust to see how it was added to the perl code in 01143847db\n(add--interactive: allow custom diff highlighting programs, 2016-02-27):\n\n  https://github.com/git/git/commit/01143847dbf4fbf27268650f3ace16eac03b3130\n\nIn shell it might look something like:\n\n  filter=$(git config --get interactive.difffilter)\n  if test -n \"$filter\"; then\n    git show ... | eval \"$filter\"\n  else\n    git show ...\n  fi\n\n-Peff\n"},{"id":"480751","messageId":"CAMRL+qbSn058AsMW7+d1nRbt4Ao6R+BZ6HYHxDrVQhURgWoy7Q@mail.gmail.com","threadId":"60117","inReplyTo":"20230817054405.GD3006160@coredump.intra.peff.net","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Patrick","fromEmail":"patrickf3139@gmail.com","sentAt":"2023-08-17T17:06:58Z","receivedAt":"2023-08-17T17:08:21Z","isPatch":false,"sender":{"key":"patrickf3139@gmail.com","avatar":null},"body":"Hi Jeff, I think I finally get it now. Thank you for so patiently\nexplaining this to me in different ways. I'm sorry I was slow to\nunderstand heh.\nJust to be really clear, the filter in `interactive.diffFilter` is\nmeant as a filter for transforming (think a photo filter), as opposed\nto a filter that removes elements, correct? I think that's what I got\ntripped up on when you explained the first two times.\n\nOn Wed, Aug 16, 2023 at 10:44 PM Jeff King <peff@peff.net> wrote:\n>\n> On Wed, Aug 16, 2023 at 09:30:38AM -0700, Patrick wrote:\n>\n> > Jeff, would you be so kind as to elaborate more on the\n> > interactive.diffFilter approach? My understanding is that\n> > interactive.diffFilter is only used for git add -p or git reset -p.\n> > However, the limitation for my use case is I need to use the pager\n> > for git log and git show so that won't work. So then, you are\n> > suggesting that I ask my users to opt in by setting an arbitrary git\n> > config like fzf.pager and then read out the pager from that git var?\n>\n> Yes, they'd have to set a new config variable. Though if you are really\n> just filtering diffs and the semantics would be the same as\n> interactive.diffFilter, I would probably just use that. You could also\n> introduce a new foo.diffFilter option, and if unset have it default to\n> the value of interactive.diffFilter. That provides flexibility without\n> forcing users to repeat themselves.\n>\n> That said, I would not be surprised if many users who set pager.log do\n> not even know about interactive.diffFilter. It's a bit more obscure, and\n> came later.\n>\n> If you do want to follow that approach, the simplest example is probably\n> just to see how it was added to the perl code in 01143847db\n> (add--interactive: allow custom diff highlighting programs, 2016-02-27):\n>\n>   https://github.com/git/git/commit/01143847dbf4fbf27268650f3ace16eac03b3130\n>\n> In shell it might look something like:\n>\n>   filter=$(git config --get interactive.difffilter)\n>   if test -n \"$filter\"; then\n>     git show ... | eval \"$filter\"\n>   else\n>     git show ...\n>   fi\n>\n> -Peff\n"},{"id":"480758","messageId":"20230817194735.GA3032779@coredump.intra.peff.net","threadId":"60117","inReplyTo":"CAMRL+qbSn058AsMW7+d1nRbt4Ao6R+BZ6HYHxDrVQhURgWoy7Q@mail.gmail.com","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2023-08-17T19:47:35Z","receivedAt":"2023-08-17T19:48:20Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Aug 17, 2023 at 10:06:58AM -0700, Patrick wrote:\n\n> Just to be really clear, the filter in `interactive.diffFilter` is\n> meant as a filter for transforming (think a photo filter), as opposed\n> to a filter that removes elements, correct? I think that's what I got\n> tripped up on when you explained the first two times.\n\nYes, exactly. In retrospect, the name is a little ambiguous. :)\nGlad you've figured it out.\n\n-Peff\n"},{"id":"480764","messageId":"CAMRL+qbyLvdfKnv47BRSrRoBKZW5pU_uy1w7GO3Hyo3+X224PQ@mail.gmail.com","threadId":"60117","inReplyTo":"20230817194735.GA3032779@coredump.intra.peff.net","subject":"Re: Force usage of pager for diff, show, etc when piping to non-TTY","fromName":"Patrick","fromEmail":"patrickf3139@gmail.com","sentAt":"2023-08-17T20:03:05Z","receivedAt":"2023-08-17T20:04:39Z","isPatch":false,"sender":{"key":"patrickf3139@gmail.com","avatar":null},"body":"Thank you for clarifying, Jeff. And thank you to all the git\ncontributors who not only maintain git for all the developers\neverywhere, but also provide first class support :)\n\nOn Thu, Aug 17, 2023 at 12:47 PM Jeff King <peff@peff.net> wrote:\n>\n> On Thu, Aug 17, 2023 at 10:06:58AM -0700, Patrick wrote:\n>\n> > Just to be really clear, the filter in `interactive.diffFilter` is\n> > meant as a filter for transforming (think a photo filter), as opposed\n> > to a filter that removes elements, correct? I think that's what I got\n> > tripped up on when you explained the first two times.\n>\n> Yes, exactly. In retrospect, the name is a little ambiguous. :)\n> Glad you've figured it out.\n>\n> -Peff\n"}]}