/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
493 by Martin Pool
- Merge aaron's merge command
1
import changeset
2
from changeset import Inventory, apply_changeset, invert_dict
3
import os.path
4
558 by Martin Pool
- All top-level classes inherit from object
5
class ThreewayInventory(object):
493 by Martin Pool
- Merge aaron's merge command
6
    def __init__(self, this_inventory, base_inventory, other_inventory):
7
        self.this = this_inventory
8
        self.base = base_inventory
9
        self.other = other_inventory
10
def invert_invent(inventory):
11
    invert_invent = {}
12
    for key, value in inventory.iteritems():
13
        invert_invent[value.id] = key
14
    return invert_invent
15
16
def make_inv(inventory):
17
    return Inventory(invert_invent(inventory))
18
        
19
20
def merge_flex(this, base, other, changeset_function, inventory_function,
21
               conflict_handler):
22
    this_inventory = inventory_function(this)
23
    base_inventory = inventory_function(base)
24
    other_inventory = inventory_function(other)
25
    inventory = ThreewayInventory(make_inv(this_inventory),
26
                                  make_inv(base_inventory), 
27
                                  make_inv(other_inventory))
28
    cset = changeset_function(base, other, base_inventory, other_inventory)
29
    new_cset = make_merge_changeset(cset, inventory, this, base, other, 
30
                                    conflict_handler)
622 by Martin Pool
Updated merge patch from Aaron
31
    result = apply_changeset(new_cset, invert_invent(this_inventory),
32
                             this.root, conflict_handler, False)
33
    conflict_handler.finalize()
34
    return result
493 by Martin Pool
- Merge aaron's merge command
35
36
    
37
38
def make_merge_changeset(cset, inventory, this, base, other, 
39
                         conflict_handler=None):
40
    new_cset = changeset.Changeset()
41
    def get_this_contents(id):
42
        path = os.path.join(this.root, inventory.this.get_path(id))
43
        if os.path.isdir(path):
44
            return changeset.dir_create
45
        else:
46
            return changeset.FileCreate(file(path, "rb").read())
47
48
    for entry in cset.entries.itervalues():
49
        if entry.is_boring():
50
            new_cset.add_entry(entry)
51
        else:
850 by Martin Pool
- Merge merge updates from aaron
52
            new_entry = make_merged_entry(entry, inventory, conflict_handler)
53
            new_contents = make_merged_contents(entry, this, base, other,
54
                                                conflict_handler)
55
            new_entry.contents_change = new_contents
56
            new_entry.metadata_change = make_merged_metadata(entry, base, other)
57
            new_cset.add_entry(new_entry)
58
493 by Martin Pool
- Merge aaron's merge command
59
    return new_cset
60
850 by Martin Pool
- Merge merge updates from aaron
61
def make_merged_entry(entry, inventory, conflict_handler):
913 by Martin Pool
- merge aaron's merge-rename fix
62
    from bzrlib.trace import mutter
493 by Martin Pool
- Merge aaron's merge command
63
    this_name = inventory.this.get_name(entry.id)
64
    this_parent = inventory.this.get_parent(entry.id)
65
    this_dir = inventory.this.get_dir(entry.id)
66
    if this_dir is None:
67
        this_dir = ""
68
69
    base_name = inventory.base.get_name(entry.id)
70
    base_parent = inventory.base.get_parent(entry.id)
71
    base_dir = inventory.base.get_dir(entry.id)
72
    if base_dir is None:
73
        base_dir = ""
74
    other_name = inventory.other.get_name(entry.id)
75
    other_parent = inventory.other.get_parent(entry.id)
76
    other_dir = inventory.base.get_dir(entry.id)
77
    if other_dir is None:
78
        other_dir = ""
913 by Martin Pool
- merge aaron's merge-rename fix
79
    mutter("Dirs: this, base, other %r %r %r" % (this_dir, base_dir, other_dir))
80
    mutter("Names: this, base, other %r %r %r" % (this_name, base_name, other_name))
493 by Martin Pool
- Merge aaron's merge command
81
    if base_name == other_name:
82
        old_name = this_name
