~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_pull.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-20 12:19:29 UTC
  • mfrom: (6437.23.11 2.5)
  • mto: (6581.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6582.
  • Revision ID: jelmer@samba.org-20120220121929-7ni2psvjoatm1yp4
Merge bzr/2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2012 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
import sys
22
22
 
23
23
from bzrlib import (
 
24
    branch,
24
25
    debug,
 
26
    osutils,
25
27
    remote,
 
28
    tests,
 
29
    uncommit,
26
30
    urlutils,
 
31
    workingtree,
27
32
    )
28
33
 
29
 
from bzrlib.branch import Branch
30
34
from bzrlib.directory_service import directories
31
 
from bzrlib.osutils import pathjoin
32
35
from bzrlib.tests import (
33
36
    fixtures,
34
 
    TestCaseWithTransport,
 
37
    script,
35
38
    )
36
 
from bzrlib.uncommit import uncommit
37
 
from bzrlib.workingtree import WorkingTree
38
 
 
39
 
 
40
 
class TestPull(TestCaseWithTransport):
 
39
 
 
40
 
 
41
class TestPull(tests.TestCaseWithTransport):
41
42
 
42
43
    def example_branch(self, path='.'):
43
44
        tree = self.make_branch_and_tree(path)
44
45
        self.build_tree_contents([
45
 
            (pathjoin(path, 'hello'),   'foo'),
46
 
            (pathjoin(path, 'goodbye'), 'baz')])
 
46
            (osutils.pathjoin(path, 'hello'),   'foo'),
 
47
            (osutils.pathjoin(path, 'goodbye'), 'baz')])
47
48
        tree.add('hello')
48
49
        tree.commit(message='setup')
49
50
        tree.add('goodbye')
53
54
    def test_pull(self):
54
55
        """Pull changes from one branch to another."""
55
56
        a_tree = self.example_branch('a')
56
 
        os.chdir('a')
57
 
        self.run_bzr('pull', retcode=3)
58
 
        self.run_bzr('missing', retcode=3)
59
 
        self.run_bzr('missing .')
60
 
        self.run_bzr('missing')
 
57
        base_rev = a_tree.branch.last_revision()
 
58
        self.run_bzr('pull', retcode=3, working_dir='a')
 
59
        self.run_bzr('missing', retcode=3, working_dir='a')
 
60
        self.run_bzr('missing .', working_dir='a')
 
61
        self.run_bzr('missing', working_dir='a')
61
62
        # this will work on windows because we check for the same branch
62
63
        # in pull - if it fails, it is a regression
63
 
        self.run_bzr('pull')
64
 
        self.run_bzr('pull /', retcode=3)
 
64
        self.run_bzr('pull', working_dir='a')
 
65
        self.run_bzr('pull /', retcode=3, working_dir='a')
65
66
        if sys.platform not in ('win32', 'cygwin'):
66
 
            self.run_bzr('pull')
 
67
            self.run_bzr('pull', working_dir='a')
67
68
 
68
 
        os.chdir('..')
69
69
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
70
 
        os.chdir('b')
71
 
        self.run_bzr('pull')
72
 
        os.mkdir('subdir')
 
70
        self.run_bzr('pull', working_dir='b')
 
71
        os.mkdir('b/subdir')
73
72
        b_tree.add('subdir')
74
 
        b_tree.commit(message='blah', allow_pointless=True)
75
 
 
76
 
        os.chdir('..')
77
 
        a = Branch.open('a')
78
 
        b = Branch.open('b')
79
 
        self.assertEqual(a.revision_history(), b.revision_history()[:-1])
80
 
 
81
 
        os.chdir('a')
82
 
        self.run_bzr('pull ../b')
83
 
        self.assertEqual(a.revision_history(), b.revision_history())
 
73
        new_rev = b_tree.commit(message='blah', allow_pointless=True)
 
