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

  • Committer: Jelmer Vernooij
  • Date: 2019-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Support for committing in native Git working trees."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
from dulwich.index import (
21
23
    commit_tree,
22
 
    read_submodule_head,
23
24
    )
24
25
import stat
25
26
 
38
39
from ..repository import (
39
40
    CommitBuilder,
40
41
    )
 
42
from ..sixish import (
 
43
    viewitems,
 
44
    )
41
45
 
42
46
from dulwich.objects import (
43
47
    Blob,
44
48
    Commit,
45
49
    )
 
50
from dulwich.index import read_submodule_head
46
51
 
47
52
 
48
53
from .mapping import (
49
 
    encode_git_path,
50
54
    object_mode,
51
55
    fix_person_identifier,
52
56
    )
53
57
from .tree import entry_factory
54
58
 
55
59
 
 
60
class SettingCustomFileIdsUnsupported(UnsupportedOperation):
 
61
 
 
62
    _fmt = ("Unable to store addition of file with custom file ids: "
 
63
            "%(file_ids)r")
 
64
 
 
65
    def __init__(self, file_ids):
 
66
        BzrError.__init__(self)
 
67
        self.file_ids = file_ids
 
68
 
 
69
 
56
70
class GitCommitBuilder(CommitBuilder):
57
71
    """Commit builder for Git repositories."""
58
72
 
65
79
        self.store = self.repository._git.object_store
66
80
        self._blobs = {}
67
81
        self._inv_delta = []
68
 
        self._deleted_paths = set()
69
82
        self._any_changes = False
 
83
        self._override_fileids = {}
70
84
        self._mapping = self.repository.get_mapping()
71
85
 
72
86
    def any_changes(self):
74
88
 
75
89
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
76
90
        seen_root = False
77
 
        for change in iter_changes:
78
 
            if change.kind == (None, None):
79
 
                # Ephemeral
80
 
                continue
81
 
            if change.versioned[0] and not change.copied:
82
 
                file_id = self._mapping.generate_file_id(change.path[0])
83
 
            elif change.versioned[1]:
84
 
                file_id = self._mapping.generate_file_id(change.path[1])
85
 
            else:
86
 
                file_id = None
87
 
            if change.path[1]:
88
 
                parent_id_new = self._mapping.generate_file_id(osutils.dirname(change.path[1]))
89
 
            else:
90
 
                parent_id_new = None
91
 
            if change.kind[1] in ("directory",):
 
91
        for (file_id, path, changed_content, versioned, parent, name, kind,
 
92
             executable) in iter_changes:
 
93
            if kind[1] in ("directory",):
92
94
                self._inv_delta.append(
93
 
                    (change.path[0], change.path[1], file_id,
94
 
                     entry_factory[change.kind[1]](
95
 
                         file_id, change.name[1], parent_id_new)))
96
 
                if change.kind[0] in ("file", "symlink"):
97
 
                    self._blobs[encode_git_path(change.path[0])] = None
 
95
                    (path[0], path[1], file_id, entry_factory[kind[1]](
 
96
                        file_id, name[1], parent[1])))
 
97
                if kind[0] in ("file", "symlink"):
 
98
                    self._blobs[path[0].encode("utf-8")] = None
98
99
                    self._any_changes = True
99
 
                if change.path[1] == "":
 
100
                if path[1] == "":
100
101
                    seen_root = True
101
102
                continue
102
103
            self._any_changes = True
103
 
            if change.path[1] is None:
104
 
                self._inv_delta.append((change.path[0], change.path[1], file_id, None))
105
 
                self._deleted_paths.add(encode_git_path(change.path[0]))
 
104
            if path[1] is None:
 
105
                self._inv_delta.append((path[0], path[1], file_id, None))
 
106
                self._blobs[path[0].encode("utf-8")] = None
106
107
                continue
107
108
            try:
108
 
                entry_kls = entry_factory[change.kind[1]]
 
109
                entry_kls = entry_factory[kind[1]]
109
110
            except KeyError:
110
 
                raise KeyError("unknown kind %s" % change.kind[1])
111
 
            entry = entry_kls(file_id, change.name[1], parent_id_new)
112
 
            if change.kind[1] == "file":
113
 
                entry.executable = change.executable[1]
 
111
                raise KeyError("unknown kind %s" % kind[1])
 
112
            entry = entry_kls(file_id, name[1], parent[1])
 
113
            if kind[1] == "file":
 
114
                entry.executable = executable[1]
114
115
                blob = Blob()
115
 
                f, st = workingtree.get_file_with_stat(change.path[1])
 
116
                f, st = workingtree.get_file_with_stat(path[1])
116
117
                try:
117
118
                    blob.data = f.read()
118
119
                finally:
119
120
                    f.close()
 
121
                entry.text_size = len(blob.data)
 
122
                entry.text_sha1 = osutils.sha_string(blob.data)
 
123
                self.store.add_object(blob)
120
124
                sha = blob.id
121
 
                if st is not None:
122
 
                    entry.text_size = st.st_size
123
 
                else:
124
 
                    entry.text_size = len(blob.data)
125
 
                entry.git_sha1 = sha
126
 
                self.store.add_object(blob)
127
 
            elif change.kind[1] == "symlink":
