~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

  • Committer: Martin Pool
  • Date: 2006-05-17 07:09:13 UTC
  • mfrom: (1668.1.15 bzr-0.8.mbp)
  • mto: This revision was merged to the branch mainline in revision 1710.
  • Revision ID: mbp@sourcefrog.net-20060517070913-723e387ac91d55c0
merge 0.8 fix branch, including register-branch plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from cStringIO import StringIO
 
18
import os
 
19
 
 
20
from bzrlib.branch import Branch
 
21
import bzrlib.errors as errors
 
22
from bzrlib.diff import internal_diff
 
23
from bzrlib.inventory import (Inventory, ROOT_ID, InventoryFile,
 
24
    InventoryDirectory, InventoryEntry)
 
25
import bzrlib.inventory as inventory
 
26
from bzrlib.osutils import has_symlinks, rename, pathjoin
 
27
from bzrlib.tests import TestCase, TestCaseWithTransport
 
28
from bzrlib.transform import TreeTransform
 
29
from bzrlib.uncommit import uncommit
 
30
 
 
31
 
 
32
class TestInventory(TestCase):
 
33
 
 
34
    def test_is_within(self):
 
35
        from bzrlib.osutils import is_inside_any
 
36
 
 
37
        SRC_FOO_C = pathjoin('src', 'foo.c')
 
38
        for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
 
39
                         (['src'], SRC_FOO_C),
 
40
                         (['src'], 'src'),
 
41
                         ]:
 
42
            self.assert_(is_inside_any(dirs, fn))
 
43
            
 
44
        for dirs, fn in [(['src'], 'srccontrol'),
 
45
                         (['src'], 'srccontrol/foo')]:
 
46
            self.assertFalse(is_inside_any(dirs, fn))
 
47
            
 
48
    def test_ids(self):
 
49
        """Test detection of files within selected directories."""
 
50
        inv = Inventory()
 
51
        
 
52
        for args in [('src', 'directory', 'src-id'), 
 
53
                     ('doc', 'directory', 'doc-id'), 
 
54
                     ('src/hello.c', 'file'),
 
55
                     ('src/bye.c', 'file', 'bye-id'),
 
56
                     ('Makefile', 'file')]:
 
57
            inv.add_path(*args)
 
58
            
 
59
        self.assertEqual(inv.path2id('src'), 'src-id')
 
60
        self.assertEqual(inv.path2id('src/bye.c'), 'bye-id')
 
61
        
 
62
        self.assert_('src-id' in inv)
 
63
 
 
64
 
 
65
    def test_version(self):
 
66
        """Inventory remembers the text's version."""
 
67
        inv = Inventory()
 
68
        ie = inv.add_path('foo.txt', 'file')
 
69
        ## XXX
 
70
 
 
71
 
 
72
class TestInventoryEntry(TestCase):
 
73
 
 
74
    def test_file_kind_character(self):
 
75
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
76
        self.assertEqual(file.kind_character(), '')
 
77
 
 
78
    def test_dir_kind_character(self):
 
79
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
80
        self.assertEqual(dir.kind_character(), '/')
 
81
 
 
82
    def test_link_kind_character(self):
 
83
        dir = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
84
        self.assertEqual(dir.kind_character(), '')
 
85
 
 
86
    def test_dir_detect_changes(self):
 
87
        left = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
88
        left.text_sha1 = 123
 
89
        left.executable = True
 
90
        left.symlink_target='foo'
 
91
        right = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
92
        right.text_sha1 = 321
 
93
        right.symlink_target='bar'
 
94
        self.assertEqual((False, False), left.detect_changes(right))
 
95
        self.assertEqual((False, False), right.detect_changes(left))
 
96
 
 
97
    def test_file_detect_changes(self):
 
98
        left = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
99
        left.text_sha1 = 123
 
100
        right = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
101
        right.text_sha1 = 123
 
102
        self.assertEqual((False, False), left.detect_changes(right))
 
103
        self.assertEqual((False, False), right.detect_changes(left))
 
104
        left.executable = True
 
105
        self.assertEqual((False, True), left.detect_changes(right))
 
106
        self.assertEqual((False, True), right.detect_changes(left))
 
107
        right.text_sha1 = 321
 
108
        self.assertEqual((True, True), left.detect_changes(right))
 
109
        self.assertEqual((True, True), right.detect_changes(left))
 
