/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

[merge] basic_io metaformat (mbp)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
from cStringIO import StringIO
18
 
 
19
 
from bzrlib.symbol_versioning import deprecated_function, deprecated_in
20
 
from bzrlib.lazy_import import lazy_import
21
 
lazy_import(globals(), """
22
 
from bzrlib import (
23
 
    errors,
24
 
    urlutils,
25
 
    )
26
 
from bzrlib.bundle import serializer as _serializer
27
 
from bzrlib.merge_directive import MergeDirective
28
 
from bzrlib.transport import (
29
 
    do_catching_redirections,
30
 
    get_transport,
31
 
    )
32
 
""")
33
 
from bzrlib.trace import note
34
 
 
35
 
 
36
 
@deprecated_function(deprecated_in((1, 12, 0)))
37
 
def read_bundle_from_url(url):
38
 
    return read_mergeable_from_url(url, _do_directive=False)
39
 
 
40
 
 
41
 
def read_mergeable_from_url(url, _do_directive=True, possible_transports=None):
42
 
    """Read mergable object from a given URL.
43
 
 
44
 
    :return: An object supporting get_target_revision.  Raises NotABundle if
45
 
        the target is not a mergeable type.
46
 
    """
47
 
    child_transport = get_transport(url,
48
 
        possible_transports=possible_transports)
49
 
    transport = child_transport.clone('..')
50
 
    filename = transport.relpath(child_transport.base)
51
 
    mergeable, transport = read_mergeable_from_transport(transport, filename,
52
 
                                                         _do_directive)
53
 
    return mergeable
54
 
 
55
 
 
56
 
def read_mergeable_from_transport(transport, filename, _do_directive=True):
57
 
    def get_bundle(transport):
58
 
        return StringIO(transport.get_bytes(filename)), transport
59
 
 
60
 
    def redirected_transport(transport, exception, redirection_notice):
61
 
        note(redirection_notice)
62
 
        url, filename = urlutils.split(exception.target,
63
 
                                       exclude_trailing_slash=False)
64
 
        if not filename:
65
 
            raise errors.NotABundle('A directory cannot be a bundle')
66
 
        return get_transport(url)
67
 
 
 
1
# Copyright (C) 2004 Aaron Bentley <aaron.bentley@utoronto.ca>
 
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
"""Represent and apply a changeset.
 
18
 
 
19
Conflicts in applying a changeset are represented as exceptions.
 
20
 
 
21
This only handles the in-memory objects representing changesets, which are
 
22
primarily used by the merge code. 
 
23
"""
 
24
 
 
25
import os.path
 
26
import errno
 
27
import stat
 
28
from tempfile import mkdtemp
 
29
from shutil import rmtree
 
30
from itertools import izip
 
31
 
 
32
from bzrlib.trace import mutter, warning
 
33
from bzrlib.osutils import rename, sha_file
 
34
import bzrlib
 
35
from bzrlib.errors import BzrCheckError
 
36
 
 
37
__docformat__ = "restructuredtext"
 
38
 
 
39
NULL_ID = "!NULL"
 
40
 
 
41
class OldFailedTreeOp(Exception):
 
42
    def __init__(self):
 
43
        Exception.__init__(self, "bzr-tree-change contains files from a"
 
44
                           " previous failed merge operation.")
 
45
def invert_dict(dict):
 
46
    newdict = {}
 
47
    for (key,value) in dict.iteritems():
 
48
        newdict[value] = key
 
49
    return newdict
 
50
 
 
51
       
 
52
class ChangeExecFlag(object):
 
53
    """This is two-way change, suitable for file modification, creation,
 
54
    deletion"""
 
55
    def __init__(self, old_exec_flag, new_exec_flag):
 
56
        self.old_exec_flag = old_exec_flag
 
57
        self.new_exec_flag = new_exec_flag
 
58
 
 
59
    def apply(self, filename, conflict_handler, reverse=False):
 
60
        if not reverse:
 
61
            from_exec_flag = self.old_exec_flag
 
62
            to_exec_flag = self.new_exec_flag
 
63
        else:
 
64
            from_exec_flag = self.new_exec_flag
 
65
            to_exec_flag = self.old_exec_flag
 
66
        try:
 
67
            current_exec_flag = bool(os.stat(filename).st_mode & 0111)
 
68
        except OSError, e:
 
69
            if e.errno == errno.ENOENT:
 
70
                if conflict_handler.missing_for_exec_flag(filename) == "skip":
 
71
                    return
 
72
                else:
 
73
                    current_exec_flag = from_exec_flag
 
74
 
 
75
        if from_exec_flag is not None and current_exec_flag != from_exec_flag:
 
76
            if conflict_handler.wrong_old_exec_flag(filename,
 
77
                        from_exec_flag, current_exec_flag) != "continue":
 
78
                return
 
79
 
 
80
        if to_exec_flag is not None:
 
81
            current_mode = os.stat(filename).st_mode
 
82
            if to_exec_flag:
 
83
                umask = os.umask(0)
 
84
                os.umask(umask)
 
85
                to_mode = current_mode | (0100 & ~umask)
 
86
                # Enable x-bit for others only if they can read it.
 
87
                if current_mode & 0004:
 
88
                    to_mode |= 0001 & ~umask
 
89
                if current_mode & 0040:
 
90
                    to_mode |= 0010 & ~umask
 
91
            else:
 
92
                to_mode = current_mode & ~0111
 
93
            try:
 
94
                os.chmod(filename, to_mode)
 
95
            except IOError, e:
 
96
                if e.errno == errno.ENOENT:
 
97
                    conflict_handler.missing_for_exec_flag(filename)
 
98
 
 
99
    def __eq__(self, other):
 
100
        return (isinstance(other, ChangeExecFlag) and
 
101
                self.old_exec_flag == other.old_exec_flag and
 
102
                self.new_exec_flag == other.new_exec_flag)
 
103
 
 
104
    def __ne__(self, other):
 
105
        return not (self == other)
 
106
 
 
107
 
 
108
def dir_create(filename, conflict_handler, reverse):
 
109
    """Creates the directory, or deletes it if reverse is true.  Intended to be
 
110
    used with ReplaceContents.
 
111
 
 
112
    :param filename: The name of the directory to create
 
113
    :type filename: str
 
114
    :param reverse: If true, delete the directory, instead
 
115
    :type reverse: bool
 
116
    """
 
117
    if not reverse:
 
118
        try:
 
119
            os.mkdir(filename)
 
120
        except OSError, e:
 
121
            if e.errno != errno.EEXIST:
 
122
                raise
 
123
            if conflict_handler.dir_exists(filename) == "continue":
 
124
                os.mkdir(filename)
 
125
        except IOError, e:
 
126
            if e.errno == errno.ENOENT:
 
127
                if conflict_handler.missing_parent(filename)=="continue":
 
128
                    file(filename, "wb").write(self.contents)
 
129
    else:
 
130
        try:
 
131
            os.rmdir(filename)
 
132
        except OSError, e:
 
133
            if e.errno != errno.ENOTEMPTY:
 
134
                raise
 
135
            if conflict_handler.rmdir_non_empty(filename) == "skip":
 
136
                return
 
137
            os.rmdir(filename)
 
138
 
 
139
 
 
140
class SymlinkCreate(object):
 
141
    """Creates or deletes a symlink (for use with ReplaceContents)"""
 
142
    def __init__(self, contents):
 
143
        """Constructor.
 
144
 
 
145
        :param contents: The filename of the target the symlink should point to
 
146
        :type contents: str
 
147
        """
 
148
        self.target = contents
 
149
 
 
150
    def __repr__(self):
 
151
        return "SymlinkCreate(%s)" % self.target
 
152
 
 
153
    def __call__(self, filename, conflict_handler, reverse):
 
154
        """Creates or destroys the symlink.
 
155
 
 
156
        :param filename: The name of the symlink to create
 
157
        :type filename: str
 
158
        """
 
159
        if reverse:
 
160
            assert(os.readlink(filename) == self.target)
 
161
            os.unlink(filename)
 
162
        else:
 
163
            try:
 
164
                os.symlink(self.target, filename)
 
165
            except OSError, e:
 
166
                if e.errno != errno.EEXIST:
 
167
                    raise
 
168
                if conflict_handler.link_name_exists(filename) == "continue":
 
169
                    os.symlink(self.target, filename)
 
170
 
 
171
    def __eq__(self, other):
 
172
        if not isinstance(other, SymlinkCreate):
 
173
            return False
 
174
        elif self.target != other.target:
 
175
            return False
 
176
        else:
 
177
            return True
 
