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