~bzr-pqm/bzr/bzr.dev

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