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
17
18
"""Tests for interfacing with a Git Branch"""
21
from bzrlib import branch, revision
23
from bzrlib.plugins.git import tests
27
from bzrlib.branch import (
30
from bzrlib.bzrdir import (
33
from bzrlib.repository import (
24
37
from bzrlib.plugins.git import (
41
from bzrlib.plugins.git.mapping import default_mapping
30
44
class TestGitBranch(tests.TestCaseInTempDir):
34
48
def test_open_existing(self):
35
49
tests.run_git('init')
37
thebranch = branch.Branch.open('.')
38
self.assertIsInstance(thebranch, git_branch.GitBranch)
51
thebranch = Branch.open('.')
52
self.assertIsInstance(thebranch, branch.GitBranch)
56
thebranch = Branch.open('.')
57
self.assertEquals("LocalGitBranch('file://%s/', 'HEAD')" % self.test_dir, repr(thebranch))
40
59
def test_last_revision_is_null(self):
41
60
tests.run_git('init')
43
thebranch = branch.Branch.open('.')
62
thebranch = Branch.open('.')
44
63
self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
45
64
self.assertEqual((0, revision.NULL_REVISION),
46
65
thebranch.last_revision_info())
67
def simple_commit_a(self):
69
self.build_tree(['a'])
70
tests.run_git('add', 'a')
71
tests.run_git('commit', '-m', 'a')
48
73
def test_last_revision_is_valid(self):
50
self.build_tree(['a'])
51
tests.run_git('add', 'a')
52
tests.run_git('commit', '-m', 'a')
74
self.simple_commit_a()
53
75
head = tests.run_git('rev-parse', 'HEAD').strip()
55
thebranch = branch.Branch.open('.')
56
self.assertEqual(ids.convert_revision_id_git_to_bzr(head),
77
thebranch = Branch.open('.')
78
self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
57
79
thebranch.last_revision())
59
81
def test_revision_history(self):
61
self.build_tree(['a'])
62
tests.run_git('add', 'a')
63
tests.run_git('commit', '-m', 'a')
82
self.simple_commit_a()
64
83
reva = tests.run_git('rev-parse', 'HEAD').strip()
65
84
self.build_tree(['b'])
66
85
tests.run_git('add', 'b')
67
86
tests.run_git('commit', '-m', 'b')
68
87
revb = tests.run_git('rev-parse', 'HEAD').strip()
70
thebranch = branch.Branch.open('.')
71
self.assertEqual([ids.convert_revision_id_git_to_bzr(r) for r in (reva, revb)],
89
thebranch = Branch.open('.')
90
self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
72
91
thebranch.revision_history())
93
def test_tag_annotated(self):
94
self.simple_commit_a()
95
reva = tests.run_git('rev-parse', 'HEAD').strip()
96
tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
97
thebranch = Branch.open('.')
98
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
99
thebranch.tags.get_tag_dict())
102
self.simple_commit_a()
103
reva = tests.run_git('rev-parse', 'HEAD').strip()
104
tests.run_git('tag', '-m', 'add tag', 'foo')
105
thebranch = Branch.open('.')
106
self.assertEquals({"foo": default_mapping.revision_id_foreign_to_bzr(reva)},
107
thebranch.tags.get_tag_dict())
75
111
class TestWithGitBranch(tests.TestCaseWithTransport):
78
114
tests.TestCaseWithTransport.setUp(self)
79
git.repo.Repo.create(self.test_dir)
80
self.git_branch = branch.Branch.open(self.test_dir)
115
dulwich.repo.Repo.create(self.test_dir)
116
self.git_branch = Branch.open(self.test_dir)
82
118
def test_get_parent(self):
83
119
self.assertIs(None, self.git_branch.get_parent())
95
131
super(TestGitBranchFormat, self).setUp()
96
self.format = git_branch.GitBranchFormat()
132
self.format = branch.GitBranchFormat()
98
134
def test_get_format_description(self):
99
135
self.assertEquals("Git Branch", self.format.get_format_description())
139
class BranchTests(tests.TestCaseInTempDir):
141
def make_onerev_branch(self):
144
tests.run_git("init")
145
bb = tests.GitBranchBuilder()
146
bb.set_file("foobar", "foo\nbar\n", False)
147
mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
148
gitsha = bb.finish()[mark]
152
def clone_git_branch(self, from_url, to_url):
153
from_dir = BzrDir.open(from_url)
154
to_dir = from_dir.sprout(to_url)
155
return to_dir.open_branch()
157
def test_single_rev(self):
158
path, gitsha = self.make_onerev_branch()
159
oldrepo = Repository.open(path)
160
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
161
newbranch = self.clone_git_branch(path, "f")
162
self.assertEquals([revid], newbranch.repository.all_revision_ids())
164
def test_sprouted_tags(self):
165
path, gitsha = self.make_onerev_branch()
167
tests.run_git("tag", "lala")
168
os.chdir(self.test_dir)
169
oldrepo = Repository.open(path)
170
revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
171
newbranch = self.clone_git_branch(path, "f")
172
self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
173
self.assertEquals([revid], newbranch.repository.all_revision_ids())