forked from gitonomy/gitlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingCopy.php
More file actions
96 lines (79 loc) · 2.42 KB
/
WorkingCopy.php
File metadata and controls
96 lines (79 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <alexandre.salome@gmail.com>
* (c) Julien DIDIER <genzo.wm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git;
use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\LogicException;
/**
* @author Alexandre Salomé <alexandre.salome@gmail.com>
*/
class WorkingCopy
{
/**
* @var Repository
*/
protected $repository;
public function __construct(Repository $repository)
{
$this->repository = $repository;
if ($this->repository->isBare()) {
throw new LogicException('Can\'t create a working copy on a bare repository');
}
}
public function getUntrackedFiles()
{
$lines = explode("\0", $this->run('status', ['--porcelain', '--untracked-files=all', '-z']));
$lines = array_filter($lines, function ($l) {
return substr($l, 0, 3) === '?? ';
});
$lines = array_map(function ($l) {
return substr($l, 3);
}, $lines);
return $lines;
}
public function getDiffPending()
{
$diff = Diff::parse($this->run('diff', ['-r', '-p', '--raw', '-m', '-M', '--full-index']));
$diff->setRepository($this->repository);
return $diff;
}
public function getDiffStaged()
{
$diff = Diff::parse($this->run('diff', ['-r', '-p', '--raw', '-m', '-M', '--full-index', '--staged']));
$diff->setRepository($this->repository);
return $diff;
}
/**
* @return WorkingCopy
*/
public function checkout($revision, $branch = null)
{
$args = [];
if ($revision instanceof Commit) {
$args[] = $revision->getHash();
} elseif ($revision instanceof Reference) {
$args[] = $revision->getFullname();
} elseif (is_string($revision)) {
$args[] = $revision;
} else {
throw new InvalidArgumentException(sprintf('Unknown type "%s"', gettype($revision)));
}
if (null !== $branch) {
$args = array_merge($args, ['-b', $branch]);
}
$this->run('checkout', $args);
return $this;
}
protected function run($command, array $args = [])
{
return $this->repository->run($command, $args);
}
}