/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 claim control directories can be accessed directly, always open the
branch itself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
 
17
18
"""Tests for interfacing with a Git Branch"""
18
19
 
19
 
from bzrlib import revision
20
 
from bzrlib.branch import Branch
 
20
 
 
21
import dulwich
 
22
from dulwich.repo import (
 
23
    Repo as GitRepo,
 
24
    )
 
25
 
 
26
import os
 
27
 
 
28
from bzrlib import (
 
29
    errors,
 
30
    revision,
 
31
    )
 
32
from bzrlib.branch import (
 
33
    Branch,
 
34
    InterBranch,
 
35
    )
 
36
from bzrlib.bzrdir import (
 
37
    BzrDir,
 
38
    )
 
39
from bzrlib.repository import (
 
40
    Repository,
 
41
    )
21
42
 
22
43
from bzrlib.plugins.git import (
 
44
    LocalGitBzrDirFormat,
23
45
    branch,
24
46
    tests,
25
47
    )
26
 
from bzrlib.plugins.git.mapping import default_mapping
27
 
from bzrlib.plugins.git import git
28
 
 
 
48
from bzrlib.plugins.git.mapping import (
 
49
    default_mapping,
 
50
    )
29
51
 
30
52
 
31
53
class TestGitBranch(tests.TestCaseInTempDir):
33
55
    _test_needs_features = [tests.GitCommandFeature]
34
56
 
35
57
    def test_open_existing(self):
36
 
        tests.run_git('init')
 
58
        GitRepo.init('.')
37
59
 
38
60
        thebranch = Branch.open('.')
39
61
        self.assertIsInstance(thebranch, branch.GitBranch)
40
62
 
 
63
    def test_repr(self):
 
64
        GitRepo.init('.')
 
65
        thebranch = Branch.open('.')
 
66
        self.assertEquals("LocalGitBranch('file://%s/', 'HEAD')" % self.test_dir, repr(thebranch))
 
67
 
41
68
    def test_last_revision_is_null(self):
42
 
        tests.run_git('init')
 
69
        GitRepo.init('.')
43
70
 
44
71
        thebranch = Branch.open('.')
45
72
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
47
74
                         thebranch.last_revision_info())
48
75
 
49
76
    def simple_commit_a(self):
50
 
        tests.run_git('init')
 
77
        GitRepo.init('.')
51
78
        self.build_tree(['a'])
52
79
        tests.run_git('add', 'a')
53
80
        tests.run_git('commit', '-m', 'a')
72
99
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
73
100
                         thebranch.revision_history())
74
101
 
75
 
    def test_tags(self):
 
102
    def test_tag_annotated(self):
76
103
        self.simple_commit_a()
77
104
        reva = tests.run_git('rev-parse', 'HEAD').strip()
78
 
        
79
105
        tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
80
 
        
81
 
        newid = open('.git/refs/tags/foo').read().rstrip()
82
 
 
83
 
        thebranch = Branch.open('.')
84
 
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(newid)},
85
 
                          thebranch.tags.get_tag_dict())
 
106
        thebranch = Branch.open('.')
 
107
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
108
                          thebranch.tags.get_tag_dict())
 
109
 
 
110
    def test_tag(self):
 
111
        self.simple_commit_a()
 
112
        reva = tests.run_git('rev-parse', 'HEAD').strip()
 
113
        tests.run_git('tag', '-m', 'add tag', 'foo')
 
114
        thebranch = Branch.open('.')
 
115
        self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
 
116
                          thebranch.tags.get_tag_dict())
 
117
 
86
118
        
87
119
 
88
120
class TestWithGitBranch(tests.TestCaseWithTransport):
89
121
 
90
122
    def setUp(self):
91
123
        tests.TestCaseWithTransport.setUp(self)
92
 
        git.repo.Repo.create(self.test_dir)
 
124
        dulwich.repo.Repo.create(self.test_dir)
93
125
        self.git_branch = Branch.open(self.test_dir)
94
126
 
95
127
    def test_get_parent(self):
96
128
        self.assertIs(None, self.git_branch.get_parent())
97
129
 
98
130
    def test_get_stacked_on_url(self):
