Skip to content

Commit 07451ab

Browse files
author
alexandresalome
committed
first implementation of debug-mode - #29
1 parent 08160ec commit 07451ab

File tree

7 files changed

+316
-186
lines changed

7 files changed

+316
-186
lines changed

doc/api.rst

Lines changed: 0 additions & 19 deletions
This file was deleted.

doc/api/admin.rst

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,75 @@
1-
Administration of git repositories
1+
Create and access git repositories
22
==================================
33

4-
Initializing repositories
5-
:::::::::::::::::::::::::
4+
gitlib provides methods to initialize new repositories.
65

7-
Create a bare repository
8-
------------------------
6+
Create a repository
7+
-------------------
98

10-
You can create a bare repository using ``Admin::init``:
9+
To initialize a new repository, use method ``Admin::init``.
1110

1211
.. code-block:: php
1312
13+
// Initialize a bare repository
1414
$repository = Gitonomy\Git\Admin::init('/path/to/repository');
1515
16-
Create a repository with a working copy
17-
---------------------------------------
16+
// Initialize a non-bare repository
17+
$repository = Gitonomy\Git\Admin::init('/path/to/repository', false);
1818
19-
If you want to create a repository with a working directory, pass ``false`` as
20-
second argument:
21-
22-
.. code-block:: php
23-
24-
$repository = Gitonomy\Git\Admin::init('/tmp/git_sandbox', false);
19+
Default behavior is to create a bare repository. If you want to initialize a
20+
repository with a working copy,pass ``false`` as third argument of Repository
21+
constructor.
2522

2623
Cloning repositories
27-
::::::::::::::::::::
24+
--------------------
2825

2926
You can clone a repository from an URL by doing:
3027

3128
.. code-block:: php
3229
30+
// Clone to a bare repository
3331
$repository = Gitonomy\Git\Admin::cloneTo('/tmp/gitlib', 'https://github.com/gitonomy/gitlib.git');
3432
35-
You can pass ``false`` and third argument to get a repository with a working copy.
33+
// Clone to a non-bare repository
34+
$repository = Gitonomy\Git\Admin::cloneTo('/tmp/gitlib', 'https://github.com/gitonomy/gitlib.git', false);
35+
36+
Default behavior is to clone in a bare repository.
37+
38+
Clone a Repository object
39+
-------------------------
3640

3741
If you already have a Repository instance and want to clone it, you can use this shortcut:
3842

3943
.. code-block:: php
4044
4145
$new = $repository->cloneTo('/tmp/clone');
4246
47+
Mirror a repository
48+
-------------------
49+
50+
If you want to mirror fully a repository and all references, use the ``mirrorTo`` method. This method
51+
takes only two arguments, where to mirror and what to mirror:
52+
53+
.. code-block:: php
54+
55+
// Mirror to a bare repository
56+
$mirror = Gitonomy\Git\Admin::mirrorTo('/tmp/mirror', 'https://github.com/gitonomy/gitlib.git');
57+
58+
// Mirror to a non-bare repository
59+
$mirror = Gitonomy\Git\Admin::mirrorTo('/tmp/mirror', 'https://github.com/gitonomy/gitlib.git', false);
60+
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
72+
4373
References
4474
::::::::::
4575

doc/api/repository.rst

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +0,0 @@
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-
Test if a repository is bare
12-
----------------------------
13-
14-
On a *Repository* object, you can method *isBare* to test it:
15-
16-
.. code-block:: php
17-
18-
if ($repository->isBare()) {
19-
echo "This repository is bare\n";
20-
}
21-
22-
Compute size of a repository
23-
----------------------------
24-
25-
To know how much size a repository is using on your drive, you can use
26-
``getSize`` method on a *Repository* object.
27-
28-
This method will basically compute the size of the folder, using system ``du`` tool.
29-
30-
The returned size is in kilobytes:
31-
32-
.. code-block:: php
33-
34-
$size = $repository->getSize();
35-
echo "Your repository size is ".$size."KB";
36-
37-
Access HEAD
38-
-----------
39-
40-
``HEAD`` represents in git the version you are working on (in working tree).
41-
Your ``HEAD`` can be attached (using a reference) or detached (using a commit).
42-
43-
.. code-block:: php
44-
45-
$head = $repository->getHead(); // Commit or Reference
46-
$head = $repository->getHeadCommit(); // Commit
47-
48-
if ($repository->isHeadDetached()) {
49-
echo "Sorry man\n";
50-
}
51-
52-
Logger
53-
------
54-
55-
If you are developing, you may appreciate to have a logger inside repository, telling
56-
you every executed command.
57-
58-
To do so, inject a logger inside the Repository:
59-
60-
.. code-block:: php
61-
62-
$repository->setLogger(new Monolog\Logger('repository'));
63-
64-
$repository->run('fetch', array('--all'));
65-
66-
This will output:
67-
68-
.. code-block:: text
69-
70-
info run command: fetch "--all"
71-
debug last command (fetch) duration: 23.24ms
72-
debug last command (fetch) return code: 0
73-
debug last command (fetch) output: Fetching origin