74
 
 
75
        a = branch.Branch.open('a')
 
76
        b = branch.Branch.open('b')
 
77
        self.assertEqual(a.last_revision(), base_rev)
 
78
        self.assertEqual(b.last_revision(), new_rev)
 
79
 
 
80
        self.run_bzr('pull ../b', working_dir='a')
 
81
        self.assertEqual(a.last_revision(), b.last_revision())
84
82
        a_tree.commit(message='blah2', allow_pointless=True)
85
83
        b_tree.commit(message='blah3', allow_pointless=True)
86
84
        # no overwrite
87
 
        os.chdir('../b')
88
 
        self.run_bzr('pull ../a', retcode=3)
89
 
        os.chdir('..')
 
85
        self.run_bzr('pull ../a', retcode=3, working_dir='b')
90
86
        b_tree.bzrdir.sprout('overwriteme')
91
 
        os.chdir('overwriteme')
92
 
        self.run_bzr('pull --overwrite ../a')
93
 
        overwritten = Branch.open('.')
94
 
        self.assertEqual(overwritten.revision_history(),
95
 
                         a.revision_history())
 
87
        self.run_bzr('pull --overwrite ../a', working_dir='overwriteme')
 
88
        overwritten = branch.Branch.open('overwriteme')
 
89
        self.assertEqual(overwritten.last_revision(),
 
90
                         a.last_revision())
96
91
        a_tree.merge_from_branch(b_tree.branch)
97
92
        a_tree.commit(message="blah4", allow_pointless=True)
98
 
        os.chdir('../b/subdir')
99
 
        self.run_bzr('pull ../../a')
100
 
        self.assertEqual(a.revision_history()[-1], b.revision_history()[-1])
101
 
        sub_tree = WorkingTree.open_containing('.')[0]
 
93
 
 
94
        self.run_bzr('pull ../../a', working_dir='b/subdir')
 
95
        self.assertEqual(a.last_revision(), b.last_revision())
 
96
        sub_tree = workingtree.WorkingTree.open_containing('b/subdir')[0]
102
97
        sub_tree.commit(message="blah5", allow_pointless=True)
103
98
        sub_tree.commit(message="blah6", allow_pointless=True)
104
 
        os.chdir('..')
105
 
        self.run_bzr('pull ../a')
106
 
        os.chdir('../a')
 
99
        self.run_bzr('pull ../a', working_dir='b')
107
100
        a_tree.commit(message="blah7", allow_pointless=True)
108
101
        a_tree.merge_from_branch(b_tree.branch)
109
102
        a_tree.commit(message="blah8", allow_pointless=True)
110
 
        self.run_bzr('pull ../b')
111
 
        self.run_bzr('pull ../b')
 
103
        self.run_bzr('pull ../b', working_dir='a')
 
104
        self.run_bzr('pull ../b', working_dir='a')
112
105
 
113
106
    def test_pull_dash_d(self):
114
107
        self.example_branch('a')
134
127
 
135
128
        b_tree = a_tree.bzrdir.sprout('b',
136
129
                   revision_id=a_tree.branch.get_rev_id(1)).open_workingtree()
137
 
        os.chdir('b')
138
 
        self.run_bzr('pull -r 2')
139
 
        a = Branch.open('../a')
140
 
        b = Branch.open('.')
 
130
        self.run_bzr('pull -r 2', working_dir='b')
 
131
        a = branch.Branch.open('a')
 
132
        b = branch.Branch.open('b')
141
133
        self.assertEqual(a.revno(),4)
142
134
        self.assertEqual(b.revno(),2)
143
 
        self.run_bzr('pull -r 3')
 
135
        self.run_bzr('pull -r 3', working_dir='b')
144
136
        self.assertEqual(b.revno(),3)
145
 
        self.run_bzr('pull -r 4')
146
 
        self.assertEqual(a.revision_history(), b.revision_history())
 
