/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: Breezy landing bot
  • Author(s): Martin
  • Date: 2018-11-20 23:50:57 UTC
  • mfrom: (7195.5.2 lint_W1_W2_W3)
  • Revision ID: breezy.the.bot@gmail.com-20181120235057-rcl0xb6zs0gspupb
Fix remaining whitespace lint in codebase

Merged from https://code.launchpad.net/~gz/brz/lint_W1_W2_W3/+merge/358968

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
 
26
27
from .. import (
27
 
    bugtracker,
28
28
    config as _mod_config,
29
29
    gpg,
30
30
    osutils,
38
38
from ..repository import (
39
39
    CommitBuilder,
40
40
    )
 
41
from ..sixish import (
 
42
    viewitems,
 
43
    )
41
44
 
42
45
from dulwich.objects import (
43
46
    Blob,
44
47
    Commit,
45
48
    )
 
49
from dulwich.index import read_submodule_head
46
50
 
47
51
 
48
52
from .mapping import (
49
 
    encode_git_path,
50
53
    object_mode,
51
54
    fix_person_identifier,
52
55
    )
53
56
from .tree import entry_factory
54
57
 
55
58
 
 
59
class SettingCustomFileIdsUnsupported(UnsupportedOperation):
 
60
 
 
61
    _fmt = ("Unable to store addition of file with custom file ids: "
 
62
            "%(file_ids)r")
 
63
 
 
64
    def __init__(self, file_ids):
 
65
        BzrError.__init__(self)
 
66
        self.file_ids = file_ids
 
67
 
 
68
 
56
69
class GitCommitBuilder(CommitBuilder):
57
70
    """Commit builder for Git repositories."""
58
71
 
65
78
        self.store = self.repository._git.object_store
66
79
        self._blobs = {}
67
80
        self._inv_delta = []
68
 
        self._deleted_paths = set()
69
81
        self._any_changes = False
 
82
        self._override_fileids = {}
70
83
        self._mapping = self.repository.get_mapping()
71
84
 
72
85
    def any_changes(self):
74
87
 
75
88
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
76
89
        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",):
 
90
        for (file_id, path, changed_content, versioned, parent, name, kind,
 
91
             executable) in iter_changes:
 
92
            if kind[1] in ("directory",):
92
93
                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
 
94
                    (path[0], path[1], file_id, entry_factory[kind[1]](
 
95
                        file_id, name[1], parent[1])))
 
96
                if kind[0] in ("file", "symlink"):
 
97
                    self._blobs[path[0].encode("utf-8")] = None
98
98
                    self._any_changes = True
99
 
                if change.path[1] == "":
 
99
                if path[1] == "":
100
100
                    seen_root = True
101
101
                continue
102
102
            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]))
 
103
            if path[1] is None:
 
104
                self._inv_delta.append((path[0], path[1], file_id, None))
 
105
                self._blobs[path[0].encode("utf-8")] = None
106
106
                continue
107
107
            try:
108
 
                entry_kls = entry_factory[change.kind[1]]
 
108
                entry_kls = entry_factory[kind[1]]
109
109
            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]
 
110
                raise KeyError("unknown kind %s" % kind[1])
 
111
            entry = entry_kls(file_id, name[1], parent[1])
 
112
            if kind[1] == "file":
 
113
                entry.executable = executable[1]
114
114
                blob = Blob()
115
 
                f, st = workingtree.get_file_with_stat(change.path[1])
 
115
                f, st = workingtree.get_file_with_stat(path[1])
116
116
                try:
117
117
                    blob.data = f.read()
118
118
                finally:
119
119
                    f.close()
 
120
                entry.text_size = len(blob.data)
 
121
                entry.text_sha1 = osutils.sha_string(blob.data)
 
122
                self.store.add_object(blob)
120
123
                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])
 
124
            elif kind[1] == "symlink":
 
125
                symlink_target = workingtree.get_symlink_target(path[1])
129
126
                blob = Blob()
130
 
                blob.data = encode_git_path(symlink_target)
 
127
                blob.data = symlink_target.encode("utf-8")
131
128
                self.store.add_object(blob)
132
129
                sha = blob.id
133
130
                entry.symlink_target = symlink_target
134
131
                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])
 
132
            elif kind[1] == "tree-reference":
 
133
                sha = read_submodule_head(workingtree.abspath(path[1]))
 
134
                reference_revision = workingtree.get_reference_revision(path[1])
138
135
                entry.reference_revision = reference_revision
139
136
                st = None
140
137
            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)
 