83
        new_name = this_name
84
    else:
85
        if this_name != base_name and this_name != other_name:
86
            conflict_handler.rename_conflict(entry.id, this_name, base_name,
87
                                             other_name)
88
        else:
89
            old_name = this_name
90
            new_name = other_name
91
92
    if base_parent == other_parent:
93
        old_parent = this_parent
94
        new_parent = this_parent
95
        old_dir = this_dir
96
        new_dir = this_dir
97
    else:
98
        if this_parent != base_parent and this_parent != other_parent:
99
            conflict_handler.move_conflict(entry.id, inventory)
100
        else:
101
            old_parent = this_parent
102
            old_dir = this_dir
103
            new_parent = other_parent
104
            new_dir = other_dir
850 by Martin Pool
- Merge merge updates from aaron
105
    if old_name is not None and old_parent is not None:
106
        old_path = os.path.join(old_dir, old_name)
107
    else:
108
        old_path = None
913 by Martin Pool
- merge aaron's merge-rename fix
109
    new_entry = changeset.ChangesetEntry(entry.id, old_parent, old_path)
850 by Martin Pool
- Merge merge updates from aaron
110
    if new_name is not None and new_parent is not None:
493 by Martin Pool
- Merge aaron's merge command
111
        new_entry.new_path = os.path.join(new_dir, new_name)
112
    else:
113
        new_entry.new_path = None
114
    new_entry.new_parent = new_parent
913 by Martin Pool
- merge aaron's merge-rename fix
115
    mutter(repr(new_entry))
850 by Martin Pool
- Merge merge updates from aaron
116
    return new_entry
117
118
119
def make_merged_contents(entry, this, base, other, conflict_handler):
120
    contents = entry.contents_change
121
    if contents is None:
122
        return None
123
    this_path = this.readonly_path(entry.id)
124
    def make_diff3():
125
        if this_path is None:
126
            return conflict_handler.missing_for_merge(entry.id, inventory)
127
        base_path = base.readonly_path(entry.id)
128
        other_path = other.readonly_path(entry.id)    
129
        return changeset.Diff3Merge(base_path, other_path)
130
131
    if isinstance(contents, changeset.PatchApply):
132
        return make_diff3()
133
    if isinstance(contents, changeset.ReplaceContents):
134
        if contents.old_contents is None and contents.new_contents is None:
135
            return None
136
        if contents.new_contents is None:
137
            if this_path is not None and os.path.exists(this_path):
138
                return contents
139
            else:
140
                return None
141
        elif contents.old_contents is None:
142
            if this_path is None or not os.path.exists(this_path):
143
                return contents
144
            else:
145
                this_contents = file(this_path, "rb").read()
146
                if this_contents == contents.new_contents:
147
                    return None
148
                else:
149
                    other_path = other.readonly_path(entry.id)    
150
                    conflict_handler.new_contents_conflict(this_path, 
151
                                                           other_path)
152
        elif isinstance(contents.old_contents, changeset.FileCreate) and \
153
            isinstance(contents.new_contents, changeset.FileCreate):
154
            return make_diff3()
155
        else:
156
            raise Exception("Unhandled merge scenario")
157
158
def make_merged_metadata(entry, base, other):
159
    if entry.metadata_change is not None:
160
        base_path = base.readonly_path(entry.id)
161
        other_path = other.readonly_path(entry.id)    
162
        return PermissionsMerge(base_path, other_path)
493 by Martin Pool
- Merge aaron's merge command
163
    
850 by Martin Pool
- Merge merge updates from aaron
164
def get_merge_entry(entry, inventory, base, other, conflict_handler):
493 by Martin Pool
- Merge aaron's merge command
165
    if entry.contents_change is not None:
166
        new_entry.contents_change = changeset.Diff3Merge(base_path, other_path)
167
    if entry.metadata_change is not None:
168
        new_entry.metadata_change = PermissionsMerge(base_path, other_path)
169
170
    return new_entry
171
558 by Martin Pool
- All top-level classes inherit from object
172
class PermissionsMerge(object):
493 by Martin Pool
- Merge aaron's merge command
173
    def __init__(self, base_path, other_path):