110
 
 
111
    def test_symlink_detect_changes(self):
 
112
        left = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
113
        left.text_sha1 = 123
 
114
        left.executable = True
 
115
        left.symlink_target='foo'
 
116
        right = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
117
        right.text_sha1 = 321
 
118
        right.symlink_target='foo'
 
119
        self.assertEqual((False, False), left.detect_changes(right))
 
120
        self.assertEqual((False, False), right.detect_changes(left))
 
121
        left.symlink_target = 'different'
 
122
        self.assertEqual((True, False), left.detect_changes(right))
 
123
        self.assertEqual((True, False), right.detect_changes(left))
 
124
 
 
125
    def test_file_has_text(self):
 
126
        file = inventory.InventoryFile('123', 'hello.c', ROOT_ID)
 
127
        self.failUnless(file.has_text())
 
128
 
 
129
    def test_directory_has_text(self):
 
130
        dir = inventory.InventoryDirectory('123', 'hello.c', ROOT_ID)
 
131
        self.failIf(dir.has_text())
 
132
 
 
133
    def test_link_has_text(self):
 
134
        link = inventory.InventoryLink('123', 'hello.c', ROOT_ID)
 
135
        self.failIf(link.has_text())
 
136
 
 
137
 
 
138
class TestEntryDiffing(TestCaseWithTransport):
 
139
 
 
140
    def setUp(self):
 
141
        super(TestEntryDiffing, self).setUp()
 
142
        self.wt = self.make_branch_and_tree('.')
 
143
        self.branch = self.wt.branch
 
144
        print >> open('file', 'wb'), 'foo'
 
145
        print >> open('binfile', 'wb'), 'foo'
 
146
        self.wt.add(['file'], ['fileid'])
 
147
        self.wt.add(['binfile'], ['binfileid'])
 
148
        if has_symlinks():
 
149
            os.symlink('target1', 'symlink')
 
150
            self.wt.add(['symlink'], ['linkid'])
 
151
        self.wt.commit('message_1', rev_id = '1')
 
152
        print >> open('file', 'wb'), 'bar'
 
153
        print >> open('binfile', 'wb'), 'x' * 1023 + '\x00'
 
154
        if has_symlinks():
 
155
            os.unlink('symlink')
 
156
            os.symlink('target2', 'symlink')
 
157
        self.tree_1 = self.branch.repository.revision_tree('1')
 
158
        self.inv_1 = self.branch.repository.get_inventory('1')
 
159
        self.file_1 = self.inv_1['fileid']
 
160
        self.file_1b = self.inv_1['binfileid']
 
161
        self.tree_2 = self.wt
 
162
        self.inv_2 = self.tree_2.read_working_inventory()
 
163
        self.file_2 = self.inv_2['fileid']
 
164
        self.file_2b = self.inv_2['binfileid']
 
165
        if has_symlinks():
 
166
            self.link_1 = self.inv_1['linkid']
 
167
            self.link_2 = self.inv_2['linkid']
 
168
 
 
169
    def test_file_diff_deleted(self):
 
170
        output = StringIO()
 
171
        self.file_1.diff(internal_diff, 
 
172
                          "old_label", self.tree_1,
 
173
                          "/dev/null", None, None,
 
174
                          output)
 
175
        self.assertEqual(output.getvalue(), "--- old_label\t\n"
 
176
                                            "+++ /dev/null\t\n"
 
177
                                            "@@ -1,1 +0,0 @@\n"
 
178
                                            "-foo\n"
 
179
                                            "\n")
 
180
 
 
181
    def test_file_diff_added(self):
 
182
        output = StringIO()
 
183
        self.file_1.diff(internal_diff, 
 
184
                          "new_label", self.tree_1,
 
185
                          "/dev/null", None, None,
 
186
                          output, reverse=True)
 
187
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
188
                                            "+++ new_label\t\n"
 
189
                                            "@@ -0,0 +1,1 @@\n"
 
190
                                            "+foo\n"
 
191
                                            "\n")
 
192
 
 
193
    def test_file_diff_changed(self):
 
194
        output = StringIO()
 
195
        self.file_1.diff(internal_diff, 
 
196
                          "/dev/null", self.tree_1, 
 
197
                          "new_label", self.file_2, self.tree_2,
 
198
                          output)
 
