Skip to content

Commit 2ddde38

Browse files
authored
Refactor extension to call git binary (#75)
1 parent cb6efea commit 2ddde38

11 files changed

Lines changed: 147 additions & 207 deletions

File tree

.github/workflows/javascript.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout
14-
uses: actions/checkout@v3
14+
uses: actions/checkout@v6
1515
- name: Use Node.js
16-
uses: actions/setup-node@v3
16+
uses: actions/setup-node@v6
17+
with:
18+
node-version-file: ".nvmrc"
1719
- name: Install dependencies
1820
run: yarn install --frozen-lockfile
1921
- name: Run tests
@@ -22,9 +24,11 @@ jobs:
2224
runs-on: ubuntu-latest
2325
steps:
2426
- name: Checkout
25-
uses: actions/checkout@v3
27+
uses: actions/checkout@v6
2628
- name: Use Node.js
27-
uses: actions/setup-node@v3
29+
uses: actions/setup-node@v6
30+
with:
31+
node-version-file: ".nvmrc"
2832
- name: Install dependencies
2933
run: yarn install --frozen-lockfile
3034
- name: Run tests

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22.22.0

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+
## 4.0.0 - 2026-03-16
11+
12+
### Changed
13+
14+
- Extension now calls `git` instead of manually reading `.git/` files. (#75)
15+
1016
## 3.4.0 - 2026-03-07
1117

1218
### Changed

images/logo.sketch

999 Bytes
Binary file not shown.

package.json

Lines changed: 8 additions & 9 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": "3.4.0",
5+
"version": "4.0.0",
66
"publisher": "chdsbd",
77
"license": "SEE LICENSE IN LICENSE",
88
"icon": "images/logo256.png",
@@ -455,14 +455,14 @@
455455
"@types/lodash": "^4.14.170",
456456
"@types/mocha": "^10.0.6",
457457
"@types/mz": "^2.7.3",
458-
"@types/node": "^15.12.4",
459-
"@types/vscode": "^1.32.0",
458+
"@types/node": "^22.19.15",
459+
"@types/vscode": "^1.110.0",
460460
"@vscode/test-cli": "^0.0.9",
461461
"@vscode/test-electron": "^2.4.0",
462462
"esbuild": "^0.11.12",
463463
"mocha": "^10.4.0",
464464
"prettier": "^3.3.0",
465-
"typescript": "^4.2.2",
465+
"typescript": "^5.9.3",
466466
"vscode-test": "^1.6.1"
467467
},
468468
"dependencies": {
@@ -472,9 +472,8 @@
472472
"lodash": "^4.17.21",
473473
"mz": "^2.7.0"
474474
},
475-
"packageManager": "yarn@1.22.22",
476-
"volta": {
477-
"node": "18.20.3",
478-
"yarn": "1.22.22"
479-
}
475+
"extensionDependencies": [
476+
"vscode.git"
477+
],
478+
"packageManager": "yarn@1.22.22"
480479
}

src/extension.ts

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,35 @@ export interface IGithubinatorConfig {
7777
}
7878
}
7979

