Skip to content

Use repository root as the base path for worktree creation #159

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

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
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
23 changes: 21 additions & 2 deletions denops/gin/action/worktree_new.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down Expand Up @@ -42,12 +46,14 @@ async function doNew(
force: boolean,
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
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) {
Expand All @@ -69,9 +75,11 @@ async function doNewOrphan(
_range: Range,
_gatherCandidates: GatherCandidates<unknown>,
): Promise<void> {
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) {
Expand All @@ -85,3 +93,14 @@ async function doNewOrphan(
worktreePath,
]);
}

async function findRoot(
cwd: string,
): Promise<string> {
try {
const gitdir = await revParse(cwd, ["--git-common-dir"]);
return dirname(gitdir);
} catch {
return "";
}
}
2 changes: 1 addition & 1 deletion denops/gin/git/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function isWorktreeRoot(currentPath: string): Promise<boolean> {
return false;
}

async function revParse(cwd: string, args: string[]): Promise<string> {
export async function revParse(cwd: string, args: string[]): Promise<string> {
const stdout = await execute(["rev-parse", ...args], { cwd });
const output = decodeUtf8(stdout);
return resolve(cwd, output.split(/\n/, 2)[0]);
Expand Down