137
        self.run_bzr('pull -r 4', working_dir='b')
 
138
        self.assertEqual(a.last_revision(), b.last_revision())
147
139
 
148
140
    def test_pull_tags(self):
149
141
        """Tags are updated by pull, and revisions named in those tags are
152
144
        # Make a source, sprout a target off it
153
145
        builder = self.make_branch_builder('source')
154
146
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
155
 
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
147
        source.get_config_stack().set('branch.fetch_tags', True)
156
148
        target_bzrdir = source.bzrdir.sprout('target')
157
149
        source.tags.set_tag('tag-a', 'rev-2')
158
150
        # Pull from source
179
171
        self.build_tree_contents([('a/foo', 'a third change')])
180
172
        a_tree.commit(message='a third change')
181
173
 
182
 
        rev_history_a = a_tree.branch.revision_history()
183
 
        self.assertEqual(len(rev_history_a), 3)
 
174
        self.assertEqual(a_tree.branch.last_revision_info()[0], 3)
184
175
 
185
176
        b_tree.merge_from_branch(a_tree.branch)
186
177
        b_tree.commit(message='merge')
187
178
 
188
 
        self.assertEqual(len(b_tree.branch.revision_history()), 2)
189
 
 
190
 
        os.chdir('b')
191
 
        self.run_bzr('pull --overwrite ../a')
192
 
        rev_history_b = b_tree.branch.revision_history()
193
 
        self.assertEqual(len(rev_history_b), 3)
194
 
 
195
 
        self.assertEqual(rev_history_b, rev_history_a)
 
179
        self.assertEqual(b_tree.branch.last_revision_info()[0], 2)
 
180
 
 
181
        self.run_bzr('pull --overwrite ../a', working_dir='b')
 
182
        (last_revinfo_b) = b_tree.branch.last_revision_info()
 
183
        self.assertEqual(last_revinfo_b[0], 3)
 
184
        self.assertEqual(last_revinfo_b[1], a_tree.branch.last_revision())
196
185
 
197
186
    def test_overwrite_children(self):
198
187
        # Make sure pull --overwrite sets the revision-history
210
199
        self.build_tree_contents([('a/foo', 'a third change')])
211
200
        a_tree.commit(message='a third change')
212
201
 
213
 
        self.assertEqual(len(a_tree.branch.revision_history()), 3)
 
202
        self.assertEqual(a_tree.branch.last_revision_info()[0], 3)
214
203
 
215
204
        b_tree.merge_from_branch(a_tree.branch)
216
205
        b_tree.commit(message='merge')
217
206
 
218
 
        self.assertEqual(len(b_tree.branch.revision_history()), 2)
 
207
        self.assertEqual(b_tree.branch.last_revision_info()[0], 2)
219
208
 
220
209
        self.build_tree_contents([('a/foo', 'a fourth change\n')])
221
210
        a_tree.commit(message='a fourth change')
222
211
 
223
 
        rev_history_a = a_tree.branch.revision_history()
224
 
        self.assertEqual(len(rev_history_a), 4)
 
212
        rev_info_a = a_tree.branch.last_revision_info()
 
213
        self.assertEqual(rev_info_a[0], 4)
225
214
 
226
215
        # With convergence, we could just pull over the
227
216
        # new change, but with --overwrite, we want to switch our history
228
 
        os.chdir('b')
229
 
        self.run_bzr('pull --overwrite ../a')
230
 
        rev_history_b = b_tree.branch.revision_history()
231
 
        self.assertEqual(len(rev_history_b), 4)
232
 
 
233
 
        self.assertEqual(rev_history_b, rev_history_a)
 
217
        self.run_bzr('pull --overwrite ../a', working_dir='b')
 
218
        rev_info_b = b_tree.branch.last_revision_info()
 
219
        self.assertEqual(rev_info_b[0], 4)
 
220
        self.assertEqual(rev_info_b, rev_info_a)
234
221
 
235
222
    def test_pull_remember(self):
236
223
        """Pull changes from one branch to another and test parent location."""
237
 
        transport = self.get_transport()
 
224
        t = self.get_transport()
238
225
        tree_a = self.make_branch_and_tree('branch_a')
239
226
        branch_a = tree_a.branch
240
227
        self.build_tree(['branch_a/a'])
252
239
        branch_b.set_parent(None)
253
240
        self.assertEqual(None, branch_b.get_parent())
254
241
        # test pull for failure without parent set
255
 
        os.chdir('branch_b')
256
 
        out = self.run_bzr('pull', retcode=3)
 
242
        out = self.run_bzr('pull', retcode=3, working_dir='branch_b')
257
243
        self.assertEqual(out,
258
244
                ('','bzr: ERROR: No pull location known or specified.\n'))
259
245
        # test implicit --remember when no parent set, this pull conflicts
260
 
        self.build_tree(['d'])
 
246
        self.build_tree(['branch_b/d'])
261
247
        tree_b.add('d')
262
248
        tree_b.commit('commit d')
263
 
        out = self.run_bzr('pull ../branch_a', retcode=3)
 
249
        out = self.run_bzr('pull ../branch_a', retcode=3,
 
250
                           working_dir='branch_b')
264
251
        self.assertEqual(out,
265
252
                ('','bzr: ERROR: These branches have diverged.'
266
253
                    ' Use the missing command to see how.\n'
267
254
                    'Use the merge command to reconcile them.\n'))
268
255
        self.assertEqual(branch_b.get_parent(), parent)
269
256
        # test implicit --remember after resolving previous failure
270
 
        uncommit(branch=branch_b, tree=tree_b)
271
 
        transport.delete('branch_b/d')
272
 
        self.run_bzr('pull')
 
257
        uncommit.uncommit(branch=branch_b, tree=tree_b)
 
258
        t.delete('branch_b/d')
 
259
        self.run_bzr('pull', working_dir='branch_b')
273
260
        self.assertEqual(branch_b.get_parent(), parent)
274
261
        # test explicit --remember
275
 
        self.run_bzr('pull ../branch_c --remember')
 
262
        self.run_bzr('pull ../branch_c --remember', working_dir='branch_b')
276
263
        self.assertEqual(branch_b.get_parent(),
277
264
                          branch_c.bzrdir.root_transport.base)
278
265
 
280
267
        from bzrlib.testament import Testament
281
268
        # Build up 2 trees and prepare for a pull
282
269
        tree_a = self.make_branch_and_tree('branch_a')
283
 
        f = open('branch_a/a', 'wb')
284
 
        f.write('hello')
285
 
        f.close()
 
270
        with open('branch_a/a', 'wb') as f:
 
271
            f.write('hello')
286
272
        tree_a.add('a')
287
273
        tree_a.commit('message')
288
274
 
289
275
        tree_b = tree_a.bzrdir.sprout('branch_b').open_workingtree()
290
276
 
291
277
        # Make a change to 'a' that 'b' can pull
292
 
        f = open('branch_a/a', 'wb')
293
 
        f.write('hey there')
294
 
        f.close()
 
278
        with open('branch_a/a', 'wb') as f:
 
279
            f.write('hey there')
295
280
        tree_a.commit('message')
296
281
 
297
282
        # Create the bundle for 'b' to pull
298
 
        os.chdir('branch_a')
299
 
        self.run_bzr('bundle ../branch_b -o ../bundle')
 
283
        self.run_bzr('bundle ../branch_b -o ../bundle', working_dir='branch_a')
300
284
 
301
 
        os.chdir('../branch_b')
302
 
        out, err = self.run_bzr('pull ../bundle')
 
285
        out, err = self.run_bzr('pull ../bundle', working_dir='branch_b')
303
286
        self.assertEqual(out,
304
287
                         'Now on revision 2.\n')
305
288
        self.assertEqual(err,
306
289
                ' M  a\nAll changes applied successfully.\n')
307
290
 
308
 
        self.assertEqualDiff(tree_a.branch.revision_history(),
309
 
                             tree_b.branch.revision_history())
 
291
        self.assertEqualDiff(tree_a.branch.last_revision(),
 
292
                             tree_b.branch.last_revision())
310
293
 
311
294
        testament_a = Testament.from_revision(tree_a.branch.repository,
312
295
                                              tree_a.get_parent_ids()[0])
316
299
                             testament_b.as_text())
317
300
 
318
301
        # it is legal to attempt to pull an already-merged bundle
319
 
        out, err = self.run_bzr('pull ../bundle')
 
302
        out, err = self.run_bzr('pull ../bundle', working_dir='branch_b')
320
303
        self.assertEqual(err, '')
321
 
        self.assertEqual(out, 'No revisions to pull.\n')
 
304
        self.assertEqual(out, 'No revisions or tags to pull.\n')
322
305
 
323
306
    def test_pull_verbose_no_files(self):
324
307
        """Pull --verbose should not list modified files"""
377
360
    def test_pull_verbose_uses_default_log(self):
378
361
        tree = self.example_branch('source')
379
362
        target = self.make_branch_and_tree('target')
380
 
        target_config = target.branch.get_config()
381
 
        target_config.set_user_option('log_format', 'short')
 
363
        target_config = target.branch.get_config_stack()
 
364
        target_config.set('log_format', 'short')
382
365
        out = self.run_bzr('pull -v source -d target')[0]
383
366
        self.assertContainsRe(out, r'\n {4}1 .*\n {6}setup\n')
384
367
        self.assertNotContainsRe(
385
368
            out, r'revno: 1\ncommitter: .*\nbranch nick: source')
386
369
 
 
370
    def test_pull_smart_bound_branch(self):
 
371
        self.setup_smart_server_with_call_log()
 
372
        parent = self.make_branch_and_tree('parent')
 
373
        parent.commit(message='first commit')
 
374
        child = parent.bzrdir.sprout('child').open_workingtree()
 
375
        child.commit(message='second commit')
 
376
        checkout = parent.branch.create_checkout('checkout')
 
377
        self.run_bzr(['pull', self.get_url('child')], working_dir='checkout')
 
378
 
387
379
    def test_pull_smart_stacked_streaming_acceptance(self):
388
380
        """'bzr pull -r 123' works on stacked, smart branches, even when the
