forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(core): worktree-based workspace switching with stash-based warp #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import type { Argv } from "yargs" | ||
| import { EOL } from "os" | ||
| import { Effect } from "effect" | ||
| import { cmd } from "./cmd" | ||
| import { effectCmd, fail } from "../effect-cmd" | ||
| import { Worktree } from "@/worktree" | ||
| import { UI } from "../ui" | ||
| import { GlobalBus, type GlobalEvent } from "@/bus/global" | ||
|
|
||
| function formatTable(entries: (Omit<Worktree.Info, "branch"> & { branch?: string })[]): string { | ||
| const maxName = Math.max(4, ...entries.map((e) => e.name.length)) | ||
| const maxBranch = Math.max(6, ...entries.map((e) => (e.branch ?? "-").length)) | ||
| const fmt = ` ${"Name".padEnd(maxName)} ${"Branch".padEnd(maxBranch)} Directory` | ||
| const sep = " " + "─".repeat(maxName) + " " + "─".repeat(maxBranch) + " " + "─".repeat(80) | ||
| const rows = entries.map( | ||
| (e) => ` ${e.name.padEnd(maxName)} ${(e.branch ?? "-").padEnd(maxBranch)} ${e.directory}`, | ||
| ) | ||
| return [fmt, sep, ...rows].join(EOL) | ||
| } | ||
|
|
||
| export const WorktreeCommand = cmd({ | ||
| command: "worktree", | ||
| describe: "manage git worktrees", | ||
| builder: (yargs: Argv) => | ||
| yargs | ||
| .command(WorktreeCreateCommand) | ||
| .command(WorktreeListCommand) | ||
| .command(WorktreeRemoveCommand) | ||
| .command(WorktreeResetCommand) | ||
| .demandCommand(), | ||
| async handler() {}, | ||
| }) | ||
|
|
||
| const waitWorktreeEvent = (directory: string) => | ||
| Effect.callback<"ready" | "failed", never>((resume) => { | ||
| const handler = (event: GlobalEvent) => { | ||
| if (event.directory !== directory) return | ||
| if (event.payload.type === Worktree.Event.Ready.type) { | ||
| GlobalBus.off("event", handler) | ||
| resume(Effect.succeed("ready" as const)) | ||
| } | ||
| if (event.payload.type === Worktree.Event.Failed.type) { | ||
| GlobalBus.off("event", handler) | ||
| resume(Effect.succeed("failed" as const)) | ||
| } | ||
| } | ||
|
|
||
| GlobalBus.on("event", handler) | ||
| return Effect.sync(() => GlobalBus.off("event", handler)) | ||
| }) | ||
|
|
||
| export const WorktreeCreateCommand = effectCmd({ | ||
| command: "create", | ||
| describe: "create a worktree", | ||
| builder: (yargs) => | ||
| yargs | ||
| .option("name", { | ||
| describe: "worktree name (slug; generated if omitted)", | ||
| type: "string", | ||
| }) | ||
| .option("start-command", { | ||
| describe: "additional startup script", | ||
| type: "string", | ||
| }), | ||
| handler: Effect.fn("Cli.worktree.create")(function* (args) { | ||
| const svc = yield* Worktree.Service | ||
| const info = yield* svc.create({ name: args.name, startCommand: args.startCommand }).pipe( | ||
| Effect.catch((e) => fail(e.message)), | ||
| ) | ||
|
|
||
| UI.println(UI.Style.TEXT_DIM + "Worktree directory: " + UI.Style.TEXT_NORMAL + info.directory) | ||
| if (info.branch) { | ||
| UI.println(UI.Style.TEXT_DIM + "Branch: " + UI.Style.TEXT_NORMAL + info.branch) | ||
| } | ||
| UI.empty() | ||
|
|
||
| const result = yield* waitWorktreeEvent(info.directory).pipe( | ||
| Effect.timeoutOrElse({ | ||
| duration: "120 seconds", | ||
| orElse: () => Effect.succeed("timeout" as const), | ||
| }), | ||
| ) | ||
|
|
||
| if (result === "timeout") { | ||
| UI.println(UI.Style.TEXT_WARNING_BOLD + "Worktree created but bootstrap still running" + UI.Style.TEXT_NORMAL) | ||
| return | ||
| } | ||
|
|
||
| if (result === "failed") { | ||
| return yield* fail("Worktree bootstrap failed") | ||
| } | ||
|
|
||
| UI.println(UI.Style.TEXT_SUCCESS_BOLD + "Worktree ready" + UI.Style.TEXT_NORMAL) | ||
| }), | ||
| }) | ||
|
|
||
| export const WorktreeListCommand = effectCmd({ | ||
| command: "list", | ||
| describe: "list worktrees", | ||
| builder: (yargs) => | ||
| yargs.option("format", { | ||
| describe: "output format", | ||
| type: "string", | ||
| choices: ["table", "json"], | ||
| default: "table", | ||
| }), | ||
| handler: Effect.fn("Cli.worktree.list")(function* (args) { | ||
| const svc = yield* Worktree.Service | ||
| const list = yield* svc.list().pipe( | ||
| Effect.catch((e) => fail(e.message)), | ||
| ) | ||
| if (list.length === 0) return | ||
| if (args.format === "json") { | ||
| console.log(JSON.stringify(list, null, 2)) | ||
| } else { | ||
| console.log(formatTable(list)) | ||
| } | ||
| }), | ||
| }) | ||
|
|
||
| const resolveWorktreeName = Effect.fnUntraced(function* (svc: Worktree.Interface, name: string) { | ||
| const list = yield* svc.list().pipe(Effect.catch((e) => fail(e.message))) | ||
| const match = list.find((w) => w.name === name) | ||
| if (!match) return name | ||
| return match.directory | ||
| }) | ||
|
|
||
| export const WorktreeRemoveCommand = effectCmd({ | ||
| command: "remove <name>", | ||
| describe: "remove a worktree", | ||
| builder: (yargs) => | ||
| yargs.positional("name", { | ||
| describe: "worktree name or directory", | ||
| type: "string", | ||
| demandOption: true, | ||
| }), | ||
| handler: Effect.fn("Cli.worktree.remove")(function* (args) { | ||
| const svc = yield* Worktree.Service | ||
| const directory = yield* resolveWorktreeName(svc, args.name) | ||
| yield* svc.remove({ directory }).pipe( | ||
| Effect.catch((e) => fail(e.message)), | ||
| ) | ||
| UI.println(UI.Style.TEXT_SUCCESS_BOLD + "Worktree removed" + UI.Style.TEXT_NORMAL) | ||
| }), | ||
| }) | ||
|
|
||
| export const WorktreeResetCommand = effectCmd({ | ||
| command: "reset <name>", | ||
| describe: "reset a worktree", | ||
| builder: (yargs) => | ||
| yargs.positional("name", { | ||
| describe: "worktree name or directory", | ||
| type: "string", | ||
| demandOption: true, | ||
| }), | ||
| handler: Effect.fn("Cli.worktree.reset")(function* (args) { | ||
| const svc = yield* Worktree.Service | ||
| const directory = yield* resolveWorktreeName(svc, args.name) | ||
| yield* svc.reset({ directory }).pipe( | ||
| Effect.catch((e) => fail(e.message)), | ||
| ) | ||
| UI.println(UI.Style.TEXT_SUCCESS_BOLD + "Worktree reset" + UI.Style.TEXT_NORMAL) | ||
| }), | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
stashPopsubstring match can pop the wrong stashline.includes(input.message)is a substring search acrossgit stash listoutput. If multiple stash entries contain the same substring (e.g., message "fix" matches "fix" and "fix-bug"),findreturns the first match — which may not be the stash created by the correspondingstashcall. Popping the wrong stash overwrites the working tree with unintended content, risking data loss.Use an exact message match or a more precise delimiter-based extraction to avoid ambiguity.
🛡️ Proposed fix: match full stash message after last colon
stashPop: Effect.fn("Vcs.stashPop")(function* (input: StashInput) { const ctx = yield* InstanceState.context if (ctx.project.vcs !== "git") return false const list = yield* git.run(["stash", "list"], { cwd: ctx.directory }) - const ref = list - .text() - .split(/\r?\n/) - .find((line) => line.includes(input.message)) - ?.split(":")[0] + const entry = list + .text() + .split(/\r?\n/) + .find((line) => { + // git stash list format: stash@{N}: WIP on branch: hash: message + // Extract message as everything after the 3rd colon (or 2nd for non-WIP) + const parts = line.split(": ") + const message = parts.slice(2).join(": ") + return message === input.message + }) + const ref = entry?.split(":")[0] if (!ref) return false yield* git.run(["stash", "pop", ref], { cwd: ctx.directory }) return true }),📝 Committable suggestion
🤖 Prompt for AI Agents