From 8aa0cafbd30f1d2df149e32b22b581a5d86afdeb Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 14:11:56 -0700 Subject: Look up a request's commit in one place The diff page now names a revision that is no commit the way the request wrote it, as the other pages already did, rather than as the full id it resolved to. The helpers sit beside the error page in ui-shared.c, since shared.c prints nothing. --- source/ui-shared.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source/ui-shared.c') diff --git a/source/ui-shared.c b/source/ui-shared.c index 5f5dcca..b34861d 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -1499,6 +1499,33 @@ void cgit_print_blob_too_large(size_t size) size / 1024, ctx.cfg.max_blob_size); } +struct commit *cgit_get_commit(const char *rev, struct object_id *oid) +{ + struct commit *commit; + + if (repo_get_oid(the_repository, rev, oid)) { + cgit_print_error_page(404, "Not Found", "Bad object id: %s", rev); + return NULL; + } + // lookup_commit_reference parses the commit, and fails on one git + // cannot parse. + commit = lookup_commit_reference(the_repository, oid); + if (!commit) + cgit_print_error_page(404, "Not Found", "Not a commit: %s", rev); + return commit; +} + +int cgit_get_old_oid(const char *old_rev, const struct commit *commit, struct object_id *oid) +{ + if (old_rev) + return cgit_get_commit(old_rev, oid) ? 0 : -1; + if (commit->parents && commit->parents->item) + oidcpy(oid, &commit->parents->item->object.oid); + else + oidclr(oid, the_repository->hash_algo); + return 0; +} + static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix) { struct strbuf **url_list = strbuf_split_str(urls, ' ', 0); -- cgit