Commit 545381c7 authored by Christoph Ruegge's avatar Christoph Ruegge
Browse files

Filter and hook setup

parent b84d3c05
*.ipynb merge=text
*.ipynb filter=ipynb-generic
exercises/*.ipynb filter=ipynb-codecells
exercises/solutions/*.ipynb filter=ipynb-outputs
[filter "ipynb-generic"]
clean = scripts/
[filter "ipynb-outputs"]
clean = scripts/ --clean-outputs
[filter "ipynb-codecells"]
clean = scripts/ --clean-code-cells --add-empty
#!/usr/bin/env python3
import argparse
import json
import sys
p = argparse.ArgumentParser()
p.add_argument('--leave-metadata', action='store_true')
p.add_argument('--leave-exec-count', action='store_true')
p.add_argument('--clean-outputs', action='store_true')
p.add_argument('--clean-code-cells', action='store_true')
p.add_argument('--add-empty', action='store_true')
args = p.parse_args()
empty = {
'cell_type': 'code',
'execution_count': 1,
'metadata': {
'collapsed': False
'source': [],
'outputs': []
js = json.load(sys.stdin)
cells = []
for cell in js['cells']:
if cell['cell_type'] != 'code':
if args.add_empty:
if args.clean_code_cells:
if not args.leave_metadata:
cell['metadata'] = empty['metadata']
if not args.leave_exec_count:
cell['execution_count'] = empty['execution_count']
if args.clean_outputs:
cell['outputs'] = empty['outputs']
js['cells'] = cells
print(json.dumps(js, sort_keys=True, indent=1))
set -e
exec 2>&1
cd "$(git rev-parse --show-toplevel)"
if git diff --cached --name-only | grep -q '^exercises/'; then
git stash -q --keep-index
rm -f exercises/*.ipynb
for nb in exercises/solutions/*.ipynb; do
target="exercises/$(basename "$nb")"
cat "$nb" | $(git config filter.ipynb-codecells.clean) > "$target"
git add "$target"
git stash pop -q
exit 0
cd "$(git rev-parse --show-toplevel)"
git config include.path ../.gitconfig
ln -sfr scripts/ .git/hooks/pre-commit
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment