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.

index.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict'
  2. var url = require('url')
  3. var gitHosts = require('./git-host-info.js')
  4. var GitHost = module.exports = require('./git-host.js')
  5. var protocolToRepresentationMap = {
  6. 'git+ssh:': 'sshurl',
  7. 'git+https:': 'https',
  8. 'ssh:': 'sshurl',
  9. 'git:': 'git'
  10. }
  11. function protocolToRepresentation (protocol) {
  12. return protocolToRepresentationMap[protocol] || protocol.slice(0, -1)
  13. }
  14. var authProtocols = {
  15. 'git:': true,
  16. 'https:': true,
  17. 'git+https:': true,
  18. 'http:': true,
  19. 'git+http:': true
  20. }
  21. var cache = {}
  22. module.exports.fromUrl = function (giturl, opts) {
  23. if (typeof giturl !== 'string') return
  24. var key = giturl + JSON.stringify(opts || {})
  25. if (!(key in cache)) {
  26. cache[key] = fromUrl(giturl, opts)
  27. }
  28. return cache[key]
  29. }
  30. function fromUrl (giturl, opts) {
  31. if (giturl == null || giturl === '') return
  32. var url = fixupUnqualifiedGist(
  33. isGitHubShorthand(giturl) ? 'github:' + giturl : giturl
  34. )
  35. var parsed = parseGitUrl(url)
  36. var shortcutMatch = url.match(new RegExp('^([^:]+):(?:(?:[^@:]+(?:[^@]+)?@)?([^/]*))[/](.+?)(?:[.]git)?($|#)'))
  37. var matches = Object.keys(gitHosts).map(function (gitHostName) {
  38. try {
  39. var gitHostInfo = gitHosts[gitHostName]
  40. var auth = null
  41. if (parsed.auth && authProtocols[parsed.protocol]) {
  42. auth = parsed.auth
  43. }
  44. var committish = parsed.hash ? decodeURIComponent(parsed.hash.substr(1)) : null
  45. var user = null
  46. var project = null
  47. var defaultRepresentation = null
  48. if (shortcutMatch && shortcutMatch[1] === gitHostName) {
  49. user = shortcutMatch[2] && decodeURIComponent(shortcutMatch[2])
  50. project = decodeURIComponent(shortcutMatch[3])
  51. defaultRepresentation = 'shortcut'
  52. } else {
  53. if (parsed.host && parsed.host !== gitHostInfo.domain && parsed.host.replace(/^www[.]/, '') !== gitHostInfo.domain) return
  54. if (!gitHostInfo.protocols_re.test(parsed.protocol)) return
  55. if (!parsed.path) return
  56. var pathmatch = gitHostInfo.pathmatch
  57. var matched = parsed.path.match(pathmatch)
  58. if (!matched) return
  59. /* istanbul ignore else */
  60. if (matched[1] !== null && matched[1] !== undefined) {
  61. user = decodeURIComponent(matched[1].replace(/^:/, ''))
  62. }
  63. project = decodeURIComponent(matched[2])
  64. defaultRepresentation = protocolToRepresentation(parsed.protocol)
  65. }
  66. return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
  67. } catch (ex) {
  68. /* istanbul ignore else */
  69. if (ex instanceof URIError) {
  70. } else throw ex
  71. }
  72. }).filter(function (gitHostInfo) { return gitHostInfo })
  73. if (matches.length !== 1) return
  74. return matches[0]
  75. }
  76. function isGitHubShorthand (arg) {
  77. // Note: This does not fully test the git ref format.
  78. // See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  79. //
  80. // The only way to do this properly would be to shell out to
  81. // git-check-ref-format, and as this is a fast sync function,
  82. // we don't want to do that. Just let git fail if it turns
  83. // out that the commit-ish is invalid.
  84. // GH usernames cannot start with . or -
  85. return /^[^:@%/\s.-][^:@%/\s]*[/][^:@\s/%]+(?:#.*)?$/.test(arg)
  86. }
  87. function fixupUnqualifiedGist (giturl) {
  88. // necessary for round-tripping gists
  89. var parsed = url.parse(giturl)
  90. if (parsed.protocol === 'gist:' && parsed.host && !parsed.path) {
  91. return parsed.protocol + '/' + parsed.host
  92. } else {
  93. return giturl
  94. }
  95. }
  96. function parseGitUrl (giturl) {
  97. var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
  98. if (!matched) {
  99. var legacy = url.parse(giturl)
  100. // If we don't have url.URL, then sorry, this is just not fixable.
  101. // This affects Node <= 6.12.
  102. if (legacy.auth && typeof url.URL === 'function') {
  103. // git urls can be in the form of scp-style/ssh-connect strings, like
  104. // git+ssh://user@host.com:some/path, which the legacy url parser
  105. // supports, but WhatWG url.URL class does not. However, the legacy
  106. // parser de-urlencodes the username and password, so something like
  107. // https://user%3An%40me:p%40ss%3Aword@x.com/ becomes
  108. // https://user:n@me:p@ss:word@x.com/ which is all kinds of wrong.
  109. // Pull off just the auth and host, so we dont' get the confusing
  110. // scp-style URL, then pass that to the WhatWG parser to get the
  111. // auth properly escaped.
  112. var authmatch = giturl.match(/[^@]+@[^:/]+/)
  113. /* istanbul ignore else - this should be impossible */
  114. if (authmatch) {
  115. var whatwg = new url.URL(authmatch[0])
  116. legacy.auth = whatwg.username || ''
  117. if (whatwg.password) legacy.auth += ':' + whatwg.password
  118. }
  119. }
  120. return legacy
  121. }
  122. return {
  123. protocol: 'git+ssh:',
  124. slashes: true,
  125. auth: matched[1],
  126. host: matched[2],
  127. port: null,
  128. hostname: matched[2],
  129. hash: matched[4],
  130. search: null,
  131. query: null,
  132. pathname: '/' + matched[3],
  133. path: '/' + matched[3],
  134. href: 'git+ssh://' + matched[1] + '@' + matched[2] +
  135. '/' + matched[3] + (matched[4] || '')
  136. }
  137. }