dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
21.36k stars 360 forks source link

🚀 use diff-compatible exit statuses that differ between difference-found and error #1584

Open calestyo opened 6 months ago

calestyo commented 6 months ago

Hey.

It seems right now, that delta has an exit status of 0 in case no differences were found, and one of 1 either when differences were found or an error (e.g. file not found) occurred.

It would be nice if one could differ between the latter two, ideally following POSIX' exit statuses for diff, that is:

0 No differences were found. 1 Differences were found. >1 An error occurred.

Thanks, Chris.

dandavison commented 6 months ago

Hi @calestyo, I thought that it worked like that already! Can you post an example? This is MacOS in zsh.

~ delta <(echo a) <(echo a) > /dev/null; echo $?
0
~ delta <(echo a) <(echo b) > /dev/null; echo $?
1
~ delta <(echo a) __not_there__ > /dev/null; echo $?
diff: __not_there__: No such file or directory
2
calestyo commented 6 months ago

Indeed. Not sure how I produced the contradicting result.... I've been calling delta from a shell script, and I think there might have been an exit 1 at a wrong point, which has overridden delta’s 2.

Sorry for the noise.

btw: Is there a way in delta to disable colour output?

dandavison commented 6 months ago

No worries.

btw: Is there a way in delta to disable colour output?

You mean, keep all the reformatting and decorations but specifically disable colour? I don't think there is a way to do that, although perhaps there are enough colour settings in delta to achieve it piecemeal!

So in a script the answer would be not to call delta if one only wants the raw git/diff input, and of course, git doesn't invoke delta at all if output is not a tty. But if you explicitly pipe to delta in a script then it will add colours and text decorations / reformatting.

calestyo commented 6 months ago

No worries.

I reproduced it... wait a second...

calestyo commented 6 months ago
$ touch foo
$ stat /tmp/bar
stat: cannot statx '/tmp/bar': No such file or directory
$ delta foo /tmp/bar ; echo $?
error: Could not access '/tmp/bar'
1

If I do however:

$ delta <(echo foo) /tmp/bar ; echo $?
diff: /tmp/bar: No such file or directory
2
dandavison commented 6 months ago

Ah-ha, I'm not at my computer but I do remember that the code handling process substitutions had to do something unfortunate. Feel free to investigate!

calestyo commented 6 months ago

There are more cases:

$ delta /dev/null non-existent
$ delta /dev/random non-existent
$ delta /dev/uinput non-existent

all give 1, and only uinput is permission-wise not accessible by the user.

Also:

$ ln -s /nonexistent dangling.symlink
$ delta dangling.symlink non-existent

Though it works when comparing with an existent file (not sure, btw. whether it should even then, cause then there's still one inaccessible file - the target of the dangling symlink).

calestyo commented 6 months ago

Also when doing using a directory as existent file:

$ delta /tmp non-existent
calestyo commented 2 months ago

That issue may even go yet a bit further.

Assume I do:

foo="$(diff -u a b)"

(and there is a difference)
and then:

printf '%s\n' "$foo"  |  delta

I get the differences shown, but exit status is 0 (and not 1).

dandavison commented 2 months ago

That's right, in something | delta delta behaves like cat or bat -- it's just a pager displaying stuff and exit code is 0 unless something goes wrong. On the other hand in delta a b delta acts as a diff program, and exit codes are as diff.

calestyo commented 2 months ago

Well unless for the other cases mentioned above, where one does have delta a b style and it still doesn't act like diff. :-)