You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.2 KiB

  1. <?php
  2. class RepositoryGit extends RepositoryBase {
  3. function __construct ($id, $def) {
  4. parent::__construct($id, $def);
  5. if (array_key_exists('branch', $def)) {
  6. $this->branch = $def['branch'];
  7. }
  8. else {
  9. $this->branch = chop(shell_exec("cd " . escapeShellArg($this->path) . "; git rev-parse --abbrev-ref HEAD"));
  10. }
  11. $this->branchEsc = escapeShellArg($this->branch);
  12. }
  13. function setBranch ($branch) {
  14. $this->branch = $branch;
  15. $this->branchEsc = escapeShellArg($this->branch);
  16. exec("cd " . escapeShellArg($this->path) . "; git ls-tree {$this->branchEsc}", $output, $return);
  17. if ($return !== 0) {
  18. throw new Exception('no such branch');
  19. }
  20. }
  21. function timestamp () {
  22. if (!isset($this->_timestamp)) {
  23. $ts = shell_exec("cd " . escapeShellArg($this->path) . "; git log -1 --all --pretty=format:%ct");
  24. $this->_timestamp = $ts === null ? null : (int)$ts;
  25. }
  26. return $this->_timestamp;
  27. }
  28. function isEmpty () {
  29. if ($this->timestamp() === null) {
  30. return true;
  31. }
  32. if (!sizeof($this->scandir('.'))) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. function data ($options) {
  38. $data = parent::data($options);
  39. $lang = array_key_exists('lang', $options) ? $options['lang'] : 'en';
  40. if (true) {
  41. $data['lang'] = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:lang/en.json 2>/dev/null"), true);
  42. $lang = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:lang/" . escapeShellArg("{$options['lang']}.json") . " 2>/dev/null"), true);
  43. foreach ($lang as $k => $v) {
  44. if ($v !== null && $v !== '') {
  45. $data['lang'][$k] = $v;
  46. }
  47. }
  48. }
  49. $d = popen("cd " . escapeShellArg($this->path) . "; git ls-tree {$this->branchEsc}", "r");
  50. while ($r = fgets($d)) {
  51. if (preg_match("/^[0-9]{6} blob [0-9a-f]{40}\t(([0-9a-zA-Z_\-]+)\.json)$/", $r, $m)) {
  52. $f = $m[1];
  53. $id = $m[2];
  54. if ($f === 'package.json') {
  55. continue;
  56. }
  57. $d1 = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:" . escapeShellArg($f)), true);
  58. $d1['format'] = 'json';
  59. $d1['fileName'] = $f;
  60. if (!$this->isCategory($d1)) {
  61. continue;
  62. }
  63. $data['categories'][$id] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const'), array('filter'))));
  64. }
  65. if (preg_match("/^[0-9]{6} blob [0-9a-f]{40}\t(([0-9a-zA-Z_\-]+)\.yaml)$/", $r, $m)) {
  66. $f = $m[1];
  67. $id = $m[2];
  68. $d1 = yaml_parse(shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:" . escapeShellArg($f)));
  69. $d1['format'] = 'yaml';
  70. $d1['fileName'] = $f;
  71. if (!$this->isCategory($d1)) {
  72. continue;
  73. }
  74. $data['categories'][$id] = $d1;
  75. }
  76. if (preg_match("/^[0-9]{6} blob [0-9a-f]{40}\t((detailsBody|popupBody)\.html)$/", $r, $m)) {
  77. $data['templates'][$m[2]] = shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:" . escapeShellArg($m[1]));
  78. }
  79. }
  80. pclose($d);
  81. if (!array_key_exists('branch', $this->def)) {
  82. $d = popen("cd " . escapeShellArg($this->path) . "; git for-each-ref --sort=-committerdate refs/heads/", "r");
  83. $data['branch'] = $this->branch;
  84. $data['branches'] = array();
  85. while ($r = fgets($d)) {
  86. if (preg_match("/^([0-9a-f]{40}) commit\trefs\/heads\/(.*)$/", $r, $m)) {
  87. $data['branches'][$m[2]] = array(
  88. 'commit' => $m[1],
  89. );
  90. }
  91. }
  92. pclose($d);
  93. }
  94. return $data;
  95. }
  96. function scandir($path="") {
  97. if ($path !== '' && substr($path, -1) !== '/') {
  98. $path .= '/';
  99. }
  100. $d = popen("cd " . escapeShellArg($this->path) . "; git ls-tree {$this->branchEsc} " . escapeShellArg($path), "r");
  101. $ret = array();
  102. while ($r = fgets($d)) {
  103. $ret[] = chop(substr($r, 53));
  104. }
  105. pclose($d);
  106. $ret = array_filter(
  107. $ret,
  108. function ($file) {
  109. return preg_match("/\.(html|json|yaml)$/", $file);
  110. }
  111. );
  112. return $ret;
  113. }
  114. function file_get_contents ($file) {
  115. return shell_exec("cd " . escapeShellArg($this->path) . "; git show {$this->branchEsc}:" . escapeShellArg($file));
  116. }
  117. }