Skip to content

Commit 6145ad3

Browse files
committed
finished system test suite
1 parent 775d519 commit 6145ad3

File tree

4 files changed

+247
-54
lines changed

4 files changed

+247
-54
lines changed

CI_Git.php

100644100755
Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ public function &create($repo_path, $source = null) {
5656
public function open($repo_path) {
5757
return new GitRepo($repo_path);
5858
}
59+
60+
/**
61+
* Checks if a variable is an instance of GitRepo
62+
*
63+
* Accepts a variable
64+
*
65+
* @access public
66+
* @param mixed variable
67+
* @return bool
68+
*/
69+
public static function is_repo($var) {
70+
return (get_class($var) == 'GitRepo');
71+
}
5972

6073
}
6174

@@ -112,10 +125,10 @@ public static function &create_new($repo_path, $source = null) {
112125
* @param string repository path
113126
* @param bool create if not exists?
114127
* @return void
115-
*/
116-
public function __construct($repo_path = null, $create_new = false) {
128+
*/
129+
public function __construct($repo_path = null, $create_new = false, $_init = true) {
117130
if (is_string($repo_path))
118-
$this->set_repo_path($repo_path, $create_new);
131+
$this->set_repo_path($repo_path, $create_new, $_init);
119132
}
120133

121134
/**
@@ -127,17 +140,18 @@ public function __construct($repo_path = null, $create_new = false) {
127140
* @param string repository path
128141
* @param bool create if not exists?
129142
* @return void
130-
*/
131-
public function set_repo_path($repo_path, $create_new = false) {
143+
*/
144+
public function set_repo_path($repo_path, $create_new = false, $_init = true) {
132145
if (is_string($repo_path)) {
133-
if ($repo_path = realpath($repo_path)) {
146+
if ($new_path = realpath($repo_path)) {
147+
$repo_path = $new_path;
134148
if (is_dir($repo_path)) {
135149
if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
136150
$this->repo_path = $repo_path;
137151
} else {
138152
if ($create_new) {
139153
$this->repo_path = $repo_path;
140-
$this->run('init');
154+
if ($_init) $this->run('init');
141155
} else {
142156
throw new Exception('"$repo_path" is not a git repository');
143157
}
@@ -148,13 +162,9 @@ public function set_repo_path($repo_path, $create_new = false) {
148162
} else {
149163
if ($create_new) {
150164
if ($parent = realpath(dirname($repo_path))) {
151-
try {
152-
mkdir($repo_path);
153-
} catch (Exception $e) {
154-
throw $e; return;
155-
}
165+
mkdir($repo_path);
156166
$this->repo_path = $repo_path;
157-
$this->run('init');
167+
if ($_init) $this->run('init');
158168
} else {
159169
throw new Exception('cannot create repository in non-existent directory');
160170
}
@@ -165,6 +175,30 @@ public function set_repo_path($repo_path, $create_new = false) {
165175
}
166176
}
167177

178+
/**
179+
* Tests if git is installed
180+
*
181+
* @access public
182+
* @return bool
183+
*/
184+
public function test_git() {
185+
$descriptorspec = array(
186+
1 => array('pipe', 'w'),
187+
2 => array('pipe', 'w'),
188+
);
189+
$pipes = array();
190+
$resource = proc_open($this->git_path, $descriptorspec, $pipes);
191+
192+
$stdout = stream_get_contents($pipes[1]);
193+
$stderr = stream_get_contents($pipes[2]);
194+
foreach ($pipes as $pipe) {
195+
fclose($pipe);
196+
}
197+
198+
$status = trim(proc_close($resource));
199+
return ($status != 127);
200+
}
201+
168202
/**
169203
* Run a command in the git repository
170204
*
@@ -175,8 +209,6 @@ public function set_repo_path($repo_path, $create_new = false) {
175209
* @return string
176210
*/
177211
protected function run_command($command) {
178-
$command = $this->createCommandString($arguments, $options);
179-
180212
$descriptorspec = array(
181213
1 => array('pipe', 'w'),
182214
2 => array('pipe', 'w'),
@@ -191,13 +223,7 @@ protected function run_command($command) {
191223
}
192224

193225
$status = trim(proc_close($resource));
194-
if ($status) {
195-
$message =
196-
"Git command threw errors.\n\n" .
197-
"Output:\n$stdout\n" .
198-
"Error:\n$stderr";
199-
throw new Exception($message);
200-
}
226+
if ($status) throw new Exception($stderr);
201227

202228
return $stdout;
203229
}

Git.php

100644100755
Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

README.textile

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
h1. Git.php
22

33
Author: James Brumond
4-
Version: 0.1.1-a
4+
Version: 0.1.1-rc
55

66
Copyright 2010 James Brumond
77
Dual licensed under MIT and GPL

0 commit comments

Comments
 (0)