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/git-clean-filter.py
[filter "ipynb-outputs"]
clean = scripts/git-clean-filter.py --clean-outputs
[filter "ipynb-codecells"]
clean = scripts/git-clean-filter.py --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':
cells.append(cell)
if args.add_empty:
cells.append(empty)
continue
if args.clean_code_cells:
continue
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']
cells.append(cell)
js['cells'] = cells
print(json.dumps(js, sort_keys=True, indent=1))
#!/bin/bash
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"
done
git stash pop -q
fi
exit 0
#!/bin/bash
cd "$(git rev-parse --show-toplevel)"
git config include.path ../.gitconfig
ln -sfr scripts/pre-commit.sh .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