Skip to content

Add git worktree support actions and :x variants to rebase actions #158

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 2 commits 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
20 changes: 14 additions & 6 deletions denops/gin/action/branch_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { define, GatherCandidates, Range } from "./core.ts";

export type Candidate =
| { kind: "remote"; branch: string; remote: string }
| { kind?: "alias" | "local"; branch: string };
| { kind?: "alias" | "local"; branch: string; worktree?: string };

export async function init(
denops: Denops,
Expand Down Expand Up @@ -50,11 +50,19 @@ async function doDelete(
]);
break;
default:
await denops.dispatch("gin", "command", "", [
"branch",
force ? "-D" : "-d",
x.branch,
]);
if (x.worktree) {
await denops.dispatch("gin", "command", "", [
"worktree",
"remove",
x.worktree,
]);
} else {
await denops.dispatch("gin", "command", "", [
"branch",
force ? "-D" : "-d",
x.branch,
]);
}
break;
}
}
Expand Down
54 changes: 37 additions & 17 deletions denops/gin/action/branch_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as batch from "jsr:@denops/std@^7.0.0/batch";
import * as helper from "jsr:@denops/std@^7.0.0/helper";
import { define, GatherCandidates, Range } from "./core.ts";

export type Candidate = { branch: string };
export type Candidate = { branch: string; worktree?: string };