389
381
        revision specified by the revno is only present in the fallback
412
404
        # become necessary for this use case. Please do not adjust this number
413
405
        # upwards without agreement from bzr's network support maintainers.
414
406
        self.assertLength(19, self.hpss_calls)
415
 
        remote = Branch.open('stacked')
 
407
        self.assertLength(1, self.hpss_connections)
 
408
        remote = branch.Branch.open('stacked')
416
409
        self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
417
 
    
 
410
 
418
411
    def test_pull_cross_format_warning(self):
419
412
        """You get a warning for probably slow cross-format pulls.
420
413
        """
482
475
        a_tree = self.example_branch('a')
483
476
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
484
477
 
485
 
        f = open(pathjoin('a', 'hello'),'wt')
486
 
        f.write('fee')
487
 
        f.close()
 
478
        with open(osutils.pathjoin('a', 'hello'),'wt') as f:
 
479
            f.write('fee')
488
480
        a_tree.commit('fee')
489
481
 
490
 
        f = open(pathjoin('b', 'hello'),'wt')
491
 
        f.write('fie')
492
 
        f.close()
 
482
        with open(osutils.pathjoin('b', 'hello'),'wt') as f:
 
483
            f.write('fie')
493
484
 
494
485
        out,err=self.run_bzr(['pull','-d','b','a','--show-base'])