199
        self.assertEqual(output.getvalue(), "--- /dev/null\t\n"
 
200
                                            "+++ new_label\t\n"
 
201
                                            "@@ -1,1 +1,1 @@\n"
 
202
                                            "-foo\n"
 
203
                                            "+bar\n"
 
204
                                            "\n")
 
205
        
 
206
    def test_file_diff_binary(self):
 
207
        output = StringIO()
 
208
        self.file_1.diff(internal_diff, 
 
209
                          "/dev/null", self.tree_1, 
 
210
                          "new_label", self.file_2b, self.tree_2,
 
211
                          output)
 
212
        self.assertEqual(output.getvalue(), 
 
213
                         "Binary files /dev/null and new_label differ\n")
 
214
    def test_link_diff_deleted(self):
 
215
        if not has_symlinks():
 
216
            return
 
217
        output = StringIO()
 
218
        self.link_1.diff(internal_diff, 
 
219
                          "old_label", self.tree_1,
 
220
                          "/dev/null", None, None,
 
221
                          output)
 
222
        self.assertEqual(output.getvalue(),
 
223
                         "=== target was 'target1'\n")
 
224
 
 
225
    def test_link_diff_added(self):
 
226
        if not has_symlinks():
 
227
            return
 
228
        output = StringIO()
 
229
        self.link_1.diff(internal_diff, 
 
230
                          "new_label", self.tree_1,
 
231
                          "/dev/null", None, None,
 
232
                          output, reverse=True)
 
233
        self.assertEqual(output.getvalue(),
 
234
                         "=== target is 'target1'\n")
 
235
 
 
236
    def test_link_diff_changed(self):
 
237
        if not has_symlinks():
 
238
            return
 
239
        output = StringIO()
 
240
        self.link_1.diff(internal_diff, 
 
241
                          "/dev/null", self.tree_1, 
 
242
                          "new_label", self.link_2, self.tree_2,
 
243
                          output)
 
244
        self.assertEqual(output.getvalue(),
 
245
                         "=== target changed 'target1' => 'target2'\n")
 
246
 
 
247
 
 
248
class TestSnapshot(TestCaseWithTransport):
 
249
 
 
250
    def setUp(self):
 
251
        # for full testing we'll need a branch
 
252
        # with a subdir to test parent changes.
 
253
        # and a file, link and dir under that.
 
254
        # but right now I only need one attribute
 
255
        # to change, and then test merge patterns
 
256
        # with fake parent entries.
 
257
        super(TestSnapshot, self).setUp()
 
258
        self.wt = self.make_branch_and_tree('.')
 
259
        self.branch = self.wt.branch
 
260
        self.build_tree(['subdir/', 'subdir/file'], line_endings='binary')
 
261
        self.wt.add(['subdir', 'subdir/file'],
 
262
                                       ['dirid', 'fileid'])
 
263
        if has_symlinks():
 
264
            pass
 
265
        self.wt.commit('message_1', rev_id = '1')
 
266
        self.tree_1 = self.branch.repository.revision_tree('1')
 
267
        self.inv_1 = self.branch.repository.get_inventory('1')
 
268
        self.file_1 = self.inv_1['fileid']
 
269
        self.file_active = self.wt.inventory['fileid']
 
270
 
 
271
    def test_snapshot_new_revision(self):
 
272
        # This tests that a simple commit with no parents makes a new
 
273
        # revision value in the inventory entry
 
274
        self.file_active.snapshot('2', 'subdir/file', {}, self.wt, 
 
275
                                  self.branch.repository.weave_store,
 
276
                                  self.branch.get_transaction())
 
277
        # expected outcome - file_1 has a revision id of '2', and we can get
 
278
        # its text of 'file contents' out of the weave.
 
279
        self.assertEqual(self.file_1.revision, '1')
 
280
        self.assertEqual(self.file_active.revision, '2')
 
281
        # this should be a separate test probably, but lets check it once..
 
282
        lines = self.branch.repository.weave_store.get_weave(
 
283
            'fileid', 
 
284
            self.branch.get_transaction()).get_lines('2')
 
285
        self.assertEqual(lines, ['contents of subdir/file\n'])
 
286
 
 
287
    def test_snapshot_unchanged(self):
 
288
        #This tests that a simple commit does not make a new entry for
 
289
        # an unchanged inventory entry
 
