forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGit.php
155 lines (140 loc) · 5.05 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
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
<?php
/* *****************************************************************
* @Author: wushuiyong
* @Created Time : 日 8/ 2 10:43:15 2015
*
* @File Name: command/Git.php
* @Description:
* *****************************************************************/
namespace app\components;
use app\models\Project;
class Git extends Command {
public function updateRepo($branch = 'master', $gitDir = null) {
$gitDir = $gitDir ?: Project::getDeployFromDir();
$dotGit = rtrim($gitDir, '/') . '/.git';
// 存在git目录,直接pull
if (file_exists($dotGit)) {
$cmd[] = sprintf('cd %s ', $gitDir);
$cmd[] = sprintf('/usr/bin/env git checkout %s', $branch);
$cmd[] = sprintf('/usr/bin/env git fetch --all');
$cmd[] = sprintf('/usr/bin/env git reset --hard origin/%s', $branch);
$command = join(' && ', $cmd);
return $this->runLocalCommand($command);
}
// 不存在,则先checkout
else {
$cmd[] = sprintf('mkdir -p %s ', $gitDir);
$cmd[] = sprintf('cd %s ', $gitDir);
$cmd[] = sprintf('/usr/bin/env git clone %s .', $this->getConfig()->git_url);
$cmd[] = sprintf('/usr/bin/env git checkout %s', $branch);
$command = join(' && ', $cmd);
return $this->runLocalCommand($command);
}
}
/**
* 更新到指定commit版本
*
* @param string $commit
* @return bool
*/
public function updateToVersion($branch, $commit, $version) {
// 先更新
$destination = Project::getDeployWorkspace($version);
$this->updateRepo($branch, $destination);
$cmd[] = sprintf('cd %s ', $destination);
$cmd[] = sprintf('/usr/bin/env git reset %s', $commit);
$cmd[] = '/usr/bin/env git checkout .';
$command = join(' && ', $cmd);
return $this->runLocalCommand($command);
}
/**
* 获取分支列表
*
* @return array
*/
public function getBranchList() {
$destination = Project::getDeployFromDir();
// 先更新,其实没有必要更新
///$this->updateRepo('master', $destination);
$cmd[] = sprintf('cd %s ', $destination);
$cmd[] = '/usr/bin/env git pull';
$cmd[] = '/usr/bin/env git branch -a';
$command = join(' && ', $cmd);
$result = $this->runLocalCommand($command);
if (!$result) {
throw new \Exception('获取分支列表失败:' . $this->getExeLog());
}
$history = [];
$list = explode(PHP_EOL, $this->getExeLog());
foreach ($list as &$item) {
$item = trim($item);
$remotePrefix = 'remotes/origin/';
$remoteHeadPrefix = 'remotes/origin/HEAD';
// 只取远端的分支,排除当前分支
if (strcmp(substr($item, 0, strlen($remotePrefix)), $remotePrefix) === 0
&& strcmp(substr($item, 0, strlen($remoteHeadPrefix)), $remoteHeadPrefix) !== 0) {
$item = substr($item, strlen($remotePrefix));
$history[] = [
'id' => $item,
'message' => $item,
];
}
}
return $history;
}
/**
* 获取提交历史
*
* @return array
*/
public function getCommitList($branch = 'master', $count = 20) {
// 先更新
$destination = Project::getDeployFromDir();
$this->updateRepo($branch, $destination);
$cmd[] = sprintf('cd %s ', $destination);
$cmd[] = '/usr/bin/env git log -' . $count . ' --pretty="%h - %an %s" ';
$command = join(' && ', $cmd);
$result = $this->runLocalCommand($command);
if (!$result) {
throw new \Exception('获取提交历史失败:' . $this->getExeLog());
}
$history = [];
// 总有一些同学没有团队协作意识,不设置好编码:(
$log = GlobalHelper::convert2Utf8($this->getExeLog());
$list = explode(PHP_EOL, $log);
foreach ($list as $item) {
$commitId = substr($item, 0, strpos($item, '-') - 1);
$history[] = [
'id' => $commitId,
'message' => $item,
];
}
return $history;
}
/**
* 获取tag记录
*
* @return array
*/
public function getTagList($count = 20) {
// 先更新
$this->updateRepo();
$destination = Project::getDeployFromDir();
$cmd[] = sprintf('cd %s ', $destination);
$cmd[] = '/usr/bin/env git tag -l ';
$command = join(' && ', $cmd);
$result = $this->runLocalCommand($command);
if (!$result) {
throw new \Exception('获取tag记录失败:' . $this->getExeLog());
}
$history = [];
$list = explode(PHP_EOL, $this->getExeLog());
foreach ($list as $item) {
$history[] = [
'id' => $item,
'message' => $item,
];
}
return $history;
}
}