gout

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

log.c (2920B)


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