The main gitshed functionality.

Pulls together code from previous commits.

The main() wrapper, BUILD files etc. will follow shortly.
I disagree on the readability, but more to the point - this is much easier to get right, given the spaces inside the predicates etc. In fact, this was the only way I could get it to be both correct and robust.