178
 
 
179
    def __ne__(self, other):
 
180
        return not (self == other)
 
181
 
 
182
class FileCreate(object):
 
183
    """Create or delete a file (for use with ReplaceContents)"""
 
184
    def __init__(self, contents):
 
185
        """Constructor
 
186
 
 
187
        :param contents: The contents of the file to write
 
188
        :type contents: str
 
189
        """
 
190
        self.contents = contents
 
191
 
 
192
    def __repr__(self):
 
193
        return "FileCreate(%i b)" % len(self.contents)
 
194
 
 
195
    def __eq__(self, other):
 
196
        if not isinstance(other, FileCreate):
 
197
            return False
 
198
        elif self.contents != other.contents:
 
199
            return False
 
200
        else:
 
201
            return True
 
202
 
 
203
    def __ne__(self, other):
 
204
        return not (self == other)
 
205
 
 
206
    def __call__(self, filename, conflict_handler, reverse):
 
207
        """Create or delete a file
 
208
 
 
209
        :param filename: The name of the file to create
 
210
        :type filename: str
 
211
        :param reverse: Delete the file instead of creating it
 
212
        :type reverse: bool
 
213
        """
 
214
        if not reverse:
 
215
            try:
 
216
                file(filename, "wb").write(self.contents)
 
217
            except IOError, e:
 
218
                if e.errno == errno.ENOENT:
 
219
                    if conflict_handler.missing_parent(filename)=="continue":
 
220
                        file(filename, "wb").write(self.contents)
 
221
                else:
 
222
                    raise
 
223
 
 
224
        else:
 
225
            try:
 
226
                if (file(filename, "rb").read() != self.contents):
 
227
                    direction = conflict_handler.wrong_old_contents(filename,
 
228
                                                                    self.contents)
 
229
                    if  direction != "continue":
 
230
                        return
 
231
                os.unlink(filename)
 
232
            except IOError, e:
 
233
                if e.errno != errno.ENOENT:
 
234
                    raise
 
235
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
236
                    return
 
237
 
 
238
                    
 
239
 
 
240
class TreeFileCreate(object):
 
241
    """Create or delete a file (for use with ReplaceContents)"""
 
242
    def __init__(self, tree, file_id):
 
243
        """Constructor
 
244
 
 
245
        :param contents: The contents of the file to write
 
246
        :type contents: str
 
247
        """
 
248
        self.tree = tree
 
249
        self.file_id = file_id
 
250
 
 
251
    def __repr__(self):
 
252
        return "TreeFileCreate(%s)" % self.file_id
 
253
 
 
254
    def __eq__(self, other):
 
255
        if not isinstance(other, TreeFileCreate):
 
256
            return False
 
257
        return self.tree.get_file_sha1(self.file_id) == \
 
258
            other.tree.get_file_sha1(other.file_id)
 
259
 
 
260
    def __ne__(self, other):
 
261
        return not (self == other)
 
262
 
 
263
    def write_file(self, filename):
 
264
        outfile = file(filename, "wb")
 
265
        for line in self.tree.get_file(self.file_id):
 
266
            outfile.write(line)
 
267
 
 
268
    def same_text(self, filename):
 
269
        in_file = file(filename, "rb")
 
270
        return sha_file(in_file) == self.tree.get_file_sha1(self.file_id)
 
271
 
 
272
    def __call__(self, filename, conflict_handler, reverse):
 
273
        """Create or delete a file
 
274
 
 
275
        :param filename: The name of the file to create
 
276
        :type filename: str
 
277
        :param reverse: Delete the file instead of creating it
 
278
        :type reverse: bool
 
279
        """
 
280
        if not reverse:
 
281
            try:
 
282
                self.write_file(filename)
 
283
            except IOError, e:
 
284
                if e.errno == errno.ENOENT:
 
285
                    if conflict_handler.missing_parent(filename)=="continue":
 
286
                        self.write_file(filename)
 
287
                else:
 
288
                    raise
 
289
 
 
290
        else:
 
291
            try:
 
292
                if not self.same_text(filename):
 
293
                    direction = conflict_handler.wrong_old_contents(filename,
 
294
                        self.tree.get_file(self.file_id).read())
 
295
                    if  direction != "continue":
 
296
                        return
 
297
                os.unlink(filename)
 
298
            except IOError, e:
 
299
                if e.errno != errno.ENOENT:
 
300
                    raise
 
301
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
302
                    return
 
303
 
 
304
                    
 
305
 
 
306
def reversed(sequence):
 
307
    max = len(sequence) - 1
 
308
    for i in range(len(sequence)):
 
309
        yield sequence[max - i]
 
310
 
 
311
class ReplaceContents(object):
 
312
    """A contents-replacement framework.  It allows a file/directory/symlink to
 
313
    be created, deleted, or replaced with another file/directory/symlink.
 
314
    Arguments must be callable with (filename, reverse).
 
315
    """
 
316
    def __init__(self, old_contents, new_contents):
 
317
        """Constructor.
 
318
 
 
319
        :param old_contents: The change to reverse apply (e.g. a deletion), \
 
320
        when going forwards.
 
321
        :type old_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
 
322
        NoneType, etc.
 
323
        :param new_contents: The second change to apply (e.g. a creation), \
 
324
        when going forwards.
 
325
        :type new_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
 
326
        NoneType, etc.
 
327
        """
 
328
        self.old_contents=old_contents
 
329
        self.new_contents=new_contents
 
330
 
 
331
    def __repr__(self):
 
332
        return "ReplaceContents(%r -> %r)" % (self.old_contents,
 
333
                                              self.new_contents)
 
334
 
 
335
    def __eq__(self, other):
 
336
        if not isinstance(other, ReplaceContents):
 
337
            return False
 
338
        elif self.old_contents != other.old_contents:
 
339
            return False
 
340
        elif self.new_contents != other.new_contents:
 
341
            return False
 
342
        else:
 
343
            return True
 
344
    def __ne__(self, other):
 
345
        return not (self == other)
 
346
 
 
347
    def apply(self, filename, conflict_handler, reverse=False):
 
348
        """Applies the FileReplacement to the specified filename
 
349
 
 
350
        :param filename: The name of the file to apply changes to
 
351
        :type filename: str
 
352
        :param reverse: If true, apply the change in reverse
 
353
        :type reverse: bool
 
354
        """
 
355
        if not reverse:
 
356
            undo = self.old_contents
 
357
            perform = self.new_contents
 
358
        else:
 
359
            undo = self.new_contents
 
360
            perform = self.old_contents
 
361
        mode = None
 
362
        if undo is not None:
 
363
            try:
 
364
                mode = os.lstat(filename).st_mode
 
365
                if stat.S_ISLNK(mode):
 
366
                    mode = None
 
367
            except OSError, e:
 
368
                if e.errno != errno.ENOENT:
 
369
                    raise
 
370
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
371
                    return
 
372
            undo(filename, conflict_handler, reverse=True)
 
373
        if perform is not None:
 
374
            perform(filename, conflict_handler, reverse=False)
 
375
            if mode is not None:
 
376
                os.chmod(filename, mode)
 
377
 
 
378
    def is_creation(self):
 
379
        return self.new_contents is not None and self.old_contents is None
 
380
 
 
381
    def is_deletion(self):
 
382
        return self.old_contents is not None and self.new_contents is None
 
383
 
 
384
class ApplySequence(object):
 
385
    def __init__(self, changes=None):
 
386
        self.changes = []
 
387
        if changes is not None:
 
388
            self.changes.extend(changes)
 
389
 
 
390
    def __eq__(self, other):
 
391
        if not isinstance(other, ApplySequence):
 
392
            return False
 
393
        elif len(other.changes) != len(self.changes):
 
394
            return False
 
395
        else:
 
396
            for i in range(len(self.changes)):
 
397
                if self.changes[i] != other.changes[i]:
 
398
                    return False
 
399
            return True
 
400
 
 
401
    def __ne__(self, other):
 
402
        return not (self == other)
 
403
 
 
404
    
 
405
    def apply(self, filename, conflict_handler, reverse=False):
 
406
        if not reverse:
 
407
            iter = self.changes
 
408
        else:
 
409
            iter = reversed(self.changes)
 
410
        for change in iter:
 
411
            change.apply(filename, conflict_handler, reverse)
 
412
 
 
413
 
 
414
class Diff3Merge(object):
 
415
    history_based = False
 
416
    def __init__(self, file_id, base, other):
 
417
        self.file_id = file_id
 
418
        self.base = base
 
419
        self.other = other
 
420
 
 
421
    def is_creation(self):
 
