~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Robert Collins
  • Date: 2005-08-23 10:44:48 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050823104448-fb5d448e7a5a8ee3
relace runTest with test_foo in blackbox tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 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
 
 
18
 
import os
19
 
import tempfile
20
 
import shutil
21
 
import errno
22
 
from fetch import greedy_fetch
23
 
 
24
 
import bzrlib.osutils
25
 
import bzrlib.revision
26
1
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
27
2
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
28
3
from bzrlib.changeset import Inventory, Diff3Merge
29
 
from bzrlib.branch import Branch
30
 
from bzrlib.errors import BzrCommandError, UnrelatedBranches, NoCommonAncestor
31
 
from bzrlib.errors import NoCommits
 
4
from bzrlib import find_branch
 
5
import bzrlib.osutils
 
6
from bzrlib.errors import BzrCommandError
32
7
from bzrlib.delta import compare_trees
33
 
from bzrlib.trace import mutter, warning
34
 
from bzrlib.fetch import greedy_fetch
35
 
from bzrlib.revision import is_ancestor
 
8
from trace import mutter, warning
 
9
import os.path
 
10
import tempfile
 
11
import shutil
 
12
import errno
 
13
 
36
14
 
37
15
# comments from abentley on irc: merge happens in two stages, each
38
16
# of which generates a changeset object
40
18
# stage 1: generate OLD->OTHER,
41
19
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT
42
20
 
 
21
class UnrelatedBranches(BzrCommandError):
 
22
    def __init__(self):
 
23
        msg = "Branches have no common ancestor, and no base revision"\
 
24
            " specified."
 
25
        BzrCommandError.__init__(self, msg)
 
26
 
 
27
 
43
28
class MergeConflictHandler(ExceptionConflictHandler):
44
29
    """Handle conflicts encountered while merging.
45
30
 
47
32
    conflict that are not explicitly handled cause an exception and
48
33
    terminate the merge.
49
34
    """
50
 
    def __init__(self, ignore_zero=False):
51
 
        ExceptionConflictHandler.__init__(self)
 
35
    def __init__(self, dir, ignore_zero=False):
 
36
        ExceptionConflictHandler.__init__(self, dir)
52
37
        self.conflicts = 0
53
38
        self.ignore_zero = ignore_zero
54
39
 
132
117
        if not self.ignore_zero:
133
118
            print "%d conflicts encountered.\n" % self.conflicts
134
119
            
135
 
def get_tree(treespec, temp_root, label, local_branch=None):
 
120
def get_tree(treespec, temp_root, label):
136
121
    location, revno = treespec
137
 
    branch = Branch.open(location)
 
122
    branch = find_branch(location)
138
123
    if revno is None:
139
 
        revision = None
 
124
        base_tree = branch.working_tree()
140
125
    elif revno == -1:
141
 
        revision = branch.last_patch()
142
 
    else:
143
 
        revision = branch.get_rev_id(revno)
144
 
    return branch, get_revid_tree(branch, revision, temp_root, label,
145
 
                                  local_branch)
146
 
 
147
 
def get_revid_tree(branch, revision, temp_root, label, local_branch):
148
 
    if revision is None:
149
 
        base_tree = branch.working_tree()
150
 
    else:
151
 
        if local_branch is not None:
152
 
            greedy_fetch(local_branch, branch, revision)
153
 
            base_tree = local_branch.revision_tree(revision)
154
 
        else:
155
 
            base_tree = branch.revision_tree(revision)
 
126
        base_tree = branch.basis_tree()
 
127
    else:
 
128
        base_tree = branch.revision_tree(branch.lookup_revision(revno))
156
129
    temp_path = os.path.join(temp_root, label)
157
130
    os.mkdir(temp_path)
158
 
    return MergeTree(base_tree, temp_path)
 
131
    return branch, MergeTree(base_tree, temp_path)
159
132
 
160
133
 
161
134
def file_exists(tree, file_id):
192
165
    def has_id(self, file_id):
193
166
        return self.tree.has_id(file_id)
194
167
 
195
 
    def has_or_had_id(self, file_id):
196
 
        if file_id == self.tree.inventory.root.file_id:
197
 
            return True
198
 
        return self.tree.inventory.has_id(file_id)
199
 
 
200
 
    def has_or_had_id(self, file_id):
201
 
        if file_id == self.tree.inventory.root.file_id:
202
 
            return True
203
 
        return self.tree.inventory.has_id(file_id)
204
 
 
205
168
    def readonly_path(self, id):
206
169
        if id not in self.tree:
207
170
            return None
