/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
1
#    __init__.py -- Quilt support for breezy
2
#
3
#    Breezy 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
#    Breezy 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 Breezy; if not, write to the Free Software
15
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16
#
17
18
"""Quilt patch integration.
19
20
This plugin adds support for three configuration options:
21
22
 * quilt.commit_policy
23
 * quilt.smart_merge
24
 * quilt.tree_policy
25
26
"""
27
28
from ...errors import BzrError
29
from ... import trace
30
31
32
class QuiltUnapplyError(BzrError):
33
34
    _fmt = ("Unable to unapply quilt patches for %(kind)r tree: %(msg)s")
35
36
    def __init__(self, kind, msg):
37
        BzrError.__init__(self)
38
        self.kind = kind
39
        if msg is not None and msg.count("\n") == 1:
40
            msg = msg.strip()
41
        self.msg = msg
42
43
44
def pre_merge_quilt(merger):
45
    if getattr(merger, "_no_quilt_unapplying", False):
46
        return
47
48
    config = merger.working_tree.get_config_stack()
49
    merger.debuild_config = config
50
    if not config.get('quilt.smart_merge'):
51
        trace.mutter("skipping smart quilt merge, not enabled.")
52
        return
53
54
    if (not merger.other_tree.is_versioned(".pc") and
55
            not merger.this_tree.is_versioned(".pc") and
56
            not merger.working_tree.is_versioned(".pc")):
57
        return
58
59
    from .quilt import QuiltPatches, QuiltError
60
    quilt = QuiltPatches(merger.working_tree)
61
    from .merge import tree_unapply_patches
62
    trace.note("Unapplying quilt patches to prevent spurious conflicts")
63
    merger._quilt_tempdirs = []
64
    merger._old_quilt_series = quilt.series()
65
    if merger._old_quilt_series:
66
        quilt.pop_all()
67
    try:
68
        merger.this_tree, this_dir = tree_unapply_patches(
69
            merger.this_tree, merger.this_branch, force=True)
70
    except QuiltError as e:
71
        raise QuiltUnapplyError("this", e.stderr)
72
    else:
73
        if this_dir is not None:
74
            merger._quilt_tempdirs.append(this_dir)
75
    try:
76
        merger.base_tree, base_dir = tree_unapply_patches(
77
            merger.base_tree, merger.this_branch, force=True)
78
    except QuiltError as e:
79
        raise QuiltUnapplyError("base", e.stderr)
80
    else:
81
        if base_dir is not None:
82
            merger._quilt_tempdirs.append(base_dir)
83
    other_branch = getattr(merger, "other_branch", None)
84
    if other_branch is None:
85
        other_branch = merger.this_branch
86
    try:
87
        merger.other_tree, other_dir = tree_unapply_patches(
88
            merger.other_tree, other_branch, force=True)
89
    except QuiltError as e:
90
        raise QuiltUnapplyError("other", e.stderr)
91
    else:
92
        if other_dir is not None:
93
            merger._quilt_tempdirs.append(other_dir)
94
95
96
def post_merge_quilt_cleanup(merger):
97
    import shutil
98
    for dir in getattr(merger, "_quilt_tempdirs", []):
99
        shutil.rmtree(dir)
100
    config = merger.working_tree.get_config_stack()
101
    policy = config.get('quilt.tree_policy')
102
    if policy is None:
103
        return
104
    from .merge import post_process_quilt_patches
105
    post_process_quilt_patches(
106
        merger.working_tree,
107
        getattr(merger, "_old_quilt_series", []), policy)
108
109
110
def start_commit_check_quilt(tree):
111
    """start_commit hook which checks the state of quilt patches.
112
    """
113
    config = tree.get_config_stack()
114
    policy = config.get('quilt.commit_policy')
115
    from .merge import start_commit_quilt_patches
7490.87.1 by Jelmer Vernooij
Don't throw exception when quilt is not available, but the tree contains patches.
116
    from .wrapper import QuiltNotInstalled
117
    try:
118
        start_commit_quilt_patches(tree, policy)
119
    except QuiltNotInstalled:
120
        trace.warning(
121
            'quilt not installed; not updating patches')
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
122
123
124
def post_build_tree_quilt(tree):
125
    config = tree.get_config_stack()
126
    policy = config.get('quilt.tree_policy')
127
    if policy is None:
128
        return
129
    from .merge import post_process_quilt_patches
7490.87.1 by Jelmer Vernooij
Don't throw exception when quilt is not available, but the tree contains patches.
130
    from .wrapper import QuiltNotInstalled
131
    try:
132
        post_process_quilt_patches(tree, [], policy)
133
    except QuiltNotInstalled:
134
        trace.warning(
135
            'quilt not installed; not touching patches')
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
136
137
138
from ...hooks import install_lazy_named_hook
139
install_lazy_named_hook(
140
    "breezy.merge", "Merger.hooks",
141
    'pre_merge_quilt', pre_merge_quilt,
142
    'Quilt patch (un)applying')
143
install_lazy_named_hook(
144
    "breezy.mutabletree", "MutableTree.hooks",
145
    "start_commit", start_commit_check_quilt,
146
    "Check for (un)applied quilt patches")
147
install_lazy_named_hook(
148
    "breezy.merge", "Merger.hooks",
149
    'post_merge', post_merge_quilt_cleanup,
150
    'Cleaning up quilt temporary directories')
151
install_lazy_named_hook(
152
    "breezy.mutabletree", "MutableTree.hooks",
153
    'post_build_tree', post_build_tree_quilt,
154
    'Applying quilt patches.')
155
156
157
from ...config import option_registry, Option, bool_from_store
158
option_registry.register(
159
    Option('quilt.smart_merge', default=True, from_unicode=bool_from_store,
160
           help="Unapply quilt patches before merging."))
161
7321.2.3 by Jelmer Vernooij
Fix flake8 errors.
162
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
163
def policy_from_store(s):
7321.2.3 by Jelmer Vernooij
Fix flake8 errors.
164
    if s not in ('applied', 'unapplied'):
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
165
        raise ValueError('Invalid quilt.commit_policy: %s' % s)
166
    return s
167
168
option_registry.register(
169
    Option('quilt.commit_policy', default=None, from_unicode=policy_from_store,
7321.2.3 by Jelmer Vernooij
Fix flake8 errors.
170
           help="Whether to apply or unapply all patches in commits."))
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
171
172
option_registry.register(
173
    Option('quilt.tree_policy', default=None, from_unicode=policy_from_store,
7321.2.3 by Jelmer Vernooij
Fix flake8 errors.
174
           help="Whether to apply or unapply all patches after checkout/update."))
7321.2.1 by Jelmer Vernooij
Bundle the quilt plugin.
175
176
177
def load_tests(loader, basic_tests, pattern):
178
    basic_tests.addTest(
179
        loader.loadTestsFromModuleNames([__name__ + '.tests']))
180
    return basic_tests