Mentions légales du service

Skip to content
Snippets Groups Projects
Commit a7f027ea authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Merge branch '2_git_hook' into 'master'

Add a pre-commit git hook to prevent commit of a notebook with executed cells within

Closes #2

See merge request formations/cpp/gettingstartedwithmoderncpp!7
parents 46401ffc 240fe1a3
No related branches found
No related tags found
No related merge requests found
All contributions are welcome.
# Git hook
If you intend to contribute, please install first the pre-commit hook:
````
cp Scripts/pre-commit .git/hooks/pre-commit
````
This hook ensure no notebook with executed cells is committed.
# Workflow
The project is led through an integration manager workflow, so when you have a contribution to share, just open a merge request.
The MR will be reviewed and integrated directly by one of the project managers.
\ No newline at end of file
This directory includes several utilities used in the TPs and demos...
The Dockerfile here is not meant to be used directly bbut the ones for TPs and the demo about third party warnings are bbuilt upon it.
The Dockerfile here is not meant to be used directly but the ones for TPs and the demo about third party warnings are built upon it.
To build it, run:
......
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
#!/bin/bash
# This file is expected to be use as pre-commit git hook; copy it in .git/hooks/
file_list=`git diff --cached --name-only`
root_dir=`git rev-parse --show-toplevel`
/usr/bin/env python3 ${root_dir}/Scripts/check_no_executed_cell.py ${file_list}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment