~jelmer/bzr-builddeb/relative-imports

670.1.1 by Jelmer Vernooij
Add base files for quilt handling.
1
#    quilt.py -- Quilt patch handling
2
#    Copyright (C) 2011 Canonical Ltd.
3
#
4
#    This file is part of bzr-builddeb.
5
#
6
#    bzr-builddeb is free software; you can redistribute it and/or modify
7
#    it under the terms of the GNU General Public License as published by
8
#    the Free Software Foundation; either version 2 of the License, or
9
#    (at your option) any later version.
10
#
11
#    bzr-builddeb is distributed in the hope that it will be useful,
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#    GNU General Public License for more details.
15
#
16
#    You should have received a copy of the GNU General Public License
17
#    along with bzr-builddeb; if not, write to the Free Software
18
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
"""Quilt patch handling."""
22
23
from __future__ import absolute_import
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
24
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
25
import shutil
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
26
import tempfile
710 by Jelmer Vernooij
Fix patch unapplying from DirStateRevisionTrees. LP: #923688
27
from bzrlib.mutabletree import MutableTree
727 by Jelmer Vernooij
Skip quilt unapplying for trees that can not be copied (such as
28
from bzrlib.revisiontree import RevisionTree
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
29
from bzrlib import (
678.4.6 by Jelmer Vernooij
Add tests.
30
    errors,
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
31
    merge as _mod_merge,
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
32
    trace,
33
    )
34
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
35
from bzrlib.plugins.builddeb import gettext
678.4.6 by Jelmer Vernooij
Add tests.
36
from bzrlib.plugins.builddeb.quilt import (
37
    quilt_applied,
38
    quilt_unapplied,
693 by Jelmer Vernooij
Add pre-commit hook that warns about applied quilt patches, and can
39
    quilt_pop,
678.4.6 by Jelmer Vernooij
Add tests.
40
    quilt_pop_all,
688.1.1 by Jelmer Vernooij
Do post-merge processing with merge rather than with transform hooks.
41
    quilt_push,
678.4.6 by Jelmer Vernooij
Add tests.
42
    quilt_push_all,
720 by Jelmer Vernooij
Use quilt_series in more places.
43
    quilt_series,
678.4.6 by Jelmer Vernooij
Add tests.
44
    )
45
from bzrlib.plugins.builddeb.util import debuild_config
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
46
47
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
48
class NoUnapplyingMerger(_mod_merge.Merge3Merger):
49
50
    _no_quilt_unapplying = True
51
52
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
53
def tree_unapply_patches(orig_tree, orig_branch=None, force=False):
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
54
    """Return a tree with patches unapplied.
55
680 by Jelmer Vernooij
Merge support for running quilt.
56
    :param orig_tree: Tree from which to unapply quilt patches
57
    :param orig_branch: Related branch (optional)
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
58
    :return: Tuple with tree and temp path.
59
        The tree is a tree with unapplied patches; either a checkout of
60
        tree or tree itself if there were no patches
61
    """
680 by Jelmer Vernooij
Merge support for running quilt.
62
    if orig_branch is None:
63
        orig_branch = orig_tree.branch
719 by Jelmer Vernooij
Read applied patches directly from tree.
64
    applied_patches = quilt_applied(orig_tree)
65
    if not applied_patches:
670.1.2 by Jelmer Vernooij
Add basic quilt wrapper.
66
        # No quilt patches
67
        return orig_tree, None
68
69
    target_dir = tempfile.mkdtemp()
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
70
    try:
710 by Jelmer Vernooij
Fix patch unapplying from DirStateRevisionTrees. LP: #923688
71
        if isinstance(orig_tree, MutableTree):
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
72
            tree = orig_branch.create_checkout(target_dir, lightweight=True,
73
                revision_id=orig_tree.last_revision(), accelerator_tree=orig_tree)
74
            merger = _mod_merge.Merger.from_uncommitted(tree, orig_tree)
75
            merger.merge_type = NoUnapplyingMerger
76
            merger.do_merge()
727 by Jelmer Vernooij
Skip quilt unapplying for trees that can not be copied (such as
77
        elif isinstance(orig_tree, RevisionTree):
78
            tree = orig_branch.create_checkout(target_dir, lightweight=True,
79
                accelerator_tree=orig_tree, revision_id=orig_tree.get_revision_id())
710 by Jelmer Vernooij
Fix patch unapplying from DirStateRevisionTrees. LP: #923688
80
        else:
727 by Jelmer Vernooij
Skip quilt unapplying for trees that can not be copied (such as
81
            trace.mutter("Not sure how to create copy of %r", orig_tree)
82
            shutil.rmtree(target_dir)
83
            return orig_tree, None
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
84
        trace.mutter("Applying quilt patches for %r in %s", orig_tree, target_dir)
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
85
        quilt_pop_all(working_dir=tree.basedir, force=force)
670.1.9 by Jelmer Vernooij
Fix handling of revision trees.
86
        return tree, target_dir
87
    except:
88
        shutil.rmtree(target_dir)
89
        raise
678.4.6 by Jelmer Vernooij
Add tests.
90
91
688.1.2 by Jelmer Vernooij
Move more code to merge_quilt.
92
def post_process_quilt_patches(tree, old_patches, policy):
688.1.1 by Jelmer Vernooij
Do post-merge processing with merge rather than with transform hooks.
93
    """(Un)apply patches after a merge.
94
95
    :param tree: Working tree to work in
96
    :param old_patches: List of patches applied before the operation (usually a merge)
97
    """
720 by Jelmer Vernooij
Use quilt_series in more places.
98
    new_patches = quilt_series(tree)
719 by Jelmer Vernooij
Read applied patches directly from tree.
99
    applied_patches = quilt_applied(tree)
688.1.1 by Jelmer Vernooij
Do post-merge processing with merge rather than with transform hooks.
100
    if policy == "applied":
101
        to_apply = []
102
        for p in new_patches:
103
            if p in old_patches:
104
                continue
105
            if not p in applied_patches:
106
                to_apply.append(p)
107
        if to_apply == []:
108
            return
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
109
        trace.note(gettext("Applying %d quilt patches."), to_apply)
688.1.1 by Jelmer Vernooij
Do post-merge processing with merge rather than with transform hooks.
110
        for p in to_apply:
111
            quilt_push(tree.basedir, p)
112
    elif policy == "unapplied":
113
        to_unapply = []
114
        for p in new_patches:
115
            if p in old_patches:
116
                continue
117
            if p in applied_patches:
118
                to_unapply.append(p)
119
        if to_unapply == []:
120
            return
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
121
        trace.note(gettext("Unapplying %d quilt patches."), to_unapply)
688.1.1 by Jelmer Vernooij
Do post-merge processing with merge rather than with transform hooks.
122
        for p in to_unapply:
123
            quilt_pop(tree.basedir, p)
693 by Jelmer Vernooij
Add pre-commit hook that warns about applied quilt patches, and can
124
125
678.4.6 by Jelmer Vernooij
Add tests.
126
def start_commit_quilt_patches(tree):
127
    config = debuild_config(tree, False)
693 by Jelmer Vernooij
Add pre-commit hook that warns about applied quilt patches, and can
128
    policy = config.quilt_commit_policy
719 by Jelmer Vernooij
Read applied patches directly from tree.
129
    applied_patches = quilt_applied(tree)
678.4.6 by Jelmer Vernooij
Add tests.
130
    unapplied_patches = quilt_unapplied(tree.basedir)
131
    if policy is None:
132
        # No policy set - just warn about having both applied and unapplied
133
        # patches.
134
        if applied_patches and unapplied_patches:
135
            trace.warning(
707 by Jelmer Vernooij
Use gettext in new quilt handling code, tweak messages.
136
                gettext("Committing with %d patches applied and %d patches unapplied."),
678.4.6 by Jelmer Vernooij
Add tests.
137
                len(applied_patches), len(unapplied_patches))
138
    elif policy == "applied":
139
        quilt_push_all(tree.basedir)
140
    elif policy == "unapplied":
141
        quilt_pop_all(tree.basedir)
142
    else:
143
        raise errors.BzrError("Invalid setting %r for quilt-commit-policy" %
144
                policy)