Skip to content

Commit 7ef9dfa

Browse files
authored
Merge pull request #159 from lambdalisue/fix-worktree-support
Use repository root as the base path for worktree creation
2 parents 620f56a + ca9aca7 commit 7ef9dfa

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

denops/gin/action/worktree_new.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { Denops } from "jsr:@denops/std@^7.0.0";
2+
import * as fn from "jsr:@denops/std@^7.0.0/function";
23
import * as batch from "jsr:@denops/std@^7.0.0/batch";
34
import * as helper from "jsr:@denops/std@^7.0.0/helper";
5+
import { join } from "jsr:@std/path@^1.0.0/join";
6+
import { dirname } from "jsr:@std/path@^1.0.0/dirname";
47
import { define, GatherCandidates, Range } from "./core.ts";
8+
import { revParse } from "../git/finder.ts";
59

610
export type Candidate = { target?: string };
711

@@ -42,12 +46,14 @@ async function doNew(
4246
force: boolean,
4347
gatherCandidates: GatherCandidates<Candidate>,
4448
): Promise<void> {
49+
const cwd = await fn.getcwd(denops);
50+
const root = await findRoot(cwd);
4551
const xs = await gatherCandidates(denops, bufnr, range);
4652
const x = xs.at(0);
4753
const target = x?.target ?? "HEAD";
4854
const worktreePath = await helper.input(denops, {
4955
prompt: `Worktree path (for ${target}): `,
50-
text: `.worktrees/${target}`,
56+
text: join(root, `.worktrees/${target}`),
5157
});
5258
await denops.cmd('redraw | echo ""');
5359
if (!worktreePath) {
@@ -69,9 +75,11 @@ async function doNewOrphan(
6975
_range: Range,
7076
_gatherCandidates: GatherCandidates<unknown>,
7177
): Promise<void> {
78+
const cwd = await fn.getcwd(denops);
79+
const root = await findRoot(cwd);
7280
const worktreePath = await helper.input(denops, {
7381
prompt: "Worktree path (orphan): ",
74-
text: `.worktrees/orphan`,
82+
text: join(root, `.worktrees/orphan`),
7583
});
7684
await denops.cmd('redraw | echo ""');
7785
if (!worktreePath) {
@@ -85,3 +93,14 @@ async function doNewOrphan(
8593
worktreePath,
8694
]);
8795
}
96+
97+
async function findRoot(
98+
cwd: string,
99+
): Promise<string> {
100+
try {
101+
const gitdir = await revParse(cwd, ["--git-common-dir"]);
102+
return dirname(gitdir);
103+
} catch {
104+
return "";
105+
}
106+
}

denops/gin/git/finder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async function isWorktreeRoot(currentPath: string): Promise<boolean> {
104104
return false;
105105
}
106106

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

0 commit comments

Comments
 (0)