git - Automatically track certain files -
long story short: we're using jupyter notebooks (.ipynb
files) , have set jupyter config save .py
copies (à la this answer on so, purposes of nicer git diffs).
so every time save .ipynb
file, saves .py
version, otherwise same filename. if no .py
version existed, creates new one.
is possible automatically add / track these newly created .py
files, perhaps putting in git config?
edit
so may possible using git pre-commit hook, having read it. however, don't know enough write hook scratch.
to reiterate want: save foo_bar.ipynb
, automatically creating foo_bar.py
. want pre-commit hook add foo_bar.py
if do, e.g., git commit -a
. to emphasise, don't want add old .py
files, ones have same filename existing .ipynb
file.
write script adds new , updated files git , commits them. run manually or cron job. better, hook tool generates files, if possible, run every time tool saves files or when exits.
the script simple as:
# change directory cd /path/to/the/directory/where/the/py/files/are/saved # set 1 when commit needed commit=0 # check .ipynb files in *.ipynb; # generate name of corresponding .py file p=${i//.ipybn}.py # if .py file exists if [ -f $p ]; # add committed; doesn't hurt if not modified git add $p # remember have commit @ end commit=1 fi done # avoid running "git commit" when nothing staged if [ $commit -eq 1 ]; # commit, generate unique (not useful) commit message. git commit -m "automatic commit on $(date +'%y-%m-%d %h:%i:%s')" fi
the code above assumes .ipynb
files stored in single directory (no subdirectories) , corresponding .py
files stored in same directory.
if .ipynb
files stored in multiple directories replace for
line with:
for in $(find . -name \*.ipynb);
if .py
file not stored in same directory corresponding .ipybn
file have change line p=${i//.ipybn}.py
.
multiple conditions can verified before staging file.
Comments
Post a Comment