Minority Opinions

Not everyone can be mainstream, after all.

git cat perfectionism

leave a comment »

At one point, I found a situation where I wanted something like svn cat, but for a git repository.  I managed to find something mostly right, with some links to discussion of various commands involved.  I’m not the first with this problem, which makes me think that a decently perfect script might be helpful.

My first major decision was to support the [<committish>] [--] <paths> parameter format favored by existing git commands.  That lets me concatenate multiple files, as long as they’re all from the same commit; if you want more flexibility, you can call git show yourself.  I ended up relying on git rev-list to determine the type of the first parameter.

The second trick is to add the right prefix to each <path>.  The git show command by default assumes paths are from the repository root, instead of from the current directory, though it looks like recent git versions accept a ./ prefix to indicate the latter. My git cat command reverses the assumption, and completely fails on encountering ./ anywhere in the path. I should fix that sometime.

To be fair, I only recently discovered that it failed for paths with a ../ prefix, even when the path remains within the repository. That I have fixed, mostly because I needed it. While there, I repaired its behavior for paths with spaces; that involved learning more about bash’s array variable syntax. It can even handle the empty string, though that’s far more limited in utility.

Even more recently, I discovered that it behaves oddly when only no path is given, but a commit and/or double-dash is. Namely, it shows the HEAD commit.

All that said, if you still want the command for your own use, its source is available in my dotfiles repository.  I should probably put a proper license on it; considering how little real work it does, a permissive license seems to be in order.


Written by eswald

24 Jul 2012 at 10:21 pm

Posted in Linux, Technology

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s