Skip to content

Commit f30acf6

Browse files
author
alexandresalome
committed
start implementing #6
1 parent 09d51b5 commit f30acf6

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

Diff for: src/Gitonomy/Git/Remote.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Gitonomy.
5+
*
6+
* (c) Alexandre Salomé <[email protected]>
7+
* (c) Julien DIDIER <[email protected]>
8+
*
9+
* This source file is subject to the GPL license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Gitonomy\Git;
14+
15+
/**
16+
* @author Alexandre Salomé <[email protected]>
17+
*/
18+
class Remote
19+
{
20+
protected $repository;
21+
protected $name;
22+
protected $urls;
23+
24+
public function __construct(Repository $repository, $name, array $urls = array())
25+
{
26+
$this->repository = $repository;
27+
$this->name = $name;
28+
$this->urls = $urls;
29+
}
30+
31+
public function getName()
32+
{
33+
return $this->name;
34+
}
35+
36+
public function getLocalReferences()
37+
{
38+
$name = $this->name;
39+
return array_filter((array) $this->repository->getReferences()->getAll(), function ($ref) use ($name) {
40+
return preg_match('#^refs/remotes/'.preg_quote($name).'/#', $ref->getFullname());
41+
});
42+
}
43+
}

Diff for: src/Gitonomy/Git/RemoteBag.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Gitonomy.
5+
*
6+
* (c) Alexandre Salomé <[email protected]>
7+
* (c) Julien DIDIER <[email protected]>
8+
*
9+
* This source file is subject to the GPL license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Gitonomy\Git;
14+
15+
/**
16+
*
17+
* @author Alexandre Salomé <[email protected]>
18+
*/
19+
class RemoteBag implements \IteratorAggregate
20+
{
21+
protected $repository;
22+
23+
protected $remotes;
24+
25+
public function __construct(Repository $repository)
26+
{
27+
$this->repository = $repository;
28+
}
29+
30+
public function getAll()
31+
{
32+
if (null !== $this->remotes) {
33+
return $this->remotes;
34+
}
35+
36+
$read = array();
37+
$output = $this->repository->run('remote', array('-v'));
38+
$exp = explode("\n", $output);
39+
foreach ($exp as $i => $line) {
40+
if ($line == "") {
41+
continue;
42+
}
43+
44+
if (!preg_match('/(?P<name>[^\s]+)\s(?P<url>[^\s]+)\s\((?P<type>\w+)\)$/', $line, $vars)) {
45+
throw new \RuntimeException(sprintf('Unexpected git remote output. Expected "name url (type)", got "%s" at line %s :'."\n".$output, $line, $i + 1));
46+
}
47+
48+
$name = $vars['name'];
49+
$type = $vars['type'];
50+
$url = $vars['url'];
51+
$read[$name][$type] = $url;
52+
}
53+
54+
foreach ($read as $name => $urls) {
55+
$this->remotes[$name] = new Remote($this->repository, $name, $urls);
56+
}
57+
58+
return $this->remotes;
59+
}
60+
61+
public function fetch($name = null)
62+
{
63+
if (null === $name) {
64+
$args = array('--all');
65+
} else {
66+
$args = array($name);
67+
}
68+
69+
$this->repository->run('fetch', $args);
70+
71+
return $this;
72+
}
73+
74+
public function getIterator()
75+
{
76+
return new \ArrayIterator($this->getAll());
77+
}
78+
}

Diff for: src/Gitonomy/Git/Repository.php

+21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class Repository
8181
*/
8282
protected $debug;
8383

84+
/**
85+
* Bag containing remotes and remote references.
86+
*
87+
* @var RemoteBag
88+
*/
89+
protected $remoteBag;
90+
8491
/**
8592
* Environment variables that should be set for every running process.
8693
*
@@ -309,6 +316,20 @@ public function getReferences()
309316
return $this->referenceBag;
310317
}
311318

319+
/**
320+
* Returns the remote list associated to the repository.
321+
*
322+
* @return RemoteBag
323+
*/
324+
public function getRemotes()
325+
{
326+
if (null === $this->remoteBag) {
327+
$this->remoteBag = new RemoteBag($this);
328+
}
329+
330+
return $this->remoteBag;
331+
}
332+
312333
/**
313334
* Instanciates a commit object or fetches one from the cache.
314335
*

0 commit comments

Comments
 (0)