diff options
author | EuAndreh <eu@euandre.org> | 2021-08-21 07:39:33 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-08-21 07:39:33 -0300 |
commit | 7024585f3791a5362ac6dcbbe14f6b28b7eb146f (patch) | |
tree | dc57ab694d0adc16eda33de7cb1ae0bb99cc28c2 /src/gistatic.c | |
parent | tests/build-sample.sh: Also build $PWD (diff) | |
download | gistatic-7024585f3791a5362ac6dcbbe14f6b28b7eb146f.tar.gz gistatic-7024585f3791a5362ac6dcbbe14f6b28b7eb146f.tar.xz |
src/gistatic.c: create functions for loops; encode refs.html page dynamic content
Diffstat (limited to '')
-rw-r--r-- | src/gistatic.c | 361 |
1 files changed, 196 insertions, 165 deletions
diff --git a/src/gistatic.c b/src/gistatic.c index 9c82f1f..f3411f9 100644 --- a/src/gistatic.c +++ b/src/gistatic.c @@ -1437,6 +1437,196 @@ cleanup: return ret; } +static int repo_refs_branch_each( + FILE *const refs_fd, + struct git_repository *const repo, + struct git_reference *const ref +) { + int ret = 0; + struct git_commit *commit = NULL; + char *encoded_name = NULL; + char *encoded_summary = NULL; + char *encoded_author = NULL; + char *date = NULL; + int e; + + const char *const name = git_reference_shorthand(ref); + assert(name); + const struct git_oid *const oid = git_reference_target(ref); + assert(oid); + + if (git_commit_lookup(&commit, repo, oid)) { + const git_error *const error = git_error_last(); + assert(error); + logerrs("git_commit_lookup(", name, ")", error->message, + __LINE__); + ret = -1; + goto cleanup; + } + + const char *const sha = git_oid_tostr_s(oid); + assert(sha); + const char *const summary = git_commit_summary(commit); + assert(summary); + const struct git_signature *const author = git_commit_author(commit); + assert(author); + + if ( + !(encoded_name = escape_html(name)) + || !(encoded_summary = escape_html(summary)) + || !(encoded_author = escape_html(author->name)) + || !(date = formatted_date(author->when.time)) + ) { + ret = -1; + goto cleanup; + } + + e = fprintf( + refs_fd, + "" + " <tr>\n" + " <td>\n" + " <a href=\"log/%s.html\">\n" + " %s\n" + " </a>\n" + " </td>\n" + " <td>\n" + " <a href=\"commit/%s.html\">\n" + " %s\n" + " </a>\n" + " </td>\n" + " <td>\n" + " %s\n" + " </td>\n" + " <td>\n" + " %s\n" + " </td>\n" + " </tr>\n" + "", + encoded_name, + encoded_name, + sha, + encoded_summary, + encoded_author, + date + ); + if (e < 0) { + logerr("fprintf()", strerror(errno), __LINE__); + ret = -1; + goto cleanup; + } + +cleanup: + free(date); + free(encoded_author); + free(encoded_summary); + free(encoded_name); + git_commit_free(commit); + return ret; +} + +static int repo_refs_tag_each( + FILE *const refs_fd, + struct git_repository *const repo, + struct git_reference *const ref, + const char *const project_name +) { + int ret = 0; + struct git_commit *commit = NULL; + char *encoded_name = NULL; + char *encoded_summary = NULL; + char *encoded_author = NULL; + char *date = NULL; + int e; + + if (!git_reference_is_tag(ref)) { + goto cleanup; + } + + const char *const name = git_reference_shorthand(ref); + assert(name); + const struct git_oid *const oid = git_reference_target(ref); + assert(oid); + + if (git_commit_lookup(&commit, repo, oid)) { + const git_error *const error = git_error_last(); + assert(error); + logerrs("git_commit_lookup(", name, ")", error->message, __LINE__); + ret = -1; + goto cleanup; + } + + const char *const sha = git_oid_tostr_s(oid); + assert(sha); + const char *const summary = git_commit_summary(commit); + assert(summary); + const struct git_signature *const author = git_commit_author(commit); + assert(author); + + if ( + !(encoded_name = escape_html(name)) + || !(encoded_summary = escape_html(summary)) + || !(encoded_author = escape_html(author->name)) + || !(date = formatted_date(author->when.time)) + ) { + ret = -1; + goto cleanup; + } + + e = fprintf( + refs_fd, + "" + " <tr>\n" + " <td>\n" + " <a href=\"tag/%s.html\">\n" + " %s\n" + " </a>\n" + " </td>\n" + " <td>\n" + " <a href=\"commit/%s.html\">\n" + " %s\n" + " </a>\n" + " </td>\n" + " <td>\n" + " <a href=\"tarballs/%s-%s.tar.xz\">%s-%s.tar.xz</a>\n" + " (<a href=\"tarballs/%s-%s.tar.xz.asc\">sig</a>)\n" + " </td>\n" + " <td>\n" + " %s\n" + " </td>\n" + " <td>\n" + " %s\n" + " </td>\n" + " </tr>\n" + "", + encoded_name, + encoded_name, + sha, + encoded_summary, + project_name, + encoded_name, + project_name, + encoded_name, + project_name, + encoded_name, + encoded_author, + date + ); + if (e < 0) { + logerr("fprintf()", strerror(errno), __LINE__); + ret = -1; + goto cleanup; + } + +cleanup: + free(date); + free(encoded_author); + free(encoded_summary); + free(encoded_name); + git_commit_free(commit); + return ret; +} + static int repo_refs_write( const char *const outdir, struct git_repository *const repo, @@ -1586,82 +1776,11 @@ static int repo_refs_write( struct git_reference *ref; git_branch_t _btype; while (!(e = git_branch_next(&ref, &_btype, branch_iter))) { - const char *const name = git_reference_shorthand(ref); - assert(name); - - const struct git_oid *const oid = git_reference_target(ref); - if (!oid) { - logerrs("git_reference_target(\"", name, "\")", - _(MSG_ERR_NONDIRECT_REF), __LINE__); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - struct git_commit *commit; - if (git_commit_lookup(&commit, repo, oid)) { - const git_error *const error = git_error_last(); - assert(error); - logerr("git_commit_lookup()", error->message, __LINE__); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - - const char *const sha = git_oid_tostr_s(oid); - assert(sha); - - const char *const summary = git_commit_summary(commit); - assert(summary); - const struct git_signature *const author = - git_commit_author(commit); - assert(author); - char *const date = formatted_date(author->when.time); - if (!date) { - git_commit_free(commit); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - - e = fprintf( - refs_fd, - "" - " <tr>\n" - " <td>\n" - " <a href=\"log/%s.html\">\n" - " %s\n" - " </a>\n" - " </td>\n" - " <td>\n" - " <a href=\"commit/%s.html\">\n" - " %s\n" - " </a>\n" - " </td>\n" - " <td>\n" - " %s\n" - " </td>\n" - " <td>\n" - " %s\n" - " </td>\n" - " </tr>\n" - "", - name, - name, - sha, - summary, - author->name, - date - ); - if (e < 0) { - logerr("fprintf()", strerror(errno), __LINE__); - free(date); - git_reference_free(ref); - ret = -1; + e = repo_refs_branch_each(refs_fd, repo, ref); + git_reference_free(ref); + if (e) { goto cleanup; } - free(date); - git_commit_free(commit); - git_reference_free(ref); } if (e != GIT_ITEROVER) { const git_error *const error = git_error_last(); @@ -1710,96 +1829,11 @@ static int repo_refs_write( } while (!(e = git_reference_next(&ref, ref_iter))) { - if (!git_reference_is_tag(ref)) { - git_reference_free(ref); - continue; - } - const char *const name = git_reference_shorthand(ref); - assert(name); - - const struct git_oid *const oid = git_reference_target(ref); - if (!oid) { - logerrs("git_reference_target(\"", name, "\")", - _(MSG_ERR_NONDIRECT_REF), __LINE__); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - struct git_commit *commit; - if (git_commit_lookup(&commit, repo, oid)) { - const git_error *const error = git_error_last(); - assert(error); - logerr("git_commit_lookup()", error->message, __LINE__); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - - const char *const sha = git_oid_tostr_s(oid); - assert(sha); - - const char *const summary = git_commit_summary(commit); - assert(summary); - const struct git_signature *const author = - git_commit_author(commit); - assert(author); - char *const date = formatted_date(author->when.time); - if (!date) { - git_commit_free(commit); - git_reference_free(ref); - ret = -1; - goto cleanup; - } - - e = fprintf( - refs_fd, - "" - " <tr>\n" - " <td>\n" - " <a href=\"tag/%s.html\">\n" - " %s\n" - " </a>\n" - " </td>\n" - " <td>\n" - " <a href=\"commit/%s.html\">\n" - " %s\n" - " </a>\n" - " </td>\n" - " <td>\n" - " <a href=\"tarballs/%s-%s.tar.xz\">%s-%s.tar.xz</a>\n" - " (<a href=\"tarballs/%s-%s.tar.xz.asc\">sig</a>)\n" - " </td>\n" - " <td>\n" - " %s\n" - " </td>\n" - " <td>\n" - " %s\n" - " </td>\n" - " </tr>\n" - "", - name, - name, - sha, - summary, - project_name, - name, - project_name, - name, - project_name, - name, - author->name, - date - ); - if (e < 0) { - logerr("fprintf()", strerror(errno), __LINE__); - free(date); - git_reference_free(ref); - ret = -1; + e = repo_refs_tag_each(refs_fd, repo, ref, project_name); + git_reference_free(ref); + if (e) { goto cleanup; } - free(date); - git_commit_free(commit); - git_reference_free(ref); } if (e != GIT_ITEROVER) { const git_error *const error = git_error_last(); @@ -1831,7 +1865,6 @@ static int repo_refs_write( goto cleanup; } - cleanup: git_branch_iterator_free(branch_iter); git_reference_iterator_free(ref_iter); @@ -1852,9 +1885,7 @@ static int repo_tarballs_write( ) { int ret = 0; int e; - (void)outdir; (void)repo; - (void)project_name; char *tarballs_dir = NULL; struct git_reference_iterator *ref_iter = NULL; |