r/adventofcode • u/anoi • Dec 06 '19
Upping the Ante [2019 Day 6] [Git] Version control is important
git is great for working with trees, so why not?
Here's how I generated a git repo from my sample data. The code's been slightly cleaned up since I ran it, and it takes a long time, so I haven't tested it again. Hopefully it still works.
The generated repo is here for my input. There's only one commit on master, but there's a whole bunch of tags/releases.
Part 1:
echo $(($(git log --all --pretty=oneline | cut -d' ' -f1 | \
xargs -n 1 git log --pretty=oneline | wc -l) - \
$(git log --all --pretty=oneline | wc -l)))Broken down: git log --all gets a list of every commit in the repo. cut pulls out the commit hash. xargs -n 1 will take each of those, and pass it back to git log. wc -l counts how many lines of output are generated. Basically, "for each commit, get its commit log, and add them all up". Finally, we subtract the total number of commits, as nodes do not orbit themselves.
Part 2: echo $(($(git log YOU...SAN --pretty=oneline | wc -l) - 2))
Broken down: YOU...SAN in git is "the set of commits that are reachable from either one of YOU or SAN but not from both." That is to say, it finds the most recent common ancestor, and only shows you stuff after that point (on either side of the fork). We have to subtract two because this will output both YOU and SAN.
Part 1 takes quite a long time to run, but part 2 surprised me by only taking about 7 seconds.
EDIT: u/CCC_037 gave me another idea
8
u/amalloy Dec 06 '19
Instead of git log --all --pretty=oneline | cut -d' ' -f1, why not git rev-list --all? It's better to use plumbing commands from a script than to parse the output of porcelain commands.
2
u/anoi Dec 06 '19
That's a better way to do it, thanks. I actually thought about plumbing/porcelain, but couldn't remember which plumbing command did this. My first two guesses (
filter-branchandfor-each-ref) weren't right, googled "log all commits" and didn't think to look further than first few results.1
u/e_blake Dec 07 '19
Similarly, instead of subtracting the node, why not exclude it from the getgo? Golfing further:
git rev-list --all|xargs -i git log --oneline {}^ 2>/dev/null|wc -lThere, no use of $() or $(()). (The 2>/dev/null is necessary since COM^ does not exist and will cause output to stderr)
1
u/e_blake Dec 07 '19
Or exclude COM from the initial list, shaving a few more bytes from the command line:
git rev-list --all --not COM|xargs -i git log --oneline {}^|wc -l
42
u/1vader Dec 06 '19
I thought this post was just some story about how git is important because you would have lost some changes or something but this is awesome!
13
13
u/M124367 Dec 06 '19
Now this is next level version control.
Using Git to track version history: <insert small brain>
Using Git to track planet orbits and calculate distance between two orbiting celestial objects in terms of hops through the most recent ancestor: <insert BIG BRAIN>
1
2
u/miauw62 Dec 06 '19
i thought about this while i was solving the challenge, but i just dismissed it as ridiculous. i guess not!
2
2
u/e_blake Dec 07 '19
Part 2 can be golfed:
git log YOU^...SAN^ --oneline|wc -lThat is, by starting your search from the ancestor, you don't have to perform subtraction.
1
1
1
1
1
u/e_blake Dec 07 '19
Hmm. My input file includes lines such as:
CCD)W8T
176)S7S
B41)G6R
Note that those lines include valid 3-digit hex values. Fortunately, git requires at least 4 hex characters before it will start complaining that 'warning: refname 'CCD1' is ambiguous'; at only 3 characters, git finds only the tag, and not any other abbreviated object whose sha1 prefix starts with those 3 hex chars. If the puzzle had used 4-byte names, you'd have to take care of adding a disambiguation character (perhaps a 'g' prefix) to make all your tag names unambiguously non-hex strings and therefore no risk of a potential collision with the sha1 numbers computed while constructing your repository.
15
u/CCC_037 Dec 06 '19
Now I'm thinking that you could probably do something with a directory tree to represent this data...