diff --git a/packages/core/src/plugin/vcs/git.ts b/packages/core/src/plugin/vcs/git.ts index 7ce2249a9a6..0b0d8d72144 100644 --- a/packages/core/src/plugin/vcs/git.ts +++ b/packages/core/src/plugin/vcs/git.ts @@ -263,19 +263,22 @@ function makeGit(proc: AppProcess.Interface, env?: Record) { }) const branches = Effect.fn("VcsGit.branches")(function* (cwd: string, options?: BranchOptions) { - const search = options?.search?.trim().replace(/[*?[\]\\]/g, "\\$&") + const search = options?.search?.trim().toLowerCase() + // `refname:short` collapses `refs/remotes//HEAD` to the bare remote name, so strip the namespace + // instead. Search and limit apply here because ref globs cannot cross `/` and `--count` precedes the filter. return (yield* lines( [ "for-each-ref", - "--ignore-case", "--sort=refname", "--sort=-committerdate", - "--format=%(refname:short)", - ...(options?.limit ? [`--count=${options.limit}`] : []), - ...(search ? [`refs/heads/*${search}*`, `refs/remotes/*${search}*`] : ["refs/heads", "refs/remotes"]), + "--format=%(refname:lstrip=2)", + "refs/heads", + "refs/remotes", ], { cwd }, - )).filter((item) => !item.endsWith("/HEAD")) satisfies BranchList + )) + .filter((item) => !item.endsWith("/HEAD") && (!search || item.toLowerCase().includes(search))) + .slice(0, options?.limit) satisfies BranchList }) const defaultBranch = Effect.fn("VcsGit.defaultBranch")(function* (cwd: string) { @@ -292,7 +295,7 @@ function makeGit(proc: AppProcess.Interface, env?: Record) { } } - const list = yield* lines(["for-each-ref", "--format=%(refname:short)", "refs/heads"], { cwd }) + const list = yield* lines(["for-each-ref", "--format=%(refname:lstrip=2)", "refs/heads"], { cwd }) const next = yield* configured(cwd, list) if (next) return next if (list.includes("main")) return { name: "main", ref: "main" } diff --git a/packages/core/test/vcs.test.ts b/packages/core/test/vcs.test.ts index 8ade788b876..c349c621660 100644 --- a/packages/core/test/vcs.test.ts +++ b/packages/core/test/vcs.test.ts @@ -552,6 +552,29 @@ describe("Vcs", () => { ), ) + it.live("lists remote branches without remote HEAD symrefs", () => + withGit((directory) => + Effect.gen(function* () { + yield* Effect.promise(async () => { + await fs.writeFile(path.join(directory, "file.txt"), "one\n") + await commitAll(directory, "initial") + await $`git tag main`.cwd(directory).quiet() + await $`git remote add origin ${directory}`.cwd(directory).quiet() + await $`git remote add fork ${directory}`.cwd(directory).quiet() + await $`git fetch --quiet origin`.cwd(directory).quiet() + await $`git fetch --quiet fork`.cwd(directory).quiet() + await $`git remote set-head origin main`.cwd(directory).quiet() + await $`git remote set-head fork main`.cwd(directory).quiet() + }) + const vcs = yield* Vcs.Service + expect(yield* vcs.branches()).toEqual(["main", "fork/main", "origin/main"]) + expect(yield* vcs.branches({ limit: 2 })).toEqual(["main", "fork/main"]) + expect(yield* vcs.branches({ search: "origin" })).toEqual(["origin/main"]) + expect(yield* vcs.branches({ search: "MAIN", limit: 2 })).toEqual(["main", "fork/main"]) + }), + ), + ) + it.live("reports modified, deleted, and untracked files", () => withGit((directory) => Effect.gen(function* () {