/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.910 by Jelmer Vernooij
update copyright years
1
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
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
0.200.405 by Jelmer Vernooij
More work on commit.
17
18
"""Support for committing in native Git working trees."""
19
20
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
21
from dulwich.index import (
22
    commit_tree,
23
    )
0.200.405 by Jelmer Vernooij
More work on commit.
24
import stat
25
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
26
from bzrlib.errors import (
27
    RootMissing,
28
    )
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
29
from bzrlib.repository import (
30
    CommitBuilder,
31
    )
32
33
from dulwich.objects import (
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
34
    S_IFGITLINK,
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
35
    Blob,
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
36
    Commit,
37
    )
38
39
0.200.510 by Jelmer Vernooij
Fill in old entries.
40
from bzrlib.plugins.git.mapping import (
41
    entry_mode,
42
    )
43
44
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
45
class GitCommitBuilder(CommitBuilder):
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
46
    """Commit builder for Git repositories."""
47
48
    supports_record_entry_contents = False
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
49
50
    def __init__(self, *args, **kwargs):
51
        super(GitCommitBuilder, self).__init__(*args, **kwargs)
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
52
        self.store = self.repository._git.object_store
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
53
        self._blobs = {}
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
54
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
55
    def record_entry_contents(self, ie, parent_invs, path, tree,
56
        content_summary):
0.200.668 by Jelmer Vernooij
Fix some places where we were way too much memory for repositories with a large number of entries in the inventory and a large number of revisions.
57
        raise NotImplementedError(self.record_entry_contents)
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
58
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
59
    def record_delete(self, path, file_id):
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
60
        self._blobs[path] = None
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
61
        self._any_changes = True
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
62
63
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
64
        index = getattr(workingtree, "index", None)
65
        if index is not None:
66
            def index_sha1(path, file_id):
67
                return index.get_sha1(path.encode("utf-8"))
68
            text_sha1 = link_sha1 = index_sha1
69
        else:
70
            def link_sha1(path, file_id):
71
                blob = Blob()
72
                blob.data = workingtree.get_symlink_target(file_id)
73
                return blob.id
74
            def text_sha1(path, file_id):
75
                blob = Blob()
76
                blob.data = workingtree.get_file_text(file_id, path)
77
                return blob.id
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
78
        seen_root = False
0.200.668 by Jelmer Vernooij
Fix some places where we were way too much memory for repositories with a large number of entries in the inventory and a large number of revisions.
79
        for (file_id, path, changed_content, versioned, parent, name, kind,
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
80
             executable) in iter_changes:
81
            if kind[1] in ("directory",):
82
                if kind[0] in ("file", "symlink"):
83
                    self.record_delete(path[0], file_id)
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
84
                if path[1] == "":
85
                    seen_root = True
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
86
                continue
0.200.460 by Jelmer Vernooij
Somewhat fix commit in git working trees.
87
            if path[1] is None:
88
                self.record_delete(path[0], file_id)
89
                continue
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
90
            if kind[1] == "file":
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
91
                mode = stat.S_IFREG
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
92
                sha = text_sha1(path[1], file_id)
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
93
            elif kind[1] == "symlink":
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
94
                mode = stat.S_IFLNK
0.200.459 by Jelmer Vernooij
Use new commit_tree function from dulwich.
95
                sha = link_sha1(path[1], file_id)
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
96
            elif kind[1] == "tree-reference":
97
                mode = S_IFGITLINK
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
98
                sha = "FIXME" # FIXME
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
99
            else:
100
                raise AssertionError("Unknown kind %r" % kind[1])
0.200.523 by Jelmer Vernooij
Fix undefined error.
101
            if executable[1]:
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
102
                mode |= 0111
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
103
            self._any_changes = True
0.200.460 by Jelmer Vernooij
Somewhat fix commit in git working trees.
104
            self._blobs[path[1].encode("utf-8")] = (mode, sha)
0.200.700 by Jelmer Vernooij
Support committing to a git branch from a bzr working tree.
105
            file_sha1 = workingtree.get_file_sha1(file_id, path[1])
