forked from npm/hosted-git-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-host.js
133 lines (117 loc) · 3.78 KB
/
git-host.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict'
var gitHosts = require('./git-host-info.js')
/* eslint-disable node/no-deprecated-api */
var extend = Object.assign || require('util')._extend
var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) {
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
gitHostInfo.opts = opts || {}
}
GitHost.prototype = {}
GitHost.prototype.hash = function () {
return this.committish ? '#' + this.committish : ''
}
GitHost.prototype._fill = function (template, opts) {
if (!template) return
var vars = extend({}, opts)
vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
opts = extend(extend({}, this.opts), opts)
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 rawcommittish = vars.committish
var rawFragment = vars.fragment
var rawPath = vars.path
var rawProject = vars.project
Object.keys(vars).forEach(function (key) {
vars[key] = encodeURIComponent(vars[key])
})
vars['auth@'] = rawAuth ? rawAuth + '@' : ''
vars['#fragment'] = rawFragment ? '#' + this.hashformat(rawFragment) : ''
vars.fragment = vars.fragment ? vars.fragment : ''
vars['#path'] = rawPath ? '#' + this.hashformat(rawPath) : ''
vars['/path'] = vars.path ? '/' + vars.path : ''
vars.projectPath = rawProject.split('/').map(encodeURIComponent).join('/')
if (opts.noCommittish) {
vars['#committish'] = ''
vars['/tree/committish'] = ''
vars['/committish'] = ''
vars.committish = ''
} else {
vars['#committish'] = rawcommittish ? '#' + rawcommittish : ''
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])
})
if (opts.noGitPlus) {
return res.replace(/^git[+]/, '')
} else {
return res
}
}
GitHost.prototype.ssh = function (opts) {
return this._fill(this.sshtemplate, opts)
}
GitHost.prototype.sshurl = function (opts) {
return this._fill(this.sshurltemplate, opts)
}
GitHost.prototype.browse = function (P, F, opts) {
if (typeof P === 'string') {
if (typeof F !== 'string') {
opts = F
F = null
}
return this._fill(this.browsefiletemplate, extend({
fragment: F,
path: P
}, opts))
} else {
return this._fill(this.browsetemplate, P)
}
}
GitHost.prototype.docs = function (opts) {
return this._fill(this.docstemplate, opts)
}
GitHost.prototype.bugs = function (opts) {
return this._fill(this.bugstemplate, opts)
}
GitHost.prototype.https = function (opts) {
return this._fill(this.httpstemplate, opts)
}
GitHost.prototype.git = function (opts) {
return this._fill(this.gittemplate, opts)
}
GitHost.prototype.shortcut = function (opts) {
return this._fill(this.shortcuttemplate, opts)
}
GitHost.prototype.path = function (opts) {
return this._fill(this.pathtemplate, opts)
}
GitHost.prototype.tarball = function (opts) {
return this._fill(this.tarballtemplate, opts)
}
GitHost.prototype.file = function (P, opts) {
return this._fill(this.filetemplate, extend({ path: P }, opts))
}
GitHost.prototype.getDefaultRepresentation = function () {
return this.default
}
GitHost.prototype.toString = function (opts) {
return (this[this.default] || this.sshurl).call(this, opts)
}