290
        self.file_active.snapshot('2', 'subdir/file', {'1':self.file_1},
 
291
                                  self.wt, 
 
292
                                  self.branch.repository.weave_store,
 
293
                                  self.branch.get_transaction())
 
294
        self.assertEqual(self.file_1.revision, '1')
 
295
        self.assertEqual(self.file_active.revision, '1')
 
296
        vf = self.branch.repository.weave_store.get_weave(
 
297
            'fileid', 
 
298
            self.branch.repository.get_transaction())
 
299
        self.assertRaises(errors.RevisionNotPresent,
 
300
                          vf.get_lines,
 
301
                          '2')
 
302
 
 
303
    def test_snapshot_merge_identical_different_revid(self):
 
304
        # This tests that a commit with two identical parents, one of which has
 
305
        # a different revision id, results in a new revision id in the entry.
 
306
        # 1->other, commit a merge of other against 1, results in 2.
 
307
        other_ie = inventory.InventoryFile('fileid', 'newname', self.file_1.parent_id)
 
308
        other_ie = inventory.InventoryFile('fileid', 'file', self.file_1.parent_id)
 
309
        other_ie.revision = '1'
 
310
        other_ie.text_sha1 = self.file_1.text_sha1
 
311
        other_ie.text_size = self.file_1.text_size
 
312
        self.assertEqual(self.file_1, other_ie)
 
313
        other_ie.revision = 'other'
 
314
        self.assertNotEqual(self.file_1, other_ie)
 
315
        versionfile = self.branch.repository.weave_store.get_weave(
 
316
            'fileid', self.branch.repository.get_transaction())
 
317
        versionfile.clone_text('other', '1', ['1'])
 
318
        self.file_active.snapshot('2', 'subdir/file', 
 
319
                                  {'1':self.file_1, 'other':other_ie},
 
320
                                  self.wt, 
 
321
                                  self.branch.repository.weave_store,
 
322
                                  self.branch.get_transaction())
 
323
        self.assertEqual(self.file_active.revision, '2')
 
324
 
 
325
    def test_snapshot_changed(self):
 
326
        # This tests that a commit with one different parent results in a new
 
327
        # revision id in the entry.
 
328
        self.file_active.name='newname'
 
329
        rename('subdir/file', 'subdir/newname')
 
330
        self.file_active.snapshot('2', 'subdir/newname', {'1':self.file_1}, 
 
331
                                  self.wt,
 
332
                                  self.branch.repository.weave_store,
 
333
                                  self.branch.get_transaction())
 
334
        # expected outcome - file_1 has a revision id of '2'
 
335
        self.assertEqual(self.file_active.revision, '2')
 
336
 
 
337
 
 
338
class TestPreviousHeads(TestCaseWithTransport):
 
339
 
 
340
    def setUp(self):
 
341
        # we want several inventories, that respectively
 
342
        # give use the following scenarios:
 
343
        # A) fileid not in any inventory (A),
 
344
        # B) fileid present in one inventory (B) and (A,B)
 
345
        # C) fileid present in two inventories, and they
 
346
        #   are not mutual descendents (B, C)
 
347
        # D) fileid present in two inventories and one is
 
348
        #   a descendent of the other. (B, D)
 
349
        super(TestPreviousHeads, self).setUp()
 
350
        self.wt = self.make_branch_and_tree('.')
 
351
        self.branch = self.wt.branch
 
352
        self.build_tree(['file'])
 
353
        self.wt.commit('new branch', allow_pointless=True, rev_id='A')
 
354
        self.inv_A = self.branch.repository.get_inventory('A')
 
355
        self.wt.add(['file'], ['fileid'])
 
356
        self.wt.commit('add file', rev_id='B')
 
357
        self.inv_B = self.branch.repository.get_inventory('B')
 
358
        uncommit(self.branch, tree=self.wt)
 
359
        self.assertEqual(self.branch.revision_history(), ['A'])
 
360
        self.wt.commit('another add of file', rev_id='C')
 
361
        self.inv_C = self.branch.repository.get_inventory('C')
 
362
        self.wt.add_pending_merge('B')
 
363
        self.wt.commit('merge in B', rev_id='D')
 
364
        self.inv_D = self.branch.repository.get_inventory('D')
 
365
        self.file_active = self.wt.inventory['fileid']
 
