Skip to content

Commit 7979b76

Browse files
author
decobot
committed
feat: revert to a certain commit hash
1 parent e843611 commit 7979b76

File tree

1 file changed

+71
-20
lines changed

1 file changed

+71
-20
lines changed

Diff for: daemon/git.ts

+71-20
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ export const status: Handler = async (c) => {
144144
});
145145
};
146146

147+
const doBuild = (oid: string, build: Deno.Command | null) => {
148+
const buildMap = new Map<string, Promise<void>>();
149+
150+
const p = buildMap.get(oid) ||
151+
(async () => {
152+
try {
153+
await build?.spawn()?.status;
154+
await persist(oid);
155+
} catch (e) {
156+
console.error("Building failed with:", e);
157+
} finally {
158+
buildMap.delete(oid);
159+
}
160+
})();
161+
162+
buildMap.set(oid, p);
163+
164+
return p;
165+
};
147166
export interface PublishAPI {
148167
body: {
149168
message: string;
@@ -186,25 +205,6 @@ const persist = async (oid: string) => {
186205
// TODO: maybe tag with versions!
187206
// TODO: handle rebase conflicts
188207
export const publish = ({ build }: Options): Handler => {
189-
const buildMap = new Map<string, Promise<void>>();
190-
191-
const doBuild = (oid: string) => {
192-
const p = buildMap.get(oid) ||
193-
(async () => {
194-
try {
195-
await build?.spawn()?.status;
196-
await persist(oid);
197-
} catch (e) {
198-
console.error("Building failed with:", e);
199-
} finally {
200-
buildMap.delete(oid);
201-
}
202-
})();
203-
204-
buildMap.set(oid, p);
205-
206-
return p;
207-
};
208208

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

225225
// Runs build pipeline asynchronously
226-
doBuild(commit.commit);
226+
doBuild(commit.commit, build);
227227

228228
return Response.json(result);
229229
};
@@ -436,6 +436,56 @@ interface Options {
436436
site: string;
437437
}
438438

439+
export interface RevertAPI {
440+
body: {
441+
commitHash: string;
442+
message?: string;
443+
author?: {
444+
name?: string;
445+
email?: string;
446+
};
447+
};
448+
response: {
449+
status: StatusResult;
450+
commit: string;
451+
};
452+
}
453+
454+
export const revert = ({ build }: Options): Handler => {
455+
456+
return async (c) => {
457+
const { commitHash, message, author = { name: "decobot", email: "[email protected]" } } =
458+
await c.req.json() as RevertAPI["body"];
459+
460+
await git.fetch(["-p"]).submoduleUpdate(["--depth", "1"]);
461+
462+
await resetToMergeBase();
463+
464+
// Create revert commit
465+
await git.revert(commitHash, {
466+
"--no-commit": null, // Stage changes without committing
467+
});
468+
469+
// Commit the revert with custom message
470+
const commitMessage = message || `Revert "${commitHash}"`;
471+
const commit = await git.commit(commitMessage, {
472+
"--author": `${author.name} <${author.email}>`,
473+
"--no-verify": null,
474+
});
475+
476+
// Runs build pipeline asynchronously
477+
doBuild(commit.commit, build);
478+
479+
// Push the revert commit
480+
await git.push();
481+
482+
return Response.json({
483+
status: await git.status(),
484+
commit: commit.commit
485+
});
486+
};
487+
};
488+
439489
export const createGitAPIS = (options: Options) => {
440490
const app = new Hono();
441491

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

450501
return app;
451502
};

0 commit comments

Comments
 (0)