Skip to content

Commit fc1dfd7

Browse files
authored
#issue-72 fixed and small changes (#77)
1 parent 84e172b commit fc1dfd7

File tree

2 files changed

+93
-80
lines changed

2 files changed

+93
-80
lines changed

src/extension.js

Lines changed: 87 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -20,87 +20,88 @@ const requireSelectionForLines = workspace.getConfiguration('openInGitHub').get(
2020

2121
function getGitProviderLink(cb, fileFsPath, lines, pr) {
2222
var repoDir = findParentDir.sync(fileFsPath, '.git');
23-
if(!repoDir) {
23+
if (!repoDir) {
2424
throw 'Cant locate .git repository for this file';
2525
}
2626

2727
locateGitConfig(repoDir)
28-
.then(readConfigFile)
29-
.then(config => {
30-
31-
gitRev.branch(repoDir, function(branchErr, branch) {
32-
var rawUri,
33-
configuredBranch,
34-
provider = null,
35-
remoteName;
36-
37-
if (branchErr || !branch) branch = 'master';
38-
39-
// Check to see if the branch has a configured remote
40-
configuredBranch = config[`branch "${branch}"`];
41-
42-
if (configuredBranch) {
43-
// Use the current branch's configured remote
44-
remoteName = configuredBranch.remote;
45-
rawUri = config[`remote "${remoteName}"`].url;
46-
} else {
47-
const remotes = Object.keys(config).filter(k => k.startsWith('remote '));
48-
if (remotes.length > 0) {
49-
rawUri = config[remotes[0]].url;
28+
.then(readConfigFile)
29+
.then(config => {
30+
31+
gitRev.branch(repoDir, function (branchErr, branch) {
32+
var rawUri,
33+
configuredBranch,
34+
provider = null,
35+
remoteName;
36+
37+
if (branchErr || !branch) branch = 'master';
38+
39+
// Check to see if the branch has a configured remote
40+
configuredBranch = config[`branch "${branch}"`];
41+
42+
if (configuredBranch) {
43+
// Use the current branch's configured remote
44+
remoteName = configuredBranch.remote;
45+
rawUri = config[`remote "${remoteName}"`].url;
46+
} else {
47+
const remotes = Object.keys(config).filter(k => k.startsWith('remote '));
48+
if (remotes.length > 0) {
49+
rawUri = config[remotes[0]].url;
50+
}
5051
}
51-
}
52-
53-
if (!rawUri) {
54-
Window.showWarningMessage(`No remote found on branch.`);
55-
return;
56-
}
57-
58-
try {
59-
provider = gitProvider(rawUri);
60-
} catch (e) {
61-
let errmsg = e.toString();
62-
Window.showWarningMessage(`Unknown Git provider. ${errmsg}`);
63-
return;
64-
}
6552

66-
let subdir = repoDir !== fileFsPath ? '/' + path.relative(repoDir, fileFsPath) : '';
53+
if (!rawUri) {
54+
Window.showWarningMessage(`No remote found on branch.`);
55+
return;
56+
}
6757

68-
if (pr){
6958
try {
70-
cb(provider.prUrl(branch));
71-
}catch (e){
72-
Window.showWarningMessage(e.toString());
59+
provider = gitProvider(rawUri);
60+
} catch (e) {
61+
let errmsg = e.toString();
62+
Window.showWarningMessage(`Unknown Git provider. ${errmsg}`);
7363
return;
7464
}
75-
}
76-
else {
77-
if (lines) {
78-
if (lines[0] == lines[1]) {
79-
cb(provider.webUrl(branch, subdir, lines[0]));
65+
66+
let subdir = repoDir !== fileFsPath ? '/' + path.relative(repoDir, fileFsPath) : '';
67+
68+
if (pr) {
69+
try {
70+
cb(provider.prUrl(branch));
71+
} catch (e) {
72+
Window.showWarningMessage(e.toString());
73+
return;
8074
}
81-
else {
82-
cb(provider.webUrl(branch, subdir, lines[0], lines[1]));
75+
} else {
76+
if (lines) {
77+
if (lines[0] == lines[1]) {
78+
cb(provider.webUrl(branch, subdir, lines[0]));
79+
} else {
80+
cb(provider.webUrl(branch, subdir, lines[0], lines[1]));
81+
}
82+
} else {
83+
cb(provider.webUrl(branch, subdir));
8384
}
8485
}
85-
else {
86-
cb(provider.webUrl(branch, subdir));
87-
}
88-
}
86+
});
8987
});
90-
});
9188
}
9289

9390
function locateGitConfig(repoDir) {
9491
return new Promise((resolve, reject) => {
9592
fs.lstat(path.join(repoDir, '.git'), (err, stat) => {
96-
if(err) { reject(err); }
97-
if(stat.isFile()) {
93+
if (err) {
94+
reject(err);
95+
}
96+
if (stat.isFile()) {
9897
// .git may be a file, similar to symbolic link, containing "gitdir: <relative path to git dir>""
9998
// this happens in gitsubmodules
10099
fs.readFile(path.join(repoDir, '.git'), 'utf-8', (err, data) => {
101-
if(err) { reject(err); }
100+
if (err) {
101+
reject(err);
102+
}
102103
var match = data.match(/gitdir: (.*)/)[1];
103-
if(!match) {
104+
if (!match) {
104105
reject('Unable to find gitdir in .git file');
105106
}
106107
var configPath = path.join(repoDir, match, 'config');
@@ -116,7 +117,9 @@ function locateGitConfig(repoDir) {
116117
function readConfigFile(path) {
117118
return new Promise((resolve, reject) => {
118119
fs.readFile(path, 'utf-8', (err, data) => {
119-
if(err) { reject(err); }
120+
if (err) {
121+
reject(err);
122+
}
120123
resolve(ini.parse(data));
121124
});
122125
});
@@ -128,15 +131,12 @@ function getGitProviderLinkForFile(fileFsPath, cb) {
128131

129132
function getGitProviderLinkForCurrentEditorLines(cb) {
130133
var editor = Window.activeTextEditor;
131-
if (editor) {
132-
var fileFsPath = editor.document.uri.fsPath;
133-
134-
if (includeLines(editor)) {
135-
getGitProviderLink(cb, fileFsPath, getSelectedLines(editor));
136-
}
137-
else {
138-
getGitProviderLinkForFile(fileFsPath, cb);
139-
}
134+
var fileFsPath = editor.document.uri.fsPath;
135+
136+
if (includeLines(editor)) {
137+
getGitProviderLink(cb, fileFsPath, getSelectedLines(editor));
138+
} else {
139+
getGitProviderLinkForFile(fileFsPath, cb);
140140
}
141141
}
142142

@@ -148,29 +148,38 @@ function getSelectedLines(editor) {
148148
var anchorLineIndex = editor.selection.anchor.line + 1;
149149
var activeLineIndex = editor.selection.active.line + 1;
150150

151-
return [anchorLineIndex, activeLineIndex].sort(function(a, b) { return a - b });
151+
return [anchorLineIndex, activeLineIndex].sort(function (a, b) {
152+
return a - b
153+
});
152154
}
153155

154156
function getGitProviderLinkForRepo(cb) {
155157
getGitProviderLink(cb);
156158
}
157159

158-
function getGitProviderPullRequest(cb) {
159-
getGitProviderLink(cb, undefined, undefined, true);
160-
}
160+
function getGitProviderPullRequest(args, cb) {
161+
let fsPath;
162+
163+
if (args && args.fsPath) {
164+
fsPath = args.fsPath;
165+
} else if (Window.activeTextEditor) {
166+
fsPath = Window.activeTextEditor.document.uri.fsPath;
167+
}
161168

169+
getGitProviderLink(cb, fsPath, undefined, true);
170+
}
162171

163172
function branchOnCallingContext(args, cb, pr) {
164173
if (pr) {
165-
getGitProviderPullRequest(cb);
174+
return getGitProviderPullRequest(args, cb);
166175
}
167-
else if (args && args.fsPath) {
176+
177+
if (args && args.fsPath) {
168178
getGitProviderLinkForFile(args.fsPath, cb);
169-
}
170-
else if (Window.activeTextEditor) {
179+
} else if (Window.activeTextEditor) {
171180
getGitProviderLinkForCurrentEditorLines(cb);
172-
}
173-
else {
181+
} else {
182+
// TODO: This missed in code review so should be refactored, it is broken.
174183
getGitProviderLinkForRepo(cb);
175184
}
176185
}

src/gitProvider.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Bitbucket extends BaseProvider {
5151
}
5252
}
5353

54-
class GitLab extends GitHub {
54+
class GitLab extends BaseProvider {
5555
webUrl(branch, filePath, line, endLine) {
5656
if (filePath) {
5757
return `${this.baseUrl}/blob/${branch}` + (filePath ? `${filePath}` : '') + (line ? `#L${line}` : '');
@@ -61,7 +61,7 @@ class GitLab extends GitHub {
6161
prUrl(branch){
6262
//https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
6363
//`${this.baseUrl}/merge-requests/new?source_branch=${branch}&target_branch=${????}&title=${????}`
64-
throw new Error(`doesn't support Merge Request from URL in gitlab provider yet`);
64+
throw new Error(`Doesn't support Merge Request from URL in GitLab yet`);
6565
}
6666
}
6767

@@ -82,6 +82,10 @@ class VisualStudio extends BaseProvider {
8282
}
8383
return `${this.baseUrl}?${querystring.stringify(query)}`;
8484
}
85+
86+
prUrl(branch){
87+
throw new Error(`Doesn't support Merge Request from URL in VisualStudio.com yet`);
88+
}
8589
}
8690

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

0 commit comments

Comments
 (0)