33"""
44import os
55import re
6+ import shlex
67import subprocess
78from urllib .parse import unquote
89
@@ -143,9 +144,10 @@ class Git:
143144 A single parent class containing all of the individual git methods in it.
144145 """
145146
146- def __init__ (self , contents_manager ):
147+ def __init__ (self , contents_manager , config = None ):
147148 self .contents_manager = contents_manager
148149 self .root_dir = os .path .expanduser (contents_manager .root_dir )
150+ self ._config = config
149151
150152 async def config (self , top_repo_path , ** kwargs ):
151153 """Get or set Git options.
@@ -302,7 +304,7 @@ async def status(self, current_path):
302304 for line in filter (lambda l : len (l ) > 0 , strip_and_split (text_output )):
303305 diff , name = line .rsplit ("\t " , maxsplit = 1 )
304306 are_binary [name ] = diff .startswith ("-\t -" )
305-
307+
306308 result = []
307309 line_iterable = (line for line in strip_and_split (my_output ) if line )
308310 for line in line_iterable :
@@ -873,13 +875,50 @@ async def init(self, current_path):
873875 Execute git init command & return the result.
874876 """
875877 cmd = ["git" , "init" ]
878+ cwd = os .path .join (self .root_dir , current_path )
876879 code , _ , error = await execute (
877- cmd , cwd = os . path . join ( self . root_dir , current_path )
880+ cmd , cwd = cwd
878881 )
879882
883+ actions = None
884+ if code == 0 :
885+ code , actions = await self ._maybe_run_actions ('post_init' , cwd )
886+
880887 if code != 0 :
881- return {"code" : code , "command" : " " .join (cmd ), "message" : error }
882- return {"code" : code }
888+ return {"code" : code , "command" : " " .join (cmd ), "message" : error , "actions" : actions }
889+ return {"code" : code , "actions" : actions }
890+
891+ async def _maybe_run_actions (self , name , cwd ):
892+ code = 0
893+ actions = None
894+ if self ._config and name in self ._config .actions :
895+ actions = []
896+ actions_list = self ._config .actions [name ]
897+ for action in actions_list :
898+ try :
899+ # We trust the actions as they were passed via a config and not the UI
900+ code , stdout , stderr = await execute (
901+ shlex .split (action ), cwd = cwd
902+ )
903+ actions .append ({
904+ 'cmd' : action ,
905+ 'code' : code ,
906+ 'stdout' : stdout ,
907+ 'stderr' : stderr
908+ })
909+ # After any failure, stop
910+ except Exception as e :
911+ code = 1
912+ actions .append ({
913+ 'cmd' : action ,
914+ 'code' : 1 ,
915+ 'stdout' : None ,
916+ 'stderr' : 'Exception: {}' .format (e )
917+ })
918+ if code != 0 :
919+ break
920+
921+ return code , actions
883922
884923 def _is_remote_branch (self , branch_reference ):
885924 """Check if given branch is remote branch by comparing with 'remotes/',
@@ -1066,7 +1105,7 @@ async def _is_binary(self, filename, ref, top_repo_path):
10661105
10671106 Returns:
10681107 bool: Is file binary?
1069-
1108+
10701109 Raises:
10711110 HTTPError: if git command failed
10721111 """
@@ -1153,7 +1192,7 @@ async def ignore(self, top_repo_path, file_path):
11531192
11541193 async def version (self ):
11551194 """Return the Git command version.
1156-
1195+
11571196 If an error occurs, return None.
11581197 """
11591198 command = ["git" , "--version" ]
@@ -1162,12 +1201,12 @@ async def version(self):
11621201 version = GIT_VERSION_REGEX .match (output )
11631202 if version is not None :
11641203 return version .group ('version' )
1165-
1204+
11661205 return None
11671206
11681207 async def tags (self , current_path ):
11691208 """List all tags of the git repository.
1170-
1209+
11711210 current_path: str
11721211 Git path repository
11731212 """
@@ -1180,7 +1219,7 @@ async def tags(self, current_path):
11801219
11811220 async def tag_checkout (self , current_path , tag ):
11821221 """Checkout the git repository at a given tag.
1183-
1222+
11841223 current_path: str
11851224 Git path repository
11861225 tag : str
0 commit comments