-
Notifications
You must be signed in to change notification settings - Fork 14
/
AGitTag.php
executable file
·164 lines (145 loc) · 3.5 KB
/
AGitTag.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Represents a git tag.
*
* @author Charles Pick
* @author Jonas Girnatis <dermusterknabe@gmail.com>
* @package packages.git
*/
class AGitTag extends CComponent
{
/**
* The name of the tag
* @var string
*/
public $name;
/**
* The repository this tag belongs to
* @var AGitRepository
*/
public $repository;
/**
* The remote repository this tag belongs to, if any
* @var AGitRemote
*/
public $remote;
/**
* The name of the author of the tag
* @var string
*/
protected $_authorName = null;
/**
* The email address of the author of the tag
* @var string
*/
protected $_authorEmail = null;
/**
* The message for this tag
* @var string
*/
protected $_message = null;
/**
* The commit that this tag points to
* @var AGitCommit
*/
protected $_commit = null;
/**
* Constructor.
* @param string $name the name of the tag
* @param AGitBranch|null $branch the branch this tag belongs to
*/
public function __construct($name, AGitRepository $repository, AGitRemote $remote = null)
{
$this->repository = $repository;
$this->name = $name;
$this->remote = $remote;
}
/**
* Loads the metadata for the tag
*/
protected function loadData()
{
$delimiter = '|||||-----|||||-----|||||';
$command = 'show --pretty=format:"'.$delimiter.'%H'.$delimiter.'" '.$this->name;
$response = explode($delimiter,$this->repository->run($command));
$tagData = $response[0];
$commitHash = $response[1];
if (strpos($tagData, "tag $this->name") === 0) { //annotated tag
if(preg_match("/Tagger: (.*)\n/", $tagData, $matches)){
$tagger = $matches[1];
if (preg_match("/(.*) <(.*)>/u", $tagger,$matches)) {
$this->_authorEmail = trim(array_pop($matches));
$this->_authorName = trim(array_pop($matches));
}
else {
$this->_authorName = $tagger;
}
$this->_message = trim(preg_replace("/.*\nTagger: .*\n/", '', $tagData));
}
}
$this->_commit = new AGitCommit($commitHash, $this->repository);
}
/**
* Returns the name of the author of the tag
* @return string name of tag author
*/
public function getAuthorName()
{
if(is_null($this->_authorName)){
$this->loadData();
}
return $this->_authorName;
}
/**
* Returns the email address of the author of the tag
* @return string email address of tag author
*/
public function getAuthorEmail()
{
if(is_null($this->_authorEmail)){
$this->loadData();
}
return $this->_authorEmail;
}
/**
* Returns the tag description
* @return string description of the tag
*/
public function getMessage()
{
if(is_null($this->_message)){
$this->loadData();
}
return $this->_message;
}
/**
* Gets the commit this tag points to
* @return AGitCommit the commit this tag points to
*/
public function getCommit()
{
if(is_null($this->_commit)){
$this->loadData();
}
return $this->_commit;
}
/**
* Pushes a tag to a remote repository.
* @param null|string|AGitRemote $remote the remote repository to push to. If null, falls back to repository $defaultRemote.
* @return string the response from git
*/
public function push($remote = null)
{
if (is_null($remote)) {
$remote = $this->repository->remote;
}
return $this->repository->run("push $remote tag $this->name");
}
/**
* @return string tag name
*/
public function __toString()
{
return $this->name;
}
}