174
        self.base_path = base_path
175
        self.other_path = other_path
176
177
    def apply(self, filename, conflict_handler, reverse=False):
178
        if not reverse:
179
            base = self.base_path
180
            other = self.other_path
181
        else:
182
            base = self.other_path
183
            other = self.base_path
184
        base_stat = os.stat(base).st_mode
185
        other_stat = os.stat(other).st_mode
186
        this_stat = os.stat(filename).st_mode
187
        if base_stat &0777 == other_stat &0777:
188
            return
189
        elif this_stat &0777 == other_stat &0777:
190
            return
191
        elif this_stat &0777 == base_stat &0777:
192
            os.chmod(filename, other_stat)
193
        else:
194
            conflict_handler.permission_conflict(filename, base, other)
195
196
197
import unittest
198
import tempfile
199
import shutil
558 by Martin Pool
- All top-level classes inherit from object
200
class MergeTree(object):
493 by Martin Pool
- Merge aaron's merge command
201
    def __init__(self, dir):
202
        self.dir = dir;
203
        os.mkdir(dir)
204
        self.inventory = {'0': ""}
205
    
206
    def child_path(self, parent, name):
207
        return os.path.join(self.inventory[parent], name)
208
209
    def add_file(self, id, parent, name, contents, mode):
210
        path = self.child_path(parent, name)
211
        full_path = self.abs_path(path)
212
        assert not os.path.exists(full_path)
213
        file(full_path, "wb").write(contents)
214
        os.chmod(self.abs_path(path), mode)
215
        self.inventory[id] = path
216
217
    def add_dir(self, id, parent, name, mode):
218
        path = self.child_path(parent, name)
219
        full_path = self.abs_path(path)
220
        assert not os.path.exists(full_path)
221
        os.mkdir(self.abs_path(path))
222
        os.chmod(self.abs_path(path), mode)
223
        self.inventory[id] = path
224
225
    def abs_path(self, path):
226
        return os.path.join(self.dir, path)
227
228
    def full_path(self, id):
229
        return self.abs_path(self.inventory[id])
230
850 by Martin Pool
- Merge merge updates from aaron
231
    def readonly_path(self, id):
232
        return self.full_path(id)
233
493 by Martin Pool
- Merge aaron's merge command
234
    def change_path(self, id, path):
235
        new = os.path.join(self.dir, self.inventory[id])
236
        os.rename(self.abs_path(self.inventory[id]), self.abs_path(path))
237
        self.inventory[id] = path
238
558 by Martin Pool
- All top-level classes inherit from object
239
class MergeBuilder(object):
493 by Martin Pool
- Merge aaron's merge command
240
    def __init__(self):
241
        self.dir = tempfile.mkdtemp(prefix="BaZing")
242
        self.base = MergeTree(os.path.join(self.dir, "base"))
243
        self.this = MergeTree(os.path.join(self.dir, "this"))
244
        self.other = MergeTree(os.path.join(self.dir, "other"))
245
        
246
        self.cset = changeset.Changeset()
247
        self.cset.add_entry(changeset.ChangesetEntry("0", 
248
                                                     changeset.NULL_ID, "./."))
249
    def get_cset_path(self, parent, name):
250
        if name is None:
251
            assert (parent is None)
252
            return None
253
        return os.path.join(self.cset.entries[parent].path, name)
254
255
    def add_file(self, id, parent, name, contents, mode):
256
        self.base.add_file(id, parent, name, contents, mode)
257
        self.this.add_file(id, parent, name, contents, mode)
258
        self.other.add_file(id, parent, name, contents, mode)
259
        path = self.get_cset_path(parent, name)
260
        self.cset.add_entry(changeset.ChangesetEntry(id, parent, path))
261
262
    def add_dir(self, id, parent, name, mode):
263
        path = self.get_cset_path(parent, name)
264
        self.base.add_dir(id, parent, name, mode)
265
        self.cset.add_entry(changeset.ChangesetEntry(id, parent, path))
266
        self.this.add_dir(id, parent, name, mode)
