bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
493
by Martin Pool
 - Merge aaron's merge command  | 
1  | 
from merge_core import merge_flex  | 
2  | 
from changeset import generate_changeset, ExceptionConflictHandler  | 
|
3  | 
from changeset import Inventory  | 
|
4  | 
from bzrlib import Branch  | 
|
5  | 
import bzrlib.osutils  | 
|
6  | 
from trace import mutter  | 
|
7  | 
import os.path  | 
|
8  | 
import tempfile  | 
|
9  | 
import shutil  | 
|
10  | 
import errno  | 
|
11  | 
||
12  | 
class MergeConflictHandler(ExceptionConflictHandler):  | 
|
13  | 
"""Handle conflicts encountered while merging"""  | 
|
14  | 
def copy(self, source, dest):  | 
|
15  | 
"""Copy the text and mode of a file  | 
|
16  | 
        :param source: The path of the file to copy
 | 
|
17  | 
        :param dest: The distination file to create
 | 
|
18  | 
        """
 | 
|
19  | 
s_file = file(source, "rb")  | 
|
20  | 
d_file = file(dest, "wb")  | 
|
21  | 
for line in s_file:  | 
|
22  | 
d_file.write(line)  | 
|
23  | 
os.chmod(dest, 0777 & os.stat(source).st_mode)  | 
|
24  | 
||
25  | 
def add_suffix(self, name, suffix, last_new_name=None):  | 
|
26  | 
"""Rename a file to append a suffix. If the new name exists, the  | 
|
27  | 
        suffix is added repeatedly until a non-existant name is found
 | 
|
28  | 
||
29  | 
        :param name: The path of the file
 | 
|
30  | 
        :param suffix: The suffix to append
 | 
|
31  | 
        :param last_new_name: (used for recursive calls) the last name tried
 | 
|
32  | 
        """
 | 
|
33  | 
if last_new_name is None:  | 
|
34  | 
last_new_name = name  | 
|
35  | 
new_name = last_new_name+suffix  | 
|
36  | 
try:  | 
|
37  | 
os.rename(name, new_name)  | 
|
38  | 
except OSError, e:  | 
|
39  | 
if e.errno != errno.EEXIST and e.errno != errno.ENOTEMPTY:  | 
|
40  | 
                raise
 | 
|
41  | 
self.add_suffix(name, suffix, last_new_name=new_name)  | 
|
42  | 
||
43  | 
def merge_conflict(self, new_file, this_path, base_path, other_path):  | 
|
44  | 
"""  | 
|
45  | 
        Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER.  The
 | 
|
46  | 
        main file will be a version with diff3 conflicts.
 | 
|
47  | 
        :param new_file: Path to the output file with diff3 markers
 | 
|
48  | 
        :param this_path: Path to the file text for the THIS tree
 | 
|
49  | 
        :param base_path: Path to the file text for the BASE tree
 | 
|
50  | 
        :param other_path: Path to the file text for the OTHER tree
 | 
|
51  | 
        """
 | 
|
52  | 
self.add_suffix(this_path, ".THIS")  | 
|
53  | 
self.copy(base_path, this_path+".BASE")  | 
|
54  | 
self.copy(other_path, this_path+".OTHER")  | 
|
55  | 
os.rename(new_file, this_path)  | 
|
56  | 
||
57  | 
def target_exists(self, entry, target, old_path):  | 
|
58  | 
"""Handle the case when the target file or dir exists"""  | 
|
59  | 
self.add_suffix(target, ".moved")  | 
|
60  | 
||
61  | 
class SourceFile:  | 
|
62  | 
def __init__(self, path, id, present=None, isdir=None):  | 
|
63  | 
self.path = path  | 
|
64  | 
self.id = id  | 
|
65  | 
self.present = present  | 
|
66  | 
self.isdir = isdir  | 
|
67  | 
self.interesting = True  | 
|
68  | 
||
69  | 
def __repr__(self):  | 
|
70  | 
return "SourceFile(%s, %s)" % (self.path, self.id)  | 
|
71  | 
||
72  | 
def get_tree(treespec, temp_root, label):  | 
|
73  | 
dir, revno = treespec  | 
|
74  | 
branch = Branch(dir)  | 
|
75  | 
if revno is None:  | 
|
76  | 
base_tree = branch.working_tree()  | 
|
77  | 
elif revno == -1:  | 
|
78  | 
base_tree = branch.basis_tree()  | 
|
79  | 
else:  | 
|
80  | 
base_tree = branch.revision_tree(branch.lookup_revision(revno))  | 
|
81  | 
temp_path = os.path.join(temp_root, label)  | 
|
82  | 
os.mkdir(temp_path)  | 
|
83  | 
return MergeTree(base_tree, temp_path)  | 
|
84  | 
||
85  | 
||
86  | 
def abspath(tree, file_id):  | 
|
87  | 
path = tree.inventory.id2path(file_id)  | 
|
88  | 
if path == "":  | 
|
89  | 
return "./."  | 
|
90  | 
return "./" + path  | 
|
91  | 
||
92  | 
def file_exists(tree, file_id):  | 
|
93  | 
return tree.has_filename(tree.id2path(file_id))  | 
|
94  | 
||
95  | 
def inventory_map(tree):  | 
|
96  | 
inventory = {}  | 
|
97  | 
for file_id in tree.inventory:  | 
|
98  | 
if not file_exists(tree, file_id):  | 
|
99  | 
            continue
 | 
