Re: Force usage of pager for diff, show, etc when piping to non-TTY

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Aug 15, 2023 at 05:09:13PM -0700, Patrick wrote:

> I noticed there is no option I can pass to git to use the pager set in
> my gitconfig even when piping to non-TTY. Is there a workaround? If
> not, may I request this as a new feature?

I don't think there is a workaround. We have "git --paginate", but it
really means "paginate this command using the default rules, even if it
is not a command that is usually paginated".

Looking at the code in setup_pager(), I think the check for "is stdout a
tty" is fed directly into the decision of whether to use a pager.
There's no way to override it within Git. You'd have to trick Git by
opening a pty in the calling program and pumping the data through it
(which is what our test suite does; see t/test-terminal.perl).

So I think it would need a new option. But...

> Use case: integrate tools like Delta or diff-so-fancy when building
> wrappers around git commands. See
> https://github.com/dandavison/delta/discussions/840 and
> https://github.com/PatrickF1/fzf.fish/discussions/202 for examples.

I'm not quite sure that's what you want. When a user configures a custom
pager using a prettifier tool like that, they usually further pipe the
output to a pager like "less". E.g., I have:

  [pager]
  log = diff-highlight | less

in my config. If you are trying to save output that looks like what the
user would see on their tty, you want the first half of that pipeline
(the diff-highlight here), but not the second (less). Of course it
mostly works because less is smart enough to behave like a noop "cat"
when stdout isn't a tty. So it might be OK in practice.

I think your script might be better off doing the piping itself. In
theory you could ask Git what the configured pager is and the run it
yourself, but:

  1. You can use "git var GIT_PAGER" to get the default pager, but not
     command-specific ones. So you'd have to check "git config
     pager.log", etc, yourself, which means reimplementing some of Git's
     logic.

  2. You'd get a string like "diff-highlight | less", and then you'd
     have to decide whether to parse off the "| less" part yourself,
     which is obviously error-prone since the string can be an
     arbitrarily complex shell expression.

When we were faced with a similar situation within Git, we ended up
adding a new config option: interactive.diffFilter. This is used by "add
-p", etc, to filter diffs that are shown to the user. It does mean the
user has to repeat themselves (I have to set it to "diff-highlight"
separately from my settings for pager.log, pager.diff, etc). But it's
unambiguous, and it gives the user the flexibility of configuring
various outputs differently if they choose.

So depending on what your script does, you could use a similar config
option. Or even just use interactive.diffFilter if your use case is
conceptually the same.

-Peff



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux