~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: abentley
  • Date: 2005-10-14 02:38:46 UTC
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1460.
  • Revision ID: abentley@lappy-20051014023846-276686141002cfa1
remove method definition

Show diffs side-by-side

added added

removed removed

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