366
        self.weave = self.branch.repository.weave_store.get_weave('fileid',
 
367
            self.branch.repository.get_transaction())
 
368
        
 
369
    def get_previous_heads(self, inventories):
 
370
        return self.file_active.find_previous_heads(
 
371
            inventories, 
 
372
            self.branch.repository.weave_store,
 
373
            self.branch.repository.get_transaction())
 
374
        
 
375
    def test_fileid_in_no_inventory(self):
 
376
        self.assertEqual({}, self.get_previous_heads([self.inv_A]))
 
377
 
 
378
    def test_fileid_in_one_inventory(self):
 
379
        self.assertEqual({'B':self.inv_B['fileid']},
 
380
                         self.get_previous_heads([self.inv_B]))
 
381
        self.assertEqual({'B':self.inv_B['fileid']},
 
382
                         self.get_previous_heads([self.inv_A, self.inv_B]))
 
383
        self.assertEqual({'B':self.inv_B['fileid']},
 
384
                         self.get_previous_heads([self.inv_B, self.inv_A]))
 
385
 
 
386
    def test_fileid_in_two_inventories_gives_both_entries(self):
 
387
        self.assertEqual({'B':self.inv_B['fileid'],
 
388
                          'C':self.inv_C['fileid']},
 
389
                          self.get_previous_heads([self.inv_B, self.inv_C]))
 
390
        self.assertEqual({'B':self.inv_B['fileid'],
 
391
                          'C':self.inv_C['fileid']},
 
392
                          self.get_previous_heads([self.inv_C, self.inv_B]))
 
393
 
 
394
    def test_fileid_in_two_inventories_already_merged_gives_head(self):
 
395
        self.assertEqual({'D':self.inv_D['fileid']},
 
396
                         self.get_previous_heads([self.inv_B, self.inv_D]))
 
397
        self.assertEqual({'D':self.inv_D['fileid']},
 
398
                         self.get_previous_heads([self.inv_D, self.inv_B]))
 
399
 
 
400
    # TODO: test two inventories with the same file revision 
 
401
 
 
402
 
 
403
class TestDescribeChanges(TestCase):
 
404
 
 
405
    def test_describe_change(self):
 
406
        # we need to test the following change combinations:
 
407
        # rename
 
408
        # reparent
 
409
        # modify
 
410
        # gone
 
411
        # added
 
412
        # renamed/reparented and modified
 
413
        # change kind (perhaps can't be done yet?)
 
414
        # also, merged in combination with all of these?
 
415
        old_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
416
        old_a.text_sha1 = '123132'
 
417
        old_a.text_size = 0
 
418
        new_a = InventoryFile('a-id', 'a_file', ROOT_ID)
 
419
        new_a.text_sha1 = '123132'
 
420
        new_a.text_size = 0
 
421
 
 
422
        self.assertChangeDescription('unchanged', old_a, new_a)
 
423
 
 
424
        new_a.text_size = 10
 
425
        new_a.text_sha1 = 'abcabc'
 
426
        self.assertChangeDescription('modified', old_a, new_a)
 
427
 
 
428
        self.assertChangeDescription('added', None, new_a)
 
429
        self.assertChangeDescription('removed', old_a, None)
 
430
        # perhaps a bit questionable but seems like the most reasonable thing...
 
431
        self.assertChangeDescription('unchanged', None, None)
 
432
 
 
433
        # in this case it's both renamed and modified; show a rename and 
 
434
        # modification:
 
435
        new_a.name = 'newfilename'
 
436
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
437
 
 
438
        # reparenting is 'renaming'
 
439
        new_a.name = old_a.name
 
440
        new_a.parent_id = 'somedir-id'
 
441
        self.assertChangeDescription('modified and renamed', old_a, new_a)
 
442
 
 
443
        # reset the content values so its not modified
 
444
        new_a.text_size = old_a.text_size
 
445
        new_a.text_sha1 = old_a.text_sha1
 
446
        new_a.name = old_a.name
 
447
 
 
448
        new_a.name = 'newfilename'
 
449
        self.assertChangeDescription('renamed', old_a, new_a)
 
450
 
 
451
        # reparenting is 'renaming'
 
452
        new_a.name = old_a.name
 
453
        new_a.parent_id = 'somedir-id'
 
454
        self.assertChangeDescription('renamed', old_a, new_a)
 
