Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Repository.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace GitList\Util;
  3. use Silex\Application;
  4. class Repository
  5. {
  6. protected $app;
  7. protected $defaultFileTypes = array(
  8. 'php' => 'php',
  9. 'c' => 'clike',
  10. 'h' => 'clike',
  11. 'cpp' => 'clike',
  12. 'm' => 'clike',
  13. 'mm' => 'clike',
  14. 'ino' => 'clike',
  15. 'cs' => 'text/x-csharp',
  16. 'csproj' => 'text/x-csharp',
  17. 'java' => 'text/x-java',
  18. 'clj' => 'clojure',
  19. 'coffee' => 'coffeescript',
  20. 'css' => 'css',
  21. 'diff' => 'diff',
  22. 'ecl' => 'ecl',
  23. 'el' => 'erlang',
  24. 'go' => 'go',
  25. 'groovy' => 'groovy',
  26. 'hs' => 'haskell',
  27. 'lhs' => 'haskell',
  28. 'jsp' => 'htmlembedded',
  29. 'asp' => 'htmlembedded',
  30. 'aspx' => 'htmlembedded',
  31. 'html' => 'htmlmixed',
  32. 'tpl' => 'htmlmixed',
  33. 'js' => 'javascript',
  34. 'json' => 'javascript',
  35. 'less' => 'less',
  36. 'lua' => 'lua',
  37. 'md' => 'markdown',
  38. 'markdown' => 'markdown',
  39. 'sql' => 'mysql',
  40. 'pl' => 'perl',
  41. 'pm' => 'perl',
  42. 'pas' => 'pascal',
  43. 'ini' => 'properties',
  44. 'cfg' => 'properties',
  45. 'nt' => 'ntriples',
  46. 'py' => 'python',
  47. 'rb' => 'ruby',
  48. 'rst' => 'rst',
  49. 'r' => 'r',
  50. 'sh' => 'shell',
  51. 'ss' => 'scheme',
  52. 'scala' => 'text/x-scala',
  53. 'scm' => 'scheme',
  54. 'sls' => 'scheme',
  55. 'sps' => 'scheme',
  56. 'rs' => 'rust',
  57. 'st' => 'smalltalk',
  58. 'tex' => 'stex',
  59. 'vbs' => 'vbscript',
  60. 'v' => 'verilog',
  61. 'xml' => 'xml',
  62. 'xsd' => 'xml',
  63. 'xsl' => 'xml',
  64. 'xul' => 'xml',
  65. 'xlf' => 'xml',
  66. 'xliff' => 'xml',
  67. 'xaml' => 'xml',
  68. 'wxs' => 'xml',
  69. 'wxl' => 'xml',
  70. 'wxi' => 'xml',
  71. 'wsdl' => 'xml',
  72. 'svg' => 'xml',
  73. 'rss' => 'xml',
  74. 'rdf' => 'xml',
  75. 'plist' => 'xml',
  76. 'mxml' => 'xml',
  77. 'kml' => 'xml',
  78. 'glade' => 'xml',
  79. 'xq' => 'xquery',
  80. 'xqm' => 'xquery',
  81. 'xquery' => 'xquery',
  82. 'xqy' => 'xquery',
  83. 'yml' => 'yaml',
  84. 'yaml' => 'yaml',
  85. 'png' => 'image',
  86. 'jpg' => 'image',
  87. 'gif' => 'image',
  88. 'jpeg' => 'image',
  89. 'bmp' => 'image',
  90. 'csproj' => 'xml',
  91. );
  92. protected static $binaryTypes = array(
  93. 'exe', 'com', 'so', 'la', 'o', 'dll', 'pyc',
  94. 'jpg', 'jpeg', 'bmp', 'gif', 'png', 'xmp', 'pcx', 'svgz', 'ttf', 'tiff', 'oet',
  95. 'gz', 'tar', 'rar', 'zip', '7z', 'jar', 'class',
  96. 'odt', 'ods', 'pdf', 'doc', 'docx', 'dot', 'xls', 'xlsx',
  97. );
  98. public function __construct(Application $app)
  99. {
  100. $this->app = $app;
  101. }
  102. /**
  103. * Returns the file type based on filename by treating the extension
  104. *
  105. * The file type is used by CodeMirror, a Javascript-based IDE implemented in
  106. * GitList, to properly highlight the blob syntax (if it's a source-code)
  107. *
  108. * @param string $file File name
  109. * @return mixed File type
  110. */
  111. public function getFileType($file)
  112. {
  113. if (($pos = strrpos($file, '.')) !== false) {
  114. $fileType = substr($file, $pos + 1);
  115. } else {
  116. return 'text';
  117. }
  118. if (isset($this->defaultFileTypes[$fileType])) {
  119. return $this->defaultFileTypes[$fileType];
  120. }
  121. if (!empty($this->app['filetypes'])) {
  122. if (isset($this->app['filetypes'][$fileType])) {
  123. return $this->app['filetypes'][$fileType];
  124. }
  125. }
  126. return 'text';
  127. }
  128. /**
  129. * Returns whether the file is binary.
  130. *
  131. * @param string $file
  132. *
  133. * @return boolean
  134. */
  135. public function isBinary($file)
  136. {
  137. if (($pos = strrpos($file, '.')) !== false) {
  138. $fileType = substr($file, $pos + 1);
  139. } else {
  140. return false;
  141. }
  142. if (in_array($fileType, self::$binaryTypes)) {
  143. return true;
  144. }
  145. if (!empty($this->app['binary_filetypes']) && array_key_exists($fileType, $this->app['binary_filetypes'])) {
  146. return $this->app['binary_filetypes'][$fileType];
  147. }
  148. return false;
  149. }
  150. public function getReadme($repository, $branch = null)
  151. {
  152. $files = $repository->getTree($branch)->output();
  153. if ($branch === null) {
  154. $branch = $repository->getHead();
  155. }
  156. foreach ($files as $file) {
  157. if (preg_match('/^readme*/i', $file['name'])) {
  158. return array(
  159. 'filename' => $file['name'],
  160. 'content' => $repository->getBlob("$branch:\"{$file['name']}\"")->output()
  161. );
  162. }
  163. }
  164. return array();
  165. }
  166. /**
  167. * Returns an Array where the first value is the tree-ish and the second is the path
  168. *
  169. * @param \GitList\Git\Repository $repository
  170. * @param string $branch
  171. * @param string $tree
  172. * @return array
  173. */
  174. public function extractRef($repository, $branch = '', $tree = '')
  175. {
  176. $branch = trim($branch, '/');
  177. $tree = trim($tree, '/');
  178. $input = $branch . '/' . $tree;
  179. // If the ref appears to be a SHA, just split the string
  180. if (preg_match("/^([[:alnum:]]{40})(.+)/", $input, $matches)) {
  181. $branch = $matches[1];
  182. } else {
  183. // Otherwise, attempt to detect the ref using a list of the project's branches and tags
  184. $validRefs = array_merge((array) $repository->getBranches(), (array) $repository->getTags());
  185. foreach ($validRefs as $key => $ref) {
  186. if (!preg_match(sprintf("#^%s/#", preg_quote($ref, '#')), $input)) {
  187. unset($validRefs[$key]);
  188. }
  189. }
  190. // No exact ref match, so just try our best
  191. if (count($validRefs) > 1) {
  192. preg_match('/([^\/]+)(.*)/', $input, $matches);
  193. $branch = preg_replace('/^\/|\/$/', '', $matches[1]);
  194. } else {
  195. // Extract branch name
  196. $branch = array_shift($validRefs);
  197. }
  198. }
  199. return array($branch, $tree);
  200. }
  201. }