1
# (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
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
23
import bzrlib.bzrdir as bzrdir
22
from bzrlib import branch, bzrdir, errors, gpg, transactions, repository
24
23
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
25
from bzrlib.commit import commit
26
import bzrlib.errors as errors
24
from bzrlib.delta import TreeDelta
27
25
from bzrlib.errors import (FileExists,
30
28
UninitializableFormat,
34
31
from bzrlib.osutils import getcwd
35
32
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
36
33
from bzrlib.tests.bzrdir_implementations.test_bzrdir import TestCaseWithBzrDir
37
34
from bzrlib.trace import mutter
38
import bzrlib.transactions as transactions
39
35
from bzrlib.transport import get_transport
40
36
from bzrlib.transport.http import HttpServer
41
37
from bzrlib.transport.memory import MemoryServer
101
97
tree = b2.repository.revision_tree('revision-1')
102
98
self.assertEqual(tree.get_file_text('foo-id'), 'hello')
100
def test_get_revision_delta(self):
101
tree_a = self.make_branch_and_tree('a')
102
self.build_tree(['a/foo'])
103
tree_a.add('foo', 'file1')
104
tree_a.commit('rev1', rev_id='rev1')
105
self.build_tree(['a/vla'])
106
tree_a.add('vla', 'file2')
107
tree_a.commit('rev2', rev_id='rev2')
109
delta = tree_a.branch.get_revision_delta(1)
110
self.assertIsInstance(delta, TreeDelta)
111
self.assertEqual([('foo', 'file1', 'file')], delta.added)
112
delta = tree_a.branch.get_revision_delta(2)
113
self.assertIsInstance(delta, TreeDelta)
114
self.assertEqual([('vla', 'file2', 'file')], delta.added)
104
116
def get_unbalanced_tree_pair(self):
105
117
"""Return two branches, a and b, with one file in a."""
106
118
get_transport(self.get_url()).mkdir('a')
176
188
source = self.make_branch('source')
177
189
target = source.bzrdir.sprout(self.get_url('target')).open_branch()
178
190
self.assertEqual(source.bzrdir.root_transport.base, target.get_parent())
192
def test_submit_branch(self):
193
"""Submit location can be queried and set"""
194
branch = self.make_branch('branch')
195
self.assertEqual(branch.get_submit_branch(), None)
196
branch.set_submit_branch('sftp://example.com')
197
self.assertEqual(branch.get_submit_branch(), 'sftp://example.com')
198
branch.set_submit_branch('sftp://example.net')
199
self.assertEqual(branch.get_submit_branch(), 'sftp://example.net')
180
201
def test_record_initial_ghost_merge(self):
181
202
"""A pending merge with no revision present is still a merge."""
227
248
branch = wt.branch
228
249
wt.commit("base", allow_pointless=True, rev_id='A')
229
250
from bzrlib.testament import Testament
230
strategy = bzrlib.gpg.LoopbackGPGStrategy(None)
251
strategy = gpg.LoopbackGPGStrategy(None)
231
252
branch.repository.sign_revision('A', strategy)
232
253
self.assertEqual(Testament.from_revision(branch.repository,
233
254
'A').as_short_text(),
237
258
wt = self.make_branch_and_tree('.')
238
259
branch = wt.branch
239
260
branch.repository.store_revision_signature(
240
bzrlib.gpg.LoopbackGPGStrategy(None), 'FOO', 'A')
261
gpg.LoopbackGPGStrategy(None), 'FOO', 'A')
241
262
self.assertRaises(errors.NoSuchRevision,
242
263
branch.repository.has_signature_for_revision_id,
249
270
wt = self.make_branch_and_tree('source')
250
271
wt.commit('A', allow_pointless=True, rev_id='A')
251
272
wt.branch.repository.sign_revision('A',
252
bzrlib.gpg.LoopbackGPGStrategy(None))
273
gpg.LoopbackGPGStrategy(None))
253
274
#FIXME: clone should work to urls,
254
275
# wt.clone should work to disks.
255
276
self.build_tree(['target/'])
308
329
self.assertEqual(repo.bzrdir.root_transport.base,
309
330
child_branch.repository.bzrdir.root_transport.base)
310
child_branch = bzrlib.branch.Branch.open(self.get_url('child'))
331
child_branch = branch.Branch.open(self.get_url('child'))
311
332
self.assertEqual(repo.bzrdir.root_transport.base,
312
333
child_branch.repository.bzrdir.root_transport.base)
327
348
def test_get_commit_builder(self):
328
349
self.assertIsInstance(self.make_branch(".").get_commit_builder([]),
329
bzrlib.repository.CommitBuilder)
350
repository.CommitBuilder)
352
def test_generate_revision_history(self):
353
"""Create a fake revision history easily."""
354
tree = self.make_branch_and_tree('.')
355
rev1 = tree.commit('foo')
356
orig_history = tree.branch.revision_history()
357
rev2 = tree.commit('bar', allow_pointless=True)
358
tree.branch.generate_revision_history(rev1)
359
self.assertEqual(orig_history, tree.branch.revision_history())
332
362
class ChrootedTests(TestCaseWithBranch):
353
383
branch, relpath = Branch.open_containing(self.get_readonly_url('g/p/q'))
354
384
self.assertEqual('g/p/q', relpath)
356
# TODO: rewrite this as a regular unittest, without relying on the displayed output
357
# >>> from bzrlib.commit import commit
358
# >>> bzrlib.trace.silent = True
359
# >>> br1 = ScratchBranch(files=['foo', 'bar'])
360
# >>> br1.working_tree().add('foo')
361
# >>> br1.working_tree().add('bar')
362
# >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
363
# >>> br2 = ScratchBranch()
364
# >>> br2.update_revisions(br1)
366
# Added 1 inventories.
368
# >>> br2.revision_history()
370
# >>> br2.update_revisions(br1)
372
# >>> br1.text_store.total_size() == br2.text_store.total_size()
375
387
class InstrumentedTransaction(object):
490
502
self.assertEqual(None, self.get_branch().get_push_location())
492
504
def test_get_push_location_exact(self):
493
from bzrlib.config import (branches_config_filename,
505
from bzrlib.config import (locations_config_filename,
494
506
ensure_config_dir_exists)
495
507
ensure_config_dir_exists()
496
fn = branches_config_filename()
508
fn = locations_config_filename()
497
509
print >> open(fn, 'wt'), ("[%s]\n"
498
510
"push_location=foo" %
499
511
self.get_branch().base[:-1])
500
512
self.assertEqual("foo", self.get_branch().get_push_location())
502
514
def test_set_push_location(self):
503
from bzrlib.config import (branches_config_filename,
515
from bzrlib.config import (locations_config_filename,
504
516
ensure_config_dir_exists)
505
517
ensure_config_dir_exists()
506
fn = branches_config_filename()
518
fn = locations_config_filename()
507
519
self.get_branch().set_push_location('foo')
508
520
self.assertFileEqual("[%s]\n"
509
521
"push_location = foo" % self.get_branch().base[:-1],
527
539
t = get_transport(self.get_url())
528
540
readonly_t = get_transport(self.get_readonly_url())
529
541
made_branch = self.make_branch('.')
530
self.failUnless(isinstance(made_branch, bzrlib.branch.Branch))
542
self.failUnless(isinstance(made_branch, branch.Branch))
532
544
# find it via bzrdir opening:
533
545
opened_control = bzrdir.BzrDir.open(readonly_t.base)
538
550
self.branch_format.__class__))
540
552
# find it via Branch.open
541
opened_branch = bzrlib.branch.Branch.open(readonly_t.base)
553
opened_branch = branch.Branch.open(readonly_t.base)
542
554
self.failUnless(isinstance(opened_branch, made_branch.__class__))
543
555
self.assertEqual(made_branch._format.__class__,
544
556
opened_branch._format.__class__)
548
560
except NotImplementedError:
550
562
self.assertEqual(self.branch_format,
551
bzrlib.branch.BranchFormat.find_format(opened_control))
563
branch.BranchFormat.find_format(opened_control))