diff --git a/denops/gin/action/worktree_new.ts b/denops/gin/action/worktree_new.ts index ecb58e0c..f7c6e6ad 100644 --- a/denops/gin/action/worktree_new.ts +++ b/denops/gin/action/worktree_new.ts @@ -1,7 +1,11 @@ import type { Denops } from "jsr:@denops/std@^7.0.0"; +import * as fn from "jsr:@denops/std@^7.0.0/function"; import * as batch from "jsr:@denops/std@^7.0.0/batch"; import * as helper from "jsr:@denops/std@^7.0.0/helper"; +import { join } from "jsr:@std/path@^1.0.0/join"; +import { dirname } from "jsr:@std/path@^1.0.0/dirname"; import { define, GatherCandidates, Range } from "./core.ts"; +import { revParse } from "../git/finder.ts"; export type Candidate = { target?: string }; @@ -42,12 +46,14 @@ async function doNew( force: boolean, gatherCandidates: GatherCandidates, ): Promise { + const cwd = await fn.getcwd(denops); + const root = await findRoot(cwd); const xs = await gatherCandidates(denops, bufnr, range); const x = xs.at(0); const target = x?.target ?? "HEAD"; const worktreePath = await helper.input(denops, { prompt: `Worktree path (for ${target}): `, - text: `.worktrees/${target}`, + text: join(root, `.worktrees/${target}`), }); await denops.cmd('redraw | echo ""'); if (!worktreePath) { @@ -69,9 +75,11 @@ async function doNewOrphan( _range: Range, _gatherCandidates: GatherCandidates, ): Promise { + const cwd = await fn.getcwd(denops); + const root = await findRoot(cwd); const worktreePath = await helper.input(denops, { prompt: "Worktree path (orphan): ", - text: `.worktrees/orphan`, + text: join(root, `.worktrees/orphan`), }); await denops.cmd('redraw | echo ""'); if (!worktreePath) { @@ -85,3 +93,14 @@ async function doNewOrphan( worktreePath, ]); } + +async function findRoot( + cwd: string, +): Promise { + try { + const gitdir = await revParse(cwd, ["--git-common-dir"]); + return dirname(gitdir); + } catch { + return ""; + } +} diff --git a/denops/gin/git/finder.ts b/denops/gin/git/finder.ts index d3b833ae..72edb243 100644 --- a/denops/gin/git/finder.ts +++ b/denops/gin/git/finder.ts @@ -104,7 +104,7 @@ async function isWorktreeRoot(currentPath: string): Promise { return false; } -async function revParse(cwd: string, args: string[]): Promise { +export async function revParse(cwd: string, args: string[]): Promise { const stdout = await execute(["rev-parse", ...args], { cwd }); const output = decodeUtf8(stdout); return resolve(cwd, output.split(/\n/, 2)[0]);