|
| 1 | +<?php |
| 2 | + |
| 3 | +class git_command { |
| 4 | + |
| 5 | + private $workspace; |
| 6 | + |
| 7 | + function __construct($workspace = null) { |
| 8 | + if (!$workspace || !is_dir($workspace)) { |
| 9 | + return null; |
| 10 | + } |
| 11 | + $this->workspace = $workspace; |
| 12 | + } |
| 13 | + |
| 14 | + function prepare_params() { |
| 15 | + |
| 16 | + $param = new stdClass(); |
| 17 | + $param->work_tree = ''; |
| 18 | + $param->git = ''; |
| 19 | + $param->target_dir = ''; |
| 20 | + $param->other = array(); |
| 21 | + return $param; |
| 22 | + } |
| 23 | + |
| 24 | + function exec($command, $param) { |
| 25 | + |
| 26 | + $command = 'git_'.$command; |
| 27 | + $dir = getcwd(); |
| 28 | + chdir($this->workspace); |
| 29 | + if (method_exists($this, $command)) { |
| 30 | + $result = $this->$command($param); |
| 31 | + } else { |
| 32 | + $result = false; |
| 33 | + } |
| 34 | + chdir($dir); |
| 35 | + return $result; |
| 36 | + } |
| 37 | + |
| 38 | + private function git_clone($param) { |
| 39 | + |
| 40 | + if (!$param->git || !$param->target_dir) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $command = 'git clone '.$param->git.' '.$param->target_dir; |
| 45 | + return shell_exec($command); |
| 46 | + } |
| 47 | + |
| 48 | + private function git_pull($param) { |
| 49 | + |
| 50 | + if (!is_dir($param->work_tree)) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + chdir($param->work_tree); |
| 55 | + $command = 'git pull'; |
| 56 | + return shell_exec($command); |
| 57 | + } |
| 58 | + |
| 59 | + private function git_log($param) { |
| 60 | + |
| 61 | + if (!is_dir($param->work_tree)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + chdir($param->work_tree); |
| 66 | + $command = 'git log'; |
| 67 | + foreach($param->other as $p) { |
| 68 | + $command .= ' '.$p; |
| 69 | + } |
| 70 | + |
| 71 | + return shell_exec($command); |
| 72 | + } |
| 73 | +} |
0 commit comments