128
 
                symlink_target = workingtree.get_symlink_target(change.path[1])
 
125
            elif kind[1] == "symlink":
 
126
                symlink_target = workingtree.get_symlink_target(path[1])
129
127
                blob = Blob()
130
 
                blob.data = encode_git_path(symlink_target)
 
128
                blob.data = symlink_target.encode("utf-8")
131
129
                self.store.add_object(blob)
132
130
                sha = blob.id
133
131
                entry.symlink_target = symlink_target
134
132
                st = None
135
 
            elif change.kind[1] == "tree-reference":
136
 
                sha = read_submodule_head(workingtree.abspath(change.path[1]))
137
 
                reference_revision = workingtree.get_reference_revision(change.path[1])
 
133
            elif kind[1] == "tree-reference":
 
134
                sha = read_submodule_head(workingtree.abspath(path[1]))
 
135
                reference_revision = workingtree.get_reference_revision(path[1])
138
136
                entry.reference_revision = reference_revision
139
137
                st = None
140
138
            else:
141
 
                raise AssertionError("Unknown kind %r" % change.kind[1])
142
 
            mode = object_mode(change.kind[1], change.executable[1])
143
 
            self._inv_delta.append((change.path[0], change.path[1], file_id, entry))
144
 
            if change.path[0] is not None:
145
 
                self._deleted_paths.add(encode_git_path(change.path[0]))
146
 
            self._blobs[encode_git_path(change.path[1])] = (mode, sha)
 
139
                raise AssertionError("Unknown kind %r" % kind[1])
 
140
            mode = object_mode(kind[1], executable[1])
 
141
            self._inv_delta.append((path[0], path[1], file_id, entry))
 
142
            encoded_new_path = path[1].encode("utf-8")
 
143
            self._blobs[encoded_new_path] = (mode, sha)
147
144
            if st is not None:
148
 
                yield change.path[1], (entry.git_sha1, st)
 
145
                yield path[1], (entry.text_sha1, st)
 
146
            if self._mapping.generate_file_id(encoded_new_path) != file_id:
 
147
                self._override_fileids[encoded_new_path] = file_id
 
148
            else:
 
149
                self._override_fileids[encoded_new_path] = None
149
150
        if not seen_root and len(self.parents) == 0:
150
151
            raise RootMissing()
151
152
        if getattr(workingtree, "basis_tree", False):
160
161
        for entry in basis_tree._iter_tree_contents(include_trees=False):
161
162
            if entry.path in self._blobs:
162
163
                continue
163
 
            if entry.path in self._deleted_paths:
164
 
                continue
165
164
            self._blobs[entry.path] = (entry.mode, entry.sha)
 
165
        if not self._lossy:
 
166
            try:
 
167
                fileid_map = dict(basis_tree._fileid_map.file_ids)
 
168
            except AttributeError:
 
169
                fileid_map = {}
 
170
            for path, file_id in viewitems(self._override_fileids):
 
171
                if not isinstance(path, bytes):
 
172
                    raise TypeError(path)
 
173
                if file_id is None:
 
174
                    if path in fileid_map:
 
175
                        del fileid_map[path]
 
176
                else:
 
177
                    if not isinstance(file_id, bytes):
 
178
                        raise TypeError(file_id)
 
179
                    fileid_map[path] = file_id
 
180
            if fileid_map:
 
181
                fileid_blob = self._mapping.export_fileid_map(fileid_map)
 
182
            else:
 
183
                fileid_blob = None
 
184
            if fileid_blob is not None:
 
185
                if self._mapping.BZR_FILE_IDS_FILE is None:
 
186
                    raise SettingCustomFileIdsUnsupported(fileid_map)
 
187
                self.store.add_object(fileid_blob)
 
188
                self._blobs[self._mapping.BZR_FILE_IDS_FILE] = (
 
189
                    stat.S_IFREG | 0o644, fileid_blob.id)
 
190
            else:
 
191
                self._blobs[self._mapping.BZR_FILE_IDS_FILE] = None
166
192
        self.new_inventory = None
167
193
 
168
194
    def update_basis(self, tree):
171
197
 
172
198
    def finish_inventory(self):
173
199
        # eliminate blobs that were removed
174
 
        self._blobs = {k: v for (k, v) in self._blobs.items()}
 
200
        self._blobs = {k: v for (k, v) in viewitems(
 
201
            self._blobs) if v is not None}
175
202
 
176
203
    def _iterblobs(self):
177
204
        return ((path, sha, mode) for (path, (mode, sha))
178
 
                in self._blobs.items())
 
205
                in viewitems(self._blobs))
179
206
 
180
207
    def commit(self, message):
181
208
        self._validate_unicode_text(message, 'commit message')
235
262
        return self.repository.revision_tree(self._new_revision_id)
236
263
 
237
264
    def get_basis_delta(self):
 
265
        # TODO(jelmer): remove this logic when lp:~jelmer/brz/remaining lands
 
266
        for (oldpath, newpath, file_id, entry) in self._inv_delta:
 
267
            if entry is not None:
 
268
                entry.revision = self._new_revision_id
238
269
        return self._inv_delta
239
270
 
240
271
    def update_basis_by_delta(self, revid, delta):