~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
*** modified file 'bzrlib/merge.py'
--- bzrlib/merge.py 
+++ bzrlib/merge.py 
@@ -105,14 +105,21 @@
     location, revno = treespec
     branch = find_branch(location)
     if revno is None:
+        rev_id = None
+    elif revno == -1:
+        rev_id = branch.last_patch()
+    else:
+        rev_id = branch.lookup_revision(revno)
+    return branch, get_revid_tree(branch, rev_id, temp_root, label)
+
+def get_revid_tree(branch, rev_id, temp_root, label):
+    if rev_id is None:
         base_tree = branch.working_tree()
-    elif revno == -1:
-        base_tree = branch.basis_tree()
     else:
-        base_tree = branch.revision_tree(branch.lookup_revision(revno))
+        base_tree = branch.revision_tree(rev_id)
     temp_path = os.path.join(temp_root, label)
     os.mkdir(temp_path)
-    return branch, MergeTree(base_tree, temp_path)
+    return MergeTree(base_tree, temp_path)
 
 
 def abspath(tree, file_id):
@@ -178,30 +185,68 @@
         If true, this_dir must have no uncommitted changes before the
         merge begins.
     """
+    from bzrlib.revision import common_ancestor, MultipleRevisionSources
+    from bzrlib.branch import NoSuchRevision
     tempdir = tempfile.mkdtemp(prefix="bzr-")
     try:
         if this_dir is None:
             this_dir = '.'
         this_branch = find_branch(this_dir)
+        this_rev_id = this_branch.last_patch()
+        if this_rev_id is None:
+            raise BzrCommandError("This branch has no commits")
         if check_clean:
             changes = compare_trees(this_branch.working_tree(), 
                                     this_branch.basis_tree(), False)
             if changes.has_changed():
                 raise BzrCommandError("Working tree has uncommitted changes.")
         other_branch, other_tree = get_tree(other_revision, tempdir, "other")
+        if other_revision[1] == -1:
+            other_rev_id = other_branch.last_patch()
+            other_basis = other_rev_id
+        elif other_revision[1] is not None:
+            other_rev_id = other_branch.lookup_revision(other_revision[1])
+            other_basis = other_rev_id
+        else:
+            other_rev_id = None
+            other_basis = other_branch.last_patch()
         if base_revision == [None, None]:
             if other_revision[1] == -1:
                 o_revno = None
             else:
                 o_revno = other_revision[1]
-            base_revno = this_branch.common_ancestor(other_branch, 
-                                                     other_revno=o_revno)[0]
-            if base_revno is None:
+            base_rev_id = common_ancestor(this_rev_id, other_basis,
+                                          MultipleRevisionSources(this_branch, 
+                                          other_branch))
+            if base_rev_id is None:
                 raise UnrelatedBranches()
-            base_revision = ['.', base_revno]
-        base_branch, base_tree = get_tree(base_revision, tempdir, "base")
+            try:
+                base_revision = this_branch.get_revision(base_rev_id)
+                base_branch = this_branch
+            except NoSuchRevision:
+                base_branch = other_branch
+            base_tree = get_revid_tree(base_branch, base_rev_id, tempdir, 
+                                       "base")
+            base_is_ancestor = True
+        else:
+            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
+            if base_revision[1] == -1:
+                base_rev_id = base_branch.last_patch()
+            elif base_revision[1] is None:
+                base_rev_id = None
+            else:
+                base_rev_id = base_branch.lookup_revision(base_revision[1])
+            if base_rev_id is not None:
+                base_is_ancestor = is_ancestor(this_rev_id, base_rev_id, 
+                                               MultipleRevisionSources(
+                                               this_branch, 
+                                               base_branch))
+            else:
+                base_is_ancestor = False
         merge_inner(this_branch, other_tree, base_tree, tempdir, 
                     ignore_zero=ignore_zero)
+        if base_is_ancestor and other_rev_id is not None:
+            this_branch.add_pending_merge(other_rev_id)
     finally:
         shutil.rmtree(tempdir)