Monday, April 23, 2012

Creating A Source Tarball From A Github Commit

I won't claim this: I got the initial script from Eric Smith in the Fedora development mailing list. I then enhanced it a bit to make it more useful for other Github projects on which I work. But I definitely wanted to share this with everybody since it's pretty sweet and simple. To get a source tarball based on a specific git checkin with Github, you can use the following script:





#!/bin/sh

usage() {
  printf "Usage: ${ME} COMMITHASH [username] [project]\n"
  printf "\n"
  printf "\t[username] -- Overrides the default in ~/.tarballrc\n"
  printf "\t[project]  -- Override the default in ~/.tarballrc\n"
  printf "\n"
  printf "EXAMPLE ~/.tarballrc file:\n\n"
  printf "TBUSERNAME=[your username]\n"
  printf "TBPROJECT=[my project's name]\n"
  printf "\n"
}

ME=$(basename ${0})
die() { printf "$@\n"; exit 1; }

if [[ "${1}" == "-h" ]]; then
  usage
  exit 0
fi

[ -s ~/.tarballrc ] && source ~/.tarballrc

username=${2:-$TBUSERNAME}
project=${3:-$TBPROJECT}
commit=${1}


if [[ -z "${username}" ]]; then die "You must provide a username."; fi
if [[ -z "${project}" ]];  then die "You must name a project."; fi
if [[ -z "${commit}" ]];   then die "You must specify a commit."; fi


REPO="git://github.com/${username}/${project}"


git clone ${REPO}


( cd ${project} && \
  git archive --format=tar --prefix=${project}-${commit}/ ${commit} \
) | xz - >${project}-${commit}.tar.xz


This script will take as input 1) the commit hash for the checkout, and optionally 2) the username for the Github repo and 3) the Github repo name itself. So, for example, to clone my Newt Syrup repo, you could use:

tarball 6f0056 mcpierce newt-syrup

and this would give you the code for all commits up to 30 July 2011.