0c8ea742f2a35a04b7a2e04ce75ea3795f9e51f2
[jelmer/subvertpy.git] / tests / test_commit.py
1 # Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
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 """Commit and push tests."""
18
19 from bzrlib.branch import Branch, PullResult
20 from bzrlib.bzrdir import BzrDir
21 from bzrlib.errors import DivergedBranches
22 from bzrlib.workingtree import WorkingTree
23
24 from copy import copy
25 from repository import MAPPING_VERSION
26 import os
27 from tests import TestCaseWithSubversionRepository
28
29 class TestNativeCommit(TestCaseWithSubversionRepository):
30     def test_simple_commit(self):
31         self.make_client('d', 'dc')
32         self.build_tree({'dc/foo/bla': "data"})
33         self.client_add("dc/foo")
34         wt = self.open_checkout("dc")
35         revid = wt.commit(message="data")
36         self.assertEqual(wt.branch.generate_revision_id(1), revid)
37         self.client_update("dc")
38         self.assertEqual(wt.branch.generate_revision_id(1), 
39                 wt.branch.last_revision())
40         wt = WorkingTree.open("dc")
41         new_inventory = wt.branch.repository.get_inventory(
42                             wt.branch.last_revision())
43         self.assertTrue(new_inventory.has_filename("foo"))
44         self.assertTrue(new_inventory.has_filename("foo/bla"))
45
46     def test_commit_message(self):
47         self.make_client('d', 'dc')
48         self.build_tree({'dc/foo/bla': "data"})
49         self.client_add("dc/foo")
50         wt = self.open_checkout("dc")
51         revid = wt.commit(message="data")
52         self.assertEqual(wt.branch.generate_revision_id(1), revid)
53         self.assertEqual(
54                 wt.branch.generate_revision_id(1), wt.branch.last_revision())
55         new_revision = wt.branch.repository.get_revision(
56                             wt.branch.last_revision())
57         self.assertEqual(wt.branch.last_revision(), new_revision.revision_id)
58         self.assertEqual("data", new_revision.message)
59
60     def test_commit_message_nordic(self):
61         self.make_client('d', 'dc')
62         self.build_tree({'dc/foo/bla': "data"})
63         self.client_add("dc/foo")
64         wt = self.open_checkout("dc")
65         revid = wt.commit(message=u"\xe6\xf8\xe5")
66         self.assertEqual(revid, wt.branch.generate_revision_id(1))
67         self.assertEqual(
68                 wt.branch.generate_revision_id(1), wt.branch.last_revision())
69         new_revision = wt.branch.repository.get_revision(
70                             wt.branch.last_revision())
71         self.assertEqual(wt.branch.last_revision(), new_revision.revision_id)
72         self.assertEqual(u"\xe6\xf8\xe5", new_revision.message.decode("utf-8"))
73
74     def test_commit_update(self):
75         self.make_client('d', 'dc')
76         self.build_tree({'dc/foo/bla': "data"})
77         self.client_add("dc/foo")
78         wt = self.open_checkout("dc")
79         wt.set_pending_merges(["some-ghost-revision"])
80         wt.commit(message="data")
81         self.assertEqual(
82                 wt.branch.generate_revision_id(1),
83                 wt.branch.last_revision())
84
85     def test_commit_parents(self):
86         repos_url = self.make_client('d', 'dc')
87         self.build_tree({'dc/foo/bla': "data"})
88         self.client_add("dc/foo")
89         wt = self.open_checkout("dc")
90         wt.set_pending_merges(["some-ghost-revision"])
91         wt.commit(message="data")
92         self.assertEqual([wt.branch.generate_revision_id(0), "some-ghost-revision"],
93                          wt.branch.repository.revision_parents(
94                              wt.branch.last_revision()))
95         self.assertEqual("some-ghost-revision\n", 
96                 self.client_get_prop(repos_url, "bzr:merge", 1))
97
98     def test_commit_revision_id(self):
99         repos_url = self.make_client('d', 'dc')
100         wt = self.open_checkout("dc")
101         self.build_tree({'dc/foo/bla': "data", 'dc/bla': "otherdata"})
102         wt.add('bla')
103         wt.commit(message="data")
104
105         branch = Branch.open(repos_url)
106         builder = branch.get_commit_builder([branch.last_revision()], revision_id="my-revision-id")
107         tree = branch.repository.revision_tree(branch.last_revision())
108         new_tree = copy(tree)
109         ie = new_tree.inventory.root
110         ie.revision = None
111         builder.record_entry_contents(ie, [tree.inventory], '', new_tree)
112         builder.finish_inventory()
113         builder.commit("foo")
114
115         self.assertEqual("my-revision-id", 
116                 self.client_get_prop("dc", "bzr:revision-id-v%d" % MAPPING_VERSION, 2))
117
118     def test_mwh(self):
119         repo = self.make_client('d', 'sc')
120         def mv(*mvs):
121             for a, b in mvs:
122                 self.client_copy(a, b)
123                 self.client_delete(a)
124             self.client_commit('sc', '.')
125             self.client_update('sc')
126         self.build_tree({'sc/de/foo':'data', 'sc/de/bar':'DATA'})
127         self.client_add('sc/de')
128         self.client_commit('sc', 'blah') #1
129         self.client_update('sc')
130         os.mkdir('sc/de/trunk')
131         self.client_add('sc/de/trunk')
132         mv(('sc/de/foo', 'sc/de/trunk'), ('sc/de/bar', 'sc/de/trunk')) #2
133         mv(('sc/de', 'sc/pyd'))  #3
134         self.client_delete('sc/pyd/trunk/foo')
135         self.client_commit('sc', '.') #4
136         self.client_update('sc')
137
138         self.make_checkout(repo + '/pyd/trunk', 'pyd')
139         self.assertEqual("DATA", open('pyd/bar').read())
140
141         olddir = BzrDir.open("pyd")
142         os.mkdir('bc')
143         newdir = olddir.sprout("bc")
144         newdir.open_branch().pull(olddir.open_branch())
145         wt = newdir.open_workingtree()
146         self.assertEqual("DATA", open('bc/bar').read())
147         open('bc/bar', 'w').write('data')
148         wt.commit(message="Commit from Bzr")
149         olddir.open_branch().pull(newdir.open_branch())
150
151         self.client_update('pyd')
152         self.assertEqual("data", open('pyd/bar').read())
153         
154
155 class TestPush(TestCaseWithSubversionRepository):
156     def setUp(self):
157         super(TestPush, self).setUp()
158         self.repos_url = self.make_client('d', 'sc')
159
160         self.build_tree({'sc/foo/bla': "data"})
161         self.client_add("sc/foo")
162         self.client_commit("sc", "foo")
163
164         self.olddir = self.open_checkout_bzrdir("sc")
165         os.mkdir("dc")
166         self.newdir = self.olddir.sprout("dc")
167
168     def test_empty(self):
169         self.assertEqual(0, int(self.olddir.open_branch().pull(
170                                 self.newdir.open_branch())))
171
172     def test_empty_result(self):
173         result = self.olddir.open_branch().pull(self.newdir.open_branch())
174         self.assertIsInstance(result, PullResult)
175         self.assertEqual(result.old_revno, self.olddir.open_branch().revno())
176         self.assertEqual(result.master_branch, None)
177         self.assertEqual(result.target_branch.bzrdir.transport.base, self.olddir.transport.base)
178         self.assertEqual(result.source_branch.bzrdir.transport.base, self.newdir.transport.base)
179
180     def test_child(self):
181         self.build_tree({'sc/foo/bar': "data"})
182         self.client_add("sc/foo/bar")
183         self.client_commit("sc", "second message")
184
185         self.assertEqual(0, int(self.olddir.open_branch().pull(
186                                 self.newdir.open_branch())))
187
188     def test_diverged(self):
189         self.build_tree({'sc/foo/bar': "data"})
190         self.client_add("sc/foo/bar")
191         self.client_commit("sc", "second message")
192
193         olddir = BzrDir.open("sc")
194
195         self.build_tree({'dc/file': 'data'})
196         wt = self.newdir.open_workingtree()
197         wt.add('file')
198         wt.commit(message="Commit from Bzr")
199
200         self.assertRaises(DivergedBranches, 
201                           olddir.open_branch().pull,
202                           self.newdir.open_branch())
203
204     def test_change(self):
205         self.build_tree({'dc/foo/bla': 'other data'})
206         wt = self.newdir.open_workingtree()
207         wt.commit(message="Commit from Bzr")
208
209         self.olddir.open_branch().pull(self.newdir.open_branch())
210
211         repos = self.olddir.find_repository()
212         inv = repos.get_inventory(repos.generate_revision_id(2, ""))
213         self.assertEqual(repos.generate_revision_id(2, ""),
214                          inv[inv.path2id('foo/bla')].revision)
215         self.assertTrue(wt.branch.last_revision() in 
216           repos.revision_parents(repos.generate_revision_id(2, "")))
217         self.assertEqual(repos.generate_revision_id(2, ""),
218                         self.olddir.open_branch().last_revision())
219         self.assertEqual("other data", 
220             repos.revision_tree(repos.generate_revision_id(2, "")).get_file_text( inv.path2id("foo/bla")))
221
222     def test_simple(self):
223         self.build_tree({'dc/file': 'data'})
224         wt = self.newdir.open_workingtree()
225         wt.add('file')
226         wt.commit(message="Commit from Bzr")
227
228         self.olddir.open_branch().pull(self.newdir.open_branch())
229
230         repos = self.olddir.find_repository()
231         inv = repos.get_inventory(repos.generate_revision_id(2, ""))
232         self.assertTrue(inv.has_filename('file'))
233         self.assertTrue(wt.branch.last_revision() in 
234             repos.revision_parents(
235                 repos.generate_revision_id(2, "")))
236         self.assertEqual(repos.generate_revision_id(2, ""),
237                         self.olddir.open_branch().last_revision())
238
239     def test_pull_after_push(self):
240         self.build_tree({'dc/file': 'data'})
241         wt = self.newdir.open_workingtree()
242         wt.add('file')
243         wt.commit(message="Commit from Bzr")
244
245         self.olddir.open_branch().pull(self.newdir.open_branch())
246
247         repos = self.olddir.find_repository()
248         inv = repos.get_inventory(repos.generate_revision_id(2, ""))
249         self.assertTrue(inv.has_filename('file'))
250         self.assertTrue(wt.branch.last_revision() in 
251                          repos.revision_parents(repos.generate_revision_id(2, "")))
252         self.assertEqual(repos.generate_revision_id(2, ""),
253                         self.olddir.open_branch().last_revision())
254
255         self.newdir.open_branch().pull(self.olddir.open_branch())
256
257         self.assertEqual(repos.generate_revision_id(2, ""),
258                         self.newdir.open_branch().last_revision())
259
260     def test_message(self):
261         self.build_tree({'dc/file': 'data'})
262         wt = self.newdir.open_workingtree()
263         wt.add('file')
264         wt.commit(message="Commit from Bzr")
265
266         self.olddir.open_branch().pull(self.newdir.open_branch())
267
268         repos = self.olddir.find_repository()
269         self.assertEqual("Commit from Bzr",
270             repos.get_revision(repos.generate_revision_id(2, "")).message)
271
272     def test_message_nordic(self):
273         self.build_tree({'dc/file': 'data'})
274         wt = self.newdir.open_workingtree()
275         wt.add('file')
276         wt.commit(message=u"\xe6\xf8\xe5")
277
278         self.olddir.open_branch().pull(self.newdir.open_branch())
279
280         repos = self.olddir.find_repository()
281         self.assertEqual(u"\xe6\xf8\xe5",
282             repos.get_revision(repos.generate_revision_id(2, "")).message.decode("utf-8"))
283
284
285     def test_multiple(self):
286         self.build_tree({'dc/file': 'data'})
287         wt = self.newdir.open_workingtree()
288         wt.add('file')
289         wt.commit(message="Commit from Bzr")
290
291         self.build_tree({'dc/file': 'data2', 'dc/adir': None})
292         wt.add('adir')
293         wt.commit(message="Another commit from Bzr")
294
295         self.olddir.open_branch().pull(self.newdir.open_branch())
296
297         repos = self.olddir.find_repository()
298
299         self.assertEqual(repos.generate_revision_id(3, ""), 
300                         self.olddir.open_branch().last_revision())
301
302         inv = repos.get_inventory(repos.generate_revision_id(2, ""))
303         self.assertTrue(inv.has_filename('file'))
304         self.assertFalse(inv.has_filename('adir'))
305
306         inv = repos.get_inventory(repos.generate_revision_id(3, ""))
307         self.assertTrue(inv.has_filename('file'))
308         self.assertTrue(inv.has_filename('adir'))
309
310         self.assertTrue(wt.branch.last_revision() in 
311              repos.get_ancestry(repos.generate_revision_id(3, "")))
312
313 class TestPushNested(TestCaseWithSubversionRepository):
314     def setUp(self):
315         super(TestPushNested, self).setUp()
316         self.repos_url = self.make_client('d', 'sc')
317
318         self.build_tree({'sc/foo/trunk/bla': "data"})
319         self.client_add("sc/foo")
320         self.client_commit("sc", "foo")
321
322         self.olddir = self.open_checkout_bzrdir("sc/foo/trunk")
323         os.mkdir("dc")
324         self.newdir = self.olddir.sprout("dc")
325
326     def test_simple(self):
327         self.build_tree({'dc/file': 'data'})
328         wt = self.newdir.open_workingtree()
329         wt.add('file')
330         wt.commit(message="Commit from Bzr")
331         self.olddir.open_branch().pull(self.newdir.open_branch())
332         repos = self.olddir.find_repository()
333         self.client_update("sc")
334         self.assertTrue(os.path.exists("sc/foo/trunk/file"))
335         self.assertFalse(os.path.exists("sc/foo/trunk/filel"))