gitout

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

repo_writer.c (4468B)


      1 #include "writer/html/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/html/commit.h"
     16 #include "writer/html/fileblob.h"
     17 #include "writer/html/files.h"
     18 #include "writer/html/log.h"
     19 #include "writer/html/refs.h"
     20 
     21 struct HtmlRepoWriter {
     22   const GitRepo* repo;
     23   HtmlRefs* refs;
     24   HtmlLog* log;
     25   Atom* atom;
     26   Atom* tags;
     27   HtmlFiles* files;
     28 };
     29 
     30 HtmlRepoWriter* html_repowriter_create(const GitRepo* repo) {
     31   HtmlRepoWriter* writer = ecalloc(1, sizeof(HtmlRepoWriter));
     32   writer->repo = repo;
     33   writer->refs = html_refs_create(repo);
     34   writer->log = html_log_create(repo);
     35   writer->atom = atom_create(repo, kAtomTypeAll);
     36   writer->tags = atom_create(repo, kAtomTypeTags);
     37   writer->files = html_files_create(repo);
     38   return writer;
     39 }
     40 
     41 void html_repowriter_free(HtmlRepoWriter* writer) {
     42   if (!writer) {
     43     return;
     44   }
     45   html_refs_free(writer->refs);
     46   writer->refs = NULL;
     47   html_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   html_files_free(writer->files);
     54   writer->files = NULL;
     55   free(writer);
     56 }
     57 
     58 void html_repowriter_set_log_cachefile(void* writer, const char* cachefile) {
     59   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
     60   html_log_set_cachefile(html_writer->log, cachefile);
     61 }
     62 
     63 void html_repowriter_set_log_commit_limit(void* writer, size_t count) {
     64   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
     65   html_log_set_commit_limit(html_writer->log, count);
     66 }
     67 
     68 void html_repowriter_set_baseurl(void* writer, const char* baseurl) {
     69   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
     70   atom_set_baseurl(html_writer->atom, baseurl);
     71   atom_set_baseurl(html_writer->tags, baseurl);
     72 }
     73 
     74 void html_repowriter_begin(void* writer) {
     75   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
     76   mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
     77   mkdir("file", S_IRWXU | S_IRWXG | S_IRWXO);
     78 
     79   html_refs_begin(html_writer->refs);
     80   html_log_begin(html_writer->log);
     81   atom_begin(html_writer->atom);
     82   atom_begin(html_writer->tags);
     83   html_files_begin(html_writer->files);
     84 }
     85 
     86 void html_repowriter_add_commit(void* writer, const GitCommit* git_commit) {
     87   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
     88   char filename[PATH_MAX];
     89   if (snprintf(filename, sizeof(filename), "%s.html",
     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(html_writer->atom, git_commit, path, "text/html", "");
     96 
     97   if (html_log_can_add_commits(html_writer->log)) {
     98     html_log_add_commit(html_writer->log, git_commit);
     99     HtmlCommit* commit =
    100         html_commit_create(html_writer->repo, gitcommit_oid(git_commit),
    101                            gitcommit_summary(git_commit));
    102     html_commit_begin(commit);
    103     html_commit_add_commit(commit, git_commit);
    104     html_commit_end(commit);
    105     html_commit_free(commit);
    106   }
    107 }
    108 
    109 void html_repowriter_add_reference(void* writer, const GitReference* ref) {
    110   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
    111   html_refs_add_ref(html_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.html", 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(html_writer->tags, commit, path, "text/html",
    122                     gitreference_shorthand(ref));
    123   }
    124 }
    125 
    126 void html_repowriter_add_file(void* writer, const GitFile* file) {
    127   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
    128   html_files_add_file(html_writer->files, file);
    129 
    130   HtmlFileBlob* blob =
    131       html_fileblob_create(html_writer->repo, gitfile_repo_path(file));
    132   html_fileblob_begin(blob);
    133   html_fileblob_add_file(blob, file);
    134   html_fileblob_end(blob);
    135   html_fileblob_free(blob);
    136 }
    137 
    138 void html_repowriter_end(void* writer) {
    139   HtmlRepoWriter* html_writer = (HtmlRepoWriter*)writer;
    140   html_refs_end(html_writer->refs);
    141   html_log_end(html_writer->log);
    142   atom_end(html_writer->atom);
    143   atom_end(html_writer->tags);
    144   html_files_end(html_writer->files);
    145 }