Skip to content

Commit 81e8835

Browse files
authored
Fixed failure when running Githubinator with detached HEAD (#26)
Githubinator would fail because there wasn't a ref to work off when running with a detached HEAD.
1 parent 2891909 commit 81e8835

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
name: install
2323
command: |
2424
make install
25+
sudo apt update
2526
sudo apt install libgtk-3-0 libxss1 libnss3 libasound2
2627
- save_cache:
2728
paths:

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed failure when running Githubinator with detached HEAD. Githubinator
13+
would fail because there wasn't a ref to work off when running with a detached
14+
HEAD.
15+
1016
## 0.2.1 - 2019-03-08
1117

1218
### Fixed

src/extension.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async function githubinator({
136136
if (gitDir == null) {
137137
return err("Could not find .git directory.")
138138
}
139-
let headBranch: [string, string] | null = null
139+
let headBranch: [string, string | null] | null = null
140140
if (branch) {
141141
const sha = await git.getSHAForBranch(gitDir, branch)
142142
if (sha == null) {
@@ -166,8 +166,13 @@ async function githubinator({
166166
remote => git.origin(gitDir, remote),
167167
).getUrls({
168168
selection: [editor.selection.start.line, editor.selection.end.line],
169-
// permalink > branch > branch from HEAD
170-
head: !!permalink ? createSha(head) : createBranch(branchName),
169+
// priority: permalink > branch > branch from HEAD
170+
// If branchName could not be found (null) then we generate a permalink
171+
// using the SHA.
172+
head:
173+
!!permalink || branchName == null
174+
? createSha(head)
175+
: createBranch(branchName),
171176
relativeFilePath: getRelativeFilePath(gitDir, fileName),
172177
})
173178
if (parsedUrl != null) {

src/git.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export async function getSHAForBranch(
6464
}
6565

6666
/** Get the current SHA and branch from HEAD for a git directory */
67-
export async function head(gitDir: string): Promise<[string, string] | null> {
67+
export async function head(
68+
gitDir: string,
69+
): Promise<[string, string | null] | null> {
6870
const headPath = path.resolve(gitDir, "HEAD")
6971
if (!(await fs.exists(headPath))) {
7072
return null
@@ -73,8 +75,17 @@ export async function head(gitDir: string): Promise<[string, string] | null> {
7375
if (!headFileData) {
7476
return null
7577
}
76-
const headInfo = headFileData.split(" ")[1].trim()
77-
const branchName = headInfo.replace("refs/heads/", "")
78+
// If we're not on a branch, headFileData will be of the form:
79+
// `3c0cc80bbdb682f6e9f65b4c9659ca21924aad4`
80+
// If we're on a branch, it will be `ref: refs/heads/my_branch_name`
81+
const [maybeSha, maybeHeadInfo] = headFileData.split(" ") as [
82+
string,
83+
string | undefined
84+
]
85+
if (maybeHeadInfo == null) {
86+
return [maybeSha, null]
87+
}
88+
const branchName = maybeHeadInfo.trim().replace("refs/heads/", "")
7889
const sha = await getSHAForBranch(gitDir, branchName)
7990
if (sha == null) {
8091
return null

0 commit comments

Comments
 (0)