227
190
    """Merge changes into a tree.
228
191
 
229
192
    base_revision
230
 
        tuple(path, revision) Base for three-way merge.
 
193
        Base for three-way merge.
231
194
    other_revision
232
 
        tuple(path, revision) Other revision for three-way merge.
 
195
        Other revision for three-way merge.
233
196
    this_dir
234
197
        Directory to merge changes into; '.' by default.
235
198
    check_clean
236
199
        If true, this_dir must have no uncommitted changes before the
237
200
        merge begins.
238
 
    all available ancestors of other_revision and base_revision are
239
 
    automatically pulled into the branch.
240
201
    """
241
 
    from bzrlib.revision import common_ancestor, MultipleRevisionSources
242
 
    from bzrlib.errors import NoSuchRevision
243
202
    tempdir = tempfile.mkdtemp(prefix="bzr-")
244
203
    try:
245
204
        if this_dir is None:
246
205
            this_dir = '.'
247
 
        this_branch = Branch.open_containing(this_dir)
248
 
        this_rev_id = this_branch.last_patch()
249
 
        if this_rev_id is None:
250
 
            raise BzrCommandError("This branch has no commits")
 
206
        this_branch = find_branch(this_dir)
251
207
        if check_clean:
252
208
            changes = compare_trees(this_branch.working_tree(), 
253
209
                                    this_branch.basis_tree(), False)
254
210
            if changes.has_changed():
255
211
                raise BzrCommandError("Working tree has uncommitted changes.")
256
 
        other_branch, other_tree = get_tree(other_revision, tempdir, "other",
257
 
                                            this_branch)
258
 
        if other_revision[1] == -1:
259
 
            other_rev_id = other_branch.last_patch()
260
 
            if other_rev_id is None:
261
 
                raise NoCommits(other_branch)
262
 
            other_basis = other_rev_id
263
 
        elif other_revision[1] is not None:
264
 
            other_rev_id = other_branch.get_rev_id(other_revision[1])
265
 
            other_basis = other_rev_id
266
 
        else:
267
 
            other_rev_id = None
268
 
            other_basis = other_branch.last_patch()
269
 
            if other_basis is None:
270
 
                raise NoCommits(other_branch)
 
212
        other_branch, other_tree = get_tree(other_revision, tempdir, "other")
271
213
        if base_revision == [None, None]:
272
 
            try:
273
 
                base_rev_id = common_ancestor(this_rev_id, other_basis, 
274
 
                                              this_branch)
275
 
            except NoCommonAncestor:
 
214
            if other_revision[1] == -1:
 
215
                o_revno = None
 
216
            else:
 
217
                o_revno = other_revision[1]
 
218
            base_revno = this_branch.common_ancestor(other_branch, 
 
219
                                                     other_revno=o_revno)[0]
 
220
            if base_revno is None:
276
221
                raise UnrelatedBranches()
277
 
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
278
 
                                       "base", None)
279
 
            base_is_ancestor = True
280
 
        else:
281
 
            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
282
 
            if base_revision[1] == -1:
283
 
                base_rev_id = base_branch.last_patch()
284
 
            elif base_revision[1] is None:
285
 
                base_rev_id = None
286
 
            else:
287
 
                base_rev_id = base_branch.get_rev_id(base_revision[1])
288
 
            multi_source = MultipleRevisionSources(this_branch, base_branch)
289
 
            base_is_ancestor = is_ancestor(this_rev_id, base_rev_id,
290
 
                                           multi_source)
 
222
            base_revision = ['.', base_revno]
 
223
        base_branch, base_tree = get_tree(base_revision, tempdir, "base")
291
224
        if file_list is None:
292
225
            interesting_ids = None
293
226
        else:
307
240
        merge_inner(this_branch, other_tree, base_tree, tempdir, 
308
241
                    ignore_zero=ignore_zero, backup_files=backup_files, 
309
242
                    merge_type=merge_type, interesting_ids=interesting_ids)
310
 
        if base_is_ancestor and other_rev_id is not None\
311
 
            and other_rev_id not in this_branch.revision_history():
312
 
            this_branch.add_pending_merge(other_rev_id)
313
243
    finally:
314
244
        shutil.rmtree(tempdir)
315
245
 
349
279
 
350
280
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
351
281
                             generate_cset_optimized, get_inventory,
352
 
                             MergeConflictHandler(ignore_zero=ignore_zero),
 
282
                             MergeConflictHandler(base_tree.root,
 
283
                                                  ignore_zero=ignore_zero),
353
284
                             merge_factory=merge_factory, 
354
285
                             interesting_ids=interesting_ids)
355
286
 
359
290
            if path == '.':
360
291
                path = ''
361
292
            else:
362
 
                assert path.startswith('.' + os.sep), "path is %s" % path
 
293
                assert path.startswith('./'), "path is %s" % path
363
294
            path = path[2:]
364
295
        adjust_ids.append((path, id))
365
296
    if len(adjust_ids) > 0: