@@ -53,6 +53,19 @@ public static function &create($repo_path, $source = null) {
5353 public static function open ($ repo_path ) {
5454 return new GitRepo ($ repo_path );
5555 }
56+
57+ /**
58+ * Checks if a variable is an instance of GitRepo
59+ *
60+ * Accepts a variable
61+ *
62+ * @access public
63+ * @param mixed variable
64+ * @return bool
65+ */
66+ public static function is_repo ($ var ) {
67+ return (get_class ($ var ) == 'GitRepo ' );
68+ }
5669
5770}
5871
@@ -70,7 +83,7 @@ class GitRepo {
7083
7184 protected $ repo_path = null ;
7285
73- protected $ git_path = '/usr/bin/git ' ;
86+ public $ git_path = '/usr/bin/git ' ;
7487
7588 /**
7689 * Create a new git repository
@@ -86,13 +99,10 @@ public static function &create_new($repo_path, $source = null) {
8699 if (is_dir ($ repo_path ) && file_exists ($ repo_path ."/.git " ) && is_dir ($ repo_path ."/.git " )) {
87100 throw new Exception ('"$repo_path" is already a git repository ' );
88101 } else {
89- if ( is_string ( $ source )) {
90- $ repo = new self ( $ repo_path , true );
102+ $ repo = new self ( $ repo_path , true , false );
103+ if ( is_string ( $ source ))
91104 $ repo ->clone_from ($ source );
92- return $ repo ;
93- } else {
94- return new self ($ repo_path , true );
95- }
105+ return $ repo ;
96106 }
97107 }
98108
@@ -105,10 +115,10 @@ public static function &create_new($repo_path, $source = null) {
105115 * @param string repository path
106116 * @param bool create if not exists?
107117 * @return void
108- */
109- public function __construct ($ repo_path = null , $ create_new = false ) {
118+ */
119+ public function __construct ($ repo_path = null , $ create_new = false , $ _init = true ) {
110120 if (is_string ($ repo_path ))
111- $ this ->set_repo_path ($ repo_path , $ create_new );
121+ $ this ->set_repo_path ($ repo_path , $ create_new, $ _init );
112122 }
113123
114124 /**
@@ -120,17 +130,18 @@ public function __construct($repo_path = null, $create_new = false) {
120130 * @param string repository path
121131 * @param bool create if not exists?
122132 * @return void
123- */
124- public function set_repo_path ($ repo_path , $ create_new = false ) {
133+ */
134+ public function set_repo_path ($ repo_path , $ create_new = false , $ _init = true ) {
125135 if (is_string ($ repo_path )) {
126- if ($ repo_path = realpath ($ repo_path )) {
136+ if ($ new_path = realpath ($ repo_path )) {
137+ $ repo_path = $ new_path ;
127138 if (is_dir ($ repo_path )) {
128139 if (file_exists ($ repo_path ."/.git " ) && is_dir ($ repo_path ."/.git " )) {
129140 $ this ->repo_path = $ repo_path ;
130141 } else {
131142 if ($ create_new ) {
132143 $ this ->repo_path = $ repo_path ;
133- $ this ->run ('init ' );
144+ if ( $ _init ) $ this ->run ('init ' );
134145 } else {
135146 throw new Exception ('"$repo_path" is not a git repository ' );
136147 }
@@ -141,13 +152,9 @@ public function set_repo_path($repo_path, $create_new = false) {
141152 } else {
142153 if ($ create_new ) {
143154 if ($ parent = realpath (dirname ($ repo_path ))) {
144- try {
145- mkdir ($ repo_path );
146- } catch (Exception $ e ) {
147- throw $ e ; return ;
148- }
155+ mkdir ($ repo_path );
149156 $ this ->repo_path = $ repo_path ;
150- $ this ->run ('init ' );
157+ if ( $ _init ) $ this ->run ('init ' );
151158 } else {
152159 throw new Exception ('cannot create repository in non-existent directory ' );
153160 }
@@ -158,6 +165,30 @@ public function set_repo_path($repo_path, $create_new = false) {
158165 }
159166 }
160167
168+ /**
169+ * Tests if git is installed
170+ *
171+ * @access public
172+ * @return bool
173+ */
174+ public function test_git () {
175+ $ descriptorspec = array (
176+ 1 => array ('pipe ' , 'w ' ),
177+ 2 => array ('pipe ' , 'w ' ),
178+ );
179+ $ pipes = array ();
180+ $ resource = proc_open ($ this ->git_path , $ descriptorspec , $ pipes );
181+
182+ $ stdout = stream_get_contents ($ pipes [1 ]);
183+ $ stderr = stream_get_contents ($ pipes [2 ]);
184+ foreach ($ pipes as $ pipe ) {
185+ fclose ($ pipe );
186+ }
187+
188+ $ status = trim (proc_close ($ resource ));
189+ return ($ status != 127 );
190+ }
191+
161192 /**
162193 * Run a command in the git repository
163194 *
@@ -168,8 +199,6 @@ public function set_repo_path($repo_path, $create_new = false) {
168199 * @return string
169200 */
170201 protected function run_command ($ command ) {
171- $ command = $ this ->createCommandString ($ arguments , $ options );
172-
173202 $ descriptorspec = array (
174203 1 => array ('pipe ' , 'w ' ),
175204 2 => array ('pipe ' , 'w ' ),
@@ -184,13 +213,7 @@ protected function run_command($command) {
184213 }
185214
186215 $ status = trim (proc_close ($ resource ));
187- if ($ status ) {
188- $ message =
189- "Git command threw errors. \n\n" .
190- "Output: \n$ stdout \n" .
191- "Error: \n$ stderr " ;
192- throw new Exception ($ message );
193- }
216+ if ($ status ) throw new Exception ($ stderr );
194217
195218 return $ stdout ;
196219 }
0 commit comments