2 var gitHosts = require('./git-host-info.js')
4 var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation) {
6 gitHostInfo.type = type
7 Object.keys(gitHosts[type]).forEach(function (key) {
8 gitHostInfo[key] = gitHosts[type][key]
10 gitHostInfo.user = user
11 gitHostInfo.auth = auth
12 gitHostInfo.project = project
13 gitHostInfo.committish = committish
14 gitHostInfo.default = defaultRepresentation
16 GitHost.prototype = {}
18 GitHost.prototype.hash = function () {
19 return this.committish ? '#' + this.committish : ''
22 GitHost.prototype._fill = function (template, vars) {
26 Object.keys(this).forEach(function (key) {
27 if (self[key] != null && vars[key] == null) vars[key] = self[key]
29 var rawAuth = vars.auth
30 var rawComittish = vars.committish
31 Object.keys(vars).forEach(function (key) {
32 vars[key] = encodeURIComponent(vars[key])
34 vars['auth@'] = rawAuth ? rawAuth + '@' : ''
35 vars['#committish'] = rawComittish ? '#' + rawComittish : ''
36 vars['/tree/committish'] = vars.committish
37 ? '/' + vars.treepath + '/' + vars.committish
39 vars['/committish'] = vars.committish ? '/' + vars.committish : ''
40 vars.committish = vars.committish || 'master'
42 Object.keys(vars).forEach(function (key) {
43 res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
48 GitHost.prototype.ssh = function () {
49 return this._fill(this.sshtemplate)
52 GitHost.prototype.sshurl = function () {
53 return this._fill(this.sshurltemplate)
56 GitHost.prototype.browse = function () {
57 return this._fill(this.browsetemplate)
60 GitHost.prototype.docs = function () {
61 return this._fill(this.docstemplate)
64 GitHost.prototype.bugs = function () {
65 return this._fill(this.bugstemplate)
68 GitHost.prototype.https = function () {
69 return this._fill(this.httpstemplate)
72 GitHost.prototype.git = function () {
73 return this._fill(this.gittemplate)
76 GitHost.prototype.shortcut = function () {
77 return this._fill(this.shortcuttemplate)
80 GitHost.prototype.path = function () {
81 return this._fill(this.pathtemplate)
84 GitHost.prototype.file = function (P) {
85 return this._fill(this.filetemplate, {
86 path: P.replace(/^[/]+/g, '')
90 GitHost.prototype.getDefaultRepresentation = function () {
94 GitHost.prototype.toString = function () {
95 return (this[this.default] || this.sshurl).call(this)