/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 breezy/git/tests/test_branch.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

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