Skip to content

Commit 3ff8e33

Browse files
authored
add: support for multiple main branches (#39)
We try a list of branch names in order.
1 parent 95a6d64 commit 3ff8e33

File tree

5 files changed

+77
-22
lines changed

5 files changed

+77
-22
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## 1.0.0 - 2021-04-17
11+
12+
### Added
13+
14+
- support multiple default branches. vscode-githubinator now attempts to open `main`, then `master`, `trunk`, `develop`, and `dev`. Configure these branches with the `githubinator.mainBranches` option.
15+
16+
### Changed
17+
18+
- renamed `Master` commands to `Main`: `Githubinator: Copy Master` -> `Githubinator: Copy Main`, `Githubinator: Copy Master Permalink` -> `Githubinator: Copy Main Permalink`, `Githubinator: On Master` -> `Githubinator: On Main`, `Githubinator: Blame On Master` -> `Githubinator: Blame On Main`
19+
20+
### Fixed
21+
22+
- support resolving symbolic links. vscode-githubinator will now resolve symbolic links before opening a URL.
23+
1024
## 0.3.1 - 2021-04-17
1125

1226
### Fixed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@ With `vsce` installed from NPM (`yarn global add vsce`), clone [this repo](https
3030
| ------------------------------------- | :------: | :------: | -------------- | -------------- |
3131
| `Githubinator` ||| blob | current branch |
3232
| `Githubinator: Copy` ||| blob | current branch |
33-
| `Githubinator: Copy Master` ||| blob | master branch |
33+
| `Githubinator: Copy Master` ||| blob | "main" branch |
3434
| `Githubinator: Copy Permalink` ||| blob | current SHA |
35-
| `Githubinator: Copy Master Permalink` ||| blob | master SHA |
36-
| `Githubinator: On Master` ||| blob | master branch |
35+
| `Githubinator: Copy Master Permalink` ||| blob | "main" SHA |
36+
| `Githubinator: On Master` ||| blob | "main" branch |
3737
| `Githubinator: Permalink` ||| blob | current SHA |
3838
| `Githubinator: Blame` ||| blame | current branch |
39-
| `Githubinator: Blame On Master` ||| blame | master branch |
39+
| `Githubinator: Blame On Master` ||| blame | "main" branch |
4040
| `Githubinator: Blame Permalink` ||| blame | current sha |
4141
| `Githubinator: Repository` ||| open repo | N/A |
4242
| `Githubinator: History` ||| open history | N/A |
4343
| `Githubinator: Open PR` ||| open PR | N/A |
4444
| `Githubinator: Compare` ||| compare branch | N/A |
4545

46+
The "main" branch is configured via `githubinator.mainBranches` (see "Extension Settings" below).
47+
4648
## Requirements
4749

4850
- Local Git repository. You must have a git repository configured with a remote. (`"origin"` is default but this can be changed in settings).
4951

5052
## Extension Settings
5153

54+
- `githubinator.enable_context_menu`: Enable access to Githubinator commands from the context menu. (default: `true`)
55+
- `githubinator.mainBranches`: Branch names to use as `main` repository branch. (default: `["main", "master", "trunk", "develop", "dev"]`)
5256
- `githubinator.remote`: The default remote branch for a repository. (default: `"origin"`)
5357
- `githubinator.providers.github.remote`: Remote name to look for when identifying a Github origin. (default: `"origin"`)
5458
- `githubinator.providers.github.hostnames`: Hostnames for identifying a Github origin and building a URL. (default: `["github.com"]`)
@@ -65,6 +69,12 @@ With `vsce` installed from NPM (`yarn global add vsce`), clone [this repo](https
6569

6670
## Release Notes
6771

72+
## 1.0.0
73+
74+
- support multiple default branches. vscode-githubinator now attempts to open `main`, then `master`, `trunk`, `develop`, and `dev`. Configure these branches with the `githubinator.mainBranches` option.
75+
- renamed `Master` commands to `Main`: `Githubinator: Copy Master` -> `Githubinator: Copy Main`, `Githubinator: Copy Master Permalink` -> `Githubinator: Copy Main Permalink`, `Githubinator: On Master` -> `Githubinator: On Main`, `Githubinator: Blame On Master` -> `Githubinator: Blame On Main`
76+
- support resolving symbolic links. vscode-githubinator will now resolve symbolic links before opening a URL.
77+
6878
## 0.3.1
6979

7080
- allow origin urls not ending in `.git` for GitHub.

package.json

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "githubinator",
33
"displayName": "Githubinator",
44
"description": "Quickly open files on Github and other providers. View blame information, copy permalinks and more. See the \"commands\" section of the README for more details.",
5-
"version": "0.3.1",
5+
"version": "1.0.0",
66
"publisher": "chdsbd",
77
"license": "SEE LICENSE IN LICENSE",
88
"icon": "images/logo256.png",
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"command": "extension.githubinatorCopyMaster",
64-
"title": "Copy Master",
64+
"title": "Copy Main",
6565
"category": "Githubinator"
6666
},
6767
{
@@ -71,12 +71,12 @@
7171
},
7272
{
7373
"command": "extension.githubinatorCopyMasterPermalink",
74-
"title": "Copy Master Permalink",
74+
"title": "Copy Main Permalink",
7575
"category": "Githubinator"
7676
},
7777
{
7878
"command": "extension.githubinatorOnMaster",
79-
"title": "On Master",
79+
"title": "On Main",
8080
"category": "Githubinator"
8181
},
8282
{
@@ -91,7 +91,7 @@
9191
},
9292
{
9393
"command": "extension.githubinatorBlameOnMaster",
94-
"title": "Blame On Master",
94+
"title": "Blame On Main",
9595
"category": "Githubinator"
9696
},
9797
{
@@ -196,13 +196,27 @@
196196
},
197197
"configuration": {
198198
"type": "object",
199-
"title": "Githubinator configuration",
199+
"title": "Githubinator",
200200
"properties": {
201201
"githubinator.enable_context_menu": {
202202
"type": "boolean",
203203
"default": true,
204204
"description": "Enable access to Githubinator commands from the context menu."
205205
},
206+
"githubinator.mainBranches": {
207+
"type": "array",
208+
"items": {
209+
"type": "string"
210+
},
211+
"default": [
212+
"main",
213+
"master",
214+
"trunk",
215+
"develop",
216+
"dev"
217+
],
218+
"description": "Branch names to use as `main` repository branch. If you call \"Githubinator: Blame on Main\", we try to open the `main` branch then fall back to the next name."
219+
},
206220
"githubinator.remote": {
207221
"type": "string",
208222
"default": "origin",

src/extension.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ const COMMANDS: [string, IGithubinator][] = [
1414
],
1515
[
1616
"extension.githubinatorCopyMaster", //
17-
{ copyToClipboard: true, branch: "master" },
17+
{ copyToClipboard: true, mainBranch: true },
1818
],
1919
[
2020
"extension.githubinatorCopyPermalink", //
2121
{ copyToClipboard: true, permalink: true },
2222
],
2323
[
2424
"extension.githubinatorCopyMasterPermalink", //
25-
{ copyToClipboard: true, branch: "master", permalink: true },
25+
{ copyToClipboard: true, mainBranch: true, permalink: true },
2626
],
2727
[
2828
"extension.githubinatorOnMaster", //
29-
{ copyToClipboard: true, openUrl: true, branch: "master" },
29+
{ copyToClipboard: true, openUrl: true, mainBranch: true },
3030
],
3131
[
3232
"extension.githubinatorPermalink", //
@@ -38,7 +38,7 @@ const COMMANDS: [string, IGithubinator][] = [
3838
],
3939
[
4040
"extension.githubinatorBlameOnMaster", //
41-
{ copyToClipboard: true, openUrl: true, blame: true, branch: "master" },
41+
{ copyToClipboard: true, openUrl: true, blame: true, mainBranch: true },
4242
],
4343
[
4444
"extension.githubinatorBlamePermalink", //
@@ -115,11 +115,26 @@ function getEditorInfo(): { uri: vscode.Uri | null; fileName: string | null } {
115115
return { uri, fileName }
116116
}
117117

118+
const BRANCHES = ["main", "master", "trunk", "dev", "develop"]
119+
120+
async function findShaForBranches(
121+
gitDir: string,
122+
): Promise<[string, string] | null> {
123+
for (let branch of BRANCHES) {
124+
const sha = await git.getSHAForBranch(gitDir, branch)
125+
if (sha == null) {
126+
continue
127+
}
128+
return [sha, branch]
129+
}
130+
return null
131+
}
132+
118133
interface IGithubinator {
119134
openUrl?: boolean
120135
copyToClipboard?: boolean
121136
blame?: boolean
122-
branch?: string
137+
mainBranch?: boolean
123138
permalink?: boolean
124139
openRepo?: boolean
125140
history?: boolean
@@ -130,7 +145,7 @@ async function githubinator({
130145
openUrl,
131146
copyToClipboard,
132147
blame,
133-
branch,
148+
mainBranch,
134149
openRepo,
135150
permalink,
136151
history,
@@ -148,12 +163,12 @@ async function githubinator({
148163
return err("Could not find .git directory.")
149164
}
150165
let headBranch: [string, string | null] | null = null
151-
if (branch) {
152-
const sha = await git.getSHAForBranch(gitDir, branch)
153-
if (sha == null) {
154-
return err(`Could not find SHA for branch name: ${branch}`)
166+
if (mainBranch) {
167+
const res = await findShaForBranches(gitDir)
168+
if (res == null) {
169+
return err(`Could not find SHA for branch in ${BRANCHES}`)
155170
}
156-
headBranch = [sha, branch]
171+
headBranch = res
157172
} else {
158173
headBranch = await git.head(gitDir)
159174
}

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as path from "path"
2+
import * as fs from "fs"
23
/** Get path of file relative to git root. */
34
export function getRelativeFilePath(gitDir: string, fileName: string): string {
5+
const resolvedFileName = fs.realpathSync(fileName)
46
const gitProjectRoot = path.dirname(gitDir) + "/"
5-
return fileName.replace(gitProjectRoot, "")
7+
return resolvedFileName.replace(gitProjectRoot, "")
68
}
79

810
/** Convert url/hostname to hostname

0 commit comments

Comments
 (0)