~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Robert Collins
  • Date: 2005-10-17 11:56:54 UTC
  • mfrom: (1185.16.59)
  • Revision ID: robertc@robertcollins.net-20051017115654-662239e1587524a8
mergeĀ fromĀ martin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
 
125
125
    def new_contents_conflict(self, filename, other_contents):
126
126
        """Conflicting contents for newly added file."""
127
 
        self.copy(other_contents, filename + ".OTHER")
 
127
        other.contents.apply(filename + ".OTHER")
128
128
        self.conflict("Conflict in newly added file %s" % filename)
129
129
    
130
130
 
153
153
 
154
154
    def abs_this_path(self, file_id):
155
155
        """Return the absolute path for a file_id in the this tree."""
156
 
        relpath = self.this_tree.id2path(file_id)
157
 
        return self.this_tree.tree.abspath(relpath)
 
156
        return self.this_tree.id2abspath(file_id)
158
157
 
159
158
    def add_missing_parents(self, file_id, tree):
160
159
        """If some of the parents for file_id are missing, add them."""
161
 
        entry = tree.tree.inventory[file_id]
 
160
        entry = tree.inventory[file_id]
162
161
        if entry.parent_id not in self.this_tree:
163
162
            return self.create_all_missing(entry.parent_id, tree)
164
163
        else:
166
165
 
167
166
    def create_all_missing(self, file_id, tree):
168
167
        """Add contents for a file_id and all its parents to a tree."""
169
 
        entry = tree.tree.inventory[file_id]
 
168
        entry = tree.inventory[file_id]
170
169
        if entry.parent_id is not None and entry.parent_id not in self.this_tree:
171
170
            abspath = self.create_all_missing(entry.parent_id, tree)
172
171
        else:
178
177
 
179
178
    def create(self, file_id, path, tree, reverse=False):
180
179
        """Uses tree data to create a filesystem object for the file_id"""
181
 
        from merge_core import get_id_contents
182
 
        get_id_contents(file_id, tree)(path, self, reverse)
 
180
        from changeset import get_contents
 
181
        get_contents(tree, file_id)(path, self, reverse)
183
182
 
184
183
    def missing_for_merge(self, file_id, other_path):
185
184
        """The file_id doesn't exist in THIS, but does in OTHER and BASE"""
190
189
        self.create(file_id, stem+".OTHER", self.other_tree)
191
190
        self.create(file_id, stem+".BASE", self.base_tree)
192
191
 
 
192
    def threeway_contents_conflict(filename, this_contents, base_contents,
 
193
                                   other_contents):
 
194
        self.conflict("Three-way conflict merging %s" % filename)
 
195
 
193
196
    def finalize(self):
194
197
        if not self.ignore_zero:
195
198
            note("%d conflicts encountered.\n" % self.conflicts)
196
199
            
197
 
def get_tree(treespec, temp_root, label, local_branch=None):
 
200
def get_tree(treespec, local_branch=None):
198
201
    location, revno = treespec
199
202
    branch = Branch.open_containing(location)
200
203
    if revno is None:
203
206
        revision = branch.last_revision()
204
207
    else:
205
208
        revision = branch.get_rev_id(revno)
206
 
    return branch, get_revid_tree(branch, revision, temp_root, label,
207
 
                                  local_branch)
 
209
    return branch, get_revid_tree(branch, revision, local_branch)
208
210
 
209
 
def get_revid_tree(branch, revision, temp_root, label, local_branch):
 
211
def get_revid_tree(branch, revision, local_branch):
210
212
    if revision is None:
211
213
        base_tree = branch.working_tree()
212
214
    else:
215
217
            base_tree = local_branch.revision_tree(revision)
216
218
        else:
217
219
            base_tree = branch.revision_tree(revision)
218
 
    temp_path = os.path.join(temp_root, label)
219
 
    os.mkdir(temp_path)
220
 
    return MergeAdapterTree(base_tree, temp_path)
 
220
    return base_tree
221
221
 
222
222
 
223
223
def file_exists(tree, file_id):
224
224
    return tree.has_filename(tree.id2path(file_id))
225
225
    
226
226
 
227
 
class MergeAdapterTree(object):
228
 
    """MergeAdapterTree adapts a normal tree for merge_inner to use.
229
 
 
230
 
    The interface the merge_inner needs is nearly but not quite
231
 
    the same as that of bzrlib.tree with the exception of readonly_path.
232
 
    """
233
 
    
234
 
    def __init__(self, tree, tempdir):
235
 
        object.__init__(self)
236
 
        if hasattr(tree, "basedir"):
237
 
            self.root = tree.basedir
238
 
        else:
239
 
            self.root = None
240
 
        self.tree = tree
241
 
        self.tempdir = tempdir
242
 
        os.mkdir(os.path.join(self.tempdir, "texts"))
