Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 61 additions & 31 deletions packages/opencode/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ import { existsSync } from "fs"

export namespace Project {
const log = Log.create({ service: "project" })

// Helper function to run git commands with timeout to prevent hangs
async function gitWithTimeout<T>(
promise: Promise<T>,
timeoutMs: number = 5000,
fallback: T
): Promise<T> {
return Promise.race([
promise,
new Promise<T>((resolve) =>
setTimeout(() => {
log.warn("git command timed out, falling back to non-git behavior", { timeoutMs })
resolve(fallback)
}, timeoutMs)
)
])
}

export const Info = z
.object({
id: z.string(),
Expand Down Expand Up @@ -73,19 +91,23 @@ export namespace Project {

// generate id from root commit
if (!id) {
const roots = await $`git rev-list --max-parents=0 --all`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) =>
x
.split("\n")
.filter(Boolean)
.map((x) => x.trim())
.toSorted(),
)
.catch(() => undefined)
const roots = await gitWithTimeout(
$`git rev-list --max-parents=0 --all`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) =>
x
.split("\n")
.filter(Boolean)
.map((x) => x.trim())
.toSorted(),
)
.catch(() => undefined),
5000,
undefined
)

if (!roots) {
return {
Expand Down Expand Up @@ -113,13 +135,17 @@ export namespace Project {
}
}

const top = await $`git rev-parse --show-toplevel`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) => path.resolve(sandbox, x.trim()))
.catch(() => undefined)
const top = await gitWithTimeout(
$`git rev-parse --show-toplevel`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) => path.resolve(sandbox, x.trim()))
.catch(() => undefined),
5000,
undefined
)

if (!top) {
return {
Expand All @@ -132,17 +158,21 @@ export namespace Project {

sandbox = top

const worktree = await $`git rev-parse --git-common-dir`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) => {
const dirname = path.dirname(x.trim())
if (dirname === ".") return sandbox
return dirname
})
.catch(() => undefined)
const worktree = await gitWithTimeout(
$`git rev-parse --git-common-dir`
.quiet()
.nothrow()
.cwd(sandbox)
.text()
.then((x) => {
const dirname = path.dirname(x.trim())
if (dirname === ".") return sandbox
return dirname
})
.catch(() => undefined),
5000,
undefined
)

if (!worktree) {
return {
Expand Down