/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#    Copyright (C) 2011 Canonical Ltd
#    Copyright (C) 2019 Jelmer Vernooij <jelmer@jelmer.uk>
#
#    Breezy is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Breezy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Breezy; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

"""Tests for the merge_quilt code."""

import os
import shutil

from .... import (
    bedding,
    config,
    errors,
    trace,
    )
from ....merge import Merger
from ....mutabletree import MutableTree

from .. import (
    pre_merge_quilt,
    start_commit_check_quilt,
    post_build_tree_quilt,
    post_merge_quilt_cleanup,
    )
from ..quilt import QuiltPatches
from ..merge import tree_unapply_patches

from .test_wrapper import quilt_feature

from ....tests import (
    TestCaseWithTransport,
    TestSkipped,
    )

TRIVIAL_PATCH = """--- /dev/null	2012-01-02 01:09:10.986490031 +0100
+++ base/a	2012-01-02 20:03:59.710666215 +0100
@@ -0,0 +1 @@
+a
"""


def quilt_push_all(tree):
    QuiltPatches(tree, 'debian/patches').push_all()


class TestTreeUnapplyPatches(TestCaseWithTransport):

    _test_needs_features = [quilt_feature]

    def test_no_patches(self):
        tree = self.make_branch_and_tree('.')
        new_tree, target_dir = tree_unapply_patches(tree)
        self.assertIs(tree, new_tree)
        self.assertIs(None, target_dir)

    def test_unapply(self):
        orig_tree = self.make_branch_and_tree('source')
        self.build_tree(["source/debian/", "source/debian/patches/"])
        self.build_tree_contents([
            ("source/debian/patches/series", "patch1.diff\n"),
            ("source/debian/patches/patch1.diff", TRIVIAL_PATCH)])
        quilt_push_all(orig_tree)
        orig_tree.smart_add([orig_tree.basedir])
        tree, target_dir = tree_unapply_patches(orig_tree)
        self.addCleanup(shutil.rmtree, target_dir)
        self.assertPathExists("source/a")
        self.assertNotEqual(tree.basedir, orig_tree.basedir)
        self.assertPathDoesNotExist(tree.abspath("a"))
        self.assertPathExists(tree.abspath("debian/patches/series"))

    def test_unapply_nothing_applied(self):
        orig_tree = self.make_branch_and_tree('source')
        self.build_tree(["source/debian/", "source/debian/patches/"])
        self.build_tree_contents([
            ("source/debian/patches/series", "patch1.diff\n"),
            ("source/debian/patches/patch1.diff", TRIVIAL_PATCH)])
        orig_tree.smart_add([orig_tree.basedir])
        tree, target_dir = tree_unapply_patches(orig_tree)
        self.assertIs(tree, orig_tree)
        self.assertIs(None, target_dir)