243
 
        os.mkdir(os.path.join(self.tempdir, "symlinks"))
244
 
        self.cached = {}
245
 
 
246
 
    def __iter__(self):
247
 
        return self.tree.__iter__()
248
 
 
249
 
    def __contains__(self, file_id):
250
 
        return file_id in self.tree
251
 
 
252
 
    def get_file(self, file_id):
253
 
        return self.tree.get_file(file_id)
254
 
 
255
 
    def get_file_sha1(self, id):
256
 
        return self.tree.get_file_sha1(id)
257
 
 
258
 
    def is_executable(self, id):
259
 
        return self.tree.is_executable(id)
260
 
 
261
 
    def id2path(self, file_id):
262
 
        return self.tree.id2path(file_id)
263
 
 
264
 
    def has_id(self, file_id):
265
 
        return self.tree.has_id(file_id)
266
 
 
267
 
    def has_or_had_id(self, file_id):
268
 
        if file_id == self.tree.inventory.root.file_id:
269
 
            return True
270
 
        return self.tree.inventory.has_id(file_id)
271
 
 
272
 
    def has_or_had_id(self, file_id):
273
 
        if file_id == self.tree.inventory.root.file_id:
274
 
            return True
275
 
        return self.tree.inventory.has_id(file_id)
276
 
 
277
 
    def readonly_path(self, id):
278
 
        if id not in self.tree:
279
 
            return None
280
 
        if self.root is not None:
281
 
            return self.tree.abspath(self.tree.id2path(id))
282
 
        else:
283
 
            kind = self.tree.inventory[id].kind
284
 
            if kind in ("directory", "root_directory"):
285
 
                return self.tempdir
286
 
            if not self.cached.has_key(id):
287
 
                if kind == "file":
288
 
                    path = os.path.join(self.tempdir, "texts", id)
289
 
                    outfile = file(path, "wb")
290
 
                    outfile.write(self.tree.get_file(id).read())
291
 
                    assert(bzrlib.osutils.lexists(path))
292
 
                    if self.tree.is_executable(id):
293
 
                        os.chmod(path, 0755)
294
 
                else:
295
 
                    assert kind == "symlink"
296
 
                    path = os.path.join(self.tempdir, "symlinks", id)
297
 
                    target = self.tree.get_symlink_target(id)
298
 
                    os.symlink(target, path)
299
 
                self.cached[id] = path
300
 
            return self.cached[id]
301
 
 
302
 
 
303
227
def build_working_dir(to_dir):
304
228
    """Build a working directory in an empty directory.
305
229
 
349
273
                                    this_branch.basis_tree(), False)
350
274
            if changes.has_changed():
351
275
                raise BzrCommandError("Working tree has uncommitted changes.")
352
 
        other_branch, other_tree = get_tree(other_revision, tempdir, "other",
353
 
                                            this_branch)
 
276
        other_branch, other_tree = get_tree(other_revision, this_branch)
354
277
        if other_revision[1] == -1:
355
278
            other_rev_id = other_branch.last_revision()
356
279
            if other_rev_id is None:
370
293
                                              this_branch)
371
294
            except NoCommonAncestor:
372
295
                raise UnrelatedBranches()
373
 
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
374
 
                                       "base", None)
 
296
            base_tree = get_revid_tree(this_branch, base_rev_id, None)
375
297
            base_is_ancestor = True
376
298
        else:
377
 
            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
 
299
            base_branch, base_tree = get_tree(base_revision)
378
300
            if base_revision[1] == -1:
379
301
                base_rev_id = base_branch.last_revision()
380
302
            elif base_revision[1] is None:
392
314
            for fname in file_list:
393
315
                path = this_tree.relpath(fname)
394
316
                found_id = False
395
 
                for tree in (this_tree, base_tree.tree, other_tree.tree):
 
317
                for tree in (this_tree, base_tree, other_tree):
396
318
                    file_id = tree.inventory.path2id(path)
397
319
                    if file_id is not None:
398
320
                        interesting_ids.add(file_id)
428
350
            contents_change = BackupBeforeChange(contents_change)
429
351
        return contents_change
430
352
 
431
 
    this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
 
353
    this_tree = get_tree((this_branch.base, None))[1]
432
354
 
433
355
    def get_inventory(tree):
434
 
        return tree.tree.inventory
 
356
        return tree.inventory
435
357
 
436
358
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
437
359
                             generate_changeset, get_inventory,
450
372
            path = path[2:]
451
373
        adjust_ids.append((path, id))
452
374
    if len(adjust_ids) > 0:
453
 
        this_branch.set_inventory(regen_inventory(this_branch, this_tree.root,
 
375
        this_branch.set_inventory(regen_inventory(this_branch, 
 
376
                                                  this_tree.basedir,
454
377
                                                  adjust_ids))
455
378
 
456
379