/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
1
# Copyright (C) 2008 Canonical Ltd
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
17
"""Import command classes."""
18
19
0.79.5 by Ian Clatworthy
incorporate git-fast-import tweaks from bzr-fast-export script
20
# There is a bug in git 1.5.4.3 and older by which unquoting a string consumes
21
# one extra character. Set this variable to True to work-around it. It only
22
# happens when renaming a file whose name contains spaces and/or quotes, and
23
# the symptom is:
24
#   % git-fast-import
25
#   fatal: Missing space after source: R "file 1.txt" file 2.txt
26
# http://git.kernel.org/?p=git/git.git;a=commit;h=c8744d6a8b27115503565041566d97c21e722584
27
GIT_FAST_IMPORT_NEEDS_EXTRA_SPACE_AFTER_QUOTE = False
28
29
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
30
# Lists of command names
31
COMMAND_NAMES = ['blob', 'checkpoint', 'commit', 'progress', 'reset', 'tag']
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
32
FILE_COMMAND_NAMES = ['filemodify', 'filedelete', 'filecopy', 'filerename',
33
    'filedeleteall']
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
34
0.64.2 by Ian Clatworthy
use Bazaar file kinds
35
# Bazaar file kinds
36
FILE_KIND = 'file'
37
SYMLINK_KIND = 'symlink'
0.64.229 by Ian Clatworthy
Handle git submodules in the stream by warning about + ignoring them
38
TREE_REFERENCE_KIND = 'tree-reference'
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
39
40
41
class ImportCommand(object):
42
    """Base class for import commands."""
43
44
    def __init__(self, name):
45
        self.name = name
0.64.9 by Ian Clatworthy
dump parameter for info processor
46
        # List of field names not to display
47
        self._binary = []
48
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
49
    def __str__(self):
50
        return repr(self)
51
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
52
    def dump_str(self, names=None, child_lists=None, verbose=False):
53
        """Dump fields as a string.
54
55
        :param names: the list of fields to include or
56
            None for all public fields
57
        :param child_lists: dictionary of child command names to
58
            fields for that child command to include
59
        :param verbose: if True, prefix each line with the command class and
60
            display fields as a dictionary; if False, dump just the field
61
            values with tabs between them
62
        """
0.64.9 by Ian Clatworthy
dump parameter for info processor
63
        interesting = {}
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
64
        if names is None:
65
            fields = [k for k in self.__dict__.keys() if not k.startswith('_')]
66
        else:
67
            fields = names
68
        for field in fields:
69
            value = self.__dict__.get(field)
70
            if field in self._binary and value is not None:
71
                value = '(...)'
0.64.9 by Ian Clatworthy
dump parameter for info processor
72
            interesting[field] = value
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
73
        if verbose:
74
            return "%s: %s" % (self.__class__.__name__, interesting)
75
        else:
0.64.112 by Ian Clatworthy
fix unicode-related exception in fast-import-query
76
            return "\t".join([repr(interesting[k]) for k in fields])
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
77
78
79
class BlobCommand(ImportCommand):
80
0.64.35 by Ian Clatworthy
identify unmarked blobs and commits by line numbers
81
    def __init__(self, mark, data, lineno=0):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
82
        ImportCommand.__init__(self, 'blob')
83
        self.mark = mark
84
        self.data = data
0.64.35 by Ian Clatworthy
identify unmarked blobs and commits by line numbers
85
        self.lineno = lineno
86
        # Provide a unique id in case the mark is missing
87
        if mark is None:
88
            self.id = '@%d' % lineno
89
        else:
90
            self.id = ':' + mark
0.64.9 by Ian Clatworthy
dump parameter for info processor
91
        self._binary = ['data']
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
92
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
93
    def __repr__(self):
94
        if self.mark is None:
95
            mark_line = ""
96
        else:
97
            mark_line = "\nmark :%s" % self.mark
98
        return "blob%s\ndata %d\n%s" % (mark_line, len(self.data), self.data)
99
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
100
101
class CheckpointCommand(ImportCommand):
102
103
    def __init__(self):
104
        ImportCommand.__init__(self, 'checkpoint')
