gout

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

log.c (3179B)


      1 #include "writer/gopher/log.h"
      2 
      3 #include <assert.h>
      4 #include <err.h>
      5 #include <stdint.h>
      6 #include <stdlib.h>
      7 
      8 #include "format.h"
      9 #include "utils.h"
     10 #include "writer/cache/cache.h"
     11 #include "writer/gopher/page.h"
     12 
     13 #include <sys/stat.h>
     14 #include <unistd.h>
     15 
     16 struct GopherLog {
     17   const GitRepo* repo;
     18   const FileSystem* fs;
     19   FILE* out;
     20   Cache* cache;
     21   GopherPage* page;
     22   size_t remaining_commits;
     23   size_t unlogged_commits;
     24 };
     25 
     26 static void write_commit_row(FILE* out, const GitCommit* commit);
     27 
     28 GopherLog* gopher_log_create(const GitRepo* repo, const FileSystem* fs) {
     29   assert(repo != NULL);
     30   assert(fs != NULL);
     31   GopherLog* log = ecalloc(1, sizeof(GopherLog));
     32   log->repo = repo;
     33   log->fs = fs;
     34   log->out = fs->fopen("log.gph", "w");
     35   if (!log->out) {
     36     err(1, "fopen: log.gph");
     37   }
     38   log->page = gopher_page_create(log->out, repo, fs, "Log", "");
     39   log->remaining_commits = SIZE_MAX;
     40   log->unlogged_commits = 0;
     41   return log;
     42 }
     43 
     44 void gopher_log_free(GopherLog* log) {
     45   if (!log) {
     46     return;
     47   }
     48   log->fs->fclose(log->out);
     49   log->out = NULL;
     50   cache_free(log->cache);
     51   log->cache = NULL;
     52   gopher_page_free(log->page);
     53   log->page = NULL;
     54   free(log);
     55 }
     56 
     57 void gopher_log_set_cachefile(GopherLog* log, const char* cachefile) {
     58   assert(log != NULL);
     59   assert(cachefile != NULL);
     60   log->cache = cache_open(log->fs, cachefile, write_commit_row);
     61 }
     62 
     63 void gopher_log_set_commit_limit(GopherLog* log, size_t count) {
     64   assert(log != NULL);
     65   log->remaining_commits = count;
     66 }
     67 
     68 bool gopher_log_can_add_commits(const GopherLog* log) {
     69   assert(log != NULL);
     70   return !log->cache || cache_can_add_commits(log->cache);
     71 }
     72 
     73 void gopher_log_begin(GopherLog* log) {
     74   assert(log != NULL);
     75   FILE* out = log->out;
     76   gopher_page_begin(log->page);
     77   fprintf(out, "%-16.16s  ", "Date");
     78   fprintf(out, "%-40.40s  ", "Commit message");
     79   fprintf(out, "%s\n", "Author");
     80 }
     81 
     82 void gopher_log_add_commit(GopherLog* log, const GitCommit* commit) {
     83   assert(log != NULL);
     84   assert(commit != NULL);
     85   if (log->cache) {
     86     cache_add_commit_row(log->cache, commit);
     87   } else if (log->remaining_commits > 0) {
     88     write_commit_row(log->out, commit);
     89     log->remaining_commits--;
     90   } else {
     91     log->unlogged_commits++;
     92   }
     93 }
     94 
     95 void gopher_log_end(GopherLog* log) {
     96   assert(log != NULL);
     97   FILE* out = log->out;
     98   if (log->cache) {
     99     cache_close_and_replace(log->cache, log->out);
    100   } else if (log->unlogged_commits > 0) {
    101     size_t count = log->unlogged_commits;
    102     fprintf(out, "%16.16s  %zu more commits remaining, fetch the repository\n",
    103             "", count);
    104   }
    105   fprintf(out, "\n[0|Atom feed|atom.xml|server|port]\n");
    106   fprintf(out, "[0|Atom feed (tags)|tags.xml|server|port]\n");
    107   gopher_page_end(log->page);
    108 }
    109 
    110 static void write_commit_row(FILE* out, const GitCommit* commit) {
    111   assert(out != NULL);
    112   assert(commit != NULL);
    113   fprintf(out, "[1|");
    114   print_time_short(out, commit->author_time);
    115   fprintf(out, "  ");
    116   const char* summary = commit->summary;
    117   if (summary) {
    118     print_gopher_link_padded(out, summary, 40, ' ');
    119     fprintf(out, "  ");
    120   }
    121   print_gopher_link(out, commit->author_name);
    122   fprintf(out, "|commit/%s.gph|server|port]\n", commit->oid);
    123 }