git-host-info.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict'
  2. const maybeJoin = (...args) => args.every(arg => arg) ? args.join('') : ''
  3. const maybeEncode = (arg) => arg ? encodeURIComponent(arg) : ''
  4. const defaults = {
  5. sshtemplate: ({ domain, user, project, committish }) => `git@${domain}:${user}/${project}.git${maybeJoin('#', committish)}`,
  6. sshurltemplate: ({ domain, user, project, committish }) => `git+ssh://git@${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  7. browsetemplate: ({ domain, user, project, committish, treepath }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}`,
  8. browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'master')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`,
  9. docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`,
  10. httpstemplate: ({ auth, domain, user, project, committish }) => `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  11. filetemplate: ({ domain, user, project, committish, path }) => `https://${domain}/${user}/${project}/raw/${maybeEncode(committish) || 'master'}/${path}`,
  12. shortcuttemplate: ({ type, user, project, committish }) => `${type}:${user}/${project}${maybeJoin('#', committish)}`,
  13. pathtemplate: ({ user, project, committish }) => `${user}/${project}${maybeJoin('#', committish)}`,
  14. bugstemplate: ({ domain, user, project }) => `https://${domain}/${user}/${project}/issues`,
  15. hashformat: formatHashFragment
  16. }
  17. const gitHosts = {}
  18. gitHosts.github = Object.assign({}, defaults, {
  19. // First two are insecure and generally shouldn't be used any more, but
  20. // they are still supported.
  21. protocols: ['git:', 'http:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'],
  22. domain: 'github.com',
  23. treepath: 'tree',
  24. filetemplate: ({ auth, user, project, committish, path }) => `https://${maybeJoin(auth, '@')}raw.githubusercontent.com/${user}/${project}/${maybeEncode(committish) || 'master'}/${path}`,
  25. gittemplate: ({ auth, domain, user, project, committish }) => `git://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  26. tarballtemplate: ({ domain, user, project, committish }) => `https://codeload.${domain}/${user}/${project}/tar.gz/${maybeEncode(committish) || 'master'}`,
  27. extract: (url) => {
  28. let [, user, project, type, committish] = url.pathname.split('/', 5)
  29. if (type && type !== 'tree') {
  30. return
  31. }
  32. if (!type) {
  33. committish = url.hash.slice(1)
  34. }
  35. if (project && project.endsWith('.git')) {
  36. project = project.slice(0, -4)
  37. }
  38. if (!user || !project) {
  39. return
  40. }
  41. return { user, project, committish }
  42. }
  43. })
  44. gitHosts.bitbucket = Object.assign({}, defaults, {
  45. protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'],
  46. domain: 'bitbucket.org',
  47. treepath: 'src',
  48. tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/get/${maybeEncode(committish) || 'master'}.tar.gz`,
  49. extract: (url) => {
  50. let [, user, project, aux] = url.pathname.split('/', 4)
  51. if (['get'].includes(aux)) {
  52. return
  53. }
  54. if (project && project.endsWith('.git')) {
  55. project = project.slice(0, -4)
  56. }
  57. if (!user || !project) {
  58. return
  59. }
  60. return { user, project, committish: url.hash.slice(1) }
  61. }
  62. })
  63. gitHosts.gitlab = Object.assign({}, defaults, {
  64. protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'],
  65. domain: 'gitlab.com',
  66. treepath: 'tree',
  67. httpstemplate: ({ auth, domain, user, project, committish }) => `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
  68. tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/repository/archive.tar.gz?ref=${maybeEncode(committish) || 'master'}`,
  69. extract: (url) => {
  70. const path = url.pathname.slice(1)
  71. if (path.includes('/-/') || path.includes('/archive.tar.gz')) {
  72. return
  73. }
  74. const segments = path.split('/')
  75. let project = segments.pop()
  76. if (project.endsWith('.git')) {
  77. project = project.slice(0, -4)
  78. }
  79. const user = segments.join('/')
  80. if (!user || !project) {
  81. return
  82. }
  83. return { user, project, committish: url.hash.slice(1) }
  84. }
  85. })
  86. gitHosts.gist = Object.assign({}, defaults, {
  87. protocols: ['git:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'],
  88. domain: 'gist.github.com',
  89. sshtemplate: ({ domain, project, committish }) => `git@${domain}:${project}.git${maybeJoin('#', committish)}`,
  90. sshurltemplate: ({ domain, project, committish }) => `git+ssh://git@${domain}/${project}.git${maybeJoin('#', committish)}`,
  91. browsetemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`,
  92. browsefiletemplate: ({ domain, project, committish, path, hashformat }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}${maybeJoin('#', hashformat(path))}`,
  93. docstemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`,
  94. httpstemplate: ({ domain, project, committish }) => `git+https://${domain}/${project}.git${maybeJoin('#', committish)}`,
  95. filetemplate: ({ user, project, committish, path }) => `https://gist.githubusercontent.com/${user}/${project}/raw${maybeJoin('/', maybeEncode(committish))}/${path}`,
  96. shortcuttemplate: ({ type, project, committish }) => `${type}:${project}${maybeJoin('#', committish)}`,
  97. pathtemplate: ({ project, committish }) => `${project}${maybeJoin('#', committish)}`,
  98. bugstemplate: ({ domain, project }) => `https://${domain}/${project}`,
  99. gittemplate: ({ domain, project, committish }) => `git://${domain}/${project}.git${maybeJoin('#', committish)}`,
  100. tarballtemplate: ({ project, committish }) => `https://codeload.github.com/gist/${project}/tar.gz/${maybeEncode(committish) || 'master'}`,
  101. extract: (url) => {
  102. let [, user, project, aux] = url.pathname.split('/', 4)
  103. if (aux === 'raw') {
  104. return
  105. }
  106. if (!project) {
  107. if (!user) {
  108. return
  109. }
  110. project = user
  111. user = null
  112. }
  113. if (project.endsWith('.git')) {
  114. project = project.slice(0, -4)
  115. }
  116. return { user, project, committish: url.hash.slice(1) }
  117. },
  118. hashformat: function (fragment) {
  119. return fragment && 'file-' + formatHashFragment(fragment)
  120. }
  121. })
  122. const names = Object.keys(gitHosts)
  123. gitHosts.byShortcut = {}
  124. gitHosts.byDomain = {}
  125. for (const name of names) {
  126. gitHosts.byShortcut[`${name}:`] = name
  127. gitHosts.byDomain[gitHosts[name].domain] = name
  128. }
  129. function formatHashFragment (fragment) {
  130. return fragment.toLowerCase().replace(/^\W+|\/|\W+$/g, '').replace(/\W+/g, '-')
  131. }
  132. module.exports = gitHosts