-
Notifications
You must be signed in to change notification settings - Fork 14
/
AGitRemote.php
executable file
·119 lines (108 loc) · 2.83 KB
/
AGitRemote.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Represents a git remote repository
*
* @author Charles Pick
* @author Jonas Girnatis <[email protected]>
* @package packages.git
*/
class AGitRemote extends CComponent {
/**
* The name of the remote git repository
* @var string
*/
public $name;
/**
* The url to use when fetching from the remote repository
* @var string
*/
public $fetchUrl;
/**
* The url to use when pushing to the remote repository
* @var string
*/
public $pushUrl;
/**
* The main repository this remote belongs to
* @var AGitRepository
*/
public $repository;
/**
* Constructor
* @param string $name the name of the remote repository
* @param AGitRepository $repository the main git repository this remote belongs to
*/
public function __construct($name, AGitRepository $repository)
{
$this->repository = $repository;
$this->name = $name;
}
/**
* Gets a list of git branches for this remote repository
* @return AGitBranch[] an array of git branches
*/
public function getBranches()
{
$branches = array();
foreach(explode("\n",$this->repository->run("ls-remote --heads " . $this->name)) as $ref) {
$ref = explode('refs/heads/', trim($ref), 2);
$branchName = $ref[1];
$branch = new AGitBranch($branchName,$this->repository,$this);
$branches[$branchName] = $branch;
}
return $branches;
}
/**
* Checks if this remote repository has a specific branch
* @param string $branch branch name
* @return bool true if remote repository has specific branch, false otherwise
*/
public function hasBranch($branch)
{
$branches = $this->getBranches();
return isset($branches[$branch]);
}
/**
* Deletes the remote branch with the given name
* @param string $branchName the branch name
* @return string the response from git
*/
public function deleteBranch($branchName)
{
return $this->repository->run("push $this->name :$branchName");
}
/**
* Gets a list of tags for this remote repository
* @return AGitTag[] an array of tags
*/
public function getTags()
{
$tags = array();
foreach(explode("\n",$this->repository->run("ls-remote --tags " . $this->name)) as $i => $ref) {
if($i == 0) { continue; } //ignore first line "From: repository..."
if(substr_count($ref, '^{}')){ continue; } //ignore dereferenced tag objects for annotated tags
$ref = explode('refs/tags/', trim($ref), 2);
$tagName = $ref[1];
$tag = new AGitTag($tagName,$this->repository,$this);
$tags[$tagName] = $tag;
}
return $tags;
}
/**
* Checks if this remote repository has a specific tag
* @param string $tag tag name
* @return bool true if remote repository has specific tag, false otherwise
*/
public function hasTag($tag)
{
$tags = $this->getTags();
return isset($tags[$tag]);
}
/**
* @return string remote name
*/
public function __toString()
{
return $this->name;
}
}