|
100  | 
path = abspath(tree, file_id)  | 
|
101  | 
inventory[path] = SourceFile(path, file_id)  | 
|
102  | 
return inventory  | 
|
103  | 
||
104  | 
||
105  | 
class MergeTree(object):  | 
|
106  | 
def __init__(self, tree, tempdir):  | 
|
107  | 
object.__init__(self)  | 
|
108  | 
if hasattr(tree, "basedir"):  | 
|
109  | 
self.root = tree.basedir  | 
|
110  | 
else:  | 
|
111  | 
self.root = None  | 
|
112  | 
self.inventory = inventory_map(tree)  | 
|
113  | 
self.tree = tree  | 
|
114  | 
self.tempdir = tempdir  | 
|
115  | 
os.mkdir(os.path.join(self.tempdir, "texts"))  | 
|
116  | 
self.cached = {}  | 
|
117  | 
||
118  | 
def readonly_path(self, id):  | 
|
119  | 
if self.root is not None:  | 
|
120  | 
return self.tree.abspath(self.tree.id2path(id))  | 
|
121  | 
else:  | 
|
122  | 
if self.tree.inventory[id].kind in ("directory", "root_directory"):  | 
|
123  | 
return self.tempdir  | 
|
124  | 
if not self.cached.has_key(id):  | 
|
125  | 
path = os.path.join(self.tempdir, "texts", id)  | 
|
126  | 
outfile = file(path, "wb")  | 
|
127  | 
outfile.write(self.tree.get_file(id).read())  | 
|
128  | 
assert(os.path.exists(path))  | 
|
129  | 
self.cached[id] = path  | 
|
130  | 
return self.cached[id]  | 
|
131  | 
||
132  | 
def merge(other_revision, base_revision):  | 
|
133  | 
tempdir = tempfile.mkdtemp(prefix="bzr-")  | 
|
134  | 
try:  | 
|
135  | 
this_branch = Branch('.')  | 
|
136  | 
other_tree = get_tree(other_revision, tempdir, "other")  | 
|
137  | 
base_tree = get_tree(base_revision, tempdir, "base")  | 
|
138  | 
merge_inner(this_branch, other_tree, base_tree, tempdir)  | 
|
139  | 
finally:  | 
|
140  | 
shutil.rmtree(tempdir)  | 
|
141  | 
||
142  | 
||
143  | 
def generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b):  | 
|
144  | 
"""Generate a changeset, using the text_id to mark really-changed files.  | 
|
145  | 
    This permits blazing comparisons when text_ids are present.  It also
 | 
|
146  | 
    disables metadata comparison for files with identical texts.
 | 
|
147  | 
    """ 
 | 
|
148  | 
for file_id in tree_a.tree.inventory:  | 
|
149  | 
if file_id not in tree_b.tree.inventory:  | 
|
150  | 
            continue
 | 
|
151  | 
entry_a = tree_a.tree.inventory[file_id]  | 
|
152  | 
entry_b = tree_b.tree.inventory[file_id]  | 
|
153  | 
if (entry_a.kind, entry_b.kind) != ("file", "file"):  | 
|
154  | 
            continue
 | 
|
155  | 
if None in (entry_a.text_id, entry_b.text_id):  | 
|
156  | 
            continue
 | 
|
157  | 
if entry_a.text_id != entry_b.text_id:  | 
|
158  | 
            continue
 | 
|
159  | 
inventory_a[abspath(tree_a.tree, file_id)].interesting = False  | 
|
160  | 
inventory_b[abspath(tree_b.tree, file_id)].interesting = False  | 
|
161  | 
cset = generate_changeset(tree_a, tree_b, inventory_a, inventory_b)  | 
|
162  | 
for entry in cset.entries.itervalues():  | 
|
163  | 
entry.metadata_change = None  | 
|
164  | 
return cset  | 
|
165  | 
||
166  | 
||
167  | 
def merge_inner(this_branch, other_tree, base_tree, tempdir):  | 
|
168  | 
this_tree = get_tree(('.', None), tempdir, "this")  | 
|
169  | 
||
170  | 
def get_inventory(tree):  | 
|
171  | 
return tree.inventory  | 
|
172  | 
||
173  | 
inv_changes = merge_flex(this_tree, base_tree, other_tree,  | 
|
174  | 
generate_cset_optimized, get_inventory,  | 
|
175  | 
MergeConflictHandler(base_tree.root))  | 
|
176  | 
||
177  | 
adjust_ids = []  | 
|
178  | 
for id, path in inv_changes.iteritems():  | 
|
179  | 
if path is not None:  | 
|
180  | 
if path == '.':  | 
|
181  | 
path = ''  | 
|
182  | 
else:  | 
|
183  | 
assert path.startswith('./')  | 
|
184  | 
path = path[2:]  | 
|
185  | 
adjust_ids.append((path, id))  | 
|
186  | 
this_branch.set_inventory(regen_inventory(this_branch, this_tree.root, adjust_ids))  | 
|
187  | 
||
188  | 
||
189  | 
def regen_inventory(this_branch, root, new_entries):  | 
|
190  | 
old_entries = this_branch.read_working_inventory()  | 
|
191  | 
new_inventory = {}  | 
|
192  | 
by_path = {}  | 
|
193  | 
for file_id in old_entries:  | 
|
194  | 
entry = old_entries[file_id]  | 
|
195  | 
path = old_entries.id2path(file_id)  | 
|
196  | 
new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind)  | 
|
197  | 
by_path[path] = file_id  | 
|
198  | 
||
199  | 
deletions = 0  | 
|
200  | 
insertions = 0  | 
|
201  | 
new_path_list = []  | 
|
202  | 
for path, file_id in new_entries:  | 
|
203  | 
if path is None:  | 
|
204  | 
del new_inventory[file_id]  | 
|
205  | 
deletions += 1  | 
|
206  | 
else:  | 
|
207  | 
new_path_list.append((path, file_id))  | 
|
208  | 
if file_id not in old_entries:  | 
|
209  | 
insertions += 1  | 
|
210  | 
    # Ensure no file is added before its parent
 | 
|
211  | 
new_path_list.sort()  | 
|
212  | 
for path, file_id in new_path_list:  | 
|
213  | 
if path == '':  | 
|
214  | 
parent = None  | 
|
215  | 
else:  | 
|
216  | 
parent = by_path[os.path.dirname(path)]  | 
|
217  | 
kind = bzrlib.osutils.file_kind(os.path.join(root, path))  | 
|
218  | 
new_inventory[file_id] = (path, file_id, parent, kind)  | 
|
219  | 
by_path[path] = file_id  | 
|
220  | 
||
221  | 
    # Get a list in insertion order
 | 
|
222  | 
new_inventory_list = new_inventory.values()  | 
|
223  | 
mutter ("""Inventory regeneration:  | 
|
224  | 
old length: %i insertions: %i deletions: %i new_length: %i"""\  | 
|
225  | 
% (len(old_entries), insertions, deletions, len(new_inventory_list)))  | 
|
226  | 
assert len(new_inventory_list) == len(old_entries) + insertions - deletions  | 
|
227  | 
new_inventory_list.sort()  | 
|
228  | 
return new_inventory_list  |