/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2007 Canonical Ltd
0.252.32 by Jelmer Vernooij
update copyright
2
# Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
3
# Copyright (C) 2008 John Carr
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
"""Converters, etc for going between Bazaar and Git ids."""
20
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
21
import base64
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
22
import stat
23
0.200.292 by Jelmer Vernooij
Fix formatting.
24
from bzrlib import (
0.200.926 by Jelmer Vernooij
Fix formatting, drop support for Bazaar < 2.0.
25
    bencode,
0.200.292 by Jelmer Vernooij
Fix formatting.
26
    errors,
27
    foreign,
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
28
    trace,
0.200.292 by Jelmer Vernooij
Fix formatting.
29
    )
30
from bzrlib.inventory import (
31
    ROOT_ID,
32
    )
0.200.152 by Jelmer Vernooij
Fix syntax errors.
33
from bzrlib.foreign import (
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
34
    ForeignVcs,
35
    VcsMappingRegistry,
0.200.292 by Jelmer Vernooij
Fix formatting.
36
    ForeignRevision,
37
    )
0.200.701 by Jelmer Vernooij
Fix check in git repos.
38
from bzrlib.revision import (
39
    NULL_REVISION,
40
    )
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
41
from bzrlib.plugins.git.hg import (
42
    format_hg_metadata,
43
    extract_hg_metadata,
44
    )
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
45
from bzrlib.plugins.git.roundtrip import (
46
    extract_bzr_metadata,
0.252.4 by Jelmer Vernooij
More work on roundtripping.
47
    inject_bzr_metadata,
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
48
    CommitSupplement,
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
49
    deserialize_fileid_map,
50
    serialize_fileid_map,
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
51
    )
0.200.309 by Jelmer Vernooij
Add XML escaping to work around serialization bug in bzr.
52
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
53
DEFAULT_FILE_MODE = stat.S_IFREG | 0644
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
54
0.206.1 by Jelmer Vernooij
Use foreign utility functions.
55
0.200.150 by Jelmer Vernooij
Abstract away file id generation.
56
def escape_file_id(file_id):
0.200.1419 by Jelmer Vernooij
Escape/unescape ^L characters.
57
    return file_id.replace('_', '__').replace(' ', '_s').replace('\x0c', '_c')
0.200.150 by Jelmer Vernooij
Abstract away file id generation.
58
59
60
def unescape_file_id(file_id):
0.200.390 by Jelmer Vernooij
Fix file id unescape function when there are other underscores in the file id.
61
    ret = []
62
    i = 0
63
    while i < len(file_id):
64
        if file_id[i] != '_':
65
            ret.append(file_id[i])
66
        else:
67
            if file_id[i+1] == '_':
68
                ret.append("_")
69
            elif file_id[i+1] == 's':
70
                ret.append(" ")
0.200.1419 by Jelmer Vernooij
Escape/unescape ^L characters.
71
            elif file_id[i+1] == 'c':
72
                ret.append("\x0c")
0.200.390 by Jelmer Vernooij
Fix file id unescape function when there are other underscores in the file id.
73
            else:
0.200.826 by Jelmer Vernooij
Fix some long lines.
74
                raise AssertionError("unknown escape character %s" %
75
                    file_id[i+1])
0.200.390 by Jelmer Vernooij
Fix file id unescape function when there are other underscores in the file id.
76
            i += 1
77
        i += 1
78
    return "".join(ret)
0.200.150 by Jelmer Vernooij
Abstract away file id generation.
79
80
0.200.376 by Jelmer Vernooij
Make sure author and committer names pushed to git contain < and >, otherwise the git parser barfs.
81
def fix_person_identifier(text):
82
    if "<" in text and ">" in text:
83
        return text
84
    return "%s <%s>" % (text, text)
85
86
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
87
def warn_escaped(commit, num_escaped):
88
    trace.warning("Escaped %d XML-invalid characters in %s. Will be unable "
89
                  "to regenerate the SHA map.", num_escaped, commit)
90
91
92
def warn_unusual_mode(commit, path, mode):
0.200.826 by Jelmer Vernooij
Fix some long lines.
93
    trace.mutter("Unusual file mode %o for %s in %s. Storing as revision "
94
                 "property. ", mode, path, commit)
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
95
96
0.206.1 by Jelmer Vernooij
Use foreign utility functions.
97
class BzrGitMapping(foreign.VcsMapping):
0.200.97 by Jelmer Vernooij
use mapping object.
98
    """Class that maps between Git and Bazaar semantics."""
