diff --git a/denops/gin/action/fixup.ts b/denops/gin/action/fixup.ts new file mode 100644 index 00000000..83174d4c --- /dev/null +++ b/denops/gin/action/fixup.ts @@ -0,0 +1,69 @@ +import type { Denops } from "https://deno.land/x/denops_std@v5.0.1/mod.ts"; +import * as batch from "https://deno.land/x/denops_std@v5.0.1/batch/mod.ts"; +import { alias, define, GatherCandidates, Range } from "./core.ts"; + +export type Candidate = { commit: string }; + +export async function init( + denops: Denops, + bufnr: number, + gatherCandidates: GatherCandidates, +): Promise { + await batch.batch(denops, async (denops) => { + await define( + denops, + bufnr, + "fixup:fixup", + (denops, bufnr, range) => doFixup(denops, bufnr, range, gatherCandidates), + ); + await define( + denops, + bufnr, + "fixup:amend", + (denops, bufnr, range) => + doFixupInteractive(denops, bufnr, range, "amend", gatherCandidates), + ); + await define( + denops, + bufnr, + "fixup:reword", + (denops, bufnr, range) => + doFixupInteractive(denops, bufnr, range, "reword", gatherCandidates), + ); + await alias( + denops, + bufnr, + "fixup", + "fixup:fixup", + ); + }); +} + +async function doFixup( + denops: Denops, + bufnr: number, + range: Range, + gatherCandidates: GatherCandidates, +): Promise { + const xs = await gatherCandidates(denops, bufnr, range); + const commit = xs.map((v) => v.commit).join("\n"); + await denops.dispatch("gin", "command", "", [ + "commit", + `--fixup=${commit}`, + ]); +} + +async function doFixupInteractive( + denops: Denops, + bufnr: number, + range: Range, + kind: "amend" | "reword", + gatherCandidates: GatherCandidates, +): Promise { + const xs = await gatherCandidates(denops, bufnr, range); + const commit = xs.map((v) => v.commit).join("\n"); + // Do not block Vim so that users can edit commit message + denops + .dispatch("gin", "command", "", ["commit", `--fixup=${kind}:${commit}`]) + .catch((e) => console.error(`failed to execute git commit: ${e}`)); +} diff --git a/denops/gin/command/log/edit.ts b/denops/gin/command/log/edit.ts index 5c1757e8..da592785 100644 --- a/denops/gin/command/log/edit.ts +++ b/denops/gin/command/log/edit.ts @@ -20,6 +20,7 @@ import { init as initActionBrowse } from "../../action/browse.ts"; import { init as initActionCherryPick } from "../../action/cherry_pick.ts"; import { init as initActionCore, Range } from "../../action/core.ts"; import { init as initActionEcho } from "../../action/echo.ts"; +import { init as initActionFixup } from "../../action/fixup.ts"; import { init as initActionMerge } from "../../action/merge.ts"; import { init as initActionRebase } from "../../action/rebase.ts"; import { init as initActionReset } from "../../action/reset.ts"; @@ -94,6 +95,10 @@ export async function exec( await initActionBrowse(denops, bufnr, gatherCandidates); await initActionCherryPick(denops, bufnr, gatherCandidates); await initActionEcho(denops, bufnr, gatherCandidates); + await initActionFixup(denops, bufnr, async (denops, bufnr, range) => { + const xs = await gatherCandidates(denops, bufnr, range); + return xs.map((x) => ({ commit: x.commit })); + }); await initActionMerge(denops, bufnr, gatherCandidates); await initActionRebase(denops, bufnr, gatherCandidates); await initActionReset(denops, bufnr, gatherCandidates);