This repository was archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGitCheckoutTask.php
executable file
·108 lines (88 loc) · 2.06 KB
/
GitCheckoutTask.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
<?php
require_once 'GitTask.php';
/**
* Perform a checkout in git.
* @author Beau Simensen <[email protected]>
*/
class GitCheckoutTask extends GitTask {
/**
* Tag name to checkout
* @var string
*/
private $_tag_name = null;
/**
* Branch name to checkout
* @var string
*/
private $_branch_name = null;
/**
* Path to cloned repository
* @var string
*/
private $_path = null;
/**
* Sets the name for the git tag.
*/
public function setTag($tag_name) {
$this->_tag_name = $tag_name;
}
/**
* Sets the name for the git branch.
*/
public function setBranch($branch_name) {
$this->_branch_name = $branch_name;
}
/**
* Sets the target path for the cloned repository.
*/
public function setPath($path) {
$this->_path = $path;
}
/**
* Main entry point.
*/
public function main() {
if(false == isset($this->_path)) {
$this->log("GitCheckoutTask Fail: PATH must be set!\n");
exit(1);
}
if(false == isset($this->_tag_name) and false == isset($this->_branch_name)) {
$this->log("GitCheckoutTask Fail: TAG or BRANCH must be set!\n");
exit(1);
}
$which = null;
$dir = getcwd();
chdir($this->_path);
if ( $this->_tag_name !== null ) {
$which = $this->_tag_name;
}
if ( $this->_branch_name !== null ) {
$branch_exists = false;
$check_cmd = $this->git_path . ' branch';
$output = array();
$output_string = exec($check_cmd, $output, $return);
$this->log('Executing: ' . $check_cmd);
foreach ( $output as $branch ) {
$branch = preg_replace('/^[\W]*/', '', $branch);
if ( $branch == $this->_branch_name ) {
$branch_exists = true;
break;
}
}
$args = $branch_exists ? '' : ' -b ';
$addn = $branch_exists ? '' : ' origin/' . $this->_branch_name;
$which = $args . $this->_branch_name . $addn;
}
$cmd = $this->git_path . ' checkout ' . $which;
$this->log("Running " . $cmd);
passthru($cmd, $return);
$this->log("Return: " . $return);
chdir($dir);
if(intval($return) > 0) {
if ( intval)
$this->log("Git Checkout Failed.");
exit(1);
}
}
}
?>