gitout

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

refs.c (3944B)


      1 #include "writer/gopher/refs.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 #include "format.h"
      7 #include "git/commit.h"
      8 #include "utils.h"
      9 #include "writer/gopher/page.h"
     10 
     11 typedef struct {
     12   char* title;
     13   char* id;
     14   FILE* out;
     15 } GopherRefsTable;
     16 
     17 struct GopherRefs {
     18   const GitRepo* repo;
     19   FILE* out;
     20   GopherPage* page;
     21   GopherRefsTable* branches;
     22   GopherRefsTable* tags;
     23 };
     24 
     25 static GopherRefsTable* gopher_refstable_create(const char* title,
     26                                                 const char* id,
     27                                                 FILE* out);
     28 static void gopher_refstable_free(GopherRefsTable* table);
     29 static void gopher_refstable_begin(GopherRefsTable* table);
     30 static void gopher_refstable_add_ref(GopherRefsTable* table,
     31                                      const GitReference* ref);
     32 static void gopher_refstable_end(GopherRefsTable* table);
     33 
     34 GopherRefsTable* gopher_refstable_create(const char* title,
     35                                          const char* id,
     36                                          FILE* out) {
     37   GopherRefsTable* table = ecalloc(1, sizeof(GopherRefsTable));
     38   table->title = estrdup(title);
     39   table->id = estrdup(id);
     40   table->out = out;
     41   return table;
     42 }
     43 
     44 void gopher_refstable_free(GopherRefsTable* table) {
     45   if (!table) {
     46     return;
     47   }
     48   free(table->title);
     49   table->title = NULL;
     50   free(table->id);
     51   table->id = NULL;
     52   free(table);
     53 }
     54 
     55 void gopher_refstable_begin(GopherRefsTable* table) {
     56   FILE* out = table->out;
     57   fprintf(out, "%s\n", table->title);
     58   fprintf(out, "  %-32.32s", "Name");
     59   fprintf(out, "  %-16.16s", "Last commit date");
     60   fprintf(out, "  %s\n", "Author");
     61 }
     62 
     63 void gopher_refstable_add_ref(GopherRefsTable* table, const GitReference* ref) {
     64   GitCommit* commit = gitreference_commit(ref);
     65   FILE* out = table->out;
     66 
     67   fprintf(out, "  ");
     68   print_gopher_link_padded(out, gitreference_shorthand(ref), 32, ' ');
     69   fprintf(out, "  ");
     70   print_time_short(out, gitcommit_author_time(commit));
     71   fprintf(out, "  ");
     72   print_gopher_link_padded(out, gitcommit_author_name(commit), 25, '\0');
     73   fprintf(out, "\n");
     74 }
     75 
     76 void gopher_refstable_end(GopherRefsTable* table) {
     77   FILE* out = table->out;
     78   fprintf(out, "\n");
     79 }
     80 
     81 GopherRefs* gopher_refs_create(const GitRepo* repo) {
     82   GopherRefs* refs = ecalloc(1, sizeof(GopherRefs));
     83   refs->repo = repo;
     84   refs->out = efopen("refs.gph", "w");
     85   refs->page = gopher_page_create(refs->out, repo, "Refs", "");
     86   return refs;
     87 }
     88 
     89 void gopher_refs_free(GopherRefs* refs) {
     90   if (!refs) {
     91     return;
     92   }
     93   fclose(refs->out);
     94   refs->out = NULL;
     95   gopher_page_free(refs->page);
     96   refs->page = NULL;
     97   gopher_refstable_free(refs->branches);
     98   refs->branches = NULL;
     99   gopher_refstable_free(refs->tags);
    100   refs->tags = NULL;
    101   free(refs);
    102 }
    103 
    104 void gopher_refs_begin(GopherRefs* refs) {
    105   gopher_page_begin(refs->page);
    106 }
    107 
    108 void gopher_refs_add_ref(GopherRefs* refs, const GitReference* ref) {
    109   switch (gitreference_type(ref)) {
    110     case kReftypeBranch:
    111       if (!refs->branches) {
    112         refs->branches =
    113             gopher_refstable_create("Branches", "branches", refs->out);
    114         gopher_refstable_begin(refs->branches);
    115       }
    116       gopher_refstable_add_ref(refs->branches, ref);
    117       break;
    118     case kReftypeTag:
    119       if (refs->branches) {
    120         gopher_refstable_end(refs->branches);
    121         gopher_refstable_free(refs->branches);
    122         refs->branches = NULL;
    123       }
    124       if (!refs->tags) {
    125         refs->tags = gopher_refstable_create("Tags", "tags", refs->out);
    126         gopher_refstable_begin(refs->tags);
    127       }
    128       gopher_refstable_add_ref(refs->tags, ref);
    129       break;
    130   }
    131 }
    132 
    133 void gopher_refs_end(GopherRefs* refs) {
    134   if (refs->branches) {
    135     gopher_refstable_end(refs->branches);
    136     gopher_refstable_free(refs->branches);
    137     refs->branches = NULL;
    138   }
    139   if (refs->tags) {
    140     gopher_refstable_end(refs->tags);
    141     gopher_refstable_free(refs->tags);
    142     refs->tags = NULL;
    143   }
    144   gopher_page_end(refs->page);
    145 }