99
    experimental = False
100
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
101
    BZR_FILE_IDS_FILE = None
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
102
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
103
    BZR_DUMMY_FILE = None
0.252.26 by Jelmer Vernooij
Add is_control_file method to BzrGitMapping.
104
0.200.1328 by Jelmer Vernooij
More test fixes.
105
    def is_special_file(self, filename):
106
        return (filename in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE))
107
0.200.198 by Jelmer Vernooij
Cope with move of show_foreign_revid.
108
    def __init__(self):
0.200.1263 by Jelmer Vernooij
Fix foreign_vcs_git.
109
        super(BzrGitMapping, self).__init__(foreign_vcs_git)
0.200.198 by Jelmer Vernooij
Cope with move of show_foreign_revid.
110
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
111
    def __eq__(self, other):
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
112
        return (type(self) == type(other) and
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
113
                self.revid_prefix == other.revid_prefix)
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
114
115
    @classmethod
116
    def revision_id_foreign_to_bzr(cls, git_rev_id):
0.200.97 by Jelmer Vernooij
use mapping object.
117
        """Convert a git revision id handle to a Bazaar revision id."""
0.200.891 by Jelmer Vernooij
Use ZERO_SHA constant where possible.
118
        from dulwich.protocol import ZERO_SHA
119
        if git_rev_id == ZERO_SHA:
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
120
            return NULL_REVISION
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
121
        return "%s:%s" % (cls.revid_prefix, git_rev_id)
0.200.97 by Jelmer Vernooij
use mapping object.
122
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
123
    @classmethod
124
    def revision_id_bzr_to_foreign(cls, bzr_rev_id):
0.200.97 by Jelmer Vernooij
use mapping object.
125
        """Convert a Bazaar revision id to a git revision id handle."""
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
126
        if not bzr_rev_id.startswith("%s:" % cls.revid_prefix):
127
            raise errors.InvalidRevisionId(bzr_rev_id, cls)
128
        return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
0.200.97 by Jelmer Vernooij
use mapping object.
129
0.200.150 by Jelmer Vernooij
Abstract away file id generation.
130
    def generate_file_id(self, path):
0.200.297 by Jelmer Vernooij
Cope with non-ascii characters in filenames (needs a test..).
131
        # Git paths are just bytestrings
132
        # We must just hope they are valid UTF-8..
0.200.157 by Jelmer Vernooij
Fix some bit of fetching.
133
        if path == "":
134
            return ROOT_ID
0.200.973 by Jelmer Vernooij
Add tests for generate_file_id.
135
        if type(path) is unicode:
136
            path = path.encode("utf-8")
0.200.297 by Jelmer Vernooij
Cope with non-ascii characters in filenames (needs a test..).
137
        return escape_file_id(path)
0.200.150 by Jelmer Vernooij
Abstract away file id generation.
138
0.252.26 by Jelmer Vernooij
Add is_control_file method to BzrGitMapping.
139
    def is_control_file(self, path):
140
        return path in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE)
141
0.230.2 by Jelmer Vernooij
Fix versionedfiles.
142
    def parse_file_id(self, file_id):
143
        if file_id == ROOT_ID:
144
            return ""
145
        return unescape_file_id(file_id)
146
0.252.31 by Jelmer Vernooij
Properly escape revids when using them in ref names.
147
    def revid_as_refname(self, revid):
148
        import urllib
149
        return "refs/bzr/%s" % urllib.quote(revid)
150
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
151
    def import_unusual_file_modes(self, rev, unusual_file_modes):
152
        if unusual_file_modes:
0.200.878 by Jelmer Vernooij
Fix determining of unusual file modes.
153
            ret = [(path, unusual_file_modes[path])
154
                   for path in sorted(unusual_file_modes.keys())]
0.200.549 by Jelmer Vernooij
Fix storing of unusual file modes.
155
            rev.properties['file-modes'] = bencode.bencode(ret)
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
156
0.200.547 by Jelmer Vernooij
Support getting unusual file modes out of revision properties.
157
    def export_unusual_file_modes(self, rev):
158
        try:
0.200.894 by Jelmer Vernooij
Simplify formatting a bit.
159
            file_modes = rev.properties['file-modes']
0.200.547 by Jelmer Vernooij
Support getting unusual file modes out of revision properties.
160
        except KeyError:
161
            return {}
0.200.894 by Jelmer Vernooij
Simplify formatting a bit.
162
        else:
163
            return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(file_modes.encode("utf-8"))])
