/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/plugins/quilt/merge.py

  • Committer: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    quilt.py -- Quilt patch handling
2
 
#    Copyright (C) 2011 Canonical Ltd.
3
 
#    Copyright (C) 2019 Jelmer Verooij <jelmer@jelmer.uk>
4
 
#
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.
9
 
#
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.
14
 
#
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
18
 
#
19
 
 
20
 
"""Quilt patch handling."""
21
 
 
22
 
from __future__ import absolute_import
23
 
 
24
 
import shutil
25
 
import tempfile
26
 
 
27
 
from ...i18n import gettext
28
 
from ...mutabletree import MutableTree
29
 
from ...revisiontree import RevisionTree
30
 
from ... import (
31
 
    errors,
32
 
    merge as _mod_merge,
33
 
    trace,
34
 
    )
35
 
 
36
 
from .quilt import (
37
 
    QuiltPatches,
38
 
)
39
 
 
40
 
 
41
 
class NoUnapplyingMerger(_mod_merge.Merge3Merger):
42
 
 
43
 
    _no_quilt_unapplying = True
44
 
 
45
 
 
46
 
def tree_unapply_patches(orig_tree, orig_branch=None, force=False):
47
 
    """Return a tree with patches unapplied.
48
 
 
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
54
 
    """
55
 
    if orig_branch is None:
56
 
        orig_branch = orig_tree.branch
57
 
    quilt = QuiltPatches.find(orig_tree)
58
 
    if quilt is None:
59
 
        return orig_tree, None
60
 
    applied_patches = quilt.applied()
61
 
    if not applied_patches:
62
 
        # No quilt patches
63
 
        return orig_tree, None
64
 
 
65
 
    target_dir = tempfile.mkdtemp()
66
 
    try:
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
74
 
            merger.do_merge()
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())
79
 
        else:
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)
85
 
        if quilt is not None:
86
 
            quilt.pop_all(force=force)
87
 
        return tree, target_dir
88
 
    except BaseException:
89
 
        shutil.rmtree(target_dir)
90
 
        raise
91
 
 
92
 
 
93
 
def post_process_quilt_patches(tree, old_patches, policy):
94
 
    """(Un)apply patches after a merge.
95
 
 
96
 
    :param tree: Working tree to work in
97
 
    :param old_patches: List of patches applied before the operation (usually a merge)
98
 
    """
99
 
    quilt = QuiltPatches.find(tree)
100
 
    if quilt is None:
101
 
        return
102
 
    new_patches = quilt.series()
103
 
    applied_patches = quilt.applied()
104
 
    if policy == "applied":
105
 
        to_apply = []
106
 
        for p in new_patches:
107
 
            if p in old_patches:
108
 
                continue
109
 
            if p not in applied_patches:
110
 
                to_apply.append(p)
111
 
        if to_apply == []:
112
 
            return
113
 
        trace.note(gettext("Applying %d quilt patches."), len(to_apply))
114
 
        for p in to_apply:
115
 
            quilt.push(p)
116
 
    elif policy == "unapplied":
117
 
        to_unapply = []
118
 
        for p in new_patches:
119
 
            if p in old_patches:
120
 
                continue
121
 
            if p in applied_patches:
122
 
                to_unapply.append(p)
123
 
        if to_unapply == []:
124
 
            return
125
 
        trace.note(gettext("Unapplying %d quilt patches."), len(to_unapply))
126
 
        for p in to_unapply:
127
 
            quilt.pop(p)
128
 
 
129
 
 
130
 
def start_commit_quilt_patches(tree, policy):
131
 
    quilt = QuiltPatches.find(tree)
132
 
    if quilt is None:
133
 
        return
134
 
    applied_patches = quilt.applied()
135
 
    unapplied_patches = quilt.unapplied()
136
 
    if policy is None:
137
 
        # No policy set - just warn about having both applied and unapplied
138
 
        # patches.
139
 
        if applied_patches and unapplied_patches:
140
 
            trace.warning(
141
 
                gettext("Committing with %d patches applied and %d patches unapplied."),
142
 
                len(applied_patches), len(unapplied_patches))
143
 
    elif policy == "applied":
144
 
        quilt.push_all()
145
 
    elif policy == "unapplied":
146
 
        quilt.pop_all()