455
 
 
456
    def assertChangeDescription(self, expected_change, old_ie, new_ie):
 
457
        change = InventoryEntry.describe_change(old_ie, new_ie)
 
458
        self.assertEqual(expected_change, change)
 
459
 
 
460
 
 
461
class TestExecutable(TestCaseWithTransport):
 
462
 
 
463
    def test_stays_executable(self):
 
464
        a_id = "a-20051208024829-849e76f7968d7a86"
 
465
        b_id = "b-20051208024829-849e76f7968d7a86"
 
466
        wt = self.make_branch_and_tree('b1')
 
467
        b = wt.branch
 
468
        tt = TreeTransform(wt)
 
469
        tt.new_file('a', tt.root, 'a test\n', a_id, True)
 
470
        tt.new_file('b', tt.root, 'b test\n', b_id, False)
 
471
        tt.apply()
 
472
 
 
473
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
474
 
 
475
        # reopen the tree and ensure it stuck.
 
476
        wt = wt.bzrdir.open_workingtree()
 
477
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
478
 
 
479
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
480
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
481
 
 
482
        wt.commit('adding a,b', rev_id='r1')
 
483
 
 
484
        rev_tree = b.repository.revision_tree('r1')
 
485
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
 
486
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
 
487
 
 
488
        self.failUnless(rev_tree.inventory[a_id].executable)
 
489
        self.failIf(rev_tree.inventory[b_id].executable)
 
490
 
 
491
        # Make sure the entries are gone
 
492
        os.remove('b1/a')
 
493
        os.remove('b1/b')
 
494
        self.failIf(wt.has_id(a_id))
 
495
        self.failIf(wt.has_filename('a'))
 
496
        self.failIf(wt.has_id(b_id))
 
497
        self.failIf(wt.has_filename('b'))
 
498
 
 
499
        # Make sure that revert is able to bring them back,
 
500
        # and sets 'a' back to being executable
 
501
 
 
502
        wt.revert(['a', 'b'], rev_tree, backups=False)
 
503
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
504
 
 
505
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
506
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
507
 
 
508
        # Now remove them again, and make sure that after a
 
509
        # commit, they are still marked correctly
 
510
        os.remove('b1/a')
 
511
        os.remove('b1/b')
 
512
        wt.commit('removed', rev_id='r2')
 
513
 
 
514
        self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
 
515
        self.failIf(wt.has_id(a_id))
 
516
        self.failIf(wt.has_filename('a'))
 
517
        self.failIf(wt.has_id(b_id))
 
518
        self.failIf(wt.has_filename('b'))
 
519
 
 
520
        # Now revert back to the previous commit
 
521
        wt.revert([], rev_tree, backups=False)
 
522
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
 
523
 
 
524
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
 
525
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
526
 
 
527
        # Now make sure that 'bzr branch' also preserves the
 
528
        # executable bit
 
529
        # TODO: Maybe this should be a blackbox test
 
530
        d2 = b.bzrdir.clone('b2', revision_id='r1')
 
531
        t2 = d2.open_workingtree()
 
532
        b2 = t2.branch
 
533
        self.assertEquals('r1', b2.last_revision())
 
534
 
 
535
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
536
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
537
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
538
 
 
539
        # Make sure pull will delete the files
 
540
        t2.pull(b)
 
541
        self.assertEquals('r2', b2.last_revision())
 
542
        self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
 
543
 
 
544
        # Now commit the changes on the first branch
 
545
        # so that the second branch can pull the changes
 
546
        # and make sure that the executable bit has been copied
 
547
        wt.commit('resurrected', rev_id='r3')
 
548
 
 
549
        t2.pull(b)
 
550
        self.assertEquals('r3', b2.last_revision())
 
551
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
 
552
 
 
553
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
 
554
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
555
 
 
556
 
 
557
class TestRevert(TestCaseWithTransport):
 
558
 
 
559
    def test_dangling_id(self):
 
560
        wt = self.make_branch_and_tree('b1')
 
561
        self.assertEqual(len(wt.inventory), 1)
 
562
        open('b1/a', 'wb').write('a test\n')
 
563
        wt.add('a')
 
564
        self.assertEqual(len(wt.inventory), 2)
 
565
        os.unlink('b1/a')
 
566
        wt.revert([])
 
567
        self.assertEqual(len(wt.inventory), 1)