diff options
author | Aki <please@ignore.pl> | 2022-07-08 20:42:16 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-07-08 20:42:16 +0200 |
commit | 8310940d6803c4298170b2fabd763dc70236e7b0 (patch) | |
tree | 088cec63dd5495f1fd68d0c4b2feac09186f867a | |
parent | f49116131f10ac04bb7d5f51609b59305500c244 (diff) | |
download | pkgrel-8310940d6803c4298170b2fabd763dc70236e7b0.zip pkgrel-8310940d6803c4298170b2fabd763dc70236e7b0.tar.gz pkgrel-8310940d6803c4298170b2fabd763dc70236e7b0.tar.bz2 |
General clean-up, simple docs, code structure, constants
-rw-r--r-- | pkgrel.c | 37 |
1 files changed, 26 insertions, 11 deletions
@@ -15,13 +15,21 @@ enum match_result }; +static inline size_t minimum(size_t lhs, size_t rhs); +static char* relative_path_in_current_dir(git_repository* repo, const char* filename); +static int try_git(int err, const char* msg); +static int match_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_diff_line* line, void* payload); + + +/// Returns the smaller value from *lhs* and *rhs*. size_t -min(const size_t lhs, const size_t rhs) +minimum(const size_t lhs, const size_t rhs) { return lhs > rhs ? rhs : lhs; } +/// Finds the relative path of *filename* from the workdir of *repo*. char* relative_path_in_current_dir(git_repository* repo, const char* filename) { @@ -43,7 +51,7 @@ relative_path_in_current_dir(git_repository* repo, const char* filename) } } const char* workdir = git_repository_workdir(repo); - const size_t prefix = min(strlen(cwd), strlen(workdir)); + const size_t prefix = minimum(strlen(cwd), strlen(workdir)); if (0 != strncmp(cwd, workdir, prefix)) { free(cwd); return NULL; @@ -62,13 +70,15 @@ relative_path_in_current_dir(git_repository* repo, const char* filename) } +/// If *err* is considered erroneous in context of libgit2 return code, print a message describing it, and prefix it +/// with *msg* if it is not NULL. int -try_git(const int err, const char* optional_prefix) +try_git(const int err, const char* msg) { if (0 > err) { const git_error* e = git_error_last(); - if (NULL != optional_prefix) - dprintf(2, "%s: ", optional_prefix); + if (NULL != msg) + dprintf(2, "%s: ", msg); dprintf(2, "%s\n", e->message); exit(1); } @@ -76,16 +86,19 @@ try_git(const int err, const char* optional_prefix) } +/// Callback for git_diff_foreach. Check each line reported by the diff looking for "pkgver" occurrance. If found, set +/// *payload* to MATCH_RESULT_FOUND, otherwise set it to MATCH_RESULT_NOTFOUND, but only if it is not set to FOUND +/// already. int -line_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_diff_line* line, void* payload) +match_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_diff_line* line, void* payload) { - const char* const pkgver = "pkgver"; - const size_t len = strlen(pkgver); + static const char* const PKGVER = "pkgver"; + static const size_t PKGVER_LEN = 6; enum match_result* matched = (enum match_result*) payload; switch (line->origin) { case GIT_DIFF_LINE_ADDITION: case GIT_DIFF_LINE_DELETION: - if (len <= line->content_len && 0 == strncmp(line->content, pkgver, len)) + if (PKGVER_LEN <= line->content_len && 0 == strncmp(line->content, PKGVER, PKGVER_LEN)) *matched = MATCH_RESULT_FOUND; break; default: @@ -133,7 +146,7 @@ main(int argc, char* argv[]) else try_git(git_diff_tree_to_tree(&diff, repo, later, earlier, &diffopts), "diff"); enum match_result matched = MATCH_RESULT_INITIAL; - try_git(git_diff_foreach(diff, NULL, NULL, NULL, &line_line, &matched), "searching lines"); + try_git(git_diff_foreach(diff, NULL, NULL, NULL, &match_line, &matched), "searching lines"); git_diff_free(diff); git_commit_free(commit); if (MATCH_RESULT_INITIAL != matched) @@ -143,8 +156,10 @@ main(int argc, char* argv[]) if (MATCH_RESULT_NOTFOUND == matched) rel++; } - if (0 == occurred) + if (0 == occurred) { dprintf(2, "PKGBUILD not found\n"); + exit(1); + } else printf("%d\n", rel); } |