garbash-stagit

garbash fork of stagit for git hosting with subdirs
git clone git://git.alexkarle.com.com/garbash-stagit
Log | Files | Refs | README | LICENSE

stagit.c (34992B) [raw]


      1 #include <sys/stat.h>
      2 #include <sys/types.h>
      3 
      4 #include <err.h>
      5 #include <errno.h>
      6 #include <libgen.h>
      7 #include <limits.h>
      8 #include <stdint.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <time.h>
     13 #include <unistd.h>
     14 
     15 #include <git2.h>
     16 
     17 #include "compat.h"
     18 
     19 #define LEN(s)    (sizeof(s)/sizeof(*s))
     20 
     21 struct deltainfo {
     22 	git_patch *patch;
     23 
     24 	size_t addcount;
     25 	size_t delcount;
     26 };
     27 
     28 struct commitinfo {
     29 	const git_oid *id;
     30 
     31 	char oid[GIT_OID_HEXSZ + 1];
     32 	char parentoid[GIT_OID_HEXSZ + 1];
     33 
     34 	const git_signature *author;
     35 	const git_signature *committer;
     36 	const char          *summary;
     37 	const char          *msg;
     38 
     39 	git_diff   *diff;
     40 	git_commit *commit;
     41 	git_commit *parent;
     42 	git_tree   *commit_tree;
     43 	git_tree   *parent_tree;
     44 
     45 	size_t addcount;
     46 	size_t delcount;
     47 	size_t filecount;
     48 
     49 	struct deltainfo **deltas;
     50 	size_t ndeltas;
     51 };
     52 
     53 /* reference and associated data for sorting */
     54 struct referenceinfo {
     55 	struct git_reference *ref;
     56 	struct commitinfo *ci;
     57 };
     58 
     59 static git_repository *repo;
     60 
     61 static const char *baseurl = ""; /* base URL to make absolute RSS/Atom URI */
     62 static const char *rooturl = ""; /* logo.png links to this (used in multi-dir solutions) */
     63 static const char *relpath = "";
     64 static const char *repodir;
     65 
     66 static char *name = "";
     67 static char *strippedname = "";
     68 static char description[255];
     69 static char cloneurl[1024];
     70 static char *submodules;
     71 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
     72 static char *license;
     73 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
     74 static char *readme;
     75 static long long nlogcommits = -1; /* < 0 indicates not used */
     76 
     77 /* cache */
     78 static git_oid lastoid;
     79 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
     80 static FILE *rcachefp, *wcachefp;
     81 static const char *cachefile;
     82 
     83 void
     84 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
     85 {
     86 	int r;
     87 
     88 	r = snprintf(buf, bufsiz, "%s%s%s",
     89 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     90 	if (r < 0 || (size_t)r >= bufsiz)
     91 		errx(1, "path truncated: '%s%s%s'",
     92 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
     93 }
     94 
     95 void
     96 deltainfo_free(struct deltainfo *di)
     97 {
     98 	if (!di)
     99 		return;
    100 	git_patch_free(di->patch);
    101 	memset(di, 0, sizeof(*di));
    102 	free(di);
    103 }
    104 
    105 int
    106 commitinfo_getstats(struct commitinfo *ci)
    107 {
    108 	struct deltainfo *di;
    109 	git_diff_options opts;
    110 	git_diff_find_options fopts;
    111 	const git_diff_delta *delta;
    112 	const git_diff_hunk *hunk;
    113 	const git_diff_line *line;
    114 	git_patch *patch = NULL;
    115 	size_t ndeltas, nhunks, nhunklines;
    116 	size_t i, j, k;
    117 
    118 	if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
    119 		goto err;
    120 	if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
    121 		if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
    122 			ci->parent = NULL;
    123 			ci->parent_tree = NULL;
    124 		}
    125 	}
    126 
    127 	git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
    128 	opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
    129 	              GIT_DIFF_IGNORE_SUBMODULES |
    130 		      GIT_DIFF_INCLUDE_TYPECHANGE;
    131 	if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
    132 		goto err;
    133 
    134 	if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
    135 		goto err;
    136 	/* find renames and copies, exact matches (no heuristic) for renames. */
    137 	fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
    138 	               GIT_DIFF_FIND_EXACT_MATCH_ONLY;
    139 	if (git_diff_find_similar(ci->diff, &fopts))
    140 		goto err;
    141 
    142 	ndeltas = git_diff_num_deltas(ci->diff);
    143 	if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
    144 		err(1, "calloc");
    145 
    146 	for (i = 0; i < ndeltas; i++) {
    147 		if (git_patch_from_diff(&patch, ci->diff, i))
    148 			goto err;
    149 
    150 		if (!(di = calloc(1, sizeof(struct deltainfo))))
    151 			err(1, "calloc");
    152 		di->patch = patch;
    153 		ci->deltas[i] = di;
    154 
    155 		delta = git_patch_get_delta(patch);
    156 
    157 		/* skip stats for binary data */
    158 		if (delta->flags & GIT_DIFF_FLAG_BINARY)
    159 			continue;
    160 
    161 		nhunks = git_patch_num_hunks(patch);
    162 		for (j = 0; j < nhunks; j++) {
    163 			if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
    164 				break;
    165 			for (k = 0; ; k++) {
    166 				if (git_patch_get_line_in_hunk(&line, patch, j, k))
    167 					break;
    168 				if (line->old_lineno == -1) {
    169 					di->addcount++;
    170 					ci->addcount++;
    171 				} else if (line->new_lineno == -1) {
    172 					di->delcount++;
    173 					ci->delcount++;
    174 				}
    175 			}
    176 		}
    177 	}
    178 	ci->ndeltas = i;
    179 	ci->filecount = i;
    180 
    181 	return 0;
    182 
    183 err:
    184 	git_diff_free(ci->diff);
    185 	ci->diff = NULL;
    186 	git_tree_free(ci->commit_tree);
    187 	ci->commit_tree = NULL;
    188 	git_tree_free(ci->parent_tree);
    189 	ci->parent_tree = NULL;
    190 	git_commit_free(ci->parent);
    191 	ci->parent = NULL;
    192 
    193 	if (ci->deltas)
    194 		for (i = 0; i < ci->ndeltas; i++)
    195 			deltainfo_free(ci->deltas[i]);
    196 	free(ci->deltas);
    197 	ci->deltas = NULL;
    198 	ci->ndeltas = 0;
    199 	ci->addcount = 0;
    200 	ci->delcount = 0;
    201 	ci->filecount = 0;
    202 
    203 	return -1;
    204 }
    205 
    206 void
    207 commitinfo_free(struct commitinfo *ci)
    208 {
    209 	size_t i;
    210 
    211 	if (!ci)
    212 		return;
    213 	if (ci->deltas)
    214 		for (i = 0; i < ci->ndeltas; i++)
    215 			deltainfo_free(ci->deltas[i]);
    216 
    217 	free(ci->deltas);
    218 	git_diff_free(ci->diff);
    219 	git_tree_free(ci->commit_tree);
    220 	git_tree_free(ci->parent_tree);
    221 	git_commit_free(ci->commit);
    222 	git_commit_free(ci->parent);
    223 	memset(ci, 0, sizeof(*ci));
    224 	free(ci);
    225 }
    226 
    227 struct commitinfo *
    228 commitinfo_getbyoid(const git_oid *id)
    229 {
    230 	struct commitinfo *ci;
    231 
    232 	if (!(ci = calloc(1, sizeof(struct commitinfo))))
    233 		err(1, "calloc");
    234 
    235 	if (git_commit_lookup(&(ci->commit), repo, id))
    236 		goto err;
    237 	ci->id = id;
    238 
    239 	git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
    240 	git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
    241 
    242 	ci->author = git_commit_author(ci->commit);
    243 	ci->committer = git_commit_committer(ci->commit);
    244 	ci->summary = git_commit_summary(ci->commit);
    245 	ci->msg = git_commit_message(ci->commit);
    246 
    247 	return ci;
    248 
    249 err:
    250 	commitinfo_free(ci);
    251 
    252 	return NULL;
    253 }
    254 
    255 int
    256 refs_cmp(const void *v1, const void *v2)
    257 {
    258 	const struct referenceinfo *r1 = v1, *r2 = v2;
    259 	time_t t1, t2;
    260 	int r;
    261 
    262 	if ((r = git_reference_is_tag(r1->ref) - git_reference_is_tag(r2->ref)))
    263 		return r;
    264 
    265 	t1 = r1->ci->author ? r1->ci->author->when.time : 0;
    266 	t2 = r2->ci->author ? r2->ci->author->when.time : 0;
    267 	if ((r = t1 > t2 ? -1 : (t1 == t2 ? 0 : 1)))
    268 		return r;
    269 
    270 	return strcmp(git_reference_shorthand(r1->ref),
    271 	              git_reference_shorthand(r2->ref));
    272 }
    273 
    274 int
    275 getrefs(struct referenceinfo **pris, size_t *prefcount)
    276 {
    277 	struct referenceinfo *ris = NULL;
    278 	struct commitinfo *ci = NULL;
    279 	git_reference_iterator *it = NULL;
    280 	const git_oid *id = NULL;
    281 	git_object *obj = NULL;
    282 	git_reference *dref = NULL, *r, *ref = NULL;
    283 	size_t i, refcount;
    284 
    285 	*pris = NULL;
    286 	*prefcount = 0;
    287 
    288 	if (git_reference_iterator_new(&it, repo))
    289 		return -1;
    290 
    291 	for (refcount = 0; !git_reference_next(&ref, it); ) {
    292 		if (!git_reference_is_branch(ref) && !git_reference_is_tag(ref)) {
    293 			git_reference_free(ref);
    294 			ref = NULL;
    295 			continue;
    296 		}
    297 
    298 		switch (git_reference_type(ref)) {
    299 		case GIT_REF_SYMBOLIC:
    300 			if (git_reference_resolve(&dref, ref))
    301 				goto err;
    302 			r = dref;
    303 			break;
    304 		case GIT_REF_OID:
    305 			r = ref;
    306 			break;
    307 		default:
    308 			continue;
    309 		}
    310 		if (!git_reference_target(r) ||
    311 		    git_reference_peel(&obj, r, GIT_OBJ_ANY))
    312 			goto err;
    313 		if (!(id = git_object_id(obj)))
    314 			goto err;
    315 		if (!(ci = commitinfo_getbyoid(id)))
    316 			break;
    317 
    318 		if (!(ris = reallocarray(ris, refcount + 1, sizeof(*ris))))
    319 			err(1, "realloc");
    320 		ris[refcount].ci = ci;
    321 		ris[refcount].ref = r;
    322 		refcount++;
    323 
    324 		git_object_free(obj);
    325 		obj = NULL;
    326 		git_reference_free(dref);
    327 		dref = NULL;
    328 	}
    329 	git_reference_iterator_free(it);
    330 
    331 	/* sort by type, date then shorthand name */
    332 	qsort(ris, refcount, sizeof(*ris), refs_cmp);
    333 
    334 	*pris = ris;
    335 	*prefcount = refcount;
    336 
    337 	return 0;
    338 
    339 err:
    340 	git_object_free(obj);
    341 	git_reference_free(dref);
    342 	commitinfo_free(ci);
    343 	for (i = 0; i < refcount; i++) {
    344 		commitinfo_free(ris[i].ci);
    345 		git_reference_free(ris[i].ref);
    346 	}
    347 	free(ris);
    348 
    349 	return -1;
    350 }
    351 
    352 FILE *
    353 efopen(const char *filename, const char *flags)
    354 {
    355 	FILE *fp;
    356 
    357 	if (!(fp = fopen(filename, flags)))
    358 		err(1, "fopen: '%s'", filename);
    359 
    360 	return fp;
    361 }
    362 
    363 /* Escape characters below as HTML 2.0 / XML 1.0. */
    364 void
    365 xmlencode(FILE *fp, const char *s, size_t len)
    366 {
    367 	size_t i;
    368 
    369 	for (i = 0; *s && i < len; s++, i++) {
    370 		switch(*s) {
    371 		case '<':  fputs("&lt;",   fp); break;
    372 		case '>':  fputs("&gt;",   fp); break;
    373 		case '\'': fputs("&#39;",  fp); break;
    374 		case '&':  fputs("&amp;",  fp); break;
    375 		case '"':  fputs("&quot;", fp); break;
    376 		default:   putc(*s, fp);
    377 		}
    378 	}
    379 }
    380 
    381 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\r', '\n' */
    382 void
    383 xmlencodeline(FILE *fp, const char *s, size_t len)
    384 {
    385 	size_t i;
    386 
    387 	for (i = 0; *s && i < len; s++, i++) {
    388 		switch(*s) {
    389 		case '<':  fputs("&lt;",   fp); break;
    390 		case '>':  fputs("&gt;",   fp); break;
    391 		case '\'': fputs("&#39;",  fp); break;
    392 		case '&':  fputs("&amp;",  fp); break;
    393 		case '"':  fputs("&quot;", fp); break;
    394 		case '\r': break; /* ignore CR */
    395 		case '\n': break; /* ignore LF */
    396 		default:   putc(*s, fp);
    397 		}
    398 	}
    399 }
    400 
    401 int
    402 mkdirp(const char *path)
    403 {
    404 	char tmp[PATH_MAX], *p;
    405 
    406 	if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
    407 		errx(1, "path truncated: '%s'", path);
    408 	for (p = tmp + (tmp[0] == '/'); *p; p++) {
    409 		if (*p != '/')
    410 			continue;
    411 		*p = '\0';
    412 		if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
    413 			return -1;
    414 		*p = '/';
    415 	}
    416 	if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
    417 		return -1;
    418 	return 0;
    419 }
    420 
    421 void
    422 printtimez(FILE *fp, const git_time *intime)
    423 {
    424 	struct tm *intm;
    425 	time_t t;
    426 	char out[32];
    427 
    428 	t = (time_t)intime->time;
    429 	if (!(intm = gmtime(&t)))
    430 		return;
    431 	strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
    432 	fputs(out, fp);
    433 }
    434 
    435 void
    436 printtime(FILE *fp, const git_time *intime)
    437 {
    438 	struct tm *intm;
    439 	time_t t;
    440 	char out[32];
    441 
    442 	t = (time_t)intime->time + (intime->offset * 60);
    443 	if (!(intm = gmtime(&t)))
    444 		return;
    445 	strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
    446 	if (intime->offset < 0)
    447 		fprintf(fp, "%s -%02d%02d", out,
    448 		            -(intime->offset) / 60, -(intime->offset) % 60);
    449 	else
    450 		fprintf(fp, "%s +%02d%02d", out,
    451 		            intime->offset / 60, intime->offset % 60);
    452 }
    453 
    454 void
    455 printtimeshort(FILE *fp, const git_time *intime)
    456 {
    457 	struct tm *intm;
    458 	time_t t;
    459 	char out[32];
    460 
    461 	t = (time_t)intime->time;
    462 	if (!(intm = gmtime(&t)))
    463 		return;
    464 	strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
    465 	fputs(out, fp);
    466 }
    467 
    468 void
    469 writeheader(FILE *fp, const char *title)
    470 {
    471 	fputs("<!DOCTYPE html>\n"
    472 		"<html>\n<head>\n"
    473 		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
    474 		"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
    475 		"<title>", fp);
    476 	xmlencode(fp, title, strlen(title));
    477 	if (title[0] && strippedname[0])
    478 		fputs(" - ", fp);
    479 	xmlencode(fp, strippedname, strlen(strippedname));
    480 	if (description[0])
    481 		fputs(" - ", fp);
    482 	xmlencode(fp, description, strlen(description));
    483 	fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
    484 	fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
    485 		name, relpath);
    486 	fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed (tags)\" href=\"%stags.xml\" />\n",
    487 		name, relpath);
    488 	fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
    489 	fputs("</head>\n<body>\n<table><tr><td>", fp);
    490         if (rooturl) {
    491             fprintf(fp, "<a href=\"%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
    492                     rooturl, relpath);
    493         } else {
    494             fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
    495                     relpath, relpath);
    496         }
    497 	fputs("</td><td><h1>", fp);
    498 	xmlencode(fp, strippedname, strlen(strippedname));
    499 	fputs("</h1><span class=\"desc\">", fp);
    500 	xmlencode(fp, description, strlen(description));
    501 	fputs("</span></td></tr>", fp);
    502 	if (cloneurl[0]) {
    503 		fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
    504 		xmlencode(fp, cloneurl, strlen(cloneurl));
    505 		fputs("\">", fp);
    506 		xmlencode(fp, cloneurl, strlen(cloneurl));
    507 		fputs("</a></td></tr>", fp);
    508 	}
    509 	fputs("<tr><td></td><td>\n", fp);
    510 	fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
    511 	fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
    512 	fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
    513 	if (submodules)
    514 		fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
    515 		        relpath, submodules);
    516 	if (readme)
    517 		fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
    518 		        relpath, readme);
    519 	if (license)
    520 		fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
    521 		        relpath, license);
    522 	fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
    523 }
    524 
    525 void
    526 writefooter(FILE *fp)
    527 {
    528 	fputs("</div>\n</body>\n</html>\n", fp);
    529 }
    530 
    531 size_t
    532 writeblobhtml(FILE *fp, const git_blob *blob)
    533 {
    534 	size_t n = 0, i, len, prev;
    535 	const char *nfmt = "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">%7zu</a> ";
    536 	const char *s = git_blob_rawcontent(blob);
    537 
    538 	len = git_blob_rawsize(blob);
    539 	fputs("<pre id=\"blob\">\n", fp);
    540 
    541 	if (len > 0) {
    542 		for (i = 0, prev = 0; i < len; i++) {
    543 			if (s[i] != '\n')
    544 				continue;
    545 			n++;
    546 			fprintf(fp, nfmt, n, n, n);
    547 			xmlencode(fp, &s[prev], i - prev + 1);
    548 			prev = i + 1;
    549 		}
    550 		/* trailing data */
    551 		if ((len - prev) > 0) {
    552 			n++;
    553 			fprintf(fp, nfmt, n, n, n);
    554 			xmlencode(fp, &s[prev], len - prev);
    555 		}
    556 	}
    557 
    558 	fputs("</pre>\n", fp);
    559 
    560 	return n;
    561 }
    562 
    563 void
    564 printcommit(FILE *fp, struct commitinfo *ci)
    565 {
    566 	fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
    567 		relpath, ci->oid, ci->oid);
    568 
    569 	if (ci->parentoid[0])
    570 		fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
    571 			relpath, ci->parentoid, ci->parentoid);
    572 
    573 	if (ci->author) {
    574 		fputs("<b>Author:</b> ", fp);
    575 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
    576 		fputs(" &lt;<a href=\"mailto:", fp);
    577 		xmlencode(fp, ci->author->email, strlen(ci->author->email));
    578 		fputs("\">", fp);
    579 		xmlencode(fp, ci->author->email, strlen(ci->author->email));
    580 		fputs("</a>&gt;\n<b>Date:</b>   ", fp);
    581 		printtime(fp, &(ci->author->when));
    582 		putc('\n', fp);
    583 	}
    584 	if (ci->msg) {
    585 		putc('\n', fp);
    586 		xmlencode(fp, ci->msg, strlen(ci->msg));
    587 		putc('\n', fp);
    588 	}
    589 }
    590 
    591 void
    592 printshowfile(FILE *fp, struct commitinfo *ci)
    593 {
    594 	const git_diff_delta *delta;
    595 	const git_diff_hunk *hunk;
    596 	const git_diff_line *line;
    597 	git_patch *patch;
    598 	size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
    599 	char linestr[80];
    600 	int c;
    601 
    602 	printcommit(fp, ci);
    603 
    604 	if (!ci->deltas)
    605 		return;
    606 
    607 	if (ci->filecount > 1000   ||
    608 	    ci->ndeltas   > 1000   ||
    609 	    ci->addcount  > 100000 ||
    610 	    ci->delcount  > 100000) {
    611 		fputs("Diff is too large, output suppressed.\n", fp);
    612 		return;
    613 	}
    614 
    615 	/* diff stat */
    616 	fputs("<b>Diffstat:</b>\n<table>", fp);
    617 	for (i = 0; i < ci->ndeltas; i++) {
    618 		delta = git_patch_get_delta(ci->deltas[i]->patch);
    619 
    620 		switch (delta->status) {
    621 		case GIT_DELTA_ADDED:      c = 'A'; break;
    622 		case GIT_DELTA_COPIED:     c = 'C'; break;
    623 		case GIT_DELTA_DELETED:    c = 'D'; break;
    624 		case GIT_DELTA_MODIFIED:   c = 'M'; break;
    625 		case GIT_DELTA_RENAMED:    c = 'R'; break;
    626 		case GIT_DELTA_TYPECHANGE: c = 'T'; break;
    627 		default:                   c = ' '; break;
    628 		}
    629 		if (c == ' ')
    630 			fprintf(fp, "<tr><td>%c", c);
    631 		else
    632 			fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
    633 
    634 		fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
    635 		xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
    636 		if (strcmp(delta->old_file.path, delta->new_file.path)) {
    637 			fputs(" -&gt; ", fp);
    638 			xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
    639 		}
    640 
    641 		add = ci->deltas[i]->addcount;
    642 		del = ci->deltas[i]->delcount;
    643 		changed = add + del;
    644 		total = sizeof(linestr) - 2;
    645 		if (changed > total) {
    646 			if (add)
    647 				add = ((float)total / changed * add) + 1;
    648 			if (del)
    649 				del = ((float)total / changed * del) + 1;
    650 		}
    651 		memset(&linestr, '+', add);
    652 		memset(&linestr[add], '-', del);
    653 
    654 		fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
    655 		        ci->deltas[i]->addcount + ci->deltas[i]->delcount);
    656 		fwrite(&linestr, 1, add, fp);
    657 		fputs("</span><span class=\"d\">", fp);
    658 		fwrite(&linestr[add], 1, del, fp);
    659 		fputs("</span></td></tr>\n", fp);
    660 	}
    661 	fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
    662 		ci->filecount, ci->filecount == 1 ? "" : "s",
    663 	        ci->addcount,  ci->addcount  == 1 ? "" : "s",
    664 	        ci->delcount,  ci->delcount  == 1 ? "" : "s");
    665 
    666 	fputs("<hr/>", fp);
    667 
    668 	for (i = 0; i < ci->ndeltas; i++) {
    669 		patch = ci->deltas[i]->patch;
    670 		delta = git_patch_get_delta(patch);
    671 		fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
    672 		xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
    673 		fputs(".html\">", fp);
    674 		xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
    675 		fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
    676 		xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
    677 		fprintf(fp, ".html\">");
    678 		xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
    679 		fprintf(fp, "</a></b>\n");
    680 
    681 		/* check binary data */
    682 		if (delta->flags & GIT_DIFF_FLAG_BINARY) {
    683 			fputs("Binary files differ.\n", fp);
    684 			continue;
    685 		}
    686 
    687 		nhunks = git_patch_num_hunks(patch);
    688 		for (j = 0; j < nhunks; j++) {
    689 			if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
    690 				break;
    691 
    692 			fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
    693 			xmlencode(fp, hunk->header, hunk->header_len);
    694 			fputs("</a>", fp);
    695 
    696 			for (k = 0; ; k++) {
    697 				if (git_patch_get_line_in_hunk(&line, patch, j, k))
    698 					break;
    699 				if (line->old_lineno == -1)
    700 					fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
    701 						i, j, k, i, j, k);
    702 				else if (line->new_lineno == -1)
    703 					fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
    704 						i, j, k, i, j, k);
    705 				else
    706 					putc(' ', fp);
    707 				xmlencodeline(fp, line->content, line->content_len);
    708 				putc('\n', fp);
    709 				if (line->old_lineno == -1 || line->new_lineno == -1)
    710 					fputs("</a>", fp);
    711 			}
    712 		}
    713 	}
    714 }
    715 
    716 void
    717 writelogline(FILE *fp, struct commitinfo *ci)
    718 {
    719 	fputs("<tr><td>", fp);
    720 	if (ci->author)
    721 		printtimeshort(fp, &(ci->author->when));
    722 	fputs("</td><td>", fp);
    723 	if (ci->summary) {
    724 		fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
    725 		xmlencode(fp, ci->summary, strlen(ci->summary));
    726 		fputs("</a>", fp);
    727 	}
    728 	fputs("</td><td>", fp);
    729 	if (ci->author)
    730 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
    731 	fputs("</td><td class=\"num\" align=\"right\">", fp);
    732 	fprintf(fp, "%zu", ci->filecount);
    733 	fputs("</td><td class=\"num\" align=\"right\">", fp);
    734 	fprintf(fp, "+%zu", ci->addcount);
    735 	fputs("</td><td class=\"num\" align=\"right\">", fp);
    736 	fprintf(fp, "-%zu", ci->delcount);
    737 	fputs("</td></tr>\n", fp);
    738 }
    739 
    740 int
    741 writelog(FILE *fp, const git_oid *oid)
    742 {
    743 	struct commitinfo *ci;
    744 	git_revwalk *w = NULL;
    745 	git_oid id;
    746 	char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
    747 	FILE *fpfile;
    748 	int r;
    749 
    750 	git_revwalk_new(&w, repo);
    751 	git_revwalk_push(w, oid);
    752 
    753 	while (!git_revwalk_next(&id, w)) {
    754 		relpath = "";
    755 
    756 		if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
    757 			break;
    758 
    759 		git_oid_tostr(oidstr, sizeof(oidstr), &id);
    760 		r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
    761 		if (r < 0 || (size_t)r >= sizeof(path))
    762 			errx(1, "path truncated: 'commit/%s.html'", oidstr);
    763 		r = access(path, F_OK);
    764 
    765 		/* optimization: if there are no log lines to write and
    766 		   the commit file already exists: skip the diffstat */
    767 		if (!nlogcommits && !r)
    768 			continue;
    769 
    770 		if (!(ci = commitinfo_getbyoid(&id)))
    771 			break;
    772 		/* diffstat: for stagit HTML required for the log.html line */
    773 		if (commitinfo_getstats(ci) == -1)
    774 			goto err;
    775 
    776 		if (nlogcommits < 0) {
    777 			writelogline(fp, ci);
    778 		} else if (nlogcommits > 0) {
    779 			writelogline(fp, ci);
    780 			nlogcommits--;
    781 			if (!nlogcommits && ci->parentoid[0])
    782 				fputs("<tr><td></td><td colspan=\"5\">"
    783 				      "More commits remaining [...]</td>"
    784 				      "</tr>\n", fp);
    785 		}
    786 
    787 		if (cachefile)
    788 			writelogline(wcachefp, ci);
    789 
    790 		/* check if file exists if so skip it */
    791 		if (r) {
    792 			relpath = "../";
    793 			fpfile = efopen(path, "w");
    794 			writeheader(fpfile, ci->summary);
    795 			fputs("<pre>", fpfile);
    796 			printshowfile(fpfile, ci);
    797 			fputs("</pre>\n", fpfile);
    798 			writefooter(fpfile);
    799 			fclose(fpfile);
    800 		}
    801 err:
    802 		commitinfo_free(ci);
    803 	}
    804 	git_revwalk_free(w);
    805 
    806 	relpath = "";
    807 
    808 	return 0;
    809 }
    810 
    811 void
    812 printcommitatom(FILE *fp, struct commitinfo *ci, const char *tag)
    813 {
    814 	fputs("<entry>\n", fp);
    815 
    816 	fprintf(fp, "<id>%s</id>\n", ci->oid);
    817 	if (ci->author) {
    818 		fputs("<published>", fp);
    819 		printtimez(fp, &(ci->author->when));
    820 		fputs("</published>\n", fp);
    821 	}
    822 	if (ci->committer) {
    823 		fputs("<updated>", fp);
    824 		printtimez(fp, &(ci->committer->when));
    825 		fputs("</updated>\n", fp);
    826 	}
    827 	if (ci->summary) {
    828 		fputs("<title type=\"text\">", fp);
    829 		if (tag && tag[0]) {
    830 			fputs("[", fp);
    831 			xmlencode(fp, tag, strlen(tag));
    832 			fputs("] ", fp);
    833 		}
    834 		xmlencode(fp, ci->summary, strlen(ci->summary));
    835 		fputs("</title>\n", fp);
    836 	}
    837 	fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n",
    838 	        baseurl, ci->oid);
    839 
    840 	if (ci->author) {
    841 		fputs("<author>\n<name>", fp);
    842 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
    843 		fputs("</name>\n<email>", fp);
    844 		xmlencode(fp, ci->author->email, strlen(ci->author->email));
    845 		fputs("</email>\n</author>\n", fp);
    846 	}
    847 
    848 	fputs("<content type=\"text\">", fp);
    849 	fprintf(fp, "commit %s\n", ci->oid);
    850 	if (ci->parentoid[0])
    851 		fprintf(fp, "parent %s\n", ci->parentoid);
    852 	if (ci->author) {
    853 		fputs("Author: ", fp);
    854 		xmlencode(fp, ci->author->name, strlen(ci->author->name));
    855 		fputs(" &lt;", fp);
    856 		xmlencode(fp, ci->author->email, strlen(ci->author->email));
    857 		fputs("&gt;\nDate:   ", fp);
    858 		printtime(fp, &(ci->author->when));
    859 		putc('\n', fp);
    860 	}
    861 	if (ci->msg) {
    862 		putc('\n', fp);
    863 		xmlencode(fp, ci->msg, strlen(ci->msg));
    864 	}
    865 	fputs("\n</content>\n</entry>\n", fp);
    866 }
    867 
    868 int
    869 writeatom(FILE *fp, int all)
    870 {
    871 	struct referenceinfo *ris = NULL;
    872 	size_t refcount = 0;
    873 	struct commitinfo *ci;
    874 	git_revwalk *w = NULL;
    875 	git_oid id;
    876 	size_t i, m = 100; /* last 'm' commits */
    877 
    878 	fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    879 	      "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
    880 	xmlencode(fp, strippedname, strlen(strippedname));
    881 	fputs(", branch HEAD</title>\n<subtitle>", fp);
    882 	xmlencode(fp, description, strlen(description));
    883 	fputs("</subtitle>\n", fp);
    884 
    885 	/* all commits or only tags? */
    886 	if (all) {
    887 		git_revwalk_new(&w, repo);
    888 		git_revwalk_push_head(w);
    889 		for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
    890 			if (!(ci = commitinfo_getbyoid(&id)))
    891 				break;
    892 			printcommitatom(fp, ci, "");
    893 			commitinfo_free(ci);
    894 		}
    895 		git_revwalk_free(w);
    896 	} else if (getrefs(&ris, &refcount) != -1) {
    897 		/* references: tags */
    898 		for (i = 0; i < refcount; i++) {
    899 			if (git_reference_is_tag(ris[i].ref))
    900 				printcommitatom(fp, ris[i].ci,
    901 				                git_reference_shorthand(ris[i].ref));
    902 
    903 			commitinfo_free(ris[i].ci);
    904 			git_reference_free(ris[i].ref);
    905 		}
    906 		free(ris);
    907 	}
    908 
    909 	fputs("</feed>\n", fp);
    910 
    911 	return 0;
    912 }
    913 
    914 size_t
    915 writeblob(git_object *obj, const char *fpath, const char *filename, size_t filesize)
    916 {
    917 	char tmp[PATH_MAX] = "", *d;
    918 	const char *p;
    919 	size_t lc = 0;
    920 	FILE *fp;
    921 
    922 	if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
    923 		errx(1, "path truncated: '%s'", fpath);
    924 	if (!(d = dirname(tmp)))
    925 		err(1, "dirname");
    926 	if (mkdirp(d))
    927 		return -1;
    928 
    929 	for (p = fpath, tmp[0] = '\0'; *p; p++) {
    930 		if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
    931 			errx(1, "path truncated: '../%s'", tmp);
    932 	}
    933 	relpath = tmp;
    934 
    935 	fp = efopen(fpath, "w");
    936 	writeheader(fp, filename);
    937 	fputs("<p> ", fp);
    938 	xmlencode(fp, filename, strlen(filename));
    939 	fprintf(fp, " (%zuB)", filesize);
    940 	fputs("</p><hr/>", fp);
    941 
    942 	if (git_blob_is_binary((git_blob *)obj)) {
    943 		fputs("<p>Binary file.</p>\n", fp);
    944 	} else {
    945 		lc = writeblobhtml(fp, (git_blob *)obj);
    946 		if (ferror(fp))
    947 			err(1, "fwrite");
    948 	}
    949 	writefooter(fp);
    950 	fclose(fp);
    951 
    952 	relpath = "";
    953 
    954 	return lc;
    955 }
    956 
    957 const char *
    958 filemode(git_filemode_t m)
    959 {
    960 	static char mode[11];
    961 
    962 	memset(mode, '-', sizeof(mode) - 1);
    963 	mode[10] = '\0';
    964 
    965 	if (S_ISREG(m))
    966 		mode[0] = '-';
    967 	else if (S_ISBLK(m))
    968 		mode[0] = 'b';
    969 	else if (S_ISCHR(m))
    970 		mode[0] = 'c';
    971 	else if (S_ISDIR(m))
    972 		mode[0] = 'd';
    973 	else if (S_ISFIFO(m))
    974 		mode[0] = 'p';
    975 	else if (S_ISLNK(m))
    976 		mode[0] = 'l';
    977 	else if (S_ISSOCK(m))
    978 		mode[0] = 's';
    979 	else
    980 		mode[0] = '?';
    981 
    982 	if (m & S_IRUSR) mode[1] = 'r';
    983 	if (m & S_IWUSR) mode[2] = 'w';
    984 	if (m & S_IXUSR) mode[3] = 'x';
    985 	if (m & S_IRGRP) mode[4] = 'r';
    986 	if (m & S_IWGRP) mode[5] = 'w';
    987 	if (m & S_IXGRP) mode[6] = 'x';
    988 	if (m & S_IROTH) mode[7] = 'r';
    989 	if (m & S_IWOTH) mode[8] = 'w';
    990 	if (m & S_IXOTH) mode[9] = 'x';
    991 
    992 	if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
    993 	if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
    994 	if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
    995 
    996 	return mode;
    997 }
    998 
    999 int
   1000 writefilestree(FILE *fp, git_tree *tree, const char *path)
   1001 {
   1002 	const git_tree_entry *entry = NULL;
   1003 	git_object *obj = NULL;
   1004 	const char *entryname;
   1005 	char filepath[PATH_MAX], entrypath[PATH_MAX], oid[8];
   1006 	size_t count, i, lc, filesize;
   1007 	int r, ret;
   1008 
   1009 	count = git_tree_entrycount(tree);
   1010 	for (i = 0; i < count; i++) {
   1011 		if (!(entry = git_tree_entry_byindex(tree, i)) ||
   1012 		    !(entryname = git_tree_entry_name(entry)))
   1013 			return -1;
   1014 		joinpath(entrypath, sizeof(entrypath), path, entryname);
   1015 
   1016 		r = snprintf(filepath, sizeof(filepath), "file/%s.html",
   1017 		         entrypath);
   1018 		if (r < 0 || (size_t)r >= sizeof(filepath))
   1019 			errx(1, "path truncated: 'file/%s.html'", entrypath);
   1020 
   1021 		if (!git_tree_entry_to_object(&obj, repo, entry)) {
   1022 			switch (git_object_type(obj)) {
   1023 			case GIT_OBJ_BLOB:
   1024 				break;
   1025 			case GIT_OBJ_TREE:
   1026 				/* NOTE: recurses */
   1027 				ret = writefilestree(fp, (git_tree *)obj,
   1028 				                     entrypath);
   1029 				git_object_free(obj);
   1030 				if (ret)
   1031 					return ret;
   1032 				continue;
   1033 			default:
   1034 				git_object_free(obj);
   1035 				continue;
   1036 			}
   1037 
   1038 			filesize = git_blob_rawsize((git_blob *)obj);
   1039 			lc = writeblob(obj, filepath, entryname, filesize);
   1040 
   1041 			fputs("<tr><td>", fp);
   1042 			fputs(filemode(git_tree_entry_filemode(entry)), fp);
   1043 			fprintf(fp, "</td><td><a href=\"%s", relpath);
   1044 			xmlencode(fp, filepath, strlen(filepath));
   1045 			fputs("\">", fp);
   1046 			xmlencode(fp, entrypath, strlen(entrypath));
   1047 			fputs("</a></td><td class=\"num\" align=\"right\">", fp);
   1048 			if (lc > 0)
   1049 				fprintf(fp, "%zuL", lc);
   1050 			else
   1051 				fprintf(fp, "%zuB", filesize);
   1052 			fputs("</td></tr>\n", fp);
   1053 			git_object_free(obj);
   1054 		} else if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT) {
   1055 			/* commit object in tree is a submodule */
   1056 			fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
   1057 				relpath);
   1058 			xmlencode(fp, entrypath, strlen(entrypath));
   1059 			fputs("</a> @ ", fp);
   1060 			git_oid_tostr(oid, sizeof(oid), git_tree_entry_id(entry));
   1061 			xmlencode(fp, oid, strlen(oid));
   1062 			fputs("</td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
   1063 		}
   1064 	}
   1065 
   1066 	return 0;
   1067 }
   1068 
   1069 int
   1070 writefiles(FILE *fp, const git_oid *id)
   1071 {
   1072 	git_tree *tree = NULL;
   1073 	git_commit *commit = NULL;
   1074 	int ret = -1;
   1075 
   1076 	fputs("<table id=\"files\"><thead>\n<tr>"
   1077 	      "<td><b>Mode</b></td><td><b>Name</b></td>"
   1078 	      "<td class=\"num\" align=\"right\"><b>Size</b></td>"
   1079 	      "</tr>\n</thead><tbody>\n", fp);
   1080 
   1081 	if (!git_commit_lookup(&commit, repo, id) &&
   1082 	    !git_commit_tree(&tree, commit))
   1083 		ret = writefilestree(fp, tree, "");
   1084 
   1085 	fputs("</tbody></table>", fp);
   1086 
   1087 	git_commit_free(commit);
   1088 	git_tree_free(tree);
   1089 
   1090 	return ret;
   1091 }
   1092 
   1093 int
   1094 writerefs(FILE *fp)
   1095 {
   1096 	struct referenceinfo *ris = NULL;
   1097 	struct commitinfo *ci;
   1098 	size_t count, i, j, refcount;
   1099 	const char *titles[] = { "Branches", "Tags" };
   1100 	const char *ids[] = { "branches", "tags" };
   1101 	const char *s;
   1102 
   1103 	if (getrefs(&ris, &refcount) == -1)
   1104 		return -1;
   1105 
   1106 	for (i = 0, j = 0, count = 0; i < refcount; i++) {
   1107 		if (j == 0 && git_reference_is_tag(ris[i].ref)) {
   1108 			if (count)
   1109 				fputs("</tbody></table><br/>\n", fp);
   1110 			count = 0;
   1111 			j = 1;
   1112 		}
   1113 
   1114 		/* print header if it has an entry (first). */
   1115 		if (++count == 1) {
   1116 			fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
   1117 		                "<thead>\n<tr><td><b>Name</b></td>"
   1118 			        "<td><b>Last commit date</b></td>"
   1119 			        "<td><b>Author</b></td>\n</tr>\n"
   1120 			        "</thead><tbody>\n",
   1121 			         titles[j], ids[j]);
   1122 		}
   1123 
   1124 		ci = ris[i].ci;
   1125 		s = git_reference_shorthand(ris[i].ref);
   1126 
   1127 		fputs("<tr><td>", fp);
   1128 		xmlencode(fp, s, strlen(s));
   1129 		fputs("</td><td>", fp);
   1130 		if (ci->author)
   1131 			printtimeshort(fp, &(ci->author->when));
   1132 		fputs("</td><td>", fp);
   1133 		if (ci->author)
   1134 			xmlencode(fp, ci->author->name, strlen(ci->author->name));
   1135 		fputs("</td></tr>\n", fp);
   1136 	}
   1137 	/* table footer */
   1138 	if (count)
   1139 		fputs("</tbody></table><br/>\n", fp);
   1140 
   1141 	for (i = 0; i < refcount; i++) {
   1142 		commitinfo_free(ris[i].ci);
   1143 		git_reference_free(ris[i].ref);
   1144 	}
   1145 	free(ris);
   1146 
   1147 	return 0;
   1148 }
   1149 
   1150 void
   1151 usage(char *argv0)
   1152 {
   1153 	fprintf(stderr, "%s [-c cachefile | -l commits] "
   1154 	        "[-u baseurl] repodir\n", argv0);
   1155 	exit(1);
   1156 }
   1157 
   1158 int
   1159 main(int argc, char *argv[])
   1160 {
   1161 	git_object *obj = NULL;
   1162 	const git_oid *head = NULL;
   1163 	mode_t mask;
   1164 	FILE *fp, *fpread;
   1165 	char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
   1166 	char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
   1167 	size_t n;
   1168 	int i, fd;
   1169 
   1170 	for (i = 1; i < argc; i++) {
   1171 		if (argv[i][0] != '-') {
   1172 			if (repodir)
   1173 				usage(argv[0]);
   1174 			repodir = argv[i];
   1175 		} else if (argv[i][1] == 'c') {
   1176 			if (nlogcommits > 0 || i + 1 >= argc)
   1177 				usage(argv[0]);
   1178 			cachefile = argv[++i];
   1179 		} else if (argv[i][1] == 'l') {
   1180 			if (cachefile || i + 1 >= argc)
   1181 				usage(argv[0]);
   1182 			errno = 0;
   1183 			nlogcommits = strtoll(argv[++i], &p, 10);
   1184 			if (argv[i][0] == '\0' || *p != '\0' ||
   1185 			    nlogcommits <= 0 || errno)
   1186 				usage(argv[0]);
   1187 		} else if (argv[i][1] == 'u') {
   1188 			if (i + 1 >= argc)
   1189 				usage(argv[0]);
   1190 			baseurl = argv[++i];
   1191 		} else if (argv[i][1] == 'r') {
   1192 			if (i + 1 >= argc)
   1193 				usage(argv[0]);
   1194 			rooturl = argv[++i];
   1195 		}
   1196 	}
   1197 	if (!repodir)
   1198 		usage(argv[0]);
   1199 
   1200 	if (!realpath(repodir, repodirabs))
   1201 		err(1, "realpath");
   1202 
   1203 	git_libgit2_init();
   1204 
   1205 #ifdef __OpenBSD__
   1206 	if (unveil(repodir, "r") == -1)
   1207 		err(1, "unveil: %s", repodir);
   1208 	if (unveil(".", "rwc") == -1)
   1209 		err(1, "unveil: .");
   1210 	if (cachefile && unveil(cachefile, "rwc") == -1)
   1211 		err(1, "unveil: %s", cachefile);
   1212 
   1213 	if (cachefile) {
   1214 		if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
   1215 			err(1, "pledge");
   1216 	} else {
   1217 		if (pledge("stdio rpath wpath cpath", NULL) == -1)
   1218 			err(1, "pledge");
   1219 	}
   1220 #endif
   1221 
   1222 	if (git_repository_open_ext(&repo, repodir,
   1223 		GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
   1224 		fprintf(stderr, "%s: cannot open repository\n", argv[0]);
   1225 		return 1;
   1226 	}
   1227 
   1228 	/* find HEAD */
   1229 	if (!git_revparse_single(&obj, repo, "HEAD"))
   1230 		head = git_object_id(obj);
   1231 	git_object_free(obj);
   1232 
   1233 	/* use directory name as name */
   1234 	if ((name = strrchr(repodirabs, '/')))
   1235 		name++;
   1236 	else
   1237 		name = "";
   1238 
   1239 	/* strip .git suffix */
   1240 	if (!(strippedname = strdup(name)))
   1241 		err(1, "strdup");
   1242 	if ((p = strrchr(strippedname, '.')))
   1243 		if (!strcmp(p, ".git"))
   1244 			*p = '\0';
   1245 
   1246 	/* read description or .git/description */
   1247 	joinpath(path, sizeof(path), repodir, "description");
   1248 	if (!(fpread = fopen(path, "r"))) {
   1249 		joinpath(path, sizeof(path), repodir, ".git/description");
   1250 		fpread = fopen(path, "r");
   1251 	}
   1252 	if (fpread) {
   1253 		if (!fgets(description, sizeof(description), fpread))
   1254 			description[0] = '\0';
   1255 		fclose(fpread);
   1256 	}
   1257 
   1258 	/* read url or .git/url */
   1259 	joinpath(path, sizeof(path), repodir, "url");
   1260 	if (!(fpread = fopen(path, "r"))) {
   1261 		joinpath(path, sizeof(path), repodir, ".git/url");
   1262 		fpread = fopen(path, "r");
   1263 	}
   1264 	if (fpread) {
   1265 		if (!fgets(cloneurl, sizeof(cloneurl), fpread))
   1266 			cloneurl[0] = '\0';
   1267 		cloneurl[strcspn(cloneurl, "\n")] = '\0';
   1268 		fclose(fpread);
   1269 	}
   1270 
   1271 	/* check LICENSE */
   1272 	for (i = 0; i < LEN(licensefiles) && !license; i++) {
   1273 		if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
   1274 		    git_object_type(obj) == GIT_OBJ_BLOB)
   1275 			license = licensefiles[i] + strlen("HEAD:");
   1276 		git_object_free(obj);
   1277 	}
   1278 
   1279 	/* check README */
   1280 	for (i = 0; i < LEN(readmefiles) && !readme; i++) {
   1281 		if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
   1282 		    git_object_type(obj) == GIT_OBJ_BLOB)
   1283 			readme = readmefiles[i] + strlen("HEAD:");
   1284 		git_object_free(obj);
   1285 	}
   1286 
   1287 	if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
   1288 	    git_object_type(obj) == GIT_OBJ_BLOB)
   1289 		submodules = ".gitmodules";
   1290 	git_object_free(obj);
   1291 
   1292 	/* log for HEAD */
   1293 	fp = efopen("log.html", "w");
   1294 	relpath = "";
   1295 	mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
   1296 	writeheader(fp, "Log");
   1297 	fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
   1298 	      "<td><b>Commit message</b></td>"
   1299 	      "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
   1300 	      "<td class=\"num\" align=\"right\"><b>+</b></td>"
   1301 	      "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
   1302 
   1303 	if (cachefile && head) {
   1304 		/* read from cache file (does not need to exist) */
   1305 		if ((rcachefp = fopen(cachefile, "r"))) {
   1306 			if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
   1307 				errx(1, "%s: no object id", cachefile);
   1308 			if (git_oid_fromstr(&lastoid, lastoidstr))
   1309 				errx(1, "%s: invalid object id", cachefile);
   1310 		}
   1311 
   1312 		/* write log to (temporary) cache */
   1313 		if ((fd = mkstemp(tmppath)) == -1)
   1314 			err(1, "mkstemp");
   1315 		if (!(wcachefp = fdopen(fd, "w")))
   1316 			err(1, "fdopen: '%s'", tmppath);
   1317 		/* write last commit id (HEAD) */
   1318 		git_oid_tostr(buf, sizeof(buf), head);
   1319 		fprintf(wcachefp, "%s\n", buf);
   1320 
   1321 		writelog(fp, head);
   1322 
   1323 		if (rcachefp) {
   1324 			/* append previous log to log.html and the new cache */
   1325 			while (!feof(rcachefp)) {
   1326 				n = fread(buf, 1, sizeof(buf), rcachefp);
   1327 				if (ferror(rcachefp))
   1328 					err(1, "fread");
   1329 				if (fwrite(buf, 1, n, fp) != n ||
   1330 				    fwrite(buf, 1, n, wcachefp) != n)
   1331 					err(1, "fwrite");
   1332 			}
   1333 			fclose(rcachefp);
   1334 		}
   1335 		fclose(wcachefp);
   1336 	} else {
   1337 		if (head)
   1338 			writelog(fp, head);
   1339 	}
   1340 
   1341 	fputs("</tbody></table>", fp);
   1342 	writefooter(fp);
   1343 	fclose(fp);
   1344 
   1345 	/* files for HEAD */
   1346 	fp = efopen("files.html", "w");
   1347 	writeheader(fp, "Files");
   1348 	if (head)
   1349 		writefiles(fp, head);
   1350 	writefooter(fp);
   1351 	fclose(fp);
   1352 
   1353 	/* summary page with branches and tags */
   1354 	fp = efopen("refs.html", "w");
   1355 	writeheader(fp, "Refs");
   1356 	writerefs(fp);
   1357 	writefooter(fp);
   1358 	fclose(fp);
   1359 
   1360 	/* Atom feed */
   1361 	fp = efopen("atom.xml", "w");
   1362 	writeatom(fp, 1);
   1363 	fclose(fp);
   1364 
   1365 	/* Atom feed for tags / releases */
   1366 	fp = efopen("tags.xml", "w");
   1367 	writeatom(fp, 0);
   1368 	fclose(fp);
   1369 
   1370 	/* rename new cache file on success */
   1371 	if (cachefile && head) {
   1372 		if (rename(tmppath, cachefile))
   1373 			err(1, "rename: '%s' to '%s'", tmppath, cachefile);
   1374 		umask((mask = umask(0)));
   1375 		if (chmod(cachefile,
   1376 		    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
   1377 			err(1, "chmod: '%s'", cachefile);
   1378 	}
   1379 
   1380 	/* cleanup */
   1381 	git_repository_free(repo);
   1382 	git_libgit2_shutdown();
   1383 
   1384 	return 0;
   1385 }