/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
17
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
18
"""Tests for interfacing with a Git Branch"""
19
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
20
21
import dulwich
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
22
from dulwich.objects import (
23
    Commit,
24
    Tag,
25
    )
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
26
from dulwich.repo import (
27
    Repo as GitRepo,
28
    )
29
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
30
import os
31
32
from bzrlib import (
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
33
    errors,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
34
    revision,
35
    )
36
from bzrlib.branch import (
37
    Branch,
0.247.1 by Michael Hudson
test
38
    InterBranch,
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
39
    )
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
40
try:
41
    from bzrlib.controldir import (
42
        ControlDir,
43
        )
44
except ImportError:
45
    # bzr < 2.3
46
    from bzrlib.bzrdir import (
47
        BzrDir,
48
        )
49
    ControlDir = BzrDir
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
50
from bzrlib.repository import (
51
    Repository,
52
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
53
0.200.27 by David Allouche
Flat is better than nested, remove the gitlib hierarchy.
54
from bzrlib.plugins.git import (
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
55
    LocalGitControlDirFormat,
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
56
    branch,
0.200.97 by Jelmer Vernooij
use mapping object.
57
    tests,
0.200.20 by John Arbash Meinel
All tests are passing again
58
    )
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
59
from bzrlib.plugins.git.mapping import (
60
    default_mapping,
61
    )
0.200.123 by Jelmer Vernooij
Use central git module.
62
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
63
64
class TestGitBranch(tests.TestCaseInTempDir):
65
66
    def test_open_existing(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
67
        GitRepo.init('.')
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
68
        d = ControlDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
69
        thebranch = d.create_branch()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
70
        self.assertIsInstance(thebranch, branch.GitBranch)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
71
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
72
    def test_repr(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
73
        GitRepo.init('.')
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
74
        d = ControlDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
75
        thebranch = d.create_branch()
0.200.920 by Jelmer Vernooij
Fix some more tests.
76
        self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
77
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
78
    def test_last_revision_is_null(self):
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
79
        GitRepo.init('.')
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
80
        thedir = ControlDir.open('.')
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
81
        thebranch = thedir.create_branch()
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
82
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
83
        self.assertEqual((0, revision.NULL_REVISION),
84
                         thebranch.last_revision_info())
0.200.20 by John Arbash Meinel
All tests are passing again
85
0.200.82 by Jelmer Vernooij
Support listing tags.
86
    def simple_commit_a(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
87
        r = GitRepo.init('.')
0.200.82 by Jelmer Vernooij
Support listing tags.
88
        self.build_tree(['a'])
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
89
        r.stage(["a"])
90
        return r.do_commit("a", committer="Somebody <foo@example.com>")
0.200.82 by Jelmer Vernooij
Support listing tags.
91
0.200.20 by John Arbash Meinel
All tests are passing again
92
    def test_last_revision_is_valid(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
93
        head = self.simple_commit_a()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
94
        thebranch = Branch.open('.')
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
95
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
0.200.20 by John Arbash Meinel
All tests are passing again
96
                         thebranch.last_revision())
0.200.58 by Jelmer Vernooij
Fix remaining tests.
97
98
    def test_revision_history(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
99
        reva = self.simple_commit_a()
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
100
        self.build_tree(['b'])
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
101
        r = GitRepo(".")
102
        r.stage("b")
103
        revb = r.do_commit("b", committer="Somebody <foo@example.com>")
0.200.58 by Jelmer Vernooij
Fix remaining tests.
104
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
105
        thebranch = Branch.open('.')
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
106
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
0.200.58 by Jelmer Vernooij
Fix remaining tests.
107
                         thebranch.revision_history())
0.200.82 by Jelmer Vernooij
Support listing tags.
108
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
109
    def test_tag_annotated(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
110
        reva = self.simple_commit_a()
111
        o = Tag()
112
        o.name = "foo"
113
        o.tagger = "Jelmer <foo@example.com>"
114
        o.message = "add tag"
115
        o.object = (Commit, reva)
116
        o.tag_timezone = 0
117
        o.tag_time = 42
118
        r = GitRepo(".")
119
        r.object_store.add_object(o)
120
        r['refs/tags/foo'] = o.id
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
121
        thebranch = Branch.open('.')
122
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
123
                          thebranch.tags.get_tag_dict())
124
125
    def test_tag(self):
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
126
        reva = self.simple_commit_a()
127
        r = GitRepo(".")
128
        r.refs["refs/tags/foo"] = reva
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
129
        thebranch = Branch.open('.')
130
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
131
                          thebranch.tags.get_tag_dict())
132
0.200.956 by Jelmer Vernooij
Add some more format tests.
133
0.200.66 by Jelmer Vernooij
Implement branch.get_parent().
134
135
class TestWithGitBranch(tests.TestCaseWithTransport):
136
137
    def setUp(self):
138
        tests.TestCaseWithTransport.setUp(self)
0.200.411 by Jelmer Vernooij
Stop pretending dulwich has the same api as python-git.
139
        dulwich.repo.Repo.create(self.test_dir)
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
140
        d = ControlDir.open(self.test_dir)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
141
        self.git_branch = d.create_branch()
0.200.66 by Jelmer Vernooij
Implement branch.get_parent().
142
143
    def test_get_parent(self):
144
        self.assertIs(None, self.git_branch.get_parent())
0.200.67 by Jelmer Vernooij
Implement Branch.get_stacked_on_url.
145
146
    def test_get_stacked_on_url(self):
0.200.956 by Jelmer Vernooij
Add some more format tests.
147
        self.assertRaises(errors.UnstackableBranchFormat,
0.200.633 by Jelmer Vernooij
Fix Branch.get_stacked_on_url() test.
148
            self.git_branch.get_stacked_on_url)
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
149
0.200.72 by Jelmer Vernooij
Implement Branch.get_physical_lock_status.
150
    def test_get_physical_lock_status(self):
151
        self.assertFalse(self.git_branch.get_physical_lock_status())
152
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
153
154
class TestGitBranchFormat(tests.TestCase):
155
156
    def setUp(self):
157
        super(TestGitBranchFormat, self).setUp()
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
158
        self.format = branch.GitBranchFormat()
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
159
160
    def test_get_format_description(self):
161
        self.assertEquals("Git Branch", self.format.get_format_description())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
162
0.200.956 by Jelmer Vernooij
Add some more format tests.
163
    def test_get_network_name(self):
164
        self.assertEquals("git", self.format.network_name())
165
166
    def test_supports_tags(self):
167
        self.assertTrue(self.format.supports_tags())
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
168
169
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
170
class BranchTests(tests.TestCaseInTempDir):
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
171
172
    def make_onerev_branch(self):
173
        os.mkdir("d")
174
        os.chdir("d")
0.200.447 by Jelmer Vernooij
Rely less on command-line git.
175
        GitRepo.init('.')
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
176
        bb = tests.GitBranchBuilder()
177
        bb.set_file("foobar", "foo\nbar\n", False)
178
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
179
        gitsha = bb.finish()[mark]
180
        os.chdir("..")
181
        return "d", gitsha
182
0.247.1 by Michael Hudson
test
183
    def make_tworev_branch(self):
184
        os.mkdir("d")
185
        os.chdir("d")
186
        GitRepo.init('.')
187
        bb = tests.GitBranchBuilder()
188
        bb.set_file("foobar", "foo\nbar\n", False)
189
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
190
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
191
        marks = bb.finish()
192
        os.chdir("..")
193
        return "d", (marks[mark1], marks[mark2])
194
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
195
    def clone_git_branch(self, from_url, to_url):
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
196
        from_dir = ControlDir.open(from_url)
0.200.270 by Jelmer Vernooij
add tests for branch sprouting.
197
        to_dir = from_dir.sprout(to_url)
198
        return to_dir.open_branch()
199
200
    def test_single_rev(self):
201
        path, gitsha = self.make_onerev_branch()
202
        oldrepo = Repository.open(path)
203
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
204
        newbranch = self.clone_git_branch(path, "f")
205
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
206
207
    def test_sprouted_tags(self):
208
        path, gitsha = self.make_onerev_branch()
0.200.992 by Jelmer Vernooij
Avoid invoking git directly.
209
        r = GitRepo(path)
210
        r.refs["refs/tags/lala"] = r.head()
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
211
        oldrepo = Repository.open(path)
212
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
213
        newbranch = self.clone_git_branch(path, "f")
214
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
215
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
216
0.247.1 by Michael Hudson
test
217
    def test_interbranch_pull(self):
218
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
219
        oldrepo = Repository.open(path)
220
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
221
        newbranch = self.make_branch('g')
222
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
223
        inter_branch.pull()
224
        self.assertEquals(revid2, newbranch.last_revision())
225
0.247.5 by Michael Hudson
test and fix for noop pull case
226
    def test_interbranch_pull_noop(self):
227
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
228
        oldrepo = Repository.open(path)
229
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
230
        newbranch = self.make_branch('g')
231
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
232
        inter_branch.pull()
233
        # This is basically "assertNotRaises"
234
        inter_branch.pull()
235
        self.assertEquals(revid2, newbranch.last_revision())
236
0.247.3 by Michael Hudson
oh, so it wasn't (particularly) wrong, but it was a bit obscure
237
    def test_interbranch_pull_stop_revision(self):
238
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
239
        oldrepo = Repository.open(path)
240
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
241
        newbranch = self.make_branch('g')
242
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
243
        inter_branch.pull(stop_revision=revid1)
244
        self.assertEquals(revid1, newbranch.last_revision())
245
0.247.1 by Michael Hudson
test
246
    def test_interbranch_limited_pull(self):
247
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
248
        oldrepo = Repository.open(path)
249
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
250
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
251
        newbranch = self.make_branch('g')
252
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
253
        inter_branch.pull(limit=1)
254
        self.assertEquals(revid1, newbranch.last_revision())
255
        inter_branch.pull(limit=1)
256
        self.assertEquals(revid2, newbranch.last_revision())
257
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
258
259
class ForeignTestsBranchFactory(object):
260
261
    def make_empty_branch(self, transport):
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
262
        d = LocalGitControlDirFormat().initialize_on_transport(transport)
0.200.769 by Jelmer Vernooij
Cope with open_branch() actually checking whether there is a branch present.
263
        return d.create_branch()
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
264
265
    make_branch = make_empty_branch
0.200.770 by Jelmer Vernooij
Proper branch names.
266
267
0.200.833 by Jelmer Vernooij
Fix handling of remote branches.
268