gitout

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

repo_writer.c (4650B)


      1 #include "writer/gopher/repo_writer.h"
      2 
      3 #include <err.h>
      4 #include <limits.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <sys/stat.h>
      8 #include <sys/types.h>
      9 
     10 #include "git/commit.h"
     11 #include "git/file.h"
     12 #include "git/reference.h"
     13 #include "utils.h"
     14 #include "writer/atom/atom.h"
     15 #include "writer/gopher/commit.h"
     16 #include "writer/gopher/fileblob.h"
     17 #include "writer/gopher/files.h"
     18 #include "writer/gopher/log.h"
     19 #include "writer/gopher/refs.h"
     20 
     21 struct GopherRepoWriter {
     22   const GitRepo* repo;
     23   GopherRefs* refs;
     24   GopherLog* log;
     25   Atom* atom;
     26   Atom* tags;
     27   GopherFiles* files;
     28 };
     29 
     30 GopherRepoWriter* gopher_repowriter_create(const GitRepo* repo) {
     31   GopherRepoWriter* writer = ecalloc(1, sizeof(GopherRepoWriter));
     32   writer->repo = repo;
     33   writer->refs = gopher_refs_create(repo);
     34   writer->log = gopher_log_create(repo);
     35   writer->atom = atom_create(repo, kAtomTypeAll);
     36   writer->tags = atom_create(repo, kAtomTypeTags);
     37   writer->files = gopher_files_create(repo);
     38   return writer;
     39 }
     40 
     41 void gopher_repowriter_free(GopherRepoWriter* writer) {
     42   if (!writer) {
     43     return;
     44   }
     45   gopher_refs_free(writer->refs);
     46   writer->refs = NULL;
     47   gopher_log_free(writer->log);
     48   writer->log = NULL;
     49   atom_free(writer->atom);
     50   writer->atom = NULL;
     51   atom_free(writer->tags);
     52   writer->tags = NULL;
     53   gopher_files_free(writer->files);
     54   writer->files = NULL;
     55   free(writer);
     56 }
     57 
     58 void gopher_repowriter_set_log_cachefile(void* writer, const char* cachefile) {
     59   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
     60   gopher_log_set_cachefile(gopher_writer->log, cachefile);
     61 }
     62 
     63 void gopher_repowriter_set_log_commit_limit(void* writer, size_t count) {
     64   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
     65   gopher_log_set_commit_limit(gopher_writer->log, count);
     66 }
     67 
     68 void gopher_repowriter_set_baseurl(void* writer, const char* baseurl) {
     69   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
     70   atom_set_baseurl(gopher_writer->atom, baseurl);
     71   atom_set_baseurl(gopher_writer->tags, baseurl);
     72 }
     73 
     74 void gopher_repowriter_begin(void* writer) {
     75   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
     76   mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
     77   mkdir("file", S_IRWXU | S_IRWXG | S_IRWXO);
     78 
     79   gopher_refs_begin(gopher_writer->refs);
     80   gopher_log_begin(gopher_writer->log);
     81   atom_begin(gopher_writer->atom);
     82   atom_begin(gopher_writer->tags);
     83   gopher_files_begin(gopher_writer->files);
     84 }
     85 
     86 void gopher_repowriter_add_commit(void* writer, const GitCommit* git_commit) {
     87   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
     88   char filename[PATH_MAX];
     89   if (snprintf(filename, sizeof(filename), "%s.gph",
     90                gitcommit_oid(git_commit)) < 0) {
     91     err(1, "snprintf");
     92   }
     93   char path[PATH_MAX];
     94   path_concat(path, sizeof(path), "commit", filename);
     95   atom_add_commit(gopher_writer->atom, git_commit, path, "", "");
     96 
     97   if (gopher_log_can_add_commits(gopher_writer->log)) {
     98     gopher_log_add_commit(gopher_writer->log, git_commit);
     99     GopherCommit* commit =
    100         gopher_commit_create(gopher_writer->repo, gitcommit_oid(git_commit),
    101                              gitcommit_summary(git_commit));
    102     gopher_commit_begin(commit);
    103     gopher_commit_add_commit(commit, git_commit);
    104     gopher_commit_end(commit);
    105     gopher_commit_free(commit);
    106   }
    107 }
    108 
    109 void gopher_repowriter_add_reference(void* writer, const GitReference* ref) {
    110   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
    111   gopher_refs_add_ref(gopher_writer->refs, ref);
    112   if (gitreference_type(ref) == kReftypeTag) {
    113     GitCommit* commit = gitreference_commit(ref);
    114     char filename[PATH_MAX];
    115     if (snprintf(filename, sizeof(filename), "%s.gph", gitcommit_oid(commit)) <
    116         0) {
    117       err(1, "snprintf");
    118     }
    119     char path[PATH_MAX];
    120     path_concat(path, sizeof(path), "commit", filename);
    121     atom_add_commit(gopher_writer->tags, commit, path, "",
    122                     gitreference_shorthand(ref));
    123   }
    124 }
    125 
    126 void gopher_repowriter_add_file(void* writer, const GitFile* file) {
    127   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
    128   gopher_files_add_file(gopher_writer->files, file);
    129 
    130   GopherFileBlob* blob =
    131       gopher_fileblob_create(gopher_writer->repo, gitfile_repo_path(file));
    132   gopher_fileblob_begin(blob);
    133   gopher_fileblob_add_file(blob, file);
    134   gopher_fileblob_end(blob);
    135   gopher_fileblob_free(blob);
    136 }
    137 
    138 void gopher_repowriter_end(void* writer) {
    139   GopherRepoWriter* gopher_writer = (GopherRepoWriter*)writer;
    140   gopher_refs_end(gopher_writer->refs);
    141   gopher_log_end(gopher_writer->log);
    142   atom_end(gopher_writer->atom);
    143   atom_end(gopher_writer->tags);
    144   gopher_files_end(gopher_writer->files);
    145 }