~ubuntu-branches/debian/experimental/bzr-git/experimental

« back to all changes in this revision

Viewing changes to tests/test_branch.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2012-09-07 02:11:31 UTC
  • mfrom: (4.1.27 sid)
  • Revision ID: package-import@ubuntu.com-20120907021131-3wodhoeuphw57tgr
Tags: 0.6.9-2
* Orphan package.
* Add XS-Testsuite header. Closes: #692655

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