/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

  • Committer: Jelmer Vernooij
  • Author(s): Chadrik
  • Date: 2010-07-26 14:02:05 UTC
  • mto: (0.312.1 master) (6883.23.1 bundle-git)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100726140205-m45dyrhi1ifg0fck
Fix 'bzr status' after 'bzr add' in native git working trees.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
17
 
 
18
"""Tests for interfacing with a Git Branch"""
 
19
 
 
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
    )
 
42
 
 
43
from bzrlib.plugins.git import (
 
44
    LocalGitBzrDirFormat,
 
45
    branch,
 
46
    tests,
 
47
    )
 
48
from bzrlib.plugins.git.mapping import (
 
49
    default_mapping,
 
50
    )
 
51
 
 
52
 
 
53
class TestGitBranch(tests.TestCaseInTempDir):
 
54
 
 
55
    _test_needs_features = [tests.GitCommandFeature]
 
56
 
 
57
    def test_open_existing(self):
 
58
        GitRepo.init('.')
 
59
        d = BzrDir.open('.')
 
60
        thebranch = d.create_branch()
 
61
        self.assertIsInstance(thebranch, branch.GitBranch)
 
62
 
 
63
    def test_repr(self):
 
64
        GitRepo.init('.')
 
65
        d = BzrDir.open('.')
 
66
        thebranch = d.create_branch()
 
67
        self.assertEquals("<LocalGitBranch('file://%s/', 'HEAD')>" % self.test_dir, repr(thebranch))
 
68
 
 
69
    def test_last_revision_is_null(self):
 
70
        GitRepo.init('.')
 
71
        thedir = BzrDir.open('.')
 
72
        thebranch = thedir.create_branch()
 
73
        self.assertEqual(revision.NULL_REVISION, thebranch.last_revision())
 
74
        self.assertEqual((0, revision.NULL_REVISION),
 
75
                         thebranch.last_revision_info())
 
76
 
 
77
    def simple_commit_a(self):
 
78
        GitRepo.init('.')
 
79
        self.build_tree(['a'])
 
80
        tests.run_git('add', 'a')
 
81
        tests.run_git('commit', '-m', 'a')
 
82
 
 
83
    def test_last_revision_is_valid(self):
 
84
        self.simple_commit_a()
 
85
        head = tests.run_git('rev-parse', 'HEAD').strip()
 
86
        thebranch = Branch.open('.')
 
87
        self.assertEqual(default_mapping.revision_id_foreign_to_bzr(head),
 
88
                         thebranch.last_revision())
 
89
 
 
90
    def test_revision_history(self):
 
91
        self.simple_commit_a()
 
92
        reva = tests.run_git('rev-parse', 'HEAD').strip()
 
93
        self.build_tree(['b'])
 
94
        tests.run_git('add', 'b')
 
95
        tests.run_git('commit', '-m', 'b')
 
96
        revb = tests.run_git('rev-parse', 'HEAD').strip()
 
97
 
 
98
        thebranch = Branch.open('.')
 
99
        self.assertEqual([default_mapping.revision_id_foreign_to_bzr(r) for r in (reva, revb)],
 
100
                         thebranch.revision_history())
 
101
 
 
102
    def test_tag_annotated(self):
 
103
        self.simple_commit_a()
 
104
        reva = tests.run_git('rev-parse', 'HEAD').strip()
 
105
        tests.run_git('tag', '-a', '-m', 'add tag', 'foo')
 
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
 
 
118
 
 
119
 
 
120
class TestWithGitBranch(tests.TestCaseWithTransport):
 
121
 
 
122
    def setUp(self):
 
123
        tests.TestCaseWithTransport.setUp(self)
 
124
        dulwich.repo.Repo.create(self.test_dir)
 
125
        d = BzrDir.open(self.test_dir)
 
126
        self.git_branch = d.create_branch()
 
127
 
 
128
    def test_get_parent(self):
 
129
        self.assertIs(None, self.git_branch.get_parent())
 
130
 
 
131
    def test_get_stacked_on_url(self):
 
132
        self.assertRaises(errors.UnstackableBranchFormat,
 
133
            self.git_branch.get_stacked_on_url)
 
134
 
 
135
    def test_get_physical_lock_status(self):
 
136
        self.assertFalse(self.git_branch.get_physical_lock_status())
 
137
 
 
138
 
 
139
class TestGitBranchFormat(tests.TestCase):
 
140
 
 
141
    def setUp(self):
 
142
        super(TestGitBranchFormat, self).setUp()
 
143
        self.format = branch.GitBranchFormat()
 
144
 
 
145
    def test_get_format_description(self):
 
146
        self.assertEquals("Git Branch", self.format.get_format_description())
 
147
 
 
148
    def test_get_network_name(self):
 
149
        self.assertEquals("git", self.format.network_name())
 
150
 
 
151
    def test_supports_tags(self):
 
152
        self.assertTrue(self.format.supports_tags())
 
153
 
 
154
 
 
155
class BranchTests(tests.TestCaseInTempDir):
 
156
 
 
157
    def make_onerev_branch(self):
 
158
        os.mkdir("d")
 
159
        os.chdir("d")
 
160
        GitRepo.init('.')
 
161
        bb = tests.GitBranchBuilder()
 
162
        bb.set_file("foobar", "foo\nbar\n", False)
 
163
        mark = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
164
        gitsha = bb.finish()[mark]
 
165
        os.chdir("..")
 
166
        return "d", gitsha
 
167
 
 
168
    def make_tworev_branch(self):
 
169
        os.mkdir("d")
 
170
        os.chdir("d")
 
171
        GitRepo.init('.')
 
172
        bb = tests.GitBranchBuilder()
 
173
        bb.set_file("foobar", "foo\nbar\n", False)
 
174
        mark1 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
175
        mark2 = bb.commit("Somebody <somebody@someorg.org>", "mymsg")
 
176
        marks = bb.finish()
 
177
        os.chdir("..")
 
178
        return "d", (marks[mark1], marks[mark2])
 
179
 
 
180
    def clone_git_branch(self, from_url, to_url):
 
181
        from_dir = BzrDir.open(from_url)
 
182
        to_dir = from_dir.sprout(to_url)
 
183
        return to_dir.open_branch()
 
184
 
 
185
    def test_single_rev(self):
 
186
        path, gitsha = self.make_onerev_branch()
 
187
        oldrepo = Repository.open(path)
 
188
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
189
        newbranch = self.clone_git_branch(path, "f")
 
190
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
191
 
 
192
    def test_sprouted_tags(self):
 
193
        path, gitsha = self.make_onerev_branch()
 
194
        os.chdir(path)
 
195
        tests.run_git("tag", "lala")
 
196
        os.chdir(self.test_dir)
 
197
        oldrepo = Repository.open(path)
 
198
        revid = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha)
 
199
        newbranch = self.clone_git_branch(path, "f")
 
200
        self.assertEquals({"lala": revid}, newbranch.tags.get_tag_dict())
 
201
        self.assertEquals([revid], newbranch.repository.all_revision_ids())
 
202
 
 
203
    def test_interbranch_pull(self):
 
204
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
205
        oldrepo = Repository.open(path)
 
206
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
207
        newbranch = self.make_branch('g')
 
208
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
209
        inter_branch.pull()
 
210
        self.assertEquals(revid2, newbranch.last_revision())
 
211
 
 
212
    def test_interbranch_pull_noop(self):
 
213
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
214
        oldrepo = Repository.open(path)
 
215
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
216
        newbranch = self.make_branch('g')
 
217
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
218
        inter_branch.pull()
 
219
        # This is basically "assertNotRaises"
 
220
        inter_branch.pull()
 
221
        self.assertEquals(revid2, newbranch.last_revision())
 
222
 
 
223
    def test_interbranch_pull_stop_revision(self):
 
224
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
225
        oldrepo = Repository.open(path)
 
226
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
227
        newbranch = self.make_branch('g')
 
228
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
229
        inter_branch.pull(stop_revision=revid1)
 
230
        self.assertEquals(revid1, newbranch.last_revision())
 
231
 
 
232
    def test_interbranch_limited_pull(self):
 
233
        path, (gitsha1, gitsha2) = self.make_tworev_branch()
 
234
        oldrepo = Repository.open(path)
 
235
        revid1 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha1)
 
236
        revid2 = oldrepo.get_mapping().revision_id_foreign_to_bzr(gitsha2)
 
237
        newbranch = self.make_branch('g')
 
238
        inter_branch = InterBranch.get(Branch.open(path), newbranch)
 
239
        inter_branch.pull(limit=1)
 
240
        self.assertEquals(revid1, newbranch.last_revision())
 
241
        inter_branch.pull(limit=1)
 
242
        self.assertEquals(revid2, newbranch.last_revision())
 
243
 
 
244
 
 
245
class ForeignTestsBranchFactory(object):
 
246
 
 
247
    def make_empty_branch(self, transport):
 
248
        d = LocalGitBzrDirFormat().initialize_on_transport(transport)
 
249
        return d.create_branch()
 
250
 
 
251
    make_branch = make_empty_branch
 
252
 
 
253
 
 
254