0.200.547 by Jelmer Vernooij
Support getting unusual file modes out of revision properties.
164
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
165
    def _generate_git_svn_metadata(self, rev, encoding):
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
166
        try:
0.200.894 by Jelmer Vernooij
Simplify formatting a bit.
167
            git_svn_id = rev.properties["git-svn-id"]
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
168
        except KeyError:
169
            return ""
0.200.894 by Jelmer Vernooij
Simplify formatting a bit.
170
        else:
171
            return "\ngit-svn-id: %s\n" % git_svn_id.encode(encoding)
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
172
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
173
    def _generate_hg_message_tail(self, rev):
174
        extra = {}
175
        renames = []
0.200.639 by Jelmer Vernooij
Support renames in hg-git messages as well.
176
        branch = 'default'
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
177
        for name in rev.properties:
178
            if name == 'hg:extra:branch':
179
                branch = rev.properties['hg:extra:branch']
180
            elif name.startswith('hg:extra'):
0.200.826 by Jelmer Vernooij
Fix some long lines.
181
                extra[name[len('hg:extra:'):]] = base64.b64decode(
182
                    rev.properties[name])
0.200.639 by Jelmer Vernooij
Support renames in hg-git messages as well.
183
            elif name == 'hg:renames':
0.200.826 by Jelmer Vernooij
Fix some long lines.
184
                renames = bencode.bdecode(base64.b64decode(
185
                    rev.properties['hg:renames']))
0.200.639 by Jelmer Vernooij
Support renames in hg-git messages as well.
186
            # TODO: Export other properties as 'bzr:' extras?
0.200.660 by Jelmer Vernooij
Fix encoding issues.
187
        ret = format_hg_metadata(renames, branch, extra)
188
        assert isinstance(ret, str)
189
        return ret
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
190
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
191
    def _extract_git_svn_metadata(self, rev, message):
192
        lines = message.split("\n")
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
193
        if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
194
            return message
0.200.652 by Jelmer Vernooij
Split out git-svn-id parser as separate function, implement ForeignGit.serialize_foreign_revid.
195
        git_svn_id = lines[-2].split(": ", 1)[1]
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
196
        rev.properties['git-svn-id'] = git_svn_id
0.200.652 by Jelmer Vernooij
Split out git-svn-id parser as separate function, implement ForeignGit.serialize_foreign_revid.
197
        (url, rev, uuid) = parse_git_svn_id(git_svn_id)
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
198
        # FIXME: Convert this to converted-from property somehow..
0.200.660 by Jelmer Vernooij
Fix encoding issues.
199
        ret = "\n".join(lines[:-2])
200
        assert isinstance(ret, str)
201
        return ret
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
202
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
203
    def _extract_hg_metadata(self, rev, message):
204
        (message, renames, branch, extra) = extract_hg_metadata(message)
205
        if branch is not None:
206
            rev.properties['hg:extra:branch'] = branch
207
        for name, value in extra.iteritems():
208
            rev.properties['hg:extra:' + name] = base64.b64encode(value)
0.200.639 by Jelmer Vernooij
Support renames in hg-git messages as well.
209
        if renames:
0.200.826 by Jelmer Vernooij
Fix some long lines.
210
            rev.properties['hg:renames'] = base64.b64encode(bencode.bencode(
211
                [(new, old) for (old, new) in renames.iteritems()]))
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
212
        return message
213
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
214
    def _extract_bzr_metadata(self, rev, message):
215
        (message, metadata) = extract_bzr_metadata(message)
0.252.4 by Jelmer Vernooij
More work on roundtripping.
216
        return message, metadata
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
217
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
218
    def _decode_commit_message(self, rev, message, encoding):
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
219
        return message.decode(encoding), CommitSupplement()
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
220
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
221
    def _encode_commit_message(self, rev, message, encoding):
222
        return message.encode(encoding)
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
223
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
224
    def export_fileid_map(self, fileid_map):
225
        """Export a file id map to a fileid map.
226
227
        :param fileid_map: File id map, mapping paths to file ids
228
        :return: A Git blob object
229
        """
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
230
        from dulwich.objects import Blob
231
        b = Blob()
232
        b.set_raw_chunks(serialize_fileid_map(fileid_map))
