/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 commit.py

Fix tag handling when encountering packed refs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 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 committing in native Git working trees."""
 
19
 
 
20
 
 
21
from dulwich.index import (
 
22
    commit_tree,
 
23
    )
 
24
import stat
 
25
 
 
26
from bzrlib.repository import (
 
27
    CommitBuilder,
 
28
    )
 
29
 
 
30
from dulwich.objects import (
 
31
    S_IFGITLINK,
 
32
    Blob,
 
33
    Commit,
 
34
    )
 
35
 
 
36
 
 
37
from bzrlib.plugins.git.mapping import (
 
38
    entry_mode,
 
39
    )
 
40
 
 
41
 
 
42
class GitCommitBuilder(CommitBuilder):
 
43
 
 
44
    def __init__(self, *args, **kwargs):
 
45
        super(GitCommitBuilder, self).__init__(*args, **kwargs)
 
46
        self.store = self.repository._git.object_store
 
47
        self._blobs = {}
 
48
 
 
49
    def record_entry_contents(self, ie, parent_invs, path, tree,
 
50
        content_summary):
 
51
        raise NotImplementedError(self.record_entry_contents)        
 
52
 
 
53
    def record_delete(self, path, file_id):
 
54
        self._blobs[path] = None
 
55
        self._any_changes = True
 
56
 
 
57
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
 
58
        index = getattr(workingtree, "index", None)
 
59
        if index is not None:
 
60
            def index_sha1(path, file_id):
 
61
                return index.get_sha1(path.encode("utf-8"))
 
62
            text_sha1 = link_sha1 = index_sha1
 
63
        else:
 
64
            def link_sha1(path, file_id):
 
65
                blob = Blob()
 
66
                blob.data = workingtree.get_symlink_target(file_id)
 
67
                return blob.id
 
68
            def text_sha1(path, file_id):
 
69
                blob = Blob()
 
70
                blob.data = workingtree.get_file_text(file_id, path)
 
71
                return blob.id
 
72
        for (file_id, path, changed_content, versioned, parent, name, kind, 
 
73
             executable) in iter_changes:
 
74
            if kind[1] in ("directory",):
 
75
                if kind[0] in ("file", "symlink"):
 
76
                    self.record_delete(path[0], file_id)
 
77
                continue
 
78
            if path[1] is None:
 
79
                self.record_delete(path[0], file_id)
 
80
                continue
 
81
            if kind[1] == "file":
 
82
                mode = stat.S_IFREG
 
83
                sha = text_sha1(path[1], file_id)
 
84
            elif kind[1] == "symlink":
 
85
                mode = stat.S_IFLNK
 
86
                sha = link_sha1(path[1], file_id)
 
87
            elif kind[1] == "tree-reference":
 
88
                mode = S_IFGITLINK
 
89
                sha = "FIXME"
 
90
            else:
 
91
                raise AssertionError("Unknown kind %r" % kind[1])
 
92
            if executable[1]:
 
93
                mode |= 0111
 
94
            self._any_changes = True
 
95
            self._blobs[path[1].encode("utf-8")] = (mode, sha)
 
96
            yield file_id, path, (None, None)
 
97
        # Fill in entries that were not changed
 
98
        basis_tree = workingtree.basis_tree()
 
99
        assert basis_tree.get_revision_id() == basis_revid
 
100
        for path, entry in basis_tree.iter_entries_by_dir():
 
101
            if entry.kind not in ("file", "symlink"):
 
102
                continue
 
103
            if not path in self._blobs:
 
104
                blob = Blob()
 
105
                if entry.kind == "symlink":
 
106
                    blob.data = basis_tree.get_symlink_target(entry.file_id)
 
107
                else:
 
108
                    blob.data = basis_tree.get_file_text(entry.file_id)
 
109
                self._blobs[path.encode("utf-8")] = (entry_mode(entry), blob.id)
 
110
 
 
111
    def finish_inventory(self):
 
112
        # eliminate blobs that were removed
 
113
        for path, entry in self._blobs.iteritems():
 
114
            if entry is None:
 
115
                del self._blobs[path]
 
116
 
 
117
    def commit(self, message):
 
118
        c = Commit()
 
119
        c.parents = [self.repository.lookup_git_revid(revid)[0] for revid in self.parents]
 
120
        c.tree = commit_tree(self.store, 
 
121
                [(path, sha, mode) for (path, (mode, sha)) in self._blobs.iteritems()])
 
122
        c.committer = self._committer
 
123
        c.author = self._revprops.get('author', self._committer)
 
124
        c.commit_time = int(self._timestamp)
 
125
        c.author_time = int(self._timestamp)
 
126
        c.commit_timezone = self._timezone
 
127
        c.author_timezone = self._timezone
 
128
        c.message = message.encode("utf-8")
 
129
        self.store.add_object(c)
 
130
        return self.repository.get_mapping().revision_id_foreign_to_bzr(c.id)