export async function init(
denops: Denops,
Expand Down Expand Up @@ -40,21 +40,41 @@ async function doMove(
if (!x) {
return;
}
const from = x.branch;
const name = await helper.input(denops, {
prompt: `Rename (from ${from}): `,
text: from,
});
await denops.cmd('redraw | echo ""');
if (!name) {
await helper.echoerr(denops, "Cancelled");
return;
if (x.worktree) {
const from = x.worktree;
const newPath = await helper.input(denops, {
prompt: `New path (from ${from}): `,
text: from,
});
await denops.cmd('redraw | echo ""');
if (!newPath) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", [
"worktree",
"move",
...(force ? ["--force"] : []),
from,
newPath,
]);
} else {
const from = x.branch;
const newName = await helper.input(denops, {
prompt: `Rename (from ${from}): `,
text: from,
});
await denops.cmd('redraw | echo ""');
if (!newName) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", [
"branch",
...(force ? ["--force"] : []),
"--move",
from,
newName,
]);
}
await denops.dispatch("gin", "command", "", [
"branch",
...(force ? ["--force"] : []),
"--move",
from,
name,
]);
}
16 changes: 10 additions & 6 deletions denops/gin/action/branch_new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ async function doNew(
const xs = await gatherCandidates(denops, bufnr, range);
const x = xs.at(0);
const from = x?.target ?? "HEAD";
const name = await helper.input(denops, {
const branchName = await helper.input(denops, {
prompt: `New branch (from ${from}): `,
text: from,
});
await denops.cmd('redraw | echo ""');
if (!name) {
if (!branchName) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", [
"switch",
force ? "-C" : "-c",
name,
branchName,
from,
]);
}
Expand All @@ -68,13 +68,17 @@ async function doNewOrphan(
_range: Range,
_gatherCandidates: GatherCandidates<unknown>,
): Promise<void> {
const name = await helper.input(denops, {
const branchName = await helper.input(denops, {
prompt: "New branch (orphan): ",
});
await denops.cmd('redraw | echo ""');
if (!name) {
if (!branchName) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", ["switch", "--orphan", name]);
await denops.dispatch("gin", "command", "", [
"switch",
"--orphan",
branchName,
]);
}
55 changes: 46 additions & 9 deletions denops/gin/action/rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ export async function init(
(denops, bufnr, range) =>
doRebase(denops, bufnr, range, gatherCandidates),
);
await define(
denops,
bufnr,
"rebase:x",
(denops, bufnr, range) =>
doRebase(denops, bufnr, range, gatherCandidates, true),
);
await define(
denops,
bufnr,
"rebase:i",
(denops, bufnr, range) =>
doRebaseInteractive(denops, bufnr, range, gatherCandidates),
);
await define(
denops,
bufnr,
"rebase:i:x",
(denops, bufnr, range) =>
doRebaseInteractive(denops, bufnr, range, gatherCandidates, true),
);
await define(
denops,
bufnr,
Expand All @@ -40,16 +54,26 @@ async function doRebase(
bufnr: number,
range: Range,
gatherCandidates: GatherCandidates<Candidate>,
execute?: boolean,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
const x = xs.at(0);
if (!x) {
return;
}
await denops.dispatch("gin", "command", "", [
"rebase",
x.commit,
]);
const args = ["rebase", x.commit];
if (execute) {
const cmd = await helper.input(denops, {
prompt: "Execute command after rebase: ",
}) ?? "";
await denops.cmd('redraw | echo ""');
if (!cmd) {
await helper.echoerr(denops, "Cancelled");
return;
}
args.push("-x", cmd);
}
await denops.dispatch("gin", "command", "", args);

// suppress false-positive detection of file changes
await denops.cmd("silent checktime");
Expand All @@ -60,20 +84,33 @@ async function doRebaseInteractive(
bufnr: number,
range: Range,
gatherCandidates: GatherCandidates<Candidate>,
execute?: boolean,
): Promise<void> {
const xs = await gatherCandidates(denops, bufnr, range);
const x = xs.at(0);
if (!x) {
return;
}
// NOTE:
// We must NOT await the command otherwise Vim would freeze
// because command proxy could not work if we await here.
denops.dispatch("gin", "command", "", [
const args = [
"rebase",
"--interactive",
x.commit,
]).catch(async (e) => {
];
if (execute) {
const cmd = await helper.input(denops, {
prompt: "Execute command after rebase: ",
}) ?? "";
await denops.cmd('redraw | echo ""');
if (!cmd) {
await helper.echoerr(denops, "Cancelled");
return;
}
args.push("-x", cmd);
}
// NOTE:
// We must NOT await the command otherwise Vim would freeze
// because command proxy could not work if we await here.
denops.dispatch("gin", "command", "", args).catch(async (e) => {
await helper.echoerr(denops, e.toString());
}).then(
// suppress false-positive detection of file changes
Expand Down
87 changes: 87 additions & 0 deletions denops/gin/action/worktree_new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { Denops } from "jsr:@denops/std@^7.0.0";
import * as batch from "jsr:@denops/std@^7.0.0/batch";
import * as helper from "jsr:@denops/std@^7.0.0/helper";
import { define, GatherCandidates, Range } from "./core.ts";

export type Candidate = { target?: string };

export async function init(
denops: Denops,
bufnr: number,
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
await batch.batch(denops, async (denops) => {
await define(
denops,
bufnr,
"worktree",
(denops, bufnr, range) =>
doNew(denops, bufnr, range, false, gatherCandidates),
);
await define(
denops,
bufnr,
"worktree:force",
(denops, bufnr, range) =>
doNew(denops, bufnr, range, true, gatherCandidates),
);
await define(
denops,
bufnr,
"worktree:orphan",
(denops, bufnr, range) =>
doNewOrphan(denops, bufnr, range, gatherCandidates),
);
});
}

async function doNew(
denops: Denops,
bufnr: number,
range: Range,
force: boolean,
gatherCandidates: GatherCandidates<Candidate>,
): Promise<void> {
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}`,
});
await denops.cmd('redraw | echo ""');
if (!worktreePath) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", [
"worktree",
"add",
...(force ? ["-f"] : []),
worktreePath,
target,
]);
}

async function doNewOrphan(
denops: Denops,
_bufnr: number,
_range: Range,
_gatherCandidates: GatherCandidates<unknown>,
): Promise<void> {
const worktreePath = await helper.input(denops, {
prompt: "Worktree path (orphan): ",
text: `.worktrees/orphan`,
});
await denops.cmd('redraw | echo ""');
if (!worktreePath) {
await helper.echoerr(denops, "Cancelled");
return;
}
await denops.dispatch("gin", "command", "", [
"worktree",
"add",
"--orphan",
worktreePath,
]);
}
2 changes: 2 additions & 0 deletions denops/gin/command/branch/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { init as initActionMerge } from "../../action/merge.ts";
import { init as initActionRebase } from "../../action/rebase.ts";
import { init as initActionSwitch } from "../../action/switch.ts";
import { init as initActionYank } from "../../action/yank.ts";
import { init as initActionWorktreeNew } from "../../action/worktree_new.ts";
import { Branch, parse as parseBranch } from "./parser.ts";

export async function edit(
Expand Down Expand Up @@ -71,6 +72,7 @@ export async function exec(
await initActionBranchDelete(denops, bufnr, gatherCandidates);
await initActionBranchMove(denops, bufnr, gatherCandidates);
await initActionBranchNew(denops, bufnr, gatherCandidates);
await initActionWorktreeNew(denops, bufnr, gatherCandidates);
await initActionBrowse(denops, bufnr, async (denops, bufnr, range) => {
const xs = await gatherCandidates(denops, bufnr, range);
return xs.map((b) => ({ commit: b.target, ...b }));
Expand Down