422
        return False
 
423
 
 
424
    def is_deletion(self):
 
425
        return False
 
426
 
 
427
    def __eq__(self, other):
 
428
        if not isinstance(other, Diff3Merge):
 
429
            return False
 
430
        return (self.base == other.base and 
 
431
                self.other == other.other and self.file_id == other.file_id)
 
432
 
 
433
    def __ne__(self, other):
 
434
        return not (self == other)
 
435
 
 
436
    def dump_file(self, temp_dir, name, tree):
 
437
        out_path = os.path.join(temp_dir, name)
 
438
        out_file = file(out_path, "wb")
 
439
        in_file = tree.get_file(self.file_id)
 
440
        for line in in_file:
 
441
            out_file.write(line)
 
442
        return out_path
 
443
 
 
444
    def apply(self, filename, conflict_handler, reverse=False):
 
445
        import bzrlib.patch
 
446
        temp_dir = mkdtemp(prefix="bzr-")
 
447
        try:
 
448
            new_file = filename+".new"
 
449
            base_file = self.dump_file(temp_dir, "base", self.base)
 
450
            other_file = self.dump_file(temp_dir, "other", self.other)
 
451
            if not reverse:
 
452
                base = base_file
 
453
                other = other_file
 
454
            else:
 
455
                base = other_file
 
456
                other = base_file
 
457
            status = bzrlib.patch.diff3(new_file, filename, base, other)
 
458
            if status == 0:
 
459
                os.chmod(new_file, os.stat(filename).st_mode)
 
460
                rename(new_file, filename)
 
461
                return
 
462
            else:
 
463
                assert(status == 1)
 
464
                def get_lines(filename):
 
465
                    my_file = file(filename, "rb")
 
466
                    lines = my_file.readlines()
 
467
                    my_file.close()
 
468
                    return lines
 
469
                base_lines = get_lines(base)
 
470
                other_lines = get_lines(other)
 
471
                conflict_handler.merge_conflict(new_file, filename, base_lines, 
 
472
                                                other_lines)
 
473
        finally:
 
474
            rmtree(temp_dir)
 
475
 
 
476
 
 
477
def CreateDir():
 
478
    """Convenience function to create a directory.
 
479
 
 
480
    :return: A ReplaceContents that will create a directory
 
481
    :rtype: `ReplaceContents`
 
482
    """
 
483
    return ReplaceContents(None, dir_create)
 
484
 
 
485
def DeleteDir():
 
486
    """Convenience function to delete a directory.
 
487
 
 
488
    :return: A ReplaceContents that will delete a directory
 
489
    :rtype: `ReplaceContents`
 
490
    """
 
491
    return ReplaceContents(dir_create, None)
 
492
 
 
493
def CreateFile(contents):
 
494
    """Convenience fucntion to create a file.
 
495
    
 
496
    :param contents: The contents of the file to create 
 
497
    :type contents: str
 
498
    :return: A ReplaceContents that will create a file 
 
499
    :rtype: `ReplaceContents`
 
500
    """
 
501
    return ReplaceContents(None, FileCreate(contents))
 
502
 
 
503
def DeleteFile(contents):
 
504
    """Convenience fucntion to delete a file.
 
505
    
 
506
    :param contents: The contents of the file to delete
 
507
    :type contents: str
 
508
    :return: A ReplaceContents that will delete a file 
 
509
    :rtype: `ReplaceContents`
 
510
    """
 
511
    return ReplaceContents(FileCreate(contents), None)
 
512
 
 
513
def ReplaceFileContents(old_tree, new_tree, file_id):
 
514
    """Convenience fucntion to replace the contents of a file.
 
515
    
 
516
    :param old_contents: The contents of the file to replace 
 
517
    :type old_contents: str
 
518
    :param new_contents: The contents to replace the file with
 
519
    :type new_contents: str
 
520
    :return: A ReplaceContents that will replace the contents of a file a file 
 
521
    :rtype: `ReplaceContents`
 
522
    """
 
523
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
 
524
                           TreeFileCreate(new_tree, file_id))
 
525
 
 
526
def CreateSymlink(target):
 
527
    """Convenience fucntion to create a symlink.
 
528
    
 
529
    :param target: The path the link should point to
 
530
    :type target: str
 
531
    :return: A ReplaceContents that will delete a file 
 
532
    :rtype: `ReplaceContents`
 
533
    """
 
534
    return ReplaceContents(None, SymlinkCreate(target))
 
535
 
 
536
def DeleteSymlink(target):
 
537
    """Convenience fucntion to delete a symlink.
 
538
    
 
539
    :param target: The path the link should point to
 
540
    :type target: str
 
541
    :return: A ReplaceContents that will delete a file 
 
542
    :rtype: `ReplaceContents`
 
543
    """
 
544
    return ReplaceContents(SymlinkCreate(target), None)
 
545
 
 
546
def ChangeTarget(old_target, new_target):
 
547
    """Convenience fucntion to change the target of a symlink.
 
548
    
 
549
    :param old_target: The current link target
 
550
    :type old_target: str
 
551
    :param new_target: The new link target to use
 
552
    :type new_target: str
 
553
    :return: A ReplaceContents that will delete a file 
 
554
    :rtype: `ReplaceContents`
 
555
    """
 
556
    return ReplaceContents(SymlinkCreate(old_target), SymlinkCreate(new_target))
 
557
 
 
558
 
 
559
class InvalidEntry(Exception):
 
560
    """Raise when a ChangesetEntry is invalid in some way"""
 
561
    def __init__(self, entry, problem):
 
562
        """Constructor.
 
563
 
 
564
        :param entry: The invalid ChangesetEntry
 
565
        :type entry: `ChangesetEntry`
 
566
        :param problem: The problem with the entry
 
567
        :type problem: str
 
568
        """
 
569
        msg = "Changeset entry for %s (%s) is invalid.\n%s" % (entry.id, 
 
570
                                                               entry.path, 
 
571
                                                               problem)
 
572
        Exception.__init__(self, msg)
 
573
        self.entry = entry
 
574
 
 
575
 
 
576
class SourceRootHasName(InvalidEntry):
 
577
    """This changeset entry has a name other than "", but its parent is !NULL"""
 
578
    def __init__(self, entry, name):
 
579
        """Constructor.
 
580
 
 
581
        :param entry: The invalid ChangesetEntry
 
582
        :type entry: `ChangesetEntry`
 
583
        :param name: The name of the entry
 
584
        :type name: str
 
585
        """
 
586
        msg = 'Child of !NULL is named "%s", not "./.".' % name
 
587
        InvalidEntry.__init__(self, entry, msg)
 
588
 
 
589
class NullIDAssigned(InvalidEntry):
 
590
    """The id !NULL was assigned to a real entry"""
 
591
    def __init__(self, entry):
 
592
        """Constructor.
 
593
 
 
594
        :param entry: The invalid ChangesetEntry
 
595
        :type entry: `ChangesetEntry`
 
596
        """
 
597
        msg = '"!NULL" id assigned to a file "%s".' % entry.path
 
598
        InvalidEntry.__init__(self, entry, msg)
 
599
 
 
600
class ParentIDIsSelf(InvalidEntry):
 
601
    """An entry is marked as its own parent"""
 
602
    def __init__(self, entry):
 
603
        """Constructor.
 
604
 
 
605
        :param entry: The invalid ChangesetEntry
 
606
        :type entry: `ChangesetEntry`
 
607
        """
 
608
        msg = 'file %s has "%s" id for both self id and parent id.' % \
 
609
            (entry.path, entry.id)
 
610
        InvalidEntry.__init__(self, entry, msg)
 
611
 
 
612
class ChangesetEntry(object):
 
613
    """An entry the changeset"""
 
614
    def __init__(self, id, parent, path):
 
615
        """Constructor. Sets parent and name assuming it was not
 
616
        renamed/created/deleted.
 
617
        :param id: The id associated with the entry
 
618
        :param parent: The id of the parent of this entry (or !NULL if no
 
619
        parent)
 
620
        :param path: The file path relative to the tree root of this entry
 
621
        """
 
622
        self.id = id
 
623
        self.path = path 
 
624
        self.new_path = path
 
625
        self.parent = parent
 
626
        self.new_parent = parent
 
627
        self.contents_change = None
 
628
        self.metadata_change = None
 
629
        if parent == NULL_ID and path !='./.':
 
630
            raise SourceRootHasName(self, path)
 
631
        if self.id == NULL_ID:
 
632
            raise NullIDAssigned(self)
 
633
        if self.id  == self.parent:
 
634
            raise ParentIDIsSelf(self)
 
