stagit

static git page generator
git clone git://git.sgregoratto.me/stagit
Log | Files | Refs | README | LICENSE

stagit-index.c (4935B)


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