0.200.1092 by Jelmer Vernooij
Avoid dependency on os.lstat.
106
            _, st = workingtree.get_file_with_stat(file_id, path[1])
107
            yield file_id, path[1], (file_sha1, st)
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
108
        if not seen_root and len(self.parents) == 0:
109
            raise RootMissing()
0.200.510 by Jelmer Vernooij
Fill in old entries.
110
        # Fill in entries that were not changed
111
        basis_tree = workingtree.basis_tree()
0.200.1136 by Jelmer Vernooij
Make assert a bit more verbose.
112
        assert basis_tree.get_revision_id() == basis_revid, "expected %r == %r" % (
113
            basis_tree.get_revision_id(), basis_revid)
0.200.510 by Jelmer Vernooij
Fill in old entries.
114
        for path, entry in basis_tree.iter_entries_by_dir():
115
            if entry.kind not in ("file", "symlink"):
116
                continue
117
            if not path in self._blobs:
118
                blob = Blob()
119
                if entry.kind == "symlink":
120
                    blob.data = basis_tree.get_symlink_target(entry.file_id)
121
                else:
122
                    blob.data = basis_tree.get_file_text(entry.file_id)
123
                self._blobs[path.encode("utf-8")] = (entry_mode(entry), blob.id)
0.200.1145 by Jelmer Vernooij
reset new_inventory after record_iter_changes
124
        self.new_inventory = None
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
125
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
126
    def finish_inventory(self):
0.200.510 by Jelmer Vernooij
Fill in old entries.
127
        # eliminate blobs that were removed
0.200.998 by Jelmer Vernooij
Don't modify dictionary during iteration.
128
        for path, entry in iter(self._blobs.items()):
0.200.510 by Jelmer Vernooij
Fill in old entries.
129
            if entry is None:
130
                del self._blobs[path]
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
131
0.200.737 by Jelmer Vernooij
Add helper function, always set encoding to utf-8.
132
    def _iterblobs(self):
133
        return ((path, sha, mode) for (path, (mode, sha)) in self._blobs.iteritems())
134
0.200.391 by Jelmer Vernooij
Fix syntax error.
135
    def commit(self, message):
0.200.1177 by Jelmer Vernooij
Check message for validity.
136
        self._validate_unicode_text(message, 'commit message')
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
137
        c = Commit()
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
138
        c.parents = [self.repository.lookup_bzr_revision_id(revid)[0] for revid in self.parents]
0.200.737 by Jelmer Vernooij
Add helper function, always set encoding to utf-8.
139
        c.tree = commit_tree(self.store, self._iterblobs())
0.200.416 by Jelmer Vernooij
Use public properties to set git objects values.
140
        c.committer = self._committer
141
        c.author = self._revprops.get('author', self._committer)
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
142
        c.commit_time = int(self._timestamp)
143
        c.author_time = int(self._timestamp)
0.200.416 by Jelmer Vernooij
Use public properties to set git objects values.
144
        c.commit_timezone = self._timezone
145
        c.author_timezone = self._timezone
0.200.737 by Jelmer Vernooij
Add helper function, always set encoding to utf-8.
146
        c.encoding = 'utf-8'
0.200.416 by Jelmer Vernooij
Use public properties to set git objects values.
147
        c.message = message.encode("utf-8")
0.200.509 by Jelmer Vernooij
Fix a bunch of bugs in commit, partially works now.
148
        self.store.add_object(c)
0.200.1144 by Jelmer Vernooij
Raise RootMissing if root not specified.
149
        assert len(c.id) == 40
0.200.702 by Jelmer Vernooij
properly commit write group
150
        self._new_revision_id = self.repository.get_mapping().revision_id_foreign_to_bzr(c.id)
151
        self.repository.commit_write_group()
152
        return self._new_revision_id
0.200.1191 by Jelmer Vernooij
Implement CommitBuilder.revision_tree.
153
154
    def revision_tree(self):
155
        return self.repository.revision_tree(self._new_revision_id)