/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_branch.py

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
 
21
21
import dulwich
 
22
from dulwich.objects import (
 
23
    Commit,
 
24
    Tag,
 
25
    )
22
26
from dulwich.repo import (
23
27
    Repo as GitRepo,
24
28
    )
31
35
    )
32
36
from bzrlib.branch import (
33
37
    Branch,
 
38
    InterBranch,
34
39
    )
35
40
from bzrlib.bzrdir import (
36
41
    BzrDir,
40
45
    )
41
46
 
42
47
from bzrlib.plugins.git import (
43
 
    LocalGitBzrDirFormat,
 
48
    LocalGitControlDirFormat,
44
49
    branch,
45
50
    tests,
46
51
    )
51
56
 
52
57
class TestGitBranch(tests.TestCaseInTempDir):
53
58
 
54
 
    _test_needs_features = [tests.GitCommandFeature]
55
 
 
56
59
    def test_open_existing(self):
57
60
        GitRepo.init('.')
58
 
 
59
 
        thebranch = Branch.open('.')
 
61
        d = BzrDir.open('.')
 
62
        thebranch = d.create_branch()
60
63
        self.assertIsInstance(thebranch, branch.GitBranch)
61
64
 
62
65
    def test_repr(self):
63
66
        GitRepo.init('.')
64
 
        thebranch = Branch.open('.')
65
 
        self.assertEquals("LocalGitBranch('file://%s/', 'HEAD')" % self.test_dir, repr(thebranch))
 
67
        d = BzrDir.open('.')
 
68
        thebranch = d.create_branch()
 
69
        self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
66
70
 
67
71
    def test_last_revision_is_null(self):
68
72
        GitRepo.init('.')
69
 
 
70
 
        thebranch = Branch.open('.')
 
73
        thedir = BzrDir.open('.')
 
74
        thebranch = thedir.create_branch()
71
75
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
72
76
        self.assertEqual((0, revision.NULL_REVISION),
73
77
                         thebranch.last_revision_info())
74
78
 
75
79
    def simple_commit_a(self):
76
 
        GitRepo.init('.')
 
80
        r = GitRepo.init('.')
77
81
        self.build_tree(['a'])
78
 
        tests.run_git('add', 'a')
79
 
        tests.run_git('commit', '-m', 'a')
 
82
        r.stage(["a"])
 
83
        return r.do_commit("a", committer="Somebody <foo@example.com>")
80
84
 
81
85
    def test_last_revision_is_valid(self):
82
 
        self.simple_commit_a()
83
 
        head = tests.run_git('rev-parse', 'HEAD').strip()
84
 
 
 
86
        head = self.simple_commit_a()
85
87
        thebranch = Branch.open('.')
86
88
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
87
89
                         thebranch.last_revision())
88
90
 
89
91
    def test_revision_history(self):
90
 
        self.simple_commit_a()
91
 
        reva = tests.run_git('rev-parse', 'HEAD').strip()
 
92
        reva = self.simple_commit_a()
92
93
        self.build_tree(['b'])
93
 
        tests.run_git('add', 'b')
94
 
        tests.run_git('commit', '-m', 'b')
95
 
        revb = tests.run_git('rev-parse', 'HEAD').strip()
 
94
        r = GitRepo(".")
 
95
        r.stage("b")
 
96
        revb = r.do_commit("b", committer="Somebody <foo@example.com>")
96
97
 
97
98
        thebranch = Branch.open('.')
98
99
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
99
100
                         thebranch.revision_history())
100
101
 
101
102
    def test_tag_annotated(self):
102
 
        self.simple_commit_a()
103
 
        reva = tests.run_git('rev-parse', 'HEAD').strip()
104
 
        tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
 
103
        reva = self.simple_commit_a()
 
104
        o = Tag()
 
105
        o.name = "foo"
 
106
        o.tagger = "Jelmer <foo@example.com>"
 
107
        o.message = "add tag"
 
108
        o.object = (Commit, reva)
 
109
        o.tag_timezone = 0
 
110
        o.tag_time = 42
 
111
        r = GitRepo(".")
 
112
        r.object_store.add_object(o)
 
113
        r['refs/tags/foo'] = o.id
105
114
        thebranch = Branch.open('.')
106
115
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
107
116
                          thebranch.tags.get_tag_dict())
108
117
 
109
118
    def test_tag(self):
110
 
        self.simple_commit_a()
111
 
        reva = tests.run_git('rev-parse', 'HEAD').strip()
112
 
        tests.run_git('tag', '-m', 'add tag', 'foo')
 
119
        reva = self.simple_commit_a()
 
120
        r = GitRepo(".")
 
121
        r.refs["refs/tags/foo"] = reva