99
 
        self.assertIs(None, self.git_branch.get_stacked_on_url())
 
131
        self.assertRaises(errors.UnstackableBranchFormat, 
 
132
            self.git_branch.get_stacked_on_url)
100
133
 
101
134
    def test_get_physical_lock_status(self):
102
135
        self.assertFalse(self.git_branch.get_physical_lock_status())
110
143
 
111
144
    def test_get_format_description(self):
112
145
        self.assertEquals("Git Branch", self.format.get_format_description())
 
146
 
 
147
 
 
148
 
 
149
class BranchTests(tests.TestCaseInTempDir):
 
150
 
 
151
    def make_onerev_branch(self):
 
152
        os.mkdir("d")
 
153
        os.chdir("d")
 
154
        GitRepo.init('.')
 
155
        bb = tests.GitBranchBuilder()
 
156
        bb.set_file("foobar", "foo\nbar\n", False)
 
157
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
158
        gitsha = bb.finish()[mark]
 
159
        os.chdir("..")
 
160
        return "d", gitsha
 
161
 
 
162
    def make_tworev_branch(self):
 
163
        os.mkdir("d")
 
164
        os.chdir("d")
 
165
        GitRepo.init('.')
 
166
        bb = tests.GitBranchBuilder()
 
167
        bb.set_file("foobar", "foo\nbar\n", False)
 
168
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
169
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
170
        marks = bb.finish()
 
171
        os.chdir("..")
 
172
        return "d", (marks[mark1], marks[mark2])
 
173
 
 
174
    def clone_git_branch(self, from_url, to_url):
 
175
        from_dir = BzrDir.open(from_url)
 
176
        to_dir = from_dir.sprout(to_url)
 
177
        return to_dir.open_branch()
 
178
 
 
179
    def test_single_rev(self):
 
180
        path, gitsha = self.make_onerev_branch()
 
181
        oldrepo = Repository.open(path)
 
182
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
183
        newbranch = self.clone_git_branch(path, "f")
 
184
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
185
 
 
186
    def test_sprouted_tags(self):
 
187
        path, gitsha = self.make_onerev_branch()
 
188
        os.chdir(path)
 
189
        tests.run_git("tag", "lala")
 
190
        os.chdir(self.test_dir)
 
191
        oldrepo = Repository.open(path)
 
192
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
193
        newbranch = self.clone_git_branch(path, "f")
 
194
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
 
195
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
196
 
 
197
    def test_interbranch_pull(self):
 
198
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
199
        oldrepo = Repository.open(path)
 
200
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
201
        newbranch = self.make_branch('g')
 
202
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
203
        inter_branch.pull()
 
204
        self.assertEquals(revid2, newbranch.last_revision())
 
205
 
 
206
    def test_interbranch_pull_noop(self):
 
207
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
208
        oldrepo = Repository.open(path)
 
209
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
210
        newbranch = self.make_branch('g')
 
211
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
212
        inter_branch.pull()
 
213
        # This is basically "assertNotRaises"
 
214
        inter_branch.pull()
 
215
        self.assertEquals(revid2, newbranch.last_revision())
 
216
 
 
217
    def test_interbranch_pull_stop_revision(self):
 
218
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
219
        oldrepo = Repository.open(path)
 
220
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
221
        newbranch = self.make_branch('g')
 
222
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
223
        inter_branch.pull(stop_revision=revid1)
 
224
        self.assertEquals(revid1, newbranch.last_revision())
 
225
 
 
226
    def test_interbranch_limited_pull(self):
 
227
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
228
        oldrepo = Repository.open(path)
 
229
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
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(limit=1)
 
234
        self.assertEquals(revid1, newbranch.last_revision())
 
235
        inter_branch.pull(limit=1)
 
236
        self.assertEquals(revid2, newbranch.last_revision())
 
237
 
 
238
 
 
239
class ForeignTestsBranchFactory(object):
 
240
 
 
241
    def make_empty_branch(self, transport):
 
242
        return LocalGitBzrDirFormat().initialize_on_transport(transport).open_branch()
 
243
 
 
244
    make_branch = make_empty_branch