/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 breezy/git/tests/test_blackbox.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-19 15:14:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7455.
  • Revision ID: jelmer@jelmer.uk-20200119151416-f2x9y9rtvwxndr2l
Don't show submodules that are not checked out as deltas.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 David Allouche <ddaa@ddaa.net>
 
2
# Copyright (C) 2007-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""Black-box tests for bzr-git."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from dulwich.repo import (
 
23
    Repo as GitRepo,
 
24
    )
 
25
 
 
26
import os
 
27
 
 
28
from ...controldir import (
 
29
    ControlDir,
 
30
    )
 
31
 
 
32
from ...tests.blackbox import ExternalBase
 
33
from ...workingtree import WorkingTree
 
34
 
 
35
from .. import (
 
36
    tests,
 
37
    )
 
38
from ...tests.script import TestCaseWithTransportAndScript
 
39
from ...tests.features import PluginLoadedFeature
 
40
 
 
41
 
 
42
class TestGitBlackBox(ExternalBase):
 
43
 
 
44
    def simple_commit(self):
 
45
        # Create a git repository with a revision.
 
46
        repo = GitRepo.init(self.test_dir)
 
47
        builder = tests.GitBranchBuilder()
 
48
        builder.set_file('a', b'text for a\n', False)
 
49
        r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
50
        return repo, builder.finish()[r1]
 
51
 
 
52
    def test_nick(self):
 
53
        r = GitRepo.init(self.test_dir)
 
54
        dir = ControlDir.open(self.test_dir)
 
55
        dir.create_branch()
 
56
        output, error = self.run_bzr(['nick'])
 
57
        self.assertEqual("master\n", output)
 
58
 
 
59
    def test_branches(self):
 
60
        self.simple_commit()
 
61
        output, error = self.run_bzr(['branches'])
 
62
        self.assertEqual("* master\n", output)
 
63
 
 
64
    def test_info(self):
 
65
        self.simple_commit()
 
66
        output, error = self.run_bzr(['info'])
 
67
        self.assertEqual(error, '')
 
68
        self.assertEqual(
 
69
            output,
 
70
            'Standalone tree (format: git)\n'
 
71
            'Location:\n'
 
72
            '            light checkout root: .\n'
 
73
            '  checkout of co-located branch: master\n')
 
74
 
 
75
    def test_ignore(self):
 
76
        self.simple_commit()
 
77
        output, error = self.run_bzr(['ignore', 'foo'])
 
78
        self.assertEqual(error, '')
 
79
        self.assertEqual(output, '')
 
80
        self.assertFileEqual("foo\n", ".gitignore")
 
81
 
 
82
    def test_cat_revision(self):
 
83
        self.simple_commit()
 
84
        output, error = self.run_bzr(['cat-revision', '-r-1'], retcode=3)
 
85
        self.assertContainsRe(
 
86
            error,
 
87
            'brz: ERROR: Repository .* does not support access to raw '
 
88
            'revision texts')
 
89
        self.assertEqual(output, '')
 
90
 
 
91
    def test_branch(self):
 
92
        os.mkdir("gitbranch")
 
93
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
94
        os.chdir('gitbranch')
 
95
        builder = tests.GitBranchBuilder()
 
96
        builder.set_file(b'a', b'text for a\n', False)
 
97
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
98
        builder.finish()
 
99
        os.chdir('..')
 
100
 
 
101
        output, error = self.run_bzr(['branch', 'gitbranch', 'bzrbranch'])
 
102
        self.assertTrue(
 
103
            (error == 'Branched 1 revision(s).\n') or
 
104
            (error == 'Branched 1 revision.\n'),
 
105
            error)
 
106
 
 
107
    def test_checkout(self):
 
108
        os.mkdir("gitbranch")
 
109
        GitRepo.init(os.path.join(self.test_dir, "gitbranch"))
 
110
        os.chdir('gitbranch')
 
111
        builder = tests.GitBranchBuilder()
 
112
        builder.set_file(b'a', b'text for a\n', False)
 
113
        builder.commit(b'Joe Foo <joe@foo.com>', b'<The commit message>')
 
114
        builder.finish()
 
115
        os.chdir('..')
 
116
 
 
117
        output, error = self.run_bzr(['checkout', 'gitbranch', 'bzrbranch'])
 
118
        self.assertEqual(error,
 
119
                         'Fetching from Git to Bazaar repository. '
 
120
                         'For better performance, fetch into a Git repository.\n')
 
121
        self.assertEqual(output, '')
 
122
 
 
123
    def test_branch_ls(self):
 
124
        self.simple_commit()
 
125
        output, error = self.run_bzr(['ls', '-r-1'])
 
126
        self.assertEqual(error, '')
 
127
        self.assertEqual(output, "a\n")
 
128
 
 
129
    def test_init(self):
 
130
        self.run_bzr("init --format=git repo")
 
131
 
 
132
    def test_info_verbose(self):
 
133
        self.simple_commit()
 
134
 
 
135
        output, error = self.run_bzr(['info', '-v'])
 
136
        self.assertEqual(error, '')
 
137
        self.assertTrue("Standalone tree (format: git)" in output)
 
138
        self.assertTrue("control: Local Git Repository" in output)
 
139
        self.assertTrue("branch: Local Git Branch" in output)
 
140
        self.assertTrue("repository: Git Repository" in output)
 
141
 
 
142
    def test_push_roundtripping(self):
 
143
        self.knownFailure("roundtripping is not yet supported")
 
144
        self.with_roundtripping()
 
145
        os.mkdir("bla")
 
146
        GitRepo.init(os.path.join(self.test_dir, "bla"))
 
147
        self.run_bzr(['init', 'foo'])
 
148
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
149
        # when roundtripping is supported
 
150
        output, error = self.run_bzr(['push', '-d', 'foo', 'bla'])
 
151
        self.assertEqual(b"", output)
 
152
        self.assertTrue(error.endswith(b"Created new branch.\n"))
 
153
 
 
154
    def test_push_without_calculate_revnos(self):
 
155
        self.run_bzr(['init', '--git', 'bla'])
 
156
        self.run_bzr(['init', '--git', 'foo'])
 
157
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
158
        output, error = self.run_bzr(
 
159
            ['push', '-Ocalculate_revnos=no', '-d', 'foo', 'bla'])
 
160
        self.assertEqual("", output)
 
161
        self.assertContainsRe(
 
162
            error,
 
163
            'Pushed up to revision id git(.*).\n')
 
164
 
 
165
    def test_push_lossy_non_mainline(self):
 
166
        self.run_bzr(['init', '--git', 'bla'])
 
167
        self.run_bzr(['init', 'foo'])
 
168
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
169
        self.run_bzr(['branch', 'foo', 'foo1'])
 
170
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo1'])
 
171
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
172
        self.run_bzr(['merge', '-d', 'foo', 'foo1'])
 
173
        self.run_bzr(['commit', '--unchanged', '-m', 'merge', 'foo'])
 
174
        output, error = self.run_bzr(['push', '--lossy', '-r1.1.1', '-d', 'foo', 'bla'])
 
175
        self.assertEqual("", output)
 
176
        self.assertEqual(
 
177
            'Pushing from a Bazaar to a Git repository. For better '
 
178
            'performance, push into a Bazaar repository.\n'
 
179
            'All changes applied successfully.\n'
 
180
            'Pushed up to revision 2.\n', error)
 
181
 
 
182
    def test_push_lossy_non_mainline_incremental(self):
 
183
        self.run_bzr(['init', '--git', 'bla'])
 
184
        self.run_bzr(['init', 'foo'])
 
185
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
186
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
187
        output, error = self.run_bzr(['push', '--lossy', '-d', 'foo', 'bla'])
 
188
        self.assertEqual("", output)
 
189
        self.assertEqual(
 
190
            'Pushing from a Bazaar to a Git repository. For better '
 
191
            'performance, push into a Bazaar repository.\n'
 
192
            'All changes applied successfully.\n'
 
193
            'Pushed up to revision 2.\n', error)
 
194
        self.run_bzr(['commit', '--unchanged', '-m', 'bla', 'foo'])
 
195
        output, error = self.run_bzr(['push', '--lossy', '-d', 'foo', 'bla'])
 
196
        self.assertEqual("", output)
 
197
        self.assertEqual(
 
198
            'Pushing from a Bazaar to a Git repository. For better '
 
199
            'performance, push into a Bazaar repository.\n'
 
200
            'All changes applied successfully.\n'
 
201
            'Pushed up to revision 3.\n', error)
 
