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."""
20
from __future__ import absolute_import
26
from dulwich.objects import (
33
README_CONTENTS = b"""\
34
This branch contains delta files that pristine-tar can use to
35
regenerate tarballs for its own releases.
39
def revision_pristine_tar_data(rev):
40
"""Export the pristine tar data from a revision."""
41
if 'deb-pristine-delta' in rev.properties:
42
uuencoded = rev.properties['deb-pristine-delta']
44
elif 'deb-pristine-delta-bz2' in rev.properties:
45
uuencoded = rev.properties['deb-pristine-delta-bz2']
47
elif 'deb-pristine-delta-xz' in rev.properties:
48
uuencoded = rev.properties['deb-pristine-delta-xz']
51
raise KeyError(rev.revision_id)
53
return (standard_b64decode(uuencoded), kind)
56
def get_pristine_tar_tree(repo):
57
"""Retrieve the pristine tar tree for a repository.
61
cid = repo.refs[b"refs/heads/pristine-tar"]
64
tid = repo.object_store[cid].tree
65
return repo.object_store[tid]
68
def read_git_pristine_tar_data(repo, filename):
69
"""Read pristine data from a Git repository.
71
:param repo: Git repository to read from
72
:param filename: Name of file to read
73
:return: Tuple with delta and id
75
tree = get_pristine_tar_tree(repo)
76
delta = tree[filename + b".delta"][1]
77
gitid = tree[filename + b".id"][1]
78
return (repo.object_store[delta].data,
79
repo.object_store[gitid].data)
82
def store_git_pristine_tar_data(repo, filename, delta, gitid,
83
message=None, **kwargs):
84
"""Add pristine tar data to a Git repository.
86
:param repo: Git repository to add data to
87
:param filename: Name of file to store for
88
:param delta: pristine-tar delta
89
:param gitid: Git id the pristine tar delta is generated against
91
delta_ob = Blob.from_string(delta)
92
delta_name = filename + b".delta"
93
id_ob = Blob.from_string(gitid)
94
id_name = filename + b".id"
96
(delta_ob, delta_name),
98
tree = get_pristine_tar_tree(repo)
99
tree.add(delta_name, stat.S_IFREG | 0o644, delta_ob.id)
100
tree.add(id_name, stat.S_IFREG | 0o644, id_ob.id)
101
if b"README" not in tree:
102
readme_ob = Blob.from_string(README_CONTENTS)
103
objects.append((readme_ob, b"README"))
104
tree.add(b"README", stat.S_IFREG | 0o644, readme_ob.id)
105
objects.append((tree, ""))
106
repo.object_store.add_objects(objects)
108
message = b'pristine-tar data for %s' % filename
109
return repo.do_commit(ref=b'refs/heads/pristine-tar', tree=tree.id,
110
message=message, **kwargs)