• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var gitHosts = require('./git-host-info.js')
3/* eslint-disable node/no-deprecated-api */
4
5// copy-pasta util._extend from node's source, to avoid pulling
6// the whole util module into peoples' webpack bundles.
7/* istanbul ignore next */
8var extend = Object.assign || function _extend (target, source) {
9  // Don't do anything if source isn't an object
10  if (source === null || typeof source !== 'object') return target
11
12  var keys = Object.keys(source)
13  var i = keys.length
14  while (i--) {
15    target[keys[i]] = source[keys[i]]
16  }
17  return target
18}
19
20module.exports = GitHost
21function GitHost (type, user, auth, project, committish, defaultRepresentation, opts) {
22  var gitHostInfo = this
23  gitHostInfo.type = type
24  Object.keys(gitHosts[type]).forEach(function (key) {
25    gitHostInfo[key] = gitHosts[type][key]
26  })
27  gitHostInfo.user = user
28  gitHostInfo.auth = auth
29  gitHostInfo.project = project
30  gitHostInfo.committish = committish
31  gitHostInfo.default = defaultRepresentation
32  gitHostInfo.opts = opts || {}
33}
34
35GitHost.prototype.hash = function () {
36  return this.committish ? '#' + this.committish : ''
37}
38
39GitHost.prototype._fill = function (template, opts) {
40  if (!template) return
41  var vars = extend({}, opts)
42  vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
43  opts = extend(extend({}, this.opts), opts)
44  var self = this
45  Object.keys(this).forEach(function (key) {
46    if (self[key] != null && vars[key] == null) vars[key] = self[key]
47  })
48  var rawAuth = vars.auth
49  var rawcommittish = vars.committish
50  var rawFragment = vars.fragment
51  var rawPath = vars.path
52  var rawProject = vars.project
53  Object.keys(vars).forEach(function (key) {
54    var value = vars[key]
55    if ((key === 'path' || key === 'project') && typeof value === 'string') {
56      vars[key] = value.split('/').map(function (pathComponent) {
57        return encodeURIComponent(pathComponent)
58      }).join('/')
59    } else {
60      vars[key] = encodeURIComponent(value)
61    }
62  })
63  vars['auth@'] = rawAuth ? rawAuth + '@' : ''
64  vars['#fragment'] = rawFragment ? '#' + this.hashformat(rawFragment) : ''
65  vars.fragment = vars.fragment ? vars.fragment : ''
66  vars['#path'] = rawPath ? '#' + this.hashformat(rawPath) : ''
67  vars['/path'] = vars.path ? '/' + vars.path : ''
68  vars.projectPath = rawProject.split('/').map(encodeURIComponent).join('/')
69  if (opts.noCommittish) {
70    vars['#committish'] = ''
71    vars['/tree/committish'] = ''
72    vars['/committish'] = ''
73    vars.committish = ''
74  } else {
75    vars['#committish'] = rawcommittish ? '#' + rawcommittish : ''
76    vars['/tree/committish'] = vars.committish
77      ? '/' + vars.treepath + '/' + vars.committish
78      : ''
79    vars['/committish'] = vars.committish ? '/' + vars.committish : ''
80    vars.committish = vars.committish || 'master'
81  }
82  var res = template
83  Object.keys(vars).forEach(function (key) {
84    res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
85  })
86  if (opts.noGitPlus) {
87    return res.replace(/^git[+]/, '')
88  } else {
89    return res
90  }
91}
92
93GitHost.prototype.ssh = function (opts) {
94  return this._fill(this.sshtemplate, opts)
95}
96
97GitHost.prototype.sshurl = function (opts) {
98  return this._fill(this.sshurltemplate, opts)
99}
100
101GitHost.prototype.browse = function (P, F, opts) {
102  if (typeof P === 'string') {
103    if (typeof F !== 'string') {
104      opts = F
105      F = null
106    }
107    return this._fill(this.browsefiletemplate, extend({
108      fragment: F,
109      path: P
110    }, opts))
111  } else {
112    return this._fill(this.browsetemplate, P)
113  }
114}
115
116GitHost.prototype.docs = function (opts) {
117  return this._fill(this.docstemplate, opts)
118}
119
120GitHost.prototype.bugs = function (opts) {
121  return this._fill(this.bugstemplate, opts)
122}
123
124GitHost.prototype.https = function (opts) {
125  return this._fill(this.httpstemplate, opts)
126}
127
128GitHost.prototype.git = function (opts) {
129  return this._fill(this.gittemplate, opts)
130}
131
132GitHost.prototype.shortcut = function (opts) {
133  return this._fill(this.shortcuttemplate, opts)
134}
135
136GitHost.prototype.path = function (opts) {
137  return this._fill(this.pathtemplate, opts)
138}
139
140GitHost.prototype.tarball = function (opts_) {
141  var opts = extend({}, opts_, { noCommittish: false })
142  return this._fill(this.tarballtemplate, opts)
143}
144
145GitHost.prototype.file = function (P, opts) {
146  return this._fill(this.filetemplate, extend({ path: P }, opts))
147}
148
149GitHost.prototype.getDefaultRepresentation = function () {
150  return this.default
151}
152
153GitHost.prototype.toString = function (opts) {
154  if (this.default && typeof this[this.default] === 'function') return this[this.default](opts)
155  return this.sshurl(opts)
156}
157