-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-archive.py
executable file
·45 lines (35 loc) · 1.22 KB
/
git-archive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import tarfile, os, argparse
import pygit2 as git
from cStringIO import StringIO
from pygit2 import GIT_FILEMODE_LINK as SYMLINK
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('treeish', metavar='tree-ish', default='HEAD', nargs='?', help='Tree-ish to package')
parser.add_argument('-o', '--output', help='Select the filename')
return parser.parse_args()
args = parse_args()
repo = git.Repository('.')
# FIXME: This assumes a ref pointing to a commit
oid = repo.lookup_reference(args.treeish).resolve().target
commit = repo[oid]
timestamp = commit.committer.time
tree = commit.tree
filename = args.output
if filename == None:
filename = "%s.tar" % (args.treeish)
out = tarfile.open(filename, mode='w')
index = git.Index()
index.read_tree(tree)
for entry in index:
content = repo[entry.id].read_raw()
info = tarfile.TarInfo(entry.name)
info.size = len(content)
info.mtime = timestamp
info.uname = info.gname = 'root' # just because git does this
if entry.mode == SYMLINK:
info.type = tarfile.SYMTYPE
info.linkname = content
info.mode = 0777 # symlinks get placeholder
tar.addfile(info, StringIO(content))
out.close()