80+
export let outputChannel: vscode.LogOutputChannel
81+
8082
export function activate(context: vscode.ExtensionContext) {
81-
console.log("githubinator.active.start")
83+
outputChannel = vscode.window.createOutputChannel("GitHubinator", {
84+
log: true,
85+
})
86+
outputChannel.debug("githubinator.active.start")
87+
context.subscriptions.push(outputChannel)
8288
COMMANDS.forEach(([cmd, args]) => {
8389
const disposable = vscode.commands.registerCommand(cmd, () =>
8490
githubinator(args),
8591
)
8692
context.subscriptions.push(disposable)
8793
})
88-
vscode.commands.registerCommand(
94+
const openFromUrlDisposable = vscode.commands.registerCommand(
8995
"githubinator.githubinatorOpenFromUrl",
9096
openFileFromGitHubUrl,
9197
)
98+
context.subscriptions.push(openFromUrlDisposable)
9299

93-
console.log("githubinator.active.complete")
100+
outputChannel.debug("githubinator.active.complete")
94101
}
95102

96103
export function deactivate() {
97-
console.log("githubinator.deactivate")
104+
outputChannel.debug("githubinator.deactivate")
98105
}
99106

100107
function err(message: string) {
101-
console.error(message)
108+
outputChannel.error(message)
102109
vscode.window.showErrorMessage(message)
103110
}
104111

@@ -127,11 +134,15 @@ function mainBranches() {
127134
.get<string[]>("mainBranches", ["main"])
128135
}
129136

137+
/**
138+
* Search default main branch names for the first one that exists.
139+
*/
130140
async function findShaForBranches(
131-
gitDir: string,
141+
gitRepository: git.Repo,
142+
fileUri: vscode.Uri,
132143
): Promise<[string, string] | null> {
133144
for (let branch of mainBranches()) {
134-
const sha = await git.getSHAForBranch(gitDir, branch)
145+
const sha = await git.getSHAForBranch(gitRepository, branch)
135146
if (sha == null) {
136147
continue
137148
}
@@ -151,41 +162,41 @@ interface IGithubinator {
151162
openPR?: boolean
152163
compare?: boolean
153164
}
154-
async function githubinator({
155-
openUrl,
156-
copyToClipboard,
157-
blame,
158-
mainBranch,
159-
openRepo,
160-
permalink,
161-
history,
162-
openPR,
163-
compare,
164-
}: IGithubinator) {
165-
console.log("githubinator.call")
165+
async function githubinator(options: IGithubinator) {
166+
const {
167+
openUrl,
168+
copyToClipboard,
169+
blame,
170+
mainBranch,
171+
openRepo,
172+
permalink,
173+
history,
174+
openPR,
175+
compare,
176+
} = options
177+
outputChannel.info(
178+
"githubinator called with options: " + JSON.stringify(options),
179+
)
166180
const editorConfig = getEditorInfo()
167181
if (!editorConfig.uri) {
168-
return err("could not find file")
182+
return err("Could not find file for current editor.")
169183
}
184+
const fileUri = editorConfig.uri
170185

171-
const gitDirectories = git.dir(editorConfig.uri.fsPath)
172-
173-
if (gitDirectories == null) {
174-
return err("Could not find .git directory.")
186+
const gitRepository = await git.getRepo(fileUri)
187+
if (!gitRepository) {
188+
return err("Could not find git repository for file.")
175189
}
176190

177-
const gitDir = gitDirectories.git
178-
const repoDir = gitDirectories.repository
179-
180191
let headBranch: [string, string | null] | null = null
181192
if (mainBranch) {
182-
const res = await findShaForBranches(gitDir)
193+
const res = await findShaForBranches(gitRepository, fileUri)
183194
if (res == null) {
184195
return err(`Could not find SHA for branch in ${mainBranches()}`)
185196
}
186197
headBranch = res
187198
} else {
188-
headBranch = await git.head(gitDir)
199+
headBranch = await git.head(gitRepository)
189200
}
190201
if (headBranch == null) {
191202
return err("Could not find HEAD.")
@@ -209,7 +220,7 @@ async function githubinator({
209220
const parsedUrl = await new provider(
210221
providersConfig,
211222
globalDefaultRemote,
212-
(remote) => git.origin(gitDir, remote),
223+
(remote) => git.origin(gitRepository, remote),
213224
).getUrls({
214225
selection,
215226
// priority: permalink > branch > branch from HEAD
@@ -220,19 +231,19 @@ async function githubinator({
220231
? createSha(head)
221232
: createBranch(branchName),
222233
relativeFilePath: editorConfig.fileName
223-
? getRelativeFilePath(repoDir, editorConfig.fileName)
234+
? getRelativeFilePath(gitRepository.rootUri, editorConfig.fileName)
224235
: null,
225236
})
226237
if (parsedUrl != null) {
227-
console.log("Found provider", provider.name)
238+
outputChannel.debug("Found provider", provider.name)
228239
urls = parsedUrl
229240
break
230241
}
231-
console.log("Skipping provider", provider.name)
242+
outputChannel.debug("Skipping provider", provider.name)
232243
}
233244

234245
if (urls == null) {
235-
return err("Could not find provider for repo.")
246+
return err("Could not find remote for repository.")
236247
}
237248

238249
let url = compare

0 commit comments

Comments
 (0)