Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dag: add diff tool option #473

Merged
merged 3 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions cola/difftool.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cola import qtutils
from cola import gitcmds
from cola.i18n import N_
from cola.interaction import Interaction
from cola.models import main
from cola.models import selection
from cola.widgets import completion
Expand All @@ -28,20 +29,40 @@ def run():


def launch_with_head(filenames, staged, head):
args = []
if staged:
args.append('--cached')
if head != 'HEAD':
args.append(head)
args.append('--')
args.extend(filenames)
launch(args)
launch(left=head if head != 'HEAD' else None, staged=staged, paths=filenames)


def launch(args):
"""Launches 'git difftool' with args"""
def launch(left=None, right=None, paths=None, left_take_parent=False, staged=False):
"""Launches 'git difftool' with given parameters"""
difftool_args = ['git', 'difftool', '--no-prompt']
difftool_args.extend(args)
if staged:
difftool_args.append('--cached')
if left:
if left_take_parent:
# Check root commit (no parents and thus cannot execute '~')
proc = core.start_command(['git', 'rev-list', '--parents', '-n', '1', left])
out, err = proc.communicate()
status = proc.returncode
Interaction.log_status(status, out, err)
if status:
raise Exception('git rev-list command failed')
line = out.splitlines()[0]
if len(line.split()) >= 2:
# Commit has a parent, so we can take its child as requested
left += '~'
else:
# No parent, assume it's the root commit, so we have to diff against an empty tree
left = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'

difftool_args.append(left)
if right:
difftool_args.append(right)
if paths:
difftool_args.append('--')
if isinstance(paths, (tuple, list)):
difftool_args.extend(paths)
else:
difftool_args.append(paths) # paths is a string
core.fork(difftool_args)


Expand Down Expand Up @@ -160,9 +181,13 @@ def tree_selection_changed(self):

def tree_double_clicked(self, item, column):
path = self.tree.filename_from_item(item)
launch(self.diff_arg + ['--', path])
launch(left=self.diff_arg[0] if self.diff_arg else None,
right=self.diff_arg[1] if len(self.diff_arg) > 1 else None,
paths=path)

def diff(self):
paths = self.tree.selected_filenames()
for path in paths:
launch(self.diff_arg + ['--', ustr(path)])
launch(left=self.diff_arg[0] if self.diff_arg else None,
right=self.diff_arg[1] if len(self.diff_arg) > 1 else None,
paths=ustr(path))
2 changes: 1 addition & 1 deletion cola/widgets/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def difftool_predecessor(self, paths):
if not commits:
return
commit = commits[0]
difftool.launch([commit, '--'] + paths)
difftool.launch(left=commit, paths=paths)


class BrowseModel(object):
Expand Down
7 changes: 4 additions & 3 deletions cola/widgets/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def compare(self, *args):
def compare_file(self, filename):
"""Initiates the difftool session"""
if self.use_sandbox:
arg = self.diff_arg
left = self.diff_arg[0]
right = self.diff_arg[1] if len(self.diff_arg) > 1 else None
else:
arg = (self.start, self.end)
difftool.launch(arg + ('--', filename))
left, right = self.start, self.end
difftool.launch(left=left, right=right, paths=filename)
17 changes: 16 additions & 1 deletion cola/widgets/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from PyQt4.QtCore import QRectF

from cola import cmds
from cola import core
from cola import difftool
from cola import observable
from cola import qtutils
Expand Down Expand Up @@ -248,6 +249,12 @@ def goto(self, finder):
if found:
self.select([found.commit.sha1])

def selected_commit_range(self):
selected_items = self.selected_items()
if not selected_items:
return None, None
return selected_items[-1].commit.sha1, selected_items[0].commit.sha1

def set_selecting(self, selecting):
self.selecting = selecting

Expand Down Expand Up @@ -361,6 +368,8 @@ def __init__(self, model, ctx, parent=None, settings=None):
self.notifier.add_observer(refs_updated, self.display)
self.notifier.add_observer(filelist.HISTORIES_SELECTED,
self.histories_selected)
self.notifier.add_observer(filelist.DIFFTOOL_SELECTED,
self.difftool_selected)
self.notifier.add_observer(diff.COMMITS_SELECTED, self.commits_selected)

self.treewidget = CommitTreeWidget(notifier, self)
Expand Down Expand Up @@ -591,7 +600,7 @@ def resize_to_desktop(self):
def diff_commits(self, a, b):
paths = self.ctx.paths()
if paths:
difftool.launch([a, b, '--'] + paths)
difftool.launch(left=a, right=b, paths=paths)
else:
difftool.diff_commits(self, a, b)

Expand All @@ -612,6 +621,12 @@ def histories_selected(self, histories):
self.revtext.setText(text)
self.display()

def difftool_selected(self, files):
bottom, top = self.treewidget.selected_commit_range()
if not top:
return
difftool.launch(left=bottom, left_take_parent=True, right=top, paths=list(files))


class ReaderThread(QtCore.QThread):
begin = SIGNAL('begin')
Expand Down
9 changes: 9 additions & 0 deletions cola/widgets/filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cola.widgets.diff import FILES_SELECTED

HISTORIES_SELECTED = 'HISTORIES_SELECTED'
DIFFTOOL_SELECTED = 'DIFFTOOL_SELECTED'

class FileWidget(TreeWidget):

Expand Down Expand Up @@ -71,8 +72,16 @@ def contextMenuEvent(self, event):
menu = QtGui.QMenu(self)
menu.addAction(qtutils.add_action(self, N_('Show history'),
self.show_file_history))
menu.addAction(qtutils.add_action(self, N_('Launch Diff Tool'),
self.show_file_diff, 'Ctrl+D'))

menu.exec_(self.mapToGlobal(event.pos()))

def show_file_diff(self):
items = self.selected_items()
self.notifier.notify_observers(DIFFTOOL_SELECTED,
[i.file_name for i in items])

def show_file_history(self):
items = self.selected_items()
self.notifier.notify_observers(HISTORIES_SELECTED,
Expand Down