Skip to content

Commit ab8fc27

Browse files
author
alexandresalome
committed
add option command to specify git path #30
1 parent 59da1c5 commit ab8fc27

File tree

3 files changed

+96
-35
lines changed

3 files changed

+96
-35
lines changed

doc/api/repository.rst

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ This parameter can be used used to tune behavior of library.
1717
Available options are:
1818

1919
* **debug** (default: true): Enables exception when edge cases are met
20-
* **environment_variables**: An array of environment variables to be set in sub-process
21-
* **logger**: Logger to use for reporting of execution
22-
* **working_dir**: If you are using multiple working directories, this opions is for you
20+
* **environment_variables**: (default: none) An array of environment variables to be set in sub-process
21+
* **logger**: (default: none) Logger to use for reporting of execution (a ``Psr\Log\LoggerInterface``)
22+
* **command**: (default: ``git``) Specify command to execute to run git
23+
* **working_dir**: If you are using multiple working directories, this option is for you
2324

2425
An example:
2526

@@ -33,27 +34,25 @@ An example:
3334
Test if a repository is bare
3435
----------------------------
3536

36-
On a *Repository* object, you can method *isBare* to test it:
37+
On a *Repository* object, you can call method *isBare* to test if your repository is bare or not:
3738

3839
.. code-block:: php
3940
40-
if ($repository->isBare()) {
41-
echo "This repository is bare\n";
42-
}
41+
$repository->isBare();
4342
4443
Compute size of a repository
4544
----------------------------
4645

47-
To know how much size a repository is using on your drive, you can use
48-
``getSize`` method on a *Repository* object.
46+
To know how much size a repository is using on your drive, you can use ``getSize`` method on a *Repository* object.
4947

50-
This method will basically compute the size of the folder, using system ``du`` tool.
48+
.. warning:: This command was only tested with linux.
5149

5250
The returned size is in kilobytes:
5351

5452
.. code-block:: php
5553
5654
$size = $repository->getSize();
55+
5756
echo "Your repository size is ".$size."KB";
5857
5958
Access HEAD
@@ -71,20 +70,29 @@ Your ``HEAD`` can be attached (using a reference) or detached (using a commit).
7170
echo "Sorry man\n";
7271
}
7372
73+
Options for repository
74+
----------------------
75+
7476
Logger
75-
------
77+
......
7678

77-
If you are developing, you may appreciate to have a logger inside repository, telling
78-
you every executed command.
79+
If you are developing, you may appreciate to have a logger inside repository, telling you every executed command.
7980

80-
To do so, call method ``setLogger`` as an option on repository creation:
81+
You call method ``setLogger`` as an option on repository creation:
8182

8283
.. code-block:: php
8384
8485
$repository->setLogger(new Monolog\Logger('repository'));
8586
8687
$repository->run('fetch', array('--all'));
8788
89+
You can also specify as an option on repository creation:
90+
91+
$logger = new Monolog\Logger('repository');
92+
$repository = new Repository('/path/foo', array('logger' => $logger));
93+
94+
$repository->run('fetch', array('--all'));
95+
8896
This will output:
8997