class TestMergeHook(TestCaseWithTransport):

    _test_needs_features = [quilt_feature]

    def enable_hooks(self):
        Merger.hooks.install_named_hook(
            'pre_merge', pre_merge_quilt,
            'Debian quilt patch (un)applying and ancestry fixing')
        Merger.hooks.install_named_hook(
            'post_merge', post_merge_quilt_cleanup,
            'Cleaning up quilt temporary directories')
        MutableTree.hooks.install_named_hook(
            "post_build_tree", post_build_tree_quilt,
            "Apply quilt trees.")

    def test_diverged_patches(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')
        self.build_tree(
            ['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
        self.build_tree_contents([
            ('a/.pc/.quilt_patches', 'debian/patches\n'),
            ('a/.pc/.version', '2\n'),
            ('a/debian/source/format', '3.0 (quilt)'),
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        tree_b = tree_a.controldir.sprout('b').open_workingtree()
        self.build_tree_contents([
            ('a/debian/patches/patch1',
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+d\n"]))])
        quilt_push_all(tree_a)
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('apply patches')
        self.build_tree_contents([
            ('b/debian/patches/patch1',
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+c\n"]))])
        quilt_push_all(tree_b)
        tree_b.commit('apply patches')
        conflicts = tree_a.merge_from_branch(tree_b.branch)
        self.assertFileEqual("""\
--- /dev/null\t2012-01-02 01:09:10.986490031 +0100
+++ base/a\t2012-01-02 20:03:59.710666215 +0100
@@ -0,0 +1 @@
<<<<<<< TREE
+d
=======
+c
>>>>>>> MERGE-SOURCE
""", "a/debian/patches/patch1")
        # "a" should be unapplied again
        self.assertPathDoesNotExist("a/a")
        self.assertEquals(1, conflicts)

    def test_auto_apply_patches_after_checkout(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')

        self.build_tree(['a/debian/', 'a/debian/patches/'])
        self.build_tree_contents([
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        bedding.ensure_config_dir_exists()
        config.GlobalStack().set('quilt.tree_policy', 'applied')

        tree_a.branch.create_checkout("b")
        self.assertFileEqual("a\n", "b/a")

    def test_auto_apply_patches_after_update_format_1(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')
        tree_b = tree_a.branch.create_checkout("b")

        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/.pc/'])
        self.build_tree_contents([
            ('a/.pc/.quilt_patches', 'debian/patches\n'),
            ('a/.pc/.version', '2\n'),
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        self.build_tree(["b/.bzr-builddeb/", "b/debian/", "b/debian/source/"])
        tree_b.get_config_stack().set('quilt.tree_policy', 'applied')
        self.build_tree_contents([
            ("b/debian/source/format", "1.0")])

        tree_b.update()
        self.assertFileEqual("a\n", "b/a")

    def test_auto_apply_patches_after_update(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')
        tree_b = tree_a.branch.create_checkout("b")

        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
        self.build_tree_contents([
            ('a/.pc/.quilt_patches', 'debian/patches\n'),
            ('a/.pc/.version', '2\n'),
            ('a/debian/source/format', '3.0 (quilt)'),
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        self.build_tree(["b/.bzr-builddeb/", "b/debian/", "b/debian/source/"])
        tree_b.get_config_stack().set('quilt.tree_policy', 'applied')
        self.build_tree_contents([
            ('b/debian/source/format', '3.0 (quilt)'),
            ])

        tree_b.update()
        self.assertFileEqual("a\n", "b/a")

    def test_auto_unapply_patches_after_update(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')
        tree_b = tree_a.branch.create_checkout("b")

        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/debian/source/', 'a/.pc/'])
        self.build_tree_contents([
            ('a/.pc/.quilt_patches', 'debian/patches\n'),
            ('a/.pc/.version', '2\n'),
            ('a/debian/source/format', '3.0 (quilt)'),
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH)])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        self.build_tree(["b/.bzr-builddeb/"])
        tree_b.get_config_stack().set('quilt.tree_policy', 'unapplied')

        tree_b.update()
        self.assertPathDoesNotExist("b/a")

    def test_disabled_hook(self):
        self.enable_hooks()

        tree_a = self.make_branch_and_tree('a')
        tree_a.get_config_stack().set('quilt.smart_merge', False)
        self.build_tree(['a/debian/', 'a/debian/patches/', 'a/.pc/'])
        self.build_tree_contents([
            ('a/.pc/.quilt_patches', 'debian/patches\n'),
            ('a/.pc/.version', '2\n'),
            ('a/debian/patches/series', 'patch1\n'),
            ('a/debian/patches/patch1', TRIVIAL_PATCH),
            ("a/a", "")])
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('initial')

        tree_b = tree_a.controldir.sprout('b').open_workingtree()
        self.build_tree_contents([
            ('a/debian/patches/patch1',
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+d\n"]))])
        quilt_push_all(tree_a)
        tree_a.smart_add([tree_a.basedir])
        tree_a.commit('apply patches')
        self.assertFileEqual("d\n", "a/a")
        self.build_tree_contents([
            ('b/debian/patches/patch1',
                "\n".join(TRIVIAL_PATCH.splitlines()[:-1] + ["+c\n"]))])
        quilt_push_all(tree_b)
        tree_b.commit('apply patches')
        self.assertFileEqual("c\n", "b/a")
        conflicts = tree_a.merge_from_branch(tree_b.branch)
        self.assertFileEqual("""\
--- /dev/null\t2012-01-02 01:09:10.986490031 +0100
+++ base/a\t2012-01-02 20:03:59.710666215 +0100
@@ -0,0 +1 @@
<<<<<<< TREE
+d
=======
+c
>>>>>>> MERGE-SOURCE
""", "a/debian/patches/patch1")
        self.assertFileEqual("""\
<<<<<<< TREE
d
=======
c
>>>>>>> MERGE-SOURCE
""", "a/a")
        self.assertEquals(2, conflicts)



class StartCommitMergeHookTests(TestCaseWithTransport):

    _test_needs_features = [quilt_feature]

    def enable_hooks(self):
        MutableTree.hooks.install_named_hook(
            'start_commit', start_commit_check_quilt,
            'Check for (un)applied quilt patches')

    def test_applied(self):
        self.enable_hooks()
        tree = self.make_branch_and_tree('source')
        tree.get_config_stack().set('quilt.commit_policy', 'applied')
        self.build_tree(['source/debian/', 'source/debian/patches/',
                         'source/debian/source/'])
        self.build_tree_contents([
            ('source/debian/source/format', '3.0 (quilt)'),
            ('source/debian/patches/series', 'patch1\n'),
            ('source/debian/patches/patch1', TRIVIAL_PATCH)])
        self.assertPathDoesNotExist("source/.pc/applied-patches")
        self.assertPathDoesNotExist("source/a")
        tree.smart_add([tree.basedir])
        tree.commit("foo")
        self.assertPathExists("source/.pc/applied-patches")
        self.assertPathExists("source/a")

    def test_unapplied(self):
        self.enable_hooks()
        tree = self.make_branch_and_tree('source')
        tree.get_config_stack().set('quilt.commit_policy', 'unapplied')
        self.build_tree(
            ['source/debian/', 'source/debian/patches/',
             'source/debian/source/'])
        self.build_tree_contents([
            ('source/debian/patches/series', 'patch1\n'),
            ('source/debian/patches/patch1', TRIVIAL_PATCH),
            ('source/debian/source/format', '3.0 (quilt)')])
        quilt_push_all(tree)
        self.assertPathExists("source/.pc/applied-patches")
        self.assertPathExists("source/a")
        tree.smart_add([tree.basedir])
        tree.commit("foo")
        self.assertPathDoesNotExist("source/.pc/applied-patches")
        self.assertPathDoesNotExist("source/a")

    def test_warning(self):
        self.enable_hooks()
        warnings = []

        def warning(*args):
            if len(args) > 1:
                warnings.append(args[0] % args[1:])
            else:
                warnings.append(args[0])
        _warning = trace.warning
        trace.warning = warning
        self.addCleanup(setattr, trace, "warning", _warning)
        tree = self.make_branch_and_tree('source')
        self.build_tree(['source/debian/', 'source/debian/patches/',
                         'source/debian/source/'])
        self.build_tree_contents([
            ('source/debian/patches/series', 'patch1\n'),
            ('source/debian/patches/patch1', TRIVIAL_PATCH)])
        quilt_push_all(tree)
        tree.smart_add([tree.basedir])
        tree.commit("initial")
        self.assertEquals([], warnings)
        self.assertPathExists("source/.pc/applied-patches")
        self.assertPathExists("source/a")
        self.build_tree_contents([
            ('source/debian/source/format', '3.0 (quilt)'),
            ('source/debian/patches/series', 'patch1\npatch2\n'),
            ('source/debian/patches/patch2',
                """--- /dev/null	2012-01-02 01:09:10.986490031 +0100
+++ base/b	2012-01-02 20:03:59.710666215 +0100
@@ -0,0 +1 @@
+a
""")])
        tree.smart_add([tree.basedir])
        tree.commit("foo")
        self.assertEquals(
            ['Committing with 1 patches applied and 1 patches unapplied.'],
            warnings)
        self.assertPathExists("source/.pc/applied-patches")
        self.assertPathExists("source/a")
        self.assertPathDoesNotExist("source/b")