Subject: Re: Adding files to a git-archive when it is generated, and whats the best way to find out what branch a commit is on?

From: Jeff Epler <[email-masked]>
Date: Wed, 29 Jul 2009 11:12:02 -0500

When I wanted to add extra files to a tar produced by git-archive, I
used python's tarfile module.  This assumes the tar will fit comfortably
in memory.  The specifics probably don't apply in your case, but maybe
it will be useful anyhow.

You could also use tar's -r or gnu tar's -A modes to add to the
git-archive tar once it's been created:
    git-archive ... > temp.tar
    tar -rf temp.tar additional-file
(unfortunately, -r and -A don't operate on pipes)

def main(args):
    if len(args) == 1:
        version = args[0]
    elif len(args) > 1:
        raise SystemExit, "Usage: %s [version]" % sys.argv[0]
    else:
        status, gitversion = commands.getstatusoutput("git-describe --tags")
        version = highest_version()
        if status != 0 or version != gitversion:
            raise SystemExit, """\
Highest version %r doesn't match description %r.
Specify version number explicitly if this is what you want""" % (
                    version, gitversion)

    version = version.lstrip("v")

    DIRNAME = "%(p)s-%(v)s" % {'p': PACKAGE_NAME, 'v': version}
    TARNAME = DIRNAME + '.tar.gz'

    verstream = StringIO.StringIO("%s\n" % version)
    verinfo = tarfile.TarInfo(DIRNAME + "/VERSION")
    verinfo.mode = 0660
    verinfo.size = len(verstream.getvalue())
    verinfo.mtime = time.time()

    tardata = os.popen("git-archive --prefix=%(p)s/ v%(v)s"
                            % {'p': DIRNAME, 'v': version}).read()
    tarstream = StringIO.StringIO(tardata)

    tar = tarfile.TarFile(mode="a", fileobj=tarstream)
    tar.addfile(verinfo, verstream)
    tar.close()

    out = gzip.open("../" + TARNAME, "wb")
    out.write(tarstream.getvalue())
    out.close()