-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Git.php
75 lines (63 loc) · 1.86 KB
/
Git.php
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
<?php
namespace Couscous\CommandRunner;
/**
* Wrapper around git commands.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class Git
{
/**
* @var CommandRunner
*/
private $commandRunner;
public function __construct(CommandRunner $commandRunner)
{
$this->commandRunner = $commandRunner;
}
public function cloneRepository($url, $directory)
{
$this->commandRunner->run("git clone $url \"$directory\"");
}
public function checkoutOriginBranch($directory, $branch)
{
$this->run($directory, "git checkout -b $branch origin/$branch");
}
public function createBranch($directory, $branch)
{
$this->run($directory, "git checkout -b $branch");
}
public function commitAllChanges($directory, $message)
{
$this->run($directory, "git add --all . && git commit -m \"$message\"");
}
public function push($directory, $branch, $remote = 'origin')
{
$this->run($directory, "git push $remote $branch");
}
/**
* @param string $remote Remote name
*
* @return string The git URL
*/
public function getRemoteUrl($remote = 'origin')
{
$command = "git config --get remote.$remote.url";
return trim($this->commandRunner->run($command));
}
/**
* Check if the git repository in the provided directory has any uncommitted changes
*
* @param string $directory A directory containing a git repository
* @return bool True if there are changes, false otherwise
*/
public function hasUncommittedChanges($directory)
{
$changes = $this->run($directory, "git diff-index --name-only HEAD");
return !(ctype_space($changes) || $changes = '');
}
private function run($directory, $command)
{
return $this->commandRunner->run("cd \"$directory\" && $command");
}
}