From bd2f6a07335bbe202b4d3283b6b041b1338a1438 Mon Sep 17 00:00:00 2001 From: Devin Stein Date: Thu, 20 Jun 2019 13:22:05 -0700 Subject: [PATCH] Create pre-commit hook for nbstripout --- .gitignore | 14 ++++++++++++++ .pre-commit-hooks.yaml | 6 ++++++ nbstripout.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 .gitignore create mode 100644 .pre-commit-hooks.yaml create mode 100644 nbstripout.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd94216 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +*.egg +*.egg-info/ +*.eggs/ +*.pyc +.*.swp +.travis-solo/ +MANIFEST +README.html +__pycache__/ +build/ +dist/ +htmlcov/ +.coverage +.idea/ diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..f08bd24 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: nbstripout + name: Strip Outputs from Notebooks + description: Strip outputs from all new notebooks + entry: nbstripout + language: script + files: (^|/).+\.ipynb$ diff --git a/nbstripout.sh b/nbstripout.sh new file mode 100644 index 0000000..7975855 --- /dev/null +++ b/nbstripout.sh @@ -0,0 +1,30 @@ +#!/bin/sh +# +# strip output of IPython Notebooks +# add this as `.git/hooks/pre-commit` +# to run every time you commit a notebook +# +# requires `nbstripout` to be available on your PATH +# + +if git rev-parse --verify HEAD >/dev/null 2>&1; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +# Find notebooks to be committed +( +IFS=' +' +NBS=`git diff-index --cached $against --name-only --diff-filter=A | grep '.ipynb' | uniq` + +for NB in $NBS ; do + echo "Removing outputs from $NB" + nbstripout "$NB" + git add "$NB" +done +) + +exec git diff-index --check --cached $against --