File: difftool.py

package info (click to toggle)
git-cola 4.14.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,812 kB
  • sloc: python: 37,625; sh: 298; makefile: 223; xml: 102; tcl: 62
file content (418 lines) | stat: -rw-r--r-- 13,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os

from qtpy import QtWidgets
from qtpy.QtCore import Qt

from . import cmds
from . import core
from . import gitcmds
from . import hotkeys
from . import icons
from . import qtutils
from . import utils
from .git import EMPTY_TREE_OID
from .i18n import N_
from .interaction import Interaction
from .models import dag
from .widgets import completion
from .widgets import defs
from .widgets import filetree
from .widgets import standard


class LaunchDifftool(cmds.ContextCommand):
    """Launch "git difftool" with the currently selected files"""

    @staticmethod
    def name():
        return N_('Launch Diff Tool')

    def do(self):
        s = self.selection.selection()
        if s.unmerged:
            paths = s.unmerged
            if utils.is_win32():
                core.fork(['git', 'mergetool', '--no-prompt', '--'] + paths)
            else:
                cfg = self.cfg
                cmd = cfg.terminal()
                argv = utils.shell_split(cmd)

                terminal = os.path.basename(argv[0])
                shellquote_terms = {'xfce4-terminal'}
                shellquote_default = terminal in shellquote_terms

                mergetool = ['git', 'mergetool', '--no-prompt', '--']
                mergetool.extend(paths)
                needs_shellquote = cfg.get(
                    'cola.terminalshellquote', shellquote_default
                )

                if needs_shellquote:
                    argv.append(core.list2cmdline(mergetool))
                else:
                    argv.extend(mergetool)

                core.fork(argv)
        else:
            difftool_run(self.context)


