git.mcksp
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
#include <err.h>
#include <stdio.h>
#include <git2.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>

#define PATH_MAX 4096
#define DOMAIN "https://git.mcksp.com"

struct Config {
    char *output_path;
    char *files_path;
    char *repo_name;
    char *description;
};

const char header[] =
"<html lang='en'>\n"
"    <head>\n"
"        <meta charset='UTF-8'>\n"
"        <meta name='viewport' content='width=device-width, initial-scale=1.0'>\n"
"        <meta name='author' content='Maciej Spychala' />\n"
"        <title>git.mcksp.com</title>\n"
"        <link type='text/css' rel='stylesheet' href='https://mcksp.com/assets/style.css'>\n"
"        <style>.line-num{text-decoration:none;color:#555;}.icon{width:16px;height:16px;image-rendering: pixelated;padding-right:4px;}.listing > div{margin-bottom:4px;}a.link{text-decoration:none;}.repo-name{padding-right:32px;}</style>\n"
"    </head>\n"
"    <body>\n"
"        <header>\n"
"            <a id='title' href='/'>git.mcksp</a></span>\n"
"            <div id='navbar'>\n"
"                <a href='https://mcksp.com/blog'>blog</a>\n"
"                <a href='https://mcksp.com/tech'>tech</a>\n"
"                <a href='https://git.mcksp.com'>git</a>\n"
"                <a href='https://mcksp.com/about'>about</a>\n"
"                <a href='https://mcksp.com/rss.xml'>rss</a>\n"
"            </div>\n"
"        </header>\n";

const char footer[] =
"        <footer>\n"
"            made with <a href='https://git.mcksp.com/gituwa'>gituwa</a>\n"
"        </footer>\n"
"    </body>\n"
"</html>\n";

git_repository *repo;

char* basename(char *path)
{
    char *base = strrchr(path, '/');
    return base ? base + 1 : path;
}

int lines(const char *s) {
    int lines = 0;
    while (*s) {
        if (*s == '\n') {
            lines++;
        }
        s++;
    }
    return lines;
}

void join_paths(char* path, const char* path1, const char* path2) {
    char tmp[PATH_MAX];
    int n = snprintf(tmp, PATH_MAX, "%s%s%s", path1, path1[0] && path1[strlen(path1) - 1] != '/' ? "/" : "", path2);
    if (n >= PATH_MAX) {
        errx(1, "path truncated");
    }
    strcpy(path, tmp);
}

int html_encode(FILE *f, const char *s) {
    while(*s) {
        switch(*s) {
			case '<':  fputs("&lt;",   f); break;
			case '>':  fputs("&gt;",   f); break;
			case '\'': fputs("&#39;",  f); break;
			case '&':  fputs("&amp;",  f); break;
			case '"':  fputs("&quot;", f); break;
			default:   fputc(*s, f); break;
		}
        s++;
	}
}

void intro(FILE *f, const char *repo_name, const char *desc) {
    fprintf(f, "<table style='padding-bottom:16px'><tbody><tr><td><img src='/icons/image.png' style='width:32px;height:32px;image-rendering: pixelated'></td>");
    fprintf(f, "<td><div><b>%s</b> - %s</div><div>git clone %s/%s.git</div></td></tr></tbody></table>\n", repo_name, desc, DOMAIN, repo_name);
}

void print_file(FILE *f, git_blob* blob, bool lines_number) {
    const void *content = git_blob_rawcontent(blob);

    fprintf(f, "<div style='overflow-x:scroll;'><table><tbody><tr>");

    if (lines_number) {
        fprintf(f, "<td><pre>");
        for (int i = 1; i <= lines(content); i++) {
            fprintf(f, "<a href='#L%d' class='line-num' id='L%d'>%5d</a>\n", i, i, i);
        }
        fprintf(f, "</pre></td>");
    }

    fprintf(f, "<td><pre style='padding-left:8px'>");
    html_encode(f, (char *) content);
    fprintf(f, "</pre></td></tr></tbody></table></div>");
}

int process_file(const git_tree_entry *file, const char *root, const char *name, const struct Config* conf) {
    git_blob* blob = NULL;
    if (git_blob_lookup(&blob, repo, git_tree_entry_id(file)) != 0) {
        printf("cannot get blob\n");
    }

    char path[PATH_MAX];
    sprintf(path, "%s/%s/%s.html", conf->files_path, root, name);
    FILE *f = fopen(path, "w");
    if (f == NULL) {
        printf("couldnt open file %s\n", path);
        return 0;
    }
    fprintf(f, "%s", header);

    if (git_blob_is_binary(blob)) {
        fputs("<p>binary file</p>", f);
    } else {
        print_file(f, blob, true);
    }

    fprintf(f, "%s", footer);
    fclose(f);
    git_blob_free(blob);
}

