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