From 2502c5d513d81fbafce63f81ea4c42ab880838fd Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 7 Jul 2022 23:41:42 +0200 Subject: Moved lg2 error handling into a try-or-die function --- pkgrel.c | 74 +++++++++++++++++++++------------------------------------------- 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/pkgrel.c b/pkgrel.c index 5c1d348..d3b2749 100644 --- a/pkgrel.c +++ b/pkgrel.c @@ -62,11 +62,17 @@ relative_path_in_current_dir(git_repository* repo, const char* filename) } -void -gerror(void) +int +try_git(const int err, const char* optional_prefix) { - const git_error* err = git_error_last(); - dprintf(2, "%s\n", err->message); + if (0 > err) { + const git_error* e = git_error_last(); + if (NULL != optional_prefix) + dprintf(2, "%s: ", optional_prefix); + dprintf(2, "%s\n", e->message); + exit(1); + } + return err; } @@ -96,61 +102,37 @@ main(int argc, char* argv[]) { git_libgit2_init(); git_repository* repo = NULL; - int err = git_repository_open_ext(&repo, NULL, GIT_REPOSITORY_OPEN_FROM_ENV, NULL); - if (0 > err) { - gerror(); - return 1; - } + try_git(git_repository_open_ext(&repo, NULL, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), "opening repo"); git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; - diffopts.pathspec.strings = (char*[]){relative_path_in_current_dir(repo, "PKGBUILD")}; + diffopts.pathspec.strings = (char*[1]){relative_path_in_current_dir(repo, "PKGBUILD")}; diffopts.pathspec.count = 1; - git_pathspec* pathspec = NULL; - err = git_pathspec_new(&pathspec, &diffopts.pathspec); - if (0 > err) { - gerror(); - return 1; + if (NULL == diffopts.pathspec.strings[0]) { + dprintf(2, "finding PKGBUILD in repo\n"); + exit(1); } + git_pathspec* pathspec = NULL; + try_git(git_pathspec_new(&pathspec, &diffopts.pathspec), "creating pathspec"); git_revwalk* walker = NULL; - err = git_revwalk_new(&walker, repo); - if (0 > err) { - gerror(); - return 1; - } - err = git_revwalk_push_head(walker); - if (0 > err) { - gerror(); - return 1; - } + try_git(git_revwalk_new(&walker, repo), "creating revwalker"); + try_git(git_revwalk_push_head(walker), "finding head"); git_oid oid; git_tree* earlier = NULL; git_tree* later = NULL; int rel = 1; while (0 == git_revwalk_next(&oid, walker)) { git_commit* commit; - err = git_commit_lookup(&commit, repo, &oid); - if (0 > err) { - gerror(); - return 1; - } + try_git(git_commit_lookup(&commit, repo, &oid), "finding commit"); if (NULL != earlier) git_tree_free(earlier); earlier = later; - err = git_commit_tree(&later, commit); - if (0 > err) { - gerror(); - return 1; - } + try_git(git_commit_tree(&later, commit), "finding tree"); git_diff* diff; if (NULL == earlier) - err = git_diff_tree_to_workdir_with_index(&diff, repo, later, &diffopts); + try_git(git_diff_tree_to_workdir_with_index(&diff, repo, later, &diffopts), "diff"); else - err = git_diff_tree_to_tree(&diff, repo, later, earlier, &diffopts); - if (0 > err) { - gerror(); - return 1; - } + try_git(git_diff_tree_to_tree(&diff, repo, later, earlier, &diffopts), "diff"); enum match_result matched = MATCH_RESULT_INITIAL; - git_diff_foreach(diff, NULL, NULL, NULL, &line_line, &matched); + try_git(git_diff_foreach(diff, NULL, NULL, NULL, &line_line, &matched), "searching lines"); git_diff_free(diff); git_commit_free(commit); if (MATCH_RESULT_FOUND == matched) @@ -158,13 +140,5 @@ main(int argc, char* argv[]) if (MATCH_RESULT_INITIAL != matched) rel++; } - if (NULL != earlier) - git_tree_free(earlier); - if (NULL != later) - git_tree_free(later); - git_revwalk_free(walker); - git_pathspec_free(pathspec); - free(diffopts.pathspec.strings[0]); - git_repository_free(repo); printf("%d\n", rel); } -- cgit v1.1