105
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
106
    def __repr__(self):
107
        return "checkpoint"
108
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
109
110
class CommitCommand(ImportCommand):
111
0.64.60 by Ian Clatworthy
support merges when from clause implicit
112
    def __init__(self, ref, mark, author, committer, message, from_,
113
        merges, file_iter, lineno=0):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
114
        ImportCommand.__init__(self, 'commit')
115
        self.ref = ref
116
        self.mark = mark
117
        self.author = author
118
        self.committer = committer
119
        self.message = message
0.64.60 by Ian Clatworthy
support merges when from clause implicit
120
        self.from_ = from_
121
        self.merges = merges
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
122
        self.file_iter = file_iter
0.64.35 by Ian Clatworthy
identify unmarked blobs and commits by line numbers
123
        self.lineno = lineno
0.64.9 by Ian Clatworthy
dump parameter for info processor
124
        self._binary = ['file_iter']
0.64.35 by Ian Clatworthy
identify unmarked blobs and commits by line numbers
125
        # Provide a unique id in case the mark is missing
126
        if mark is None:
127
            self.id = '@%d' % lineno
128
        else:
0.79.6 by Ian Clatworthy
refactor bzr_exporter to use Command objects
129
            self.id = ':%s' % mark
0.64.9 by Ian Clatworthy
dump parameter for info processor
130
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
131
    def __repr__(self):
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
132
        return self.to_string(include_file_contents=True)
133
134
    def __str__(self):
135
        return self.to_string(include_file_contents=False)
136
137
    def to_string(self, include_file_contents=False):
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
138
        if self.mark is None:
139
            mark_line = ""
140
        else:
141
            mark_line = "\nmark :%s" % self.mark
142
        if self.author is None:
143
            author_line = ""
144
        else:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
145
            author_line = "\nauthor %s" % format_who_when(self.author)
146
        committer = "committer %s" % format_who_when(self.committer)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
147
        if self.message is None:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
148
            msg_section = ""
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
149
        else:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
150
            msg = self.message.encode('utf8')
151
            msg_section = "\ndata %d\n%s" % (len(msg), msg)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
152
        if self.from_ is None:
153
            from_line = ""
154
        else:
0.77.4 by Ian Clatworthy
tweak from and merge formatting
155
            from_line = "\nfrom %s" % self.from_
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
156
        if self.merges is None:
157
            merge_lines = ""
158
        else:
0.77.4 by Ian Clatworthy
tweak from and merge formatting
159
            merge_lines = "".join(["\nmerge %s" % (m,)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
160
                for m in self.merges])
161
        if self.file_iter is None:
162
            filecommands = ""
163
        else:
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
164
            if include_file_contents:
165
                format_str = "\n%r"
166
            else:
167
                format_str = "\n%s"
168
            filecommands = "".join([format_str % (c,)
169
                for c in self.iter_files()])
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
170
        return "commit %s%s%s\n%s%s%s%s%s" % (self.ref, mark_line, author_line,
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
171
            committer, msg_section, from_line, merge_lines, filecommands)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
172
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
173
    def dump_str(self, names=None, child_lists=None, verbose=False):
174
        result = [ImportCommand.dump_str(self, names, verbose=verbose)]
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
175
        for f in self.iter_files():
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
176
            if child_lists is None:
177
                continue
178
            try:
179
                child_names = child_lists[f.name]
180
            except KeyError:
181
                continue
182
            result.append("\t%s" % f.dump_str(child_names, verbose=verbose))
0.64.9 by Ian Clatworthy
dump parameter for info processor
183
        return '\n'.join(result)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
184
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
185
    def iter_files(self):
186
        """Iterate over files."""
187
        # file_iter may be a callable or an iterator
188
        if callable(self.file_iter):
189
            return self.file_iter()
190
        elif self.file_iter:
191
            return iter(self.file_iter)
192
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
193
194
class ProgressCommand(ImportCommand):
195
196
    def __init__(self, message):
197
        ImportCommand.__init__(self, 'progress')
198
        self.message = message
199
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
200
    def __repr__(self):
201
        return "progress %s" % (self.message,)
