Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback for diffFilter.interactive #178

Closed
pylipp opened this issue May 4, 2020 · 4 comments
Closed

Fallback for diffFilter.interactive #178

pylipp opened this issue May 4, 2020 · 4 comments

Comments

@pylipp
Copy link

pylipp commented May 4, 2020

Hej, thanks for the amazing tool!
I have a question (about git configuration rather):
How can I have diffFilter.interactive configured in a way that it falls back to the default (colored) output when delta is not installed?

I tried

[interactive]
    diffFilter = delta --color-only --theme='Solarized (light)' || less -RFX

which in combination with git add -p has the non-colored output

sh: 1: delta: not found
Missing filename ("less --help" for help)
diff --git a/gitconfig b/gitconfig
index 46e391b..c301f89 100644
...

Maybe someone has a similar situation :)

@dandavison
Copy link
Owner

Hi @pylipp, I think the way to do this is with a shell script. This should work on Linux or MacOS but on Windows might need to be changed a little.

So what you'd do is create a file like this, for example saving it at ~/delta-with-fallback-to-less.sh and make it executable (chmod +x ~/delta-with-fallback-to-less.sh):

#!/bin/bash

if which delta > /dev/null 2>&1; then
    exec delta "$@"
else
    exec less -RFX
fi

Then in your ~/.gitconfig you'd do

[interactive]
    diffFilter = ~/delta-with-fallback-to-less.sh --color-only

I tested that quickly but let me know if you have any problems with it!

There are lots of variations one could make to that. For example see #19 (comment) regarding using the same delta options in the main pager command and in diffFilter.

@pylipp
Copy link
Author

pylipp commented May 4, 2020

That does the trick, thanks (I'm on Linux)! Also for combining the two git config options.
I've substituted which by command -v because it's more portable.

What's the advantage of using exec? Not spawning the programs in another process?

@dandavison
Copy link
Owner

Good!

What's the advantage of using exec? Not spawning the programs in another process?

One reason is to ensure that the exit status is equal to the exit status of the delta process. If there were more lines in the script beyond the if statement (and without using bash's set -e) then there'd be a risk that a non-zero delta exit code would nevertheless result in a zero (success) exit code from the shell script, which could confuse the caller.

@pylipp
Copy link
Author

pylipp commented May 4, 2020

TIL, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants