Handmade Hero»Forums»Code»An easy way to browse code changes with git
Andrew Reece
51 posts / 2 projects

Maker of things

An easy way to browse code changes with git
2 years, 6 months ago Edited by Andrew Reece on Feb. 8, 2018, 4:11 p.m. Reason: Initial post
I built myself a little utility 'function' for locally browsing Casey's code changes with git that I thought I'd share with you. I've also added the thought process for anyone interested in how/why it works.

git day 211                # show changes made on day 211
git days 200 211           # show changes made from 200 to 211
git day 211 --name-status  # you can still use flags for diff;
                           # here, show only the status of file changes (not code changes)

Implementation: add these aliases to git
    day  = "! git show day$1:day.txt               && git difftool day$1^ day$1 ${@:2} -- :!/:day.txt #"
    days = "! git show day$1:day.txt day$2:day.txt && git difftool day$1  day$2 ${@:3} -- :!/:day.txt #"

  1. First off, I'm assuming that you have access to the github repo. If not, go preorder the game, you monstar!

  2. If you clone the cpp repo, you notice that all of the commits have tags in the form `day###`, so we can get use `git diff day210 day211` to get the changes for day 211. Much easier than comparing commit SHAs!
    We can also use the `^` operator, which goes to the commit's 'parent': `git diff day211^ day211`.

  3. This gets a bit cumbersome if you're using it a lot. Git aliases to the rescue!
    day = "!git difftool day$1^ day$1 #

    1. `!` lets us treat the subsequent alias text as a shell command.
    2. You can use `diff` instead of difftool if you prefer, but this lets me compare both versions with vimdiff.
    3. Any text following the invocation of the alias is both treated as a parameter and appended to the end. The `#` comments out everything at the end, preventing the unwanting append.

  4. This cuts down some typing, but we'd still like to be able to use flags for difftool: `${@:2}` is replaced by all parameters from the second one (we don't want to repeat the first).

  5. Not bad, but could I get a bit more contextual info about what happened on the day(s) I'm looking at?
    Yes, as it turns out. Because the title for the episode is given in `day.txt`, we can use `git show day$1:day.txt` to cat the file before doing anything else.

  6. We don't really need to see that `day.txt` has been changed though - it always has been - and it just gets in the way when it diffs the contents. We can exclude it from the comparison with `-- :!/:day.txt`.

Hopefully this is helpful for someone - I find it faster than browsing the github site, and I like having the files local to my machine.

P.S. This is a more general vimdiff/difftool thing: the one thing I haven't yet taken the time to figure out is how to open up all commit comparisons at once in vim (e.g. in different tabs). Currently it opens the first, then when you close it, it moves onto the second, etc, so it's a bit awkward if you want to go back. You can probably do something with a bunch of temp files of all the comparisons... If anyone works it out, I'd be happy to hear about it.
Mārtiņš Možeiko
2108 posts / 1 project
An easy way to browse code changes with git
2 years, 6 months ago Edited by Mārtiņš Možeiko on Feb. 8, 2018, 8:52 p.m.
I simply use tig.

This is how it typically looks like:
Commits on left, diff on right.
Up/down moves between commits, pageup/down scrolls diff up/down.
It has bunch of more views for different functionality.
Andrew Reece
51 posts / 2 projects

Maker of things

An easy way to browse code changes with git
2 years, 6 months ago
Ooh, that's pretty nice, I hadn't come across tig before, thanks mmozeiko!