1
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Support for pristine tar deltas."""
24
from dulwich.objects import (
31
README_CONTENTS = b"""\
32
This branch contains delta files that pristine-tar can use to
33
regenerate tarballs for its own releases.
37
def revision_pristine_tar_data(rev):
38
"""Export the pristine tar data from a revision."""
39
if 'deb-pristine-delta' in rev.properties:
40
uuencoded = rev.properties['deb-pristine-delta']
42
elif 'deb-pristine-delta-bz2' in rev.properties:
43
uuencoded = rev.properties['deb-pristine-delta-bz2']
45
elif 'deb-pristine-delta-xz' in rev.properties:
46
uuencoded = rev.properties['deb-pristine-delta-xz']
49
raise KeyError(rev.revision_id)
51
return (standard_b64decode(uuencoded), kind)
54
def get_pristine_tar_tree(repo):
55
"""Retrieve the pristine tar tree for a repository.
59
cid = repo.refs[b"refs/heads/pristine-tar"]
62
tid = repo.object_store[cid].tree
63
return repo.object_store[tid]
66
def read_git_pristine_tar_data(repo, filename):
67
"""Read pristine data from a Git repository.
69
:param repo: Git repository to read from
70
:param filename: Name of file to read
71
:return: Tuple with delta and id
73
tree = get_pristine_tar_tree(repo)
74
delta = tree[filename + b".delta"][1]
75
gitid = tree[filename + b".id"][1]
76
return (repo.object_store[delta].data,
77
repo.object_store[gitid].data)
80
def store_git_pristine_tar_data(repo, filename, delta, gitid,
81
message=None, **kwargs):
82
"""Add pristine tar data to a Git repository.
84
:param repo: Git repository to add data to
85
:param filename: Name of file to store for
86
:param delta: pristine-tar delta
87
:param gitid: Git id the pristine tar delta is generated against
89
delta_ob = Blob.from_string(delta)
90
delta_name = filename + b".delta"
91
id_ob = Blob.from_string(gitid)
92
id_name = filename + b".id"
94
(delta_ob, delta_name),
96
tree = get_pristine_tar_tree(repo)
97
tree.add(delta_name, stat.S_IFREG | 0o644, delta_ob.id)
98
tree.add(id_name, stat.S_IFREG | 0o644, id_ob.id)
99
if b"README" not in tree:
100
readme_ob = Blob.from_string(README_CONTENTS)
101
objects.append((readme_ob, b"README"))
102
tree.add(b"README", stat.S_IFREG | 0o644, readme_ob.id)
103
objects.append((tree, ""))
104
repo.object_store.add_objects(objects)
106
message = b'pristine-tar data for %s' % filename
107
return repo.do_commit(ref=b'refs/heads/pristine-tar', tree=tree.id,
108
message=message, **kwargs)