Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 56c0191a authored by ROUVREAU Vincent's avatar ROUVREAU Vincent
Browse files

Merge branch '4_pre_commit_git_hook' into 'master'

Update pre-commit git hook and its documentation to clear executed cells from notebooks

Closes #4

See merge request formations/cpp/gettingstartedwithmoderncpp!36
parents 78672720 feac70b7
No related branches found
No related tags found
1 merge request!36Update pre-commit git hook and its documentation to clear executed cells from notebooks
...@@ -313,9 +313,7 @@ ...@@ -313,9 +313,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"scrolled": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"#include <iostream>\n", "#include <iostream>\n",
......
All contributions are welcome. # Contribution guide
# Git hook All contributions are welcome, but please read this first.
## Git hook
If you intend to contribute, please install first the pre-commit hook: If you intend to contribute, please install first the pre-commit hook:
...@@ -8,9 +10,9 @@ If you intend to contribute, please install first the pre-commit hook: ...@@ -8,9 +10,9 @@ If you intend to contribute, please install first the pre-commit hook:
cp Scripts/pre-commit .git/hooks/pre-commit cp Scripts/pre-commit .git/hooks/pre-commit
```` ````
This hook ensure no notebook with executed cells is committed. This hook clears executed cells when committing a notebook.
# Workflow ## Workflow
The project is led through an integration manager workflow, so when you have a contribution to share, just open a merge request. The project is led through an integration manager workflow, so when you have a contribution to share, just open a merge request.
......
...@@ -120,3 +120,4 @@ A link to a BinderHub instance is given at the top of the project page; foresee ...@@ -120,3 +120,4 @@ A link to a BinderHub instance is given at the top of the project page; foresee
## For maintainers ## For maintainers
Informations related to CI are [here](CI.md). Informations related to CI are [here](CI.md).
All contributions are welcome, but please read the [contribution guide](CONTRIBUTING.md) first.
import argparse
import sys
from typing import Optional
from typing import Sequence
def find_executed_cell(filename):
"""Read the file and check whether there is a line with "execution_count" and a non null value."""
stream = open(filename, encoding="utf8", errors='ignore')
executed_cells = [line.strip("\n,\" ") for line in stream if "execution_count" in line and "null" not in line ]
return len(executed_cells)
def main(argv: Optional[Sequence[str]] = None) -> int:
"""First argument in command line is expected to be a list of filenames
As the purpose of this file is to be used as a pre-commit git hook, the typical list are all the files modified by the commit in progress.
If one or more file includes a cell that has been executed, the function returns 1 and print on screen the incriminated file(s).
"""
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to check.')
args = parser.parse_args(argv)
filenames_with_executed_cells = []
for filename in args.filenames:
if find_executed_cell(filename):
filenames_with_executed_cells.append(filename)
if filenames_with_executed_cells:
print("Commit rejected: the following files feature executed cells: \n\t- {}".format("\n\t- ".join(filenames_with_executed_cells)))
return 1
return 0
if __name__ == '__main__':
sys.exit(main())
\ No newline at end of file
...@@ -3,6 +3,12 @@ ...@@ -3,6 +3,12 @@
# This file is expected to be use as pre-commit git hook; copy it in .git/hooks/ # This file is expected to be use as pre-commit git hook; copy it in .git/hooks/
file_list=`git diff --cached --diff-filter=ACM --name-only` file_list=`git diff --cached --diff-filter=ACM --name-only`
root_dir=`git rev-parse --show-toplevel`
/usr/bin/env python3 ${root_dir}/Scripts/check_no_executed_cell.py ${file_list}
for file in ${file_list}
do
if [ "${file##*.}" = "ipynb" ]; then
echo "Clear cells from ${file}"
jupyter nbconvert --clear-output --inplace ${file}
git add ${file}
fi
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment