/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Robert Collins
  • Date: 2005-08-24 08:34:10 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050824083410-98aa4eeb52653394
import and use TestUtil to do regex based partial test runs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
2
 
from changeset import generate_changeset, ExceptionConflictHandler
3
 
from changeset import Inventory, Diff3Merge
 
1
from bzrlib.merge_core import merge_flex, ApplyMerge3, BackupBeforeChange
 
2
from bzrlib.changeset import generate_changeset, ExceptionConflictHandler
 
3
from bzrlib.changeset import Inventory, Diff3Merge
4
4
from bzrlib import find_branch
5
5
import bzrlib.osutils
6
6
from bzrlib.errors import BzrCommandError
11
11
import shutil
12
12
import errno
13
13
 
 
14
 
 
15
# comments from abentley on irc: merge happens in two stages, each
 
16
# of which generates a changeset object
 
17
 
 
18
# stage 1: generate OLD->OTHER,
 
19
# stage 2: use MINE and OLD->OTHER to generate MINE -> RESULT
 
20
 
14
21
class UnrelatedBranches(BzrCommandError):
15
22
    def __init__(self):
16
23
        msg = "Branches have no common ancestor, and no base revision"\
19
26
 
20
27
 
21
28
class MergeConflictHandler(ExceptionConflictHandler):
22
 
    """Handle conflicts encountered while merging"""
 
29
    """Handle conflicts encountered while merging.
 
30
 
 
31
    This subclasses ExceptionConflictHandler, so that any types of
 
32
    conflict that are not explicitly handled cause an exception and
 
33
    terminate the merge.
 
34
    """
23
35
    def __init__(self, dir, ignore_zero=False):
24
36
        ExceptionConflictHandler.__init__(self, dir)
25
37
        self.conflicts = 0
36
48
            d_file.write(line)
37
49
        os.chmod(dest, 0777 & os.stat(source).st_mode)
38
50
 
 
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
 
55
        """
 
56
        d_file = file(dest, "wb")
 
57
        for line in lines:
 
58
            d_file.write(line)
 
59
 
39
60
    def add_suffix(self, name, suffix, last_new_name=None):
40
61
        """Rename a file to append a suffix.  If the new name exists, the
41
62
        suffix is added repeatedly until a non-existant name is found
60
81
        self.conflicts += 1
61
82
        
62
83
 
63
 
    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):
64
85
        """
65
86
        Handle diff3 conflicts by producing a .THIS, .BASE and .OTHER.  The
66
87
        main file will be a version with diff3 conflicts.
70
91
        :param other_path: Path to the file text for the OTHER tree
71
92
        """
72
93
        self.add_suffix(this_path, ".THIS")
73
 
        self.copy(base_path, this_path+".BASE")
74
 
        self.copy(other_path, this_path+".OTHER")
 
94
        self.dump(base_lines, this_path+".BASE")
 
95
        self.dump(other_lines, this_path+".OTHER")
75
96
        os.rename(new_file, this_path)
76
97
        self.conflict("Diff3 conflict encountered in %s" % this_path)
77
98
 
 
99
    def new_contents_conflict(self, filename, other_contents):
 
100
        """Conflicting contents for newly added file."""
 
101
        self.copy(other_contents, filename + ".OTHER")
 
102
        self.conflict("Conflict in newly added file %s" % filename)
 
103
    
 
104
 
78
105
    def target_exists(self, entry, target, old_path):
79
106
        """Handle the case when the target file or dir exists"""
80
107
        moved_path = self.add_suffix(target, ".moved")
90
117
        if not self.ignore_zero:
91
118
            print "%d conflicts encountered.\n" % self.conflicts
92
119
            
93
 
class SourceFile(object):
94
 
    def __init__(self, path, id, present=None, isdir=None):
95
 
        self.path = path
96
 
        self.id = id
97
 
        self.present = present
98
 
        self.isdir = isdir
99
 
        self.interesting = True
100
 
 
101
 
    def __repr__(self):
102
 
        return "SourceFile(%s, %s)" % (self.path, self.id)
103
 
 
104
120
def get_tree(treespec, temp_root, label):
105
121
    location, revno = treespec
106
122
    branch = find_branch(location)
115
131
    return branch, MergeTree(base_tree, temp_path)
116
132
 
117
133
 
118
 
def abspath(tree, file_id):
119
 
    path = tree.inventory.id2path(file_id)
120
 
    if path == "":
121
 
        return "./."
122
 
    return "./" + path
123
 
 
124
134
def file_exists(tree, file_id):
125
135
    return tree.has_filename(tree.id2path(file_id))
126
136
    
127
 
def inventory_map(tree):
128
 
    inventory = {}
129
 
    for file_id in tree.inventory:
130
 
        path = abspath(tree, file_id)
131
 
        inventory[path] = SourceFile(path, file_id)
132
 
    return inventory
133
 
 
134
137
 
135
138
class MergeTree(object):
136
139
    def __init__(self, tree, tempdir):
139
142
            self.root = tree.basedir
140
143
        else:
141
144
            self.root = None
142
 
        self.inventory = inventory_map(tree)
143
145
        self.tree = tree
144
146
        self.tempdir = tempdir
145
147
        os.mkdir(os.path.join(self.tempdir, "texts"))
146
148
        self.cached = {}
147
149
 
 
150
    def __iter__(self):
 
151
        return self.tree.__iter__()
 
152
 
 
153
    def __contains__(self, file_id):
 
154
        return file_id in self.tree
 
155
 
 
156
    def get_file(self, file_id):
 
157
        return self.tree.get_file(file_id)
 
158
 
 
159
    def get_file_sha1(self, id):
 
