Skip to content

Commit f7c5235

Browse files
committed
first commit
0 parents  commit f7c5235

File tree

4 files changed

+1409
-0
lines changed

4 files changed

+1409
-0
lines changed

CI_Git.php

Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* CodeIgniter
5+
*
6+
* An open source application development framework for PHP 4.3.2 or newer
7+
*
8+
* @package CodeIgniter
9+
* @author ExpressionEngine Dev Team
10+
* @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
11+
* @license http://codeigniter.com/user_guide/license.html
12+
* @link http://codeigniter.com
13+
* @since Version 1.0
14+
* @filesource
15+
*/
16+
17+
// ------------------------------------------------------------------------
18+
19+
/**
20+
* CodeIgniter Git Interface Class
21+
*
22+
* This class enables the creating, reading, and manipulation
23+
* of git repositories.
24+
*
25+
* @package CodeIgniter
26+
* @subpackage Git.php
27+
* @category Libraries
28+
* @author James Brumond
29+
* @link http://code.kbjrweb.com/project/gitphp
30+
*/
31+
class Git {
32+
33+
/**
34+
* Create a new git repository
35+
*
36+
* Accepts a creation path, and, optionally, a source path
37+
*
38+
* @access public
39+
* @param string repository path
40+
* @param string directory to source
41+
* @return GitRepo
42+
*/
43+
public function &create($repo_path, $source = null) {
44+
return GitRepo::create_new($repo_path, $source);
45+
}
46+
47+
/**
48+
* Open an existing git repository
49+
*
50+
* Accepts a repository path
51+
*
52+
* @access public
53+
* @param string repository path
54+
* @return GitRepo
55+
*/
56+
public function open($repo_path) {
57+
return new GitRepo($repo_path);
58+
}
59+
60+
}
61+
62+
// ------------------------------------------------------------------------
63+
64+
/**
65+
* Git Repository Interface Class
66+
*
67+
* This class enables the creating, reading, and manipulation
68+
* of a git repository
69+
*
70+
* @package CodeIgniter
71+
* @subpackage CIGit
72+
* @category Libraries
73+
* @author James Brumond
74+
* @link http://code.kbjrweb.com/project/gitphp
75+
*/
76+
class GitRepo {
77+
78+
protected $repo_path = null;
79+
80+
protected $git_path = '/usr/bin/git';
81+
82+
/**
83+
* Create a new git repository
84+
*
85+
* Accepts a creation path, and, optionally, a source path
86+
*
87+
* @access public
88+
* @param string repository path
89+
* @param string directory to source
90+
* @return GitRepo
91+
*/
92+
public static function &create_new($repo_path, $source = null) {
93+
if (is_dir($repo_path) && file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
94+
throw new Exception('"$repo_path" is already a git repository');
95+
} else {
96+
if (is_string($source)) {
97+
$repo = new self($repo_path, true);
98+
$repo->clone_from($source);
99+
return $repo;
100+
} else {
101+
return new self($repo_path, true);
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Constructor
108+
*
109+
* Accepts a repository path
110+
*
111+
* @access public
112+
* @param string repository path
113+
* @param bool create if not exists?
114+
* @return void
115+
*/
116+
public function __construct($repo_path = null, $create_new = false) {
117+
if (is_string($repo_path))
118+
$this->set_repo_path($repo_path, $create_new);
119+
}
120+
121+
/**
122+
* Set the repository's path
123+
*
124+
* Accepts the repository path
125+
*
126+
* @access public
127+
* @param string repository path
128+
* @param bool create if not exists?
129+
* @return void
130+
*/
131+
public function set_repo_path($repo_path, $create_new = false) {
132+
if (is_string($repo_path)) {
133+
if ($repo_path = realpath($repo_path)) {
134+
if (is_dir($repo_path)) {
135+
if (file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
136+
$this->repo_path = $repo_path;
137+
} else {
138+
if ($create_new) {
139+
$this->repo_path = $repo_path;
140+
$this->run('init');
141+
} else {
142+
throw new Exception('"$repo_path" is not a git repository');
143+
}
144+
}
145+
} else {
146+
throw new Exception('"$repo_path" is not a directory');
147+
}
148+
} else {
149+
if ($create_new) {
150+
if ($parent = realpath(dirname($repo_path))) {
151+
try {
152+
mkdir($repo_path);
153+
} catch (Exception $e) {
154+
throw $e; return;
155+
}
156+
$this->repo_path = $repo_path;
157+
$this->run('init');
158+
} else {
159+
throw new Exception('cannot create repository in non-existent directory');
160+
}
161+
} else {
162+
throw new Exception('"$repo_path" does not exist');
163+
}
164+
}
165+
}
166+
}
167+
168+
/**
169+
* Run a command in the git repository
170+
*
171+
* Accepts a shell command to run
172+
*
173+
* @access protected
174+
* @param string command to run
175+
* @return string
176+
*/
177+
protected function run_command($command) {
178+
$command = $this->createCommandString($arguments, $options);
179+
180+
$descriptorspec = array(
181+
1 => array('pipe', 'w'),
182+
2 => array('pipe', 'w'),
183+
);
184+
$pipes = array();
185+
$resource = proc_open($command, $descriptorspec, $pipes, $this->repo_path);
186+
187+
$stdout = stream_get_contents($pipes[1]);
188+
$stderr = stream_get_contents($pipes[2]);
189+
foreach ($pipes as $pipe) {
190+
fclose($pipe);
191+
}
192+
193+
$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+
}
201+
202+
return $stdout;
203+
}
204+
205+
/**
206+
* Run a git command in the git repository
207+
*
208+
* Accepts a git command to run
209+
*
210+
* @access public
211+
* @param string command to run
212+
* @return string
213+
*/
214+
public function run($command) {
215+
return $this->run_command($this->git_path." ".$command);
216+
}
217+
218+
/**
219+
* Runs a `git add` call
220+
*
221+
* Accepts a list of files to add
222+
*
223+
* @access public
224+
* @param mixed files to add
225+
* @return string
226+
*/
227+
public function add($files = "*") {
228+
if (is_array($files)) $files = '"'.implode('" "', $files).'"';
229+
return $this->run("add $file -v");
230+
}
231+
232+
/**
233+
* Runs a `git commit` call
234+
*
235+
* Accepts a commit message string
236+
*
237+
* @access public
238+
* @param string commit message
239+
* @return string
240+
*/
241+
public function commit($message = "") {
242+
return $this->run("commit -av -m \"$message\"");
243+
}
244+
245+
/**
246+
* Runs a `git clone` call to clone the current repository
247+
* into a different directory
248+
*
249+
* Accepts a target directory
250+
*
251+
* @access public
252+
* @param string target directory
253+
* @return string
254+
*/
255+
public function clone_to($target) {
256+
return $this->run("clone --local ".$this->repo_path." $target");
257+
}
258+
259+
/**
260+
* Runs a `git clone` call to clone a different repository
261+
* into the current repository
262+
*
263+
* Accepts a source directory
264+
*
265+
* @access public
266+
* @param string source directory
267+
* @return string
268+
*/
269+
public function clone_from($source) {
270+
return $this->run("clone --local $source ".$this->repo_path);
271+
}
272+
273+
/**
274+
* Runs a `git clone` call to clone a remote repository
275+
* into the current repository
276+
*
277+
* Accepts a source url
278+
*
279+
* @access public
280+
* @param string source url
281+
* @return string
282+
*/
283+
public function clone_remote($source) {
284+
return $this->run("clone $source ".$this->repo_path);
285+
}
286+
287+
/**
288+
* Runs a `git clean` call
289+
*
290+
* Accepts a remove directories flag
291+
*
292+
* @access public
293+
* @param bool delete directories?
294+
* @return string
295+
*/
296+
public function clean($dirs = false) {
297+
return $this->run("clean".(($dirs) ? " -d" : ""));
298+
}
299+
300+
/**
301+
* Runs a `git branch` call
302+
*
303+
* Accepts a name for the branch
304+
*
305+
* @access public
306+
* @param string branch name
307+
* @return string
308+
*/
309+
public function create_branch($branch) {
310+
return $this->run("branch $branch");
311+
}
312+
313+
/**
314+
* Runs a `git branch -[d|D]` call
315+
*
316+
* Accepts a name for the branch
317+
*
318+
* @access public
319+
* @param string branch name
320+
* @return string
321+
*/
322+
public function delete_branch($branch, $force = false) {
323+
return $this->run("branch ".(($force) ? '-D' : '-d')." $branch");
324+
}
325+
326+
/**
327+
* Runs a `git checkout` call
328+
*
329+
* Accepts a name for the branch
330+
*
331+
* @access public
332+
* @param string branch name
333+
* @return string
334+
*/
335+
public function checkout($branch) {
336+
return $this->run("checkout $branch");
337+
}
338+
339+
}
340+
341+
/* End Of File */

0 commit comments

Comments
 (0)