22
22
import bzrlib.errors as errors
23
23
from bzrlib.errors import NoSuchRevision, UnlistableBranch, NotBranchError
25
from bzrlib.tests import TestCase, TestCaseInTempDir
26
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
25
from bzrlib.selftest import TestCase, TestCaseInTempDir
26
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
27
27
from bzrlib.trace import mutter
28
28
import bzrlib.transactions as transactions
29
from bzrlib.revision import NULL_REVISION
31
30
# TODO: Make a branch using basis branch, and check that it
32
31
# doesn't request any files that could have been avoided, by
37
36
def test_append_revisions(self):
38
37
"""Test appending more than one revision"""
39
br = Branch.initialize(u".")
38
br = Branch.initialize(".")
40
39
br.append_revision("rev1")
41
40
self.assertEquals(br.revision_history(), ["rev1",])
42
41
br.append_revision("rev2", "rev3")
50
49
b1 = Branch.initialize('b1')
51
50
b2 = Branch.initialize('b2')
52
51
file(os.sep.join(['b1', 'foo']), 'w').write('hello')
53
b1.working_tree().add(['foo'], ['foo-id'])
54
b1.working_tree().commit('lala!', rev_id='revision-1', allow_pointless=False)
52
b1.add(['foo'], ['foo-id'])
53
b1.commit('lala!', rev_id='revision-1', allow_pointless=False)
56
55
mutter('start fetch')
57
56
f = Fetcher(from_branch=b1, to_branch=b2)
63
62
tree = b2.revision_tree('revision-1')
64
63
eq(tree.get_file_text('foo-id'), 'hello')
66
def test_revision_tree(self):
67
b1 = Branch.initialize(u'.')
68
b1.working_tree().commit('lala!', rev_id='revision-1', allow_pointless=True)
69
tree = b1.revision_tree('revision-1')
70
tree = b1.revision_tree(None)
71
self.assertEqual(len(tree.list_files()), 0)
72
tree = b1.revision_tree(NULL_REVISION)
73
self.assertEqual(len(tree.list_files()), 0)
75
def get_unbalanced_branch_pair(self):
76
"""Return two branches, a and b, with one file in a."""
65
def test_push_stores(self):
66
"""Copy the stores from one branch to another"""
78
68
br_a = Branch.initialize("a")
79
69
file('a/b', 'wb').write('b')
80
br_a.working_tree().add('b')
81
commit(br_a, "silly commit", rev_id='A')
71
commit(br_a, "silly commit")
83
74
br_b = Branch.initialize("b")
86
def get_balanced_branch_pair(self):
87
"""Returns br_a, br_b as with one commit in a, and b has a's stores."""
88
br_a, br_b = self.get_unbalanced_branch_pair()
89
br_a.push_stores(br_b)
92
def test_push_stores(self):
93
"""Copy the stores from one branch to another"""
94
br_a, br_b = self.get_unbalanced_branch_pair()
95
# ensure the revision is missing.
96
75
self.assertRaises(NoSuchRevision, br_b.get_revision,
97
76
br_a.revision_history()[0])
98
77
br_a.push_stores(br_b)
99
# check that b now has all the data from a's first commit.
100
78
rev = br_b.get_revision(br_a.revision_history()[0])
101
79
tree = br_b.revision_tree(br_a.revision_history()[0])
102
80
for file_id in tree:
107
85
def test_copy_branch(self):
108
86
"""Copy the stores from one branch to another"""
109
br_a, br_b = self.get_balanced_branch_pair()
87
br_a, br_b = self.test_push_stores()
110
88
commit(br_b, "silly commit")
112
90
br_c = copy_branch(br_a, 'c', basis_branch=br_b)
116
94
"""Copy only part of the history of a branch."""
117
95
self.build_tree(['a/', 'a/one'])
118
96
br_a = Branch.initialize('a')
119
br_a.working_tree().add(['one'])
120
br_a.working_tree().commit('commit one', rev_id='u@d-1')
98
br_a.commit('commit one', rev_id='u@d-1')
121
99
self.build_tree(['a/two'])
122
br_a.working_tree().add(['two'])
123
br_a.working_tree().commit('commit two', rev_id='u@d-2')
101
br_a.commit('commit two', rev_id='u@d-2')
124
102
br_b = copy_branch(br_a, 'b', revision='u@d-1')
125
103
self.assertEqual(br_b.last_revision(), 'u@d-1')
126
104
self.assertTrue(os.path.exists('b/one'))
127
105
self.assertFalse(os.path.exists('b/two'))
129
108
def test_record_initial_ghost_merge(self):
130
109
"""A pending merge with no revision present is still a merge."""
131
branch = Branch.initialize(u'.')
132
branch.working_tree().add_pending_merge('non:existent@rev--ision--0--2')
133
branch.working_tree().commit('pretend to merge nonexistent-revision', rev_id='first')
110
branch = Branch.initialize('.')
111
branch.add_pending_merge('non:existent@rev--ision--0--2')
112
branch.commit('pretend to merge nonexistent-revision', rev_id='first')
134
113
rev = branch.get_revision(branch.last_revision())
135
114
self.assertEqual(len(rev.parent_ids), 1)
136
115
# parent_sha1s is not populated now, WTF. rbc 20051003
137
116
self.assertEqual(len(rev.parent_sha1s), 0)
138
117
self.assertEqual(rev.parent_ids[0], 'non:existent@rev--ision--0--2')
140
def test_bad_revision(self):
141
branch = Branch.initialize(u'.')
142
self.assertRaises(errors.InvalidRevisionId, branch.get_revision, None)
144
119
# TODO 20051003 RBC:
145
120
# compare the gpg-to-sign info for a commit with a ghost and
146
121
# an identical tree without a ghost
149
124
def test_pending_merges(self):
150
125
"""Tracking pending-merged revisions."""
151
b = Branch.initialize(u'.')
152
wt = b.working_tree()
153
self.assertEquals(wt.pending_merges(), [])
154
wt.add_pending_merge('foo@azkhazan-123123-abcabc')
155
self.assertEquals(wt.pending_merges(), ['foo@azkhazan-123123-abcabc'])
156
wt.add_pending_merge('foo@azkhazan-123123-abcabc')
157
self.assertEquals(wt.pending_merges(), ['foo@azkhazan-123123-abcabc'])
158
wt.add_pending_merge('wibble@fofof--20050401--1928390812')
159
self.assertEquals(wt.pending_merges(),
126
b = Branch.initialize('.')
128
self.assertEquals(b.pending_merges(), [])
129
b.add_pending_merge('foo@azkhazan-123123-abcabc')
130
self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
131
b.add_pending_merge('foo@azkhazan-123123-abcabc')
132
self.assertEquals(b.pending_merges(), ['foo@azkhazan-123123-abcabc'])
133
b.add_pending_merge('wibble@fofof--20050401--1928390812')
134
self.assertEquals(b.pending_merges(),
160
135
['foo@azkhazan-123123-abcabc',
161
136
'wibble@fofof--20050401--1928390812'])
162
b.working_tree().commit("commit from base with two merges")
137
b.commit("commit from base with two merges")
163
138
rev = b.get_revision(b.revision_history()[0])
164
139
self.assertEquals(len(rev.parent_ids), 2)
165
140
self.assertEquals(rev.parent_ids[0],
167
142
self.assertEquals(rev.parent_ids[1],
168
143
'wibble@fofof--20050401--1928390812')
169
144
# list should be cleared when we do a commit
170
self.assertEquals(wt.pending_merges(), [])
145
self.assertEquals(b.pending_merges(), [])
172
147
def test_sign_existing_revision(self):
173
branch = Branch.initialize(u'.')
174
branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
148
branch = Branch.initialize('.')
149
branch.commit("base", allow_pointless=True, rev_id='A')
175
150
from bzrlib.testament import Testament
176
151
branch.sign_revision('A', bzrlib.gpg.LoopbackGPGStrategy(None))
177
152
self.assertEqual(Testament.from_revision(branch, 'A').as_short_text(),
178
153
branch.revision_store.get('A', 'sig').read())
180
155
def test_store_signature(self):
181
branch = Branch.initialize(u'.')
156
branch = Branch.initialize('.')
182
157
branch.store_revision_signature(bzrlib.gpg.LoopbackGPGStrategy(None),
184
159
self.assertEqual('FOO', branch.revision_store.get('A', 'sig').read())
186
def test__relcontrolfilename(self):
187
branch = Branch.initialize(u'.')
188
self.assertEqual('.bzr/%25', branch._rel_controlfilename('%'))
190
def test__relcontrolfilename_empty(self):
191
branch = Branch.initialize(u'.')
192
self.assertEqual('.bzr', branch._rel_controlfilename(''))
194
def test_nicks(self):
195
"""Branch nicknames"""
197
branch = Branch.initialize('bzr.dev')
198
self.assertEqual(branch.nick, 'bzr.dev')
199
os.rename('bzr.dev', 'bzr.ab')
200
branch = Branch.open('bzr.ab')
201
self.assertEqual(branch.nick, 'bzr.ab')
202
branch.nick = "Aaron's branch"
203
branch.nick = "Aaron's branch"
204
self.failUnless(os.path.exists(branch.controlfilename("branch.conf")))
205
self.assertEqual(branch.nick, "Aaron's branch")
206
os.rename('bzr.ab', 'integration')
207
branch = Branch.open('integration')
208
self.assertEqual(branch.nick, "Aaron's branch")
209
branch.nick = u"\u1234"
210
self.assertEqual(branch.nick, u"\u1234")
212
def test_commit_nicks(self):
213
"""Nicknames are committed to the revision"""
215
branch = Branch.initialize('bzr.dev')
216
branch.nick = "My happy branch"
217
branch.working_tree().commit('My commit respect da nick.')
218
committed = branch.get_revision(branch.last_revision())
219
self.assertEqual(committed.properties["branch-nick"],
223
162
class TestRemote(TestCaseWithWebserver):
227
166
self.get_remote_url(''))
228
167
self.assertRaises(NotBranchError, Branch.open_containing,
229
168
self.get_remote_url('g/p/q'))
230
b = Branch.initialize(u'.')
169
b = Branch.initialize('.')
231
170
branch, relpath = Branch.open_containing(self.get_remote_url(''))
232
171
self.assertEqual('', relpath)
233
172
branch, relpath = Branch.open_containing(self.get_remote_url('g/p/q'))
237
176
# >>> from bzrlib.commit import commit
238
177
# >>> bzrlib.trace.silent = True
239
178
# >>> br1 = ScratchBranch(files=['foo', 'bar'])
240
# >>> br1.working_tree().add('foo')
241
# >>> br1.working_tree().add('bar')
242
181
# >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
243
182
# >>> br2 = ScratchBranch()
244
183
# >>> br2.update_revisions(br1)
362
301
self.failUnless(isinstance(self.branch._transaction,
363
302
transactions.PassThroughTransaction))
364
303
self.branch.unlock()
367
class TestBranchPushLocations(TestCaseInTempDir):
370
super(TestBranchPushLocations, self).setUp()
371
self.branch = Branch.initialize(u'.')
373
def test_get_push_location_unset(self):
374
self.assertEqual(None, self.branch.get_push_location())
376
def test_get_push_location_exact(self):
377
self.build_tree(['.bazaar/'])
378
print >> open('.bazaar/branches.conf', 'wt'), ("[%s]\n"
379
"push_location=foo" %
381
self.assertEqual("foo", self.branch.get_push_location())
383
def test_set_push_location(self):
384
self.branch.set_push_location('foo')
385
self.assertFileEqual("[%s]\n"
386
"push_location = foo" % os.getcwdu(),
387
'.bazaar/branches.conf')
389
# TODO RBC 20051029 test getting a push location from a branch in a
390
# recursive section - that is, it appends the branch name.