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