Skip to content

Commit 79fd1b0

Browse files
authored
Merge pull request #43 from itiut/add-support-for-gitlab-com
Add support for GitLab.com
2 parents 540b532 + b3df135 commit 79fd1b0

File tree

5 files changed

+338
-56
lines changed

5 files changed

+338
-56
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
root = true
22

33
[*]
4-
indent_style = tab
4+
indent_style = space
55
end_of_line = lf
66
charset = utf-8
77
trim_trailing_whitespace = true

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@
8686
},
8787
"devDependencies": {
8888
"chai": "^3.5.0",
89+
"proxyquire": "^1.7.10",
8990
"typescript": "^1.6.2",
9091
"vscode": "0.11.13"
9192
},
9293
"dependencies": {
9394
"copy-paste": "^1.1.4",
9495
"find-parent-dir": "^0.3.0",
9596
"git-rev-2": "^0.1.0",
96-
"github-url-from-git": "^1.4.0",
97+
"git-url-parse": "^6.1.0",
9798
"open": "0.0.5",
9899
"parse-git-config": "^0.3.1"
99100
}
100-
}
101+
}

src/extension.js

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,12 @@ var Position = VsCode.Position;
1010
var path = require('path');
1111
var fs = require('fs');
1212
var git = require('parse-git-config');
13-
var parse = require('github-url-from-git');
1413
var open = require('open');
1514
var copy = require('copy-paste').copy;
1615
var gitRev = require('git-rev-2');
1716
var findParentDir = require('find-parent-dir');
1817

19-
function formGitHubLink(parsedUri, branch, subdir, line) {
20-
if (subdir) {
21-
return parsedUri + "/blob/" + branch + subdir + (line ? "#L" + line : "");
22-
}
23-
24-
return parsedUri + "/tree/" + branch;
25-
}
26-
27-
28-
function formBitBucketLink(parsedUri, branch, subdir, line) {
29-
return parsedUri + "/src/" + branch + (subdir ? subdir : "") + (line ? "#cl-" + line : "");
30-
}
31-
32-
function formVisualStudioLink(parsedUri, subdir, branch, line) {
33-
return parsedUri + "#" + (subdir ? "path=" + subdir : "") + "&version=GB" + branch + (line ? "&line=" + line : "");
34-
}
18+
const gitProvider = require('./gitProvider');
3519

