/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

Don't fetch revision already present.

Show diffs side-by-side

added added

removed removed

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