git-host.js
2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict'
var gitHosts = require('./git-host-info.js')
var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation) {
var gitHostInfo = this
gitHostInfo.type = type
Object.keys(gitHosts[type]).forEach(function (key) {
gitHostInfo[key] = gitHosts[type][key]
})
gitHostInfo.user = user
gitHostInfo.auth = auth
gitHostInfo.project = project
gitHostInfo.committish = committish
gitHostInfo.default = defaultRepresentation
}
GitHost.prototype = {}
GitHost.prototype.hash = function () {
return this.committish ? '#' + this.committish : ''
}
GitHost.prototype._fill = function (template, vars) {
if (!template) return
if (!vars) vars = {}
var self = this
Object.keys(this).forEach(function (key) {
if (self[key] != null && vars[key] == null) vars[key] = self[key]
})
var rawAuth = vars.auth
var rawComittish = vars.committish
Object.keys(vars).forEach(function (key) {
vars[key] = encodeURIComponent(vars[key])
})
vars['auth@'] = rawAuth ? rawAuth + '@' : ''
vars['#committish'] = rawComittish ? '#' + rawComittish : ''
vars['/tree/committish'] = vars.committish
? '/' + vars.treepath + '/' + vars.committish
: ''
vars['/committish'] = vars.committish ? '/' + vars.committish : ''
vars.committish = vars.committish || 'master'
var res = template
Object.keys(vars).forEach(function (key) {
res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
})
return res
}
GitHost.prototype.ssh = function () {
return this._fill(this.sshtemplate)
}
GitHost.prototype.sshurl = function () {
return this._fill(this.sshurltemplate)
}
GitHost.prototype.browse = function () {
return this._fill(this.browsetemplate)
}
GitHost.prototype.docs = function () {
return this._fill(this.docstemplate)
}
GitHost.prototype.bugs = function () {
return this._fill(this.bugstemplate)
}
GitHost.prototype.https = function () {
return this._fill(this.httpstemplate)
}
GitHost.prototype.git = function () {
return this._fill(this.gittemplate)
}
GitHost.prototype.shortcut = function () {
return this._fill(this.shortcuttemplate)
}
GitHost.prototype.path = function () {
return this._fill(this.pathtemplate)
}
GitHost.prototype.file = function (P) {
return this._fill(this.filetemplate, {
path: P.replace(/^[/]+/g, '')
})
}
GitHost.prototype.getDefaultRepresentation = function () {
return this.default
}
GitHost.prototype.toString = function () {
return (this[this.default] || this.sshurl).call(this)
}