Brak opisu

index.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if (protocol.substr(-1) === ':') protocol = protocol.slice(0, -1)
  13. return protocolToRepresentationMap[protocol] || protocol
  14. }
  15. var authProtocols = {
  16. 'git:': true,
  17. 'https:': true,
  18. 'git+https:': true,
  19. 'http:': true,
  20. 'git+http:': true
  21. }
  22. var cache = {}
  23. module.exports.fromUrl = function (giturl, opts) {
  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 = decodeURIComponent(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 !== 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. if (matched[1] != null) user = decodeURIComponent(matched[1].replace(/^:/, ''))
  60. if (matched[2] != null) project = decodeURIComponent(matched[2])
  61. defaultRepresentation = protocolToRepresentation(parsed.protocol)
  62. }
  63. return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
  64. } catch (ex) {
  65. if (!(ex instanceof URIError)) throw ex
  66. }
  67. }).filter(function (gitHostInfo) { return gitHostInfo })
  68. if (matches.length !== 1) return
  69. return matches[0]
  70. }
  71. function isGitHubShorthand (arg) {
  72. // Note: This does not fully test the git ref format.
  73. // See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  74. //
  75. // The only way to do this properly would be to shell out to
  76. // git-check-ref-format, and as this is a fast sync function,
  77. // we don't want to do that. Just let git fail if it turns
  78. // out that the commit-ish is invalid.
  79. // GH usernames cannot start with . or -
  80. return /^[^:@%/\s.-][^:@%/\s]*[/][^:@\s/%]+(?:#.*)?$/.test(arg)
  81. }
  82. function fixupUnqualifiedGist (giturl) {
  83. // necessary for round-tripping gists
  84. var parsed = url.parse(giturl)
  85. if (parsed.protocol === 'gist:' && parsed.host && !parsed.path) {
  86. return parsed.protocol + '/' + parsed.host
  87. } else {
  88. return giturl
  89. }
  90. }
  91. function parseGitUrl (giturl) {
  92. if (typeof giturl !== 'string') giturl = '' + giturl
  93. var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
  94. if (!matched) return url.parse(giturl)
  95. return {
  96. protocol: 'git+ssh:',
  97. slashes: true,
  98. auth: matched[1],
  99. host: matched[2],
  100. port: null,
  101. hostname: matched[2],
  102. hash: matched[4],
  103. search: null,
  104. query: null,
  105. pathname: '/' + matched[3],
  106. path: '/' + matched[3],
  107. href: 'git+ssh://' + matched[1] + '@' + matched[2] +
  108. '/' + matched[3] + (matched[4] || '')
  109. }
  110. }