~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-05-17 06:56:16 UTC
  • Revision ID: mbp@sourcefrog.net-20050517065616-6f23381d6184a8aa
- add space for un-merged patches

Show diffs side-by-side

added added

removed removed

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