-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from minrk/difftool
Add git difftool prototype
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters