gitout

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

log.c (2726B)


      1 #include "writer/gopher/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/gopher/page.h"
     10 
     11 struct GopherLog {
     12   const GitRepo* repo;
     13   FILE* out;
     14   Cache* cache;
     15   GopherPage* 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 GopherLog* gopher_log_create(const GitRepo* repo) {
     23   GopherLog* log = ecalloc(1, sizeof(GopherLog));
     24   log->repo = repo;
     25   log->out = efopen("log.gph", "w");
     26   log->page = gopher_page_create(log->out, repo, "Log", "");
     27   log->remaining_commits = SIZE_MAX;
     28   log->unlogged_commits = 0;
     29   return log;
     30 }
     31 
     32 void gopher_log_free(GopherLog* 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   gopher_page_free(log->page);
     41   log->page = NULL;
     42   free(log);
     43 }
     44 
     45 void gopher_log_set_cachefile(GopherLog* log, const char* cachefile) {
     46   log->cache = cache_create(cachefile, write_commit_row);
     47 }
     48 
     49 void gopher_log_set_commit_limit(GopherLog* log, size_t count) {
     50   log->remaining_commits = count;
     51 }
     52 
     53 bool gopher_log_can_add_commits(const GopherLog* log) {
     54   return !log->cache || cache_can_add_commits(log->cache);
     55 }
     56 
     57 void gopher_log_begin(GopherLog* log) {
     58   FILE* out = log->out;
     59   gopher_page_begin(log->page);
     60   fprintf(out, "%-16.16s  ", "Date");
     61   fprintf(out, "%-40.40s  ", "Commit message");
     62   fprintf(out, "%s\n", "Author");
     63 }
     64 
     65 void gopher_log_add_commit(GopherLog* log, const GitCommit* commit) {
     66   if (log->cache) {
     67     cache_add_commit_row(log->cache, commit);
     68   } else if (log->remaining_commits > 0) {
     69     write_commit_row(log->out, commit);
     70     log->remaining_commits--;
     71   } else {
     72     log->unlogged_commits++;
     73   }
     74 }
     75 
     76 void gopher_log_end(GopherLog* log) {
     77   FILE* out = log->out;
     78   if (log->cache) {
     79     cache_write(log->cache);
     80     cache_copy_log(log->cache, log->out);
     81   } else if (log->unlogged_commits > 0) {
     82     size_t count = log->unlogged_commits;
     83     fprintf(out, "%16.16s  %zu more commits remaining, fetch the repository\n",
     84             "", count);
     85   }
     86   fprintf(out, "\n[0|Atom feed|atom.xml|server|port]\n");
     87   fprintf(out, "[0|Atom feed (tags)|tags.xml|server|port]\n");
     88   gopher_page_end(log->page);
     89 }
     90 
     91 void write_commit_row(FILE* out, const GitCommit* commit) {
     92   fprintf(out, "[1|");
     93   print_time_short(out, gitcommit_author_time(commit));
     94   fprintf(out, "  ");
     95   const char* summary = gitcommit_summary(commit);
     96   if (summary) {
     97     print_gopher_link_padded(out, summary, 40, ' ');
     98     fprintf(out, "  ");
     99   }
    100   print_gopher_link(out, gitcommit_author_name(commit));
    101   fprintf(out, "|commit/%s.gph|server|port]\n", gitcommit_oid(commit));
    102 }