233
        return b
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
234
0.200.1023 by Jelmer Vernooij
Set and verify testament.
235
    def export_commit(self, rev, tree_sha, parent_lookup, roundtrip,
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
236
                      verifiers):
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
237
        """Turn a Bazaar revision in to a Git commit
238
239
        :param tree_sha: Tree sha for the commit
0.200.826 by Jelmer Vernooij
Fix some long lines.
240
        :param parent_lookup: Function for looking up the GIT sha equiv of a
241
            bzr revision
0.200.1023 by Jelmer Vernooij
Set and verify testament.
242
        :param roundtrip: Whether to store roundtripping information.
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
243
        :param verifiers: Verifiers info
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
244
        :return dulwich.objects.Commit represent the revision:
245
        """
246
        from dulwich.objects import Commit
247
        commit = Commit()
248
        commit.tree = tree_sha
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
249
        if roundtrip:
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
250
            metadata = CommitSupplement()
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
251
            metadata.verifiers = verifiers
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
252
        else:
253
            metadata = None
0.252.43 by Jelmer Vernooij
Some refactoring, support proper file ids in revision deltas.
254
        parents = []
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
255
        for p in rev.parent_ids:
0.200.705 by Jelmer Vernooij
Cope with imports.
256
            try:
257
                git_p = parent_lookup(p)
258
            except KeyError:
259
                git_p = None
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
260
                if metadata is not None:
261
                    metadata.explicit_parent_ids = rev.parent_ids
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
262
            if git_p is not None:
263
                assert len(git_p) == 40, "unexpected length for %r" % git_p
0.252.43 by Jelmer Vernooij
Some refactoring, support proper file ids in revision deltas.
264
                parents.append(git_p)
265
        commit.parents = parents
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
266
        try:
267
            encoding = rev.properties['git-explicit-encoding']
268
        except KeyError:
269
            encoding = rev.properties.get('git-implicit-encoding', 'utf-8')
270
        commit.encoding = rev.properties.get('git-explicit-encoding')
271
        commit.committer = fix_person_identifier(rev.committer.encode(
272
            encoding))
273
        commit.author = fix_person_identifier(
274
            rev.get_apparent_authors()[0].encode(encoding))
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
275
        commit.commit_time = long(rev.timestamp)
276
        if 'author-timestamp' in rev.properties:
277
            commit.author_time = long(rev.properties['author-timestamp'])
278
        else:
279
            commit.author_time = commit.commit_time
0.200.884 by Jelmer Vernooij
Cope with -0000 as timezone in Git commits.
280
        commit._commit_timezone_neg_utc = "commit-timezone-neg-utc" in rev.properties
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
281
        commit.commit_timezone = rev.timezone
0.200.884 by Jelmer Vernooij
Cope with -0000 as timezone in Git commits.
282
        commit._author_timezone_neg_utc = "author-timezone-neg-utc" in rev.properties
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
283
        if 'author-timezone' in rev.properties:
284
            commit.author_timezone = int(rev.properties['author-timezone'])
285
        else:
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
286
            commit.author_timezone = commit.commit_timezone
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
287
        commit.message = self._encode_commit_message(rev, rev.message, 
288
            encoding)
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
289
        assert type(commit.message) == str
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
290
        if metadata is not None:
0.252.4 by Jelmer Vernooij
More work on roundtripping.
291
            try:
292
                mapping_registry.parse_revision_id(rev.revision_id)
293
            except errors.InvalidRevisionId:
294
                metadata.revision_id = rev.revision_id
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
295
            mapping_properties = set(
296
                ['author', 'author-timezone', 'author-timezone-neg-utc',
297
                 'commit-timezone-neg-utc', 'git-implicit-encoding',
0.252.15 by Jelmer Vernooij
Add file-modes to list of mapping properties.
298
                 'git-explicit-encoding', 'author-timestamp', 'file-modes'])
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
299
            for k, v in rev.properties.iteritems():
300
                if not k in mapping_properties:
301
                    metadata.properties[k] = v
0.200.912 by Jelmer Vernooij
Merge roundtrip support.
302
        if self.roundtripping:
303
            commit.message = inject_bzr_metadata(commit.message, metadata, 
304
                                                 encoding)
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
305
        assert type(commit.message) == str
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
306
        return commit
307
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
308
    def import_fileid_map(self, blob):
309
        """Convert a git file id map blob.
310
311
        :param blob: Git blob object with fileid map
312
        :return: Dictionary mapping paths to file ids
313
        """
0.252.35 by Jelmer Vernooij
Ignore control files in inventories.
314
        return deserialize_fileid_map(blob.data)
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
315
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
316
    def import_commit(self, commit, lookup_parent_revid):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
