33import os , json , git , urllib , requests
44from git import Repo , GitCommandError
55from subprocess import check_output
6+ import subprocess
67
78class GitCommitHandler (IPythonHandler ):
89
@@ -32,6 +33,8 @@ def put(self):
3233 data = json .loads (self .request .body .decode ('utf-8' ))
3334 filename = urllib .parse .unquote (data ['filename' ])
3435 msg = data ['msg' ]
36+ commit_only_source = data ['commit_only_source' ]
37+
3538
3639 # get current directory (to return later)
3740 cwd = os .getcwd ()
@@ -54,8 +57,13 @@ def put(self):
5457 # commit current notebook
5558 # client will sent pathname containing git directory; append to git directory's parent
5659 try :
60+ if commit_only_source :
61+ subprocess .run (['jupyter' , 'nbconvert' , '--to' , 'script' , str (os .environ .get ('GIT_PARENT_DIR' ) + "/" + os .environ .get ('GIT_REPO_NAME' ) + filename )])
62+ filename = str (os .environ .get ('GIT_PARENT_DIR' ) + "/" + os .environ .get ('GIT_REPO_NAME' ) + filename .replace ('ipynb' , 'py' ))
63+
5764 print (repo .git .add (str (os .environ .get ('GIT_PARENT_DIR' ) + "/" + os .environ .get ('GIT_REPO_NAME' ) + filename )))
58- print (repo .git .commit ( a = True , m = "{}\n \n Updated {}" .format (msg , filename ) ))
65+ print (repo .git .commit ( a = False , m = "{}\n \n Updated {}" .format (msg , filename ) ))
66+
5967 except GitCommandError as e :
6068 print (e )
6169 self .error_and_return (cwd , "Could not commit changes to notebook: {}" .format (git_dir_parent + filename ))
0 commit comments