202
 
 
203
    def test_log(self):
 
204
        # Smoke test for "bzr log" in a git repository.
 
205
        self.simple_commit()
 
206
 
 
207
        # Check that bzr log does not fail and includes the revision.
 
208
        output, error = self.run_bzr(['log'])
 
209
        self.assertEqual(error, '')
 
210
        self.assertTrue(
 
211
            '<The commit message>' in output,
 
212
            "Commit message was not found in output:\n%s" % (output,))
 
213
 
 
214
    def test_log_verbose(self):
 
215
        # Smoke test for "bzr log -v" in a git repository.
 
216
        self.simple_commit()
 
217
 
 
218
        # Check that bzr log does not fail and includes the revision.
 
219
        output, error = self.run_bzr(['log', '-v'])
 
220
        self.assertContainsRe(output, 'revno: 1')
 
221
 
 
222
    def test_log_without_revno(self):
 
223
        # Smoke test for "bzr log -v" in a git repository.
 
224
        self.simple_commit()
 
225
 
 
226
        # Check that bzr log does not fail and includes the revision.
 
227
        output, error = self.run_bzr(['log', '-Ocalculate_revnos=no'])
 
228
        self.assertNotContainsRe(output, 'revno: 1')
 
229
 
 
230
    def test_commit_without_revno(self):
 
231
        repo = GitRepo.init(self.test_dir)
 
232
        output, error = self.run_bzr(
 
233
            ['commit', '-Ocalculate_revnos=yes', '--unchanged', '-m', 'one'])
 
234
        self.assertContainsRe(error, 'Committed revision 1.')
 
235
        output, error = self.run_bzr(
 
236
            ['commit', '-Ocalculate_revnos=no', '--unchanged', '-m', 'two'])
 
237
        self.assertNotContainsRe(error, 'Committed revision 2.')
 
238
        self.assertContainsRe(error, 'Committed revid .*.')
 
239
 
 
240
    def test_log_file(self):
 
241
        # Smoke test for "bzr log" in a git repository.
 
242
        repo = GitRepo.init(self.test_dir)
 
243
        builder = tests.GitBranchBuilder()
 
244
        builder.set_file('a', b'text for a\n', False)
 
245
        r1 = builder.commit(b'Joe Foo <joe@foo.com>', u'First')
 
246
        builder.set_file('a', b'text 3a for a\n', False)
 
247
        r2a = builder.commit(b'Joe Foo <joe@foo.com>', u'Second a', base=r1)
 
248
        builder.set_file('a', b'text 3b for a\n', False)
 
249
        r2b = builder.commit(b'Joe Foo <joe@foo.com>', u'Second b', base=r1)
 
250
        builder.set_file('a', b'text 4 for a\n', False)
 
251
        builder.commit(b'Joe Foo <joe@foo.com>', u'Third', merge=[r2a], base=r2b)
 
252
        builder.finish()
 
253
 
 
254
        # Check that bzr log does not fail and includes the revision.
 
255
        output, error = self.run_bzr(['log', '-n2', 'a'])
 
256
        self.assertEqual(error, '')
 
257
        self.assertIn('Second a', output)
 
258
        self.assertIn('Second b', output)
 
259
        self.assertIn('First', output)
 
260
        self.assertIn('Third', output)
 
261
 
 
262
    def test_tags(self):
 
263
        git_repo, commit_sha1 = self.simple_commit()
 
264
        git_repo.refs[b"refs/tags/foo"] = commit_sha1
 
265
 
 
266
        output, error = self.run_bzr(['tags'])
 
267
        self.assertEqual(error, '')
 
268
        self.assertEqual(output, "foo                  1\n")
 
269
 
 
270
    def test_tag(self):
 
271
        self.simple_commit()
 
272
 
 
273
        output, error = self.run_bzr(["tag", "bar"])
 
274
 
 
275
        # bzr <= 2.2 emits this message in the output stream
 
276
        # bzr => 2.3 emits this message in the error stream
 
277
        self.assertEqual(error + output, 'Created tag bar.\n')
 
278
 
 
279
    def test_init_repo(self):
 
280
        output, error = self.run_bzr(["init", "--format=git", "bla.git"])
 
