/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

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
 
 
22
20
from dulwich.index import (
23
21
    commit_tree,
 
22
    read_submodule_head,
24
23
    )
25
24
import stat
26
25
 
27
26
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
 
    )
44
41
 
45
42
from dulwich.objects import (
46
43
    Blob,
47
44
    Commit,
48
45
    )
49
 
from dulwich.index import read_submodule_head
50
46
 
51
47
 
52
48
from .mapping import (
 
49
    encode_git_path,
53
50
    object_mode,
54
51
    fix_person_identifier,
55
52
    )
56
53
from .tree import entry_factory
57
54
 
58
55
 
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
 
 
69
56
class GitCommitBuilder(CommitBuilder):
70
57
    """Commit builder for Git repositories."""
71
58
 
78
65
        self.store = self.repository._git.object_store
79
66
        self._blobs = {}
80
67
        self._inv_delta = []
 
68
        self._deleted_paths = set()
81
69
        self._any_changes = False
82
 
        self._override_fileids = {}
83
70
        self._mapping = self.repository.get_mapping()
84
71
 
85
72
    def any_changes(self):
87
74
 
88
75
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
89
76
        seen_root = False
90
 
        for (file_id, path, changed_content, versioned, parent, name, kind,
91
 
             executable) in iter_changes:
92
 
            if kind[1] in ("directory",):
 
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",):
93
92
                self._inv_delta.append(
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
 
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
98
98
                    self._any_changes = True
99
 
                if path[1] == "":
 
99
                if change.path[1] == "":
100
100
                    seen_root = True
101
101
                continue
102
102
            self._any_changes = True
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
 
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]))
106
106
                continue
107
107
            try:
108
 
                entry_kls = entry_factory[kind[1]]
 
108
                entry_kls = entry_factory[change.kind[1]]
109
109
            except KeyError:
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]
 
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]
114
114
                blob = Blob()
115
 
                f, st = workingtree.get_file_with_stat(path[1])
 
115
                f, st = workingtree.get_file_with_stat(change.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)
 
120
                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
122
126
                self.store.add_object(blob)
123
 
                sha = blob.id
124
 
            elif kind[1] == "symlink":
125
 
                symlink_target = workingtree.get_symlink_target(path[1])
 
127
            elif change.kind[1] == "symlink":
 
128
                symlink_target = workingtree.get_symlink_target(change.path[1])
126
129
                blob = Blob()
127
 
                blob.data = symlink_target.encode("utf-8")
 
130
                blob.data = encode_git_path(symlink_target)
128
131
                self.store.add_object(blob)
129
132
                sha = blob.id
130
133
                entry.symlink_target = symlink_target
131
134
                st = None
132
 
            elif kind[1] == "tree-reference":
133
 
                sha = read_submodule_head(workingtree.abspath(path[1]))
134
 
                reference_revision = workingtree.get_reference_revision(path[1])
 
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])
135
138
                entry.reference_revision = reference_revision
136
139
                st = None
137
140
            else:
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)
 
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)
143
147
            if st is not None:
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
 
148
                yield change.path[1], (entry.git_sha1, st)
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
163
165
            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
191
166
        self.new_inventory = None
192
167
 
193
168
    def update_basis(self, tree):
196
171
 
197
172
    def finish_inventory(self):
198
173
        # eliminate blobs that were removed
199
 
        self._blobs = {k: v for (k, v) in viewitems(
200
 
            self._blobs) if v is not None}
 
174
        self._blobs = {k: v for (k, v) in self._blobs.items()}
201
175
 
202
176
    def _iterblobs(self):
203
177
        return ((path, sha, mode) for (path, (mode, sha))
204
 
                in viewitems(self._blobs))
 
178
                in self._blobs.items())
205
179
 
206
180
    def commit(self, message):
207
181
        self._validate_unicode_text(message, 'commit message')
227
201
                else:
228
202
                    author = authors[0]
229
203
        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)
230
214
        if self._revprops:
231
215
            raise NotImplementedError(self._revprops)
232
216
        c.commit_time = int(self._timestamp)
251
235
        return self.repository.revision_tree(self._new_revision_id)
252
236
 
253
237
    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
258
238
        return self._inv_delta
259
239
 
260
240
    def update_basis_by_delta(self, revid, delta):