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