49
49
os.chmod(dest, 0777 & os.stat(source).st_mode)
51
def dump(self, lines, dest):
52
"""Copy the text and mode of a file
53
:param source: The path of the file to copy
54
:param dest: The distination file to create
56
d_file = file(dest, "wb")
51
60
def add_suffix(self, name, suffix, last_new_name=None):
52
61
"""Rename a file to append a suffix. If the new name exists, the
53
62
suffix is added repeatedly until a non-existant name is found
72
81
self.conflicts += 1
75
def merge_conflict(self, new_file, this_path, base_path, other_path):
84
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
77
86
Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER. The
78
87
main file will be a version with diff3 conflicts.
82
91
:param other_path: Path to the file text for the OTHER tree
84
93
self.add_suffix(this_path, ".THIS")
85
self.copy(base_path, this_path+".BASE")
86
self.copy(other_path, this_path+".OTHER")
94
self.dump(base_lines, this_path+".BASE")
95
self.dump(other_lines, this_path+".OTHER")
87
96
os.rename(new_file, this_path)
88
97
self.conflict("Diff3 conflict encountered in %s" % this_path)
108
117
if not self.ignore_zero:
109
118
print "%d conflicts encountered.\n" % self.conflicts
111
class SourceFile(object):
112
def __init__(self, path, id, present=None, isdir=None):
115
self.present = present
117
self.interesting = True
120
return "SourceFile(%s, %s)" % (self.path, self.id)
122
120
def get_tree(treespec, temp_root, label):
123
121
location, revno = treespec
124
122
branch = find_branch(location)
133
131
return branch, MergeTree(base_tree, temp_path)
136
def abspath(tree, file_id):
137
path = tree.inventory.id2path(file_id)
142
134
def file_exists(tree, file_id):
143
135
return tree.has_filename(tree.id2path(file_id))
145
def inventory_map(tree):
147
for file_id in tree.inventory:
148
path = abspath(tree, file_id)
149
inventory[path] = SourceFile(path, file_id)
153
138
class MergeTree(object):
154
139
def __init__(self, tree, tempdir):
157
142
self.root = tree.basedir
160
self.inventory = inventory_map(tree)
162
146
self.tempdir = tempdir
163
147
os.mkdir(os.path.join(self.tempdir, "texts"))
151
return self.tree.__iter__()
166
153
def __contains__(self, file_id):
167
return id in self.tree
154
return file_id in self.tree
156
def get_file(self, file_id):
157
return self.tree.get_file(file_id)
169
159
def get_file_sha1(self, id):
170
160
return self.tree.get_file_sha1(id)
162
def id2path(self, file_id):
163
return self.tree.id2path(file_id)
165
def has_id(self, file_id):
166
return self.tree.has_id(file_id)
168
def has_or_had_id(self, file_id):
169
if file_id == self.tree.inventory.root.file_id:
171
return self.tree.inventory.has_id(file_id)
172
173
def readonly_path(self, id):
173
174
if id not in self.tree:
256
257
source_file.interesting = source_file.id in interesting_ids
259
def generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b,
260
interesting_ids=None):
260
def generate_cset_optimized(tree_a, tree_b, interesting_ids=None):
261
261
"""Generate a changeset. If interesting_ids is supplied, only changes
262
262
to those files will be shown. Metadata changes are stripped.
264
if interesting_ids is not None:
265
set_interesting(inventory_a, inventory_b, interesting_ids)
266
cset = generate_changeset(tree_a, tree_b, inventory_a, inventory_b)
264
cset = generate_changeset(tree_a, tree_b, interesting_ids)
267
265
for entry in cset.entries.itervalues():
268
266
entry.metadata_change = None
273
271
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
274
272
interesting_ids=None):
276
def merge_factory(base_file, other_file):
277
contents_change = merge_type(base_file, other_file)
274
def merge_factory(file_id, base, other):
275
contents_change = merge_type(file_id, base, other)
279
277
contents_change = BackupBeforeChange(contents_change)
280
278
return contents_change
282
def generate_cset(tree_a, tree_b, inventory_a, inventory_b):
283
return generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b,
286
280
this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
288
282
def get_inventory(tree):
289
return tree.inventory
283
return tree.tree.inventory
291
285
inv_changes = merge_flex(this_tree, base_tree, other_tree,
292
generate_cset, get_inventory,
286
generate_cset_optimized, get_inventory,
293
287
MergeConflictHandler(base_tree.root,
294
288
ignore_zero=ignore_zero),
295
merge_factory=merge_factory)
289
merge_factory=merge_factory,
290
interesting_ids=interesting_ids)
298
293
for id, path in inv_changes.iteritems():
312
307
old_entries = this_branch.read_working_inventory()
313
308
new_inventory = {}
311
for path, file_id in new_entries:
314
new_entries_map[file_id] = path
316
def id2path(file_id):
317
path = new_entries_map.get(file_id)
320
entry = old_entries[file_id]
321
if entry.parent_id is None:
323
return os.path.join(id2path(entry.parent_id), entry.name)
315
325
for file_id in old_entries:
316
326
entry = old_entries[file_id]
317
path = old_entries.id2path(file_id)
327
path = id2path(file_id)
318
328
new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind)
319
329
by_path[path] = file_id