Fix more bugs in the bindings.
[jelmer/subvertpy.git] / tests / test_push.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19 from bzrlib.branch import Branch, BranchReferenceFormat
20 from bzrlib.bzrdir import BzrDir, BzrDirFormat
21 from bzrlib.errors import AlreadyBranchError, BzrError, DivergedBranches
22 from bzrlib.inventory import Inventory
23 from bzrlib.merge import Merger, Merge3Merger
24 from bzrlib.osutils import has_symlinks
25 from bzrlib.progress import DummyProgress
26 from bzrlib.repository import Repository
27 from bzrlib.tests import KnownFailure, TestCaseWithTransport
28 from bzrlib.trace import mutter
29 from bzrlib.workingtree import WorkingTree
30
31 import os
32 import format
33 from bzrlib.plugins.svn.errors import ChangesRootLHSHistory, MissingPrefix
34 from time import sleep
35 from commit import push
36 from mapping import SVN_PROP_BZR_REVISION_ID
37 from tests import TestCaseWithSubversionRepository
38
39 class TestPush(TestCaseWithSubversionRepository):
40     def setUp(self):
41         super(TestPush, self).setUp()
42         self.repos_url = self.make_client('d', 'sc')
43
44         self.build_tree({'sc/foo/bla': "data"})
45         self.client_add("sc/foo")
46         self.client_commit("sc", "foo")
47
48         self.svndir = BzrDir.open("sc")
49         os.mkdir("dc")
50         self.bzrdir = self.svndir.sprout("dc")
51
52     def test_empty(self):
53         svnbranch = self.svndir.open_branch()
54         bzrbranch = self.bzrdir.open_branch()
55         result = svnbranch.pull(bzrbranch)
56         self.assertEqual(0, result.new_revno - result.old_revno)
57         self.assertEqual(svnbranch.revision_history(),
58                          bzrbranch.revision_history())
59
60     def test_child(self):
61         self.build_tree({'sc/foo/bar': "data"})
62         self.client_add("sc/foo/bar")
63         self.client_commit("sc", "second message")
64
65         svnbranch = self.svndir.open_branch()
66         bzrbranch = self.bzrdir.open_branch()
67         result = svnbranch.pull(bzrbranch)
68         self.assertEqual(0, result.new_revno - result.old_revno)
69
70     def test_diverged(self):
71         self.build_tree({'sc/foo/bar': "data"})
72         self.client_add("sc/foo/bar")
73         self.client_commit("sc", "second message")
74
75         svndir = BzrDir.open("sc")
76
77         self.build_tree({'dc/file': 'data'})
78         wt = self.bzrdir.open_workingtree()
79         wt.add('file')
80         wt.commit(message="Commit from Bzr")
81
82         self.assertRaises(DivergedBranches, 
83                           svndir.open_branch().pull,
84                           self.bzrdir.open_branch())
85
86     def test_change(self):
87         self.build_tree({'dc/foo/bla': 'other data'})
88         wt = self.bzrdir.open_workingtree()
89         newid = wt.commit(message="Commit from Bzr")
90
91         svnbranch = self.svndir.open_branch()
92         svnbranch.pull(self.bzrdir.open_branch())
93
94         repos = self.svndir._find_repository()
95         mapping = repos.get_mapping()
96         self.assertEquals(newid, svnbranch.last_revision())
97         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
98         self.assertEqual(newid, inv[inv.path2id('foo/bla')].revision)
99         self.assertEqual(wt.branch.last_revision(),
100           repos.generate_revision_id(2, "", mapping))
101         self.assertEqual(repos.generate_revision_id(2, "", mapping),
102                         self.svndir.open_branch().last_revision())
103         self.assertEqual("other data", 
104             repos.revision_tree(repos.generate_revision_id(2, "", 
105                                 mapping)).get_file_text(inv.path2id("foo/bla")))
106
107     def test_simple(self):
108         self.build_tree({'dc/file': 'data'})
109         wt = self.bzrdir.open_workingtree()
110         wt.add('file')
111         wt.commit(message="Commit from Bzr")
112
113         self.svndir.open_branch().pull(self.bzrdir.open_branch())
114
115         repos = self.svndir._find_repository()
116         mapping = repos.get_mapping()
117         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
118         self.assertTrue(inv.has_filename('file'))
119         self.assertEquals(wt.branch.last_revision(),
120                 repos.generate_revision_id(2, "", mapping))
121         self.assertEqual(repos.generate_revision_id(2, "", mapping),
122                         self.svndir.open_branch().last_revision())
123
124     def test_override_revprops(self):
125         self.svndir._find_repository().get_config().set_user_option("override-svn-revprops", "True")
126         self.build_tree({'dc/file': 'data'})
127         wt = self.bzrdir.open_workingtree()
128         wt.add('file')
129         wt.commit(message="Commit from Bzr", committer="Sombody famous", timestamp=1012604400, timezone=0)
130
131         self.svndir.open_branch().pull(self.bzrdir.open_branch())
132
133         self.assertEquals(("Sombody famous", "2002-02-01T23:00:00.000000Z", "Commit from Bzr"), 
134             self.client_log(self.repos_url)[2][1:])
135
136     def test_empty_file(self):
137         self.build_tree({'dc/file': ''})
138         wt = self.bzrdir.open_workingtree()
139         wt.add('file')
140         wt.commit(message="Commit from Bzr")
141
142         self.svndir.open_branch().pull(self.bzrdir.open_branch())
143
144         repos = self.svndir._find_repository()
145         mapping = repos.get_mapping()
146         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
147         self.assertTrue(inv.has_filename('file'))
148         self.assertEquals(wt.branch.last_revision(),
149                 repos.generate_revision_id(2, "", mapping))
150         self.assertEqual(repos.generate_revision_id(2, "", mapping),
151                         self.svndir.open_branch().last_revision())
152
153     def test_symlink(self):
154         if not has_symlinks():
155             return
156         os.symlink("bla", "dc/south")
157         assert os.path.islink("dc/south")
158         wt = self.bzrdir.open_workingtree()
159         wt.add('south')
160         wt.commit(message="Commit from Bzr")
161
162         self.svndir.open_branch().pull(self.bzrdir.open_branch())
163
164         repos = self.svndir._find_repository()
165         mapping = repos.get_mapping() 
166         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
167         self.assertTrue(inv.has_filename('south'))
168         self.assertEquals('symlink', inv[inv.path2id('south')].kind)
169         self.assertEquals('bla', inv[inv.path2id('south')].symlink_target)
170
171     def test_pull_after_push(self):
172         self.build_tree({'dc/file': 'data'})
173         wt = self.bzrdir.open_workingtree()
174         wt.add('file')
175         wt.commit(message="Commit from Bzr")
176
177         self.svndir.open_branch().pull(self.bzrdir.open_branch())
178
179         repos = self.svndir._find_repository()
180         mapping = repos.get_mapping()
181         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
182         self.assertTrue(inv.has_filename('file'))
183         self.assertEquals(wt.branch.last_revision(),
184                          repos.generate_revision_id(2, "", mapping))
185         self.assertEqual(repos.generate_revision_id(2, "", mapping),
186                         self.svndir.open_branch().last_revision())
187
188         self.bzrdir.open_branch().pull(self.svndir.open_branch())
189
190         self.assertEqual(repos.generate_revision_id(2, "", mapping),
191                         self.bzrdir.open_branch().last_revision())
192
193     def test_branch_after_push(self):
194         self.build_tree({'dc/file': 'data'})
195         wt = self.bzrdir.open_workingtree()
196         wt.add('file')
197         wt.commit(message="Commit from Bzr")
198
199         self.svndir.open_branch().pull(self.bzrdir.open_branch())
200
201         os.mkdir("b")
202         repos = self.svndir.sprout("b")
203
204         self.assertEqual(Branch.open("dc").revision_history(), 
205                          Branch.open("b").revision_history())
206
207     def test_message(self):
208         self.build_tree({'dc/file': 'data'})
209         wt = self.bzrdir.open_workingtree()
210         wt.add('file')
211         wt.commit(message="Commit from Bzr")
212
213         self.svndir.open_branch().pull(self.bzrdir.open_branch())
214
215         repos = self.svndir._find_repository()
216         mapping = repos.get_mapping()
217         self.assertEqual("Commit from Bzr",
218           repos.get_revision(repos.generate_revision_id(2, "", mapping)).message)
219
220     def test_commit_set_revid(self):
221         self.build_tree({'dc/file': 'data'})
222         wt = self.bzrdir.open_workingtree()
223         wt.add('file')
224         wt.commit(message="Commit from Bzr", rev_id="some-rid")
225
226         self.svndir.open_branch().pull(self.bzrdir.open_branch())
227
228         self.client_update("sc")
229         self.assertEqual("3 some-rid\n", 
230                 self.client_get_prop("sc", SVN_PROP_BZR_REVISION_ID+"none"))
231
232     def test_commit_check_rev_equal(self):
233         self.build_tree({'dc/file': 'data'})
234         wt = self.bzrdir.open_workingtree()
235         wt.add('file')
236         wt.commit(message="Commit from Bzr")
237
238         self.svndir.open_branch().pull(self.bzrdir.open_branch())
239
240         rev1 = self.svndir._find_repository().get_revision(wt.branch.last_revision())
241         rev2 = self.bzrdir.find_repository().get_revision(wt.branch.last_revision())
242
243         self.assertEqual(rev1.committer, rev2.committer)
244         self.assertEqual(rev1.timestamp, rev2.timestamp)
245         self.assertEqual(rev1.timezone, rev2.timezone)
246         self.assertEqual(rev1.properties, rev2.properties)
247         self.assertEqual(rev1.message, rev2.message)
248         self.assertEqual(rev1.revision_id, rev2.revision_id)
249
250     def test_multiple(self):
251         self.build_tree({'dc/file': 'data'})
252         wt = self.bzrdir.open_workingtree()
253         wt.add('file')
254         wt.commit(message="Commit from Bzr")
255
256         self.build_tree({'dc/file': 'data2', 'dc/adir': None})
257         wt.add('adir')
258         wt.commit(message="Another commit from Bzr")
259
260         self.svndir.open_branch().pull(self.bzrdir.open_branch())
261
262         repos = self.svndir._find_repository()
263
264         mapping = repos.get_mapping()
265
266         self.assertEqual(repos.generate_revision_id(3, "", mapping), 
267                         self.svndir.open_branch().last_revision())
268
269         inv = repos.get_inventory(repos.generate_revision_id(2, "", mapping))
270         self.assertTrue(inv.has_filename('file'))
271         self.assertFalse(inv.has_filename('adir'))
272
273         inv = repos.get_inventory(repos.generate_revision_id(3, "", mapping))
274         self.assertTrue(inv.has_filename('file'))
275         self.assertTrue(inv.has_filename('adir'))
276
277         self.assertEqual(self.svndir.open_branch().revision_history(),
278                          self.bzrdir.open_branch().revision_history())
279
280         self.assertEqual(wt.branch.last_revision(), 
281                 repos.generate_revision_id(3, "", mapping))
282         self.assertEqual(
283                 wt.branch.repository.get_ancestry(wt.branch.last_revision()), 
284                 repos.get_ancestry(wt.branch.last_revision()))
285
286     def test_multiple_diverged(self):
287         oc_url = self.make_client("o", "oc")
288
289         self.build_tree({'dc/file': 'data'})
290         wt = self.bzrdir.open_workingtree()
291         wt.add('file')
292         wt.commit(message="Commit from Bzr")
293
294         self.build_tree({'oc/file': 'data2', 'oc/adir': None})
295         self.client_add("oc/file")
296         self.client_add("oc/adir")
297         self.client_commit("oc", "Another commit from Bzr")
298
299         self.assertRaises(DivergedBranches, 
300                 lambda: Branch.open(oc_url).pull(self.bzrdir.open_branch()))
301
302     def test_different_branch_path(self):
303         # A       ,> C
304         # \ -> B /
305         self.build_tree({'sc/trunk/foo': "data", 'sc/branches': None})
306         self.client_add("sc/trunk")
307         self.client_add("sc/branches")
308         self.client_commit("sc", "foo")
309
310         self.client_copy('sc/trunk', 'sc/branches/mybranch')
311         self.build_tree({'sc/branches/mybranch/foo': "data2"})
312         self.client_commit("sc", "add branch")
313
314         self.svndir = BzrDir.open("sc/branches/mybranch")
315         os.mkdir("mybranch")
316         self.bzrdir = self.svndir.sprout("mybranch")
317
318         self.build_tree({'mybranch/foo': 'bladata'})
319         wt = self.bzrdir.open_workingtree()
320         revid = wt.commit(message="Commit from Bzr")
321         push(Branch.open("sc/trunk"), wt.branch, 
322              wt.branch.revision_history()[-2])
323         mutter('log %r' % self.client_log("sc/trunk")[4][0])
324         self.assertEquals('M',
325             self.client_log("sc/trunk")[4][0]['/trunk'].action)
326         push(Branch.open("sc/trunk"), wt.branch, wt.branch.last_revision())
327         mutter('log %r' % self.client_log("sc/trunk")[5][0])
328         self.assertEquals("/branches/mybranch", 
329             self.client_log("sc/trunk")[5][0]['/trunk'].copyfrom_path)
330
331 class PushNewBranchTests(TestCaseWithSubversionRepository):
332     def test_single_revision(self):
333         repos_url = self.make_client("a", "dc")
334         bzrwt = BzrDir.create_standalone_workingtree("c", 
335             format=format.get_rich_root_format())
336         self.build_tree({'c/test': "Tour"})
337         bzrwt.add("test")
338         revid = bzrwt.commit("Do a commit")
339         newdir = BzrDir.open("%s/trunk" % repos_url)
340         newbranch = newdir.import_branch(bzrwt.branch)
341         newtree = newbranch.repository.revision_tree(revid)
342         bzrwt.lock_read()
343         self.assertEquals(bzrwt.inventory.root.file_id,
344                           newtree.inventory.root.file_id)
345         bzrwt.unlock()
346         self.assertEquals(revid, newbranch.last_revision())
347         self.assertEquals([revid], newbranch.revision_history())
348
349     def test_single_revision_single_branching_scheme(self):
350         repos_url = self.make_client("a", "dc")
351         bzrwt = BzrDir.create_standalone_workingtree("c", 
352             format=format.get_rich_root_format())
353         self.build_tree({'c/test': "Tour"})
354         bzrwt.add("test")
355         revid = bzrwt.commit("Do a commit")
356         self.build_tree({"dc/some/funny/branch": None})
357         self.client_add("dc/some")
358         self.client_commit("dc", "msg")
359         newdir = BzrDir.open("%s/some/funny/branch/name" % repos_url)
360         newbranch = newdir.import_branch(bzrwt.branch)
361         self.assertEquals(revid, newbranch.last_revision())
362
363     # revision graph for the two tests below:
364     # svn-1
365     # |
366     # base
367     # |    \
368     # diver svn2
369     # |    /
370     # merge
371
372     def test_push_replace_existing_root(self):
373         repos_url = self.make_client("test", "svnco")
374         self.build_tree({'svnco/foo.txt': 'foo'})
375         self.client_add("svnco/foo.txt")
376         self.client_commit("svnco", "add file") #1
377         self.client_update("svnco")
378
379         os.mkdir('bzrco')
380         dir = BzrDir.open(repos_url).sprout("bzrco")
381         wt = dir.open_workingtree()
382         self.build_tree({'bzrco/bar.txt': 'bar'})
383         wt.add("bar.txt")
384         base_revid = wt.commit("add another file", rev_id="mybase")
385         wt.branch.push(Branch.open(repos_url))
386
387         self.build_tree({"svnco/baz.txt": "baz"})
388         self.client_add("svnco/baz.txt")
389         self.assertEquals(3, 
390                 self.client_commit("svnco", "add yet another file")[0])
391         self.client_update("svnco")
392
393         self.build_tree({"bzrco/qux.txt": "qux"})
394         wt.add("qux.txt")
395         wt.commit("add still more files", rev_id="mydiver")
396
397         repos = Repository.open(repos_url)
398         wt.branch.repository.fetch(repos)
399         mapping = repos.get_mapping()
400         other_rev = repos.generate_revision_id(3, "", mapping)
401         wt.lock_write()
402         try:
403             merge = Merger.from_revision_ids(DummyProgress(), wt, other=other_rev)
404             merge.merge_type = Merge3Merger
405             merge.do_merge()
406             self.assertEquals(base_revid, merge.base_rev_id)
407             merge.set_pending()
408             self.assertEquals([wt.last_revision(), other_rev], wt.get_parent_ids())
409             wt.commit("merge", rev_id="mymerge")
410         finally:
411             wt.unlock()
412         self.assertTrue(os.path.exists("bzrco/baz.txt"))
413         self.assertRaises(BzrError, 
414                 lambda: wt.branch.push(Branch.open(repos_url)))
415
416     def test_push_replace_existing_branch(self):
417         repos_url = self.make_client("test", "svnco")
418         self.build_tree({'svnco/trunk/foo.txt': 'foo'})
419         self.client_add("svnco/trunk")
420         self.client_commit("svnco", "add file") #1
421         self.client_update("svnco")
422
423         os.mkdir('bzrco')
424         dir = BzrDir.open(repos_url+"/trunk").sprout("bzrco")
425         wt = dir.open_workingtree()
426         self.build_tree({'bzrco/bar.txt': 'bar'})
427         wt.add("bar.txt")
428         base_revid = wt.commit("add another file", rev_id="mybase")
429         wt.branch.push(Branch.open(repos_url+"/trunk"))
430
431         self.build_tree({"svnco/trunk/baz.txt": "baz"})
432         self.client_add("svnco/trunk/baz.txt")
433         self.assertEquals(3, 
434                 self.client_commit("svnco", "add yet another file")[0])
435         self.client_update("svnco")
436
437         self.build_tree({"bzrco/qux.txt": "qux"})
438         wt.add("qux.txt")
439         wt.commit("add still more files", rev_id="mydiver")
440
441         repos = Repository.open(repos_url)
442         wt.branch.repository.fetch(repos)
443         mapping = repos.get_mapping()
444         other_rev = repos.generate_revision_id(3, "trunk", mapping)
445         wt.lock_write()
446         try:
447             merge = Merger.from_revision_ids(DummyProgress(), wt, other=other_rev)
448             merge.merge_type = Merge3Merger
449             merge.do_merge()
450             self.assertEquals(base_revid, merge.base_rev_id)
451             merge.set_pending()
452             self.assertEquals([wt.last_revision(), other_rev], wt.get_parent_ids())
453             wt.commit("merge", rev_id="mymerge")
454         finally:
455             wt.unlock()
456         self.assertTrue(os.path.exists("bzrco/baz.txt"))
457         wt.branch.push(Branch.open(repos_url+"/trunk"))
458
459     def test_missing_prefix_error(self):
460         repos_url = self.make_client("a", "dc")
461         bzrwt = BzrDir.create_standalone_workingtree("c", 
462             format=format.get_rich_root_format())
463         self.build_tree({'c/test': "Tour"})
464         bzrwt.add("test")
465         revid = bzrwt.commit("Do a commit")
466         newdir = BzrDir.open(repos_url+"/foo/trunk")
467         self.assertRaises(MissingPrefix, 
468                           lambda: newdir.import_branch(bzrwt.branch))
469
470     def test_repeat(self):
471         repos_url = self.make_client("a", "dc")
472         bzrwt = BzrDir.create_standalone_workingtree("c", 
473             format=format.get_rich_root_format())
474         self.build_tree({'c/test': "Tour"})
475         bzrwt.add("test")
476         revid = bzrwt.commit("Do a commit")
477         newdir = BzrDir.open(repos_url+"/trunk")
478         newbranch = newdir.import_branch(bzrwt.branch)
479         self.assertEquals(revid, newbranch.last_revision())
480         self.assertEquals([revid], newbranch.revision_history())
481         self.build_tree({'c/test': "Tour de France"})
482         bzrwt.commit("Do a commit")
483         newdir = BzrDir.open(repos_url+"/trunk")
484         self.assertRaises(AlreadyBranchError, newdir.import_branch, 
485                           bzrwt.branch)
486
487     def test_multiple(self):
488         repos_url = self.make_client("a", "dc")
489         bzrwt = BzrDir.create_standalone_workingtree("c", 
490             format=format.get_rich_root_format())
491         self.build_tree({'c/test': "Tour"})
492         bzrwt.add("test")
493         revid1 = bzrwt.commit("Do a commit")
494         self.build_tree({'c/test': "Tour de France"})
495         revid2 = bzrwt.commit("Do a commit")
496         newdir = BzrDir.open(repos_url+"/trunk")
497         newbranch = newdir.import_branch(bzrwt.branch)
498         self.assertEquals(revid2, newbranch.last_revision())
499         self.assertEquals([revid1, revid2], newbranch.revision_history())
500
501     def test_dato(self):
502         repos_url = self.make_client("a", "dc")
503         bzrwt = BzrDir.create_standalone_workingtree("c", 
504             format=format.get_rich_root_format())
505         self.build_tree({'c/foo.txt': "foo"})
506         bzrwt.add("foo.txt")
507         revid1 = bzrwt.commit("Do a commit", 
508                               committer=u"Adeodato Simó <dato@net.com.org.es>")
509         newdir = BzrDir.open(repos_url+"/trunk")
510         newdir.import_branch(bzrwt.branch)
511         self.assertEquals(u"Adeodato Simó <dato@net.com.org.es>", 
512                 Repository.open(repos_url).get_revision(revid1).committer)
513
514     def test_utf8_commit_msg(self):
515         repos_url = self.make_client("a", "dc")
516         bzrwt = BzrDir.create_standalone_workingtree("c", 
517             format=format.get_rich_root_format())
518         self.build_tree({'c/foo.txt': "foo"})
519         bzrwt.add("foo.txt")
520         revid1 = bzrwt.commit(u"Do Ã¡ commït")
521         newdir = BzrDir.open(repos_url+"/trunk")
522         newdir.import_branch(bzrwt.branch)
523         self.assertEquals(u"Do Ã¡ commït",
524                 Repository.open(repos_url).get_revision(revid1).message)
525
526     def test_multiple_part_exists(self):
527         repos_url = self.make_client("a", "dc")
528         self.build_tree({'dc/trunk/myfile': "data", 'dc/branches': None})
529         self.client_add('dc/trunk')
530         self.client_add('dc/branches')
531         self.client_commit("dc", "Message")
532         svnrepos = Repository.open(repos_url)
533         os.mkdir("c")
534         bzrdir = BzrDir.open(repos_url+"/trunk").sprout("c")
535         bzrwt = bzrdir.open_workingtree()
536         self.build_tree({'c/myfile': "Tour"})
537         revid1 = bzrwt.commit("Do a commit")
538         self.build_tree({'c/myfile': "Tour de France"})
539         revid2 = bzrwt.commit("Do a commit")
540         newdir = BzrDir.open(repos_url+"/branches/mybranch")
541         newbranch = newdir.import_branch(bzrwt.branch)
542         self.assertEquals(revid2, newbranch.last_revision())
543         mapping = svnrepos.get_mapping()
544         self.assertEquals([
545             svnrepos.generate_revision_id(1, "trunk", mapping) 
546             , revid1, revid2], newbranch.revision_history())
547
548     def test_push_overwrite(self):
549         repos_url = self.make_client("a", "dc")
550         self.build_tree({'dc/trunk/bloe': "text"})
551         self.client_add("dc/trunk")
552         self.client_commit("dc", "initial")
553
554         os.mkdir("d1")
555         bzrdir = BzrDir.open(repos_url+"/trunk").sprout("d1")
556         bzrwt1 = bzrdir.open_workingtree()
557
558         os.mkdir("d2")
559         bzrdir = BzrDir.open(repos_url+"/trunk").sprout("d2")
560         bzrwt2 = bzrdir.open_workingtree()
561
562         self.build_tree({'d1/myfile': "Tour"})
563         bzrwt1.add("myfile")
564         revid1 = bzrwt1.commit("Do a commit")
565
566         self.build_tree({'d2/myfile': "France"})
567         bzrwt2.add("myfile")
568         revid2 = bzrwt1.commit("Do a commit")
569
570         bzrwt1.branch.push(Branch.open(repos_url+"/trunk"))
571
572         raise KnownFailure("push --overwrite not supported yet")
573
574         bzrwt2.branch.push(Branch.open(repos_url+"/trunk"), overwrite=True)
575
576         self.assertEquals([revid2], 
577                 Branch.open(repos_url+"/trunk").revision_history())
578
579     def test_complex_rename(self):
580         repos_url = self.make_client("a", "dc")
581         bzrwt = BzrDir.create_standalone_workingtree("c", 
582             format=format.get_rich_root_format())
583         self.build_tree({'c/registry/generic.c': "Tour"})
584         bzrwt.add("registry")
585         bzrwt.add("registry/generic.c")
586         revid1 = bzrwt.commit("Add initial directory + file")
587         bzrwt.rename_one("registry", "registry.moved")
588         os.unlink("c/registry.moved/generic.c")
589         bzrwt.remove("registry.moved/generic.c")
590         self.build_tree({'c/registry/generic.c': "bla"})
591         bzrwt.add("registry")
592         bzrwt.add("registry/generic.c")
593         revid2 = bzrwt.commit("Do some funky things")
594         newdir = BzrDir.open(repos_url+"/trunk")
595         newbranch = newdir.import_branch(bzrwt.branch)
596         self.assertEquals(revid2, newbranch.last_revision())
597         self.assertEquals([revid1, revid2], newbranch.revision_history())
598         tree = newbranch.repository.revision_tree(revid2)
599         mutter("inventory: %r" % tree.inventory.entries())
600         delta = tree.changes_from(bzrwt)
601         self.assertFalse(delta.has_changed())
602         self.assertTrue(tree.inventory.has_filename("registry"))
603         self.assertTrue(tree.inventory.has_filename("registry.moved"))
604         self.assertTrue(tree.inventory.has_filename("registry/generic.c"))
605         self.assertFalse(tree.inventory.has_filename("registry.moved/generic.c"))
606         os.mkdir("n")
607         BzrDir.open(repos_url+"/trunk").sprout("n")
608
609     def test_rename_dir_changing_contents(self):
610         repos_url = self.make_client("a", "dc")
611         bzrwt = BzrDir.create_standalone_workingtree("c", 
612             format=format.get_rich_root_format())
613         self.build_tree({'c/registry/generic.c': "Tour"})
614         bzrwt.add("registry", "dirid")
615         bzrwt.add("registry/generic.c", "origid")
616         revid1 = bzrwt.commit("Add initial directory + file")
617         bzrwt.rename_one("registry/generic.c", "registry/c.c")
618         self.build_tree({'c/registry/generic.c': "Tour2"})
619         bzrwt.add("registry/generic.c", "newid")
620         revid2 = bzrwt.commit("Other change")
621         bzrwt.rename_one("registry", "registry.moved")
622         revid3 = bzrwt.commit("Rename")
623         newdir = BzrDir.open(repos_url+"/trunk")
624         newbranch = newdir.import_branch(bzrwt.branch)
625         def check(b):
626             self.assertEquals([revid1, revid2, revid3], b.revision_history())
627             tree = b.repository.revision_tree(revid3)
628             self.assertEquals("origid", tree.path2id("registry.moved/c.c"))
629             self.assertEquals("newid", tree.path2id("registry.moved/generic.c"))
630             self.assertEquals("dirid", tree.path2id("registry.moved"))
631         check(newbranch)
632         os.mkdir("n")
633         BzrDir.open(repos_url+"/trunk").sprout("n")
634         copybranch = Branch.open("n")
635         check(copybranch)
636     
637     def test_rename_dir(self):
638         repos_url = self.make_client("a", "dc")
639         bzrwt = BzrDir.create_standalone_workingtree("c", 
640             format=format.get_rich_root_format())
641         self.build_tree({'c/registry/generic.c': "Tour"})
642         bzrwt.add("registry", "dirid")
643         bzrwt.add("registry/generic.c", "origid")
644         revid1 = bzrwt.commit("Add initial directory + file")
645         bzrwt.rename_one("registry", "registry.moved")
646         revid2 = bzrwt.commit("Rename")
647         newdir = BzrDir.open(repos_url+"/trunk")
648         newbranch = newdir.import_branch(bzrwt.branch)
649         def check(b):
650             self.assertEquals([revid1, revid2], b.revision_history())
651             tree = b.repository.revision_tree(revid2)
652             self.assertEquals("origid", tree.path2id("registry.moved/generic.c"))
653             self.assertEquals("dirid", tree.path2id("registry.moved"))
654         check(newbranch)
655         os.mkdir("n")
656         BzrDir.open(repos_url+"/trunk").sprout("n")
657         copybranch = Branch.open("n")
658         check(copybranch)
659
660     def test_push_non_lhs_parent(self):        
661         from bzrlib.debug import debug_flags
662         debug_flags.add("commit")
663         debug_flags.add("fetch")
664         repos_url = self.make_client("a", "dc")
665         bzrwt = BzrDir.create_standalone_workingtree("c", 
666             format=format.get_rich_root_format())
667         self.build_tree({'c/registry/generic.c': "Tour"})
668         bzrwt.add("registry")
669         bzrwt.add("registry/generic.c")
670         revid1 = bzrwt.commit("Add initial directory + file", 
671                               rev_id="initialrevid")
672
673         # Push first branch into Subversion
674         newdir = BzrDir.open(repos_url+"/trunk")
675         mapping = newdir.find_repository().get_mapping()
676         newbranch = newdir.import_branch(bzrwt.branch)
677
678         # Should create dc/trunk
679         self.client_update("dc")
680
681         self.build_tree({'dc/branches': None})
682         self.client_add("dc/branches")
683         self.client_copy("dc/trunk", "dc/branches/foo")
684         self.client_commit("dc", "Copy branches")
685         self.client_update("dc")
686
687         self.build_tree({'dc/branches/foo/registry/generic.c': "France"})
688         merge_revno = self.client_commit("dc", "Change copied branch")[0]
689         merge_revid = newdir.find_repository().generate_revision_id(merge_revno, "branches/foo", mapping)
690
691         self.build_tree({'c/registry/generic.c': "de"})
692         revid2 = bzrwt.commit("Change something", rev_id="changerevid")
693
694         # Merge 
695         self.build_tree({'c/registry/generic.c': "France"})
696         bzrwt.add_pending_merge(merge_revid)
697         revid3 = bzrwt.commit("Merge something", rev_id="mergerevid")
698
699         trunk = Branch.open(repos_url + "/branches/foo")
700         trunk.pull(bzrwt.branch)
701
702         self.assertEquals([revid1, revid2, revid3], trunk.revision_history())
703         self.client_update("dc")
704         self.assertEquals(
705                 '1 initialrevid\n2 changerevid\n3 mergerevid\n',
706                 self.client_get_prop("dc/branches/foo", SVN_PROP_BZR_REVISION_ID+"trunk0"))
707
708     def test_complex_replace_dir(self):
709         repos_url = self.make_client("a", "dc")
710         bzrwt = BzrDir.create_standalone_workingtree("c", 
711             format=format.get_rich_root_format())
712         self.build_tree({'c/registry/generic.c': "Tour"})
713         bzrwt.add(["registry"], ["origdir"])
714         bzrwt.add(["registry/generic.c"], ["file"])
715         revid1 = bzrwt.commit("Add initial directory + file")
716
717         bzrwt.remove('registry/generic.c')
718         bzrwt.remove('registry')
719         bzrwt.add(["registry"], ["newdir"])
720         bzrwt.add(["registry/generic.c"], ["file"])
721         revid2 = bzrwt.commit("Do some funky things")
722
723         newdir = BzrDir.open(repos_url+"/trunk")
724         newbranch = newdir.import_branch(bzrwt.branch)
725         self.assertEquals(revid2, newbranch.last_revision())
726         self.assertEquals([revid1, revid2], newbranch.revision_history())
727
728         os.mkdir("n")
729         BzrDir.open(repos_url+"/trunk").sprout("n")
730
731     def test_push_unnecessary_merge(self):        
732         from bzrlib.debug import debug_flags
733         debug_flags.add("commit")
734         debug_flags.add("fetch")
735         repos_url = self.make_client("a", "dc")
736         bzrwt = BzrDir.create_standalone_workingtree("c", 
737             format=format.get_rich_root_format())
738         self.build_tree({'c/registry/generic.c': "Tour"})
739         bzrwt.add("registry")
740         bzrwt.add("registry/generic.c")
741         revid1 = bzrwt.commit("Add initial directory + file", 
742                               rev_id="initialrevid")
743
744         # Push first branch into Subversion
745         newdir = BzrDir.open(repos_url+"/trunk")
746         newbranch = newdir.import_branch(bzrwt.branch)
747
748         # Should create dc/trunk
749         self.client_update("dc")
750
751         self.assertTrue(os.path.exists("dc/trunk/registry/generic.c"))
752         sleep(1) # Subversion relies on timestamps to detect 
753                  # changed files...
754         self.build_tree({'dc/trunk/registry/generic.c': "BLA"})
755         self.client_commit("dc/trunk", "Change copied branch")
756         self.client_update("dc")
757         mapping = newdir.find_repository().get_mapping()
758         merge_revid = newdir.find_repository().generate_revision_id(2, "trunk", mapping)
759
760         # Merge 
761         self.build_tree({'c/registry/generic.c': "DE"})
762         bzrwt.add_pending_merge(merge_revid)
763         self.assertEquals(bzrwt.get_parent_ids()[1], merge_revid)
764         revid2 = bzrwt.commit("Merge something", rev_id="mergerevid")
765         bzr_parents = bzrwt.branch.repository.get_revision(revid2).parent_ids
766         trunk = Branch.open(repos_url + "/trunk")
767         trunk.pull(bzrwt.branch)
768
769         self.assertEquals(tuple(bzr_parents), 
770                 trunk.repository.get_revision(revid2).parent_ids)
771
772         self.assertEquals([revid1, revid2], trunk.revision_history())
773         self.client_update("dc")
774         self.assertEquals(
775                 '1 initialrevid\n2 mergerevid\n',
776                 self.client_get_prop("dc/trunk", SVN_PROP_BZR_REVISION_ID+"trunk0"))
777
778