Skip to content

Commit 59da1c5

Browse files
author
alexandresalome
committed
use options for Repository creation - #30
1 parent 07451ab commit 59da1c5

File tree

5 files changed

+173
-69
lines changed

5 files changed

+173
-69
lines changed

doc/api/admin.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ takes only two arguments, where to mirror and what to mirror:
5858
// Mirror to a non-bare repository
5959
$mirror = Gitonomy\Git\Admin::mirrorTo('/tmp/mirror', 'https://github.com/gitonomy/gitlib.git', false);
6060
61-
Execution logs
62-
--------------
63-
64-
All methods seen above take an additional parameters after ``$debug``. This parameter is used to report
65-
everything happening in repository: ``$logger``.
66-
67-
If you want a report of execution, inject logger via constructor or repository-creation methods:
68-
69-
.. code-block:: php
70-
71-
$logger = new
7261
7362
References
7463
::::::::::

doc/api/repository.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Repository methods
2+
==================
3+
4+
Creating a *Repository* object is possible, providing a *path* argument to the
5+
constructor:
6+
7+
.. code-block:: php
8+
9+
$repository = new Repository('/path/to/repo');
10+
11+
Repository options
12+
------------------
13+
14+
The constructor of Repository takes an additional parameter: ``$options``.
15+
This parameter can be used used to tune behavior of library.
16+
17+
Available options are:
18+
19+
* **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
23+
24+
An example:
25+
26+
.. code-block:: php
27+
28+
$repository = new Repository('/path/to/repo', array(
29+
'debug' => true,
30+
'logger' => new Monolog\Logger()
31+
));
32+
33+
Test if a repository is bare
34+
----------------------------
35+
36+
On a *Repository* object, you can method *isBare* to test it:
37+
38+
.. code-block:: php
39+
40+
if ($repository->isBare()) {
41+
echo "This repository is bare\n";
42+
}
43+
44+
Compute size of a repository
45+
----------------------------
46+
47+
To know how much size a repository is using on your drive, you can use
48+
``getSize`` method on a *Repository* object.
49+
50+
This method will basically compute the size of the folder, using system ``du`` tool.
51+
52+
The returned size is in kilobytes:
53+
54+
.. code-block:: php
55+
56+
$size = $repository->getSize();
57+
echo "Your repository size is ".$size."KB";
58+
59+
Access HEAD
60+
-----------
61+
62+
``HEAD`` represents in git the version you are working on (in working tree).
63+
Your ``HEAD`` can be attached (using a reference) or detached (using a commit).
64+
65+
.. code-block:: php
66+
67+
$head = $repository->getHead(); // Commit or Reference
68+
$head = $repository->getHeadCommit(); // Commit
69+
70+
if ($repository->isHeadDetached()) {
71+
echo "Sorry man\n";
72+
}
73+
74+
Logger
75+
------
76+
77+
If you are developing, you may appreciate to have a logger inside repository, telling
78+
you every executed command.
79+
80+
To do so, call method ``setLogger`` as an option on repository creation:
81+
82+
.. code-block:: php
83+
84+
$repository->setLogger(new Monolog\Logger('repository'));
85+
86+
$repository->run('fetch', array('--all'));
87+
88+
This will output:
89+
90+
.. code-block:: text
91+
92+
info run command: fetch "--all"
93+
debug last command (fetch) duration: 23.24ms
94+
debug last command (fetch) return code: 0
95+
debug last command (fetch) output: Fetching origin

doc/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Reference
2323
.. toctree::
2424
:maxdepth: 1
2525

26-
api/create_repository
26+
api/admin
27+
api/repository
2728
api/hooks
2829
api/workingcopy
2930
api/commit

