/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/mergetools.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd.
 
2
#
 
3
# This program 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
# This program 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 this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Utility functions for managing external merge tools such as kdiff3."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import os
 
22
import shutil
 
23
import subprocess
 
24
import sys
 
25
import tempfile
 
26
 
 
27
from .lazy_import import lazy_import
 
28
lazy_import(globals(), """
 
29
from breezy import (
 
30
    cmdline,
 
31
    osutils,
 
32
    trace,
 
33
)
 
34
""")
 
35
 
 
36
 
 
37
known_merge_tools = {
 
38
    'bcompare': 'bcompare {this} {other} {base} {result}',
 
39
    'kdiff3': 'kdiff3 {base} {this} {other} -o {result}',
 
40
    'xdiff': 'xxdiff -m -O -M {result} {this} {base} {other}',
 
41
    'meld': 'meld {base} {this_temp} {other}',
 
42
    'opendiff': 'opendiff {this} {other} -ancestor {base} -merge {result}',
 
43
    'winmergeu': 'winmergeu {result}',
 
44
}
 
45
 
 
46
 
 
47
def check_availability(command_line):
 
48
    cmd_list = cmdline.split(command_line)
 
49
    exe = cmd_list[0]
 
50
    if sys.platform == 'win32':
 
51
        exe = _get_executable_path(exe)
 
52
        if exe is None:
 
53
            return False
 
54
        base, ext = os.path.splitext(exe)
 
55
        path_ext = [s.lower()
 
56
                    for s in os.getenv('PATHEXT', '').split(os.pathsep)]
 
57
        return os.path.exists(exe) and ext in path_ext
 
58
    else:
 
59
        return (os.access(exe, os.X_OK) or
 
60
                osutils.find_executable_on_path(exe) is not None)
 
61
 
 
62
 
 
63
def invoke(command_line, filename, invoker=None):
 
64
    """Invokes the given merge tool command line, substituting the given
 
65
    filename according to the embedded substitution markers. Optionally, it
 
66
    will use the given invoker function instead of the default
 
67
    subprocess_invoker.
 
68
    """
 
69
    if invoker is None:
 
70
        invoker = subprocess_invoker
 
71
    cmd_list = cmdline.split(command_line)
 
72
    exe = _get_executable_path(cmd_list[0])
 
73
    if exe is not None:
 
74
        cmd_list[0] = exe
 
75
    args, tmp_file = _subst_filename(cmd_list, filename)
 
76
 
 
77
    def cleanup(retcode):
 
78
        if tmp_file is not None:
 
79
            if retcode == 0:  # on success, replace file with temp file
 
80
                shutil.move(tmp_file, filename)
 
81
            else:  # otherwise, delete temp file
 
82
                os.remove(tmp_file)
 
83
    return invoker(args[0], args[1:], cleanup)
 
84
 
 
85
 
 
86
def _get_executable_path(exe):
 
87
    if os.path.isabs(exe):
 
88
        return exe
 
89
    return osutils.find_executable_on_path(exe)
 
90
 
 
91
 
 
92
def _subst_filename(args, filename):
 
93
    subst_names = {
 
94
        'base': filename + u'.BASE',
 
95
        'this': filename + u'.THIS',
 
96
        'other': filename + u'.OTHER',
 
97
        'result': filename,
 
98
    }
 
99
    tmp_file = None
 
100
    subst_args = []
 
101
    for arg in args:
 
102
        if '{this_temp}' in arg and 'this_temp' not in subst_names:
 
103
            fh, tmp_file = tempfile.mkstemp(u"_bzr_mergetools_%s.THIS" %
 
104
                                            os.path.basename(filename))
 
105
            trace.mutter('fh=%r, tmp_file=%r', fh, tmp_file)
 
106
            os.close(fh)
 
107
            shutil.copy(filename + u".THIS", tmp_file)
 
108
            subst_names['this_temp'] = tmp_file
 
109
        arg = _format_arg(arg, subst_names)
 
110
        subst_args.append(arg)
 
111
    return subst_args, tmp_file
 
112
 
 
113
 
 
114
# This would be better implemented using format() from python 2.6
 
115
def _format_arg(arg, subst_names):
 
116
    arg = arg.replace('{base}', subst_names['base'])
 
117
    arg = arg.replace('{this}', subst_names['this'])
 
118
    arg = arg.replace('{other}', subst_names['other'])
 
119
    arg = arg.replace('{result}', subst_names['result'])
 
120
    if 'this_temp' in subst_names:
 
121
        arg = arg.replace('{this_temp}', subst_names['this_temp'])
 
122
    return arg
 
123
 
 
124
 
 
125
def subprocess_invoker(executable, args, cleanup):
 
126
    retcode = subprocess.call([executable] + args)
 
127
    cleanup(retcode)
 
128
    return retcode