~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

- constraints on revprops
- tests for this

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
 
        other.contents.apply(filename + ".OTHER")
 
127
        self.copy(other_contents, 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
 
        return self.this_tree.id2abspath(file_id)
 
156
        relpath = self.this_tree.id2path(file_id)
 
157
        return self.this_tree.tree.abspath(relpath)
157
158
 
158
159
    def add_missing_parents(self, file_id, tree):
159
160
        """If some of the parents for file_id are missing, add them."""
160
 
        entry = tree.inventory[file_id]
 
161
        entry = tree.tree.inventory[file_id]
161
162
        if entry.parent_id not in self.this_tree:
162
163
            return self.create_all_missing(entry.parent_id, tree)
163
164
        else:
165
166
 
166
167
    def create_all_missing(self, file_id, tree):
167
168
        """Add contents for a file_id and all its parents to a tree."""
168
 
        entry = tree.inventory[file_id]
 
169
        entry = tree.tree.inventory[file_id]
169
170
        if entry.parent_id is not None and entry.parent_id not in self.this_tree:
170
171
            abspath = self.create_all_missing(entry.parent_id, tree)
171
172
        else:
177
178
 
178
179
    def create(self, file_id, path, tree, reverse=False):
179
180
        """Uses tree data to create a filesystem object for the file_id"""
180
 
        from changeset import get_contents
181
 
        get_contents(tree, file_id)(path, self, reverse)
 
181
        from merge_core import get_id_contents
 
182
        get_id_contents(file_id, tree)(path, self, reverse)
182
183
 
183
184
    def missing_for_merge(self, file_id, other_path):
184
185
        """The file_id doesn't exist in THIS, but does in OTHER and BASE"""
189
190
        self.create(file_id, stem+".OTHER", self.other_tree)
190
191
        self.create(file_id, stem+".BASE", self.base_tree)
191
192
 
192
 
    def threeway_contents_conflict(filename, this_contents, base_contents,
193
 
                                   other_contents):
194
 
        self.conflict("Three-way conflict merging %s" % filename)
195
 
 
196
193
    def finalize(self):
197
194
        if not self.ignore_zero:
198
195
            note("%d conflicts encountered.\n" % self.conflicts)
199
196
            
200
 
def get_tree(treespec, local_branch=None):
 
197
def get_tree(treespec, temp_root, label, local_branch=None):
201
198
    location, revno = treespec
202
199
    branch = Branch.open_containing(location)
203
200
    if revno is None:
206
203
        revision = branch.last_revision()
207
204
    else:
208
205
        revision = branch.get_rev_id(revno)
209
 
    return branch, get_revid_tree(branch, revision, local_branch)
 
206
    return branch, get_revid_tree(branch, revision, temp_root, label,
 
207
                                  local_branch)
210
208
 
211
 
def get_revid_tree(branch, revision, local_branch):
 
209
def get_revid_tree(branch, revision, temp_root, label, local_branch):
212
210
    if revision is None:
213
211
        base_tree = branch.working_tree()
214
212
    else:
217
215
            base_tree = local_branch.revision_tree(revision)
218
216
        else:
219
217
            base_tree = branch.revision_tree(revision)
220
 
    return base_tree
 
218
    temp_path = os.path.join(temp_root, label)
 
219
    os.mkdir(temp_path)
 
220
    return MergeTree(base_tree, temp_path)
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 MergeTree(object):
 
228
    def __init__(self, tree, tempdir):
 
229
        object.__init__(self)
 
230
        if hasattr(tree, "basedir"):
 
231
            self.root = tree.basedir
 
232
        else:
 
233
            self.root = None
 
234
        self.tree = tree
 
235
        self.tempdir = tempdir
 
236
        os.mkdir(os.path.join(self.tempdir, "texts"))
 
237
        os.mkdir(os.path.join(self.tempdir, "symlinks"))
 
238
        self.cached = {}
 
239
 
 
240
    def __iter__(self):
 
241
        return self.tree.__iter__()
 
242
 
 
243
    def __contains__(self, file_id):
 
244
        return file_id in self.tree
 
245
 
 
246
    def get_file(self, file_id):
 
247
        return self.tree.get_file(file_id)
 
248
 
 
249
    def get_file_sha1(self, id):
 
250
        return self.tree.get_file_sha1(id)
 
251
 
 
252
    def is_executable(self, id):
 
253
        return self.tree.is_executable(id)
 
254
 
 
255
    def id2path(self, file_id):
 
256
        return self.tree.id2path(file_id)
 
257
 
 
258
    def has_id(self, file_id):
 
259
        return self.tree.has_id(file_id)
 
260
 
 
261
    def has_or_had_id(self, file_id):
 
262
        if file_id == self.tree.inventory.root.file_id:
 
263
            return True
 
264
        return self.tree.inventory.has_id(file_id)
 
265
 
 
266
    def has_or_had_id(self, file_id):
 
267
        if file_id == self.tree.inventory.root.file_id:
 
268
            return True
 
269
        return self.tree.inventory.has_id(file_id)
 
270
 
 
271
    def readonly_path(self, id):
 
272
        if id not in self.tree:
 
273
            return None
 
274
        if self.root is not None:
 
275
            return self.tree.abspath(self.tree.id2path(id))
 
276
        else:
 
277
            kind = self.tree.inventory[id].kind
 
278
            if kind in ("directory", "root_directory"):
 
279
                return self.tempdir
 
280
            if not self.cached.has_key(id):
 
281
                if kind == "file":
 
282
                    path = os.path.join(self.tempdir, "texts", id)
 
283
                    outfile = file(path, "wb")
 
284
                    outfile.write(self.tree.get_file(id).read())
 
285
                    assert(bzrlib.osutils.lexists(path))
 
286
                    if self.tree.is_executable(id):
 
287
                        os.chmod(path, 0755)
 
288
                else:
 
289
                    assert kind == "symlink"
 
290
                    path = os.path.join(self.tempdir, "symlinks", id)
 
291
                    target = self.tree.get_symlink_target(id)
 
292
                    os.symlink(target, path)
 
293
                self.cached[id] = path
 
294
            return self.cached[id]
 
295
 
 
296
 
227
297
def build_working_dir(to_dir):
228
298
    """Build a working directory in an empty directory.
229
299
 
273
343
                                    this_branch.basis_tree(), False)
274
344
            if changes.has_changed():
275
345
                raise BzrCommandError("Working tree has uncommitted changes.")
276
 
        other_branch, other_tree = get_tree(other_revision, this_branch)
 
346
        other_branch, other_tree = get_tree(other_revision, tempdir, "other",
 
347
                                            this_branch)
277
348
        if other_revision[1] == -1:
278
349
            other_rev_id = other_branch.last_revision()
279
350
            if other_rev_id is None:
293
364
                                              this_branch)
294
365
            except NoCommonAncestor:
295
366
                raise UnrelatedBranches()
296
 
            base_tree = get_revid_tree(this_branch, base_rev_id, None)
 
367
            base_tree = get_revid_tree(this_branch, base_rev_id, tempdir, 
 
368
                                       "base", None)
297
369
            base_is_ancestor = True
298
370
        else:
299
 
            base_branch, base_tree = get_tree(base_revision)
 
371
            base_branch, base_tree = get_tree(base_revision, tempdir, "base")
300
372
            if base_revision[1] == -1:
301
373
                base_rev_id = base_branch.last_revision()
302
374
            elif base_revision[1] is None:
312
384
            interesting_ids = set()
313
385
            this_tree = this_branch.working_tree()
314
386
            for fname in file_list:
315
 
                path = this_tree.relpath(fname)
 
387
                path = this_branch.relpath(fname)
316
388
                found_id = False
317
 
                for tree in (this_tree, base_tree, other_tree):
 
389
                for tree in (this_tree, base_tree.tree, other_tree.tree):
318
390
                    file_id = tree.inventory.path2id(path)
319
391
                    if file_id is not None:
320
392
                        interesting_ids.add(file_id)
350
422
            contents_change = BackupBeforeChange(contents_change)
351
423
        return contents_change
352
424
 
353
 
    this_tree = get_tree((this_branch.base, None))[1]
 
425
    this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
354
426
 
355
427
    def get_inventory(tree):
356
 
        return tree.inventory
 
428
        return tree.tree.inventory
357
429
 
358
430
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
359
431
                             generate_changeset, get_inventory,
372
444
            path = path[2:]
373
445
        adjust_ids.append((path, id))
374
446
    if len(adjust_ids) > 0:
375
 
        this_branch.set_inventory(regen_inventory(this_branch, 
376
 
                                                  this_tree.basedir,
 
447
        this_branch.set_inventory(regen_inventory(this_branch, this_tree.root,
377
448
                                                  adjust_ids))
378
449
 
379
450