Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
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.

Repository.php 6.6KB

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