/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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 shutil import rmtree
1185.33.26 by Martin Pool
Give a warning, not exception, when trying to merge
29
from itertools import izip
30
31
from bzrlib.trace import mutter, warning
1185.31.40 by John Arbash Meinel
Added osutils.mkdtemp()
32
from bzrlib.osutils import rename, sha_file, pathjoin, mkdtemp
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
33
import bzrlib
1185.33.46 by Martin Pool
Better exception for one unhandled merge case
34
from bzrlib.errors import BzrCheckError
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
35
493 by Martin Pool
- Merge aaron's merge command
36
__docformat__ = "restructuredtext"
37
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
38
493 by Martin Pool
- Merge aaron's merge command
39
NULL_ID = "!NULL"
40
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
41
850 by Martin Pool
- Merge merge updates from aaron
42
class OldFailedTreeOp(Exception):
43
    def __init__(self):
44
        Exception.__init__(self, "bzr-tree-change contains files from a"
45
                           " previous failed merge operation.")
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
46
47
493 by Martin Pool
- Merge aaron's merge command
48
def invert_dict(dict):
49
    newdict = {}
50
    for (key,value) in dict.iteritems():
51
        newdict[value] = key
52
    return newdict
53
974.1.15 by aaron.bentley at utoronto
Removed use of patch and diff in merge, removed patch.diff
54
       
1434 by Robert Collins
merge Gustavos executable2 patch
55
class ChangeExecFlag(object):
493 by Martin Pool
- Merge aaron's merge command
56
    """This is two-way change, suitable for file modification, creation,
57
    deletion"""
1434 by Robert Collins
merge Gustavos executable2 patch
58
    def __init__(self, old_exec_flag, new_exec_flag):
59
        self.old_exec_flag = old_exec_flag
60
        self.new_exec_flag = new_exec_flag
493 by Martin Pool
- Merge aaron's merge command
61
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
62
    def apply(self, filename, conflict_handler):
63
        from_exec_flag = self.old_exec_flag
64
        to_exec_flag = self.new_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
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
107
def dir_create(filename, conflict_handler, reverse=False):
493 by Martin Pool
- Merge aaron's merge command
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
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
152
    def __call__(self, filename, conflict_handler, reverse=False):
493 by Martin Pool
- Merge aaron's merge command
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
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
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
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
206
    def __call__(self, filename, conflict_handler, reverse=False):
493 by Martin Pool
- Merge aaron's merge command
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
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
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
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
271
    def __call__(self, filename, conflict_handler, reverse=False):
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
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
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
303
558 by Martin Pool
- All top-level classes inherit from object
304
class ReplaceContents(object):
493 by Martin Pool
- Merge aaron's merge command
305
    """A contents-replacement framework.  It allows a file/directory/symlink to
306
    be created, deleted, or replaced with another file/directory/symlink.
307
    Arguments must be callable with (filename, reverse).
308
    """
309
    def __init__(self, old_contents, new_contents):
310
        """Constructor.
311
312
        :param old_contents: The change to reverse apply (e.g. a deletion), \
313
        when going forwards.
314
        :type old_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
315
        NoneType, etc.
316
        :param new_contents: The second change to apply (e.g. a creation), \
317
        when going forwards.
318
        :type new_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
319
        NoneType, etc.
320
        """
321
        self.old_contents=old_contents
322
        self.new_contents=new_contents
323
324
    def __repr__(self):
325
        return "ReplaceContents(%r -> %r)" % (self.old_contents,
326
                                              self.new_contents)
327
328
    def __eq__(self, other):
329
        if not isinstance(other, ReplaceContents):
330
            return False
331
        elif self.old_contents != other.old_contents:
332
            return False
333
        elif self.new_contents != other.new_contents:
334
            return False
335
        else:
336
            return True
337
    def __ne__(self, other):
338
        return not (self == other)
339
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
340
    def apply(self, filename, conflict_handler):
493 by Martin Pool
- Merge aaron's merge command
341
        """Applies the FileReplacement to the specified filename
342
343
        :param filename: The name of the file to apply changes to
344
        :type filename: str
345
        """
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
346
        undo = self.old_contents
347
        perform = self.new_contents
493 by Martin Pool
- Merge aaron's merge command
348
        mode = None
349
        if undo is not None:
350
            try:
351
                mode = os.lstat(filename).st_mode
352
                if stat.S_ISLNK(mode):
353
                    mode = None
354
            except OSError, e:
355
                if e.errno != errno.ENOENT:
356
                    raise
357
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
358
                    return
359
            undo(filename, conflict_handler, reverse=True)
