Skip to content

Commit

Permalink
Merge pull request #43 from itiut/add-support-for-gitlab-com
Browse files Browse the repository at this point in the history
Add support for GitLab.com
  • Loading branch information
bugthesystem authored Nov 30, 2016
2 parents 540b532 + b3df135 commit 79fd1b0
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
root = true

[*]
indent_style = tab
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@
},
"devDependencies": {
"chai": "^3.5.0",
"proxyquire": "^1.7.10",
"typescript": "^1.6.2",
"vscode": "0.11.13"
},
"dependencies": {
"copy-paste": "^1.1.4",
"find-parent-dir": "^0.3.0",
"git-rev-2": "^0.1.0",
"github-url-from-git": "^1.4.0",
"git-url-parse": "^6.1.0",
"open": "0.0.5",
"parse-git-config": "^0.3.1"
}
}
}
61 changes: 8 additions & 53 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,12 @@ var Position = VsCode.Position;
var path = require('path');
var fs = require('fs');
var git = require('parse-git-config');
var parse = require('github-url-from-git');
var open = require('open');
var copy = require('copy-paste').copy;
var gitRev = require('git-rev-2');
var findParentDir = require('find-parent-dir');

function formGitHubLink(parsedUri, branch, subdir, line) {
if (subdir) {
return parsedUri + "/blob/" + branch + subdir + (line ? "#L" + line : "");
}

return parsedUri + "/tree/" + branch;
}


function formBitBucketLink(parsedUri, branch, subdir, line) {
return parsedUri + "/src/" + branch + (subdir ? subdir : "") + (line ? "#cl-" + line : "");
}

function formVisualStudioLink(parsedUri, subdir, branch, line) {
return parsedUri + "#" + (subdir ? "path=" + subdir : "") + "&version=GB" + branch + (line ? "&line=" + line : "");
}
const gitProvider = require('./gitProvider');

function getGitHubLink(cb, fileFsPath, line) {
var cwd = workspace.rootPath;
Expand All @@ -40,56 +24,27 @@ function getGitHubLink(cb, fileFsPath, line) {
git({
cwd: repoDir
}, function (err, config) {
var rawUri, parseOpts, parsedUri, branch, projectName,
subdir, gitLink, scUrls, workspaceConfiguration;

workspaceConfiguration = VsCode.workspace.getConfiguration("openInGitHub");
scUrls = {
github: 'https://' + workspaceConfiguration.gitHubDomain,
bitbucket: 'https://bitbucket.org',
visualstudiocom: /^https:\/\/[\w\d-]*\.visualstudio.com\//
}
const rawUri = config['remote \"origin\"'].url;
const provider = gitProvider(rawUri);

rawUri = config['remote \"origin\"'].url;
parseOpts = {
extraBaseUrls: [
'bitbucket.org',
workspaceConfiguration.gitHubDomain
]
}

rawUri = rawUri.replace('bitbucket.org:', 'bitbucket.org/')

parsedUri = parse(rawUri, parseOpts);
if (!parsedUri) {
parsedUri = rawUri;
if (!provider) {
Window.showWarningMessage('Unknown Git provider.');
return;
}

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

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

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

if (parsedUri.startsWith(scUrls.github)) {
gitLink = formGitHubLink(parsedUri, branch, subdir, line);
} else if (parsedUri.startsWith(scUrls.bitbucket)) {
gitLink = formBitBucketLink(parsedUri, branch, subdir, line);
} else if (scUrls.visualstudiocom.test(parsedUri)) {
gitLink = formVisualStudioLink(parsedUri, subdir, branch, line);
} else {
Window.showWarningMessage('Unknown Git provider.');
}

if (gitLink)
cb(gitLink);
cb(provider.webUrl(branch, subdir, line));
});
});
}
Expand Down
91 changes: 91 additions & 0 deletions src/gitProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

const workspace = require('vscode').workspace
const querystring = require('querystring');
const gitUrlParse = require('git-url-parse');

class BaseProvider {
constructor(gitUrl) {
this.gitUrl = gitUrl;
}

get baseUrl() {
return this.gitUrl.toString('https').replace(/\.git/, '');
}

/**
* Get the Web URL.
*
* @param {string} branch
* @param {string} filePath The file path relative to repository root, beginning with '/'.
* @param {number} line
* @return {string} The URL to be opened with the browser.
*/
webUrl(branch, filePath, line) {
return '';
}
}

class GitHub extends BaseProvider {
webUrl(branch, filePath, line) {
if (filePath) {
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : '');
}
return `${this.baseUrl}/tree/${branch}`;
}
}

class Bitbucket extends BaseProvider {
webUrl(branch, filePath, line) {
return `${this.baseUrl}/src/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#cl-${line}` : '');
}
}

class GitLab extends GitHub {
}

class VisualStudio extends BaseProvider {
get baseUrl() {
return `https://${this.gitUrl.resource}${this.gitUrl.pathname}`.replace(/\.git/, '');
}

webUrl(branch, filePath, line) {
let query = {
version: `GB${branch}`,
};
if (filePath) {
query['path'] = filePath;
}
if (line) {
query['line'] = line;
}
return `${this.baseUrl}?${querystring.stringify(query)}`;
}
}

const gitHubDomain = workspace.getConfiguration('openInGitHub').get('gitHubDomain', 'github.com');

const providers = {
[gitHubDomain]: GitHub,
'bitbucket.org': Bitbucket,
'gitlab.com': GitLab,
'visualstudio.com': VisualStudio,
};

/**
* Get the Git provider of the remote URL.
*
* @param {string} remoteUrl
* @return {BaseProvider|null}
*/
function gitProvider(remoteUrl) {
const gitUrl = gitUrlParse(remoteUrl);
for (const domain of Object.keys(providers)) {
if (domain === gitUrl.resource || domain === gitUrl.source) {
return new providers[domain](gitUrl);
}
}
return null;
}

module.exports = gitProvider;
Loading

0 comments on commit 79fd1b0

Please sign in to comment.