gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | Submodules | README | LICENSE

log.c (3271B)


      1 #include "writer/html/log.h"
      2 
      3 #include <stdint.h>
      4 #include <stdlib.h>
      5 
      6 #include "format.h"
      7 #include "utils.h"
      8 #include "writer/cache/cache.h"
      9 #include "writer/html/page.h"
     10 
     11 struct HtmlLog {
     12   const GitRepo* repo;
     13   FILE* out;
     14   Cache* cache;
     15   HtmlPage* page;
     16   size_t remaining_commits;
     17   size_t unlogged_commits;
     18 };
     19 
     20 static void write_commit_row(FILE* out, const GitCommit* commit);
     21 
     22 HtmlLog* html_log_create(const GitRepo* repo) {
     23   HtmlLog* log = ecalloc(1, sizeof(HtmlLog));
     24   log->repo = repo;
     25   log->out = efopen("log.html", "w");
     26   log->page = html_page_create(log->out, repo, "Log", "");
     27   log->remaining_commits = SIZE_MAX;
     28   log->unlogged_commits = 0;
     29   return log;
     30 }
     31 
     32 void html_log_free(HtmlLog* log) {
     33   if (!log) {
     34     return;
     35   }
     36   fclose(log->out);
     37   log->out = NULL;
     38   cache_free(log->cache);
     39   log->cache = NULL;
     40   html_page_free(log->page);
     41   log->page = NULL;
     42   free(log);
     43 }
     44 
     45 void html_log_set_cachefile(HtmlLog* log, const char* cachefile) {
     46   log->cache = cache_create(cachefile, write_commit_row);
     47 }
     48 
     49 void html_log_set_commit_limit(HtmlLog* log, size_t count) {
     50   log->remaining_commits = count;
     51 }
     52 
     53 bool html_log_can_add_commits(const HtmlLog* log) {
     54   return !log->cache || cache_can_add_commits(log->cache);
     55 }
     56 
     57 void html_log_begin(HtmlLog* log) {
     58   html_page_begin(log->page);
     59   fprintf(log->out,
     60           "<table id=\"log\"><thead>\n"
     61           "<tr>"
     62           "<td><b>Date</b></td>"
     63           "<td><b>Commit message</b></td>"
     64           "<td><b>Author</b></td>"
     65           "<td class=\"num\" align=\"right\"><b>Files</b></td>"
     66           "<td class=\"num\" align=\"right\"><b>+</b></td>"
     67           "<td class=\"num\" align=\"right\"><b>-</b></td>"
     68           "</tr>\n"
     69           "</thead><tbody>\n");
     70 }
     71 
     72 void html_log_add_commit(HtmlLog* log, const GitCommit* commit) {
     73   if (log->cache) {
     74     cache_add_commit_row(log->cache, commit);
     75   } else if (log->remaining_commits > 0) {
     76     write_commit_row(log->out, commit);
     77     log->remaining_commits--;
     78   } else {
     79     log->unlogged_commits++;
     80   }
     81 }
     82 
     83 void html_log_end(HtmlLog* log) {
     84   FILE* out = log->out;
     85   if (log->cache) {
     86     cache_write(log->cache);
     87     cache_copy_log(log->cache, log->out);
     88   } else if (log->unlogged_commits > 0) {
     89     size_t count = log->unlogged_commits;
     90     fprintf(out, "<tr><td></td><td colspan=\"5\">");
     91     fprintf(out, "%zu more commits remaining, fetch the repository", count);
     92     fprintf(out, "</td></tr>\n");
     93   }
     94   fprintf(out, "</tbody></table>");
     95   html_page_end(log->page);
     96 }
     97 
     98 void write_commit_row(FILE* out, const GitCommit* commit) {
     99   fprintf(out, "<tr><td>");
    100   print_time_short(out, gitcommit_author_time(commit));
    101   fprintf(out, "</td><td>");
    102   const char* summary = gitcommit_summary(commit);
    103   if (summary) {
    104     fprintf(out, "<a href=\"commit/%s.html\">", gitcommit_oid(commit));
    105     print_xml_encoded(out, summary);
    106     fprintf(out, "</a>");
    107   }
    108   fprintf(out, "</td><td>");
    109   print_xml_encoded(out, gitcommit_author_name(commit));
    110   fprintf(out,
    111           "</td>"
    112           "<td class=\"num\" align=\"right\">%zu</td>"
    113           "<td class=\"num\" align=\"right\">+%zu</td>"
    114           "<td class=\"num\" align=\"right\">-%zu</td></tr>\n",
    115           gitcommit_filecount(commit), gitcommit_addcount(commit),
    116           gitcommit_delcount(commit));
    117 }