Thursday, 11 June 2015

Diffing diffs with bash process substitution

I found myself wanting to examine the differences between different git commits. Specifically these commits represent the same change applied to 2 different branches, so I'm interested in what changes the backport author had to make. I was initially using temporary files for this, but stumbled across this bash gem:
$ diff -u <(git show [original]) <(git show [backport])
Note that this isn't your regular redirection, because I'm passing 2 filenames to diff. This is bash's process substitution syntax. It essentially runs a command and substitutes the path of a temporary named pipe connected to the command.

Stick it in a bash function, and you get:
function diffdiff() {
    diff -u <(git show "$1") <(git show "$2") | less
}
Now I can run:
$ diffdiff 01234567 89abcdef
and see my gloriously diffed diffs.

No comments:

Post a Comment