index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. module.exports.fromUrl = function (giturl, opts) {
  23. if (giturl == null || giturl === '') return
  24. var url = fixupUnqualifiedGist(
  25. isGitHubShorthand(giturl) ? 'github:' + giturl : giturl
  26. )
  27. var parsed = parseGitUrl(url)
  28. var shortcutMatch = url.match(new RegExp('^([^:]+):(?:(?:[^@:]+(?:[^@]+)?@)?([^/]*))[/](.+?)(?:[.]git)?($|#)'))
  29. var matches = Object.keys(gitHosts).map(function (gitHostName) {
  30. try {
  31. var gitHostInfo = gitHosts[gitHostName]
  32. var auth = null
  33. if (parsed.auth && authProtocols[parsed.protocol]) {
  34. auth = decodeURIComponent(parsed.auth)
  35. }
  36. var committish = parsed.hash ? decodeURIComponent(parsed.hash.substr(1)) : null
  37. var user = null
  38. var project = null
  39. var defaultRepresentation = null
  40. if (shortcutMatch && shortcutMatch[1] === gitHostName) {
  41. user = shortcutMatch[2] && decodeURIComponent(shortcutMatch[2])
  42. project = decodeURIComponent(shortcutMatch[3])
  43. defaultRepresentation = 'shortcut'
  44. } else {
  45. if (parsed.host !== gitHostInfo.domain) return
  46. if (!gitHostInfo.protocols_re.test(parsed.protocol)) return
  47. if (!parsed.path) return
  48. var pathmatch = gitHostInfo.pathmatch
  49. var matched = parsed.path.match(pathmatch)
  50. if (!matched) return
  51. if (matched[1] != null) user = decodeURIComponent(matched[1].replace(/^:/, ''))
  52. if (matched[2] != null) project = decodeURIComponent(matched[2])
  53. defaultRepresentation = protocolToRepresentation(parsed.protocol)
  54. }
  55. return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts)
  56. } catch (ex) {
  57. if (!(ex instanceof URIError)) throw ex
  58. }
  59. }).filter(function (gitHostInfo) { return gitHostInfo })
  60. if (matches.length !== 1) return
  61. return matches[0]
  62. }
  63. function isGitHubShorthand (arg) {
  64. // Note: This does not fully test the git ref format.
  65. // See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  66. //
  67. // The only way to do this properly would be to shell out to
  68. // git-check-ref-format, and as this is a fast sync function,
  69. // we don't want to do that. Just let git fail if it turns
  70. // out that the commit-ish is invalid.
  71. // GH usernames cannot start with . or -
  72. return /^[^:@%/\s.-][^:@%/\s]*[/][^:@\s/%]+(?:#.*)?$/.test(arg)
  73. }
  74. function fixupUnqualifiedGist (giturl) {
  75. // necessary for round-tripping gists
  76. var parsed = url.parse(giturl)
  77. if (parsed.protocol === 'gist:' && parsed.host && !parsed.path) {
  78. return parsed.protocol + '/' + parsed.host
  79. } else {
  80. return giturl
  81. }
  82. }
  83. function parseGitUrl (giturl) {
  84. if (typeof giturl !== 'string') giturl = '' + giturl
  85. var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
  86. if (!matched) return url.parse(giturl)
  87. return {
  88. protocol: 'git+ssh:',
  89. slashes: true,
  90. auth: matched[1],
  91. host: matched[2],
  92. port: null,
  93. hostname: matched[2],
  94. hash: matched[4],
  95. search: null,
  96. query: null,
  97. pathname: '/' + matched[3],
  98. path: '/' + matched[3],
  99. href: 'git+ssh://' + matched[1] + '@' + matched[2] +
  100. '/' + matched[3] + (matched[4] || '')
  101. }
  102. }