Easily Create (Clean) Compressed Tarballs of Your Git Repository
Ideally, you could refer the whole world --- or at least, the significant portion thereof that want your code --- to your (public mirror) Git repository. But unfortunately, the whole world does not (yet) use Git ("I know it was you Fredo, I know it was you, and it breaks my heart."). Sad. Sooooo sad. But true. So the only recourse is for you to send these tortured souls an archived snapshot of your code via e-mail. I'll pause now to let you finish retching/sobbing/lamenting/venting. ... Back? Anyway, Git has a neat "archive" command that helps you create the required archive, but perhaps it does not have the most friendliest interface in the world. Drop the following script anywhere on your path name it "git-targz" and set its executable bit on. Then invoking "git targz TARBALL-FILEPATH" will create a tar'd and gzip'd bundle of your (repository's) current HEAD. Similar scripts for bzip'ing and plain old zipping can easily be created by varying the final command, and are shown after the tar + gzip script.
git-targz:
#! /bin/bash if [ -z "$1" ] then echo "Usage: git-targz [rev] path" echo echo "Creates gzipped-compressed tarball archive of the current or specified branch." exit 1 fi if [ "$#" == "1" ] then path=$1 rev="HEAD" else rev=$1 path=$2 fi basepath=$(basename $path) prefix=${basepath%%.*} git archive --format=tar --prefix=$prefix/ $rev | gzip >$path
git-tarbz:
#! /bin/bash if [ -z "$1" ] then echo "Usage: git-tarbz [rev] path" echo echo "Creates bzipped-compressed tarball archive of the current or specified branch." exit 1 fi if [ "$#" == "1" ] then path=$1 rev="HEAD" else rev=$1 path=$2 fi basepath=$(basename $path) prefix=${basepath%%.*} git archive --format=tar --prefix=$prefix/ $rev | bzip2 >$path
git-zip:
#! /bin/bash if [ -z "$1" ] then echo "Usage: git-zip [rev] path" echo echo "Creates zip archive of the current or specified branch." exit 1 fi if [ "$#" == "1" ] then path=$1 rev="HEAD" else rev=$1 path=$2 fi basepath=$(basename $path) prefix=${basepath%%.*} git archive --format=zip --prefix=$prefix/ $rev > $path
I will leave the obviously-needed exercise of bundling these three independent scripts into a single one that takes a switch that determines the compression method as an exercise for the reader. Once my scripts get to that level of complexity, I usually transition into Python.
feed
Comments
2 comments postedNice. You don't mention it, but you can drop these scripts in your libexec/git-core directory and call them as "git targz", etc.
I made a few changes to git-targz (the one that I will likely use) to make it work under standard /bin/sh here: http://gist.github.com/376455 (because not everybody has bash!)
Yes, I'm a little fuzzy on where 'sh' ends and 'bash' begins. Your tweak is definitely useful in that regard.
It would be nice if the script could default to a sensible name if none is given (e.g. "-", so you might get "project-ff3021"). But, as I mentioned, if I extend these scripts any more, it will probably have to be in Python!
I did not know about lib-exec: thanks for pointing that out. However, any executable script on your $PATH that has a "git-XXXX" pattern can be invoked via "git XXXX". So, for example, if '~/bin' is in your $PATH, then a executable script called "~/bin/git-foo" can be invoked via "git foo".
Post new comment