267
        self.other.add_dir(id, parent, name, mode)
268
269
270
    def change_name(self, id, base=None, this=None, other=None):
271
        if base is not None:
272
            self.change_name_tree(id, self.base, base)
273
            self.cset.entries[id].name = base
274
275
        if this is not None:
276
            self.change_name_tree(id, self.this, this)
277
278
        if other is not None:
279
            self.change_name_tree(id, self.other, other)
280
            self.cset.entries[id].new_name = other
281
282
    def change_parent(self, id, base=None, this=None, other=None):
283
        if base is not None:
284
            self.change_parent_tree(id, self.base, base)
285
            self.cset.entries[id].parent = base
286
            self.cset.entries[id].dir = self.cset.entries[base].path
287
288
        if this is not None:
289
            self.change_parent_tree(id, self.this, this)
290
291
        if other is not None:
292
            self.change_parent_tree(id, self.other, other)
293
            self.cset.entries[id].new_parent = other
294
            self.cset.entries[id].new_dir = \
295
                self.cset.entries[other].new_path
296
297
    def change_contents(self, id, base=None, this=None, other=None):
298
        if base is not None:
299
            self.change_contents_tree(id, self.base, base)
300
301
        if this is not None:
302
            self.change_contents_tree(id, self.this, this)
303
304
        if other is not None:
305
            self.change_contents_tree(id, self.other, other)
306
307
        if base is not None or other is not None:
308
            old_contents = file(self.base.full_path(id)).read()
309
            new_contents = file(self.other.full_path(id)).read()
310
            contents = changeset.ReplaceFileContents(old_contents, 
311
                                                     new_contents)
312
            self.cset.entries[id].contents_change = contents
313
314
    def change_perms(self, id, base=None, this=None, other=None):
315
        if base is not None:
316
            self.change_perms_tree(id, self.base, base)
317
318
        if this is not None:
319
            self.change_perms_tree(id, self.this, this)
320
321
        if other is not None:
322
            self.change_perms_tree(id, self.other, other)
323
324
        if base is not None or other is not None:
325
            old_perms = os.stat(self.base.full_path(id)).st_mode &077
326
            new_perms = os.stat(self.other.full_path(id)).st_mode &077
327
            contents = changeset.ChangeUnixPermissions(old_perms, 
328
                                                       new_perms)
329
            self.cset.entries[id].metadata_change = contents
330
331
    def change_name_tree(self, id, tree, name):
332
        new_path = tree.child_path(self.cset.entries[id].parent, name)
333
        tree.change_path(id, new_path)
334
335
    def change_parent_tree(self, id, tree, parent):
336
        new_path = tree.child_path(parent, self.cset.entries[id].name)
337
        tree.change_path(id, new_path)
338
339
    def change_contents_tree(self, id, tree, contents):
340
        path = tree.full_path(id)
341
        mode = os.stat(path).st_mode
342
        file(path, "w").write(contents)
343
        os.chmod(path, mode)
344
345
    def change_perms_tree(self, id, tree, mode):
346
        os.chmod(tree.full_path(id), mode)
347
348
    def merge_changeset(self):
349
        all_inventory = ThreewayInventory(Inventory(self.this.inventory),
350
                                          Inventory(self.base.inventory), 
351
                                          Inventory(self.other.inventory))
352
        conflict_handler = changeset.ExceptionConflictHandler(self.this.dir)
850 by Martin Pool
- Merge merge updates from aaron
353
        return make_merge_changeset(self.cset, all_inventory, self.this,
354
                                    self.base, self.other, conflict_handler)
355
356
    def apply_inv_change(self, inventory_change, orig_inventory):
357
        orig_inventory_by_path = {}
358
        for file_id, path in orig_inventory.iteritems():
359
            orig_inventory_by_path[path] = file_id
360
361
        def parent_id(file_id):
362
            try:
363
                parent_dir = os.path.dirname(orig_inventory[file_id])
364
            except:
365
                print file_id
366
                raise
367
            if parent_dir == "":
368
                return None
369
            return orig_inventory_by_path[parent_dir]