317
        """Convert a git commit to a bzr revision.
318
0.200.1021 by Jelmer Vernooij
Put testament sha1 in revisions.
319
        :return: a `bzrlib.revision.Revision` object, foreign revid and a
320
            testament sha1
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
321
        """
322
        if commit is None:
323
            raise AssertionError("Commit object can't be None")
0.200.826 by Jelmer Vernooij
Fix some long lines.
324
        rev = ForeignRevision(commit.id, self,
325
                self.revision_id_foreign_to_bzr(commit.id))
0.252.4 by Jelmer Vernooij
More work on roundtripping.
326
        rev.git_metadata = None
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
327
        def decode_using_encoding(rev, commit, encoding):
328
            rev.committer = str(commit.committer).decode(encoding)
329
            if commit.committer != commit.author:
330
                rev.properties['author'] = str(commit.author).decode(encoding)
0.252.4 by Jelmer Vernooij
More work on roundtripping.
331
            rev.message, rev.git_metadata = self._decode_commit_message(
332
                rev, commit.message, encoding)
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
333
        if commit.encoding is not None:
334
            rev.properties['git-explicit-encoding'] = commit.encoding
335
            decode_using_encoding(rev, commit, commit.encoding)
336
        else:
337
            for encoding in ('utf-8', 'latin1'):
338
                try:
339
                    decode_using_encoding(rev, commit, encoding)
340
                except UnicodeDecodeError:
341
                    pass
342
                else:
343
                    if encoding != 'utf-8':
344
                        rev.properties['git-implicit-encoding'] = encoding
345
                    break
0.200.350 by Jelmer Vernooij
Support author_time
346
        if commit.commit_time != commit.author_time:
347
            rev.properties['author-timestamp'] = str(commit.author_time)
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
348
        if commit.commit_timezone != commit.author_timezone:
0.200.826 by Jelmer Vernooij
Fix some long lines.
349
            rev.properties['author-timezone'] = "%d" % commit.author_timezone
0.200.884 by Jelmer Vernooij
Cope with -0000 as timezone in Git commits.
350
        if commit._author_timezone_neg_utc:
351
            rev.properties['author-timezone-neg-utc'] = ""
352
        if commit._commit_timezone_neg_utc:
353
            rev.properties['commit-timezone-neg-utc'] = ""
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
354
        rev.timestamp = commit.commit_time
0.200.440 by Jelmer Vernooij
Remove silly mapping of timezones; dulwich uses offsets now as well.
355
        rev.timezone = commit.commit_timezone
0.261.5 by Jelmer Vernooij
Fix looking up of parents during fetch.
356
        rev.parent_ids = None
0.252.4 by Jelmer Vernooij
More work on roundtripping.
357
        if rev.git_metadata is not None:
0.252.6 by Jelmer Vernooij
Roundtripping support for revision ids works.
358
            md = rev.git_metadata
0.200.1021 by Jelmer Vernooij
Put testament sha1 in revisions.
359
            roundtrip_revid = md.revision_id
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
360
            if md.explicit_parent_ids:
361
                rev.parent_ids = md.explicit_parent_ids
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
362
            rev.properties.update(md.properties)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
363
            verifiers = md.verifiers
0.200.1021 by Jelmer Vernooij
Put testament sha1 in revisions.
364
        else:
365
            roundtrip_revid = None
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
366
            verifiers = {}
0.261.5 by Jelmer Vernooij
Fix looking up of parents during fetch.
367
        if rev.parent_ids is None:
368
            rev.parent_ids = tuple([lookup_parent_revid(p) for p in commit.parents])
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
369
        return rev, roundtrip_revid, verifiers
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
370
0.252.43 by Jelmer Vernooij
Some refactoring, support proper file ids in revision deltas.
371
    def get_fileid_map(self, lookup_object, tree_sha):
372
        """Obtain a fileid map for a particular tree.
373
374
        :param lookup_object: Function for looking up an object
375
        :param tree_sha: SHA of the root tree
376
        :return: GitFileIdMap instance
377
        """
378
        try:
379
            file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
380
        except KeyError:
381
            file_ids = {}
382
        else:
383
            file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
384
        return GitFileIdMap(file_ids, self)
385
0.200.97 by Jelmer Vernooij
use mapping object.
386
0.200.190 by Jelmer Vernooij
Bless current mapping as v1.
387
class BzrGitMappingv1(BzrGitMapping):
388
    revid_prefix = 'git-v1'
389
    experimental = False
390
0.200.393 by Jelmer Vernooij
Provide __str__ implementation for mapping, fix docstring for ForeignGit.
391
    def __str__(self):