9098
.. code-block:: text
@@ -93,3 +101,35 @@ This will output:
93101
debug last command (fetch) duration: 23.24ms
94102
debug last command (fetch) return code: 0
95103
debug last command (fetch) output: Fetching origin
104+
105+
Disable debug-mode
106+
..................
107+
108+
Gitlib throws an exception when something seems wrong. If a ``git` command returns a non-zero result, it will stop execution and throw an ``RuntimeException``.
109+
110+
If you want to prevent this, set ``debug`` option to ``false``. This will make Repository log errors and return empty data instead of throwing exceptions.
111+
112+
.. code-block:: php
113+
114+
$repository = new Repository('/tmp/foo', array('debug' => false, 'logger' => $logger));
115+
116+
.. note:: if you plan to disable debug, you should rely on logger to keep a trace of edge failing cases.
117+
118+
Specify git command to use
119+
..........................
120+
121+
You can pass option ``command`` to specify which command to use to run git calls. If you have a git binary
122+
located somewhere else, use this option to specify to gitlib path to your git binary:
123+
124+
.. code-block:: php
125+
126+
$repository = new Gitonomy\Git\Repository('/tmp/foo', array('command' => '/home/alice/bin/git'));
127+
128+
Environment variables
129+
.....................
130+
131+
Now you want to set environment variables to use to run ``git`` commands. It might be useful.
132+
133+
.. code-block:: php
134+
135+
$repository = new Gitonomy\Git\Repository('/tmp/foo', array('environment_variables' => array('GIT_')))

src/Gitonomy/Git/Admin.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ class Admin
3535
*/
3636
public static function init($path, $bare = true, array $options = array())
3737
{
38-
$builder = ProcessBuilder::create(array('git', 'init', '-q'));
38+
$command = isset($options['command']) ? $options['command'] : 'git';
3939

40+
// command-line
41+
$builder = ProcessBuilder::create(array('git', 'init', '-q'));
4042
if ($bare) {
4143
$builder->add('--bare');
4244
}
43-
4445
$builder->add($path);
4546

47+
// environment
4648
$builder->inheritEnvironmentVariables(false);
4749
$process = $builder->getProcess();
4850
if (isset($options['environment_variables'])) {
4951
$process->setEnv($options['environment_variables']);
5052
}
53+
5154
$process->run();
5255

5356
if (!$process->isSuccessFul()) {
@@ -100,7 +103,8 @@ public static function mirrorTo($path, $url, array $options = array())
100103
*/
101104
private static function cloneRepository($path, $url, array $args = array(), array $options = array())
102105
{
103-
$builder = ProcessBuilder::create(array('git', 'clone', '-q'));
106+
$command = isset($options['command']) ? $options['command'] : 'git';
107+
$builder = ProcessBuilder::create(array($command, 'clone', '-q'));
104108
foreach ($args as $value) {
105109
$builder->add($value);
106110
}

src/Gitonomy/Git/Repository.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class Repository
6767
*/
6868
protected $logger;
6969

70+
/**
71+
* Path to git command
72+
*/
73+
protected $command;
74+
7075
/**
7176
* Debug flag, indicating if errors should be thrown.
7277
*
@@ -90,7 +95,7 @@ class Repository
9095
*
9196
* * debug : (default: true) enable/disable minimize errors and reduce log level
9297
*
93-
* * logger : a logger to use for logging actions
98+
* * logger : a logger to use for logging actions (Psr\Log\LoggerInterface)
9499
*
95100
* * environment_variables : define environment variables for every ran process
96101
*
@@ -105,11 +110,36 @@ public function __construct($dir, $options = array())
105110
'working_dir' => null,
106111
'debug' => true,
107112
'logger' => null,
108-
'environment_variables' => array()
113+
'environment_variables' => array(),
114+
'command' => 'git'
109115
), $options);
110116

111-
$gitDir = realpath($dir);
112-
$workingDir = $options['working_dir'];
117+
if (null !== $options['logger'] && ! $options['logger'] instanceof LoggerInterface) {
118+
throw new \InvalidArgumentException(sprintf('Argument "logger" passed to Repository should be a Psr\Log\LoggerInterface. A %s was provided', is_object($options['logger']) ? get_class($options['logger']) : gettype($options['logger'])));
119+
}
120+
121+
$this->logger = $options['logger'];
122+
$this->initDir($dir, $options['working_dir']);
123+
124+
$this->objects = array();
125+
$this->debug = (bool) $options['debug'];
126+
$this->environmentVariables = $options['environment_variables'];
127+
$this->command = $options['command'];
128+
129+
if (true === $this->debug && null !== $this->logger) {
130+
$logger->debug('Repository created (git dir: %s, working dir: %s)', $this->gitDir, $this->workingDir ? : 'none');
131+
}
132+
}
133+
134+
/**
135+
* Initializes directory attributes on repository:
136+
*
137+
* @param string $gitDir directory of a working copy with files checked out
138+
* @param string $workingDir directory containing git files (objects, config...)
139+
*/
140+
private function initDir($gitDir, $workingDir = null)
141+
{
142+
$gitDir = realpath($gitDir);
113143

114144
if (null === $workingDir && is_dir($gitDir.'/.git')) {
115145
$workingDir = $gitDir;
@@ -120,19 +150,6 @@ public function __construct($dir, $options = array())
120150

121151
$this->gitDir = $gitDir;
122152
$this->workingDir = $workingDir;
123-
124-
$this->objects = array();
125-
$this->debug = (bool) $options['debug'];
126-
$this->environmentVariables = $options['environment_variables'];
127-
128-
if (null !== $options['logger'] && ! $options['logger'] instanceof LoggerInterface) {
129-
throw new \InvalidArgumentException(sprintf('Argument "logger" passed to Repository should be a LoggerInterface. A %s was provided', is_object($options['logger']) ? get_class($options['logger']) : gettype($options['logger'])));
130-
}
131-
$this->logger = $options['logger'];
132-
133-
if (true === $this->debug && null !== $this->logger) {
134-
$logger->debug('Repository created (git dir: %s, working dir: %s)', $this->gitDir, $this->workingDir ? : 'none');
135-
}
136153
}
137154

138155
/**
@@ -567,7 +584,7 @@ public function cloneTo($path, $bare = true, array $options = array())
567584
*/
568585
private function getProcess($command, $args = array())
569586
{
570-
$base = array('git', '--git-dir', $this->gitDir);
587+
$base = array($this->command, '--git-dir', $this->gitDir);
571588

572589
if ($this->workingDir) {
573590
$base = array_merge($base, array('--work-tree', $this->workingDir));;

0 commit comments

Comments
 (0)