113
122
        thebranch = Branch.open('.')
114
123
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
115
124
                          thebranch.tags.get_tag_dict())
116
125
 
117
 
        
 
126
 
118
127
 
119
128
class TestWithGitBranch(tests.TestCaseWithTransport):
120
129
 
121
130
    def setUp(self):
122
131
        tests.TestCaseWithTransport.setUp(self)
123
132
        dulwich.repo.Repo.create(self.test_dir)
124
 
        self.git_branch = Branch.open(self.test_dir)
 
133
        d = BzrDir.open(self.test_dir)
 
134
        self.git_branch = d.create_branch()
125
135
 
126
136
    def test_get_parent(self):
127
137
        self.assertIs(None, self.git_branch.get_parent())
128
138
 
129
139
    def test_get_stacked_on_url(self):
130
 
        self.assertRaises(errors.UnstackableBranchFormat, 
 
140
        self.assertRaises(errors.UnstackableBranchFormat,
131
141
            self.git_branch.get_stacked_on_url)
132
142
 
133
143
    def test_get_physical_lock_status(self):
143
153
    def test_get_format_description(self):
144
154
        self.assertEquals("Git Branch", self.format.get_format_description())
145
155
 
 
156
    def test_get_network_name(self):
 
157
        self.assertEquals("git", self.format.network_name())
 
158
 
 
159
    def test_supports_tags(self):
 
160
        self.assertTrue(self.format.supports_tags())
146
161
 
147
162
 
148
163
class BranchTests(tests.TestCaseInTempDir):
158
173
        os.chdir("..")
159
174
        return "d", gitsha
160
175
 
 
176
    def make_tworev_branch(self):
 
177
        os.mkdir("d")
 
178
        os.chdir("d")
 
179
        GitRepo.init('.')
 
180
        bb = tests.GitBranchBuilder()
 
181
        bb.set_file("foobar", "foo\nbar\n", False)
 
182
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
183
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
184
        marks = bb.finish()
 
185
        os.chdir("..")
 
186
        return "d", (marks[mark1], marks[mark2])
 
187
 
161
188
    def clone_git_branch(self, from_url, to_url):
162
189
        from_dir = BzrDir.open(from_url)
163
190
        to_dir = from_dir.sprout(to_url)
172
199
 
173
200
    def test_sprouted_tags(self):
174
201
        path, gitsha = self.make_onerev_branch()
175
 
        os.chdir(path)
176
 
        tests.run_git("tag", "lala")
177
 
        os.chdir(self.test_dir)
 
202
        r = GitRepo(path)
 
203
        r.refs["refs/tags/lala"] = r.head()
178
204
        oldrepo = Repository.open(path)
179
205
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
180
206
        newbranch = self.clone_git_branch(path, "f")
181
207
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
182
208
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
183
209
 
 
210
    def test_interbranch_pull(self):
 
211
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
212
        oldrepo = Repository.open(path)
 
213
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
214
        newbranch = self.make_branch('g')
 
215
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
216
        inter_branch.pull()
 
217
        self.assertEquals(revid2, newbranch.last_revision())
 
218
 
 
219
    def test_interbranch_pull_noop(self):
 
220
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
221
        oldrepo = Repository.open(path)
 
222
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
223
        newbranch = self.make_branch('g')
 
224
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
225
        inter_branch.pull()
 
226
        # This is basically "assertNotRaises"
 
227
        inter_branch.pull()
 
228
        self.assertEquals(revid2, newbranch.last_revision())
 
229
 
 
230
    def test_interbranch_pull_stop_revision(self):
 
231
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
232
        oldrepo = Repository.open(path)
 
233
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
234
        newbranch = self.make_branch('g')
 
235
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
236
        inter_branch.pull(stop_revision=revid1)
 
237
        self.assertEquals(revid1, newbranch.last_revision())
 
238
 
 
239
    def test_interbranch_limited_pull(self):
 
240
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
241
        oldrepo = Repository.open(path)
 
242
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
243
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
244
        newbranch = self.make_branch('g')
 
245
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
246
        inter_branch.pull(limit=1)
 
247
        self.assertEquals(revid1, newbranch.last_revision())
 
248
        inter_branch.pull(limit=1)
 
249
        self.assertEquals(revid2, newbranch.last_revision())
 
250
 
184
251
 
185
252
class ForeignTestsBranchFactory(object):
186
253
 
187
254
    def make_empty_branch(self, transport):
188
 
        return LocalGitBzrDirFormat().initialize_on_transport(transport).open_branch()
 
255
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
 
256
        return d.create_branch()
189
257
 
190
258
    make_branch = make_empty_branch
 
259
 
 
260
 
 
261