138
                raise AssertionError("Unknown kind %r" % kind[1])
 
139
            mode = object_mode(kind[1], executable[1])
 
140
            self._inv_delta.append((path[0], path[1], file_id, entry))
 
141
            encoded_new_path = path[1].encode("utf-8")
 
142
            self._blobs[encoded_new_path] = (mode, sha)
147
143
            if st is not None:
148
 
                yield change.path[1], (entry.git_sha1, st)
 
144
                yield file_id, path[1], (entry.text_sha1, st)
 
145
            if self._mapping.generate_file_id(encoded_new_path) != file_id:
 
146
                self._override_fileids[encoded_new_path] = file_id
 
147
            else:
 
148
                self._override_fileids[encoded_new_path] = None
149
149
        if not seen_root and len(self.parents) == 0:
150
150
            raise RootMissing()
151
151
        if getattr(workingtree, "basis_tree", False):
160
160
        for entry in basis_tree._iter_tree_contents(include_trees=False):
161
161
            if entry.path in self._blobs:
162
162
                continue
163
 
            if entry.path in self._deleted_paths:
164
 
                continue
165
163
            self._blobs[entry.path] = (entry.mode, entry.sha)
 
164
        if not self._lossy:
 
165
            try:
 
166
                fileid_map = dict(basis_tree._fileid_map.file_ids)
 
167
            except AttributeError:
 
168
                fileid_map = {}
 
169
            for path, file_id in viewitems(self._override_fileids):
 
170
                if not isinstance(path, bytes):
 
171
                    raise TypeError(path)
 
172
                if file_id is None:
 
173
                    if path in fileid_map:
 
174
                        del fileid_map[path]
 
175
                else:
 
176
                    if not isinstance(file_id, bytes):
 
177
                        raise TypeError(file_id)
 
178
                    fileid_map[path] = file_id
 
179
            if fileid_map:
 
180
                fileid_blob = self._mapping.export_fileid_map(fileid_map)
 
181
            else:
 
182
                fileid_blob = None
 
183
            if fileid_blob is not None:
 
184
                if self._mapping.BZR_FILE_IDS_FILE is None:
 
185
                    raise SettingCustomFileIdsUnsupported(fileid_map)
 
186
                self.store.add_object(fileid_blob)
 
187
                self._blobs[self._mapping.BZR_FILE_IDS_FILE] = (
 
188
                    stat.S_IFREG | 0o644, fileid_blob.id)
 
189
            else:
 
190
                self._blobs[self._mapping.BZR_FILE_IDS_FILE] = None
166
191
        self.new_inventory = None
167
192
 
168
193
    def update_basis(self, tree):
171
196
 
172
197
    def finish_inventory(self):
173
198
        # eliminate blobs that were removed
174
 
        self._blobs = {k: v for (k, v) in self._blobs.items()}
 
199
        self._blobs = {k: v for (k, v) in viewitems(
 
200
            self._blobs) if v is not None}
175
201
 
176
202
    def _iterblobs(self):
177
203
        return ((path, sha, mode) for (path, (mode, sha))
178
 
                in self._blobs.items())
 
204
                in viewitems(self._blobs))
179
205
 
180
206
    def commit(self, message):
181
207
        self._validate_unicode_text(message, 'commit message')
201
227
                else:
202
228
                    author = authors[0]
203
229
        c.author = fix_person_identifier(author.encode(encoding))
204
 
        bugstext = self._revprops.pop('bugs', None)
205
 
        if bugstext is not None:
206
 
            message += "\n"
207
 
            for url, status in bugtracker.decode_bug_urls(bugstext):
208
 
                if status == bugtracker.FIXED:
209
 
                    message += "Fixes: %s\n" % url
210
 
                elif status == bugtracker.RELATED:
211
 
                    message += "Bug: %s\n" % url
212
 
                else:
213
 
                    raise bugtracker.InvalidBugStatus(status)
214
230
        if self._revprops:
215
231
            raise NotImplementedError(self._revprops)
216
232
        c.commit_time = int(self._timestamp)
235
251
        return self.repository.revision_tree(self._new_revision_id)
236
252
 
237
253
    def get_basis_delta(self):
 
254
        # TODO(jelmer): remove this logic when lp:~jelmer/brz/remaining lands
 
255
        for (oldpath, newpath, file_id, entry) in self._inv_delta:
 
256
            if entry is not None:
 
257
                entry.revision = self._new_revision_id
238
258
        return self._inv_delta
239
259
 
240
260
    def update_basis_by_delta(self, revid, delta):