160
        return self.tree.get_file_sha1(id)
 
161
 
 
162
    def id2path(self, file_id):
 
163
        return self.tree.id2path(file_id)
 
164
 
 
165
    def has_id(self, file_id):
 
166
        return self.tree.has_id(file_id)
 
167
 
 
168
    def has_or_had_id(self, file_id):
 
169
        if file_id == self.tree.inventory.root.file_id:
 
170
            return True
 
171
        return self.tree.inventory.has_id(file_id)
 
172
 
148
173
    def readonly_path(self, id):
149
174
        if id not in self.tree:
150
175
            return None
232
257
             source_file.interesting = source_file.id in interesting_ids
233
258
 
234
259
 
235
 
def set_optimized(tree_a, tree_b, inventory_a, inventory_b):
236
 
    """Mark files that have changed texts as interesting
237
 
    """
238
 
    for file_id in tree_a.tree.inventory:
239
 
        if file_id not in tree_b.tree.inventory:
240
 
            continue
241
 
        entry_a = tree_a.tree.inventory[file_id]
242
 
        entry_b = tree_b.tree.inventory[file_id]
243
 
        if (entry_a.kind, entry_b.kind) != ("file", "file"):
244
 
            continue
245
 
        if None in (entry_a.text_id, entry_b.text_id):
246
 
            continue
247
 
        if entry_a.text_id != entry_b.text_id:
248
 
            continue
249
 
        inventory_a[abspath(tree_a.tree, file_id)].interesting = False
250
 
        inventory_b[abspath(tree_b.tree, file_id)].interesting = False
251
 
 
252
 
 
253
 
def generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b,
254
 
                            interesting_ids=None):
255
 
    """Generate a changeset, with preprocessing to select interesting files.
256
 
    using the text_id to mark really-changed files.
257
 
    This permits blazing comparisons when text_ids are present.  It also
258
 
    disables metadata comparison for files with identical texts.
 
260
def generate_cset_optimized(tree_a, tree_b, interesting_ids=None):
 
261
    """Generate a changeset.  If interesting_ids is supplied, only changes
 
262
    to those files will be shown.  Metadata changes are stripped.
259
263
    """ 
260
 
    if interesting_ids is None:
261
 
        set_optimized(tree_a, tree_b, inventory_a, inventory_b)
262
 
    else:
263
 
        set_interesting(inventory_a, inventory_b, interesting_ids)
264
 
    cset =  generate_changeset(tree_a, tree_b, inventory_a, inventory_b)
 
264
    cset =  generate_changeset(tree_a, tree_b, interesting_ids)
265
265
    for entry in cset.entries.itervalues():
266
266
        entry.metadata_change = None
267
267
    return cset
271
271
                ignore_zero=False, merge_type=ApplyMerge3, backup_files=False,
272
272
                interesting_ids=None):
273
273
 
274
 
    def merge_factory(base_file, other_file):
275
 
        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)
276
276
        if backup_files:
277
277
            contents_change = BackupBeforeChange(contents_change)
278
278
        return contents_change
279
 
    
280
 
    def generate_cset(tree_a, tree_b, inventory_a, inventory_b):
281
 
        return generate_cset_optimized(tree_a, tree_b, inventory_a, inventory_b,
282
 
                                       interesting_ids)
283
279
 
284
280
    this_tree = get_tree((this_branch.base, None), tempdir, "this")[1]
285
281
 
286
282
    def get_inventory(tree):
287
 
        return tree.inventory
 
283
        return tree.tree.inventory
288
284
 
289
285
    inv_changes = merge_flex(this_tree, base_tree, other_tree,
290
 
                             generate_cset, get_inventory,
 
286
                             generate_cset_optimized, get_inventory,
291
287
                             MergeConflictHandler(base_tree.root,
292
288
                                                  ignore_zero=ignore_zero),
293
 
                             merge_factory=merge_factory)
 
289
                             merge_factory=merge_factory, 
 
290
                             interesting_ids=interesting_ids)
294
291
 
295
292
    adjust_ids = []
296
293
    for id, path in inv_changes.iteritems():
298
295
            if path == '.':
299
296
                path = ''
300
297
            else:
301
 
                assert path.startswith('./')
 
298
                assert path.startswith('./'), "path is %s" % path
302
299
            path = path[2:]
303
300
        adjust_ids.append((path, id))
304
 
    this_branch.set_inventory(regen_inventory(this_branch, this_tree.root, adjust_ids))
 
301
    if len(adjust_ids) > 0:
 
302
        this_branch.set_inventory(regen_inventory(this_branch, this_tree.root,
 
303
                                                  adjust_ids))
305
304
 
306
305
 
307
306
def regen_inventory(this_branch, root, new_entries):
308
307
    old_entries = this_branch.read_working_inventory()
309
308
    new_inventory = {}
310
309
    by_path = {}
 
310
    new_entries_map = {} 
 
311
    for path, file_id in new_entries:
 
312
        if path is None:
 
313
            continue
 
314
        new_entries_map[file_id] = path
 
315
 
 
316
    def id2path(file_id):
 
317
        path = new_entries_map.get(file_id)
 
318
        if path is not None:
 
319
            return path
 
320
        entry = old_entries[file_id]
 
321
        if entry.parent_id is None:
 
322
            return entry.name
 
323
        return os.path.join(id2path(entry.parent_id), entry.name)
 
324
        
311
325
    for file_id in old_entries:
312
326
        entry = old_entries[file_id]
313
 
        path = old_entries.id2path(file_id)
 
327
        path = id2path(file_id)
314
328
        new_inventory[file_id] = (path, file_id, entry.parent_id, entry.kind)
315
329
        by_path[path] = file_id
316
330