src/Gitonomy/Git/Admin.php

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ class Admin
2525
/**
2626
* Initializes a repository and returns the instance.
2727
*
28-
* @param string $path path to the repository
29-
* @param boolean $bare indicate to create a bare repository
30-
* @param boolean $debug flag indicating if errors should be thrown
31-
* @param LoggerInterface $logger logger for debug purposes
28+
* @param string $path path to the repository
29+
* @param boolean $bare indicate to create a bare repository
30+
* @param array $options options for Repository creation
3231
*
3332
* @return Repository
3433
*
3534
* @throws RuntimeException Directory exists or not writable (only if debug=true)
3635
*/
37-
public static function init($path, $bare = true, $debug = true, LoggerInterface $logger = null)
36+
public static function init($path, $bare = true, array $options = array())
3837
{
3938
$builder = ProcessBuilder::create(array('git', 'init', '-q'));
4039

@@ -44,90 +43,82 @@ public static function init($path, $bare = true, $debug = true, LoggerInterface
4443

4544
$builder->add($path);
4645

46+
$builder->inheritEnvironmentVariables(false);
4747
$process = $builder->getProcess();
48+
if (isset($options['environment_variables'])) {
49+
$process->setEnv($options['environment_variables']);
50+
}
4851
$process->run();
4952

5053
if (!$process->isSuccessFul()) {
51-
$message = sprintf("Error on repository initialization, command wasn't successful (%s). Error output:\n%s", $process->getCommandLine(), $process->getErrorOutput());
52-
53-
if (null !== $logger) {
54-
$logger->error($message);
55-
}
56-
57-
if (true === $debug) {
58-
throw new \RuntimeException($message);
59-
}
54+
throw new \RuntimeException(sprintf("Error on repository initialization, command wasn't successful (%s). Error output:\n%s", $process->getCommandLine(), $process->getErrorOutput()));
6055
}
6156

62-
return new Repository($path, $debug, $logger);
57+
return new Repository($path, $options);
6358
}
6459

6560
/**
6661
* Clone a repository to a local path.
6762
*
68-
* @param string $path indicates where to clone repository
69-
* @param string $url url of repository to clone
70-
* @param boolean $bare indicates if repository should be bare or have a working copy
71-
* @param boolean $debug flag indicating if errors should be thrown
72-
* @param LoggerInterface $logger logger for debug purpopses
63+
* @param string $path indicates where to clone repository
64+
* @param string $url url of repository to clone
65+
* @param boolean $bare indicates if repository should be bare or have a working copy
66+
* @param array $options options for Repository creation
7367
*
7468
* @return Repository
7569
*/
76-
public static function cloneTo($path, $url, $bare = true, $debug = true, LoggerInterface $logger = null)
70+
public static function cloneTo($path, $url, $bare = true, array $options = array())
7771
{
78-
$options = array();
79-
80-
if ($bare) {
81-
$options[] = '--bare';
82-
}
72+
$args = $bare ? array('--bare') : array();
8373

84-
return static::cloneRepository($path, $url, $options, $debug, $logger);
74+
return static::cloneRepository($path, $url, $args, $options);
8575
}
8676

8777
/**
8878
* Mirrors a repository (fetch all revisions, not only branches).
8979
*
90-
* @param string $path indicates where to clone repository
91-
* @param string $url url of repository to clone
92-
* @param boolean $debug flag indicating if errors should be thrown
93-
* @param LoggerInterface $logger logger for debug purpopses
80+
* @param string $path indicates where to clone repository
81+
* @param string $url url of repository to clone
82+
* @param array $options options for Repository creation
9483
*
9584
* @return Repository
9685
*/
97-
public static function mirrorTo($path, $url, $debug = true, LoggerInterface $logger = null)
86+
public static function mirrorTo($path, $url, array $options = array())
9887
{
99-
return static::cloneRepository($path, $url, array('--mirror'), $logger);
88+
return static::cloneRepository($path, $url, array('--mirror'), $options);
10089
}
10190

10291
/**
10392
* Internal method to launch effective ``git clone`` command.
10493
*
105-
* @param string $path indicates where to clone repository
106-
* @param string $url url of repository to clone
107-
* @param array $options arguments to be added to the command-line
108-
* @param boolean $debug flag indicating if errors should be thrown
109-
* @param LoggerInterface $logger logger for debug purpopses
94+
* @param string $path indicates where to clone repository
95+
* @param string $url url of repository to clone
96+
* @param array $args arguments to be added to the command-line
97+
* @param array $options options for Repository creation
11098
*
11199
* @return Repository
112100
*/
113-
private static function cloneRepository($path, $url, array $options = array(), $debug = true, LoggerInterface $logger = null)
101+
private static function cloneRepository($path, $url, array $args = array(), array $options = array())
114102
{
115103
$builder = ProcessBuilder::create(array('git', 'clone', '-q'));
116-
117-
foreach ($options as $value) {
104+
foreach ($args as $value) {
118105
$builder->add($value);
119106
}
120-
121107
$builder->add($url);
122108
$builder->add($path);
123109

110+
$builder->inheritEnvironmentVariables(false);
111+
if (isset($options['environment_variables'])) {
112+
$builder->setEnv($options['environment_variables']);
113+
}
114+
124115
$process = $builder->getProcess();
125116
$process->run();
126117

127118
if (!$process->isSuccessFul()) {
128119
throw new \RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
129120
}
130121

131-
return new Repository($path, $debug, $logger);
122+
return new Repository($path, $options);
132123
}
133124
}

src/Gitonomy/Git/Repository.php

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,43 @@ class Repository
7575
protected $debug;
7676

7777
/**
78-
* Constructor.
78+
* Environment variables that should be set for every running process.
7979
*
80-
* @param string $dir
81-
* @param string $workingDir
82-
* @param LoggerInterface $logger
80+
* @var array
81+
*/
82+
protected $environmentVariables;
83+
84+
/**
85+
* Constructs a new repository.
86+
*
87+
* Available options are:
88+
*
89+
* * working_dir : specify where working copy is located (option --work-tree)
90+
*
91+
* * debug : (default: true) enable/disable minimize errors and reduce log level
92+
*
93+
* * logger : a logger to use for logging actions
94+
*
95+
* * environment_variables : define environment variables for every ran process
96+
*
97+
* @param string $dir path to git repository
98+
* @param array $options array of options values
8399
*
84100
* @throws InvalidArgumentException The folder does not exists
85101
*/
86-
public function __construct($dir, $debug = true, LoggerInterface $logger = null)
102+
public function __construct($dir, $options = array())
87103
{
104+
$options = array_merge(array(
105+
'working_dir' => null,
106+
'debug' => true,
107+
'logger' => null,
108+
'environment_variables' => array()
109+
), $options);
110+
88111
$gitDir = realpath($dir);
89-
$workingDir = null;
112+
$workingDir = $options['working_dir'];
90113

91-
if (is_dir($gitDir.'/.git')) {
114+
if (null === $workingDir && is_dir($gitDir.'/.git')) {
92115
$workingDir = $gitDir;
93116
$gitDir = $gitDir.'/.git';
94117
} elseif (!is_dir($gitDir)) {
@@ -99,8 +122,13 @@ public function __construct($dir, $debug = true, LoggerInterface $logger = null)
99122
$this->workingDir = $workingDir;
100123

101124
$this->objects = array();
102-
$this->debug = (bool) $debug;
103-
$this->logger = $logger;
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'];
104132

105133
if (true === $this->debug && null !== $this->logger) {
106134
$logger->debug('Repository created (git dir: %s, working dir: %s)', $this->gitDir, $this->workingDir ? : 'none');
@@ -521,14 +549,12 @@ public function setLogger(LoggerInterface $logger)
521549
*
522550
* @param string $path path to the new repository in which current repository will be cloned
523551
* @param boolean $bare flag indicating if repository is bare or has a working-copy
524-
* @param boolean $debug flag indicating if new repository should have debug-mode enabled
525-
* @param LoggerInterface $logger a logger to send messages about execution.
526552
*
527553
* @return Repository the newly created repository
528554
*/
529-
public function cloneTo($path, $bare = true, $debug = true, LoggerInterface $logger = null)
555+
public function cloneTo($path, $bare = true, array $options = array())
530556
{
531-
return Admin::cloneTo($path, $this->gitDir, $bare, $debug, $logger);
557+
return Admin::cloneTo($path, $this->gitDir, $bare, $options);
532558
}
533559

534560
/**
@@ -551,7 +577,9 @@ private function getProcess($command, $args = array())
551577

552578
$builder = new ProcessBuilder(array_merge($base, $args));
553579
$builder->inheritEnvironmentVariables(false);
580+
$process = $builder->getProcess();
581+
$process->setEnv($this->environmentVariables);
554582

555-
return $builder->getProcess();
583+
return $process;
556584
}
557585
}

0 commit comments

Comments
 (0)