Skip to content

feat: revert to a certain commit hash #889

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
91 changes: 71 additions & 20 deletions daemon/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ export const status: Handler = async (c) => {
});
};

const doBuild = (oid: string, build: Deno.Command | null) => {
const buildMap = new Map<string, Promise<void>>();

const p = buildMap.get(oid) ||
(async () => {
try {
await build?.spawn()?.status;
await persist(oid);
} catch (e) {
console.error("Building failed with:", e);
} finally {
buildMap.delete(oid);
}
})();

buildMap.set(oid, p);

return p;
};
export interface PublishAPI {
body: {
message: string;
Expand Down Expand Up @@ -186,25 +205,6 @@ const persist = async (oid: string) => {
// TODO: maybe tag with versions!
// TODO: handle rebase conflicts
export const publish = ({ build }: Options): Handler => {
const buildMap = new Map<string, Promise<void>>();

const doBuild = (oid: string) => {
const p = buildMap.get(oid) ||
(async () => {
try {
await build?.spawn()?.status;
await persist(oid);
} catch (e) {
console.error("Building failed with:", e);
} finally {
buildMap.delete(oid);
}
})();

buildMap.set(oid, p);

return p;
};

return async (c) => {
const body = (await c.req.json()) as PublishAPI["body"];
Expand All @@ -223,7 +223,7 @@ export const publish = ({ build }: Options): Handler => {
const result = await git.push();

// Runs build pipeline asynchronously
doBuild(commit.commit);
doBuild(commit.commit, build);

return Response.json(result);
};
Expand Down Expand Up @@ -436,6 +436,56 @@ interface Options {
site: string;
}

export interface RevertAPI {
body: {
commitHash: string;
message?: string;
author?: {
name?: string;
email?: string;
};
};
response: {
status: StatusResult;
commit: string;
};
}

export const revert = ({ build }: Options): Handler => {

return async (c) => {
const { commitHash, message, author = { name: "decobot", email: "[email protected]" } } =
await c.req.json() as RevertAPI["body"];

await git.fetch(["-p"]).submoduleUpdate(["--depth", "1"]);

await resetToMergeBase();

// Create revert commit
await git.revert(commitHash, {
"--no-commit": null, // Stage changes without committing
});

// Commit the revert with custom message
const commitMessage = message || `Revert "${commitHash}"`;
const commit = await git.commit(commitMessage, {
"--author": `${author.name} <${author.email}>`,
"--no-verify": null,
});

// Runs build pipeline asynchronously
doBuild(commit.commit, build);

// Push the revert commit
await git.push();

return Response.json({
status: await git.status(),
commit: commit.commit
});
};
};

export const createGitAPIS = (options: Options) => {
const app = new Hono();

Expand All @@ -446,6 +496,7 @@ export const createGitAPIS = (options: Options) => {
app.post("/publish", publish(options));
app.post("/discard", discard);
app.post("/rebase", rebase);
app.post("/revert", revert(options));

return app;
};
Loading