635
 
 
636
    def __repr__(self):
 
637
        return "ChangesetEntry(%s)" % self.id
 
638
 
 
639
    __str__ = __repr__
 
640
 
 
641
    def __get_dir(self):
 
642
        if self.path is None:
 
643
            return None
 
644
        return os.path.dirname(self.path)
 
645
 
 
646
    def __set_dir(self, dir):
 
647
        self.path = os.path.join(dir, os.path.basename(self.path))
 
648
 
 
649
    dir = property(__get_dir, __set_dir)
 
650
    
 
651
    def __get_name(self):
 
652
        if self.path is None:
 
653
            return None
 
654
        return os.path.basename(self.path)
 
655
 
 
656
    def __set_name(self, name):
 
657
        self.path = os.path.join(os.path.dirname(self.path), name)
 
658
 
 
659
    name = property(__get_name, __set_name)
 
660
 
 
661
    def __get_new_dir(self):
 
662
        if self.new_path is None:
 
663
            return None
 
664
        return os.path.dirname(self.new_path)
 
665
 
 
666
    def __set_new_dir(self, dir):
 
667
        self.new_path = os.path.join(dir, os.path.basename(self.new_path))
 
668
 
 
669
    new_dir = property(__get_new_dir, __set_new_dir)
 
670
 
 
671
    def __get_new_name(self):
 
672
        if self.new_path is None:
 
673
            return None
 
674
        return os.path.basename(self.new_path)
 
675
 
 
676
    def __set_new_name(self, name):
 
677
        self.new_path = os.path.join(os.path.dirname(self.new_path), name)
 
678
 
 
679
    new_name = property(__get_new_name, __set_new_name)
 
680
 
 
681
    def needs_rename(self):
 
682
        """Determines whether the entry requires renaming.
 
683
 
 
684
        :rtype: bool
 
685
        """
 
686
 
 
687
        return (self.parent != self.new_parent or self.name != self.new_name)
 
688
 
 
689
    def is_deletion(self, reverse):
 
690
        """Return true if applying the entry would delete a file/directory.
 
691
 
 
692
        :param reverse: if true, the changeset is being applied in reverse
 
693
        :rtype: bool
 
694
        """
 
695
        return self.is_creation(not reverse)
 
696
 
 
697
    def is_creation(self, reverse):
 
698
        """Return true if applying the entry would create a file/directory.
 
699
 
 
700
        :param reverse: if true, the changeset is being applied in reverse
 
701
        :rtype: bool
 
702
        """
 
703
        if self.contents_change is None:
 
704
            return False
 
705
        if reverse:
 
706
            return self.contents_change.is_deletion()
 
707
        else:
 
708
            return self.contents_change.is_creation()
 
709
 
 
710
    def is_creation_or_deletion(self):
 
711
        """Return true if applying the entry would create or delete a 
 
712
        file/directory.
 
713
 
 
714
        :rtype: bool
 
715
        """
 
716
        return self.is_creation(False) or self.is_deletion(False)
 
717
 
 
718
    def get_cset_path(self, mod=False):
 
719
        """Determine the path of the entry according to the changeset.
 
720
 
 
721
        :param changeset: The changeset to derive the path from
 
722
        :type changeset: `Changeset`
 
723
        :param mod: If true, generate the MOD path.  Otherwise, generate the \
 
724
        ORIG path.
 
725
        :return: the path of the entry, or None if it did not exist in the \
 
726
        requested tree.
 
727
        :rtype: str or NoneType
 
728
        """
 
729
        if mod:
 
730
            if self.new_parent == NULL_ID:
 
731
                return "./."
 
732
            elif self.new_parent is None:
 
733
                return None
 
734
            return self.new_path
 
735
        else:
 
736
            if self.parent == NULL_ID:
 
737
                return "./."
 
738
            elif self.parent is None:
 
739
                return None
 
740
            return self.path
 
741
 
 
742
    def summarize_name(self, reverse=False):
 
743
        """Produce a one-line summary of the filename.  Indicates renames as
 
744
        old => new, indicates creation as None => new, indicates deletion as
 
745
        old => None.
 
746
 
 
747
        :param changeset: The changeset to get paths from
 
748
        :type changeset: `Changeset`
 
749
        :param reverse: If true, reverse the names in the output
 
750
        :type reverse: bool
 
751
        :rtype: str
 
752
        """
 
753
        orig_path = self.get_cset_path(False)
 
754
        mod_path = self.get_cset_path(True)
 
755
        if orig_path and orig_path.startswith('./'):
 
756
            orig_path = orig_path[2:]
 
757
        if mod_path and mod_path.startswith('./'):
 
758
            mod_path = mod_path[2:]
 
759
        if orig_path == mod_path:
 
760
            return orig_path
 
761
        else:
 
762
            if not reverse:
 
763
                return "%s => %s" % (orig_path, mod_path)
 
764
            else:
 
765
                return "%s => %s" % (mod_path, orig_path)
 
766
 
 
767
 
 
768
    def get_new_path(self, id_map, changeset, reverse=False):
 
769
        """Determine the full pathname to rename to
 
770
 
 
771
        :param id_map: The map of ids to filenames for the tree
 
772
        :type id_map: Dictionary
 
773
        :param changeset: The changeset to get data from
 
774
        :type changeset: `Changeset`
 
775
        :param reverse: If true, we're applying the changeset in reverse
 
776
        :type reverse: bool
 
777
        :rtype: str
 
778
        """
 
779
        mutter("Finding new path for %s", self.summarize_name())
 
780
        if reverse:
 
781
            parent = self.parent
 
782
            to_dir = self.dir
 
783
            from_dir = self.new_dir
 
784
            to_name = self.name
 
785
            from_name = self.new_name
 
786
        else:
 
787
            parent = self.new_parent
 
788
            to_dir = self.new_dir
 
789
            from_dir = self.dir
 
790
            to_name = self.new_name
 
791
            from_name = self.name
 
792
 
 
793
        if to_name is None:
 
794
            return None
 
795
 
 
796
        if parent == NULL_ID or parent is None:
 
797
            if to_name != '.':
 
798
                raise SourceRootHasName(self, to_name)
 
799
            else:
 
800
                return '.'
 
801
        if from_dir == to_dir:
 
802
            dir = os.path.dirname(id_map[self.id])
 
803
        else:
 
804
            mutter("path, new_path: %r %r", self.path, self.new_path)
 
805
            parent_entry = changeset.entries[parent]
 
806
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
 
807
        if from_name == to_name:
 
808
            name = os.path.basename(id_map[self.id])
 
809
        else:
 
810
            name = to_name
 
811
            assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
 
812
        return os.path.join(dir, name)
 
813
 
 
814
    def is_boring(self):
 
815
        """Determines whether the entry does nothing
 
816
        
 
817
        :return: True if the entry does no renames or content changes
 
818
        :rtype: bool
 
819
        """
 
820
        if self.contents_change is not None:
 
821
            return False
 
822
        elif self.metadata_change is not None:
 
823
            return False
 
824
        elif self.parent != self.new_parent:
 
825
            return False
 
826
        elif self.name != self.new_name:
 
827
            return False
 
828
        else:
 
829
            return True
 
830
 
 
831
    def apply(self, filename, conflict_handler, reverse=False):
 
832
        """Applies the file content and/or metadata changes.
 
833
 
 
834
        :param filename: the filename of the entry
 
835
        :type filename: str
 
836
        :param reverse: If true, apply the changes in reverse
 
837
        :type reverse: bool
 
838
        """
 
839
        if self.is_deletion(reverse) and self.metadata_change is not None:
 
840
            self.metadata_change.apply(filename, conflict_handler, reverse)
 
841
        if self.contents_change is not None:
 
842
            self.contents_change.apply(filename, conflict_handler, reverse)
 
843
        if not self.is_deletion(reverse) and self.metadata_change is not None:
 
844
            self.metadata_change.apply(filename, conflict_handler, reverse)
 
845
 
 
846
class IDPresent(Exception):
 
847
    def __init__(self, id):
 
848
        msg = "Cannot add entry because that id has already been used:\n%s" %\
 
849
            id
 
850
        Exception.__init__(self, msg)
 
851
        self.id = id
 
852
 
 
853
class Changeset(object):
 
854
    """A set of changes to apply"""
 
855
    def __init__(self):
 
856
        self.entries = {}
 
857
 
 
858
    def add_entry(self, entry):
 
859
        """Add an entry to the list of entries"""
 
860
        if self.entries.has_key(entry.id):
 
861
            raise IDPresent(entry.id)
 
862
        self.entries[entry.id] = entry
 
