2 var gitHosts = require('./git-host-info.js')
3 /* eslint-disable node/no-deprecated-api */
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 */
8 var 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
12 var keys = Object.keys(source)
15 target[keys[i]] = source[keys[i]]
20 module.exports = GitHost
21 function 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]
27 gitHostInfo.user = user
28 gitHostInfo.auth = auth
29 gitHostInfo.project = project
30 gitHostInfo.committish = committish
31 gitHostInfo.default = defaultRepresentation
32 gitHostInfo.opts = opts || {}
35 GitHost.prototype.hash = function () {
36 return this.committish ? '#' + this.committish : ''
39 GitHost.prototype._fill = function (template, opts) {
41 var vars = extend({}, opts)
42 vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
43 opts = extend(extend({}, this.opts), opts)
45 Object.keys(this).forEach(function (key) {
46 if (self[key] != null && vars[key] == null) vars[key] = self[key]
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) {
55 if ((key === 'path' || key === 'project') && typeof value === 'string') {
56 vars[key] = value.split('/').map(function (pathComponent) {
57 return encodeURIComponent(pathComponent)
60 vars[key] = encodeURIComponent(value)
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'] = ''
75 vars['#committish'] = rawcommittish ? '#' + rawcommittish : ''
76 vars['/tree/committish'] = vars.committish
77 ? '/' + vars.treepath + '/' + vars.committish
79 vars['/committish'] = vars.committish ? '/' + vars.committish : ''
80 vars.committish = vars.committish || 'master'
83 Object.keys(vars).forEach(function (key) {
84 res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
87 return res.replace(/^git[+]/, '')
93 GitHost.prototype.ssh = function (opts) {
94 return this._fill(this.sshtemplate, opts)
97 GitHost.prototype.sshurl = function (opts) {
98 return this._fill(this.sshurltemplate, opts)
101 GitHost.prototype.browse = function (P, F, opts) {
102 if (typeof P === 'string') {
103 if (typeof F !== 'string') {
107 return this._fill(this.browsefiletemplate, extend({
112 return this._fill(this.browsetemplate, P)
116 GitHost.prototype.docs = function (opts) {
117 return this._fill(this.docstemplate, opts)
120 GitHost.prototype.bugs = function (opts) {
121 return this._fill(this.bugstemplate, opts)
124 GitHost.prototype.https = function (opts) {
125 return this._fill(this.httpstemplate, opts)
128 GitHost.prototype.git = function (opts) {
129 return this._fill(this.gittemplate, opts)
132 GitHost.prototype.shortcut = function (opts) {
133 return this._fill(this.shortcuttemplate, opts)
136 GitHost.prototype.path = function (opts) {
137 return this._fill(this.pathtemplate, opts)
140 GitHost.prototype.tarball = function (opts_) {
141 var opts = extend({}, opts_, { noCommittish: false })
142 return this._fill(this.tarballtemplate, opts)
145 GitHost.prototype.file = function (P, opts) {
146 return this._fill(this.filetemplate, extend({ path: P }, opts))
149 GitHost.prototype.getDefaultRepresentation = function () {
153 GitHost.prototype.toString = function (opts) {
154 if (this.default && typeof this[this.default] === 'function') return this[this.default](opts)
155 return this.sshurl(opts)