/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-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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 __future__ import absolute_import
 
21
 
 
22
from base64 import (
 
23
    standard_b64decode,
 
24
    )
 
25
 
 
26
from dulwich.objects import (
 
27
    Blob,
 
28
    Tree,
 
29
    )
 
30
 
 
31
import stat
 
32
 
 
33
README_CONTENTS = b"""\
 
34
This branch contains delta files that pristine-tar can use to
 
35
regenerate tarballs for its own releases.
 
36
"""
 
37
 
 
38
 
 
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']
 
43
        kind = 'gz'
 
44
    elif 'deb-pristine-delta-bz2' in rev.properties:
 
45
        uuencoded = rev.properties['deb-pristine-delta-bz2']
 
46
        kind = 'bz2'
 
47
    elif 'deb-pristine-delta-xz' in rev.properties:
 
48
        uuencoded = rev.properties['deb-pristine-delta-xz']
 
49
        kind = 'xz'
 
50
    else:
 
51
        raise KeyError(rev.revision_id)
 
52
 
 
53
    return (standard_b64decode(uuencoded), kind)
 
54
 
 
55
 
 
56
def get_pristine_tar_tree(repo):
 
57
    """Retrieve the pristine tar tree for a repository.
 
58
 
 
59
    """
 
60
    try:
 
61
        cid = repo.refs[b"refs/heads/pristine-tar"]
 
62
    except KeyError:
 
63
        return Tree()
 
64
    tid = repo.object_store[cid].tree
 
65
    return repo.object_store[tid]
 
66
 
 
67
 
 
68
def read_git_pristine_tar_data(repo, filename):
 
69
    """Read pristine data from a Git repository.
 
70
 
 
71
    :param repo: Git repository to read from
 
72
    :param filename: Name of file to read
 
73
    :return: Tuple with delta and id
 
74
    """
 
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)
 
80
 
 
81
 
 
82
def store_git_pristine_tar_data(repo, filename, delta, gitid,
 
83
                                message=None, **kwargs):
 
84
    """Add pristine tar data to a Git repository.
 
85
 
 
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
 
90
    """
 
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"
 
95
    objects = [
 
96
        (delta_ob, delta_name),
 
97
        (id_ob, id_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)
 
107
    if message is None:
 
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)