doc/debug.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Debug-mode
2+
==========
3+
4+
gitlib offers a debug mode, to make you see edge-cases of your usage. This is called
5+
debug-mode.
6+
7+
Debug-mode is enabled by default. If you disable it, gitlib will behave differently:
8+
9+
* when an error is met during execution, gitlib will try to minimize it, to not block
10+
execution flow. Errors will still be reporter in logger.
11+
* logs will be more verbose. They will contain every output, every return code, every
12+
possible information to ease debugging.
13+
14+
If you want to disable exceptions and try to minimize as much as possible errors, pass
15+
``false`` when construction a repository:
16+
17+
.. code-block:: php
18+
19+
$repository = new Gitonomy\Git\Repository($path'/tmp/repo', $debug = false)
20+
21+
``$debug`` argument should be available in every method you can use to create a
22+
repository.

doc/index.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,34 @@ to fetch informations from repository.
1717
$repository->run('fetch', array('--all'));
1818
1919
20+
Reference
21+
---------
22+
23+
.. toctree::
24+
:maxdepth: 1
25+
26+
api/create_repository
27+
api/hooks
28+
api/workingcopy
29+
api/commit
30+
api/blame
31+
api/blob
32+
api/branch
33+
api/tree
34+
api/log
35+
api/diff
36+
api/references
37+
api/revision
38+
39+
2040
Documentation
2141
-------------
2242

2343
.. toctree::
2444
:maxdepth: 2
2545

2646
installation
27-
api
47+
debug
2848
development
2949

3050
Missing features

src/Gitonomy/Git/Admin.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ 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
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
3032
*
3133
* @return Repository
3234
*
33-
* @throws RuntimeException Directory exists or not writable
35+
* @throws RuntimeException Directory exists or not writable (only if debug=true)
3436
*/
35-
public static function init($path, $bare = true, LoggerInterface $logger = null)
37+
public static function init($path, $bare = true, $debug = true, LoggerInterface $logger = null)
3638
{
3739
$builder = ProcessBuilder::create(array('git', 'init', '-q'));
3840

@@ -46,29 +48,69 @@ public static function init($path, $bare = true, LoggerInterface $logger = null)
4648
$process->run();
4749

4850
if (!$process->isSuccessFul()) {
49-
throw new \RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
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+
}
5060
}
5161

52-
return new Repository($path, null, $logger);
62+
return new Repository($path, $debug, $logger);
5363
}
5464

55-
public static function cloneTo($path, $url, $bare = true, LoggerInterface $logger = null)
65+
/**
66+
* Clone a repository to a local path.
67+
*
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
73+
*
74+
* @return Repository
75+
*/
76+
public static function cloneTo($path, $url, $bare = true, $debug = true, LoggerInterface $logger = null)
5677
{
5778
$options = array();
5879

5980
if ($bare) {
6081
$options[] = '--bare';
6182
}
6283

63-
return static::cloneRepository($path, $url, $options, $logger);
84+
return static::cloneRepository($path, $url, $options, $debug, $logger);
6485
}
6586

66-
public static function mirrorTo($path, $url, LoggerInterface $logger = null)
87+
/**
88+
* Mirrors a repository (fetch all revisions, not only branches).
89+
*
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
94+
*
95+
* @return Repository
96+
*/
97+
public static function mirrorTo($path, $url, $debug = true, LoggerInterface $logger = null)
6798
{
6899
return static::cloneRepository($path, $url, array('--mirror'), $logger);
69100
}
70101

71-
private static function cloneRepository($path, $url, array $options = array(), LoggerInterface $logger = null)
102+
/**
103+
* Internal method to launch effective ``git clone`` command.
104+
*
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
110+
*
111+
* @return Repository
112+
*/
113+
private static function cloneRepository($path, $url, array $options = array(), $debug = true, LoggerInterface $logger = null)
72114
{
73115
$builder = ProcessBuilder::create(array('git', 'clone', '-q'));
74116

@@ -86,6 +128,6 @@ private static function cloneRepository($path, $url, array $options = array(), L
86128
throw new \RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
87129
}
88130

89-
return new Repository($path, null, $logger);
131+
return new Repository($path, $debug, $logger);
90132
}
91133
}

0 commit comments

Comments
 (0)