863
 
 
864
def my_sort(sequence, key, reverse=False):
 
865
    """A sort function that supports supplying a key for comparison
 
866
    
 
867
    :param sequence: The sequence to sort
 
868
    :param key: A callable object that returns the values to be compared
 
869
    :param reverse: If true, sort in reverse order
 
870
    :type reverse: bool
 
871
    """
 
872
    def cmp_by_key(entry_a, entry_b):
 
873
        if reverse:
 
874
            tmp=entry_a
 
875
            entry_a = entry_b
 
876
            entry_b = tmp
 
877
        return cmp(key(entry_a), key(entry_b))
 
878
    sequence.sort(cmp_by_key)
 
879
 
 
880
def get_rename_entries(changeset, inventory, reverse):
 
881
    """Return a list of entries that will be renamed.  Entries are sorted from
 
882
    longest to shortest source path and from shortest to longest target path.
 
883
 
 
884
    :param changeset: The changeset to look in
 
885
    :type changeset: `Changeset`
 
886
    :param inventory: The source of current tree paths for the given ids
 
887
    :type inventory: Dictionary
 
888
    :param reverse: If true, the changeset is being applied in reverse
 
889
    :type reverse: bool
 
890
    :return: source entries and target entries as a tuple
 
891
    :rtype: (List, List)
 
892
    """
 
893
    source_entries = [x for x in changeset.entries.itervalues() 
 
894
                      if x.needs_rename() or x.is_creation_or_deletion()]
 
895
    # these are done from longest path to shortest, to avoid deleting a
 
896
    # parent before its children are deleted/renamed 
 
897
    def longest_to_shortest(entry):
 
898
        path = inventory.get(entry.id)
 
899
        if path is None:
 
900
            return 0
 
901
        else:
 
902
            return len(path)
 
903
    my_sort(source_entries, longest_to_shortest, reverse=True)
 
904
 
 
905
    target_entries = source_entries[:]
 
906
    # These are done from shortest to longest path, to avoid creating a
 
907
    # child before its parent has been created/renamed
 
908
    def shortest_to_longest(entry):
 
909
        path = entry.get_new_path(inventory, changeset, reverse)
 
910
        if path is None:
 
911
            return 0
 
912
        else:
 
913
            return len(path)
 
914
    my_sort(target_entries, shortest_to_longest)
 
915
    return (source_entries, target_entries)
 
916
 
 
917
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
 
918
                          conflict_handler, reverse):
 
919
    """Delete and rename entries as appropriate.  Entries are renamed to temp
 
920
    names.  A map of id -> temp name (or None, for deletions) is returned.
 
921
 
 
922
    :param source_entries: The entries to rename and delete
 
923
    :type source_entries: List of `ChangesetEntry`
 
924
    :param inventory: The map of id -> filename in the current tree
 
925
    :type inventory: Dictionary
 
926
    :param dir: The directory to apply changes to
 
927
    :type dir: str
 
928
    :param reverse: Apply changes in reverse
 
929
    :type reverse: bool
 
930
    :return: a mapping of id to temporary name
 
931
    :rtype: Dictionary
 
932
    """
 
933
    temp_name = {}
 
934
    for i in range(len(source_entries)):
 
935
        entry = source_entries[i]
 
936
        if entry.is_deletion(reverse):
 
937
            path = os.path.join(dir, inventory[entry.id])
 
938
            entry.apply(path, conflict_handler, reverse)
 
939
            temp_name[entry.id] = None
 
940
 
 
941
        elif entry.needs_rename():
 
942
            to_name = os.path.join(temp_dir, str(i))
 
943
            src_path = inventory.get(entry.id)
 
944
            if src_path is not None:
 
945
                src_path = os.path.join(dir, src_path)
 
946
                try:
 
947
                    rename(src_path, to_name)
 
948
                    temp_name[entry.id] = to_name
 
949
                except OSError, e:
 
950
                    if e.errno != errno.ENOENT:
 
951
                        raise
 
952
                    if conflict_handler.missing_for_rename(src_path, to_name) \
 
953
                        == "skip":
 
954
                        continue
 
955
 
 
956
    return temp_name
 
957
 
 
958
 
 
959
def rename_to_new_create(changed_inventory, target_entries, inventory, 
 
960
                         changeset, dir, conflict_handler, reverse):
 
961
    """Rename entries with temp names to their final names, create new files.
 
962
 
 
963
    :param changed_inventory: A mapping of id to temporary name
 
964
    :type changed_inventory: Dictionary
 
965
    :param target_entries: The entries to apply changes to
 
966
    :type target_entries: List of `ChangesetEntry`
 
967
    :param changeset: The changeset to apply
 
968
    :type changeset: `Changeset`
 
969
    :param dir: The directory to apply changes to
 
970
    :type dir: str
 
971
    :param reverse: If true, apply changes in reverse
 
972
    :type reverse: bool
 
973
    """
 
974
    for entry in target_entries:
 
975
        new_tree_path = entry.get_new_path(inventory, changeset, reverse)
 
976
        if new_tree_path is None:
 
977
            continue
 
978
        new_path = os.path.join(dir, new_tree_path)
 
979
        old_path = changed_inventory.get(entry.id)
 
980
        if bzrlib.osutils.lexists(new_path):
 
981
            if conflict_handler.target_exists(entry, new_path, old_path) == \
 
982
                "skip":
 
983
                continue
 
984
        if entry.is_creation(reverse):
 
985
            entry.apply(new_path, conflict_handler, reverse)
 
986
            changed_inventory[entry.id] = new_tree_path
 
987
        elif entry.needs_rename():
 
988
            if old_path is None:
 
989
                continue
 
990
            try:
 
991
                mutter('rename %s to final name %s', old_path, new_path)
 
992
                rename(old_path, new_path)
 
993
                changed_inventory[entry.id] = new_tree_path
 
994
            except OSError, e:
 
995
                raise BzrCheckError('failed to rename %s to %s for changeset entry %s: %s'
 
996
                        % (old_path, new_path, entry, e))
 
997
 
 
998
class TargetExists(Exception):
 
999
    def __init__(self, entry, target):
 
1000
        msg = "The path %s already exists" % target
 
1001
        Exception.__init__(self, msg)
 
1002
        self.entry = entry
 
1003
        self.target = target
 
1004
 
 
1005
class RenameConflict(Exception):
 
1006
    def __init__(self, id, this_name, base_name, other_name):
 
1007
        msg = """Trees all have different names for a file
 
1008
 this: %s
 
1009
 base: %s
 
1010
other: %s
 
1011
   id: %s""" % (this_name, base_name, other_name, id)
 
1012
        Exception.__init__(self, msg)
 
1013
        self.this_name = this_name
 
1014
        self.base_name = base_name
 
1015
        self_other_name = other_name
 
1016
 
 
1017
class MoveConflict(Exception):
 
1018
    def __init__(self, id, this_parent, base_parent, other_parent):
 
1019
        msg = """The file is in different directories in every tree
 
1020
 this: %s
 
1021
 base: %s
 
1022
other: %s
 
1023
   id: %s""" % (this_parent, base_parent, other_parent, id)
 
1024
        Exception.__init__(self, msg)
 
1025
        self.this_parent = this_parent
 
1026
        self.base_parent = base_parent
 
1027
        self_other_parent = other_parent
 
1028
 
 
1029
class MergeConflict(Exception):
 
1030
    def __init__(self, this_path):
 
1031
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
 
1032
        self.this_path = this_path
 
1033
 
 
1034
class WrongOldContents(Exception):
 
1035
    def __init__(self, filename):
 
1036
        msg = "Contents mismatch deleting %s" % filename
 
1037
        self.filename = filename
 
1038
        Exception.__init__(self, msg)
 
1039
 
 
1040
class WrongOldExecFlag(Exception):
 
1041
    def __init__(self, filename, old_exec_flag, new_exec_flag):
 
1042
        msg = "Executable flag missmatch on %s:\n" \
 
1043
        "Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
 
1044
        self.filename = filename
 
1045
        Exception.__init__(self, msg)
 
1046
 
 
1047
class RemoveContentsConflict(Exception):
 
1048
    def __init__(self, filename):
 
1049
        msg = "Conflict deleting %s, which has different contents in BASE"\
 
1050
            " and THIS" % filename
 
1051
        self.filename = filename
 
1052
        Exception.__init__(self, msg)
 
1053
 
 
1054
class DeletingNonEmptyDirectory(Exception):
 
1055
    def __init__(self, filename):
 
1056
        msg = "Trying to remove dir %s while it still had files" % filename
 
