Skip to content

Commit 51da3b7

Browse files
committed
Merge remote-tracking branch 'carlos/archive'
2 parents 81104d4 + ab730cb commit 51da3b7

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

docs/repository.rst

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ Below there are some general attributes and methods:
6969
.. automethod:: pygit2.Repository.write
7070
.. automethod:: pygit2.Repository.reset
7171
.. automethod:: pygit2.Repository.state_cleanup
72+
.. automethod:: pygit2.Repository.write_archive

pygit2/repository.py

+72-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from _pygit2 import Repository as _Repository
3636
from _pygit2 import Oid, GIT_OID_HEXSZ, GIT_OID_MINPREFIXLEN
3737
from _pygit2 import GIT_CHECKOUT_SAFE_CREATE, GIT_DIFF_NORMAL
38+
from _pygit2 import GIT_FILEMODE_LINK
3839
from _pygit2 import Reference, Tree, Commit, Blob
3940

4041
from .config import Config
@@ -43,7 +44,7 @@
4344
from .index import Index
4445
from .remote import Remote
4546
from .blame import Blame
46-
from .utils import to_bytes, to_str
47+
from .utils import to_bytes, to_str, is_string
4748

4849

4950
class Repository(_Repository):
@@ -488,3 +489,73 @@ def index(self):
488489
check_error(err, True)
489490

490491
return Index.from_c(self, cindex)
492+
493+
#
494+
# Utility for writing a tree into an archive
495+
#
496+
def write_archive(self, treeish, archive, timestamp=None):
497+
"""Write treeish into an archive
498+
499+
If no timestamp is provided and 'treeish' is a commit, its committer
500+
timestamp will be used. Otherwise the current time will be used.
501+
502+
Arguments:
503+
504+
treeish
505+
The treeish to write.
506+
archive
507+
An archive from the 'tarfile' module
508+
timestamp
509+
Timestamp to use for the files in the archive.
510+
511+
Example::
512+
513+
>>> import tarfile, pygit2
514+
>>>> with tarfile.open('foo.tar', 'w') as archive:
515+
>>>> repo = pygit2.Repsitory('.')
516+
>>>> repo.write_archive(archive, repo.head.target)
517+
"""
518+
519+
import tarfile, sys
520+
from time import time
521+
if sys.version_info[0] < 3:
522+
from cStringIO import StringIO
523+
else:
524+
from io import BytesIO as StringIO
525+
526+
# Try to get a tree form whatever we got
527+
if isinstance(treeish, Tree):
528+
tree = treeish
529+
530+
if isinstance(treeish, Oid) or is_string(treeish):
531+
treeish = self[treeish]
532+
533+
# if we don't have a timestamp, try to get it from a commit
534+
if not timestamp:
535+
try:
536+
commit = treeish.peel(Commit)
537+
timestamp = commit.committer.time
538+
except:
539+
pass
540+
541+
# as a last resort, use the current timestamp
542+
if not timestamp:
543+
timestamp = int(time())
544+
545+
tree = treeish.peel(Tree)
546+
547+
index = Index()
548+
index.read_tree(tree)
549+
550+
for entry in index:
551+
content = self[entry.id].read_raw()
552+
info = tarfile.TarInfo(entry.path)
553+
info.size = len(content)
554+
info.mtime = timestamp
555+
info.uname = info.gname = 'root' # just because git does this
556+
if entry.mode == GIT_FILEMODE_LINK:
557+
info.type = archive.SYMTYPE
558+
info.linkname = content
559+
info.mode = 0o777 # symlinks get placeholder
560+
561+
archive.addfile(info, StringIO(content))

test/test_archive.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Copyright 2010-2014 The pygit2 contributors
4+
#
5+
# This file is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License, version 2,
7+
# as published by the Free Software Foundation.
8+
#
9+
# In addition to the permissions in the GNU General Public License,
10+
# the authors give you unlimited permission to link the compiled
11+
# version of this file into combinations with other programs,
12+
# and to distribute those combinations without any restriction
13+
# coming from the use of this file. (The General Public License
14+
# restrictions do apply in other respects; for example, they cover
15+
# modification of the file, and distribution when not linked into
16+
# a combined executable.)
17+
#
18+
# This file is distributed in the hope that it will be useful, but
19+
# WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
# General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU General Public License
24+
# along with this program; see the file COPYING. If not, write to
25+
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
26+
# Boston, MA 02110-1301, USA.
27+
28+
"""Tests for Blame objects."""
29+
30+
from __future__ import absolute_import
31+
from __future__ import unicode_literals
32+
import unittest
33+
import pygit2
34+
from pygit2 import Index, Oid, Tree, Object
35+
import tarfile
36+
import os
37+
from . import utils
38+
from time import time
39+
40+
TREE_HASH = 'fd937514cb799514d4b81bb24c5fcfeb6472b245'
41+
COMMIT_HASH = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
42+
43+
class ArchiveTest(utils.RepoTestCase):
44+
45+
def check_writing(self, treeish, timestamp=None):
46+
archive = tarfile.open('foo.tar', mode='w')
47+
self.repo.write_archive(treeish, archive)
48+
49+
index = Index()
50+
if isinstance(treeish, Object):
51+
index.read_tree(treeish.peel(Tree))
52+
else:
53+
index.read_tree(self.repo[treeish].peel(Tree))
54+
55+
self.assertEqual(len(index), len(archive.getmembers()))
56+
57+
if timestamp:
58+
fileinfo = archive.getmembers()[0]
59+
self.assertEqual(timestamp, fileinfo.mtime)
60+
61+
archive.close()
62+
self.assertTrue(os.path.isfile('foo.tar'))
63+
os.remove('foo.tar')
64+
65+
def test_write_tree(self):
66+
self.check_writing(TREE_HASH)
67+
self.check_writing(Oid(hex=TREE_HASH))
68+
self.check_writing(self.repo[TREE_HASH])
69+
70+
def test_write_commit(self):
71+
commit_timestamp = self.repo[COMMIT_HASH].committer.time
72+
self.check_writing(COMMIT_HASH, commit_timestamp)
73+
self.check_writing(Oid(hex=COMMIT_HASH), commit_timestamp)
74+
self.check_writing(self.repo[COMMIT_HASH], commit_timestamp)

0 commit comments

Comments
 (0)