3620
function getGitHubLink(cb, fileFsPath, line) {
3721
var cwd = workspace.rootPath;
@@ -40,56 +24,27 @@ function getGitHubLink(cb, fileFsPath, line) {
4024
git({
4125
cwd: repoDir
4226
}, function (err, config) {
43-
var rawUri, parseOpts, parsedUri, branch, projectName,
44-
subdir, gitLink, scUrls, workspaceConfiguration;
45-
46-
workspaceConfiguration = VsCode.workspace.getConfiguration("openInGitHub");
47-
scUrls = {
48-
github: 'https://' + workspaceConfiguration.gitHubDomain,
49-
bitbucket: 'https://bitbucket.org',
50-
visualstudiocom: /^https:\/\/[\w\d-]*\.visualstudio.com\//
51-
}
27+
const rawUri = config['remote \"origin\"'].url;
28+
const provider = gitProvider(rawUri);
5229

53-
rawUri = config['remote \"origin\"'].url;
54-
parseOpts = {
55-
extraBaseUrls: [
56-
'bitbucket.org',
57-
workspaceConfiguration.gitHubDomain
58-
]
59-
}
60-
61-
rawUri = rawUri.replace('bitbucket.org:', 'bitbucket.org/')
62-
63-
parsedUri = parse(rawUri, parseOpts);
64-
if (!parsedUri) {
65-
parsedUri = rawUri;
30+
if (!provider) {
31+
Window.showWarningMessage('Unknown Git provider.');
32+
return;
6633
}
6734

6835
gitRev.branch(cwd, function (branchErr, branch) {
6936
if (branchErr || !branch)
7037
branch = 'master';
7138

72-
projectName = parsedUri.substring(parsedUri.lastIndexOf("/") + 1, parsedUri.length);
73-
subdir = fileFsPath ? fileFsPath.substring(workspace.rootPath.length).replace(/\"/g, "") : undefined;
39+
let subdir = fileFsPath ? fileFsPath.substring(workspace.rootPath.length).replace(/\"/g, "") : undefined;
7440

7541
if (repoDir !== cwd) {
7642
// The workspace directory is a subdirectory of the git repo folder so we need to prepend the the nested path
7743
var repoRelativePath = cwd.replace(repoDir, "/");
7844
subdir = repoRelativePath + subdir;
7945
}
8046

81-
if (parsedUri.startsWith(scUrls.github)) {
82-
gitLink = formGitHubLink(parsedUri, branch, subdir, line);
83-
} else if (parsedUri.startsWith(scUrls.bitbucket)) {
84-
gitLink = formBitBucketLink(parsedUri, branch, subdir, line);
85-
} else if (scUrls.visualstudiocom.test(parsedUri)) {
86-
gitLink = formVisualStudioLink(parsedUri, subdir, branch, line);
87-
} else {
88-
Window.showWarningMessage('Unknown Git provider.');
89-
}
90-
91-
if (gitLink)
92-
cb(gitLink);
47+
cb(provider.webUrl(branch, subdir, line));
9348
});
9449
});
9550
}

src/gitProvider.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
const workspace = require('vscode').workspace
4+
const querystring = require('querystring');
5+
const gitUrlParse = require('git-url-parse');
6+
7+
class BaseProvider {
8+
constructor(gitUrl) {
9+
this.gitUrl = gitUrl;
10+
}
11+
12+
get baseUrl() {
13+
return this.gitUrl.toString('https').replace(/\.git/, '');
14+
}
15+
16+
/**
17+
* Get the Web URL.
18+
*
19+
* @param {string} branch
20+
* @param {string} filePath The file path relative to repository root, beginning with '/'.
21+
* @param {number} line
22+
* @return {string} The URL to be opened with the browser.
23+
*/
24+
webUrl(branch, filePath, line) {
25+
return '';
26+
}
27+
}
28+
29+
class GitHub extends BaseProvider {
30+
webUrl(branch, filePath, line) {
31+
if (filePath) {
32+
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : '');
33+
}
34+
return `${this.baseUrl}/tree/${branch}`;
35+
}
36+
}
37+
38+
class Bitbucket extends BaseProvider {
39+
webUrl(branch, filePath, line) {
40+
return `${this.baseUrl}/src/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#cl-${line}` : '');
41+
}
42+
}
43+
44+
class GitLab extends GitHub {
45+
}
46+
47+
class VisualStudio extends BaseProvider {
48+
get baseUrl() {
49+
return `https://${this.gitUrl.resource}${this.gitUrl.pathname}`.replace(/\.git/, '');
50+
}
51+
52+
webUrl(branch, filePath, line) {
53+
let query = {
54+
version: `GB${branch}`,
55+
};
56+
if (filePath) {
57+
query['path'] = filePath;
58+
}
59+
if (line) {
60+
query['line'] = line;
61+
}
62+
return `${this.baseUrl}?${querystring.stringify(query)}`;
63+
}
64+
}
65+
66+
const gitHubDomain = workspace.getConfiguration('openInGitHub').get('gitHubDomain', 'github.com');
67+
68+
const providers = {
69+
[gitHubDomain]: GitHub,
70+
'bitbucket.org': Bitbucket,
71+
'gitlab.com': GitLab,
72+
'visualstudio.com': VisualStudio,
73+
};
74+
75+
/**
76+
* Get the Git provider of the remote URL.
77+
*
78+
* @param {string} remoteUrl
79+
* @return {BaseProvider|null}
80+
*/
81+
function gitProvider(remoteUrl) {
82+
const gitUrl = gitUrlParse(remoteUrl);
83+
for (const domain of Object.keys(providers)) {
84+
if (domain === gitUrl.resource || domain === gitUrl.source) {
85+
return new providers[domain](gitUrl);
86+
}
87+
}
88+
return null;
89+
}
90+
91+
module.exports = gitProvider;

0 commit comments

Comments
 (0)