392
        return self.revid_prefix
393
0.200.190 by Jelmer Vernooij
Bless current mapping as v1.
394
395
class BzrGitMappingExperimental(BzrGitMappingv1):
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
396
    revid_prefix = 'git-experimental'
397
    experimental = True
0.200.912 by Jelmer Vernooij
Merge roundtrip support.
398
    roundtripping = True
0.200.97 by Jelmer Vernooij
use mapping object.
399
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
400
    BZR_FILE_IDS_FILE = '.bzrfileids'
401
402
    BZR_DUMMY_FILE = '.bzrdummy'
403
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
404
    def _decode_commit_message(self, rev, message, encoding):
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
405
        message = self._extract_hg_metadata(rev, message)
0.200.643 by Jelmer Vernooij
Attempt to parse git-svn-id metadata.
406
        message = self._extract_git_svn_metadata(rev, message)
0.252.4 by Jelmer Vernooij
More work on roundtripping.
407
        message, metadata = self._extract_bzr_metadata(rev, message)
408
        return message.decode(encoding), metadata
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
409
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
410
    def _encode_commit_message(self, rev, message, encoding):
411
        ret = message.encode(encoding)
0.200.638 by Jelmer Vernooij
Abstract support for hg-git metadata.
412
        ret += self._generate_hg_message_tail(rev)
0.200.727 by Jelmer Vernooij
Cope with different encodings better, rather than just stripping out
413
        ret += self._generate_git_svn_metadata(rev, encoding)
0.242.1 by Jelmer Vernooij
Add support for parsing hg-git metadata in the experimental mappings.
414
        return ret
415
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
416
    def import_commit(self, commit, lookup_parent_revid):
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
417
        rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
0.200.642 by Jelmer Vernooij
In experimental mappings, set 'converted_revision' property.
418
        rev.properties['converted_revision'] = "git %s\n" % commit.id
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
419
        return rev, roundtrip_revid, verifiers
0.200.642 by Jelmer Vernooij
In experimental mappings, set 'converted_revision' property.
420
0.200.97 by Jelmer Vernooij
use mapping object.
421
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
422
class GitMappingRegistry(VcsMappingRegistry):
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
423
    """Registry with available git mappings."""
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
424
425
    def revision_id_bzr_to_foreign(self, bzr_revid):
0.200.701 by Jelmer Vernooij
Fix check in git repos.
426
        if bzr_revid == NULL_REVISION:
0.200.892 by Jelmer Vernooij
Lazy import ZERO_SHA.
427
            from dulwich.protocol import ZERO_SHA
0.200.891 by Jelmer Vernooij
Use ZERO_SHA constant where possible.
428
            return ZERO_SHA, None
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
429
        if not bzr_revid.startswith("git-"):
430
            raise errors.InvalidRevisionId(bzr_revid, None)
431
        (mapping_version, git_sha) = bzr_revid.split(":", 1)
432
        mapping = self.get(mapping_version)
433
        return mapping.revision_id_bzr_to_foreign(bzr_revid)
434
435
    parse_revision_id = revision_id_bzr_to_foreign
436
437
438
mapping_registry = GitMappingRegistry()
439
mapping_registry.register_lazy('git-v1', "bzrlib.plugins.git.mapping",
0.200.826 by Jelmer Vernooij
Fix some long lines.
440
    "BzrGitMappingv1")
441
mapping_registry.register_lazy('git-experimental',
442
    "bzrlib.plugins.git.mapping", "BzrGitMappingExperimental")
0.200.1416 by Jelmer Vernooij
Don't use experimental mapping by default.
443
#mapping_registry.set_default('git-experimental')
444
mapping_registry.set_default('git-v1')
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
445
446
447
class ForeignGit(ForeignVcs):
0.200.393 by Jelmer Vernooij
Provide __str__ implementation for mapping, fix docstring for ForeignGit.
448
    """The Git Stupid Content Tracker"""
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
449
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
450
    @property
451
    def branch_format(self):
452
        from bzrlib.plugins.git.branch import GitBranchFormat
453
        return GitBranchFormat()
454
0.200.657 by Jelmer Vernooij
Provide repository_format attribute, as required by newer foreign VCS tests in bzrlib.
455
    @property
456
    def repository_format(self):
457
        from bzrlib.plugins.git.repository import GitRepositoryFormat
458
        return GitRepositoryFormat()
459
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
460
    def __init__(self):
461
        super(ForeignGit, self).__init__(mapping_registry)