1057
        self.filename = filename
 
1058
        Exception.__init__(self, msg)
 
1059
 
 
1060
 
 
1061
class PatchTargetMissing(Exception):
 
1062
    def __init__(self, filename):
 
1063
        msg = "Attempt to patch %s, which does not exist" % filename
 
1064
        Exception.__init__(self, msg)
 
1065
        self.filename = filename
 
1066
 
 
1067
class MissingForSetExec(Exception):
 
1068
    def __init__(self, filename):
 
1069
        msg = "Attempt to change permissions on  %s, which does not exist" %\
 
1070
            filename
 
1071
        Exception.__init__(self, msg)
 
1072
        self.filename = filename
 
1073
 
 
1074
class MissingForRm(Exception):
 
1075
    def __init__(self, filename):
 
1076
        msg = "Attempt to remove missing path %s" % filename
 
1077
        Exception.__init__(self, msg)
 
1078
        self.filename = filename
 
1079
 
 
1080
 
 
1081
class MissingForRename(Exception):
 
1082
    def __init__(self, filename, to_path):
 
1083
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
 
1084
        Exception.__init__(self, msg)
 
1085
        self.filename = filename
 
1086
 
 
1087
class NewContentsConflict(Exception):
 
1088
    def __init__(self, filename):
 
1089
        msg = "Conflicting contents for new file %s" % (filename)
 
1090
        Exception.__init__(self, msg)
 
1091
 
 
1092
class WeaveMergeConflict(Exception):
 
1093
    def __init__(self, filename):
 
1094
        msg = "Conflicting contents for file %s" % (filename)
 
1095
        Exception.__init__(self, msg)
 
1096
 
 
1097
class ThreewayContentsConflict(Exception):
 
1098
    def __init__(self, filename):
 
1099
        msg = "Conflicting contents for file %s" % (filename)
 
1100
        Exception.__init__(self, msg)
 
1101
 
 
1102
 
 
1103
class MissingForMerge(Exception):
 
1104
    def __init__(self, filename):
 
1105
        msg = "The file %s was modified, but does not exist in this tree"\
 
1106
            % (filename)
 
1107
        Exception.__init__(self, msg)
 
1108
 
 
1109
 
 
1110
class ExceptionConflictHandler(object):
 
1111
    """Default handler for merge exceptions.
 
1112
 
 
1113
    This throws an error on any kind of conflict.  Conflict handlers can
 
1114
    descend from this class if they have a better way to handle some or
 
1115
    all types of conflict.
 
1116
    """
 
1117
    def missing_parent(self, pathname):
 
1118
        parent = os.path.dirname(pathname)
 
1119
        raise Exception("Parent directory missing for %s" % pathname)
 
1120
 
 
1121
    def dir_exists(self, pathname):
 
1122
        raise Exception("Directory already exists for %s" % pathname)
 
1123
 
 
1124
    def failed_hunks(self, pathname):
 
1125
        raise Exception("Failed to apply some hunks for %s" % pathname)
 
1126
 
 
1127
    def target_exists(self, entry, target, old_path):
 
1128
        raise TargetExists(entry, target)
 
1129
 
 
1130
    def rename_conflict(self, id, this_name, base_name, other_name):
 
1131
        raise RenameConflict(id, this_name, base_name, other_name)
 
1132
 
 
1133
    def move_conflict(self, id, this_dir, base_dir, other_dir):
 
1134
        raise MoveConflict(id, this_dir, base_dir, other_dir)
 
1135
 
 
1136
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
 
1137
        os.unlink(new_file)
 
1138
        raise MergeConflict(this_path)
 
1139
 
 
1140
    def wrong_old_contents(self, filename, expected_contents):
 
1141
        raise WrongOldContents(filename)
 
1142
 
 
1143
    def rem_contents_conflict(self, filename, this_contents, base_contents):
 
1144
        raise RemoveContentsConflict(filename)
 
1145
 
 
1146
    def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
 
1147
        raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
 
1148
 
 
1149
    def rmdir_non_empty(self, filename):
 
1150
        raise DeletingNonEmptyDirectory(filename)
 
1151
 
 
1152
    def link_name_exists(self, filename):
 
1153
        raise TargetExists(filename)
 
1154
 
 
1155
    def patch_target_missing(self, filename, contents):
 
1156
        raise PatchTargetMissing(filename)
 
1157
 
 
1158
    def missing_for_exec_flag(self, filename):
 
1159
        raise MissingForExecFlag(filename)
 
1160
 
 
1161
    def missing_for_rm(self, filename, change):
 
1162
        raise MissingForRm(filename)
 
1163
 
 
1164
    def missing_for_rename(self, filename, to_path):
 
1165
        raise MissingForRename(filename, to_path)
 
1166
 
 
1167
    def missing_for_merge(self, file_id, other_path):
 
1168
        raise MissingForMerge(other_path)
 
1169
 
 
1170
    def new_contents_conflict(self, filename, other_contents):
 
1171
        raise NewContentsConflict(filename)
 
1172
 
 
1173
    def weave_merge_conflict(self, filename, weave, other_i, out_file):
 
1174
        raise WeaveMergeConflict(filename)
 
1175
 
 
1176
    def threeway_contents_conflict(self, filename, this_contents,
 
1177
                                   base_contents, other_contents):
 
1178
        raise ThreewayContentsConflict(filename)
 
1179
 
 
1180
    def finalize(self):
 
1181
        pass
 
1182
 
 
1183
def apply_changeset(changeset, inventory, dir, conflict_handler=None, 
 
1184
                    reverse=False):
 
1185
    """Apply a changeset to a directory.
 
1186
 
 
1187
    :param changeset: The changes to perform
 
1188
    :type changeset: `Changeset`
 
1189
    :param inventory: The mapping of id to filename for the directory
 
1190
    :type inventory: Dictionary
 
1191
    :param dir: The path of the directory to apply the changes to
 
1192
    :type dir: str
 
1193
    :param reverse: If true, apply the changes in reverse
 
1194
    :type reverse: bool
 
1195
    :return: The mapping of the changed entries
 
1196
    :rtype: Dictionary
 
1197
    """
 
1198
    if conflict_handler is None:
 
1199
        conflict_handler = ExceptionConflictHandler()
 
1200
    temp_dir = os.path.join(dir, "bzr-tree-change")
68
1201
    try:
69
 
        bytef, transport = do_catching_redirections(get_bundle, transport,
70
 
                                                    redirected_transport)
71
 
    except errors.TooManyRedirections:
72
 
        raise errors.NotABundle(transport.clone(filename).base)
73
 
    except (errors.ConnectionReset, errors.ConnectionError), e:
74
 
        raise
75
 
    except (errors.TransportError, errors.PathError), e:
76
 
        raise errors.NotABundle(str(e))
77
 
    except (IOError,), e:
78
 
        # jam 20060707
79
 
        # Abstraction leakage, SFTPTransport.get('directory')
80
 
        # doesn't always fail at get() time. Sometimes it fails
81
 
        # during read. And that raises a generic IOError with
82
 
        # just the string 'Failure'
83
 
        # StubSFTPServer does fail during get() (because of prefetch)
84
 
        # so it has an opportunity to translate the error.
85
 
        raise errors.NotABundle(str(e))
86
 
 
87
 
    if _do_directive:
88
 
        try:
89
 
            return MergeDirective.from_lines(bytef), transport
90
 
        except errors.NotAMergeDirective:
91
 
            bytef.seek(0)
92
 
 
93
 
    return _serializer.read_bundle(bytef), transport
 
1202
        os.mkdir(temp_dir)
 
1203
    except OSError, e:
 
1204
        if e.errno == errno.EEXIST:
 
1205
            try:
 
1206
                os.rmdir(temp_dir)
 
1207
            except OSError, e:
 
1208
                if e.errno == errno.ENOTEMPTY:
 
1209
                    raise OldFailedTreeOp()
 
1210
            os.mkdir(temp_dir)
 
1211
        else:
 
1212
            raise
 
1213
    
 
1214
    #apply changes that don't affect filenames
 
1215
    for entry in changeset.entries.itervalues():
 
1216
        if not entry.is_creation_or_deletion() and not entry.is_boring():
 
1217
            if entry.id not in inventory:
 
1218
                warning("entry {%s} no longer present, can't be updated",
 
1219
                        entry.id)
 
1220
                continue
 
1221
            path = os.path.join(dir, inventory[entry.id])
 
1222
            entry.apply(path, conflict_handler, reverse)
 
1223
 
 
1224
    # Apply renames in stages, to minimize conflicts:
 