281
        self.assertEqual(error, '')
 
282
        self.assertEqual(output, 'Created a standalone tree (format: git)\n')
 
283
 
 
284
    def test_diff_format(self):
 
285
        tree = self.make_branch_and_tree('.')
 
286
        self.build_tree(['a'])
 
287
        tree.add(['a'])
 
288
        output, error = self.run_bzr(['diff', '--format=git'], retcode=1)
 
289
        self.assertEqual(error, '')
 
290
        # Some older versions of Dulwich (< 0.19.12) formatted diffs slightly
 
291
        # differently.
 
292
        from dulwich import __version__ as dulwich_version
 
293
        if dulwich_version < (0, 19, 12):
 
294
            self.assertEqual(output,
 
295
                             'diff --git /dev/null b/a\n'
 
296
                             'old mode 0\n'
 
297
                             'new mode 100644\n'
 
298
                             'index 0000000..c197bd8 100644\n'
 
299
                             '--- /dev/null\n'
 
300
                             '+++ b/a\n'
 
301
                             '@@ -0,0 +1 @@\n'
 
302
                             '+contents of a\n')
 
303
        else:
 
304
            self.assertEqual(output,
 
305
                             'diff --git a/a b/a\n'
 
306
                             'old file mode 0\n'
 
307
                             'new file mode 100644\n'
 
308
                             'index 0000000..c197bd8 100644\n'
 
309
                             '--- /dev/null\n'
 
310
                             '+++ b/a\n'
 
311
                             '@@ -0,0 +1 @@\n'
 
312
                             '+contents of a\n')
 
313
 
 
314
    def test_git_import_uncolocated(self):
 
315
        r = GitRepo.init("a", mkdir=True)
 
316
        self.build_tree(["a/file"])
 
317
        r.stage("file")
 
318
        r.do_commit(ref=b"refs/heads/abranch",
 
319
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
320
        r.do_commit(ref=b"refs/heads/bbranch",
 
321
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
322
        self.run_bzr(["git-import", "a", "b"])
 
323
        self.assertEqual(
 
324
            set([".bzr", "abranch", "bbranch"]), set(os.listdir("b")))
 
325
 
 
326
    def test_git_import(self):
 
327
        r = GitRepo.init("a", mkdir=True)
 
328
        self.build_tree(["a/file"])
 
329
        r.stage("file")
 
330
        r.do_commit(ref=b"refs/heads/abranch",
 
331
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
332
        r.do_commit(ref=b"refs/heads/bbranch",
 
333
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
334
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
335
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
336
        self.assertEqual(set(["abranch", "bbranch"]),
 
337
                         set(ControlDir.open("b").get_branches().keys()))
 
338
 
 
339
    def test_git_import_incremental(self):
 
340
        r = GitRepo.init("a", mkdir=True)
 
341
        self.build_tree(["a/file"])
 
342
        r.stage("file")
 
343
        r.do_commit(ref=b"refs/heads/abranch",
 
344
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
345
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
346
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
347
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
348
        b = ControlDir.open("b")
 
349
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
350
 
 
351
    def test_git_import_tags(self):
 
352
        r = GitRepo.init("a", mkdir=True)
 
353
        self.build_tree(["a/file"])
 
354
        r.stage("file")
 
355
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
356
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
357
        r[b"refs/tags/atag"] = cid
 
358
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
359
        self.assertEqual(set([".bzr"]), set(os.listdir("b")))
 
360
        b = ControlDir.open("b")
 
361
        self.assertEqual(["abranch"], list(b.get_branches().keys()))
 
362
        self.assertEqual(["atag"],
 
363
                         list(b.open_branch("abranch").tags.get_tag_dict().keys()))
 
364
 
 
365
    def test_git_import_colo(self):
 
366
        r = GitRepo.init("a", mkdir=True)
 
367
        self.build_tree(["a/file"])
 
368
        r.stage("file")
 
369
        r.do_commit(ref=b"refs/heads/abranch",
 
370
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
371
        r.do_commit(ref=b"refs/heads/bbranch",
 
372
                    committer=b"Joe <joe@example.com>", message=b"Dummy")
 
373
        self.make_controldir("b", format="development-colo")
 
374
        self.run_bzr(["git-import", "--colocated", "a", "b"])
 
375
        self.assertEqual(
 
376
            set([b.name for b in ControlDir.open("b").list_branches()]),
 
377
            set(["abranch", "bbranch"]))
 
378
 
 
379
    def test_git_refs_from_git(self):
 
380
        r = GitRepo.init("a", mkdir=True)
 
381
        self.build_tree(["a/file"])
 
382
        r.stage("file")
 
383
        cid = r.do_commit(ref=b"refs/heads/abranch",
 
384
                          committer=b"Joe <joe@example.com>", message=b"Dummy")
 
385
        r[b"refs/tags/atag"] = cid
 
386
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
387
        self.assertEqual(stderr, "")
 
388
        self.assertEqual(stdout,
 
389
                         'refs/heads/abranch -> ' + cid.decode('ascii') + '\n'
 
390
                         'refs/tags/atag -> ' + cid.decode('ascii') + '\n')
 
391
 
 
392
    def test_git_refs_from_bzr(self):
 
393
        tree = self.make_branch_and_tree('a')
 
394
        self.build_tree(["a/file"])
 
395
        tree.add(["file"])
 
396
        revid = tree.commit(
 
397
            committer=b"Joe <joe@example.com>", message=b"Dummy")
 
398
        tree.branch.tags.set_tag("atag", revid)
 
399
        (stdout, stderr) = self.run_bzr(["git-refs", "a"])
 
400
        self.assertEqual(stderr, "")
 
401
        self.assertTrue("refs/tags/atag -> " in stdout)
 
402
        self.assertTrue("HEAD -> " in stdout)
 
403
 
 
404
    def test_check(self):
 
405
        r = GitRepo.init("gitr", mkdir=True)
 
406
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
407
        r.stage("foo")
 
408
        r.do_commit(b"message", committer=b"Somebody <user@example.com>")
 
409
        out, err = self.run_bzr(["check", "gitr"])
 
410
        self.maxDiff = None
 
411
        self.assertEqual(out, '')
 
412
        self.assertTrue(err.endswith, '3 objects\n')
 
413
 
 
414
 
 
415
class ShallowTests(ExternalBase):
 
416
 
 
417
    def setUp(self):
 
418
        super(ShallowTests, self).setUp()
 
419
        # Smoke test for "bzr log" in a git repository with shallow depth.
 
420
        self.repo = GitRepo.init('gitr', mkdir=True)
 
421
        self.build_tree_contents([("gitr/foo", b"hello from git")])
 
422
        self.repo.stage("foo")
 
423
        self.repo.do_commit(
 
424
            b"message", committer=b"Somebody <user@example.com>",
 
425
            author=b"Somebody <user@example.com>",
 
426
            commit_timestamp=1526330165, commit_timezone=0,
 
427
            author_timestamp=1526330165, author_timezone=0,
 
428
            merge_heads=[b'aa' * 20])
 
429
 
 
430
    def test_log_shallow(self):
 
431
        # Check that bzr log does not fail and includes the revision.
 
432
        output, error = self.run_bzr(['log', 'gitr'], retcode=3)
 
433
        self.assertEqual(
 
434
            error, 'brz: ERROR: Further revision history missing.\n')
 
435
        self.assertEqual(output,
 
436
                         '------------------------------------------------------------\n'
 
437
                         'revision-id: git-v1:' + self.repo.head().decode('ascii') + '\n'
 
438
                         'git commit: ' + self.repo.head().decode('ascii') + '\n'
 
439
                         'committer: Somebody <user@example.com>\n'
 
440
                         'timestamp: Mon 2018-05-14 20:36:05 +0000\n'
 
441
                         'message:\n'
 
442
                         '  message\n')
 
443
 
 
444
    def test_version_info_rio(self):
 
445
        output, error = self.run_bzr(['version-info', '--rio', 'gitr'])
 
446
        self.assertEqual(error, '')
 
447
        self.assertNotIn('revno:', output)
 
448
 
 
449
    def test_version_info_python(self):
 
450
        output, error = self.run_bzr(['version-info', '--python', 'gitr'])
 
451
        self.assertEqual(error, '')
 
452
        self.assertNotIn('revno:', output)
 
453
 
 
454
    def test_version_info_custom_with_revno(self):
 
455
        output, error = self.run_bzr(
 
456
            ['version-info', '--custom',
 
457
             '--template=VERSION_INFO r{revno})\n', 'gitr'], retcode=3)
 
458
        self.assertEqual(
 
459
            error, 'brz: ERROR: Variable {revno} is not available.\n')
 
460
        self.assertEqual(output, 'VERSION_INFO r')
 
461
 
 
462
    def test_version_info_custom_without_revno(self):
 
463
        output, error = self.run_bzr(
 
464
            ['version-info', '--custom', '--template=VERSION_INFO \n',
 
465
             'gitr'])
 
466
        self.assertEqual(error, '')
 
467
        self.assertEqual(output, 'VERSION_INFO \n')
 
468
 
 
469
 
 
470
class SwitchTests(ExternalBase):
 
471
 
 
472
    def test_switch_branch(self):
 
473
        # Create a git repository with a revision.
 
474
        repo = GitRepo.init(self.test_dir)
 
475
        builder = tests.GitBranchBuilder()
 
476
        builder.set_branch(b'refs/heads/oldbranch')
 
477
        builder.set_file('a', b'text for a\n', False)
 
478
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
479
        builder.set_branch(b'refs/heads/newbranch')
 
480
        builder.reset()
 
481
        builder.set_file('a', b'text for new a\n', False)
 
482
        builder.commit(b'Joe Foo <joe@foo.com>', u'<The commit message>')
 
483
        builder.finish()
 
484
 
 
485
        repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/newbranch')
 
486
 
 
487
        repo.reset_index()
 
488
 
 
489
        output, error = self.run_bzr('switch oldbranch')
 
490
        self.assertEqual(output, '')
 
491
        self.assertTrue(error.startswith('Updated to revision 1.\n'), error)
 
492
 
 
493
        self.assertFileEqual("text for a\n", 'a')
 
494
        tree = WorkingTree.open('.')
 
495
        with tree.lock_read():
 
496
            basis_tree = tree.basis_tree()
 
497
            with basis_tree.lock_read():
 
498
                self.assertEqual([], list(tree.iter_changes(basis_tree)))
 
499
 
 
500
    def test_branch_with_nested_trees(self):
 
501
        orig = self.make_branch_and_tree('source', format='git')
 
502
        subtree = self.make_branch_and_tree('source/subtree', format='git')
 
503
        self.build_tree(['source/subtree/a'])
 
504
        self.build_tree_contents([('source/.gitmodules', """\
 
505
[submodule "subtree"]
 
506
    path = subtree
 
507
    url = %s
 
508
""" % subtree.user_url)])
 
509
        subtree.add(['a'])
 
510
        subtree.commit('add subtree contents')
 
511
        orig.add_reference(subtree)
 
512
        orig.add(['.gitmodules'])
 
513
        orig.commit('add subtree')
 
514
 
 
515
        self.run_bzr('branch source target')
 
516
 
 
517
        target = WorkingTree.open('target')
 
518
        target_subtree = WorkingTree.open('target/subtree')
 
519
        self.assertTreesEqual(orig, target)
 
520
        self.assertTreesEqual(subtree, target_subtree)
 
521
 
 
522
 
 
523
class SwitchScriptTests(TestCaseWithTransportAndScript):
 
524
 
 
525
    def test_switch_preserves(self):
 
526
        # See https://bugs.launchpad.net/brz/+bug/1820606
 
527
        self.run_script("""
 
528
$ brz init --git r
 
529
Created a standalone tree (format: git)
 
530
$ cd r
 
531
$ echo original > file.txt
 
532
$ brz add
 
533
adding file.txt
 
534
$ brz ci -q -m "Initial"
 
535
$ echo "entered on master branch" > file.txt
 
536
$ brz stat
 
537
modified:
 
538
  file.txt
 
539
$ brz switch -b other
 
540
2>Tree is up to date at revision 1.
 
541
2>Switched to branch other
 
542
$ cat file.txt
 
543
entered on master branch
 
544
""")
 
545
 
 
546
 
 
547
class GrepTests(ExternalBase):
 
548
 
 
549
    def test_simple_grep(self):
 
550
        tree = self.make_branch_and_tree('.', format='git')
 
551
        self.build_tree_contents([('a', 'text for a\n')])
 
552
        tree.add(['a'])
 
553
        output, error = self.run_bzr('grep text')
 
554
        self.assertEqual(output, 'a:text for a\n')
 
555
        self.assertEqual(error, '')
 
556
 
 
557
 
 
558
class ReconcileTests(ExternalBase):
 
559
 
 
560
    def test_simple_reconcile(self):
 
561
        tree = self.make_branch_and_tree('.', format='git')
 
562
        self.build_tree_contents([('a', 'text for a\n')])
 
563
        tree.add(['a'])
 
564
        output, error = self.run_bzr('reconcile')
 
565
        self.assertContainsRe(
 
566
            output,
 
567
            'Reconciling branch file://.*\n'
 
568
            'Reconciling repository file://.*\n'
 
569
            'Reconciliation complete.\n')
 
570
        self.assertEqual(error, '')
 
571
 
 
572
 
 
573
class StatusTests(ExternalBase):
 
574
 
 
575
    def test_empty_dir(self):
 
576
        tree = self.make_branch_and_tree('.', format='git')
 
577
        self.build_tree(['a/', 'a/foo'])
 
578
        self.build_tree_contents([('.gitignore', 'foo\n')])
 
579
        tree.add(['.gitignore'])
 
580
        tree.commit('add ignore')
 
581
        output, error = self.run_bzr('st')
 
582
        self.assertEqual(output, '')
 
583
        self.assertEqual(error, '')
 
584
 
 
585
 
 
586
class StatsTests(ExternalBase):
 
587
 
 
588
    def test_simple_stats(self):
 
589
        self.requireFeature(PluginLoadedFeature('stats'))
 
590
        tree = self.make_branch_and_tree('.', format='git')
 
591
        self.build_tree_contents([('a', 'text for a\n')])
 
592
        tree.add(['a'])
 
593
        tree.commit('a commit', committer='Somebody <somebody@example.com>')
 
594
        output, error = self.run_bzr('stats')
 
595
        self.assertEqual(output, '   1 Somebody <somebody@example.com>\n')
 
596
 
 
597
 
 
598
class GitObjectsTests(ExternalBase):
 
599
 
 
600
    def run_simple(self, format):
 
601
        tree = self.make_branch_and_tree('.', format=format)
 
602
        self.build_tree(['a/', 'a/foo'])
 
603
        tree.add(['a'])
 
604
        tree.commit('add a')
 
605
        output, error = self.run_bzr('git-objects')
 
606
        shas = list(output.splitlines())
 
607
        self.assertEqual([40, 40], [len(s) for s in shas])
 
608
        self.assertEqual(error, '')
 
609
 
 
610
        output, error = self.run_bzr('git-object %s' % shas[0])
 
611
        self.assertEqual('', error)
 
612
 
 
613
    def test_in_native(self):
 
614
        self.run_simple(format='git')
 
615
 
 
616
    def test_in_bzr(self):
 
617
        self.run_simple(format='2a')
 
618
 
 
619
 
 
620
class GitApplyTests(ExternalBase):
 
621
 
 
622
    def test_apply(self):
 
623
        b = self.make_branch_and_tree('.')
 
624
 
 
625
        with open('foo.patch', 'w') as f:
 
626
            f.write("""\
 
627
From bdefb25fab801e6af0a70e965f60cb48f2b759fa Mon Sep 17 00:00:00 2001
 
628
From: Dmitry Bogatov <KAction@debian.org>
 
629
Date: Fri, 8 Feb 2019 23:28:30 +0000
 
630
Subject: [PATCH] Add fixed for out-of-date-standards-version
 
631
 
 
632
---
 
633
 message           | 3 +++
 
634
 1 files changed, 14 insertions(+)
 
635
 create mode 100644 message
 
636
 
 
637
diff --git a/message b/message
 
638
new file mode 100644
 
639
index 0000000..05ec0b1
 
640
--- /dev/null
 
641
+++ b/message
 
642
@@ -0,0 +1,3 @@
 
643
+Update standards version, no changes needed.
 
644
+Certainty: certain
 
645
+Fixed-Lintian-Tags: out-of-date-standards-version
 
646
""")
 
647
        output, error = self.run_bzr('git-apply foo.patch')
 
648
        self.assertContainsRe(
 
649
            error,
 
650
            'Committing to: .*\n'
 
651
            'Committed revision 1.\n')