Skip to content

Commit 209908f

Browse files
committed
Initial commit
0 parents  commit 209908f

23 files changed

+2324
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build/
2+
vendor/
3+
.DS_Store
4+
.Spotlight-V100
5+
.Trashes
6+
composer.lock

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
before_script:
4+
- curl -s http://getcomposer.org/installer | php
5+
- php composer.phar install --dev
6+
7+
php:
8+
- 5.3
9+
- 5.4
10+
11+
script: phpunit

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Copyright (c) 2012, Klaus Silveira and contributors
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
Neither the name of Gitter nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Gitter
2+
[![Build Status](https://secure.travis-ci.org/klaussilveira/gitter.png)](http://travis-ci.org/klaussilveira/gitter)
3+
4+
Gitter allows you to interact in an object oriented manner with Git repositories
5+
via PHP. The main goal of the library is not to replace the system `git` command,
6+
but provide a coherent, stable and performatic object oriented interface.
7+
8+
Most commands are sent to the system's `git` command, parsed and then interpreted
9+
by Gitter. Everything is transparent to you, so you don't have to worry about a thing.
10+
11+
## Requirements
12+
13+
* git (http://git-scm.com) (tested with 1.7.5.4)
14+
15+
## Authors and contributors
16+
* [Klaus Silveira](http://www.klaussilveira.com) (Creator, developer)
17+
18+
## License
19+
[New BSD license](http://www.opensource.org/licenses/bsd-license.php)
20+
21+
## Usage
22+
23+
Gitter is very easy to use and you'll just need a few method calls to get
24+
started. For example, to create a new repository:
25+
26+
$client = new Gitter\Client;
27+
$repository = $client->createRepository('/home/user/test');
28+
29+
Or a bare repository:
30+
31+
$client = new Gitter\Client;
32+
$repository = $client->createRepository('/home/user/test', true);
33+
34+
Or to open an existing repository:
35+
36+
$client = new Gitter\Client;
37+
$repository = $client->getRepository('/home/user/anothertest');
38+
39+
Both methods will return a `Repository` object, which has various methods
40+
that allow you to interact with that repository.
41+
42+
### Getting a list of commits
43+
44+
Once you get hold of the `Repository` object, you can use:
45+
46+
$commits = $repository->getCommits();
47+
print_r($commits);
48+
49+
To get a list of various commits.
50+
51+
### Getting a single commit
52+
53+
Given a specific commit hash, you can find information about that commit:
54+
55+
$commit = $repository->getCommit('920be98a05');
56+
print_r($commit);

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "klaussilveira/gitter",
3+
"description": "Gitter allows you to interact in an object oriented manner with Git repositories.",
4+
"keywords": ["git", "vcs"],
5+
"homepage": "https://github.com/klaussilveira/gitter",
6+
"type": "library",
7+
"license": "BSD-2-Clause",
8+
"authors": [
9+
{
10+
"name": "Klaus Silveira",
11+
"email": "[email protected]",
12+
"homepage": "http://www.klaussilveira.com/",
13+
"role": "Developer"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.3.0",
18+
"symfony/process": "2.1.*"
19+
},
20+
"require-dev": {
21+
"symfony/filesystem": "2.1.*"
22+
},
23+
"autoload": {
24+
"psr-0": {
25+
"Gitter": "lib/"
26+
}
27+
}
28+
}

lib/Gitter/Client.php

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Gitter library.
5+
*
6+
* (c) Klaus Silveira <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gitter;
13+
14+
use Symfony\Component\Process\Process;
15+
16+
class Client
17+
{
18+
protected $path;
19+
protected $hidden;
20+
21+
public function __construct($options = null)
22+
{
23+
$this->setPath((isset($options['path'])) ? $options['path'] : '/usr/bin/git');
24+
$this->setHidden((isset($options['hidden'])) ? $options['hidden'] : array());
25+
}
26+
27+
/**
28+
* Creates a new repository on the specified path
29+
*
30+
* @param string $path Path where the new repository will be created
31+
* @return Repository Instance of Repository
32+
*/
33+
public function createRepository($path, $bare = null)
34+
{
35+
if (file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) {
36+
throw new \RuntimeException('A GIT repository already exists at ' . $path);
37+
}
38+
39+
$repository = new Repository($path, $this);
40+
41+
return $repository->create($bare);
42+
}
43+
44+
/**
45+
* Opens a repository at the specified path
46+
*
47+
* @param string $path Path where the repository is located
48+
* @return Repository Instance of Repository
49+
*/
50+
public function getRepository($path)
51+
{
52+
if (!file_exists($path) || !file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) {
53+
throw new \RuntimeException('There is no GIT repository at ' . $path);
54+
}
55+
56+
if (in_array($path, $this->getHidden())) {
57+
throw new \RuntimeException('You don\'t have access to this repository');
58+
}
59+
60+
return new Repository($path, $this);
61+
}
62+
63+
/**
64+
* Searches for valid repositories on the specified path
65+
*
66+
* @param string $path Path where repositories will be searched
67+
* @return array Found repositories, containing their name, path and description
68+
*/
69+
public function getRepositories($path)
70+
{
71+
$repositories = $this->recurseDirectory($path);
72+
73+
if (empty($repositories)) {
74+
throw new \RuntimeException('There are no GIT repositories in ' . $path);
75+
}
76+
77+
sort($repositories);
78+
79+
return $repositories;
80+
}
81+
82+
private function recurseDirectory($path)
83+
{
84+
$dir = new \DirectoryIterator($path);
85+
86+
$repositories = array();
87+
88+
foreach ($dir as $file) {
89+
if ($file->isDot()) {
90+
continue;
91+
}
92+
93+
if (strrpos($file->getFilename(), '.') === 0) {
94+
continue;
95+
}
96+
97+
if ($file->isDir()) {
98+
$isBare = file_exists($file->getPathname() . '/HEAD');
99+
$isRepository = file_exists($file->getPathname() . '/.git/HEAD');
100+
101+
if ($isRepository || $isBare) {
102+
if (in_array($file->getPathname(), $this->getHidden())) {
103+
continue;
104+
}
105+
106+
if ($isBare) {
107+
$description = $file->getPathname() . '/description';
108+
} else {
109+
$description = $file->getPathname() . '/.git/description';
110+
}
111+
112+
if (file_exists($description)) {
113+
$description = file_get_contents($description);
114+
} else {
115+
$description = 'There is no repository description file. Please, create one to remove this message.';
116+
}
117+
118+
$repositories[] = array('name' => $file->getFilename(), 'path' => $file->getPathname(), 'description' => $description);
119+
continue;
120+
}
121+
}
122+
}
123+
124+
return $repositories;
125+
}
126+
127+
public function run($repository, $command)
128+
{
129+
$process = new Process($this->getPath() . ' -c "color.ui"=false ' . $command, $repository->getPath());
130+
$process->setTimeout(180);
131+
$process->run();
132+
133+
if (!$process->isSuccessful()) {
134+
throw new \RuntimeException($process->getErrorOutput());
135+
}
136+
137+
return $process->getOutput();
138+
}
139+
140+
/**
141+
* Get the current Git binary path
142+
*
143+
* @return string Path where the Git binary is located
144+
*/
145+
protected function getPath()
146+
{
147+
return $this->path;
148+
}
149+
150+
/**
151+
* Set the current Git binary path
152+
*
153+
* @param string $path Path where the Git binary is located
154+
*/
155+
protected function setPath($path)
156+
{
157+
$this->path = $path;
158+
159+
return $this;
160+
}
161+
162+
/**
163+
* Get hidden repository list
164+
*
165+
* @return array List of repositories to hide
166+
*/
167+
protected function getHidden()
168+
{
169+
return $this->hidden;
170+
}
171+
172+
/**
173+
* Set the hidden repository list
174+
*
175+
* @param array $hidden List of repositories to hide
176+
*/
177+
protected function setHidden($hidden)
178+
{
179+
$this->hidden = $hidden;
180+
181+
return $this;
182+
}
183+
}

lib/Gitter/Model/AbstractModel.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Gitter library.
5+
*
6+
* (c) Klaus Silveira <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gitter\Model;
13+
14+
abstract class AbstractModel
15+
{
16+
protected $repository;
17+
18+
public function getRepository()
19+
{
20+
return $this->repository;
21+
}
22+
23+
public function setRepository($repository)
24+
{
25+
$this->repository = $repository;
26+
27+
return $this;
28+
}
29+
}

0 commit comments

Comments
 (0)