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