202
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
203
204
class ResetCommand(ImportCommand):
205
206
    def __init__(self, ref, from_):
207
        ImportCommand.__init__(self, 'reset')
208
        self.ref = ref
209
        self.from_ = from_
210
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
211
    def __repr__(self):
212
        if self.from_ is None:
213
            from_line = ""
214
        else:
0.79.5 by Ian Clatworthy
incorporate git-fast-import tweaks from bzr-fast-export script
215
            # According to git-fast-import(1), the extra LF is optional here;
216
            # however, versions of git up to 1.5.4.3 had a bug by which the LF
217
            # was needed. Always emit it, since it doesn't hurt and maintains
218
            # compatibility with older versions.
219
            # http://git.kernel.org/?p=git/git.git;a=commit;h=655e8515f279c01f525745d443f509f97cd805ab
220
            from_line = "\nfrom %s\n" % self.from_
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
221
        return "reset %s%s" % (self.ref, from_line)
222
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
223
224
class TagCommand(ImportCommand):
225
226
    def __init__(self, id, from_, tagger, message):
227
        ImportCommand.__init__(self, 'tag')
228
        self.id = id
229
        self.from_ = from_
230
        self.tagger = tagger
231
        self.message = message
232
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
233
    def __repr__(self):
234
        if self.from_ is None:
235
            from_line = ""
236
        else:
0.77.6 by Ian Clatworthy
no filtering tests
237
            from_line = "\nfrom %s" % self.from_
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
238
        if self.tagger is None:
239
            tagger_line = ""
240
        else:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
241
            tagger_line = "\ntagger %s" % format_who_when(self.tagger)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
242
        if self.message is None:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
243
            msg_section = ""
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
244
        else:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
245
            msg = self.message.encode('utf8')
246
            msg_section = "\ndata %d\n%s" % (len(msg), msg)
247
        return "tag %s%s%s%s" % (self.id, from_line, tagger_line, msg_section)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
248
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
249
250
class FileCommand(ImportCommand):
251
    """Base class for file commands."""
252
    pass
253
254
255
class FileModifyCommand(FileCommand):
256
257
    def __init__(self, path, kind, is_executable, dataref, data):
258
        # Either dataref or data should be null
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
259
        FileCommand.__init__(self, 'filemodify')
0.64.147 by Ian Clatworthy
error on zero length paths in the input stream
260
        self.path = check_path(path)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
261
        self.kind = kind
262
        self.is_executable = is_executable
263
        self.dataref = dataref
264
        self.data = data
0.64.9 by Ian Clatworthy
dump parameter for info processor
265
        self._binary = ['data']
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
266
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
267
    def __repr__(self):
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
268
        return self.to_string(include_file_contents=True)
269
270
    def __str__(self):
271
        return self.to_string(include_file_contents=False)
272
273
    def to_string(self, include_file_contents=False):
0.64.229 by Ian Clatworthy
Handle git submodules in the stream by warning about + ignoring them
274
        if self.is_executable:
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
275
            mode = "755"
0.64.229 by Ian Clatworthy
Handle git submodules in the stream by warning about + ignoring them
276
        elif self.kind == 'file':
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
277
            mode = "644"
0.64.229 by Ian Clatworthy
Handle git submodules in the stream by warning about + ignoring them
278
        elif self.kind == 'symlink':
279
            mode = "120000"
280
        elif self.kind == 'tree-reference':
281
            mode = "160000"
282
        else:
283
            raise AssertionError("unknown kind %s" % (self.kind,))
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
284
        datastr = ""
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
285
        if self.dataref is None:
286
            dataref = "inline"
0.64.223 by Ian Clatworthy
Support displaying a CommitCommand without inline blob data
287
            if include_file_contents:
288
                datastr = "\ndata %d\n%s" % (len(self.data), self.data)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
289
        else:
0.77.3 by Ian Clatworthy
get fast-import-filter formatting commands in the output correctly
290
            dataref = "%s" % (self.dataref,)
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
291
        path = format_path(self.path)
292
        return "M %s %s %s%s" % (mode, dataref, path, datastr)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