1225
    # Only files whose name or parent change are interesting, because their
 
1226
    # target name may exist in the source tree.  If a directory's name changes,
 
1227
    # that doesn't make its children interesting.
 
1228
    (source_entries, target_entries) = get_rename_entries(changeset, inventory,
 
1229
                                                          reverse)
 
1230
 
 
1231
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
 
1232
                                              temp_dir, conflict_handler,
 
1233
                                              reverse)
 
1234
 
 
1235
    rename_to_new_create(changed_inventory, target_entries, inventory,
 
1236
                         changeset, dir, conflict_handler, reverse)
 
1237
    os.rmdir(temp_dir)
 
1238
    return changed_inventory
 
1239
 
 
1240
 
 
1241
def apply_changeset_tree(cset, tree, reverse=False):
 
1242
    r_inventory = {}
 
1243
    for entry in tree.source_inventory().itervalues():
 
1244
        inventory[entry.id] = entry.path
 
1245
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
 
1246
                                    reverse=reverse)
 
1247
    new_entries, remove_entries = \
 
1248
        get_inventory_change(inventory, new_inventory, cset, reverse)
 
1249
    tree.update_source_inventory(new_entries, remove_entries)
 
1250
 
 
1251
 
 
1252
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
 
1253
    new_entries = {}
 
1254
    remove_entries = []
 
1255
    for entry in cset.entries.itervalues():
 
1256
        if entry.needs_rename():
 
1257
            new_path = entry.get_new_path(inventory, cset)
 
1258
            if new_path is None:
 
1259
                remove_entries.append(entry.id)
 
1260
            else:
 
1261
                new_entries[new_path] = entry.id
 
1262
    return new_entries, remove_entries
 
1263
 
 
1264
 
 
1265
def print_changeset(cset):
 
1266
    """Print all non-boring changeset entries
 
1267
    
 
1268
    :param cset: The changeset to print
 
1269
    :type cset: `Changeset`
 
1270
    """
 
1271
    for entry in cset.entries.itervalues():
 
1272
        if entry.is_boring():
 
1273
            continue
 
1274
        print entry.id
 
1275
        print entry.summarize_name(cset)
 
1276
 
 
1277
class CompositionFailure(Exception):
 
1278
    def __init__(self, old_entry, new_entry, problem):
 
1279
        msg = "Unable to conpose entries.\n %s" % problem
 
1280
        Exception.__init__(self, msg)
 
1281
 
 
1282
class IDMismatch(CompositionFailure):
 
1283
    def __init__(self, old_entry, new_entry):
 
1284
        problem = "Attempt to compose entries with different ids: %s and %s" %\
 
1285
            (old_entry.id, new_entry.id)
 
1286
        CompositionFailure.__init__(self, old_entry, new_entry, problem)
 
1287
 
 
1288
def compose_changesets(old_cset, new_cset):
 
1289
    """Combine two changesets into one.  This works well for exact patching.
 
1290
    Otherwise, not so well.
 
1291
 
 
1292
    :param old_cset: The first changeset that would be applied
 
1293
    :type old_cset: `Changeset`
 
1294
    :param new_cset: The second changeset that would be applied
 
1295
    :type new_cset: `Changeset`
 
1296
    :return: A changeset that combines the changes in both changesets
 
1297
    :rtype: `Changeset`
 
1298
    """
 
1299
    composed = Changeset()
 
1300
    for old_entry in old_cset.entries.itervalues():
 
1301
        new_entry = new_cset.entries.get(old_entry.id)
 
1302
        if new_entry is None:
 
1303
            composed.add_entry(old_entry)
 
1304
        else:
 
1305
            composed_entry = compose_entries(old_entry, new_entry)
 
1306
            if composed_entry.parent is not None or\
 
1307
                composed_entry.new_parent is not None:
 
1308
                composed.add_entry(composed_entry)
 
1309
    for new_entry in new_cset.entries.itervalues():
 
1310
        if not old_cset.entries.has_key(new_entry.id):
 
1311
            composed.add_entry(new_entry)
 
1312
    return composed
 
1313
 
 
1314
def compose_entries(old_entry, new_entry):
 
1315
    """Combine two entries into one.
 
1316
 
 
1317
    :param old_entry: The first entry that would be applied
 
1318
    :type old_entry: ChangesetEntry
 
1319
    :param old_entry: The second entry that would be applied
 
1320
    :type old_entry: ChangesetEntry
 
1321
    :return: A changeset entry combining both entries
 
1322
    :rtype: `ChangesetEntry`
 
1323
    """
 
1324
    if old_entry.id != new_entry.id:
 
1325
        raise IDMismatch(old_entry, new_entry)
 
1326
    output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
 
1327
 
 
1328
    if (old_entry.parent != old_entry.new_parent or 
 
1329
        new_entry.parent != new_entry.new_parent):
 
1330
        output.new_parent = new_entry.new_parent
 
1331
 
 
1332
    if (old_entry.path != old_entry.new_path or 
 
1333
        new_entry.path != new_entry.new_path):
 
1334
        output.new_path = new_entry.new_path
 
1335
 
 
1336
    output.contents_change = compose_contents(old_entry, new_entry)
 
1337
    output.metadata_change = compose_metadata(old_entry, new_entry)
 
1338
    return output
 
1339
 
 
1340
def compose_contents(old_entry, new_entry):
 
1341
    """Combine the contents of two changeset entries.  Entries are combined
 
1342
    intelligently where possible, but the fallback behavior returns an 
 
1343
    ApplySequence.
 
1344
 
 
1345
    :param old_entry: The first entry that would be applied
 
1346
    :type old_entry: `ChangesetEntry`
 
1347
    :param new_entry: The second entry that would be applied
 
1348
    :type new_entry: `ChangesetEntry`
 
1349
    :return: A combined contents change
 
1350
    :rtype: anything supporting the apply(reverse=False) method
 
1351
    """
 
1352
    old_contents = old_entry.contents_change
 
1353
    new_contents = new_entry.contents_change
 
1354
    if old_entry.contents_change is None:
 
1355
        return new_entry.contents_change
 
1356
    elif new_entry.contents_change is None:
 
1357
        return old_entry.contents_change
 
1358
    elif isinstance(old_contents, ReplaceContents) and \
 
1359
        isinstance(new_contents, ReplaceContents):
 
1360
        if old_contents.old_contents == new_contents.new_contents:
 
1361
            return None
 
1362
        else:
 
1363
            return ReplaceContents(old_contents.old_contents,
 
1364
                                   new_contents.new_contents)
 
1365
    elif isinstance(old_contents, ApplySequence):
 
1366
        output = ApplySequence(old_contents.changes)
 
1367
        if isinstance(new_contents, ApplySequence):
 
1368
            output.changes.extend(new_contents.changes)
 
1369
        else:
 
1370
            output.changes.append(new_contents)
 
1371
        return output
 
1372
    elif isinstance(new_contents, ApplySequence):
 
1373
        output = ApplySequence((old_contents.changes,))
 
1374
        output.extend(new_contents.changes)
 
1375
        return output
 
1376
    else:
 
1377
        return ApplySequence((old_contents, new_contents))
 
1378
 
 
1379
def compose_metadata(old_entry, new_entry):
 
1380
    old_meta = old_entry.metadata_change
 
1381
    new_meta = new_entry.metadata_change
 
1382
    if old_meta is None:
 
1383
        return new_meta
 
1384
    elif new_meta is None:
 
1385
        return old_meta
 
1386
    elif (isinstance(old_meta, ChangeExecFlag) and
 
1387
          isinstance(new_meta, ChangeExecFlag)):
 
1388
        return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
 
1389
    else:
 
1390
        return ApplySequence(old_meta, new_meta)
 
1391
 
 
1392
 
 
1393
def changeset_is_null(changeset):
 
1394
    for entry in changeset.entries.itervalues():
 
1395
        if not entry.is_boring():
 
1396
            return False
 
1397
    return True
 
1398
 
 
1399
class UnsupportedFiletype(Exception):
 
1400
    def __init__(self, kind, full_path):
 
1401
        msg = "The file \"%s\" is a %s, which is not a supported filetype." \
 
1402
            % (full_path, kind)
 
1403
        Exception.__init__(self, msg)
 
1404
        self.full_path = full_path
 
1405
        self.kind = kind
 
1406
 
 
1407
def generate_changeset(tree_a, tree_b, interesting_ids=None):
 
1408
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
 
1409
 
 
1410
 
 
1411
class ChangesetGenerator(object):
 
1412
    def __init__(self, tree_a, tree_b, interesting_ids=None):
 