void root_readme(FILE* f, const git_tree* tree) {
        fprintf(f,
                "<hr><div style='text-align:center;'><img src='img.png'/></div>\n");
        const git_tree_entry* readme = git_tree_entry_byname(tree, "README");
        if (readme != NULL) {
            git_blob* blob = NULL;
            if (git_blob_lookup(&blob, repo, git_tree_entry_id(readme)) != 0) {
                printf("cannot get blob\n");
            }
            print_file(f, blob, false);
        }
}

int process_tree(const git_tree* tree, const char* tree_path, const struct Config* conf, bool is_root)
{
    char dir_path[PATH_MAX];
    join_paths(dir_path, conf->files_path, tree_path);
    mkdir(dir_path, 0755);
    

    char index_path[PATH_MAX];
    if (is_root) {
        sprintf(index_path, "%s/index.html", conf->output_path);
    } else {
        join_paths(index_path, conf->files_path, tree_path);
        strcat(index_path, ".html");
    }

    FILE *f = fopen(index_path, "w");

    fprintf(f, "%s", header);
    intro(f, conf->repo_name, conf->description);
    fprintf(f, "<div class='listing'>");

    size_t child_count = git_tree_entrycount(tree);

    for (size_t i = 0; i < child_count; i++) {
        const git_tree_entry* child = git_tree_entry_byindex(tree, i);
        git_otype child_type = git_tree_entry_type(child);

        if (child_type == GIT_OBJECT_TREE) {
            const char *child_name = git_tree_entry_name(child);
            char child_path[PATH_MAX];
            join_paths(child_path, tree_path, child_name);

            fprintf(f, "<div><img src='/icons/dir.png' class='icon'><a class='link' href='/%s/%s.html'>%s</a></div>", conf->files_path, child_path, child_name);
            git_tree *child_tree = NULL;
            if (git_tree_lookup(&child_tree, repo, git_tree_entry_id(child))) {
                printf("couldnt find tree for %s\n", child_path);
            }
            process_tree(child_tree, child_path, conf, false);
            continue;
        }
    }

    for (size_t i = 0; i < child_count; i++) {
        const git_tree_entry* child = git_tree_entry_byindex(tree, i);
        git_otype child_type = git_tree_entry_type(child);

        if (child_type == GIT_OBJECT_BLOB) {
            const char *child_name = git_tree_entry_name(child);
            char child_path[PATH_MAX];
            join_paths(child_path, tree_path, child_name);

            fprintf(f, "<div><img src='/icons/text.png' class='icon'><a class='link' href='/%s/%s.html'>%s</a></div>", conf->files_path, child_path, child_name);
            process_file(child, tree_path, child_name, conf);
            continue;
        }
    }

    if (is_root) {
        root_readme(f, tree);
    }

    fprintf(f, "</div>");
    fprintf(f, "%s", footer);

    return 0;
}

void remove_ext(char* repo_name) {
    char *dot = strrchr(repo_name, '.');
    if (dot) {
        dot[0] = 0;
    }
}

int main(int argc, char *argv[]) {
    git_libgit2_init();
    if (argc < 3) {
        printf("usage: gituwa <repo_path> <output_path> <description>\n");
        return 0;
    }

    char files_path[PATH_MAX];
    join_paths(files_path, argv[2], "files");

    char description[PATH_MAX] = "";
    for (int i = 3; i < argc; i++) {
        strncat(description, argv[i], PATH_MAX - strlen(description) - 1);
        if (i < argc - 1) {
            strncat(description, " ", PATH_MAX - strlen(description) - 1);
        }
    }

    if (git_repository_open(&repo, argv[1])) {
        printf("cannot open repository %s\n", git_error_last()->message);
    }

    struct Config conf = {
        .output_path = argv[2],
        .files_path = files_path,
        .repo_name = basename(argv[1]),
        .description = description
    };
    remove_ext(conf.repo_name);

    mkdir(conf.output_path, 0755);
    mkdir(conf.files_path, 0755);

    git_object *head;
    git_revparse_single(&head, repo, "HEAD");
    git_commit *commit = (git_commit*)head;

    git_tree* tree = NULL;
    if (git_commit_tree(&tree, commit) != 0) {
        printf("cannot get tree\n");
    }

    process_tree(tree, "", &conf, true);
    git_tree_free(tree);
    git_libgit2_shutdown();

    return 0;
}