No “svn cat” in git.

A couple of years ago, I was working with a guy who write a little script that I’ve found completely invaluable. It simply did the following:

  • Accepted a filename of a file in a svn working copy tree.
  • Grabbed the previous checked in version of the file.
  • Opened the two files in the Filemerge utility on a mac.

Filemerge is a great utility for looking at diffs side by side. From the command line, you use the opendiff command to get at it via scripting.

However, with all the recent work I’ve been doing with Git I’ve been been without my nice little tool. Yesterday I finished up some work on parsing xml and found that the diff via the terminal was just a little too much to go through. So I decided to hack up a Git version of the tool and add a couple of enhancements. Turns out not so easy.
The problem was that Git doesn’t have a simple way to output the checked in copy of a particular file. The previous version of the script used “svn cat” to output from revision control. Git does not have an equivalent. So I hacked my own version using three different Git commands in the process.
To get the equivalent of “svn cat” in git, you need to do the following commands:
  1. svn log
    This will print out a list of the commits that the file has been associated with. Your most concerned with the first commit listed in the log (which represents the last commit). Grab the hash for that commit. Should look like this:

    commit 9978b72045c139b80eab960f7381502fa2106374

  2. svn ls-tree
    This will list the files associated with that commit and give you the hashes for the particular version of that file. Grab the hash for the particular file you’re interested in. Should look like this:

    100644 blob 342c0eacda335f17d21275f657e827c83e5e3f48 filename

  3. svn show -p
    This will print to stdout the contents of that file at that version. This is what we’ve been looking for.

Viola. Now I have the contents of the previous checked in version of the file and am happy as a clam. I was able to get the script working after that.

When I can get around to spending a little time cleaning up the code, I’ll publish it here for folks to use.


5 Responses to “No “svn cat” in git.”

  1. May 13, 2009 at 1:39 am

    Actually, you can just use git show. e.g. git show commit_id:some/filename.txt. This syntax is described in git rev-parse.

    • 2 jedi
      May 13, 2009 at 9:46 am

      Thanks for the comment.

      Yep, you are correct about the “git show” syntax. I couldn’t get this to work initially when I was hacking on it, so I tried a different route. Turns out the issue was that I wasn’t providing the full path relative to the top of the repository for the file.

      However, the script only requires you naming the file and not the full path. This presents a problem for still having to using the blob hash vs the commit hash. If I can pull the full path relative to the top from another command, that would simplify things. Any ideas?

  2. May 14, 2009 at 5:39 am

    You can use git ls-files to get a list of all files. That might help?

    • 4 jedi
      May 14, 2009 at 7:57 am

      Yep, that worked. To get the full path name, you’d do:

      git ls-files –full-name

      I’ll update the post shortly.

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

May 2009
« Mar    

%d bloggers like this: