~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# Copyright (C) 2005 Canonical Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import os
import tempfile
import shutil
import errno

import bzrlib.osutils
import bzrlib.revision
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
from bzrlib.merge_core import WeaveMerge
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
from bzrlib.changeset import Inventory, Diff3Merge, ReplaceContents
from bzrlib.branch import Branch
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
from bzrlib.errors import NoCommits, WorkingTreeNotRevision, NotBranchError
from bzrlib.delta import compare_trees
from bzrlib.trace import mutter, warning, note
from bzrlib.fetch import greedy_fetch, fetch
from bzrlib.revision import is_ancestor, NULL_REVISION
from bzrlib.osutils import rename
from bzrlib.revision import common_ancestor, MultipleRevisionSources
from bzrlib.errors import NoSuchRevision

# TODO: Report back as changes are merged in

# TODO: build_working_dir can be built on something simpler than merge()

# FIXME: merge() parameters seem oriented towards the command line
# NOTABUG: merge is a helper for commandline functions.  merge_inner is the
#          the core functionality.

# comments from abentley on irc: merge happens in two stages, each
# of which generates a changeset object

# stage 1: generate OLD->OTHER,
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT

class MergeConflictHandler(ExceptionConflictHandler):
    """Handle conflicts encountered while merging.

    This subclasses ExceptionConflictHandler, so that any types of
    conflict that are not explicitly handled cause an exception and
    terminate the merge.
    """
    def __init__(self, this_tree, base_tree, other_tree, ignore_zero=False):
        ExceptionConflictHandler.__init__(self)
        self.conflicts = 0
        self.ignore_zero = ignore_zero
        self.this_tree = this_tree
        self.base_tree = base_tree
        self.other_tree = other_tree

    def copy(self, source, dest):
        """Copy the text and mode of a file
        :param source: The path of the file to copy
        :param dest: The distination file to create
        """
        s_file = file(source, "rb")
        d_file = file(dest, "wb")
        for line in s_file:
            d_file.write(line)
        os.chmod(dest, 0777 & os.stat(source).st_mode)

    def dump(self, lines, dest):
        """Copy the text and mode of a file
        :param source: The path of the file to copy
        :param dest: The distination file to create
        """
        d_file = file(dest, "wb")
        for line in lines:
            d_file.write(line)

    def add_suffix(self, name, suffix, last_new_name=None, fix_inventory=True):
        """Rename a file to append a suffix.  If the new name exists, the
        suffix is added repeatedly until a non-existant name is found

        :param name: The path of the file
        :param suffix: The suffix to append
        :param last_new_name: (used for recursive calls) the last name tried
        """
        if last_new_name is None:
            last_new_name = name
        new_name = last_new_name+suffix
        try:
            rename(name, new_name)
            if fix_inventory is True:
                try:
                    relpath = self.this_tree.relpath(name)
                except NotBranchError:
                    relpath = None
                if relpath is not None:
                    file_id = self.this_tree.path2id(relpath)
                    if file_id is not None:
                        new_path = self.this_tree.relpath(new_name)
                        rename(new_name, name)
                        self.this_tree.branch.rename_one(relpath, new_path)
                        assert self.this_tree.id2path(file_id) == relpath
                        self.this_tree._inventory = self.this_tree.read_working_inventory()
                        assert self.this_tree.id2path(file_id) == new_path
        except OSError, e:
            if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:
                raise
            return self.add_suffix(name, suffix, last_new_name=new_name, 
                                   fix_inventory=fix_inventory)
        return new_name

    def conflict(self, text):
        warning(text)
        self.conflicts += 1
        

    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
        """
        Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER.  The
        main file will be a version with diff3 conflicts.
        :param new_file: Path to the output file with diff3 markers
        :param this_path: Path to the file text for the THIS tree
        :param base_path: Path to the file text for the BASE tree
        :param other_path: Path to the file text for the OTHER tree
        """
        self.add_suffix(this_path, ".THIS", fix_inventory=False)
        self.dump(base_lines, this_path+".BASE")
        self.dump(other_lines, this_path+".OTHER")
        rename(new_file, this_path)
        self.conflict("Diff3 conflict encountered in %s" % this_path)

    def weave_merge_conflict(self, filename, weave, other_i, out_file):
        """
        Handle weave conflicts by producing a .THIS, and .OTHER.  The
        main file will be a version with diff3-style conflicts.
        """
        self.add_suffix(filename, ".THIS")
        out_file.commit()
        self.dump(weave.get_iter(other_i), filename+".OTHER")
        self.conflict("Text conflict encountered in %s" % filename)

    def new_contents_conflict(self, filename, other_contents):
        """Conflicting contents for newly added file."""
        other_contents(filename + ".OTHER", self, False)
        self.conflict("Conflict in newly added file %s" % filename)
    

    def target_exists(self, entry, target, old_path):
        """Handle the case when the target file or dir exists"""
        moved_path = self.add_suffix(target, ".moved")
        self.conflict("Moved existing %s to %s" % (target, moved_path))

    def rmdir_non_empty(self, filename):
        """Handle the case where the dir to be removed still has contents"""
        self.conflict("Directory %s not removed because it is not empty"\
            % filename)
        return "skip"

    def rem_contents_conflict(self, filename, this_contents, base_contents):
        base_contents(filename+".BASE", self, False)
        this_contents(filename+".THIS", self, False)
        return ReplaceContents(this_contents, None)

    def rem_contents_conflict(self, filename, this_contents, base_contents):
        base_contents(filename+".BASE", self, False)
        this_contents(filename+".THIS", self, False)
        self.conflict("Other branch deleted locally modified file %s" %
                      filename)
        return ReplaceContents(this_contents, None)

    def abs_this_path(self, file_id):
        """Return the absolute path for a file_id in the this tree."""
        return self.this_tree.id2abspath(file_id)

    def add_missing_parents(self, file_id, tree):
        """If some of the parents for file_id are missing, add them."""
        entry = tree.inventory[file_id]
        if entry.parent_id not in self.this_tree:
            return self.create_all_missing(entry.parent_id, tree)
        else:
            return self.abs_this_path(entry.parent_id)

    def create_all_missing(self, file_id, tree):
        """Add contents for a file_id and all its parents to a tree."""
        entry = tree.inventory[file_id]
        if entry.parent_id is not None and entry.parent_id not in self.this_tree:
            abspath = self.create_all_missing(entry.parent_id, tree)
        else:
            abspath = self.abs_this_path(entry.parent_id)
        entry_path = os.path.join(abspath, entry.name)
        if not os.path.isdir(entry_path):
            self.create(file_id, entry_path, tree)
        return entry_path

    def create(self, file_id, path, tree, reverse=False):
        """Uses tree data to create a filesystem object for the file_id"""
        from changeset import get_contents
        get_contents(tree, file_id)(path, self, reverse)

    def missing_for_merge(self, file_id, other_path):
        """The file_id doesn't exist in THIS, but does in OTHER and BASE"""
        self.conflict("Other branch modified locally deleted file %s" %
                      other_path)
        parent_dir = self.add_missing_parents(file_id, self.other_tree)
        stem = os.path.join(parent_dir, os.path.basename(other_path))
        self.create(file_id, stem+".OTHER", self.other_tree)
        self.create(file_id, stem+".BASE", self.base_tree)

    def threeway_contents_conflict(filename, this_contents, base_contents,
                                   other_contents):
        self.conflict("Three-way conflict merging %s" % filename)

    def finalize(self):
        if not self.ignore_zero:
            note("%d conflicts encountered.\n" % self.conflicts)
            
def get_tree(treespec, local_branch=None):
    location, revno = treespec
    branch = Branch.open_containing(location)[0]
    if revno is None:
        revision = None
    elif revno == -1:
        revision = branch.last_revision()
    else:
        revision = branch.get_rev_id(revno)
        if revision is None:
            revision = NULL_REVISION
    return branch, get_revid_tree(branch, revision, local_branch)

def get_revid_tree(branch, revision, local_branch):
    if revision is None:
        base_tree = branch.working_tree()
    else:
        if local_branch is not None:
            greedy_fetch(local_branch, branch, revision)
            base_tree = local_branch.revision_tree(revision)
        else:
            base_tree = branch.revision_tree(revision)
    return base_tree


def file_exists(tree, file_id):
    return tree.has_filename(tree.id2path(file_id))
    

def build_working_dir(to_dir):
    """Build a working directory in an empty directory.

    to_dir is a directory containing branch metadata but no working files,
    typically constructed by cloning an existing branch. 

    This is split out as a special idiomatic case of merge.  It could
    eventually be done by just building the tree directly calling into 
    lower-level code (e.g. constructing a changeset).
    """
    # RBC 20051019 is this not just 'export' ?
    # Well, export doesn't take care of inventory...
    this_branch = Branch.open_containing(to_dir)[0]
    transform_tree(this_branch.working_tree(), this_branch.basis_tree())

