/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
1
# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Support for pristine tar deltas."""
19
20
from base64 import (
21
    standard_b64decode,
22
    )
23
0.277.4 by Jelmer Vernooij
Add function for storing git pristine tar delta files.
24
from dulwich.objects import (
25
    Blob,
26
    Tree,
27
    )
28
29
import stat
30
31
README_CONTENTS = """\
32
This branch contains delta files that pristine-tar can use to
33
regenerate tarballs for its own releases.
34
"""
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
35
36
def revision_pristine_tar_data(rev):
37
    """Export the pristine tar data from a revision."""
38
    if 'deb-pristine-delta' in rev.properties:
39
        uuencoded = rev.properties['deb-pristine-delta']
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
40
        kind = 'gz'
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
41
    elif 'deb-pristine-delta-bz2' in rev.properties:
42
        uuencoded = rev.properties['deb-pristine-delta-bz2']
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
43
        kind = 'bz2'
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
44
    elif 'deb-pristine-delta-xz' in rev.properties:
45
        uuencoded = rev.properties['deb-pristine-delta-xz']
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
46
        kind = 'xz'
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
47
    else:
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
48
        raise KeyError(rev.revision_id)
0.277.1 by Jelmer Vernooij
Add function for extracting pristine tar data.
49
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
50
    return (standard_b64decode(uuencoded), kind)
0.277.3 by Jelmer Vernooij
Add function for reading pristine tar data from a git repo.
51
52
53
def get_pristine_tar_tree(repo):
54
    """Retrieve the pristine tar tree for a repository.
55
56
    """
57
    try:
58
        cid = repo.refs["refs/heads/pristine-tar"]
59
    except KeyError:
60
        return Tree()
61
    tid = repo.object_store[cid].tree
62
    return repo.object_store[tid]
63
64
65
def read_git_pristine_tar_data(repo, filename):
66
    """Read pristine data from a Git repository.
67
68
    :param repo: Git repository to read from
69
    :param filename: Name of file to read
70
    :return: Tuple with delta and id
71
    """
72
    tree = get_pristine_tar_tree(repo)
73
    delta = tree[filename + ".delta"][1]
74
    gitid = tree[filename + ".id"][1]
75
    return (repo.object_store[delta].data,
76
            repo.object_store[gitid].data)
0.277.4 by Jelmer Vernooij
Add function for storing git pristine tar delta files.
77
78
79
def store_git_pristine_tar_data(repo, filename, delta, gitid,
80
        message=None, **kwargs):
81
    """Add pristine tar data to a Git repository.
82
83
    :param repo: Git repository to add data to
84
    :param filename: Name of file to store for
85
    :param delta: pristine-tar delta
86
    :param gitid: Git id the pristine tar delta is generated against
87
    """
88
    delta_ob = Blob.from_string(delta)
89
    delta_name = filename + ".delta"
90
    id_ob = Blob.from_string(gitid)
91
    id_name = filename + ".id"
92
    objects = [
93
        (delta_ob, delta_name),
94
        (id_ob, id_name)]
95
    tree = get_pristine_tar_tree(repo)
96
    tree.add(delta_name, stat.S_IFREG | 0644, delta_ob.id)
97
    tree.add(id_name, stat.S_IFREG | 0644, id_ob.id)
98
    if not "README" in tree:
99
        readme_ob = Blob.from_string(README_CONTENTS)
100
        objects.append((readme_ob, "README"))
101
        tree.add("README", stat.S_IFREG | 0644, readme_ob.id)
102
    objects.append((tree, ""))
103
    repo.object_store.add_objects(objects)
104
    if message is None:
105
        message = 'pristine-tar data for %s' % filename
106
    return repo.do_commit(ref='refs/heads/pristine-tar', tree=tree.id,
107
        message=message, **kwargs)