logo

stagit

STAtic GIT web view generator (in C)

stagit-index.c (5557B)


      1 #include <sys/stat.h>
      2 
      3 #include <err.h>
      4 #include <errno.h>
      5 #include <inttypes.h>
      6 #include <limits.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <unistd.h>
     11 
     12 #include <git2.h>
     13 
     14 #include "compat.h"
     15 #include "config.h"
     16 
     17 static git_repository *repo;
     18 
     19 static const char *relpath = "";
     20 
     21 static char description[255] = "Repositories";
     22 static char *name = "";
     23 static char owner[255];
     24 
     25 void
     26 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
     27 {
     28 	int r;
     29 
     30 	r = snprintf(buf, bufsiz, "%s%s%s",
     31 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     32 	if (r < 0 || (size_t)r >= bufsiz)
     33 		errx(1, "path truncated: '%s%s%s'",
     34 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     35 }
     36 
     37 /* Escape characters below as HTML 2.0 / XML 1.0. */
     38 void
     39 xmlencode(FILE *fp, const char *s, size_t len)
     40 {
     41 	size_t i;
     42 
     43 	for (i = 0; *s && i < len; s++, i++) {
     44 		switch(*s) {
     45 		case '<':  fputs("&lt;",   fp); break;
     46 		case '>':  fputs("&gt;",   fp); break;
     47 		case '\'': fputs("&#39;" , fp); break;
     48 		case '&':  fputs("&amp;",  fp); break;
     49 		case '"':  fputs("&quot;", fp); break;
     50 		default:   fputc(*s, fp);
     51 		}
     52 	}
     53 }
     54 
     55 void
     56 printtimeshort(FILE *fp, const git_time *intime)
     57 {
     58 	struct tm *intm;
     59 	time_t t;
     60 	char out[32];
     61 
     62 	t = (time_t)intime->time;
     63 	if (!(intm = gmtime(&t)))
     64 		return;
     65 	strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
     66 	fputs(out, fp);
     67 }
     68 
     69 void
     70 writeheader(FILE *fp)
     71 {
     72 	fputs("<!DOCTYPE html>\n"
     73 		"<html>\n<head>\n"
     74 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
     75 		"<title>", fp);
     76 	xmlencode(fp, description, strlen(description));
     77 	if(strlen(faviconurl) > 0) {
     78 		fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%s\" />\n", faviconurl);
     79 	} else {
     80 		fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", assetpath);
     81 	}
     82 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
     83 	fputs("</head>\n<body>\n<header>", fp);
     84 	fprintf(fp, "<img src=\"%slogo.png\" alt=\"logo\" width=\"32\" height=\"32\" />",
     85 	        assetpath);
     86 	fputs("<h1>", fp);
     87 	xmlencode(fp, repo_name, strlen(repo_name));
     88 	fputs("</h1><span class=\"desc\">", fp);
     89 	xmlencode(fp, repo_description, strlen(repo_description));
     90 	fputs("</span></header>", fp);
     91 	fputs("<main>\n"
     92 		"<table id=\"index\"><thead>\n"
     93 		"<tr><th>Name</th><th>Description</th>"
     94 		"<th>Last commit</th></tr>"
     95 		"</thead><tbody>\n", fp);
     96 }
     97 
     98 void
     99 writefooter(FILE *fp)
    100 {
    101 	fputs("</tbody>\n</table>\n",fp);
    102 	fputs("</main>\n", fp);
    103 	fputs("\t<footer>Please <a href=\"https://hacktivis.me/about\">contact me</a> by any means for patches, issues(tracker will maybe be added oneday), thanks/donations, or whatever else.</footer>\n", fp);
    104 	fputs("</body>\n</html>\n", fp);
    105 }
    106 
    107 int
    108 writelog(FILE *fp)
    109 {
    110 	git_commit *commit = NULL;
    111 	const git_signature *author;
    112 	git_revwalk *w = NULL;
    113 	git_oid id;
    114 	char *stripped_name = NULL, *p;
    115 	int ret = 0;
    116 
    117 	git_revwalk_new(&w, repo);
    118 	git_revwalk_push_head(w);
    119 	git_revwalk_simplify_first_parent(w);
    120 
    121 	if (git_revwalk_next(&id, w) ||
    122 	    git_commit_lookup(&commit, repo, &id)) {
    123 		ret = -1;
    124 		goto err;
    125 	}
    126 
    127 	author = git_commit_author(commit);
    128 
    129 	/* strip .git suffix */
    130 	if (!(stripped_name = strdup(name)))
    131 		err(1, "strdup");
    132 	if ((p = strrchr(stripped_name, '.')))
    133 		if (!strcmp(p, ".git"))
    134 			*p = '\0';
    135 
    136 	fputs("<tr><td class=\"text index-name\"><a href=\"", fp);
    137 	xmlencode(fp, stripped_name, strlen(stripped_name));
    138 	/*fputs("/log.html\">", fp); Let http server set default index */
    139 	fputs("/\">", fp);
    140 	xmlencode(fp, stripped_name, strlen(stripped_name));
    141 	fputs("</a></td><td class=\"text index-description\">", fp);
    142 	xmlencode(fp, description, strlen(description));
    143 	fputs("</td><td class=\"date index-author-date\">", fp);
    144 	if (author)
    145 		printtimeshort(fp, &(author->when));
    146 	fputs("</td></tr>", fp);
    147 
    148 	git_commit_free(commit);
    149 err:
    150 	git_revwalk_free(w);
    151 	free(stripped_name);
    152 
    153 	return ret;
    154 }
    155 
    156 int
    157 main(int argc, char *argv[])
    158 {
    159 	FILE *fp;
    160 	char path[PATH_MAX], repodirabs[PATH_MAX + 1];
    161 	const char *repodir;
    162 	int i, ret = 0;
    163 
    164 	if (argc < 2) {
    165 		fprintf(stderr, "%s [repodir...]\n", argv[0]);
    166 		return 1;
    167 	}
    168 
    169 	git_libgit2_init();
    170 
    171 #ifdef __OpenBSD__
    172 	if (pledge("stdio rpath", NULL) == -1)
    173 		err(1, "pledge");
    174 #endif
    175 
    176 	writeheader(stdout);
    177 
    178 	for (i = 1; i < argc; i++) {
    179 		repodir = argv[i];
    180 		if (!realpath(repodir, repodirabs))
    181 			err(1, "realpath");
    182 
    183 		if (git_repository_open_ext(&repo, repodir,
    184 		    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
    185 			fprintf(stderr, "%s: cannot open repository\n", argv[0]);
    186 			ret = 1;
    187 			continue;
    188 		}
    189 
    190 		/* use directory name as name */
    191 		if ((name = strrchr(repodirabs, '/')))
    192 			name++;
    193 		else
    194 			name = "";
    195 
    196 		/* read description or .git/description */
    197 		joinpath(path, sizeof(path), repodir, "description");
    198 		if (!(fp = fopen(path, "r"))) {
    199 			joinpath(path, sizeof(path), repodir, ".git/description");
    200 			fp = fopen(path, "r");
    201 		}
    202 		description[0] = '\0';
    203 		if (fp) {
    204 			if (!fgets(description, sizeof(description), fp))
    205 				description[0] = '\0';
    206 			fclose(fp);
    207 		}
    208 
    209 		/* read owner or .git/owner */
    210 		joinpath(path, sizeof(path), repodir, "owner");
    211 		if (!(fp = fopen(path, "r"))) {
    212 			joinpath(path, sizeof(path), repodir, ".git/owner");
    213 			fp = fopen(path, "r");
    214 		}
    215 		owner[0] = '\0';
    216 		if (fp) {
    217 			if (!fgets(owner, sizeof(owner), fp))
    218 				owner[0] = '\0';
    219 			owner[strcspn(owner, "\n")] = '\0';
    220 			fclose(fp);
    221 		}
    222 		writelog(stdout);
    223 	}
    224 	writefooter(stdout);
    225 
    226 	/* cleanup */
    227 	git_repository_free(repo);
    228 	git_libgit2_shutdown();
    229 
    230 	return ret;
    231 }