360
        if perform is not None:
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
361
            perform(filename, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
362
            if mode is not None:
363
                os.chmod(filename, mode)
364
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
365
    def is_creation(self):
366
        return self.new_contents is not None and self.old_contents is None
367
368
    def is_deletion(self):
369
        return self.old_contents is not None and self.new_contents is None
370
493 by Martin Pool
- Merge aaron's merge command
371
558 by Martin Pool
- All top-level classes inherit from object
372
class Diff3Merge(object):
1185.12.83 by Aaron Bentley
Preliminary weave merge support
373
    history_based = False
974.1.23 by Aaron Bentley
Avoided unnecessary temp files
374
    def __init__(self, file_id, base, other):
375
        self.file_id = file_id
376
        self.base = base
377
        self.other = other
493 by Martin Pool
- Merge aaron's merge command
378
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
379
    def is_creation(self):
380
        return False
381
382
    def is_deletion(self):
383
        return False
384
493 by Martin Pool
- Merge aaron's merge command
385
    def __eq__(self, other):
386
        if not isinstance(other, Diff3Merge):
387
            return False
974.1.23 by Aaron Bentley
Avoided unnecessary temp files
388
        return (self.base == other.base and 
389
                self.other == other.other and self.file_id == other.file_id)
493 by Martin Pool
- Merge aaron's merge command
390
391
    def __ne__(self, other):
392
        return not (self == other)
393
1185.12.35 by abentley
Avoided readonly_path in ApplyDiff3
394
    def dump_file(self, temp_dir, name, tree):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
395
        out_path = pathjoin(temp_dir, name)
1185.12.35 by abentley
Avoided readonly_path in ApplyDiff3
396
        out_file = file(out_path, "wb")
397
        in_file = tree.get_file(self.file_id)
398
        for line in in_file:
399
            out_file.write(line)
400
        return out_path
401
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
402
    def apply(self, filename, conflict_handler):
1185.33.26 by Martin Pool
Give a warning, not exception, when trying to merge
403
        import bzrlib.patch
1185.12.35 by abentley
Avoided readonly_path in ApplyDiff3
404
        temp_dir = mkdtemp(prefix="bzr-")
405
        try:
406
            new_file = filename+".new"
407
            base_file = self.dump_file(temp_dir, "base", self.base)
408
            other_file = self.dump_file(temp_dir, "other", self.other)
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
409
            base = base_file
410
            other = other_file
1185.33.26 by Martin Pool
Give a warning, not exception, when trying to merge
411
            status = bzrlib.patch.diff3(new_file, filename, base, other)
1185.12.35 by abentley
Avoided readonly_path in ApplyDiff3
412
            if status == 0:
413
                os.chmod(new_file, os.stat(filename).st_mode)
414
                rename(new_file, filename)
415
                return
416
            else:
417
                assert(status == 1)
418
                def get_lines(filename):
419
                    my_file = file(filename, "rb")
420
                    lines = my_file.readlines()
421
                    my_file.close()
422
                    return lines
423
                base_lines = get_lines(base)
424
                other_lines = get_lines(other)
425
                conflict_handler.merge_conflict(new_file, filename, base_lines, 
426
                                                other_lines)
427
        finally:
428
            rmtree(temp_dir)
493 by Martin Pool
- Merge aaron's merge command
429
430
431
def CreateDir():
432
    """Convenience function to create a directory.
433
434
    :return: A ReplaceContents that will create a directory
435
    :rtype: `ReplaceContents`
436
    """
437
    return ReplaceContents(None, dir_create)
438
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
439
493 by Martin Pool
- Merge aaron's merge command
440
def DeleteDir():
441
    """Convenience function to delete a directory.
442
443
    :return: A ReplaceContents that will delete a directory
444
    :rtype: `ReplaceContents`
445
    """
446
    return ReplaceContents(dir_create, None)
447
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
448
493 by Martin Pool
- Merge aaron's merge command
449
def CreateFile(contents):
450
    """Convenience fucntion to create a file.
451
    
452
    :param contents: The contents of the file to create 
453
    :type contents: str
454
    :return: A ReplaceContents that will create a file 
455
    :rtype: `ReplaceContents`
456
    """
457
    return ReplaceContents(None, FileCreate(contents))
458
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
459
493 by Martin Pool
- Merge aaron's merge command
460
def DeleteFile(contents):
461
    """Convenience fucntion to delete a file.
462
    
463
    :param contents: The contents of the file to delete
464
    :type contents: str
465
    :return: A ReplaceContents that will delete a file 
466
    :rtype: `ReplaceContents`
467
    """
468
    return ReplaceContents(FileCreate(contents), None)
469
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
470
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
471
def ReplaceFileContents(old_tree, new_tree, file_id):
493 by Martin Pool
- Merge aaron's merge command
472
    """Convenience fucntion to replace the contents of a file.
473
    
474
    :param old_contents: The contents of the file to replace 
475
    :type old_contents: str
476
    :param new_contents: The contents to replace the file with
477
    :type new_contents: str
478
    :return: A ReplaceContents that will replace the contents of a file a file 
479
    :rtype: `ReplaceContents`
480
    """
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
481
    return ReplaceContents(TreeFileCreate(old_tree, file_id), 
482
                           TreeFileCreate(new_tree, file_id))
493 by Martin Pool
- Merge aaron's merge command
483
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
484
493 by Martin Pool
- Merge aaron's merge command
485
def CreateSymlink(target):
486
    """Convenience fucntion to create a symlink.
487
    
488
    :param target: The path the link should point to
489
    :type target: str
490
    :return: A ReplaceContents that will delete a file 
491
    :rtype: `ReplaceContents`
492
    """
493
    return ReplaceContents(None, SymlinkCreate(target))
494
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
495
493 by Martin Pool
- Merge aaron's merge command
496
def DeleteSymlink(target):
497
    """Convenience fucntion to delete a symlink.
498
    
499
    :param target: The path the link should point to
500
    :type target: str
501
    :return: A ReplaceContents that will delete a file 
502
    :rtype: `ReplaceContents`
503
    """
504
    return ReplaceContents(SymlinkCreate(target), None)
505
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
506
493 by Martin Pool
- Merge aaron's merge command
507
def ChangeTarget(old_target, new_target):
508
    """Convenience fucntion to change the target of a symlink.
509
    
510
    :param old_target: The current link target
511
    :type old_target: str
512
    :param new_target: The new link target to use
513
    :type new_target: str
514
    :return: A ReplaceContents that will delete a file 
515
    :rtype: `ReplaceContents`
516
    """
517
    return ReplaceContents(SymlinkCreate(old_target), SymlinkCreate(new_target))
518
519
520
class InvalidEntry(Exception):
521
    """Raise when a ChangesetEntry is invalid in some way"""
522
    def __init__(self, entry, problem):
523
        """Constructor.
524
525
        :param entry: The invalid ChangesetEntry
526
        :type entry: `ChangesetEntry`
527
        :param problem: The problem with the entry
528
        :type problem: str
529
        """
530
        msg = "Changeset entry for %s (%s) is invalid.\n%s" % (entry.id, 
531
                                                               entry.path, 
532
                                                               problem)
533
        Exception.__init__(self, msg)
534
        self.entry = entry
535
536
537
class SourceRootHasName(InvalidEntry):
538
    """This changeset entry has a name other than "", but its parent is !NULL"""
539
    def __init__(self, entry, name):
540
        """Constructor.
541
542
        :param entry: The invalid ChangesetEntry
543
        :type entry: `ChangesetEntry`
544
        :param name: The name of the entry
545
        :type name: str
546
        """
547
        msg = 'Child of !NULL is named "%s", not "./.".' % name
548
        InvalidEntry.__init__(self, entry, msg)
549
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
550
493 by Martin Pool
- Merge aaron's merge command
551
class NullIDAssigned(InvalidEntry):
552
    """The id !NULL was assigned to a real entry"""
553
    def __init__(self, entry):
554
        """Constructor.
555
556
        :param entry: The invalid ChangesetEntry
557
        :type entry: `ChangesetEntry`
558
        """
559
        msg = '"!NULL" id assigned to a file "%s".' % entry.path
560
        InvalidEntry.__init__(self, entry, msg)
561
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
562
493 by Martin Pool
- Merge aaron's merge command
563
class ParentIDIsSelf(InvalidEntry):
564
    """An entry is marked as its own parent"""
565
    def __init__(self, entry):
566
        """Constructor.
567
568
        :param entry: The invalid ChangesetEntry
569
        :type entry: `ChangesetEntry`
570
        """
571
        msg = 'file %s has "%s" id for both self id and parent id.' % \
572
            (entry.path, entry.id)
573
        InvalidEntry.__init__(self, entry, msg)
574
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
575
493 by Martin Pool
- Merge aaron's merge command
576
class ChangesetEntry(object):
577
    """An entry the changeset"""
578
    def __init__(self, id, parent, path):
579
        """Constructor. Sets parent and name assuming it was not
580
        renamed/created/deleted.
581
        :param id: The id associated with the entry
582
        :param parent: The id of the parent of this entry (or !NULL if no
583
        parent)
584
        :param path: The file path relative to the tree root of this entry
585
        """
586
        self.id = id
587
        self.path = path 
588
        self.new_path = path
589
        self.parent = parent
590
        self.new_parent = parent
591
        self.contents_change = None
592
        self.metadata_change = None
593
        if parent == NULL_ID and path !='./.':
594
            raise SourceRootHasName(self, path)
595
        if self.id == NULL_ID:
596
            raise NullIDAssigned(self)
597
        if self.id  == self.parent:
598
            raise ParentIDIsSelf(self)
599
1185.33.47 by Martin Pool
Add ChangesetEntry.__repr__
600
    def __repr__(self):
493 by Martin Pool
- Merge aaron's merge command
601
        return "ChangesetEntry(%s)" % self.id
602
1185.33.47 by Martin Pool
Add ChangesetEntry.__repr__
603
    __str__ = __repr__
604
493 by Martin Pool
- Merge aaron's merge command
605
    def __get_dir(self):
606
        if self.path is None:
607
            return None
608
        return os.path.dirname(self.path)
609
610
    def __set_dir(self, dir):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
611
        self.path = pathjoin(dir, os.path.basename(self.path))
493 by Martin Pool
- Merge aaron's merge command
612
613
    dir = property(__get_dir, __set_dir)
614
    
615
    def __get_name(self):
616
        if self.path is None:
617
            return None
618
        return os.path.basename(self.path)
619
620
    def __set_name(self, name):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
621
        self.path = pathjoin(os.path.dirname(self.path), name)
493 by Martin Pool
- Merge aaron's merge command
622
623
    name = property(__get_name, __set_name)
624
625
    def __get_new_dir(self):
626
        if self.new_path is None:
627
            return None
628
        return os.path.dirname(self.new_path)
629
630
    def __set_new_dir(self, dir):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
631
        self.new_path = pathjoin(dir, os.path.basename(self.new_path))
493 by Martin Pool
- Merge aaron's merge command
632
633
    new_dir = property(__get_new_dir, __set_new_dir)
634
635
    def __get_new_name(self):
636
        if self.new_path is None:
637
            return None
638
        return os.path.basename(self.new_path)
639
640
    def __set_new_name(self, name):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
641
        self.new_path = pathjoin(os.path.dirname(self.new_path), name)
493 by Martin Pool
- Merge aaron's merge command
642
643
    new_name = property(__get_new_name, __set_new_name)
644
645
    def needs_rename(self):
646
        """Determines whether the entry requires renaming.
647
648
        :rtype: bool
649
        """
1185.54.15 by Aaron Bentley
Removed merge fix with no test case
650
493 by Martin Pool
- Merge aaron's merge command
651
        return (self.parent != self.new_parent or self.name != self.new_name)
652
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
653
    def is_deletion(self, reverse=False):
493 by Martin Pool
- Merge aaron's merge command
654
        """Return true if applying the entry would delete a file/directory.
655
656
        :param reverse: if true, the changeset is being applied in reverse
657
        :rtype: bool
658
        """
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
659
        return self.is_creation(not reverse)
493 by Martin Pool
- Merge aaron's merge command
660
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
661
    def is_creation(self, reverse=False):
493 by Martin Pool
- Merge aaron's merge command
662
        """Return true if applying the entry would create a file/directory.
663
664
        :param reverse: if true, the changeset is being applied in reverse
665
        :rtype: bool
666
        """
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
667
        if self.contents_change is None:
668
            return False
669
        if reverse:
670
            return self.contents_change.is_deletion()
671
        else:
672
            return self.contents_change.is_creation()
493 by Martin Pool
- Merge aaron's merge command
673
674
    def is_creation_or_deletion(self):
675
        """Return true if applying the entry would create or delete a 
676
        file/directory.
677
678
        :rtype: bool
679
        """
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
680
        return self.is_creation() or self.is_deletion()
493 by Martin Pool
- Merge aaron's merge command
681
682
    def get_cset_path(self, mod=False):
683
        """Determine the path of the entry according to the changeset.
684
685
        :param changeset: The changeset to derive the path from
686
        :type changeset: `Changeset`
687
        :param mod: If true, generate the MOD path.  Otherwise, generate the \
688
        ORIG path.
689
        :return: the path of the entry, or None if it did not exist in the \
690
        requested tree.
691
        :rtype: str or NoneType
692
        """
693
        if mod:
694
            if self.new_parent == NULL_ID:
695
                return "./."
696
            elif self.new_parent is None:
697
                return None
698
            return self.new_path
699
        else:
700
            if self.parent == NULL_ID:
701
                return "./."
702
            elif self.parent is None:
703
                return None
704
            return self.path
705
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
706
    def summarize_name(self):
493 by Martin Pool
- Merge aaron's merge command
707
        """Produce a one-line summary of the filename.  Indicates renames as
708
        old => new, indicates creation as None => new, indicates deletion as
709
        old => None.
710
711
        :rtype: str
712
        """
713
        orig_path = self.get_cset_path(False)
714
        mod_path = self.get_cset_path(True)
1185.33.47 by Martin Pool
Add ChangesetEntry.__repr__
715
        if orig_path and orig_path.startswith('./'):
493 by Martin Pool
- Merge aaron's merge command
716
            orig_path = orig_path[2:]
1185.33.47 by Martin Pool
Add ChangesetEntry.__repr__
717
        if mod_path and mod_path.startswith('./'):
493 by Martin Pool
- Merge aaron's merge command
718
            mod_path = mod_path[2:]
719
        if orig_path == mod_path:
720
            return orig_path
721
        else:
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
722
            return "%s => %s" % (orig_path, mod_path)
723
724
    def get_new_path(self, id_map, changeset):
493 by Martin Pool
- Merge aaron's merge command
725
        """Determine the full pathname to rename to
726
727
        :param id_map: The map of ids to filenames for the tree
728
        :type id_map: Dictionary
729
        :param changeset: The changeset to get data from
730
        :type changeset: `Changeset`
731
        :rtype: str
732
        """
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
733
        mutter("Finding new path for %s", self.summarize_name())
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
734
        parent = self.new_parent
735
        to_dir = self.new_dir
736
        from_dir = self.dir
737
        to_name = self.new_name
738
        from_name = self.name
493 by Martin Pool
- Merge aaron's merge command
739
740
        if to_name is None:
741
            return None
742
743
        if parent == NULL_ID or parent is None:
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
744
            if to_name != u'.':
493 by Martin Pool
- Merge aaron's merge command
745
                raise SourceRootHasName(self, to_name)
746
            else:
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
747
                return u'.'
1185.45.2 by Aaron Bentley
Tweaked tree name generation when applying changesets
748
        parent_entry = changeset.entries.get(parent)
749
        if parent_entry is None:
493 by Martin Pool
- Merge aaron's merge command
750
            dir = os.path.dirname(id_map[self.id])
751
        else:
1185.31.4 by John Arbash Meinel
Fixing mutter() calls to not have to do string processing.
752
            mutter("path, new_path: %r %r", self.path, self.new_path)
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
753
            dir = parent_entry.get_new_path(id_map, changeset)
493 by Martin Pool
- Merge aaron's merge command
754
        if from_name == to_name:
755
            name = os.path.basename(id_map[self.id])
756
        else:
757
            name = to_name
758
            assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
759
        return pathjoin(dir, name)
493 by Martin Pool
- Merge aaron's merge command
760
761
    def is_boring(self):
762
        """Determines whether the entry does nothing
763
        
764
        :return: True if the entry does no renames or content changes
765
        :rtype: bool
766
        """
767
        if self.contents_change is not None:
768
            return False
769
        elif self.metadata_change is not None:
770
            return False
771
        elif self.parent != self.new_parent:
772
            return False
773
        elif self.name != self.new_name:
774
            return False
775
        else:
776
            return True
777
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
778
    def apply(self, filename, conflict_handler):
493 by Martin Pool
- Merge aaron's merge command
779
        """Applies the file content and/or metadata changes.
780
781
        :param filename: the filename of the entry
782
        :type filename: str
783
        """
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
784
        if self.is_deletion() and self.metadata_change is not None:
785
            self.metadata_change.apply(filename, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
786
        if self.contents_change is not None:
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
787
            self.contents_change.apply(filename, conflict_handler)
788
        if not self.is_deletion() and self.metadata_change is not None:
789
            self.metadata_change.apply(filename, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
790
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
791
493 by Martin Pool
- Merge aaron's merge command
792
class IDPresent(Exception):
793
    def __init__(self, id):
794
        msg = "Cannot add entry because that id has already been used:\n%s" %\
795
            id
796
        Exception.__init__(self, msg)
797
        self.id = id
798
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
799
558 by Martin Pool
- All top-level classes inherit from object
800
class Changeset(object):
493 by Martin Pool
- Merge aaron's merge command
801
    """A set of changes to apply"""
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
802
    def __init__(self, base_id=None, target_id=None):
803
        self.base_id = base_id
804
        self.target_id = target_id
493 by Martin Pool
- Merge aaron's merge command
805
        self.entries = {}
806
807
    def add_entry(self, entry):
808
        """Add an entry to the list of entries"""
809
        if self.entries.has_key(entry.id):
810
            raise IDPresent(entry.id)
811
        self.entries[entry.id] = entry
812
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
813
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
814
def get_rename_entries(changeset, inventory):
493 by Martin Pool
- Merge aaron's merge command
815
    """Return a list of entries that will be renamed.  Entries are sorted from
816
    longest to shortest source path and from shortest to longest target path.
817
818
    :param changeset: The changeset to look in
819
    :type changeset: `Changeset`
820
    :param inventory: The source of current tree paths for the given ids
821
    :type inventory: Dictionary
822
    :return: source entries and target entries as a tuple
823
    :rtype: (List, List)
824
    """
825
    source_entries = [x for x in changeset.entries.itervalues() 
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
826
                      if x.needs_rename() or x.is_creation_or_deletion()]
493 by Martin Pool
- Merge aaron's merge command
827
    # these are done from longest path to shortest, to avoid deleting a
828
    # parent before its children are deleted/renamed 
829
    def longest_to_shortest(entry):
830
        path = inventory.get(entry.id)
831
        if path is None:
832
            return 0
833
        else:
834
            return len(path)
1185.59.3 by Denys Duchier
sort with key and reverse is standard in Python 2.4
835
    source_entries.sort(None, longest_to_shortest, True)
493 by Martin Pool
- Merge aaron's merge command
836
837
    target_entries = source_entries[:]
838
    # These are done from shortest to longest path, to avoid creating a
839
    # child before its parent has been created/renamed
840
    def shortest_to_longest(entry):
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
841
        path = entry.get_new_path(inventory, changeset)
493 by Martin Pool
- Merge aaron's merge command
842
        if path is None:
843
            return 0
844
        else:
845
            return len(path)
1185.59.3 by Denys Duchier
sort with key and reverse is standard in Python 2.4
846
    target_entries.sort(None, shortest_to_longest)
493 by Martin Pool
- Merge aaron's merge command
847
    return (source_entries, target_entries)
848
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
849
850 by Martin Pool
- Merge merge updates from aaron
850
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
851
                          conflict_handler):
493 by Martin Pool
- Merge aaron's merge command
852
    """Delete and rename entries as appropriate.  Entries are renamed to temp
850 by Martin Pool
- Merge merge updates from aaron
853
    names.  A map of id -> temp name (or None, for deletions) is returned.
493 by Martin Pool
- Merge aaron's merge command
854
855
    :param source_entries: The entries to rename and delete
856
    :type source_entries: List of `ChangesetEntry`
857
    :param inventory: The map of id -> filename in the current tree
858
    :type inventory: Dictionary
859
    :param dir: The directory to apply changes to
860
    :type dir: str
861
    :return: a mapping of id to temporary name
862
    :rtype: Dictionary
863
    """
864
    temp_name = {}
865
    for i in range(len(source_entries)):
866
        entry = source_entries[i]
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
867
        if entry.is_deletion():
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
868
            path = pathjoin(dir, inventory[entry.id])
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
869
            entry.apply(path, conflict_handler)
850 by Martin Pool
- Merge merge updates from aaron
870
            temp_name[entry.id] = None
493 by Martin Pool
- Merge aaron's merge command
871
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
872
        elif entry.needs_rename():
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
873
            if entry.is_creation():
1185.45.1 by Aaron Bentley
Handled cases where inventory entries need renaming but files don't exist
874
                continue
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
875
            to_name = pathjoin(temp_dir, str(i))
493 by Martin Pool
- Merge aaron's merge command
876
            src_path = inventory.get(entry.id)
877
            if src_path is not None:
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
878
                src_path = pathjoin(dir, src_path)
493 by Martin Pool
- Merge aaron's merge command
879
                try:
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
880
                    rename(src_path, to_name)
493 by Martin Pool
- Merge aaron's merge command
881
                    temp_name[entry.id] = to_name
882
                except OSError, e:
883
                    if e.errno != errno.ENOENT:
884
                        raise
1185.12.40 by abentley
Got even closer to standard Tree interface
885
                    if conflict_handler.missing_for_rename(src_path, to_name) \
886
                        == "skip":
493 by Martin Pool
- Merge aaron's merge command
887
                        continue
888
889
    return temp_name
890
891
850 by Martin Pool
- Merge merge updates from aaron
892
def rename_to_new_create(changed_inventory, target_entries, inventory, 
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
893
                         changeset, dir, conflict_handler):
493 by Martin Pool
- Merge aaron's merge command
894
    """Rename entries with temp names to their final names, create new files.
895
850 by Martin Pool
- Merge merge updates from aaron
896
    :param changed_inventory: A mapping of id to temporary name
897
    :type changed_inventory: Dictionary
493 by Martin Pool
- Merge aaron's merge command
898
    :param target_entries: The entries to apply changes to
899
    :type target_entries: List of `ChangesetEntry`
900
    :param changeset: The changeset to apply
901
    :type changeset: `Changeset`
902
    :param dir: The directory to apply changes to
903
    :type dir: str
904
    """
905
    for entry in target_entries:
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
906
        new_tree_path = entry.get_new_path(inventory, changeset)
850 by Martin Pool
- Merge merge updates from aaron
907
        if new_tree_path is None:
493 by Martin Pool
- Merge aaron's merge command
908
            continue
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
909
        new_path = pathjoin(dir, new_tree_path)
850 by Martin Pool
- Merge merge updates from aaron
910
        old_path = changed_inventory.get(entry.id)
1092.2.24 by Robert Collins
merge from martins newformat branch - brings in transport abstraction
911
        if bzrlib.osutils.lexists(new_path):
493 by Martin Pool
- Merge aaron's merge command
912
            if conflict_handler.target_exists(entry, new_path, old_path) == \
913
                "skip":
914
                continue
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
915
        if entry.is_creation():
916
            entry.apply(new_path, conflict_handler)
850 by Martin Pool
- Merge merge updates from aaron
917
            changed_inventory[entry.id] = new_tree_path
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
918
        elif entry.needs_rename():
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
919
            if entry.is_deletion():
1185.45.1 by Aaron Bentley
Handled cases where inventory entries need renaming but files don't exist
920
                continue
493 by Martin Pool
- Merge aaron's merge command
921
            if old_path is None:
922
                continue
923
            try:
1185.33.47 by Martin Pool
Add ChangesetEntry.__repr__
924
                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.
925
                rename(old_path, new_path)
850 by Martin Pool
- Merge merge updates from aaron
926
                changed_inventory[entry.id] = new_tree_path
493 by Martin Pool
- Merge aaron's merge command
927
            except OSError, e:
1185.33.46 by Martin Pool
Better exception for one unhandled merge case
928
                raise BzrCheckError('failed to rename %s to %s for changeset entry %s: %s'
929
                        % (old_path, new_path, entry, e))
493 by Martin Pool
- Merge aaron's merge command
930
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
931
493 by Martin Pool
- Merge aaron's merge command
932
class TargetExists(Exception):
933
    def __init__(self, entry, target):
934
        msg = "The path %s already exists" % target
935
        Exception.__init__(self, msg)
936
        self.entry = entry
937
        self.target = target
938
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
939
493 by Martin Pool
- Merge aaron's merge command
940
class RenameConflict(Exception):
941
    def __init__(self, id, this_name, base_name, other_name):
942
        msg = """Trees all have different names for a file
943
 this: %s
944
 base: %s
945
other: %s
946
   id: %s""" % (this_name, base_name, other_name, id)
947
        Exception.__init__(self, msg)
948
        self.this_name = this_name
949
        self.base_name = base_name
950
        self_other_name = other_name
951
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
952
493 by Martin Pool
- Merge aaron's merge command
953
class MoveConflict(Exception):
954
    def __init__(self, id, this_parent, base_parent, other_parent):
955
        msg = """The file is in different directories in every tree
956
 this: %s
957
 base: %s
958
other: %s
959
   id: %s""" % (this_parent, base_parent, other_parent, id)
960
        Exception.__init__(self, msg)
961
        self.this_parent = this_parent
962
        self.base_parent = base_parent
963
        self_other_parent = other_parent
964
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
965
493 by Martin Pool
- Merge aaron's merge command
966
class MergeConflict(Exception):
967
    def __init__(self, this_path):
968
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
969
        self.this_path = this_path
970
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
971
493 by Martin Pool
- Merge aaron's merge command
972
class WrongOldContents(Exception):
973
    def __init__(self, filename):
974
        msg = "Contents mismatch deleting %s" % filename
975
        self.filename = filename
976
        Exception.__init__(self, msg)
977
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
978
1434 by Robert Collins
merge Gustavos executable2 patch
979
class WrongOldExecFlag(Exception):
980
    def __init__(self, filename, old_exec_flag, new_exec_flag):
981
        msg = "Executable flag missmatch on %s:\n" \
982
        "Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
493 by Martin Pool
- Merge aaron's merge command
983
        self.filename = filename
984
        Exception.__init__(self, msg)
985
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
986
493 by Martin Pool
- Merge aaron's merge command
987
class RemoveContentsConflict(Exception):
988
    def __init__(self, filename):
989
        msg = "Conflict deleting %s, which has different contents in BASE"\
990
            " and THIS" % filename
991
        self.filename = filename
992
        Exception.__init__(self, msg)
993
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
994
493 by Martin Pool
- Merge aaron's merge command
995
class DeletingNonEmptyDirectory(Exception):
996
    def __init__(self, filename):
997
        msg = "Trying to remove dir %s while it still had files" % filename
998
        self.filename = filename
999
        Exception.__init__(self, msg)
1000
1001
1002
class PatchTargetMissing(Exception):
1003
    def __init__(self, filename):
1004
        msg = "Attempt to patch %s, which does not exist" % filename
1005
        Exception.__init__(self, msg)
1006
        self.filename = filename
1007
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1008
1434 by Robert Collins
merge Gustavos executable2 patch
1009
class MissingForSetExec(Exception):
493 by Martin Pool
- Merge aaron's merge command
1010
    def __init__(self, filename):
1011
        msg = "Attempt to change permissions on  %s, which does not exist" %\
1012
            filename
1013
        Exception.__init__(self, msg)
1014
        self.filename = filename
1015
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1016
493 by Martin Pool
- Merge aaron's merge command
1017
class MissingForRm(Exception):
1018
    def __init__(self, filename):
1019
        msg = "Attempt to remove missing path %s" % filename
1020
        Exception.__init__(self, msg)
1021
        self.filename = filename
1022
1023
1024
class MissingForRename(Exception):
1185.12.40 by abentley
Got even closer to standard Tree interface
1025
    def __init__(self, filename, to_path):
1026
        msg = "Attempt to move missing path %s to %s" % (filename, to_path)
493 by Martin Pool
- Merge aaron's merge command
1027
        Exception.__init__(self, msg)
1028
        self.filename = filename
1029
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1030
850 by Martin Pool
- Merge merge updates from aaron
1031
class NewContentsConflict(Exception):
1032
    def __init__(self, filename):
1033
        msg = "Conflicting contents for new file %s" % (filename)
1034
        Exception.__init__(self, msg)
1035
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1036
1185.12.88 by Aaron Bentley
Added weave merge conflict to ExceptionConflictHandler
1037
class WeaveMergeConflict(Exception):
1038
    def __init__(self, filename):
1039
        msg = "Conflicting contents for file %s" % (filename)
1040
        Exception.__init__(self, msg)
1041
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1042
1185.12.33 by Aaron Bentley
Fixed symlink reverting
1043
class ThreewayContentsConflict(Exception):
1044
    def __init__(self, filename):
1045
        msg = "Conflicting contents for file %s" % (filename)
1046
        Exception.__init__(self, msg)
1047
850 by Martin Pool
- Merge merge updates from aaron
1048
1049
class MissingForMerge(Exception):
1050
    def __init__(self, filename):
1051
        msg = "The file %s was modified, but does not exist in this tree"\
1052
            % (filename)
1053
        Exception.__init__(self, msg)
1054
1055
558 by Martin Pool
- All top-level classes inherit from object
1056
class ExceptionConflictHandler(object):
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
1057
    """Default handler for merge exceptions.
1058
1059
    This throws an error on any kind of conflict.  Conflict handlers can
1060
    descend from this class if they have a better way to handle some or
1061
    all types of conflict.
1062
    """
493 by Martin Pool
- Merge aaron's merge command
1063
    def missing_parent(self, pathname):
1064
        parent = os.path.dirname(pathname)
1065
        raise Exception("Parent directory missing for %s" % pathname)
1066
1067
    def dir_exists(self, pathname):
1068
        raise Exception("Directory already exists for %s" % pathname)
1069
1070
    def failed_hunks(self, pathname):
1071
        raise Exception("Failed to apply some hunks for %s" % pathname)
1072
1073
    def target_exists(self, entry, target, old_path):
1074
        raise TargetExists(entry, target)
1075
1076
    def rename_conflict(self, id, this_name, base_name, other_name):
1077
        raise RenameConflict(id, this_name, base_name, other_name)
1078
974.1.20 by Aaron Bentley
Eliminated ThreeWayInventory
1079
    def move_conflict(self, id, this_dir, base_dir, other_dir):
493 by Martin Pool
- Merge aaron's merge command
1080
        raise MoveConflict(id, this_dir, base_dir, other_dir)
1081
974.1.23 by Aaron Bentley
Avoided unnecessary temp files
1082
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
493 by Martin Pool
- Merge aaron's merge command
1083
        os.unlink(new_file)
1084
        raise MergeConflict(this_path)
1085
1086
    def wrong_old_contents(self, filename, expected_contents):
1087
        raise WrongOldContents(filename)
1088
1089
    def rem_contents_conflict(self, filename, this_contents, base_contents):
1090
        raise RemoveContentsConflict(filename)
1091
1434 by Robert Collins
merge Gustavos executable2 patch
1092
    def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1093
        raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
493 by Martin Pool
- Merge aaron's merge command
1094
1095
    def rmdir_non_empty(self, filename):
1096
        raise DeletingNonEmptyDirectory(filename)
1097
1098
    def link_name_exists(self, filename):
1099
        raise TargetExists(filename)
1100
1101
    def patch_target_missing(self, filename, contents):
1102
        raise PatchTargetMissing(filename)
1103
1434 by Robert Collins
merge Gustavos executable2 patch
1104
    def missing_for_exec_flag(self, filename):
1105
        raise MissingForExecFlag(filename)
493 by Martin Pool
- Merge aaron's merge command
1106
1107
    def missing_for_rm(self, filename, change):
1108
        raise MissingForRm(filename)
1109
1185.12.40 by abentley
Got even closer to standard Tree interface
1110
    def missing_for_rename(self, filename, to_path):
1111
        raise MissingForRename(filename, to_path)
493 by Martin Pool
- Merge aaron's merge command
1112
974.1.20 by Aaron Bentley
Eliminated ThreeWayInventory
1113
    def missing_for_merge(self, file_id, other_path):
1114
        raise MissingForMerge(other_path)
850 by Martin Pool
- Merge merge updates from aaron
1115
1116
    def new_contents_conflict(self, filename, other_contents):
1117
        raise NewContentsConflict(filename)
1118
1185.12.88 by Aaron Bentley
Added weave merge conflict to ExceptionConflictHandler
1119
    def weave_merge_conflict(self, filename, weave, other_i, out_file):
1120
        raise WeaveMergeConflict(filename)
1121
 
1185.12.33 by Aaron Bentley
Fixed symlink reverting
1122
    def threeway_contents_conflict(self, filename, this_contents,
1123
                                   base_contents, other_contents):
1124
        raise ThreewayContentsConflict(filename)
1125
1114 by Martin Pool
- fix bad method declaration
1126
    def finalize(self):
622 by Martin Pool
Updated merge patch from Aaron
1127
        pass
1128
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1129
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1130
def apply_changeset(changeset, inventory, dir, conflict_handler=None):
493 by Martin Pool
- Merge aaron's merge command
1131
    """Apply a changeset to a directory.
1132
1133
    :param changeset: The changes to perform
1134
    :type changeset: `Changeset`
1135
    :param inventory: The mapping of id to filename for the directory
1136
    :type inventory: Dictionary
1137
    :param dir: The path of the directory to apply the changes to
1138
    :type dir: str
1139
    :return: The mapping of the changed entries
1140
    :rtype: Dictionary
1141
    """
1142
    if conflict_handler is None:
974.1.83 by Aaron Bentley
Removed unused dir parameter from ExceptionConflictHandler
1143
        conflict_handler = ExceptionConflictHandler()
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
1144
    temp_dir = pathjoin(dir, "bzr-tree-change")
850 by Martin Pool
- Merge merge updates from aaron
1145
    try:
1146
        os.mkdir(temp_dir)
1147
    except OSError, e:
1148
        if e.errno == errno.EEXIST:
1149
            try:
1150
                os.rmdir(temp_dir)
1151
            except OSError, e:
1152
                if e.errno == errno.ENOTEMPTY:
1153
                    raise OldFailedTreeOp()
1154
            os.mkdir(temp_dir)
1155
        else:
1156
            raise
493 by Martin Pool
- Merge aaron's merge command
1157
    
1158
    #apply changes that don't affect filenames
1159
    for entry in changeset.entries.itervalues():
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
1160
        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
1161
            if entry.id not in inventory:
1162
                warning("entry {%s} no longer present, can't be updated",
1163
                        entry.id)
1164
                continue
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
1165
            path = pathjoin(dir, inventory[entry.id])
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1166
            entry.apply(path, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
1167
1168
    # Apply renames in stages, to minimize conflicts:
1169
    # Only files whose name or parent change are interesting, because their
1170
    # target name may exist in the source tree.  If a directory's name changes,
1171
    # that doesn't make its children interesting.
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1172
    (source_entries, target_entries) = get_rename_entries(changeset, inventory)
493 by Martin Pool
- Merge aaron's merge command
1173
850 by Martin Pool
- Merge merge updates from aaron
1174
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1175
                                              temp_dir, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
1176
850 by Martin Pool
- Merge merge updates from aaron
1177
    rename_to_new_create(changed_inventory, target_entries, inventory,
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1178
                         changeset, dir, conflict_handler)
493 by Martin Pool
- Merge aaron's merge command
1179
    os.rmdir(temp_dir)
850 by Martin Pool
- Merge merge updates from aaron
1180
    return changed_inventory
493 by Martin Pool
- Merge aaron's merge command
1181
1182
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1183
def apply_changeset_tree(cset, tree):
493 by Martin Pool
- Merge aaron's merge command
1184
    r_inventory = {}
1185
    for entry in tree.source_inventory().itervalues():
1186
        inventory[entry.id] = entry.path
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1187
    new_inventory = apply_changeset(cset, r_inventory, tree.basedir)
493 by Martin Pool
- Merge aaron's merge command
1188
    new_entries, remove_entries = \
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1189
        get_inventory_change(inventory, new_inventory, cset)
493 by Martin Pool
- Merge aaron's merge command
1190
    tree.update_source_inventory(new_entries, remove_entries)
1191
1192
1185.59.1 by Denys Duchier
removed support for applying changesets in reverse
1193
def get_inventory_change(inventory, new_inventory, cset):
493 by Martin Pool
- Merge aaron's merge command
1194
    new_entries = {}
1195
    remove_entries = []
1196
    for entry in cset.entries.itervalues():
1197
        if entry.needs_rename():
850 by Martin Pool
- Merge merge updates from aaron
1198
            new_path = entry.get_new_path(inventory, cset)
1199
            if new_path is None:
1200
                remove_entries.append(entry.id)
493 by Martin Pool
- Merge aaron's merge command
1201
            else:
850 by Martin Pool
- Merge merge updates from aaron
1202
                new_entries[new_path] = entry.id
493 by Martin Pool
- Merge aaron's merge command
1203
    return new_entries, remove_entries
1204
1205
1206
def print_changeset(cset):
1207
    """Print all non-boring changeset entries
1208
    
1209
    :param cset: The changeset to print
1210
    :type cset: `Changeset`
1211
    """
1212
    for entry in cset.entries.itervalues():
1213
        if entry.is_boring():
1214
            continue
1215
        print entry.id
1216
        print entry.summarize_name(cset)
1217
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1218
1185.12.30 by Aaron Bentley
Moved all fooCreate into get_contents
1219
class UnsupportedFiletype(Exception):
1220
    def __init__(self, kind, full_path):
1221
        msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1222
            % (full_path, kind)
493 by Martin Pool
- Merge aaron's merge command
1223
        Exception.__init__(self, msg)
1224
        self.full_path = full_path
1185.12.30 by Aaron Bentley
Moved all fooCreate into get_contents
1225
        self.kind = kind
493 by Martin Pool
- Merge aaron's merge command
1226
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1227
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1228
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1229
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
493 by Martin Pool
- Merge aaron's merge command
1230
1448 by Robert Collins
revert symlinks correctly
1231
493 by Martin Pool
- Merge aaron's merge command
1232
class ChangesetGenerator(object):
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1233
    def __init__(self, tree_a, tree_b, interesting_ids=None):
493 by Martin Pool
- Merge aaron's merge command
1234
        object.__init__(self)
1235
        self.tree_a = tree_a
1236
        self.tree_b = tree_b
974.1.17 by Aaron Bentley
Switched to using a set of interesting file_ids instead of SourceFile attribute
1237
        self._interesting_ids = interesting_ids
493 by Martin Pool
- Merge aaron's merge command
1238
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1239
    def iter_both_tree_ids(self):
1240
        for file_id in self.tree_a:
1241
            yield file_id
1242
        for file_id in self.tree_b:
1243
            if file_id not in self.tree_a:
1244
                yield file_id
493 by Martin Pool
- Merge aaron's merge command
1245
1246
    def __call__(self):
1185.82.3 by John Arbash Meinel
Working on creating a factor for serializing changesets.
1247
        base_id = hasattr(self.tree_a, 'get_revision_id') and self.tree_a.get_revision_id()
1248
        target_id = hasattr(self.tree_b, 'get_revision_id') and self.tree_b.get_revision_id()
1249
        cset = Changeset(base_id, target_id)
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1250
        for file_id in self.iter_both_tree_ids():
1251
            cs_entry = self.make_entry(file_id)
493 by Martin Pool
- Merge aaron's merge command
1252
            if cs_entry is not None and not cs_entry.is_boring():
1253
                cset.add_entry(cs_entry)
1254
1255
        for entry in list(cset.entries.itervalues()):
1256
            if entry.parent != entry.new_parent:
1257
                if not cset.entries.has_key(entry.parent) and\
1258
                    entry.parent != NULL_ID and entry.parent is not None:
1259
                    parent_entry = self.make_boring_entry(entry.parent)
1260
                    cset.add_entry(parent_entry)
1261
                if not cset.entries.has_key(entry.new_parent) and\
1262
                    entry.new_parent != NULL_ID and \
1263
                    entry.new_parent is not None:
1264
                    parent_entry = self.make_boring_entry(entry.new_parent)
1265
                    cset.add_entry(parent_entry)
1266
        return cset
1267
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1268
    def iter_inventory(self, tree):
1269
        for file_id in tree:
1270
            yield self.get_entry(file_id, tree)
1271
1272
    def get_entry(self, file_id, tree):
1185.12.39 by abentley
Propogated has_or_had_id to Tree
1273
        if not tree.has_or_had_id(file_id):
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1274
            return None
1185.12.40 by abentley
Got even closer to standard Tree interface
1275
        return tree.inventory[file_id]
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1276
1277
    def get_entry_parent(self, entry):
1069 by Martin Pool
- merge merge improvements from aaron
1278
        if entry is None:
1279
            return None
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1280
        return entry.parent_id
1281
1282
    def get_path(self, file_id, tree):
1185.12.39 by abentley
Propogated has_or_had_id to Tree
1283
        if not tree.has_or_had_id(file_id):
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1284
            return None
1285
        path = tree.id2path(file_id)
1286
        if path == '':
1287
            return './.'
1288
        else:
1289
            return path
1290
1291
    def make_basic_entry(self, file_id, only_interesting):
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1292
        entry_a = self.get_entry(file_id, self.tree_a)
1293
        entry_b = self.get_entry(file_id, self.tree_b)
493 by Martin Pool
- Merge aaron's merge command
1294
        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
1295
            return None
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1296
        parent = self.get_entry_parent(entry_a)
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1297
        path = self.get_path(file_id, self.tree_a)
1298
        cs_entry = ChangesetEntry(file_id, parent, path)
974.1.19 by Aaron Bentley
Removed MergeTree.inventory
1299
        new_parent = self.get_entry_parent(entry_b)
1069 by Martin Pool
- merge merge improvements from aaron
1300
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1301
        new_path = self.get_path(file_id, self.tree_b)
493 by Martin Pool
- Merge aaron's merge command
1302
1303
        cs_entry.new_path = new_path
1304
        cs_entry.new_parent = new_parent
974.1.12 by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization
1305
        return cs_entry
493 by Martin Pool
- Merge aaron's merge command
1306
1307
    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
1308
        if self._interesting_ids is None:
1309
            return True
493 by Martin Pool
- Merge aaron's merge command
1310
        if entry_a is not None:
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1311
            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
1312
        elif entry_b is not None:
974.1.18 by Aaron Bentley
Stopped using SourceFile in inventory
1313
            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
1314
        else:
1315
            return False
1316
        return file_id in self._interesting_ids
493 by Martin Pool
- Merge aaron's merge command
1317
1318
    def make_boring_entry(self, id):
974.1.12 by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization
1319
        cs_entry = self.make_basic_entry(id, only_interesting=False)
493 by Martin Pool
- Merge aaron's merge command
1320
        if cs_entry.is_creation_or_deletion():
1321
            return self.make_entry(id, only_interesting=False)
1322
        else:
1323
            return cs_entry
1324
1325
    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
1326
        cs_entry = self.make_basic_entry(id, only_interesting)
493 by Martin Pool
- Merge aaron's merge command
1327
1328
        if cs_entry is None:
1329
            return None
1398 by Robert Collins
integrate in Gustavos x-bit patch
1330
1185.12.28 by Aaron Bentley
Removed use of readonly path for executability test
1331
        cs_entry.metadata_change = self.make_exec_flag_change(id)
1398 by Robert Collins
integrate in Gustavos x-bit patch
1332
974.1.12 by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization
1333
        if id in self.tree_a and id in self.tree_b:
1334
            a_sha1 = self.tree_a.get_file_sha1(id)
1335
            b_sha1 = self.tree_b.get_file_sha1(id)
1336
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1337
                return cs_entry
1338
1185.12.29 by Aaron Bentley
Removed readonly_path from changeset generation
1339
        cs_entry.contents_change = self.make_contents_change(id)
493 by Martin Pool
- Merge aaron's merge command
1340
        return cs_entry
1341
1185.12.28 by Aaron Bentley
Removed use of readonly path for executability test
1342
    def make_exec_flag_change(self, file_id):
1434 by Robert Collins
merge Gustavos executable2 patch
1343
        exec_flag_a = exec_flag_b = None
1185.12.28 by Aaron Bentley
Removed use of readonly path for executability test
1344
        if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1345
            exec_flag_a = self.tree_a.is_executable(file_id)
1346
1347
        if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1348
            exec_flag_b = self.tree_b.is_executable(file_id)
1349
1434 by Robert Collins
merge Gustavos executable2 patch
1350
        if exec_flag_a == exec_flag_b:
493 by Martin Pool
- Merge aaron's merge command
1351
            return None
1434 by Robert Collins
merge Gustavos executable2 patch
1352
        return ChangeExecFlag(exec_flag_a, exec_flag_b)
493 by Martin Pool
- Merge aaron's merge command
1353
1185.12.29 by Aaron Bentley
Removed readonly_path from changeset generation
1354
    def make_contents_change(self, file_id):
1185.12.30 by Aaron Bentley
Moved all fooCreate into get_contents
1355
        a_contents = get_contents(self.tree_a, file_id)
1356
        b_contents = get_contents(self.tree_b, file_id)
493 by Martin Pool
- Merge aaron's merge command
1357
        if a_contents == b_contents:
1358
            return None
1359
        return ReplaceContents(a_contents, b_contents)
1360
1185.12.30 by Aaron Bentley
Moved all fooCreate into get_contents
1361
1362
def get_contents(tree, file_id):
1363
    """Return the appropriate contents to create a copy of file_id from tree"""
1364
    if file_id not in tree:
1365
        return None
1366
    kind = tree.kind(file_id)
1367
    if kind == "file":
1185.12.31 by Aaron Bentley
Replaced FileCreate with TreeFileCreate, fixed revert with empty parents
1368
        return TreeFileCreate(tree, file_id)
1185.12.30 by Aaron Bentley
Moved all fooCreate into get_contents
1369
    elif kind in ("directory", "root_directory"):
1370
        return dir_create
1371
    elif kind == "symlink":
1372
        return SymlinkCreate(tree.get_symlink_target(file_id))
1373
    else:
1374
        raise UnsupportedFiletype(kind, tree.id2path(file_id))
493 by Martin Pool
- Merge aaron's merge command
1375
1376
1377
def full_path(entry, tree):
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
1378
    return pathjoin(tree.basedir, entry.path)
493 by Martin Pool
- Merge aaron's merge command
1379
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1380
493 by Martin Pool
- Merge aaron's merge command
1381
def new_delete_entry(entry, tree, inventory, delete):
1382
    if entry.path == "":
1383
        parent = NULL_ID
1384
    else:
1385
        parent = inventory[dirname(entry.path)].id
1386
    cs_entry = ChangesetEntry(parent, entry.path)
1387
    if delete:
1388
        cs_entry.new_path = None
1389
        cs_entry.new_parent = None
1390
    else:
1391
        cs_entry.path = None
1392
        cs_entry.parent = None
1393
    full_path = full_path(entry, tree)
1394
    status = os.lstat(full_path)
1395
    if stat.S_ISDIR(file_stat.st_mode):
1396
        action = dir_create
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1397
1398
959 by Martin Pool
doc
1399
# XXX: Can't we unify this with the regular inventory object
558 by Martin Pool
- All top-level classes inherit from object
1400
class Inventory(object):
493 by Martin Pool
- Merge aaron's merge command
1401
    def __init__(self, inventory):
1402
        self.inventory = inventory
1403
        self.rinventory = None
1404
1405
    def get_rinventory(self):
1406
        if self.rinventory is None:
1407
            self.rinventory  = invert_dict(self.inventory)
1408
        return self.rinventory
1409
1410
    def get_path(self, id):
1411
        return self.inventory.get(id)
1412
1413
    def get_name(self, id):
850 by Martin Pool
- Merge merge updates from aaron
1414
        path = self.get_path(id)
1415
        if path is None:
1416
            return None
1417
        else:
1418
            return os.path.basename(path)
493 by Martin Pool
- Merge aaron's merge command
1419
1420
    def get_dir(self, id):
1421
        path = self.get_path(id)
1422
        if path == "":
1423
            return None
850 by Martin Pool
- Merge merge updates from aaron
1424
        if path is None:
1425
            return None
493 by Martin Pool
- Merge aaron's merge command
1426
        return os.path.dirname(path)
1427
1428
    def get_parent(self, id):
850 by Martin Pool
- Merge merge updates from aaron
1429
        if self.get_path(id) is None:
1430
            return None
493 by Martin Pool
- Merge aaron's merge command
1431
        directory = self.get_dir(id)
1432
        if directory == '.':
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
1433
            directory = u'./.'
493 by Martin Pool
- Merge aaron's merge command
1434
        if directory is None:
1435
            return NULL_ID
1436
        return self.get_rinventory().get(directory)
1185.50.33 by John Arbash Meinel
Whitespace cleanups.
1437
1438