Skip to content

Commit 32ae270

Browse files
marocchinobugthesystem
authored andcommitted
Add option to use commit sha instead of branch (#87)
1 parent 4400c8f commit 32ae270

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ Add following line into workspace settings;
7373
{
7474
"openInGitHub.gitHubDomain": "your custom github domain here",
7575
"openInGitHub.requireSelectionForLines": false, // If enabled, the copied or opened URL won't include line number(s) unless there's an active selection
76-
"openInGitHub.providerType": "gitlab" //github, gitlab, bitbucket, ...
76+
"openInGitHub.useCommitSHAInURL": false,
77+
"openInGitHub.providerType": "gitlab", //github, gitlab, bitbucket, ...
7778
"openInGitHub.providerProtocol": "https" //https, http. Useful for custom domains that don't support https. Defaults to https.
7879
}
7980
```

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
"default": false,
6262
"description": "If enabled, the copied or opened URL won't include line number(s) unless there's an active selection"
6363
},
64+
"openInGitHub.useCommitSHAInURL": {
65+
"type": "boolean",
66+
"default": false,
67+
"description": "If enabled, use commit SHA instead of branch"
68+
},
6469
"openInGitHub.providerType": {
6570
"type": "string",
6671
"default": "gitlab",

src/gitProvider.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const workspace = require('vscode').workspace
44
const querystring = require('querystring');
55
const gitUrlParse = require('git-url-parse');
66
const path = require('path');
7+
const useCommitSHAInURL = workspace.getConfiguration('openInGitHub').get('useCommitSHAInURL');
78

89
class BaseProvider {
910
constructor(gitUrl, sha) {
@@ -34,12 +35,16 @@ class BaseProvider {
3435

3536
class GitHub extends BaseProvider {
3637
webUrl(branch, filePath, line, endLine) {
38+
let blob = branch;
39+
if (useCommitSHAInURL) {
40+
blob = this.sha;
41+
}
3742
if (filePath) {
38-
return `${this.baseUrl}/blob/${branch}${filePath}` + (line ? '#L' + line : '') + (endLine ? '-L' + endLine : '');
43+
return `${this.baseUrl}/blob/${blob}${filePath}` + (line ? '#L' + line : '') + (endLine ? '-L' + endLine : '');
3944
}
40-
return `${this.baseUrl}/tree/${branch}`;
45+
return `${this.baseUrl}/tree/${blob}`;
4146
}
42-
prUrl(branch){
47+
prUrl(branch) {
4348
return `${this.baseUrl}/pull/new/${branch}`;
4449
}
4550
}
@@ -49,7 +54,7 @@ class Bitbucket extends BaseProvider {
4954
const fileName = path.basename(filePath)
5055
return `${this.baseUrl}/src/${this.sha}` + (filePath ? `${filePath}` : '') + (line ? `#${fileName}-${line}` : '');
5156
}
52-
prUrl(branch){
57+
prUrl(branch) {
5358
return `${this.baseUrl}/pull-requests/new?source=${branch}`;
5459
}
5560
}
@@ -61,7 +66,7 @@ class GitLab extends BaseProvider {
6166
}
6267
return `${this.baseUrl}/tree/${branch}`;
6368
}
64-
prUrl(branch){
69+
prUrl(branch) {
6570
//https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
6671
//`${this.baseUrl}/merge-requests/new?source_branch=${branch}&target_branch=${????}&title=${????}`
6772
throw new Error(`Doesn't support Merge Request from URL in GitLab yet`);
@@ -86,7 +91,7 @@ class VisualStudio extends BaseProvider {
8691
return `${this.baseUrl}?${querystring.stringify(query)}`;
8792
}
8893

89-
prUrl(branch){
94+
prUrl(branch) {
9095
throw new Error(`Doesn't support Merge Request from URL in VisualStudio.com yet`);
9196
}
9297
}
@@ -113,7 +118,7 @@ function gitProvider(remoteUrl, sha) {
113118
for (const domain of Object.keys(providers)) {
114119
if (domain === gitUrl.resource || domain === gitUrl.source) {
115120
return new providers[domain](gitUrl, sha);
116-
}else if( domain.indexOf(providerType) > -1 ){
121+
} else if (domain.indexOf(providerType) > -1) {
117122
return new providers[domain](gitUrl, sha);
118123
}
119124
}

0 commit comments

Comments
 (0)