293
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
294
295
class FileDeleteCommand(FileCommand):
296
297
    def __init__(self, path):
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
298
        FileCommand.__init__(self, 'filedelete')
0.64.147 by Ian Clatworthy
error on zero length paths in the input stream
299
        self.path = check_path(path)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
300
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
301
    def __repr__(self):
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
302
        return "D %s" % (format_path(self.path),)
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
303
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
304
305
class FileCopyCommand(FileCommand):
306
307
    def __init__(self, src_path, dest_path):
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
308
        FileCommand.__init__(self, 'filecopy')
0.64.147 by Ian Clatworthy
error on zero length paths in the input stream
309
        self.src_path = check_path(src_path)
310
        self.dest_path = check_path(dest_path)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
311
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
312
    def __repr__(self):
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
313
        return "C %s %s" % (
314
            format_path(self.src_path, quote_spaces=True),
315
            format_path(self.dest_path))
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
316
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
317
318
class FileRenameCommand(FileCommand):
319
0.64.2 by Ian Clatworthy
use Bazaar file kinds
320
    def __init__(self, old_path, new_path):
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
321
        FileCommand.__init__(self, 'filerename')
0.64.147 by Ian Clatworthy
error on zero length paths in the input stream
322
        self.old_path = check_path(old_path)
323
        self.new_path = check_path(new_path)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
324
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
325
    def __repr__(self):
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
326
        return "R %s %s" % (
327
            format_path(self.old_path, quote_spaces=True),
328
            format_path(self.new_path))
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
329
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
330
331
class FileDeleteAllCommand(FileCommand):
332
0.64.2 by Ian Clatworthy
use Bazaar file kinds
333
    def __init__(self):
0.64.19 by Ian Clatworthy
filtering enhancements: selected fields, filecommands, non-verbose format
334
        FileCommand.__init__(self, 'filedeleteall')
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
335
336
    def __repr__(self):
337
        return "deleteall"
338
339
0.64.147 by Ian Clatworthy
error on zero length paths in the input stream
340
def check_path(path):
341
    """Check that a path is legal.
342
343
    :return: the path if all is OK
344
    :raise ValueError: if the path is illegal
345
    """
346
    if path is None or path == '':
347
        raise ValueError("illegal path '%s'" % path)
348
    return path
349
350
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
351
def format_path(p, quote_spaces=False):
352
    """Format a path in utf8, quoting it if necessary."""
353
    if '\n' in p:
354
        import re
355
        p = re.sub('\n', '\\n', p)
356
        quote = True
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
357
    else:
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
358
        quote = p[0] == '"' or (quote_spaces and ' ' in p)
359
    if quote:
0.79.5 by Ian Clatworthy
incorporate git-fast-import tweaks from bzr-fast-export script
360
        extra = GIT_FAST_IMPORT_NEEDS_EXTRA_SPACE_AFTER_QUOTE and ' ' or ''
361
        p = '"%s"%s' % (p, extra)
0.77.15 by Ian Clatworthy
ensure output is utf8 encoded when required/recommended
362
    return p.encode('utf8')
363
364
365
def format_who_when(fields):
0.77.2 by Ian Clatworthy
code & tests for formatting Commands as file-import stream strings
366
    """Format a tuple of name,email,secs-since-epoch,utc-offset-secs as a string."""
367
    offset = fields[3]
368
    if offset < 0:
369
        offset_sign = '-'
370
        offset = abs(offset)
371
    else:
372
        offset_sign = '+'
373
    offset_hours = offset / 3600
374
    offset_minutes = offset / 60 - offset_hours * 60
375
    offset_str = "%s%02d%02d" % (offset_sign, offset_hours, offset_minutes)
0.64.132 by Ian Clatworthy
fix fast-export's encoding of user names
376
    name = fields[0]
0.79.6 by Ian Clatworthy
refactor bzr_exporter to use Command objects
377
    if name == '':
378
        sep = ''
379
    else:
380
        sep = ' '
0.64.132 by Ian Clatworthy
fix fast-export's encoding of user names
381
    result = "%s%s<%s> %d %s" % (name, sep, fields[1], fields[2], offset_str)
382
    return result.encode('utf8')