-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgit_command.php
163 lines (122 loc) · 4.13 KB
/
git_command.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
<?php
require_once($CFG->libdir.'/filelib.php');
class git_command {
private $workspace;
private $command;
private static $terminals = array();
private function __construct($workspace = null) {
global $ASSIGNMENT_GITHUB;
$default = $ASSIGNMENT_GITHUB->workspace;
if (!is_dir("$default")) {
mkdir($default, 0777);
}
clearstatcache();
if (empty($workspace) || !is_dir("$workspace") || !is_writable("$workspace")) {
$workspace = $default;
}
$this->workspace = $workspace;
$this->command = $ASSIGNMENT_GITHUB->command;
}
public static function init($workspace = null) {
$cmd = new git_command($workspace);
$workspace = $cmd->get_workspace();
if (empty(self::$terminals[$workspace])) {
self::$terminals[$workspace] = $cmd;
}
return self::$terminals[$workspace];
}
function get_workspace() {
return $this->workspace;
}
function prepare_params() {
global $ASSIGNMENT_GITHUB;
$param = new stdClass();
$param->worktree = '';
$param->url = '';
$param->branch = $ASSIGNMENT_GITHUB->branch;
$param->other = array();
return $param;
}
function exec($command, $param) {
if (empty($this->workspace)) {
return false;
}
$command = 'git_'.$command;
if (method_exists($this, $command)) {
return $this->$command($param);
}
return false;
}
private function git_clone($param) {
$dir = $this->get_worktree($param);
$param_string = sprintf('clone -b %s %s %s', $param->branch, escapeshellarg($param->url), escapeshellarg($dir));
return $this->run($this->workspace, $this->command, $param_string);
}
private function git_pull($param) {
$dir = $this->get_worktree($param);
$param_string = 'pull';
return $this->run($dir, $this->command, $param_string);
}
private function git_log($param) {
$dir = $this->get_worktree($param);
$param_string = 'log';
foreach($param->other as $p) {
$param_string .= ' '.$p;
}
return $this->run($dir, $this->command, $param_string);
}
private function git_show($param) {
$dir = $this->get_worktree($param);
$param_string = 'show';
foreach($param->other as $p) {
$param_string .= ' '.$p;
}
return $this->run($dir, $this->command, $param_string);
}
private function git_branch($param) {
$dir = $this->get_worktree($param);
$param_string = 'branch';
foreach($param->other as $p) {
$param_string .= ' '.$p;
}
return $this->run($dir, $this->command, $param_string);
}
private function git_remote($param) {
$dir = $this->get_worktree($param);
$param_string = 'remote';
foreach($param->other as $p) {
$param_string .= ' '.$p;
}
return $this->run($dir, $this->command, $param_string);
}
private function git_delete($param) {
$dir = $this->get_worktree($param);
return fulldelete($dir);
}
private function get_worktree($param) {
if (substr($param->worktree, 0, 1) == '/') {
return $param->worktree;
}
return "{$this->workspace}/{$param->worktree}";
}
public function delete($pattern) {
$worktrees = scandir($this->workspace);
foreach($worktrees as $worktree) {
if ($worktree != '.' && $worktree != '..') {
if (preg_match($pattern, $worktree)) {
fulldelete($this->workspace.'/'.$worktree);
}
}
}
}
public function run($dir, $command, $param_string) {
$command = sprintf('cd %s && %s %s', escapeshellarg($dir), $command, $param_string);
ob_start();
passthru($command, $return_var);
$output = ob_get_clean();
if ($return_var !== 0) {
throw new Exception($output, $return_var);
}
return trim($output);
}
}