enable parcel file watcher, expand parcel ignore patterns, replace fs watcher for git branches with parcel (#4805)

This commit is contained in:
Aiden Cline
2025-11-26 15:33:43 -08:00
committed by GitHub
parent 3ff0eb3065
commit 99d7ff47c4
5 changed files with 77 additions and 62 deletions

View File

@@ -12,6 +12,7 @@ export namespace Project {
.object({
id: z.string(),
worktree: z.string(),
vcsDir: z.string().optional(),
vcs: z.literal("git").optional(),
time: z.object({
created: z.number(),
@@ -80,9 +81,16 @@ export namespace Project {
.cwd(worktree)
.text()
.then((x) => x.trim())
const vcsDir = await $`git rev-parse --path-format=absolute --git-dir`
.quiet()
.nothrow()
.cwd(worktree)
.text()
.then((x) => x.trim())
const project: Info = {
id,
worktree,
vcsDir,
vcs: "git",
time: {
created: Date.now(),

View File

@@ -1,10 +1,10 @@
import { $ } from "bun"
import { watch, type FSWatcher } from "fs"
import path from "path"
import z from "zod"
import { Log } from "@/util/log"
import { Bus } from "@/bus"
import { Instance } from "./instance"
import { FileWatcher } from "@/file/watcher"
const log = Log.create({ service: "vcs" })
@@ -39,49 +39,31 @@ export namespace Vcs {
const state = Instance.state(
async () => {
if (Instance.project.vcs !== "git") {
return { branch: async () => undefined, watcher: undefined }
const vcsDir = Instance.project.vcsDir
if (Instance.project.vcs !== "git" || !vcsDir) {
return { branch: async () => undefined, unsubscribe: undefined }
}
let current = await currentBranch()
log.info("initialized", { branch: current })
const gitDir = await $`git rev-parse --git-dir`
.quiet()
.nothrow()
.cwd(Instance.worktree)
.text()
.then((x) => x.trim())
.catch(() => undefined)
if (!gitDir) {
log.warn("failed to resolve git directory")
return { branch: async () => current, watcher: undefined }
}
const gitHead = path.join(gitDir, "HEAD")
let watcher: FSWatcher | undefined
// we should probably centralize file watching (see watcher.ts)
// but parcel still marked experimental rn
try {
watcher = watch(gitHead, async () => {
const next = await currentBranch()
if (next !== current) {
log.info("branch changed", { from: current, to: next })
current = next
Bus.publish(Event.BranchUpdated, { branch: next })
}
})
log.info("watching", { path: gitHead })
} catch (e) {
log.warn("failed to watch git HEAD", { error: e })
}
const head = path.join(vcsDir, "HEAD")
const unsubscribe = Bus.subscribe(FileWatcher.Event.Updated, async (evt) => {
if (evt.properties.file !== head) return
const next = await currentBranch()
if (next !== current) {
log.info("branch changed", { from: current, to: next })
current = next
Bus.publish(Event.BranchUpdated, { branch: next })
}
})
return {
branch: async () => current,
watcher,
unsubscribe,
}
},
async (state) => {
state.watcher?.close()
state.unsubscribe?.()
},
)