~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-26 01:50:01 UTC
  • mto: (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050826015001-d8b6e6330cb6401a
Merged from bzr.24

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import tempfile
11
11
import shutil
12
12
import errno
 
13
from fetch import greedy_fetch
13
14
 
14
15
 
15
16
# comments from abentley on irc: merge happens in two stages, each
117
118
        if not self.ignore_zero:
118
119
            print "%d conflicts encountered.\n" % self.conflicts
119
120
            
120
 
def get_tree(treespec, temp_root, label):
 
121
def get_tree(treespec, temp_root, label, local_branch=None):
121
122
    location, revno = treespec
122
123
    branch = find_branch(location)
123
124
    if revno is None:
 
125
        revision = None
 
126
    elif revno == -1:
 
127
        revision = branch.last_patch()
 
128
    else:
 
129
        revision = branch.lookup_revision(revno)
 
130
    return branch, get_revid_tree(branch, revision, temp_root, label,
 
131
                                  local_branch)
 
132
 
 
133
def get_revid_tree(branch, revision, temp_root, label, local_branch):
 
134
    if revision is None:
124
135
        base_tree = branch.working_tree()
125
 
    elif revno == -1:
126
 
        base_tree = branch.basis_tree()
127
136
    else:
128
 
        base_tree = branch.revision_tree(branch.lookup_revision(revno))
 
137
        if local_branch is not None:
 
138
            greedy_fetch(local_branch, branch, revision)
 
139
            base_tree = local_branch.revision_tree(revision)
 
140
        else:
 
141
            base_tree = branch.revision_tree(revision)
129
142
    temp_path = os.path.join(temp_root, label)
130
143
    os.mkdir(temp_path)
131
 
    return branch, MergeTree(base_tree, temp_path)
 
144
    return MergeTree(base_tree, temp_path)
132
145
 
133
146
 
134
147
def file_exists(tree, file_id):
165
178
    def has_id(self, file_id):
166
179
        return self.tree.has_id(file_id)
167
180
 
168
 
    def has_or_had_id(self, file_id):
169
 
        if file_id == self.tree.inventory.root.file_id:
170
 
            return True
171
 
        return self.tree.inventory.has_id(file_id)
172
 
 
173
181
    def readonly_path(self, id):
174
182
        if id not in self.tree:
175
183
            return None
203
211
    check_clean
204
212
        If true, this_dir must have no uncommitted changes before the
205
213
        merge begins.
 
214
    all available ancestors of other_revision and base_revision are
 
215
    automatically pulled into the branch.
206
216
    """
 
217
    from bzrlib.revision import common_ancestor, is_ancestor
 
218
    from bzrlib.revision import MultipleRevisionSources
 
219
    from bzrlib.errors import NoSuchRevision
207
220
    tempdir = tempfile.mkdtemp(prefix="bzr-")
208
221
    try:
209
222
        if this_dir is None:
210
223
            this_dir = '.'
211
224
        this_branch = find_branch(this_dir)
 
225
        this_rev_id = this_branch.last_patch()
 
226
        if this_rev_id is None:
 
227
            raise BzrCommandError("This branch has no commits")
212
228
        if check_clean:
213
229
            changes = compare_trees(this_branch.working_tree(), 
214
230
                                    this_branch.basis_tree(), False)
215
231
            if changes.has_changed():
216
232
                raise BzrCommandError("Working tree has uncommitted changes.")
217
 
        other_branch, other_tree = get_tree(other_revision, tempdir, "other")
 
233
        other_branch, other_tree = get_tree(other_revision, tempdir, "other",
 
234
                                            this_branch)
 
235
        if other_revision[1] == -1:
 
236
            other_rev_id = other_branch.last_patch()
 
237
            other_basis = other_rev_id
 
238
        elif other_revision[1] is not None:
 
239
            other_rev_id = other_branch.lookup_revision(other_revision[1])
 
240
            other_basis = other_rev_id
 
241
        else:
 
242
            other_rev_id = None
 
243
            other_basis = other_branch.last_patch()
218
244
        if base_revision == [None, None]:
219
 
            if other_revision[1] == -1:
220
 
                o_revno = None
221
 
            else:
222
 
                o_revno = other_revision[1]
223
 
            base_revno = this_branch.common_ancestor(other_branch, 
224
 
                                                     other_revno=o_revno)[0]
225
 
            if base_revno is None:
 
245
            base_rev_id = common_ancestor(this_rev_id, other_basis, 
 
246
                                          this_branch)
 
247
            if base_rev_id is None:
226
248
                raise UnrelatedBranches()
227
 
            base_revision = ['.', base_revno]
228
 
        base_branch, base_tree = get_tree(base_revision, tempdir, "base")
 
249
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
 
250
                                       "base", None)
 
251
            base_is_ancestor = True
 
252
        else:
 
253
            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
 
254
            if base_revision[1] == -1:
 
255
                base_rev_id = base_branch.last_patch()
 
256
            elif base_revision[1] is None:
 
257
                base_rev_id = None
 
258
            else:
 
259
                base_rev_id = base_branch.lookup_revision(base_revision[1])
 
260
            if base_rev_id is not None:
 
261
                base_is_ancestor = is_ancestor(this_rev_id, base_rev_id, 
 
262
                                               MultipleRevisionSources(
 
263
                                               this_branch, 
 
264
                                               base_branch))
 
265
            else:
 
266
                base_is_ancestor = False
229
267
        if file_list is None:
230
268
            interesting_ids = None
231
269
        else:
245
283
        merge_inner(this_branch, other_tree, base_tree, tempdir, 
246
284
                    ignore_zero=ignore_zero, backup_files=backup_files, 
247
285
                    merge_type=merge_type, interesting_ids=interesting_ids)
 
286
        if base_is_ancestor and other_rev_id is not None:
 
287
            this_branch.add_pending_merge(other_rev_id)
248
288
    finally:
249
289
        shutil.rmtree(tempdir)
250
290