def transform_tree(from_tree, to_tree):
    merge_inner(from_tree.branch, to_tree, from_tree, ignore_zero=True)

def merge(other_revision, base_revision,
          check_clean=True, ignore_zero=False,
          this_dir=None, backup_files=False, merge_type=ApplyMerge3,
          file_list=None, show_base=False, reprocess=False):
    """Merge changes into a tree.

    base_revision
        list(path, revno) Base for three-way merge.  
        If [None, None] then a base will be automatically determined.
    other_revision
        list(path, revno) Other revision for three-way merge.
    this_dir
        Directory to merge changes into; '.' by default.
    check_clean
        If true, this_dir must have no uncommitted changes before the
        merge begins.
    ignore_zero - If true, suppress the "zero conflicts" message when 
        there are no conflicts; should be set when doing something we expect
        to complete perfectly.
    file_list - If supplied, merge only changes to selected files.

    All available ancestors of other_revision and base_revision are
    automatically pulled into the branch.

    The revno may be -1 to indicate the last revision on the branch, which is
    the typical case.

    This function is intended for use from the command line; programmatic
    clients might prefer to call merge_inner(), which has less magic behavior.
    """
    if this_dir is None:
        this_dir = '.'
    this_branch = Branch.open_containing(this_dir)[0]
    if show_base and not merge_type is ApplyMerge3:
        raise BzrCommandError("Show-base is not supported for this merge"
                              " type. %s" % merge_type)
    if reprocess and not merge_type is ApplyMerge3:
        raise BzrCommandError("Reprocess is not supported for this merge"
                              " type. %s" % merge_type)
    if reprocess and show_base:
        raise BzrCommandError("Cannot reprocess and show base.")
    merger = Merger(this_branch)
    merger.check_basis(check_clean)
    merger.set_other(other_revision)
    merger.set_base(base_revision)
    merger.backup_files = backup_files
    merger.merge_type = merge_type 
    merger.set_interesting_files(file_list)
    merger.show_base = show_base 
    merger.reprocess = reprocess
    merger.conflict_handler = MergeConflictHandler(merger.this_tree, 
                                                   merger.base_tree, 
                                                   merger.other_tree,
                                                   ignore_zero=ignore_zero)
    conflicts = merger.do_merge()
    merger.set_pending()
    return conflicts

def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
                backup_files=False, merge_type=ApplyMerge3, 
                interesting_ids=None, show_base=False, reprocess=False):
    """Primary interface for merging. 

        typical use is probably 
        'merge_inner(branch, branch.get_revision_tree(other_revision),
                     branch.get_revision_tree(base_revision))'
        """
    merger = Merger(this_branch, other_tree, base_tree)
    merger.backup_files = False
    merger.merge_type = ApplyMerge3
    merger.interesting_ids = interesting_ids
    merger.show_base = show_base 
    merger.reprocess = reprocess
    merger.conflict_handler = MergeConflictHandler(merger.this_tree, base_tree, 
                                                   other_tree,
                                                   ignore_zero=ignore_zero)
    return merger.do_merge()