495
486
 
496
487
        # check for message here
497
 
        self.assertEqual(err,
498
 
                         ' M  hello\nText conflict in hello\n1 conflicts encountered.\n')
 
488
        self.assertEqual(
 
489
            err,
 
490
            ' M  hello\nText conflict in hello\n1 conflicts encountered.\n')
499
491
 
500
492
        self.assertEqualDiff('<<<<<<< TREE\n'
501
493
                             'fie||||||| BASE-REVISION\n'
502
494
                             'foo=======\n'
503
495
                             'fee>>>>>>> MERGE-SOURCE\n',
504
 
                             open(pathjoin('b', 'hello')).read())
 
496
                             open(osutils.pathjoin('b', 'hello')).read())
505
497
 
506
498
    def test_pull_show_base_working_tree_only(self):
507
499
        """--show-base only allowed if there's a working tree
511
503
        self.make_branch('from')
512
504
        self.make_branch('to')
513
505
        out=self.run_bzr(['pull','-d','to','from','--show-base'],retcode=3)
514
 
        self.assertEqual(out,
515
 
                         ('','bzr: ERROR: Need working tree for --show-base.\n'))
 
506
        self.assertEqual(
 
507
            out, ('','bzr: ERROR: Need working tree for --show-base.\n'))
516
508
 
517
509
    def test_pull_tag_conflicts(self):
518
510
        """pulling tags with conflicts will change the exit code"""
524
516
        out = self.run_bzr(['pull','-d','to','from'],retcode=1)
525
517
        self.assertEqual(out,
526
518
            ('No revisions to pull.\nConflicting tags:\n    mytag\n', ''))
 
519
 
 
520
    def test_pull_tag_notification(self):
 
521
        """pulling tags with conflicts will change the exit code"""
 
522
        # create a branch, see that --show-base fails
 
523
        from_tree = self.make_branch_and_tree('from')
 
524
        from_tree.branch.tags.set_tag("mytag", "somerevid")
 
525
        to_tree = self.make_branch_and_tree('to')
 
526
        out = self.run_bzr(['pull', '-d', 'to', 'from'])
 
527
        self.assertEqual(out,
 
528
            ('1 tag(s) updated.\n', ''))
 
529
 
 
530
    def test_pull_tag_overwrite(self):
 
531
        """pulling tags with --overwrite only reports changed tags."""
 
532
        # create a branch, see that --show-base fails
 
533
        from_tree = self.make_branch_and_tree('from')
 
534
        from_tree.branch.tags.set_tag("mytag", "somerevid")
 
535
        to_tree = self.make_branch_and_tree('to')
 
536
        to_tree.branch.tags.set_tag("mytag", "somerevid")
 
537
        out = self.run_bzr(['pull', '--overwrite', '-d', 'to', 'from'])
 
538
        self.assertEqual(out,
 
539
            ('No revisions or tags to pull.\n', ''))
 
540
 
 
541
 
 
542
class TestPullOutput(script.TestCaseWithTransportAndScript):
 
543
 
 
544
    def test_pull_log_format(self):
 
545
        self.run_script("""
 
546
            $ bzr init trunk
 
547
            Created a standalone tree (format: 2a)
 
548
            $ cd trunk
 
549
            $ echo foo > file
 
550
            $ bzr add
 
551
            adding file
 
552
            $ bzr commit -m 'we need some foo'
 
553
            2>Committing to:...trunk/
 
554
            2>added file
 
555
            2>Committed revision 1.
 
556
            $ cd ..
 
557
            $ bzr init feature
 
558
            Created a standalone tree (format: 2a)
 
559
            $ cd feature
 
560
            $ bzr pull -v ../trunk -Olog_format=line
 
561
            Now on revision 1.
 
562
            Added Revisions:
 
563
            1: jrandom@example.com ...we need some foo
 
564
            2>+N  file
 
565
            2>All changes applied successfully.
 
566
            """)