Skip to content

Commit 7b9306c

Browse files
committed
use git binary
1 parent a3f859b commit 7b9306c

2 files changed

Lines changed: 54 additions & 48 deletions

File tree

src/extension.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as git from "./git"
33
import { providers, IUrlInfo, createSha, createBranch } from "./providers"
44
import { getRelativeFilePath } from "./utils"
55
import { openFileFromGitHubUrl } from "./openfromUrl"
6-
import { Repository } from "./vscode-git"
76

87
const COMMANDS: [string, IGithubinator][] = [
98
[
@@ -137,7 +136,7 @@ function mainBranches() {
137136
* Search default main branch names for the first one that exists.
138137
*/
139138
async function findShaForBranches(
140-
gitRepository: Repository,
139+
gitRepository: git.Repo,
141140
fileUri: vscode.Uri,
142141
): Promise<[string, string] | null> {
143142
for (let branch of mainBranches()) {

src/git.ts

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,84 @@
11
import * as vscode from "vscode"
2+
import { execFile } from "child_process"
3+
import { promisify } from "util"
4+
import * as path from "path"
25
import { outputChannel } from "./extension"
3-
import { GitExtension, Remote, Repository } from "./vscode-git"
46

5-
async function getGitAPI() {
6-
const gitExtension =
7-
vscode.extensions.getExtension<GitExtension>("vscode.git")
8-
if (!gitExtension) return null
9-
const exports = await gitExtension.activate()
10-
return exports.getAPI(1)
7+
const execFileAsync = promisify(execFile)
8+
9+
export interface Repo {
10+
rootUri: vscode.Uri
11+
}
12+
13+
async function git(cwd: string, ...args: string[]): Promise<string> {
14+
const { stdout } = await execFileAsync("git", args, { cwd })
15+
return stdout.trim()
1116
}
1217

13-
export async function getRepo(fileUri: vscode.Uri) {
14-
const api = await getGitAPI()
15-
if (!api) {
16-
vscode.window.showErrorMessage("Git API not available")
18+
export async function getRepo(fileUri: vscode.Uri): Promise<Repo | null> {
19+
try {
20+
const stat = await vscode.workspace.fs.stat(fileUri)
21+
const cwd =
22+
stat.type & vscode.FileType.Directory
23+
? fileUri.fsPath
24+
: path.dirname(fileUri.fsPath)
25+
const root = await git(cwd, "rev-parse", "--show-toplevel")
26+
return { rootUri: vscode.Uri.file(root) }
27+
} catch {
28+
outputChannel.appendLine(
29+
"Could not find git repository for file: " + fileUri.fsPath,
30+
)
1731
return null
1832
}
19-
const existing = api.getRepository(fileUri)
20-
if (existing) return existing
21-
outputChannel.appendLine("No repository found, triggering git refresh...")
22-
await vscode.commands.executeCommand("git.refresh")
23-
const afterRefresh = api.getRepository(fileUri)
24-
if (afterRefresh) return afterRefresh
25-
// Repos are discovered asynchronously after activation; wait for one to open
26-
return new Promise<ReturnType<typeof api.getRepository>>((resolve) => {
27-
const timeout = setTimeout(() => {
28-
disposable.dispose()
29-
resolve(null)
30-
outputChannel.appendLine("Hit 5 second timeout for repository to load.")
31-
}, 5000)
32-
const disposable = api.onDidOpenRepository(() => {
33-
const repo = api.getRepository(fileUri)
34-
if (repo) {
35-
outputChannel.appendLine("Found repository via onDidOpenRepository.")
36-
clearTimeout(timeout)
37-
disposable.dispose()
38-
resolve(repo)
39-
}
40-
})
41-
})
4233
}
4334

4435
export async function origin(
45-
repository: Repository,
36+
repo: Repo,
4637
remote: string,
4738
): Promise<string | null> {
48-
const found = repository.state.remotes.find((r: Remote) => r.name === remote)
49-
return found?.fetchUrl ?? found?.pushUrl ?? null
39+
try {
40+
return await git(repo.rootUri.fsPath, "remote", "get-url", remote)
41+
} catch {
42+
return null
43+
}
5044
}
5145

5246
export async function getSHAForBranch(
53-
repository: Repository,
47+
repo: Repo,
5448
branchName: string,
5549
): Promise<string | null> {
5650
try {
57-
const branch = await repository.getBranch(branchName)
58-
return branch.commit ?? null
51+
return await git(repo.rootUri.fsPath, "rev-parse", branchName)
5952
} catch {
6053
return null
6154
}
6255
}
6356

6457
export async function head(
65-
repository: Repository,
58+
repo: Repo,
6659
): Promise<[string, string | null] | null> {
67-
const repoHead = repository.state.HEAD
68-
if (!repoHead?.commit) return null
69-
return [repoHead.commit, repoHead.name ?? null]
60+
try {
61+
const sha = await git(repo.rootUri.fsPath, "rev-parse", "HEAD")
62+
let branchName: string | null = null
63+
try {
64+
branchName = await git(
65+
repo.rootUri.fsPath,
66+
"symbolic-ref",
67+
"--short",
68+
"HEAD",
69+
)
70+
} catch {
71+
// detached HEAD, branchName stays null
72+
}
73+
return [sha, branchName]
74+
} catch {
75+
return null
76+
}
7077
}
7178

7279
export async function dir(
73-
repository: Repository,
80+
repo: Repo,
7481
): Promise<{ git: string; repository: string } | null> {
75-
const root = repository.rootUri.fsPath
82+
const root = repo.rootUri.fsPath
7683
return { git: root, repository: root }
7784
}

0 commit comments

Comments
 (0)