Skip to content
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

👍 add fixup actions to gin-log (fixup, amend, reword) #114

Merged
merged 2 commits into from
Dec 7, 2023
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
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/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<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}`));
}
Comment on lines +56 to +69
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doFixupInteractive function correctly gathers commit candidates and dispatches a Git command for interactive operations like amend and reword. However, the error handling within the promise chain could be improved by ensuring that the error is not just logged to the console but also handled in a way that the user is informed or the operation is retried.

- .catch((e) => console.error(`failed to execute git commit: ${e}`));
+ .catch((e) => {
+   // Handle the error more effectively, possibly by informing the user or retrying the operation
+   console.error(`failed to execute git commit: ${e}`);
+   // Additional error handling logic here
+ });

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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}`));
}
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) => {
// Handle the error more effectively, possibly by informing the user or retrying the operation
console.error(`failed to execute git commit: ${e}`);
// Additional error handling logic here
});
}

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