Skip to content

Commit

Permalink
add: support for multiple main branches (#39)
Browse files Browse the repository at this point in the history
We try a list of branch names in order.
  • Loading branch information
chdsbd authored Apr 17, 2021
1 parent 95a6d64 commit 3ff8e33
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 22 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 1.0.0 - 2021-04-17

### Added

- 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.

### Changed

- 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`

### Fixed

- support resolving symbolic links. vscode-githubinator will now resolve symbolic links before opening a URL.

## 0.3.1 - 2021-04-17

### Fixed
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,29 @@ With `vsce` installed from NPM (`yarn global add vsce`), clone [this repo](https
| ------------------------------------- | :------: | :------: | -------------- | -------------- |
| `Githubinator` ||| blob | current branch |
| `Githubinator: Copy` ||| blob | current branch |
| `Githubinator: Copy Master` ||| blob | master branch |
| `Githubinator: Copy Master` ||| blob | "main" branch |
| `Githubinator: Copy Permalink` ||| blob | current SHA |
| `Githubinator: Copy Master Permalink` ||| blob | master SHA |
| `Githubinator: On Master` ||| blob | master branch |
| `Githubinator: Copy Master Permalink` ||| blob | "main" SHA |
| `Githubinator: On Master` ||| blob | "main" branch |
| `Githubinator: Permalink` ||| blob | current SHA |
| `Githubinator: Blame` ||| blame | current branch |
| `Githubinator: Blame On Master` ||| blame | master branch |
| `Githubinator: Blame On Master` ||| blame | "main" branch |
| `Githubinator: Blame Permalink` ||| blame | current sha |
| `Githubinator: Repository` ||| open repo | N/A |
| `Githubinator: History` ||| open history | N/A |
| `Githubinator: Open PR` ||| open PR | N/A |
| `Githubinator: Compare` ||| compare branch | N/A |

The "main" branch is configured via `githubinator.mainBranches` (see "Extension Settings" below).

## Requirements

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

## Extension Settings

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

## Release Notes

## 1.0.0

- 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.
- 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`
- support resolving symbolic links. vscode-githubinator will now resolve symbolic links before opening a URL.

## 0.3.1

- allow origin urls not ending in `.git` for GitHub.
Expand Down
26 changes: 20 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "githubinator",
"displayName": "Githubinator",
"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.",
"version": "0.3.1",
"version": "1.0.0",
"publisher": "chdsbd",
"license": "SEE LICENSE IN LICENSE",
"icon": "images/logo256.png",
Expand Down Expand Up @@ -61,7 +61,7 @@
},
{
"command": "extension.githubinatorCopyMaster",
"title": "Copy Master",
"title": "Copy Main",
"category": "Githubinator"
},
{
Expand All @@ -71,12 +71,12 @@
},
{
"command": "extension.githubinatorCopyMasterPermalink",
"title": "Copy Master Permalink",
"title": "Copy Main Permalink",
"category": "Githubinator"
},
{
"command": "extension.githubinatorOnMaster",
"title": "On Master",
"title": "On Main",
"category": "Githubinator"
},
{
Expand All @@ -91,7 +91,7 @@
},
{
"command": "extension.githubinatorBlameOnMaster",
"title": "Blame On Master",
"title": "Blame On Main",
"category": "Githubinator"
},
{
Expand Down Expand Up @@ -196,13 +196,27 @@
},
"configuration": {
"type": "object",
"title": "Githubinator configuration",
"title": "Githubinator",
"properties": {
"githubinator.enable_context_menu": {
"type": "boolean",
"default": true,
"description": "Enable access to Githubinator commands from the context menu."
},
"githubinator.mainBranches": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"main",
"master",
"trunk",
"develop",
"dev"
],
"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."
},
"githubinator.remote": {
"type": "string",
"default": "origin",
Expand Down
37 changes: 26 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ const COMMANDS: [string, IGithubinator][] = [
],
[
"extension.githubinatorCopyMaster", //
{ copyToClipboard: true, branch: "master" },
{ copyToClipboard: true, mainBranch: true },
],
[
"extension.githubinatorCopyPermalink", //
{ copyToClipboard: true, permalink: true },
],
[
"extension.githubinatorCopyMasterPermalink", //
{ copyToClipboard: true, branch: "master", permalink: true },
{ copyToClipboard: true, mainBranch: true, permalink: true },
],
[
"extension.githubinatorOnMaster", //
{ copyToClipboard: true, openUrl: true, branch: "master" },
{ copyToClipboard: true, openUrl: true, mainBranch: true },
],
[
"extension.githubinatorPermalink", //
Expand All @@ -38,7 +38,7 @@ const COMMANDS: [string, IGithubinator][] = [
],
[
"extension.githubinatorBlameOnMaster", //
{ copyToClipboard: true, openUrl: true, blame: true, branch: "master" },
{ copyToClipboard: true, openUrl: true, blame: true, mainBranch: true },
],
[
"extension.githubinatorBlamePermalink", //
Expand Down Expand Up @@ -115,11 +115,26 @@ function getEditorInfo(): { uri: vscode.Uri | null; fileName: string | null } {
return { uri, fileName }
}

const BRANCHES = ["main", "master", "trunk", "dev", "develop"]

async function findShaForBranches(
gitDir: string,
): Promise<[string, string] | null> {
for (let branch of BRANCHES) {
const sha = await git.getSHAForBranch(gitDir, branch)
if (sha == null) {
continue
}
return [sha, branch]
}
return null
}

interface IGithubinator {
openUrl?: boolean
copyToClipboard?: boolean
blame?: boolean
branch?: string
mainBranch?: boolean
permalink?: boolean
openRepo?: boolean
history?: boolean
Expand All @@ -130,7 +145,7 @@ async function githubinator({
openUrl,
copyToClipboard,
blame,
branch,
mainBranch,
openRepo,
permalink,
history,
Expand All @@ -148,12 +163,12 @@ async function githubinator({
return err("Could not find .git directory.")
}
let headBranch: [string, string | null] | null = null
if (branch) {
const sha = await git.getSHAForBranch(gitDir, branch)
if (sha == null) {
return err(`Could not find SHA for branch name: ${branch}`)
if (mainBranch) {
const res = await findShaForBranches(gitDir)
if (res == null) {
return err(`Could not find SHA for branch in ${BRANCHES}`)
}
headBranch = [sha, branch]
headBranch = res
} else {
headBranch = await git.head(gitDir)
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as path from "path"
import * as fs from "fs"
/** Get path of file relative to git root. */
export function getRelativeFilePath(gitDir: string, fileName: string): string {
const resolvedFileName = fs.realpathSync(fileName)
const gitProjectRoot = path.dirname(gitDir) + "/"
return fileName.replace(gitProjectRoot, "")
return resolvedFileName.replace(gitProjectRoot, "")
}

/** Convert url/hostname to hostname
Expand Down

0 comments on commit 3ff8e33

Please sign in to comment.