/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

TranslateĀ moreĀ strings.

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