Skip to content

Commit

Permalink
Merge pull request #26 from minrk/difftool
Browse files Browse the repository at this point in the history
Add git difftool prototype
  • Loading branch information
Martin Sandve Alnæs committed Mar 8, 2016
2 parents 24a29c2 + f95b939 commit 5af7f14
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
102 changes: 102 additions & 0 deletions nbdime/gitdifftool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python
"""A git difftool plugin for notebooks.
Uses nbdime to display diffs of notebooks instead of plain text diffs of JSON.
Other files are displayed via `git diff`.
Enable in your global git config with:
git-nbdifftool config --enable [--global]
Use with:
git difftool [<commit> [<commit>]]
"""
import os
import sys
from subprocess import check_call, check_output, CalledProcessError

from . import nbdiffapp

def enable(global_=False):
"""Enable nbdime git difftool"""
cmd = ['git', 'config']
if global_:
cmd.append('--global')

check_call(cmd + ['difftool.nbdime.cmd', 'git-nbdifftool diff "$LOCAL" "$REMOTE"'])
try:
previous = check_output(cmd + ['diff.tool']).decode('utf8', 'replace').strip()
except CalledProcessError:
previous = None
else:
if previous != 'nbdime':
check_call(cmd + ['difftool.nbdime.previous', previous])
check_call(cmd + ['difftool.prompt', 'false'])
check_call(cmd + ['diff.tool', 'nbdime'])


def disable(global_=False):
"""Disable nbdime git difftool"""
cmd = ['git', 'config']
if global_:
cmd.append('--global')
try:
previous = check_output(cmd + ['difftool.nbdime.previous']).decode('utf8', 'replace').strip()
except CalledProcessError:
check_call(cmd + ['--unset', 'diff.tool'])
else:
check_call(cmd + ['diff.tool', previous])


def show_diff(before, after):
"""Run the difftool
If we are diffing a notebook, show the diff via nbdiff.
Otherwise, call out to `git diff`.
"""
# TODO: handle /dev/null (Windows equivalent?) for new or deleted files
if before.endswith('.ipynb') or after.endswith('ipynb'):
nbdiffapp.main_diff(before, after)
else:
os.execvp('git', ['git', 'diff', before, after])


def main():
import argparse
parser = argparse.ArgumentParser('git-nbdifftool', description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subparsers = parser.add_subparsers(dest='subcommand')

diff_parser = subparsers.add_parser('diff',
description="The actual entrypoint for the diff tool. Git will call this."
)
diff_parser.add_argument('local')
diff_parser.add_argument('remote')

config = subparsers.add_parser('config',
description="Configure git to use nbdime via `git difftool`")
config.add_argument('--global', action='store_true', dest='global_',
help="configure your global git config instead of the current repo"
)
enable_disable = config.add_mutually_exclusive_group(required=True)
enable_disable.add_argument('--enable', action='store_const',
dest='config_func', const=enable,
help="enable nbdime difftool via git config"
)
enable_disable.add_argument('--disable', action='store_const',
dest='config_func', const=disable,
help="disable nbdime difftool via git config"
)
opts = parser.parse_args()
if opts.subcommand == 'diff':
show_diff(opts.local, opts.remote)
elif opts.subcommand == 'config':
opts.config_func(opts.global_)
else:
parser.print_help()
sys.exit(1)

if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions scripts/git-nbdifftool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python
from nbdime.gitdifftool import main

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
'nbdiff = nbdime.nbdiffapp:main',
'nbpatch= nbdime.nbpatchapp:main',
'nbmerge = nbdime.nbmergeapp:main',
'git-nbdifftool = nbdime.gitdifftool:main',
]
}
setup_args.pop('scripts', None)
Expand Down

0 comments on commit 5af7f14

Please sign in to comment.