scm.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. class scm
  3. {
  4. public $engine;
  5. /**
  6. * Set engine.
  7. *
  8. * @param object $repo
  9. * @access public
  10. * @return void
  11. */
  12. public function setEngine($repo)
  13. {
  14. $scm = strtolower($repo->SCM);
  15. $className = $scm . 'Repo';
  16. if($scm == 'git') $scm = 'gitrepo';
  17. if(!class_exists($className)) require($scm . '.class.php');
  18. $this->engine = new $className($repo->client, in_array($scm, array('gitlab', 'gitfox')) ? $repo->apiPath : $repo->path, $repo->account, $repo->password, $repo->encoding, $repo);
  19. }
  20. /**
  21. * List files.
  22. *
  23. * @param string $path
  24. * @param string $revision
  25. * @access public
  26. * @return array
  27. */
  28. public function ls($path, $revision = 'HEAD')
  29. {
  30. if(!scm::checkRevision($revision)) return array();
  31. return $this->engine->ls($path, $revision);
  32. }
  33. /**
  34. * Get tags.
  35. *
  36. * @param string $path svn use path, git service filter return data.
  37. * @param string $revision
  38. * @param bool $onlyDir
  39. * @param string $orderBy
  40. * @param int $limit
  41. * @param int $pageID
  42. * @access public
  43. * @return array
  44. */
  45. public function tags($path = '', $revision = 'HEAD', $onlyDir = true, $orderBy = '', $limit = 0, $pageID = 1)
  46. {
  47. if(!scm::checkRevision($revision)) return array();
  48. return $this->engine->tags($path, $revision, $onlyDir, $orderBy, $limit, $pageID);
  49. }
  50. /**
  51. * Get branch.
  52. *
  53. * @access public
  54. * @param string $showDetail
  55. * @param string $orderBy
  56. * @param int $limit
  57. * @param int $pageID
  58. * @param string $label
  59. * @param int $showArchived
  60. * @return array
  61. */
  62. public function branch($showDetail = '', $orderBy = '', $limit = 0, $pageID = 1, $label = '', $showArchived = 0)
  63. {
  64. return $this->engine->branch($showDetail, $orderBy, $limit, $pageID, $label, $showArchived);
  65. }
  66. /**
  67. * Create a branch.
  68. *
  69. * @param string $branchName
  70. * @param string $ref
  71. * @access public
  72. * @return bool
  73. */
  74. public function createBranch($branchName = '', $ref = 'master')
  75. {
  76. if(get_class($this->engine) == 'subversion') return false;
  77. return $this->engine->createBranch($branchName, $ref);
  78. }
  79. /**
  80. * 创建标签。
  81. * Creates a new tag with the given name and optional comment.
  82. *
  83. * @param string $tagName The name of the tag to be created.
  84. * @param string $ref The revision from which the tag is created, it can be a commit SHA, another tag name, or branch name.
  85. * @param string $comment An optional comment for the tag.
  86. * @return array|false Returns false if the engine is Subversion, otherwise returns the result of the createTag method of the engine object.
  87. */
  88. public function createTag($tagName, $ref, $comment = '')
  89. {
  90. if(!in_array(get_class($this->engine), array('gitlabRepo', 'gitfoxRepo'))) return false;
  91. return $this->engine->createTag($tagName, $ref, $comment);
  92. }
  93. /**
  94. * Get log.
  95. *
  96. * @param string $path
  97. * @param string $fromRevision
  98. * @param string $toRevision
  99. * @param int $count
  100. * @access public
  101. * @return array
  102. */
  103. public function log($path, $fromRevision = 0, $toRevision = 'HEAD', $count = 0)
  104. {
  105. if(!scm::checkRevision($fromRevision)) return array();
  106. if(!scm::checkRevision($toRevision)) return array();
  107. return $this->engine->log($path, $fromRevision, $toRevision);
  108. }
  109. /**
  110. * Blame file.
  111. *
  112. * @param string $path
  113. * @param string $revision
  114. * @param bool $showComment
  115. * @access public
  116. * @return array
  117. */
  118. public function blame($path, $revision, $showComment = true)
  119. {
  120. if(!scm::checkRevision($revision)) return array();
  121. return $this->engine->blame($path, $revision, $showComment);
  122. }
  123. /**
  124. * Get last log.
  125. *
  126. * @param string $path
  127. * @param int $count
  128. * @access public
  129. * @return array
  130. */
  131. public function getLastLog($path, $count = 10)
  132. {
  133. return $this->engine->getLastLog($path, $count);
  134. }
  135. /**
  136. * Diff file.
  137. *
  138. * @param string $path
  139. * @param string $fromRevision
  140. * @param string $toRevision
  141. * @param string $parse
  142. * @access public
  143. * @return array
  144. */
  145. public function diff($path, $fromRevision = 0, $toRevision = 'HEAD', $parse = 'yes', $extra = '')
  146. {
  147. if(!scm::checkRevision($fromRevision) and $extra != 'isBranchOrTag') return array();
  148. if(!scm::checkRevision($toRevision) and $extra != 'isBranchOrTag') return array();
  149. if(!$extra) $diffs = $this->engine->diff($path, $fromRevision, $toRevision);
  150. if($extra)
  151. {
  152. if(get_class($this->engine) == 'gitlab') $diffs = $this->engine->diff($path, $fromRevision, $toRevision, '', $extra);
  153. if(get_class($this->engine) != 'gitlab') $diffs = $this->engine->diff($path, $fromRevision, $toRevision, $extra);
  154. }
  155. if($parse != 'yes') return implode("\n", $diffs);
  156. return $this->engine->parseDiff($diffs);
  157. }
  158. /**
  159. * Cat file.
  160. *
  161. * @param string $entry
  162. * @param string $revision
  163. * @access public
  164. * @return string
  165. */
  166. public function cat($entry, $revision = 'HEAD')
  167. {
  168. if(!scm::checkRevision($revision)) return false;
  169. return $this->engine->cat($entry, $revision);
  170. }
  171. /**
  172. * Get info.
  173. *
  174. * @param string $entry
  175. * @param string $revision
  176. * @access public
  177. * @return object
  178. */
  179. public function info($entry, $revision = 'HEAD')
  180. {
  181. if(!scm::checkRevision($revision)) return false;
  182. return $this->engine->info($entry, $revision);
  183. }
  184. /**
  185. * Exec scm cmd.
  186. *
  187. * @param string $cmd
  188. * @access public
  189. * @return array
  190. */
  191. public function exec($cmd)
  192. {
  193. return $this->engine->exec($cmd);
  194. }
  195. /**
  196. * Get commit count
  197. *
  198. * @param int $commits
  199. * @param string $lastVersion
  200. * @access public
  201. * @return int
  202. */
  203. public function getCommitCount($commits = 0, $lastVersion = 0)
  204. {
  205. if(!scm::checkRevision($lastVersion)) return false;
  206. return $this->engine->getCommitCount($commits, $lastVersion);
  207. }
  208. /**
  209. * Get latest revision.
  210. *
  211. * @access public
  212. * @return string
  213. */
  214. public function getLatestRevision()
  215. {
  216. return $this->engine->getLatestRevision();
  217. }
  218. /**
  219. * Get first revision.
  220. *
  221. * @access public
  222. * @return string
  223. */
  224. public function getFirstRevision()
  225. {
  226. return $this->engine->getFirstRevision();
  227. }
  228. /**
  229. * Get commits.
  230. *
  231. * @param string $version
  232. * @param int $count
  233. * @param string $branch
  234. * @access public
  235. * @return array
  236. */
  237. public function getCommits($version = '', $count = 0, $branch = '')
  238. {
  239. if(!scm::checkRevision($version)) return array();
  240. return $this->engine->getCommits($version, $count, $branch);
  241. }
  242. /**
  243. * Get commits by MR branches.
  244. *
  245. * @param string $sourceBranch
  246. * @param string $targetBranch
  247. * @access public
  248. * @return array
  249. */
  250. public function getMRCommits($sourceBranch, $targetBranch)
  251. {
  252. return $this->engine->getMRCommits($sourceBranch, $targetBranch);
  253. }
  254. /**
  255. * Get clone url.
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. public function getCloneUrl()
  261. {
  262. return $this->engine->getCloneUrl();
  263. }
  264. /**
  265. * Check revision
  266. *
  267. * @param int|string $revision
  268. * @static
  269. * @access public
  270. * @return bool
  271. */
  272. public static function checkRevision($revision)
  273. {
  274. if(preg_match('/[^a-z0-9\-_\.\^\w][\x{4e00}-\x{9fa5}]/ui', $revision)) return false;
  275. return true;
  276. }
  277. /**
  278. * Get download url.
  279. *
  280. * @param string $branch
  281. * @param string $savePath
  282. * @param string $ext
  283. * @access public
  284. * @return string
  285. */
  286. public function getDownloadUrl($branch = '', $savePath = '', $ext = 'zip')
  287. {
  288. return $this->engine->getDownloadUrl($branch, $savePath, $ext);
  289. }
  290. /**
  291. * Get all files.
  292. *
  293. * @param string $path
  294. * @param string $revision
  295. * @access public
  296. * @return string
  297. */
  298. public function getAllFiles($path = '', $revision = 'HEAD')
  299. {
  300. return $this->engine->getAllFiles($path, $revision);
  301. }
  302. /**
  303. * Get files by commit.
  304. *
  305. * @param string $commit
  306. * @access public
  307. * @return array
  308. */
  309. public function getFilesByCommit($revision)
  310. {
  311. return $this->engine->getFilesByCommit($revision);
  312. }
  313. /**
  314. * Create mr by api.
  315. *
  316. * @param object $MR
  317. * @param string $openID
  318. * @param string $assignee
  319. * @access public
  320. * @return null|object
  321. */
  322. public function createMR($MR, $openID = '', $assignee = '')
  323. {
  324. return $this->engine->createMR($MR, $openID, $assignee);
  325. }
  326. /**
  327. * Get a mr by api.
  328. *
  329. * @param int $MRID
  330. * @access public
  331. * @return array
  332. */
  333. public function getSingleMR($MRID)
  334. {
  335. return $this->engine->getSingleMR($MRID);
  336. }
  337. /**
  338. * Get pipeline list by api.
  339. *
  340. * @access public
  341. * @return array
  342. */
  343. public function pipelines()
  344. {
  345. return $this->engine->pipelines();
  346. }
  347. /**
  348. * 格式化时间。
  349. * Format date.
  350. *
  351. * @param int|string $date
  352. * @param bool $hasTime
  353. * @param bool $isEnd
  354. * @access private
  355. * @return string
  356. */
  357. private function formatDate($date, $hasTime = false, $isEnd = false)
  358. {
  359. $format = $hasTime ? 'Y-m-d H:i:s' : 'Y-m-d';
  360. if(is_numeric($date))
  361. {
  362. if($date > 100000000) return date($format, $date);
  363. $date = $isEnd ? "{$date}-12-31" : "{$date}-01-01";
  364. if($isEnd) $date = date('Y-m-d', strtotime($date) + 86400);
  365. }
  366. elseif($isEnd)
  367. {
  368. $date = date('Y-m-d', strtotime($date) + 86400);
  369. }
  370. return date($format, strtotime($date));
  371. }
  372. /**
  373. * 根据开始时间和结束时间获取提交时间和提交人。
  374. * Get commit count by date.
  375. *
  376. * @param int $startDate
  377. * @param int $endDate
  378. * @access public
  379. * @return array
  380. */
  381. public function getCommitByDate($startDate, $endDate)
  382. {
  383. $startDate = trim($startDate, '-');
  384. $endDate = trim($endDate, '-');
  385. return $this->engine->getCommitByDate($this->formatDate($startDate), $this->formatDate($endDate, false, true));
  386. }
  387. public function __call($funcName, $arguments)
  388. {
  389. if(method_exists($this->engine, $funcName)) return call_user_func_array(array($this->engine, $funcName), $arguments);
  390. }
  391. }
  392. /**
  393. * Escape command.
  394. *
  395. * @param string $cmd
  396. * @access public
  397. * @return string
  398. */
  399. function escapeCmd($cmd)
  400. {
  401. $codes = array('#', '&', ';', '`', '|', '*', '?', '~', '<', '>', '^', '[', ']', '{', '}', '$', ',', '\x0A', '\xFF');
  402. if(DIRECTORY_SEPARATOR == '/') $cmd = str_replace('\\', '\\\\', $cmd);
  403. foreach($codes as $code) $cmd = str_replace($code, "\\{$code}", $cmd);
  404. return $cmd;
  405. }
  406. /**
  407. * Execute command.
  408. *
  409. * @param string $cmd
  410. * @param string $return
  411. * @param int $result
  412. * @param string $type
  413. * @access public
  414. * @return array|string
  415. */
  416. function execCmd($cmd, $return = 'string', &$result = 0, $type = 'utf-8')
  417. {
  418. if(file_exists(dirname(__FILE__) . '/config.php')) include dirname(__FILE__) . '/config.php';
  419. if($type != 'utf-8') $cmd = iconv('utf-8', $type . '//TRANSLIT', $cmd);
  420. $debug = (isset($config->debug) and $config->debug);
  421. if($debug and strpos($cmd, '2>&1') === false) $cmd = $cmd . ' 2>&1';
  422. ob_start();
  423. passthru($cmd, $result);
  424. $output = ob_get_clean();
  425. if($debug and $result)
  426. {
  427. a('The command is ' . $cmd);
  428. a('The result is ' . $result);
  429. a($output);
  430. }
  431. /* When output is empty and with chinese then try execute again in windows. */
  432. if(strtolower(substr(PHP_OS, 0, 3)) == 'win' and empty($output) and $type == 'utf-8' and preg_match("/[\x7f-\xff]/", $cmd)) $output = execCmd($cmd, 'string', $result, 'gbk');
  433. if($return == 'array') return explode("\n", trim($output));
  434. return $output;
  435. }