class Merger(object):
    def __init__(self, this_branch, other_tree=None, base_tree=None):
        object.__init__(self)
        self.this_branch = this_branch
        self.this_basis = this_branch.last_revision()
        self.this_rev_id = None
        self.this_tree = this_branch.working_tree()
        self.this_revision_tree = None
        self.other_tree = other_tree
        self.base_tree = base_tree
        self.ignore_zero = False
        self.backup_files = False
        self.interesting_ids = None
        self.show_base = False
        self.reprocess = False
        self.conflict_handler = MergeConflictHandler(self.this_tree, base_tree, 
                                                     other_tree)

    def revision_tree(self, revision_id):
        return self.this_branch.revision_tree(revision_id)

    def ensure_revision_trees(self):
        if self.this_revision_tree is None:
            if self.this_rev_id is None:
                self.compare_basis()
            if self.this_rev_id is None:
                raise WorkingTreeNotRevision(self.this_tree)
            self.this_revision_tree = self.this_branch.revision_tree(
                self.this_rev_id)

        if self.other_rev_id is None:
            other_basis_tree = self.revision_tree(self.other_basis)
            changes = compare_trees(self.other_tree, other_basis_tree)
            if changes.has_changed():
                raise WorkingTreeNotRevision(self.this_tree)
            other_rev_id = other_basis
            self.other_tree = other_basis_tree


    def file_revisions(self, file_id):
        self.ensure_revision_trees()
        def get_id(tree, file_id):
            revision_id = tree.inventory[file_id].revision
            assert revision_id is not None
            return revision_id
        trees = (self.this_revision_tree, self.other_tree)
        return [get_id(tree, file_id) for tree in trees]
            

    def merge_factory(self, file_id, base, other):
        if self.merge_type.history_based:
            t_revid, o_revid = self.file_revisions(file_id)
            weave = self.this_revision_tree.get_weave(file_id)
            contents_change = self.merge_type(weave, t_revid, o_revid)
        else:
            if self.show_base is True or self.reprocess is True:
                contents_change = self.merge_type(file_id, base, other, 
                                                  show_base=self.show_base, 
                                                  reprocess=self.reprocess)
            else:
                contents_change = self.merge_type(file_id, base, other)
        if self.backup_files:
            contents_change = BackupBeforeChange(contents_change)
        return contents_change

    def check_basis(self, check_clean):
        if self.this_basis is None:
            raise BzrCommandError("This branch has no commits")
        if check_clean:
            self.compare_basis()
            if self.this_basis != self.this_rev_id:
                raise BzrCommandError("Working tree has uncommitted changes.")

    def compare_basis(self):
        changes = compare_trees(self.this_branch.working_tree(), 
                                self.this_branch.basis_tree(), False)
        if not changes.has_changed():
            self.this_rev_id = self.this_basis

    def set_interesting_files(self, file_list):
        if file_list is None:
            self.interesting_ids = None
            return

        interesting_ids = set()
        for fname in file_list:
            path = self.this_tree.relpath(fname)
            found_id = False
            for tree in (self.this_tree, self.base_tree, self.other_tree):
                file_id = tree.inventory.path2id(path)
                if file_id is not None:
                    interesting_ids.add(file_id)
                    found_id = True
            if not found_id:
                raise BzrCommandError("%s is not a source file in any"
                                      " tree." % fname)
        self.interesting_ids = interesting_ids

    def set_pending(self):
        if not self.base_is_ancestor:
            return
        if self.other_rev_id is None:
            return
        if self.other_rev_id in self.this_branch.get_ancestry(self.this_basis):
            return
        self.this_branch.add_pending_merge(self.other_rev_id)

    def set_other(self, other_revision):
        other_branch, self.other_tree = get_tree(other_revision, 
                                                 self.this_branch)
        if other_revision[1] == -1:
            self.other_rev_id = other_branch.last_revision()
            if self.other_rev_id is None:
                raise NoCommits(other_branch)
            self.other_basis = self.other_rev_id
        elif other_revision[1] is not None:
            self.other_rev_id = other_branch.get_rev_id(other_revision[1])
            self.other_basis = self.other_rev_id
        else:
            self.other_rev_id = None
            self.other_basis = other_branch.last_revision()
            if self.other_basis is None:
                raise NoCommits(other_branch)
        fetch(from_branch=other_branch, to_branch=self.this_branch, 
              last_revision=self.other_basis)

    def set_base(self, base_revision):
        mutter("doing merge() with no base_revision specified")
        if base_revision == [None, None]:
            try:
                self.base_rev_id = common_ancestor(self.this_basis, 
                                                   self.other_basis, 
                                                   self.this_branch)
            except NoCommonAncestor:
                raise UnrelatedBranches()
            self.base_tree = get_revid_tree(self.this_branch, self.base_rev_id,
                                            None)
            self.base_is_ancestor = True
        else:
            base_branch, self.base_tree = get_tree(base_revision)
            if base_revision[1] == -1:
                self.base_rev_id = base_branch.last_revision()
            elif base_revision[1] is None:
                self.base_rev_id = None
            else:
                self.base_rev_id = base_branch.get_rev_id(base_revision[1])
            fetch(from_branch=base_branch, to_branch=self.this_branch)
            self.base_is_ancestor = is_ancestor(self.this_basis, 
                                                self.base_rev_id,
                                                self.this_branch)

    def do_merge(self):
        def get_inventory(tree):
            return tree.inventory

        inv_changes = merge_flex(self.this_tree, self.base_tree, 
                                 self.other_tree,
                                 generate_changeset, get_inventory,
                                 self.conflict_handler,
                                 merge_factory=self.merge_factory, 
                                 interesting_ids=self.interesting_ids)

        adjust_ids = []
        for id, path in inv_changes.iteritems():
            if path is not None:
                if path == '.':
                    path = ''
                else:
                    assert path.startswith('.' + os.sep), "path is %s" % path
                path = path[2:]
            adjust_ids.append((path, id))
        if len(adjust_ids) > 0:
            self.this_branch.working_tree().set_inventory(self.regen_inventory(adjust_ids))
        conflicts = self.conflict_handler.conflicts
        self.conflict_handler.finalize()
        return conflicts

    def regen_inventory(self, new_entries):
        old_entries = self.this_branch.working_tree().read_working_inventory()
        new_inventory = {}
        by_path = {}
        new_entries_map = {} 
        for path, file_id in new_entries:
            if path is None:
                continue
            new_entries_map[file_id] = path

        def id2path(file_id):
            path = new_entries_map.get(file_id)
            if path is not None:
                return path
            entry = old_entries[file_id]
            if entry.parent_id is None:
                return entry.name
            return os.path.join(id2path(entry.parent_id), entry.name)
            
        for file_id in old_entries:
            entry = old_entries[file_id]
            path = id2path(file_id)
            new_inventory[file_id] = (path, file_id, entry.parent_id, 
                                      entry.kind)
            by_path[path] = file_id
        
        deletions = 0
        insertions = 0
        new_path_list = []
        for path, file_id in new_entries:
            if path is None:
                del new_inventory[file_id]
                deletions += 1
            else:
                new_path_list.append((path, file_id))
                if file_id not in old_entries:
                    insertions += 1
        # Ensure no file is added before its parent
        new_path_list.sort()
        for path, file_id in new_path_list:
            if path == '':
                parent = None
            else:
                parent = by_path[os.path.dirname(path)]
            abspath = os.path.join(self.this_tree.basedir, path)
            kind = bzrlib.osutils.file_kind(abspath)
            new_inventory[file_id] = (path, file_id, parent, kind)
            by_path[path] = file_id 

        # Get a list in insertion order
        new_inventory_list = new_inventory.values()
        mutter ("""Inventory regeneration:
    old length: %i insertions: %i deletions: %i new_length: %i"""\
            % (len(old_entries), insertions, deletions, 
               len(new_inventory_list)))
        assert len(new_inventory_list) == len(old_entries) + insertions\
            - deletions
        new_inventory_list.sort()
        return new_inventory_list

merge_types = {     "merge3": (ApplyMerge3, "Native diff3-style merge"), 
                     "diff3": (Diff3Merge,  "Merge using external diff3"),
                     'weave': (WeaveMerge, "Weave-based merge")
              }