/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/pristine_tar.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
 
 
18
"""Support for pristine tar deltas."""
 
19
 
 
20
from base64 import (
 
21
    standard_b64decode,
 
22
    )
 
23
 
 
24
from dulwich.objects import (
 
25
    Blob,
 
26
    Tree,
 
27
    )
 
28
 
 
29
import stat
 
30
 
 
31
README_CONTENTS = b"""\
 
32
This branch contains delta files that pristine-tar can use to
 
33
regenerate tarballs for its own releases.
 
34
"""
 
35
 
 
36
 
 
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']
 
41
        kind = 'gz'
 
42
    elif 'deb-pristine-delta-bz2' in rev.properties:
 
43
        uuencoded = rev.properties['deb-pristine-delta-bz2']
 
44
        kind = 'bz2'
 
45
    elif 'deb-pristine-delta-xz' in rev.properties:
 
46
        uuencoded = rev.properties['deb-pristine-delta-xz']
 
47
        kind = 'xz'
 
48
    else:
 
49
        raise KeyError(rev.revision_id)
 
50
 
 
51
    return (standard_b64decode(uuencoded), kind)
 
52
 
 
53
 
 
54
def get_pristine_tar_tree(repo):
 
55
    """Retrieve the pristine tar tree for a repository.
 
56
 
 
57
    """
 
58
    try:
 
59
        cid = repo.refs[b"refs/heads/pristine-tar"]
 
60
    except KeyError:
 
61
        return Tree()
 
62
    tid = repo.object_store[cid].tree
 
63
    return repo.object_store[tid]
 
64
 
 
65
 
 
66
def read_git_pristine_tar_data(repo, filename):
 
67
    """Read pristine data from a Git repository.
 
68
 
 
69
    :param repo: Git repository to read from
 
70
    :param filename: Name of file to read
 
71
    :return: Tuple with delta and id
 
72
    """
 
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)
 
78
 
 
79
 
 
80
def store_git_pristine_tar_data(repo, filename, delta, gitid,
 
81
                                message=None, **kwargs):
 
82
    """Add pristine tar data to a Git repository.
 
83
 
 
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
 
88
    """
 
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"
 
93
    objects = [
 
94
        (delta_ob, delta_name),
 
95
        (id_ob, id_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)
 
105
    if message is None:
 
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)