1413
        object.__init__(self)
 
1414
        self.tree_a = tree_a
 
1415
        self.tree_b = tree_b
 
1416
        self._interesting_ids = interesting_ids
 
1417
 
 
1418
    def iter_both_tree_ids(self):
 
1419
        for file_id in self.tree_a:
 
1420
            yield file_id
 
1421
        for file_id in self.tree_b:
 
1422
            if file_id not in self.tree_a:
 
1423
                yield file_id
 
1424
 
 
1425
    def __call__(self):
 
1426
        cset = Changeset()
 
1427
        for file_id in self.iter_both_tree_ids():
 
1428
            cs_entry = self.make_entry(file_id)
 
1429
            if cs_entry is not None and not cs_entry.is_boring():
 
1430
                cset.add_entry(cs_entry)
 
1431
 
 
1432
        for entry in list(cset.entries.itervalues()):
 
1433
            if entry.parent != entry.new_parent:
 
1434
                if not cset.entries.has_key(entry.parent) and\
 
1435
                    entry.parent != NULL_ID and entry.parent is not None:
 
1436
                    parent_entry = self.make_boring_entry(entry.parent)
 
1437
                    cset.add_entry(parent_entry)
 
1438
                if not cset.entries.has_key(entry.new_parent) and\
 
1439
                    entry.new_parent != NULL_ID and \
 
1440
                    entry.new_parent is not None:
 
1441
                    parent_entry = self.make_boring_entry(entry.new_parent)
 
1442
                    cset.add_entry(parent_entry)
 
1443
        return cset
 
1444
 
 
1445
    def iter_inventory(self, tree):
 
1446
        for file_id in tree:
 
1447
            yield self.get_entry(file_id, tree)
 
1448
 
 
1449
    def get_entry(self, file_id, tree):
 
1450
        if not tree.has_or_had_id(file_id):
 
1451
            return None
 
1452
        return tree.inventory[file_id]
 
1453
 
 
1454
    def get_entry_parent(self, entry):
 
1455
        if entry is None:
 
1456
            return None
 
1457
        return entry.parent_id
 
1458
 
 
1459
    def get_path(self, file_id, tree):
 
1460
        if not tree.has_or_had_id(file_id):
 
1461
            return None
 
1462
        path = tree.id2path(file_id)
 
1463
        if path == '':
 
1464
            return './.'
 
1465
        else:
 
1466
            return path
 
1467
 
 
1468
    def make_basic_entry(self, file_id, only_interesting):
 
1469
        entry_a = self.get_entry(file_id, self.tree_a)
 
1470
        entry_b = self.get_entry(file_id, self.tree_b)
 
1471
        if only_interesting and not self.is_interesting(entry_a, entry_b):
 
1472
            return None
 
1473
        parent = self.get_entry_parent(entry_a)
 
1474
        path = self.get_path(file_id, self.tree_a)
 
1475
        cs_entry = ChangesetEntry(file_id, parent, path)
 
1476
        new_parent = self.get_entry_parent(entry_b)
 
1477
 
 
1478
        new_path = self.get_path(file_id, self.tree_b)
 
1479
 
 
1480
        cs_entry.new_path = new_path
 
1481
        cs_entry.new_parent = new_parent
 
1482
        return cs_entry
 
1483
 
 
1484
    def is_interesting(self, entry_a, entry_b):
 
1485
        if self._interesting_ids is None:
 
1486
            return True
 
1487
        if entry_a is not None:
 
1488
            file_id = entry_a.file_id
 
1489
        elif entry_b is not None:
 
1490
            file_id = entry_b.file_id
 
1491
        else:
 
1492
            return False
 
1493
        return file_id in self._interesting_ids
 
1494
 
 
1495
    def make_boring_entry(self, id):
 
1496
        cs_entry = self.make_basic_entry(id, only_interesting=False)
 
1497
        if cs_entry.is_creation_or_deletion():
 
1498
            return self.make_entry(id, only_interesting=False)
 
1499
        else:
 
1500
            return cs_entry
 
1501
        
 
1502
 
 
1503
    def make_entry(self, id, only_interesting=True):
 
1504
        cs_entry = self.make_basic_entry(id, only_interesting)
 
1505
 
 
1506
        if cs_entry is None:
 
1507
            return None
 
1508
 
 
1509
        cs_entry.metadata_change = self.make_exec_flag_change(id)
 
1510
 
 
1511
        if id in self.tree_a and id in self.tree_b:
 
1512
            a_sha1 = self.tree_a.get_file_sha1(id)
 
1513
            b_sha1 = self.tree_b.get_file_sha1(id)
 
1514
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
 
1515
                return cs_entry
 
1516
 
 
1517
        cs_entry.contents_change = self.make_contents_change(id)
 
1518
        return cs_entry
 
1519
 
 
1520
    def make_exec_flag_change(self, file_id):
 
1521
        exec_flag_a = exec_flag_b = None
 
1522
        if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
 
1523
            exec_flag_a = self.tree_a.is_executable(file_id)
 
1524
 
 
1525
        if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
 
1526
            exec_flag_b = self.tree_b.is_executable(file_id)
 
1527
 
 
1528
        if exec_flag_a == exec_flag_b:
 
1529
            return None
 
1530
        return ChangeExecFlag(exec_flag_a, exec_flag_b)
 
1531
 
 
1532
    def make_contents_change(self, file_id):
 
1533
        a_contents = get_contents(self.tree_a, file_id)
 
1534
        b_contents = get_contents(self.tree_b, file_id)
 
1535
        if a_contents == b_contents:
 
1536
            return None
 
1537
        return ReplaceContents(a_contents, b_contents)
 
1538
 
 
1539
 
 
1540
def get_contents(tree, file_id):
 
1541
    """Return the appropriate contents to create a copy of file_id from tree"""
 
1542
    if file_id not in tree:
 
1543
        return None
 
1544
    kind = tree.kind(file_id)
 
1545
    if kind == "file":
 
1546
        return TreeFileCreate(tree, file_id)
 
1547
    elif kind in ("directory", "root_directory"):
 
1548
        return dir_create
 
1549
    elif kind == "symlink":
 
1550
        return SymlinkCreate(tree.get_symlink_target(file_id))
 
1551
    else:
 
1552
        raise UnsupportedFiletype(kind, tree.id2path(file_id))
 
1553
 
 
1554
 
 
1555
def full_path(entry, tree):
 
1556
    return os.path.join(tree.basedir, entry.path)
 
1557
 
 
1558
def new_delete_entry(entry, tree, inventory, delete):
 
1559
    if entry.path == "":
 
1560
        parent = NULL_ID
 
1561
    else:
 
1562
        parent = inventory[dirname(entry.path)].id
 
1563
    cs_entry = ChangesetEntry(parent, entry.path)
 
1564
    if delete:
 
1565
        cs_entry.new_path = None
 
1566
        cs_entry.new_parent = None
 
1567
    else:
 
1568
        cs_entry.path = None
 
1569
        cs_entry.parent = None
 
1570
    full_path = full_path(entry, tree)
 
1571
    status = os.lstat(full_path)
 
1572
    if stat.S_ISDIR(file_stat.st_mode):
 
1573
        action = dir_create
 
1574
    
 
1575
 
 
1576
 
 
1577
        
 
1578
# XXX: Can't we unify this with the regular inventory object
 
1579
class Inventory(object):
 
1580
    def __init__(self, inventory):
 
1581
        self.inventory = inventory
 
1582
        self.rinventory = None
 
1583
 
 
1584
    def get_rinventory(self):
 
1585
        if self.rinventory is None:
 
1586
            self.rinventory  = invert_dict(self.inventory)
 
1587
        return self.rinventory
 
1588
 
 
1589
    def get_path(self, id):
 
1590
        return self.inventory.get(id)
 
1591
 
 
1592
    def get_name(self, id):
 
1593
        path = self.get_path(id)
 
1594
        if path is None:
 
1595
            return None
 
1596
        else:
 
1597
            return os.path.basename(path)
 
1598
 
 
1599
    def get_dir(self, id):
 
1600
        path = self.get_path(id)
 
1601
        if path == "":
 
1602
            return None
 
1603
        if path is None:
 
1604
            return None
 
1605
        return os.path.dirname(path)
 
1606
 
 
1607
    def get_parent(self, id):
 
1608
        if self.get_path(id) is None:
 
1609
            return None
 
1610
        directory = self.get_dir(id)
 
1611
        if directory == '.':
 
1612
            directory = './.'
 
1613
        if directory is None:
 
1614
            return NULL_ID
 
1615
        return self.get_rinventory().get(directory)