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