66use App \VersionControl \VersionControl ;
77use GitElephant \Repository as GitRepository ;
88use Illuminate \Support \Facades \Storage ;
9+ use Illuminate \FileSystem \FileSystem ;
910
1011class Repository extends Model
1112{
@@ -16,15 +17,82 @@ protected function absolutePath()
1617 return app ()->storagePath ().'/app/ ' .$ this ->path ;
1718 }
1819
20+ protected function init ()
21+ {
22+ $ submission = Submission::where ('repository_id ' , $ this ->id )->get ()[0 ];
23+ $ assignment = Assignment::where ('id ' , $ submission ->assignment_id )->get ()[0 ];
24+ $ fs = new FileSystem ();
25+
26+ $ laravelStorage = app ()->storagePath ();
27+ $ userId = $ submission ->user_id ;
28+ $ assignmentId = $ submission ->assignment_id ;
29+ $ repositoryPath = $ this ->absolutePath ();
30+ $ hookPath = $ this ->path .'.git/hooks/ ' ;
31+
32+ //First create the user's repository and init a git repo.
33+ Storage::makeDirectory ($ this ->path );
34+ $ this ->repo = GitRepository::open ($ this ->absolutePath ());
35+ $ this ->repo ->init ();
36+ //
37+ $ assignmentPath = realpath ($ repositoryPath . "../ " );
38+ $ assignmentPathRel = $ this ->path .'/../ ' ;
39+
40+ //Obtain the template for a repository environment.
41+ $ env = Storage::get ('templates/environment.php ' );
42+
43+ //We're starting to tie repositories directly to an assignment.
44+ //It should be broken up into a generic repository and
45+ //an AssignmentRepository.
46+
47+
48+ $ parameters = [
49+ '__LARAVEL_STORAGE__ ' => $ laravelStorage ,
50+ '__LARAVEL_DIR__ ' => __DIR__ ,
51+ '__STUDENT_ID__ ' => $ userId ,
52+ '__ASSIGNMENT_ID__ ' => $ assignmentId ,
53+ '__SUBMISSION_DIR__ ' => $ repositoryPath ,
54+ '__ASSIGNMENT_DIR__ ' => $ assignmentPath ,
55+ ];
56+
57+ foreach ($ parameters as $ key => $ value ) {
58+ $ env = str_replace ($ key , $ value , $ env );
59+ }
60+
61+
62+
63+ /*GIT Repo config*/
64+ $ file = Storage::get ('templates/config ' );
65+ $ configPath = $ this ->path .'.git/ ' ;
66+ Storage::put ($ configPath .'config ' , $ file );
67+
68+ /*Git Environment*/
69+ Storage::put ($ hookPath .'environment.php ' , $ env );
70+
71+ /*Post-update Hook*/
72+ $ file = Storage::get ('templates/post-update ' );
73+ Storage::put ($ hookPath .'post-update ' , $ file );
74+ $ fs ->chmod ($ this ->absolutePath ().'/.git/hooks/post-update ' , 0755 );
75+ /*post-commit hook*/
76+ $ file = Storage::get ('templates/post-commit ' );
77+ Storage::put ($ hookPath .'post-commit ' , $ file );
78+ $ fs ->chmod ($ this ->absolutePath ().'/.git/hooks/post-commit ' , 0755 );
79+
80+ /*Assignment config*/
81+ /*Currently this gets overwritten each time a repository is created*/
82+ Storage::makeDirectory ($ assignmentPathRel .'/.config/ ' );
83+ $ file = Storage::get ('templates/hooks.php ' );
84+ Storage::put ($ assignmentPathRel .'/.config/hooks.php ' , $ file );
85+ $ fs ->chmod ($ assignmentPath . '/.config/hooks.php ' , 0755 );
86+
87+
88+ $ this ->initialized = true ;
89+ }
90+
1991 public function repository ()
2092 {
2193 //Lazy initialization
2294 if (!$ this ->initialized ) {
23- Storage::makeDirectory ($ this ->path );
24- $ this ->repo = GitRepository::open ($ this ->absolutePath ());
25- $ this ->repo ->init ();
26-
27- $ this ->initialized = true ;
95+ $ this ->init ();
2896 }
2997
3098 if (is_null ($ this ->repo )) {
@@ -35,7 +103,8 @@ public function repository()
35103 }
36104
37105
38- public function commit ($ message ) {
106+ public function commit ($ message )
107+ {
39108 //Prehooks should be placed here.
40109 $ this ->repository ()->commit ($ message , true );
41110 //Posthooks should be placed here.
0 commit comments