974.1.8
by Aaron Bentley
Added default backups for merge-revert |
1 |
from merge_core import merge_flex, ApplyMerge3, BackupBeforeChange |
493
by Martin Pool
- Merge aaron's merge command |
2 |
from changeset import generate_changeset, ExceptionConflictHandler |
974.1.3
by Aaron Bentley
Added merge_factory parameter to merge_flex |
3 |
from changeset import Inventory, Diff3Merge |
622
by Martin Pool
Updated merge patch from Aaron |
4 |
from bzrlib import find_branch |
493
by Martin Pool
- Merge aaron's merge command |
5 |
import bzrlib.osutils |
622
by Martin Pool
Updated merge patch from Aaron |
6 |
from bzrlib.errors import BzrCommandError |
7 |
from bzrlib.diff import compare_trees |
|
8 |
from trace import mutter, warning |
|
493
by Martin Pool
- Merge aaron's merge command |
9 |
import os.path |
10 |
import tempfile |
|
11 |
import shutil |
|
12 |
import errno |
|
13 |
||
622
by Martin Pool
Updated merge patch from Aaron |
14 |
class UnrelatedBranches(BzrCommandError): |
15 |
def __init__(self): |
|
16 |
msg = "Branches have no common ancestor, and no base revision"\ |
|
17 |
" specified."
|
|
18 |
BzrCommandError.__init__(self, msg) |
|
19 |
||
20 |
||
493
by Martin Pool
- Merge aaron's merge command |
21 |
class MergeConflictHandler(ExceptionConflictHandler): |
22 |
"""Handle conflicts encountered while merging"""
|
|
622
by Martin Pool
Updated merge patch from Aaron |
23 |
def __init__(self, dir, ignore_zero=False): |
24 |
ExceptionConflictHandler.__init__(self, dir) |
|
25 |
self.conflicts = 0 |
|
26 |
self.ignore_zero = ignore_zero |
|
27 |
||
493
by Martin Pool
- Merge aaron's merge command |
28 |
def copy(self, source, dest): |
29 |
"""Copy the text and mode of a file
|
|
30 |
:param source: The path of the file to copy
|
|
31 |
:param dest: The distination file to create
|
|
32 |
"""
|
|
33 |
s_file = file(source, "rb") |
|
34 |
d_file = file(dest, "wb") |
|
35 |
for line in s_file: |
|
36 |
d_file.write(line) |
|
37 |
os.chmod(dest, 0777 & os.stat(source).st_mode) |
|
38 |
||
39 |
def add_suffix(self, name, suffix, last_new_name=None): |
|
40 |
"""Rename a file to append a suffix. If the new name exists, the
|
|
41 |
suffix is added repeatedly until a non-existant name is found
|
|
42 |
||
43 |
:param name: The path of the file
|
|
44 |
:param suffix: The suffix to append
|
|
45 |
:param last_new_name: (used for recursive calls) the last name tried
|
|
46 |
"""
|
|
47 |
if last_new_name is None: |
|
48 |
last_new_name = name |
|
49 |
new_name = last_new_name+suffix |
|
50 |
try: |
|
51 |
os.rename(name, new_name) |
|
622
by Martin Pool
Updated merge patch from Aaron |
52 |
return new_name |
493
by Martin Pool
- Merge aaron's merge command |
53 |
except OSError, e: |
54 |
if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY: |
|
55 |
raise
|
|
622
by Martin Pool
Updated merge patch from Aaron |
56 |
return self.add_suffix(name, suffix, last_new_name=new_name) |
57 |
||
58 |
def conflict(self, text): |
|
59 |
warning(text) |
|
60 |
self.conflicts += 1 |
|
61 |
||
493
by Martin Pool
- Merge aaron's merge command |
62 |
|
63 |
def merge_conflict(self, new_file, this_path, base_path, other_path): |
|
64 |
"""
|
|
65 |
Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER. The
|
|
66 |
main file will be a version with diff3 conflicts.
|
|
67 |
:param new_file: Path to the output file with diff3 markers
|
|
68 |
:param this_path: Path to the file text for the THIS tree
|
|
69 |
:param base_path: Path to the file text for the BASE tree
|
|
70 |
:param other_path: Path to the file text for the OTHER tree
|
|
71 |
"""
|
|
72 |
self.add_suffix(this_path, ".THIS") |
|
73 |
self.copy(base_path, this_path+".BASE") |
|
74 |
self.copy(other_path, this_path+".OTHER") |
|
75 |
os.rename(new_file, this_path) |
|
622
by Martin Pool
Updated merge patch from Aaron |
76 |
self.conflict("Diff3 conflict encountered in %s" % this_path) |
493
by Martin Pool
- Merge aaron's merge command |
77 |
|
78 |
def target_exists(self, entry, target, old_path): |
|
79 |
"""Handle the case when the target file or dir exists"""
|
|
622
by Martin Pool
Updated merge patch from Aaron |
80 |
moved_path = self.add_suffix(target, ".moved") |
81 |
self.conflict("Moved existing %s to %s" % (target, moved_path)) |
|
82 |
||
850
by Martin Pool
- Merge merge updates from aaron |
83 |
def rmdir_non_empty(self, filename): |
84 |
"""Handle the case where the dir to be removed still has contents"""
|
|
85 |
self.conflict("Directory %s not removed because it is not empty"\ |
|
86 |
% filename) |
|
87 |
return "skip" |
|
88 |
||
622
by Martin Pool
Updated merge patch from Aaron |
89 |
def finalize(self): |
90 |
if not self.ignore_zero: |
|
91 |
print "%d conflicts encountered.\n" % self.conflicts |
|
493
by Martin Pool
- Merge aaron's merge command |
92 |
|
93 |
def get_tree(treespec, temp_root, label): |
|
622
by Martin Pool
Updated merge patch from Aaron |
94 |
location, revno = treespec |
95 |
branch = find_branch(location) |
|
493
by Martin Pool
- Merge aaron's merge command |
96 |
if revno is None: |
97 |
base_tree = branch.working_tree() |
|
98 |
elif revno == -1: |
|
99 |
base_tree = branch.basis_tree() |
|
100 |
else: |
|
101 |
base_tree = branch.revision_tree(branch.lookup_revision(revno)) |
|
102 |
temp_path = os.path.join(temp_root, label) |
|
103 |
os.mkdir(temp_path) |
|
622
by Martin Pool
Updated merge patch from Aaron |
104 |
return branch, MergeTree(base_tree, temp_path) |
493
by Martin Pool
- Merge aaron's merge command |
105 |
|
106 |
||
107 |
def file_exists(tree, file_id): |
|
108 |
return tree.has_filename(tree.id2path(file_id)) |
|
109 |
||
110 |
||
111 |
class MergeTree(object): |
|
112 |
def __init__(self, tree, tempdir): |
|
113 |
object.__init__(self) |
|
114 |
if hasattr(tree, "basedir"): |
|
115 |
self.root = tree.basedir |
|
116 |
else: |
|
117 |
self.root = None |
|
118 |
self.tree = tree |
|
119 |
self.tempdir = tempdir |
|
120 |
os.mkdir(os.path.join(self.tempdir, "texts")) |
|
121 |
self.cached = {} |
|
122 |
||
974.1.19
by Aaron Bentley
Removed MergeTree.inventory |
123 |
def __iter__(self): |
124 |
return self.tree.__iter__() |
|
125 |
||
974.1.12
by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization |
126 |
def __contains__(self, file_id): |
974.1.14
by aaron.bentley at utoronto
Fixed bugs in merge optimization |
127 |
return file_id in self.tree |
974.1.12
by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization |
128 |
|
129 |
def get_file_sha1(self, id): |
|
130 |
return self.tree.get_file_sha1(id) |
|
131 |
||
974.1.18
by Aaron Bentley
Stopped using SourceFile in inventory |
132 |
def id2path(self, file_id): |
133 |
return self.tree.id2path(file_id) |
|
134 |
||
135 |
def has_id(self, file_id): |
|
136 |
return self.tree.has_id(file_id) |
|
137 |
||
493
by Martin Pool
- Merge aaron's merge command |
138 |
def readonly_path(self, id): |
850
by Martin Pool
- Merge merge updates from aaron |
139 |
if id not in self.tree: |
140 |
return None |
|
493
by Martin Pool
- Merge aaron's merge command |
141 |
if self.root is not None: |
142 |
return self.tree.abspath(self.tree.id2path(id)) |
|
143 |
else: |
|
144 |
if self.tree.inventory[id].kind in ("directory", "root_directory"): |
|
145 |
return self.tempdir |
|
146 |
if not self.cached.has_key(id): |
|
147 |
path = os.path.join(self.tempdir, "texts", id) |
|
148 |
outfile = file(path, "wb") |
|
149 |
outfile.write(self.tree.get_file(id).read()) |
|
150 |
assert(os.path.exists(path)) |
|
151 |
self.cached[id] = path |
|
152 |
return self.cached[id] |
|
153 |
||
628
by Martin Pool
- merge aaron's updated merge/pull code |
154 |
|
155 |
||
156 |
def merge(other_revision, base_revision, |
|
157 |
check_clean=True, ignore_zero=False, |
|
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
158 |
this_dir=None, backup_files=False, merge_type=ApplyMerge3, |
159 |
file_list=None): |
|
628
by Martin Pool
- merge aaron's updated merge/pull code |
160 |
"""Merge changes into a tree.
|
161 |
||
162 |
base_revision
|
|
163 |
Base for three-way merge.
|
|
164 |
other_revision
|
|
165 |
Other revision for three-way merge.
|
|
166 |
this_dir
|
|
167 |
Directory to merge changes into; '.' by default.
|
|
168 |
check_clean
|
|
169 |
If true, this_dir must have no uncommitted changes before the
|
|
170 |
merge begins.
|
|
171 |
"""
|
|
493
by Martin Pool
- Merge aaron's merge command |
172 |
tempdir = tempfile.mkdtemp(prefix="bzr-") |
173 |
try: |
|
628
by Martin Pool
- merge aaron's updated merge/pull code |
174 |
if this_dir is None: |
175 |
this_dir = '.' |
|
176 |
this_branch = find_branch(this_dir) |
|
177 |
if check_clean: |
|
622
by Martin Pool
Updated merge patch from Aaron |
178 |
changes = compare_trees(this_branch.working_tree(), |
179 |
this_branch.basis_tree(), False) |
|
180 |
if changes.has_changed(): |
|
181 |
raise BzrCommandError("Working tree has uncommitted changes.") |
|
182 |
other_branch, other_tree = get_tree(other_revision, tempdir, "other") |
|
183 |
if base_revision == [None, None]: |
|
184 |
if other_revision[1] == -1: |
|
185 |
o_revno = None |
|
186 |
else: |
|
187 |
o_revno = other_revision[1] |
|
188 |
base_revno = this_branch.common_ancestor(other_branch, |
|
189 |
other_revno=o_revno)[0] |
|
190 |
if base_revno is None: |
|
191 |
raise UnrelatedBranches() |
|
192 |
base_revision = ['.', base_revno] |
|
193 |
base_branch, base_tree = get_tree(base_revision, tempdir, "base") |
|
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
194 |
if file_list is None: |
195 |
interesting_ids = None |
|
196 |
else: |
|
197 |
interesting_ids = set() |
|
198 |
this_tree = this_branch.working_tree() |
|
199 |
for fname in file_list: |
|
200 |
path = this_branch.relpath(fname) |
|
201 |
found_id = False |
|
202 |
for tree in (this_tree, base_tree.tree, other_tree.tree): |
|
203 |
file_id = tree.inventory.path2id(path) |
|
204 |
if file_id is not None: |
|
205 |
interesting_ids.add(file_id) |
|
206 |
found_id = True |
|
207 |
if not found_id: |
|
208 |
raise BzrCommandError("%s is not a source file in any" |
|
209 |
" tree." % fname) |
|
622
by Martin Pool
Updated merge patch from Aaron |
210 |
merge_inner(this_branch, other_tree, base_tree, tempdir, |
974.1.9
by Aaron Bentley
Added merge-type parameter to merge. |
211 |
ignore_zero=ignore_zero, backup_files=backup_files, |
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
212 |
merge_type=merge_type, interesting_ids=interesting_ids) |
493
by Martin Pool
- Merge aaron's merge command |
213 |
finally: |
214 |
shutil.rmtree(tempdir) |
|
215 |
||
216 |
||
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
217 |
def set_interesting(inventory_a, inventory_b, interesting_ids): |
218 |
"""Mark files whose ids are in interesting_ids as interesting
|
|
219 |
"""
|
|
220 |
for inventory in (inventory_a, inventory_b): |
|
221 |
for path, source_file in inventory.iteritems(): |
|
222 |
source_file.interesting = source_file.id in interesting_ids |
|
223 |
||
224 |
||
974.1.19
by Aaron Bentley
Removed MergeTree.inventory |
225 |
def generate_cset_optimized(tree_a, tree_b, interesting_ids=None): |
974.1.12
by aaron.bentley at utoronto
Switched from text-id to hashcache for merge optimization |
226 |
"""Generate a changeset. If interesting_ids is supplied, only changes
|
227 |
to those files will be shown. Metadata changes are stripped.
|
|
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
228 |
"""
|
974.1.19
by Aaron Bentley
Removed MergeTree.inventory |
229 |
cset = generate_changeset(tree_a, tree_b, interesting_ids) |
493
by Martin Pool
- Merge aaron's merge command |
230 |
for entry in cset.entries.itervalues(): |
231 |
entry.metadata_change = None |
|
232 |
return cset |
|
233 |
||
234 |
||
622
by Martin Pool
Updated merge patch from Aaron |
235 |
def merge_inner(this_branch, other_tree, base_tree, tempdir, |
974.1.10
by aaron.bentley at utoronto
Added file-selection to merge-revert |
236 |
ignore_zero=False, merge_type=ApplyMerge3, backup_files=False, |
237 |
interesting_ids=None): |
|
974.1.4
by Aaron Bentley
Implemented merge3 as the default text merge |
238 |
|
239 |
def merge_factory(base_file, other_file): |
|
974.1.9
by Aaron Bentley
Added merge-type parameter to merge. |
240 |
contents_change = merge_type(base_file, other_file) |
974.1.8
by Aaron Bentley
Added default backups for merge-revert |
241 |
if backup_files: |
242 |
contents_change = BackupBeforeChange(contents_change) |
|
243 |
return contents_change |
|
974.1.4
by Aaron Bentley
Implemented merge3 as the default text merge |
244 |
|
628
by Martin Pool
- merge aaron's updated merge/pull code |
245 |
this_tree = get_tree((this_branch.base, None), tempdir, "this")[1] |
493
by Martin Pool
- Merge aaron's merge command |
246 |
|
247 |
def get_inventory(tree): |
|
974.1.19
by Aaron Bentley
Removed MergeTree.inventory |
248 |
return tree.tree.inventory |
493
by Martin Pool
- Merge aaron's merge command |
249 |
|
250 |
inv_changes = merge_flex(this_tree, base_tree, other_tree, |
|
974.1.17
by Aaron Bentley
Switched to using a set of interesting file_ids instead of SourceFile attribute |
251 |
generate_cset_optimized, get_inventory, |
622
by Martin Pool
Updated merge patch from Aaron |
252 |
MergeConflictHandler(base_tree.root, |
974.1.3
by Aaron Bentley
Added merge_factory parameter to merge_flex |
253 |
ignore_zero=ignore_zero), |
974.1.17
by Aaron Bentley
Switched to using a set of interesting file_ids instead of SourceFile attribute |
254 |
merge_factory=merge_factory, |
255 |
interesting_ids=interesting_ids) |
|
493
by Martin Pool
- Merge aaron's merge command |
256 |
|
257 |
adjust_ids = [] |
|
258 |
for id, path in inv_changes.iteritems(): |
|
259 |
if path is not None: |
|
260 |
if path == '.': |
|
261 |
path = '' |
|
262 |
else: |
|
974.1.19
by Aaron Bentley
Removed MergeTree.inventory |
263 |
assert path.startswith('./'), "path is %s" % path |
493
by Martin Pool
- Merge aaron's merge command |
264 |
path = path[2:] |
265 |
adjust_ids.append((path, id)) |
|
974.1.13
by aaron.bentley at utoronto
Avoid rewriting inventory when not necessary |
266 |
if len(adjust_ids) > 0: |
267 |
this_branch.set_inventory(regen_inventory(this_branch, this_tree.root, |
|
268 |
adjust_ids)) |
|
493
by Martin Pool
- Merge aaron's merge command |
269 |
|
270 |
||
271 |
def regen_inventory(this_branch, root, new_entries): |
|
272 |
old_entries = this_branch.read_working_inventory() |
|
273 |
new_inventory = {} |
|
274 |
by_path = {} |
|
275 |
for file_id in old_entries: |
|
276 |
entry = old_entries[file_id] |
|
277 |
path = old_entries.id2path(file_id) |
|
278 |
new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind) |
|
279 |
by_path[path] = file_id |
|
280 |
||
281 |
deletions = 0 |
|
282 |
insertions = 0 |
|
283 |
new_path_list = [] |
|
284 |
for path, file_id in new_entries: |
|
285 |
if path is None: |
|
286 |
del new_inventory[file_id] |
|
287 |
deletions += 1 |
|
288 |
else: |
|
289 |
new_path_list.append((path, file_id)) |
|
290 |
if file_id not in old_entries: |
|
291 |
insertions += 1 |
|
292 |
# Ensure no file is added before its parent
|
|
293 |
new_path_list.sort() |
|
294 |
for path, file_id in new_path_list: |
|
295 |
if path == '': |
|
296 |
parent = None |
|
297 |
else: |
|
298 |
parent = by_path[os.path.dirname(path)] |
|
299 |
kind = bzrlib.osutils.file_kind(os.path.join(root, path)) |
|
300 |
new_inventory[file_id] = (path, file_id, parent, kind) |
|
301 |
by_path[path] = file_id |
|
302 |
||
303 |
# Get a list in insertion order
|
|
304 |
new_inventory_list = new_inventory.values() |
|
305 |
mutter ("""Inventory regeneration: |
|
306 |
old length: %i insertions: %i deletions: %i new_length: %i"""\ |
|
307 |
% (len(old_entries), insertions, deletions, len(new_inventory_list))) |
|
308 |
assert len(new_inventory_list) == len(old_entries) + insertions - deletions |
|
309 |
new_inventory_list.sort() |
|
310 |
return new_inventory_list |
|
974.1.9
by Aaron Bentley
Added merge-type parameter to merge. |
311 |
|
312 |
merge_types = { "merge3": (ApplyMerge3, "Native diff3-style merge"), |
|
313 |
"diff3": (Diff3Merge, "Merge using external diff3") |
|
314 |
}
|
|
315 |