1
# quilt.py -- Quilt patch handling
2
# Copyright (C) 2011 Canonical Ltd.
3
# Copyright (C) 2019 Jelmer Verooij <jelmer@jelmer.uk>
5
# Breezy is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# Breezy is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with Breezy; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
"""Quilt patch handling."""
22
from __future__ import absolute_import
27
from ...i18n import gettext
28
from ...mutabletree import MutableTree
29
from ...revisiontree import RevisionTree
41
class NoUnapplyingMerger(_mod_merge.Merge3Merger):
43
_no_quilt_unapplying = True
46
def tree_unapply_patches(orig_tree, orig_branch=None, force=False):
47
"""Return a tree with patches unapplied.
49
:param orig_tree: Tree from which to unapply quilt patches
50
:param orig_branch: Related branch (optional)
51
:return: Tuple with tree and temp path.
52
The tree is a tree with unapplied patches; either a checkout of
53
tree or tree itself if there were no patches
55
if orig_branch is None:
56
orig_branch = orig_tree.branch
57
quilt = QuiltPatches.find(orig_tree)
59
return orig_tree, None
60
applied_patches = quilt.applied()
61
if not applied_patches:
63
return orig_tree, None
65
target_dir = tempfile.mkdtemp()
67
if isinstance(orig_tree, MutableTree):
68
tree = orig_branch.create_checkout(
69
target_dir, lightweight=True,
70
revision_id=orig_tree.last_revision(),
71
accelerator_tree=orig_tree)
72
merger = _mod_merge.Merger.from_uncommitted(tree, orig_tree)
73
merger.merge_type = NoUnapplyingMerger
75
elif isinstance(orig_tree, RevisionTree):
76
tree = orig_branch.create_checkout(
77
target_dir, lightweight=True,
78
accelerator_tree=orig_tree, revision_id=orig_tree.get_revision_id())
80
trace.mutter("Not sure how to create copy of %r", orig_tree)
81
shutil.rmtree(target_dir)
82
return orig_tree, None
83
trace.mutter("Applying quilt patches for %r in %s", orig_tree, target_dir)
84
quilt = QuiltPatches.find(tree)
86
quilt.pop_all(force=force)
87
return tree, target_dir
89
shutil.rmtree(target_dir)
93
def post_process_quilt_patches(tree, old_patches, policy):
94
"""(Un)apply patches after a merge.
96
:param tree: Working tree to work in
97
:param old_patches: List of patches applied before the operation (usually a merge)
99
quilt = QuiltPatches.find(tree)
102
new_patches = quilt.series()
103
applied_patches = quilt.applied()
104
if policy == "applied":
106
for p in new_patches:
109
if p not in applied_patches:
113
trace.note(gettext("Applying %d quilt patches."), len(to_apply))
116
elif policy == "unapplied":
118
for p in new_patches:
121
if p in applied_patches:
125
trace.note(gettext("Unapplying %d quilt patches."), len(to_unapply))
130
def start_commit_quilt_patches(tree, policy):
131
quilt = QuiltPatches.find(tree)
134
applied_patches = quilt.applied()
135
unapplied_patches = quilt.unapplied()
137
# No policy set - just warn about having both applied and unapplied
139
if applied_patches and unapplied_patches:
141
gettext("Committing with %d patches applied and %d patches unapplied."),
142
len(applied_patches), len(unapplied_patches))
143
elif policy == "applied":
145
elif policy == "unapplied":