Skip to content

Commit bfc6f1f

Browse files
bobfloatsroot
authored and
root
committed
Added simple tag support
1 parent c8d778c commit bfc6f1f

File tree

8 files changed

+673
-0
lines changed

8 files changed

+673
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
// src/VersionControl/GitCommandBundle/Entity/FileInfo.php
3+
4+
/*
5+
* This file is part of the GitCommandBundle package.
6+
*
7+
* (c) Paul Schweppe <paulschweppe@gmail.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace VersionControl\GitCommandBundle\Entity;
14+
15+
/**
16+
* Remote File Info.
17+
*
18+
* "git for-each-ref --format '%(refname:short)|%(subject)|%(taggerDate)|%(taggerName)|%(taggerEmail)|%(*objectname)|%(*objectname:short)' refs/tags --sort=taggerDate";
19+
20+
*
21+
* @author Paul Schweppe <paulschweppe@gmail.com>
22+
*/
23+
class GitTag{
24+
25+
/**
26+
* Tag name.
27+
*
28+
* @var string
29+
*/
30+
protected $name;
31+
32+
/**
33+
* Tags subject.
34+
*
35+
* @var string
36+
*/
37+
protected $subject;
38+
39+
/**
40+
* Tag date.
41+
*
42+
* @var \DateTime
43+
*/
44+
protected $taggerDate;
45+
46+
47+
48+
/**
49+
* Taggers name.
50+
*
51+
* @var string
52+
*/
53+
protected $taggerName;
54+
55+
/**
56+
* Taggers email.
57+
*
58+
* @var string
59+
*/
60+
protected $taggerEmail;
61+
62+
/**
63+
* Commit hash tag points to.
64+
*
65+
* @var string
66+
*/
67+
protected $commitHash;
68+
69+
/**
70+
* Abbreviated commit hash that tag points to.
71+
*
72+
* @var string
73+
*/
74+
protected $commitAbbrHash;
75+
76+
77+
78+
public function __construct($line)
79+
{
80+
$data = explode('|', $line);
81+
if (count($data) >= 7) {
82+
$this->setName($data[0]);
83+
$this->setSubject($data[1]);
84+
$this->setTaggerDate(new \DateTime($data[2]));
85+
$this->setTaggerName($data[3]);
86+
$this->setTaggerEmail($data[4]);
87+
$this->setCommitHash($data[5]);
88+
$this->setCommitAbbrHash($data[6]);
89+
}
90+
}
91+
92+
public function getName()
93+
{
94+
return $this->name;
95+
}
96+
97+
public function getTaggerDate(): \DateTime
98+
{
99+
return $this->taggerDate;
100+
}
101+
102+
public function getSubject()
103+
{
104+
return $this->subject;
105+
}
106+
107+
public function getTaggerName()
108+
{
109+
return $this->taggerName;
110+
}
111+
112+
public function getTaggerEmail()
113+
{
114+
return $this->taggerEmail;
115+
}
116+
117+
public function getCommitHash()
118+
{
119+
return $this->commitHash;
120+
}
121+
122+
public function getCommitAbbrHash()
123+
{
124+
return $this->commitAbbrHash;
125+
}
126+
127+
public function setName($name)
128+
{
129+
$this->name = $name;
130+
return $this;
131+
}
132+
133+
public function setTaggerDate(\DateTime $taggerDate)
134+
{
135+
$this->taggerDate = $taggerDate;
136+
return $this;
137+
}
138+
139+
public function setSubject($subject)
140+
{
141+
$this->subject = $subject;
142+
return $this;
143+
}
144+
145+
public function setTaggerName($taggerName)
146+
{
147+
$this->taggerName = $taggerName;
148+
return $this;
149+
}
150+
151+
public function setTaggerEmail($taggerEmail)
152+
{
153+
$this->taggerEmail = $taggerEmail;
154+
return $this;
155+
}
156+
157+
public function setCommitHash($commitHash)
158+
{
159+
$this->commitHash = $commitHash;
160+
return $this;
161+
}
162+
163+
public function setCommitAbbrHash($commitAbbrHash)
164+
{
165+
$this->commitAbbrHash = $commitAbbrHash;
166+
return $this;
167+
}
168+
169+
170+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
// src/VersionControl/GitCommandBundle/GitCommands/GitTagCommand.php
3+
4+
/*
5+
* This file is part of the GitCommandBundle package.
6+
*
7+
* (c) Paul Schweppe <paulschweppe@gmail.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace VersionControl\GitCommandBundle\GitCommands\Command;
14+
15+
use VersionControl\GitCommandBundle\GitCommands\Exception\DeleteBranchException;
16+
use VersionControl\GitCommandBundle\GitCommands\Exception\RunGitCommandException;
17+
use VersionControl\GitCommandBundle\GitCommands\Exception\InvalidBranchNameException;
18+
use VersionControl\GitCommandBundle\Entity\GitTag;
19+
20+
/**
21+
* @author Paul Schweppe <paulschweppe@gmail.com>
22+
*/
23+
class GitTagCommand extends AbstractGitCommand
24+
{
25+
26+
/**
27+
* List all of the Tags in your repository.
28+
*
29+
* @param bool $local Flag to list local branches only
30+
*
31+
* @return array | VersionControl\GitCommandBundle\Entity\GitTag
32+
*/
33+
public function getTags()
34+
{
35+
$tagEntities = [];
36+
$command = "git for-each-ref --format '%(refname:short)|%(subject)|%(taggerdate)|%(taggername)|%(taggeremail)|%(*objectname)|%(*objectname:short)' refs/tags --sort=taggerdate";
37+
38+
$tags = $this->command->runCommand($command);
39+
$lines = $this->splitOnNewLine($tags, true);
40+
41+
foreach ($lines as $line){
42+
$tagEntities[] = new GitTag($line);
43+
}
44+
45+
return $tagEntities;
46+
}
47+
48+
49+
/**
50+
* Creates a new Tag.
51+
*
52+
* @param string $branchName Name of new branch
53+
* @param bool $switchToBranch If true the new branch is checked out
54+
*
55+
* @return string command response
56+
*/
57+
public function createAnnotatedTag($version, $message, $commitShortCode = false )
58+
{
59+
if ($this->validateTagName($version)) {
60+
$command = sprintf('git tag -a %s -m "%s"', escapeshellarg($version), escapeshellarg($message));
61+
if($commitShortCode){
62+
$command .= ' '.escapeshellarg($commitShortCode);
63+
}
64+
$output = $this->command->runCommand($command);
65+
} else {
66+
throw new InvalidBranchNameException('This is not a valid branch name');
67+
}
68+
69+
return $output;
70+
}
71+
72+
73+
74+
/**
75+
* Validates Tag name. Checks if a tag name is allowed.
76+
*
77+
* @param string $tagName Name of new branch
78+
*
79+
* @return bool true if valid branch name
80+
*/
81+
public function validateTagName($tagName)
82+
{
83+
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
84+
return true;
85+
} else {
86+
//$output = $this->command->runCommand(sprintf('(git check-ref-format "refs/heads/%s");echo -e "\n$?"',$branchName));
87+
$response = $this->command->runCommand(sprintf('git check-ref-format "refs/tags/%s"', $tagName), false);
88+
89+
if ($this->command->getLastExitStatus() !== 0) {
90+
return false;
91+
}
92+
}
93+
94+
return true;
95+
}
96+
97+
}

src/VersionControl/GitCommandBundle/GitCommands/GitCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ public function command($name)
391391
case 'branch':
392392
$command = new Command\GitBranchCommand($this);
393393
break;
394+
case 'tag':
395+
$command = new Command\GitTagCommand($this);
396+
break;
394397
case 'commit':
395398
$command = new Command\GitCommitCommand($this);
396399
break;

0 commit comments

Comments
 (0)