0.200.646 by Jelmer Vernooij
Store abbreviation in foreign branch.
462
        self.abbreviation = "git"
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
463
0.200.198 by Jelmer Vernooij
Cope with move of show_foreign_revid.
464
    @classmethod
0.200.652 by Jelmer Vernooij
Split out git-svn-id parser as separate function, implement ForeignGit.serialize_foreign_revid.
465
    def serialize_foreign_revid(self, foreign_revid):
466
        return foreign_revid
467
468
    @classmethod
0.200.198 by Jelmer Vernooij
Cope with move of show_foreign_revid.
469
    def show_foreign_revid(cls, foreign_revid):
470
        return { "git commit": foreign_revid }
471
472
0.200.1263 by Jelmer Vernooij
Fix foreign_vcs_git.
473
foreign_vcs_git = ForeignGit()
0.200.637 by Jelmer Vernooij
Allow single place for configuration of default mapping.
474
default_mapping = mapping_registry.get_default()()
0.200.212 by Jelmer Vernooij
Move conversion functions to mapping, use fetch_objects() from repository if present.
475
476
0.200.354 by Jelmer Vernooij
Support symlinks in conversion to git.
477
def symlink_to_blob(entry):
478
    from dulwich.objects import Blob
479
    blob = Blob()
0.200.795 by Jelmer Vernooij
simplify sha extraction for blobs, process multiple blobs at once.
480
    symlink_target = entry.symlink_target
481
    if type(symlink_target) == unicode:
482
        symlink_target = symlink_target.encode('utf-8')
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
483
    blob.data = symlink_target
0.200.354 by Jelmer Vernooij
Support symlinks in conversion to git.
484
    return blob
485
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
486
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
487
def mode_is_executable(mode):
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
488
    """Check if mode should be considered executable."""
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
489
    return bool(mode & 0111)
490
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
491
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
492
def mode_kind(mode):
0.200.546 by Jelmer Vernooij
Add more docstrings, support storing unusual file modes.
493
    """Determine the Bazaar inventory kind based on Unix file mode."""
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
494
    entry_kind = (mode & 0700000) / 0100000
495
    if entry_kind == 0:
496
        return 'directory'
497
    elif entry_kind == 1:
498
        file_kind = (mode & 070000) / 010000
499
        if file_kind == 0:
500
            return 'file'
501
        elif file_kind == 2:
502
            return 'symlink'
503
        elif file_kind == 6:
504
            return 'tree-reference'
505
        else:
506
            raise AssertionError(
507
                "Unknown file kind %d, perms=%o." % (file_kind, mode,))
508
    else:
509
        raise AssertionError(
510
            "Unknown kind, perms=%r." % (mode,))
511
0.200.354 by Jelmer Vernooij
Support symlinks in conversion to git.
512
0.238.6 by Jelmer Vernooij
Support sending git am-style patches with "bzr send --format=git".
513
def object_mode(kind, executable):
514
    if kind == 'directory':
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
515
        return stat.S_IFDIR
0.238.6 by Jelmer Vernooij
Support sending git am-style patches with "bzr send --format=git".
516
    elif kind == 'symlink':
0.245.1 by INADA Naoki
Handle executable mode for symlink.
517
        mode = stat.S_IFLNK
518
        if executable:
0.200.703 by Jelmer Vernooij
Merge support for executable symlinks.
519
            mode |= 0111
0.245.1 by INADA Naoki
Handle executable mode for symlink.
520
        return mode
0.238.6 by Jelmer Vernooij
Support sending git am-style patches with "bzr send --format=git".
521
    elif kind == 'file':
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
522
        mode = stat.S_IFREG | 0644
0.238.6 by Jelmer Vernooij
Support sending git am-style patches with "bzr send --format=git".
523
        if executable:
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
524
            mode |= 0111
525
        return mode
0.200.665 by Jelmer Vernooij
Add more checks for submodules.
526
    elif kind == 'tree-reference':
527
        from dulwich.objects import S_IFGITLINK
528
        return S_IFGITLINK
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
529
    else:
530
        raise AssertionError
531
532
0.238.6 by Jelmer Vernooij
Support sending git am-style patches with "bzr send --format=git".
533
def entry_mode(entry):
534
    """Determine the git file mode for an inventory entry."""
535
    return object_mode(entry.kind, entry.executable)