class Difftool(standard.Dialog):
    def __init__(
        self,
        context,
        parent,
        a=None,
        b=None,
        expr=None,
        title=None,
        hide_expr=False,
        focus_tree=False,
        detect_renames=False,
    ):
        """Show files with differences and launch difftool"""

        standard.Dialog.__init__(self, parent=parent)

        self.context = context
        self.a = a
        self.b = b
        self.diff_expr = expr
        self.detect_renames = detect_renames

        if title is None:
            title = N_('git-cola diff')

        self.setWindowTitle(title)
        self.setWindowModality(Qt.WindowModal)

        self.expr = completion.GitRefLineEdit(context, parent=self)
        if expr is not None:
            self.expr.setText(expr)

        if expr is None or hide_expr:
            self.expr.hide()

        self.tree = filetree.FileTree(parent=self)

        self.diff_button = qtutils.create_button(
            text=N_('Compare'), icon=icons.diff(), enabled=False, default=True
        )
        self.diff_button.setShortcut(hotkeys.DIFF)

        self.diff_all_button = qtutils.create_button(
            text=N_('Compare All'), icon=icons.diff()
        )
        self.edit_button = qtutils.edit_button()
        self.edit_button.setShortcut(hotkeys.EDIT)

        self.close_button = qtutils.close_button()

        self.button_layout = qtutils.hbox(
            defs.no_margin,
            defs.spacing,
            qtutils.STRETCH,
            self.close_button,
            self.edit_button,
            self.diff_all_button,
            self.diff_button,
        )

        self.main_layout = qtutils.vbox(
            defs.margin, defs.spacing, self.expr, self.tree, self.button_layout
        )
        self.setLayout(self.main_layout)

        self.tree.itemSelectionChanged.connect(self.tree_selection_changed)
        self.tree.itemDoubleClicked.connect(self.tree_double_clicked)
        self.tree.up.connect(self.focus_input)

        self.expr.textChanged.connect(self.text_changed)

        self.expr.activated.connect(self.focus_tree)
        self.expr.down.connect(self.focus_tree)
        self.expr.enter.connect(self.focus_tree)

        qtutils.connect_button(self.diff_button, self.diff)
        qtutils.connect_button(self.diff_all_button, lambda: self.diff(dir_diff=True))
        qtutils.connect_button(self.edit_button, self.edit)
        qtutils.connect_button(self.close_button, self.close)

        qtutils.add_action(self, 'Focus Input', self.focus_input, hotkeys.FOCUS)
        qtutils.add_action(
            self,
            'Diff All',
            lambda: self.diff(dir_diff=True),
            hotkeys.CTRL_ENTER,
            hotkeys.CTRL_RETURN,
        )
        qtutils.add_close_action(self)

        self.init_state(None, self.resize_widget, parent)

        self.refresh()
        if focus_tree:
            self.focus_tree()

    def resize_widget(self, parent):
        """Set the initial size of the widget"""
        width, height = qtutils.default_size(parent, 720, 420)
        self.resize(width, height)

    def focus_tree(self):
        """Focus the files tree"""
        self.tree.setFocus()

    def focus_input(self):
        """Focus the expression input"""
        self.expr.setFocus()

    def text_changed(self, txt):
        self.diff_expr = txt
        self.refresh()

    def refresh(self):
        """Redo the diff when the expression changes"""
        if self.diff_expr is not None:
            self.diff_arg = utils.shell_split(self.diff_expr)
        elif self.b is None:
            self.diff_arg = [self.a]
        else:
            if self.b == dag.WORKTREE:
                if self.a == dag.STAGE:
                    self.diff_arg = []
                else:
                    self.diff_arg = [self.a]
            elif self.b == dag.STAGE:
                if self.a == dag.WORKTREE:
                    self.diff_arg = ['--cached']
                else:
                    self.diff_arg = ['--cached', self.a]
            elif self.a == dag.WORKTREE:
                self.diff_arg = [self.b]
            elif self.a == dag.STAGE:
                self.diff_arg = ['--cached', self.b]
            else:
                self.diff_arg = [self.a, self.b]
        self.refresh_filenames()

    def refresh_filenames(self):
        context = self.context
        if self.a and self.b is None:
            filenames = gitcmds.diff_index_filenames(context, self.a)
        else:
            filenames = gitcmds.diff(context, self.diff_arg)
        self.tree.set_filenames(filenames, select=True)

    def tree_selection_changed(self):
        has_selection = self.tree.has_selection()
        self.diff_button.setEnabled(has_selection)
        self.diff_all_button.setEnabled(has_selection)

    def tree_double_clicked(self, item, _column):
        path = filetree.filename_from_item(item)
        left, right = self._left_right_args()
        difftool_launch(
            self.context,
            left=left,
            right=right,
            paths=[path],
            detect_renames=self.detect_renames,
        )

    def diff(self, dir_diff=False):
        paths = self.tree.selected_filenames()
        left, right = self._left_right_args()
        difftool_launch(
            self.context,
            left=left,
            right=right,
            paths=paths,
            dir_diff=dir_diff,
            detect_renames=self.detect_renames,
        )

    def _left_right_args(self):
        if self.diff_arg:
            left = self.diff_arg[0]
        else:
            left = None
        if len(self.diff_arg) > 1:
            right = self.diff_arg[1]
        else:
            right = None
        return (left, right)

    def edit(self):
        paths = self.tree.selected_filenames()
        cmds.do(cmds.Edit, self.context, paths)


def diff_commits(context, parent, a, b, detect_renames=False):
    """Show a dialog for diffing two commits"""
    dlg = Difftool(context, parent, a=a, b=b, detect_renames=detect_renames)
    dlg.show()
    dlg.raise_()
    return dlg.exec_() == QtWidgets.QDialog.Accepted


def diff_expression(
    context, parent, expr, create_widget=False, hide_expr=False, focus_tree=False
):
    """Show a diff dialog for diff expressions"""
    dlg = Difftool(
        context, parent, expr=expr, hide_expr=hide_expr, focus_tree=focus_tree
    )
    if create_widget:
        return dlg
    dlg.show()
    dlg.raise_()
    return dlg.exec_() == QtWidgets.QDialog.Accepted