370
        
371
        def new_path(file_id):
372
            if inventory_change.has_key(file_id):
373
                return inventory_change[file_id]
374
            else:
375
                parent = parent_id(file_id)
376
                if parent is None:
377
                    return orig_inventory[file_id]
378
                dirname = new_path(parent)
379
                return os.path.join(dirname, orig_inventory[file_id])
380
381
        new_inventory = {}
382
        for file_id in orig_inventory.iterkeys():
383
            path = new_path(file_id)
384
            if path is None:
385
                continue
386
            new_inventory[file_id] = path
387
388
        for file_id, path in inventory_change.iteritems():
389
            if orig_inventory.has_key(file_id):
390
                continue
391
            new_inventory[file_id] = path
392
        return new_inventory
393
394
        
395
493 by Martin Pool
- Merge aaron's merge command
396
    def apply_changeset(self, cset, conflict_handler=None, reverse=False):
850 by Martin Pool
- Merge merge updates from aaron
397
        inventory_change = changeset.apply_changeset(cset,
398
                                                     self.this.inventory,
399
                                                     self.this.dir,
400
                                                     conflict_handler, reverse)
401
        self.this.inventory =  self.apply_inv_change(inventory_change, 
402
                                                     self.this.inventory)
403
404
                    
405
        
406
493 by Martin Pool
- Merge aaron's merge command
407
        
408
    def cleanup(self):
409
        shutil.rmtree(self.dir)
410
411
412
class MergeTest(unittest.TestCase):
413
    def test_change_name(self):
414
        """Test renames"""
415
        builder = MergeBuilder()
416
        builder.add_file("1", "0", "name1", "hello1", 0755)
417
        builder.change_name("1", other="name2")
418
        builder.add_file("2", "0", "name3", "hello2", 0755)
419
        builder.change_name("2", base="name4")
420
        builder.add_file("3", "0", "name5", "hello3", 0755)
421
        builder.change_name("3", this="name6")
422
        cset = builder.merge_changeset()
423
        assert(cset.entries["2"].is_boring())
424
        assert(cset.entries["1"].name == "name1")
425
        assert(cset.entries["1"].new_name == "name2")
426
        assert(cset.entries["3"].is_boring())
427
        for tree in (builder.this, builder.other, builder.base):
428
            assert(tree.dir != builder.dir and 
429
                   tree.dir.startswith(builder.dir))
430
            for path in tree.inventory.itervalues():
431
                fullpath = tree.abs_path(path)
432
                assert(fullpath.startswith(tree.dir))
433
                assert(not path.startswith(tree.dir))
434
                assert os.path.exists(fullpath)
435
        builder.apply_changeset(cset)
436
        builder.cleanup()
437
        builder = MergeBuilder()
438
        builder.add_file("1", "0", "name1", "hello1", 0644)
439
        builder.change_name("1", other="name2", this="name3")
440
        self.assertRaises(changeset.RenameConflict, 
441
                          builder.merge_changeset)
442
        builder.cleanup()
443
        
444
    def test_file_moves(self):
445
        """Test moves"""
446
        builder = MergeBuilder()
447
        builder.add_dir("1", "0", "dir1", 0755)
448
        builder.add_dir("2", "0", "dir2", 0755)
449
        builder.add_file("3", "1", "file1", "hello1", 0644)
450
        builder.add_file("4", "1", "file2", "hello2", 0644)
451
        builder.add_file("5", "1", "file3", "hello3", 0644)
452
        builder.change_parent("3", other="2")
453
        assert(Inventory(builder.other.inventory).get_parent("3") == "2")
454
        builder.change_parent("4", this="2")
455
        assert(Inventory(builder.this.inventory).get_parent("4") == "2")
456
        builder.change_parent("5", base="2")
457
        assert(Inventory(builder.base.inventory).get_parent("5") == "2")
458
        cset = builder.merge_changeset()
459
        for id in ("1", "2", "4", "5"):
460
            assert(cset.entries[id].is_boring())
461
        assert(cset.entries["3"].parent == "1")
