Skip to content

Commit

Permalink
Merge pull request #114 from atusy/action-fixup
Browse files Browse the repository at this point in the history
👍 add fixup actions to gin-log (fixup, amend, reword)
  • Loading branch information
lambdalisue authored Dec 7, 2023
2 parents 59e0e9d + 7a04bb7 commit 5f27fb6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
69 changes: 69 additions & 0 deletions denops/gin/action/fixup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { Denops } from "https://deno.land/x/[email protected]/mod.ts";
import * as batch from "https://deno.land/x/[email protected]/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<Candidate>,
): Promise<void> {
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<Candidate>,
): Promise<void> {
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<Candidate>,
): Promise<void> {
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}`));
}
5 changes: 5 additions & 0 deletions denops/gin/command/log/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5f27fb6

Please sign in to comment.