def difftool_run(context):
    """Start a default difftool session"""
    selection = context.selection
    files = selection.group()
    if not files:
        return
    s = selection.selection()
    head = context.model.head
    difftool_launch_with_head(context, files, bool(s.staged), head)


def difftool_launch_with_head(context, filenames, staged, head):
    """Launch difftool against the provided head"""
    if head == 'HEAD':
        left = None
    else:
        left = head
    difftool_launch(context, left=left, staged=staged, paths=filenames)


def difftool_launch(
    context,
    left=None,
    right=None,
    paths=None,
    staged=False,
    dir_diff=False,
    left_take_magic=False,
    left_take_parent=False,
    detect_renames=False,
):
    """Launches 'git difftool' with given parameters

    :param left: first argument to difftool
    :param right: second argument to difftool_args
    :param paths: paths to diff
    :param staged: activate `git difftool --staged`
    :param dir_diff: activate `git difftool --dir-diff`
    :param left_take_magic: whether to append the magic "^!" diff expression
    :param left_take_parent: whether to append the first-parent ~ for diffing

    """
    difftool_args = ['git', 'difftool', '--no-prompt']
    if staged:
        difftool_args.append('--cached')
    if dir_diff:
        difftool_args.append('--dir-diff')

    if left:
        original_left = left
        if left_take_parent or left_take_magic:
            suffix = '^!' if left_take_magic else '~'
            # Check root commit (no parents and thus cannot execute '~')
            git = context.git
            if left in (dag.STAGE, dag.WORKTREE):
                check_ref = 'HEAD'
            else:
                check_ref = left
            status, out, err = git.rev_list(
                check_ref, parents=True, n=1, _readonly=True
            )
            Interaction.log_status(status, out, err)
            if status:
                raise OSError(f'git rev-list {left} command failed')

            if len(out.split()) >= 2:
                # Commit has a parent, so we can take its child as requested
                if left not in (dag.STAGE, dag.WORKTREE):
                    left += suffix
            else:
                # No parent, assume it's the root commit, so we have to diff
                # against the empty tree.
                left = EMPTY_TREE_OID
                if not right and left_take_magic:
                    right = left
        # Commit has a parent, so we can take its child as requested
        if original_left not in (dag.STAGE, dag.WORKTREE):
            difftool_args.append(left)

    if right and right not in (dag.STAGE, dag.WORKTREE):
        difftool_args.append(right)

    all_names = _get_renamed_paths(context, left, right, paths, detect_renames)
    if all_names:
        paths.extend(all_names)

    if paths:
        difftool_args.append('--')
        difftool_args.extend(paths)

    runtask = context.runtask
    if runtask:
        Interaction.async_command(N_('Difftool'), difftool_args, runtask)
    else:
        core.fork(difftool_args)


def _get_renamed_paths(context, left, right, paths, detect_renames):
    """Get filenames as they existed beyond a rename

    Use ``git log --follow --format= --name-only -- <path>`` to to discover the
    filenames as they existed in older commits. This is a slow operation when the
    commit range is large.
    """
    all_names = set()
    if (
        detect_renames
        and len(paths) == 1
        and left
        and left not in (dag.STAGE, dag.WORKTREE)
        and right
        and right not in (dag.STAGE, dag.WORKTREE)
    ):
        current_name = paths[0]

        # We have to check in both left->right and right->left directions because we
        # may be performing either "Diff selected to this..." or
        # "Diff this to selected...", and left/right flips directions depending on which
        # is chosen. We have to log starting from the parent commit of the start range
        # in order to include the starting commit. The starting commit may be the only
        # commit that contains the original filename.
        for rev_arg in (
            f'{left}^..{right}',
            f'{right}^..{left}',
        ):
            status, out, _ = context.git.log(
                rev_arg,
                '--',
                current_name,
                follow=True,
                format='',
                name_only=True,
                z=True,
                _readonly=True,
            )
            if status == 0:
                out = out[:-1]  # Strip the final NULL terminator.
                if out:
                    all_names.update(out.split('\0'))
        try:
            all_names.remove(current_name)
        except KeyError:
            pass

    return all_names