462
        assert(cset.entries["3"].new_parent == "2")
463
        builder.apply_changeset(cset)
464
        builder.cleanup()
465
466
        builder = MergeBuilder()
467
        builder.add_dir("1", "0", "dir1", 0755)
468
        builder.add_dir("2", "0", "dir2", 0755)
469
        builder.add_dir("3", "0", "dir3", 0755)
470
        builder.add_file("4", "1", "file1", "hello1", 0644)
471
        builder.change_parent("4", other="2", this="3")
472
        self.assertRaises(changeset.MoveConflict, 
473
                          builder.merge_changeset)
474
        builder.cleanup()
475
476
    def test_contents_merge(self):
477
        """Test diff3 merging"""
478
        builder = MergeBuilder()
479
        builder.add_file("1", "0", "name1", "text1", 0755)
480
        builder.change_contents("1", other="text4")
481
        builder.add_file("2", "0", "name3", "text2", 0655)
482
        builder.change_contents("2", base="text5")
483
        builder.add_file("3", "0", "name5", "text3", 0744)
484
        builder.change_contents("3", this="text6")
485
        cset = builder.merge_changeset()
486
        assert(cset.entries["1"].contents_change is not None)
487
        assert(isinstance(cset.entries["1"].contents_change,
488
                          changeset.Diff3Merge))
489
        assert(isinstance(cset.entries["2"].contents_change,
490
                          changeset.Diff3Merge))
491
        assert(cset.entries["3"].is_boring())
492
        builder.apply_changeset(cset)
493
        assert(file(builder.this.full_path("1"), "rb").read() == "text4" )
494
        assert(file(builder.this.full_path("2"), "rb").read() == "text2" )
495
        assert(os.stat(builder.this.full_path("1")).st_mode &0777 == 0755)
496
        assert(os.stat(builder.this.full_path("2")).st_mode &0777 == 0655)
497
        assert(os.stat(builder.this.full_path("3")).st_mode &0777 == 0744)
498
        builder.cleanup()
499
500
        builder = MergeBuilder()
501
        builder.add_file("1", "0", "name1", "text1", 0755)
502
        builder.change_contents("1", other="text4", this="text3")
503
        cset = builder.merge_changeset()
504
        self.assertRaises(changeset.MergeConflict, builder.apply_changeset,
505
                          cset)
506
        builder.cleanup()
507
508
    def test_perms_merge(self):
509
        builder = MergeBuilder()
510
        builder.add_file("1", "0", "name1", "text1", 0755)
511
        builder.change_perms("1", other=0655)
512
        builder.add_file("2", "0", "name2", "text2", 0755)
513
        builder.change_perms("2", base=0655)
514
        builder.add_file("3", "0", "name3", "text3", 0755)
515
        builder.change_perms("3", this=0655)
516
        cset = builder.merge_changeset()
517
        assert(cset.entries["1"].metadata_change is not None)
518
        assert(isinstance(cset.entries["1"].metadata_change,
519
                          PermissionsMerge))
520
        assert(isinstance(cset.entries["2"].metadata_change,
521
                          PermissionsMerge))
522
        assert(cset.entries["3"].is_boring())
523
        builder.apply_changeset(cset)
524
        assert(os.stat(builder.this.full_path("1")).st_mode &0777 == 0655)
525
        assert(os.stat(builder.this.full_path("2")).st_mode &0777 == 0755)
526
        assert(os.stat(builder.this.full_path("3")).st_mode &0777 == 0655)
527
        builder.cleanup();
528
        builder = MergeBuilder()
529
        builder.add_file("1", "0", "name1", "text1", 0755)
530
        builder.change_perms("1", other=0655, base=0555)
531
        cset = builder.merge_changeset()
532
        self.assertRaises(changeset.MergePermissionConflict, 
533
                     builder.apply_changeset, cset)
534
        builder.cleanup()
535
536
def test():        
537
    changeset_suite = unittest.makeSuite(MergeTest, 'test_')
538
    runner = unittest.TextTestRunner()
539
    runner.run(changeset_suite)
540
        
541
if __name__ == "__main__":
542
    test()