536
537
0.252.30 by Jelmer Vernooij
Support creating dummy files for empty directories.
538
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes, empty_file_name):
539
    """Create a Git Tree object from a Bazaar directory.
540
541
    :param entry: Inventory entry
542
    :param lookup_ie_sha1: Lookup the Git SHA1 for a inventory entry
543
    :param unusual_modes: Dictionary with unusual file modes by file ids
544
    :param empty_file_name: Name to use for dummy files in empty directories,
545
        None to ignore empty directories.
546
    """
547
    from dulwich.objects import Blob, Tree
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
548
    tree = Tree()
0.200.807 by Jelmer Vernooij
Fix test, remove unnecessary sort.
549
    for name, value in entry.children.iteritems():
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
550
        ie = entry.children[name]
0.200.549 by Jelmer Vernooij
Fix storing of unusual file modes.
551
        try:
552
            mode = unusual_modes[ie.file_id]
553
        except KeyError:
554
            mode = entry_mode(ie)
0.200.808 by Jelmer Vernooij
Avoid recalculating tree shas we already have.
555
        hexsha = lookup_ie_sha1(ie)
0.200.589 by Jelmer Vernooij
Fix handling of empty trees.
556
        if hexsha is not None:
0.200.1152 by Jelmer Vernooij
Require dulwich 0.7.1.
557
            tree.add(name.encode("utf-8"), mode, hexsha)
0.200.589 by Jelmer Vernooij
Fix handling of empty trees.
558
    if entry.parent_id is not None and len(tree) == 0:
559
        # Only the root can be an empty tree
0.252.30 by Jelmer Vernooij
Support creating dummy files for empty directories.
560
        if empty_file_name is not None:
0.200.1152 by Jelmer Vernooij
Require dulwich 0.7.1.
561
            tree.add(empty_file_name, stat.S_IFREG | 0644, Blob().id)
0.252.30 by Jelmer Vernooij
Support creating dummy files for empty directories.
562
        else:
563
            return None
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
564
    return tree
565
566
0.200.548 by Jelmer Vernooij
Extract unusual file modes from revision when reconstructing Trees.
567
def extract_unusual_modes(rev):
568
    try:
0.200.826 by Jelmer Vernooij
Fix some long lines.
569
        foreign_revid, mapping = mapping_registry.parse_revision_id(
570
            rev.revision_id)
0.200.548 by Jelmer Vernooij
Extract unusual file modes from revision when reconstructing Trees.
571
    except errors.InvalidRevisionId:
572
        return {}
573
    else:
574
        return mapping.export_unusual_file_modes(rev)
575
576
0.200.652 by Jelmer Vernooij
Split out git-svn-id parser as separate function, implement ForeignGit.serialize_foreign_revid.
577
def parse_git_svn_id(text):
578
    (head, uuid) = text.rsplit(" ", 1)
579
    (full_url, rev) = head.rsplit("@", 1)
0.200.653 by Jelmer Vernooij
Fix typo in git-svn-id parser, return revnum as integer.
580
    return (full_url, int(rev), uuid)
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
581
582
583
class GitFileIdMap(object):
584
585
    def __init__(self, file_ids, mapping):
586
        self.file_ids = file_ids
587
        self.paths = None
588
        self.mapping = mapping
589
0.200.1201 by Jelmer Vernooij
Implement _set_root_id.
590
    def set_file_id(self, path, file_id):
0.200.1209 by Jelmer Vernooij
Check for types in file id map.
591
        assert type(path) is str
592
        assert type(file_id) is str
0.200.1201 by Jelmer Vernooij
Implement _set_root_id.
593
        self.file_ids[path] = file_id
594
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
595
    def lookup_file_id(self, path):
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
596
        assert type(path) is str
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
597
        try:
0.200.973 by Jelmer Vernooij
Add tests for generate_file_id.
598
            file_id = self.file_ids[path]
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
599
        except KeyError:
0.200.973 by Jelmer Vernooij
Add tests for generate_file_id.
600
            file_id = self.mapping.generate_file_id(path)
601
        assert type(file_id) is str
602
        return file_id
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
603
604
    def lookup_path(self, file_id):
605
        if self.paths is None:
606
            self.paths = {}
607
            for k, v in self.file_ids.iteritems():
608
                self.paths[v] = k
609
        try:
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
610
            path = self.paths[file_id]
0.252.33 by Jelmer Vernooij
Fix file id map lookups.
611
        except KeyError:
612
            return self.mapping.parse_file_id(file_id)
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
613
        else:
614
            assert type(path) is str
615
            return path
0.200.1202 by Jelmer Vernooij
Implement has_or_had_id.
616
617
    def copy(self):
618
        return self.__class__(dict(self.file_ids), self.mapping)