28 char oid[GIT_OID_HEXSZ + 1];
29 char parentoid[GIT_OID_HEXSZ + 1];
31 const git_signature *author;
32 const git_signature *committer;
39 git_tree *commit_tree;
40 git_tree *parent_tree;
46 struct deltainfo **deltas;
50 static git_repository *repo;
52 static const char *relpath = "";
53 static const char *repodir;
55 static char *name = "";
56 static char *strippedname = "";
57 static char description[255];
58 static char cloneurl[1024];
59 static int haslicense, hasreadme, hassubmodules;
62 static git_oid lastoid;
63 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + nul byte */
64 static FILE *rcachefp, *wcachefp;
65 static const char *cachefile;
68 #define pledge(p1,p2) 0
72 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
76 r = snprintf(buf, bufsiz, "%s%s%s",
77 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
78 if (r == -1 || (size_t)r >= bufsiz)
79 errx(1, "path truncated: '%s%s%s'",
80 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
84 deltainfo_free(struct deltainfo *di)
88 git_patch_free(di->patch);
94 commitinfo_getstats(struct commitinfo *ci)
97 const git_diff_delta *delta;
98 const git_diff_hunk *hunk;
99 const git_diff_line *line;
100 git_patch *patch = NULL;
101 size_t ndeltas, nhunks, nhunklines;
104 ndeltas = git_diff_num_deltas(ci->diff);
105 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
108 for (i = 0; i < ndeltas; i++) {
109 if (git_patch_from_diff(&patch, ci->diff, i))
111 if (!(di = calloc(1, sizeof(struct deltainfo))))
116 delta = git_patch_get_delta(patch);
118 /* skip stats for binary data */
119 if (delta->flags & GIT_DIFF_FLAG_BINARY)
122 nhunks = git_patch_num_hunks(patch);
123 for (j = 0; j < nhunks; j++) {
124 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
127 if (git_patch_get_line_in_hunk(&line, patch, j, k))
129 if (line->old_lineno == -1) {
132 } else if (line->new_lineno == -1) {
146 for (i = 0; i < ci->ndeltas; i++)
147 deltainfo_free(ci->deltas[i]);
159 commitinfo_free(struct commitinfo *ci)
166 for (i = 0; i < ci->ndeltas; i++)
167 deltainfo_free(ci->deltas[i]);
170 git_diff_free(ci->diff);
171 git_tree_free(ci->commit_tree);
172 git_tree_free(ci->parent_tree);
173 git_commit_free(ci->commit);
174 git_commit_free(ci->parent);
179 commitinfo_getbyoid(const git_oid *id)
181 struct commitinfo *ci;
182 git_diff_options opts;
184 if (!(ci = calloc(1, sizeof(struct commitinfo))))
187 if (git_commit_lookup(&(ci->commit), repo, id))
191 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
192 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
194 ci->author = git_commit_author(ci->commit);
195 ci->committer = git_commit_committer(ci->commit);
196 ci->summary = git_commit_summary(ci->commit);
197 ci->msg = git_commit_message(ci->commit);
199 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
201 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
202 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
204 ci->parent_tree = NULL;
208 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
209 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
210 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
222 efopen(const char *name, const char *flags)
226 if (!(fp = fopen(name, flags)))
227 err(1, "fopen: '%s'", name);
232 /* Escape characters below as HTML 2.0 / XML 1.0. */
234 xmlencode(FILE *fp, const char *s, size_t len)
238 for (i = 0; *s && i < len; s++, i++) {
240 case '<': fputs("<", fp); break;
241 case '>': fputs(">", fp); break;
242 case '\'': fputs("'", fp); break;
243 case '&': fputs("&", fp); break;
244 case '"': fputs(""", fp); break;
245 default: fputc(*s, fp);
251 mkdirp(const char *path)
253 char tmp[PATH_MAX], *p;
255 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
256 errx(1, "path truncated: '%s'", path);
257 for (p = tmp + (tmp[0] == '/'); *p; p++) {
261 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
265 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
271 printtimez(FILE *fp, const git_time *intime)
277 t = (time_t)intime->time;
278 if (!(intm = gmtime(&t)))
280 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
285 printtime(FILE *fp, const git_time *intime)
291 t = (time_t)intime->time + (intime->offset * 60);
292 if (!(intm = gmtime(&t)))
294 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
295 if (intime->offset < 0)
296 fprintf(fp, "%s -%02d%02d", out,
297 -(intime->offset) / 60, -(intime->offset) % 60);
299 fprintf(fp, "%s +%02d%02d", out,
300 intime->offset / 60, intime->offset % 60);
304 printtimeshort(FILE *fp, const git_time *intime)
310 t = (time_t)intime->time;
311 if (!(intm = gmtime(&t)))
313 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
318 writeheader(FILE *fp, const char *title)
320 fputs("<!DOCTYPE html>\n"
322 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
324 xmlencode(fp, title, strlen(title));
325 if (title[0] && strippedname[0])
327 xmlencode(fp, strippedname, strlen(strippedname));
330 xmlencode(fp, description, strlen(description));
331 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
332 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
334 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
335 fputs("</head>\n<body>\n<table><tr><td>", fp);
336 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
338 fputs("</td><td><h1>", fp);
339 xmlencode(fp, strippedname, strlen(strippedname));
340 fputs("</h1><span class=\"desc\">", fp);
341 xmlencode(fp, description, strlen(description));
342 fputs("</span></td></tr>", fp);
344 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
345 xmlencode(fp, cloneurl, strlen(cloneurl));
347 xmlencode(fp, cloneurl, strlen(cloneurl));
348 fputs("</a></td></tr>", fp);
350 fputs("<tr><td></td><td>\n", fp);
351 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
352 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
353 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
355 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
357 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
359 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
360 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
364 writefooter(FILE *fp)
366 fputs("</div>\n</body>\n</html>\n", fp);
370 writeblobhtml(FILE *fp, const git_blob *blob)
372 size_t n = 0, i, prev;
373 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
374 const char *s = git_blob_rawcontent(blob);
375 git_off_t len = git_blob_rawsize(blob);
377 fputs("<pre id=\"blob\">\n", fp);
380 for (i = 0, prev = 0; i < (size_t)len; i++) {
384 fprintf(fp, nfmt, n, n, n);
385 xmlencode(fp, &s[prev], i - prev + 1);
389 if ((len - prev) > 0) {
391 fprintf(fp, nfmt, n, n, n);
392 xmlencode(fp, &s[prev], len - prev);
396 fputs("</pre>\n", fp);
402 printcommit(FILE *fp, struct commitinfo *ci)
404 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
405 relpath, ci->oid, ci->oid);
407 if (ci->parentoid[0])
408 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
409 relpath, ci->parentoid, ci->parentoid);
412 fputs("<b>Author:</b> ", fp);
413 xmlencode(fp, ci->author->name, strlen(ci->author->name));
414 fputs(" <<a href=\"mailto:", fp);
415 xmlencode(fp, ci->author->email, strlen(ci->author->email));
417 xmlencode(fp, ci->author->email, strlen(ci->author->email));
418 fputs("</a>>\n<b>Date:</b> ", fp);
419 printtime(fp, &(ci->author->when));
424 xmlencode(fp, ci->msg, strlen(ci->msg));
430 printshowfile(FILE *fp, struct commitinfo *ci)
432 const git_diff_delta *delta;
433 const git_diff_hunk *hunk;
434 const git_diff_line *line;
436 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
444 if (ci->filecount > 1000 ||
445 ci->ndeltas > 1000 ||
446 ci->addcount > 100000 ||
447 ci->delcount > 100000) {
448 fputs("Diff is too large, output suppressed.\n", fp);
453 fputs("<b>Diffstat:</b>\n<table>", fp);
454 for (i = 0; i < ci->ndeltas; i++) {
455 delta = git_patch_get_delta(ci->deltas[i]->patch);
456 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
457 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
458 if (strcmp(delta->old_file.path, delta->new_file.path)) {
459 fputs(" -> ", fp);
460 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
463 add = ci->deltas[i]->addcount;
464 del = ci->deltas[i]->delcount;
466 total = sizeof(linestr) - 2;
467 if (changed > total) {
469 add = ((float)total / changed * add) + 1;
471 del = ((float)total / changed * del) + 1;
473 memset(&linestr, '+', add);
474 memset(&linestr[add], '-', del);
476 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
477 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
478 fwrite(&linestr, 1, add, fp);
479 fputs("</span><span class=\"d\">", fp);
480 fwrite(&linestr[add], 1, del, fp);
481 fputs("</span></td></tr>\n", fp);
483 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
484 ci->filecount, ci->filecount == 1 ? "" : "s",
485 ci->addcount, ci->addcount == 1 ? "" : "s",
486 ci->delcount, ci->delcount == 1 ? "" : "s");
490 for (i = 0; i < ci->ndeltas; i++) {
491 patch = ci->deltas[i]->patch;
492 delta = git_patch_get_delta(patch);
493 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
494 i, relpath, delta->old_file.path, delta->old_file.path,
495 relpath, delta->new_file.path, delta->new_file.path);
497 /* check binary data */
498 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
499 fputs("Binary files differ.\n", fp);
503 nhunks = git_patch_num_hunks(patch);
504 for (j = 0; j < nhunks; j++) {
505 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
508 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
509 xmlencode(fp, hunk->header, hunk->header_len);
513 if (git_patch_get_line_in_hunk(&line, patch, j, k))
515 if (line->old_lineno == -1)
516 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
518 else if (line->new_lineno == -1)
519 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
523 xmlencode(fp, line->content, line->content_len);
524 if (line->old_lineno == -1 || line->new_lineno == -1)
532 writelogline(FILE *fp, struct commitinfo *ci)
534 fputs("<tr><td>", fp);
536 printtimeshort(fp, &(ci->author->when));
537 fputs("</td><td>", fp);
539 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
540 xmlencode(fp, ci->summary, strlen(ci->summary));
543 fputs("</td><td>", fp);
545 xmlencode(fp, ci->author->name, strlen(ci->author->name));
546 fputs("</td><td class=\"num\" align=\"right\">", fp);
547 fprintf(fp, "%zu", ci->filecount);
548 fputs("</td><td class=\"num\" align=\"right\">", fp);
549 fprintf(fp, "+%zu", ci->addcount);
550 fputs("</td><td class=\"num\" align=\"right\">", fp);
551 fprintf(fp, "-%zu", ci->delcount);
552 fputs("</td></tr>\n", fp);
556 writelog(FILE *fp, const git_oid *oid)
558 struct commitinfo *ci;
559 git_revwalk *w = NULL;
565 git_revwalk_new(&w, repo);
566 git_revwalk_push(w, oid);
567 git_revwalk_sorting(w, GIT_SORT_TIME);
568 git_revwalk_simplify_first_parent(w);
570 while (!git_revwalk_next(&id, w)) {
573 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
575 if (!(ci = commitinfo_getbyoid(&id)))
577 /* lookup stats: only required here */
578 if (commitinfo_getstats(ci) == -1)
581 writelogline(fp, ci);
583 writelogline(wcachefp, ci);
587 r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
588 if (r == -1 || (size_t)r >= sizeof(path))
589 errx(1, "path truncated: 'commit/%s.html'", ci->oid);
591 /* check if file exists if so skip it */
592 if (access(path, F_OK)) {
593 fpfile = efopen(path, "w");
594 writeheader(fpfile, ci->summary);
595 fputs("<pre>", fpfile);
596 printshowfile(fpfile, ci);
597 fputs("</pre>\n", fpfile);
612 printcommitatom(FILE *fp, struct commitinfo *ci)
614 fputs("<entry>\n", fp);
616 fprintf(fp, "<id>%s</id>\n", ci->oid);
618 fputs("<published>", fp);
619 printtimez(fp, &(ci->author->when));
620 fputs("</published>\n", fp);
623 fputs("<updated>", fp);
624 printtimez(fp, &(ci->committer->when));
625 fputs("</updated>\n", fp);
628 fputs("<title type=\"text\">", fp);
629 xmlencode(fp, ci->summary, strlen(ci->summary));
630 fputs("</title>\n", fp);
632 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
636 fputs("<author><name>", fp);
637 xmlencode(fp, ci->author->name, strlen(ci->author->name));
638 fputs("</name>\n<email>", fp);
639 xmlencode(fp, ci->author->email, strlen(ci->author->email));
640 fputs("</email>\n</author>\n", fp);
643 fputs("<content type=\"text\">", fp);
644 fprintf(fp, "commit %s\n", ci->oid);
645 if (ci->parentoid[0])
646 fprintf(fp, "parent %s\n", ci->parentoid);
648 fputs("Author: ", fp);
649 xmlencode(fp, ci->author->name, strlen(ci->author->name));
651 xmlencode(fp, ci->author->email, strlen(ci->author->email));
652 fputs(">\nDate: ", fp);
653 printtime(fp, &(ci->author->when));
658 xmlencode(fp, ci->msg, strlen(ci->msg));
660 fputs("\n</content>\n</entry>\n", fp);
666 struct commitinfo *ci;
667 git_revwalk *w = NULL;
669 size_t i, m = 100; /* last 'm' commits */
671 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
672 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
673 xmlencode(fp, strippedname, strlen(strippedname));
674 fputs(", branch HEAD</title>\n<subtitle>", fp);
675 xmlencode(fp, description, strlen(description));
676 fputs("</subtitle>\n", fp);
678 git_revwalk_new(&w, repo);
679 git_revwalk_push_head(w);
680 git_revwalk_sorting(w, GIT_SORT_TIME);
681 git_revwalk_simplify_first_parent(w);
683 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
684 if (!(ci = commitinfo_getbyoid(&id)))
686 printcommitatom(fp, ci);
691 fputs("</feed>\n", fp);
697 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
699 char tmp[PATH_MAX] = "", *d;
704 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
705 errx(1, "path truncated: '%s'", fpath);
706 if (!(d = dirname(tmp)))
711 for (p = fpath, tmp[0] = '\0'; *p; p++) {
712 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
713 errx(1, "path truncated: '../%s'", tmp);
717 fp = efopen(fpath, "w");
718 writeheader(fp, filename);
720 xmlencode(fp, filename, strlen(filename));
721 fprintf(fp, " (%juB)", (uintmax_t)filesize);
722 fputs("</p><hr/>", fp);
724 if (git_blob_is_binary((git_blob *)obj)) {
725 fputs("<p>Binary file.</p>\n", fp);
727 lc = writeblobhtml(fp, (git_blob *)obj);
740 filemode(git_filemode_t m)
742 static char mode[11];
744 memset(mode, '-', sizeof(mode) - 1);
755 else if (S_ISFIFO(m))
759 else if (S_ISSOCK(m))
764 if (m & S_IRUSR) mode[1] = 'r';
765 if (m & S_IWUSR) mode[2] = 'w';
766 if (m & S_IXUSR) mode[3] = 'x';
767 if (m & S_IRGRP) mode[4] = 'r';
768 if (m & S_IWGRP) mode[5] = 'w';
769 if (m & S_IXGRP) mode[6] = 'x';
770 if (m & S_IROTH) mode[7] = 'r';
771 if (m & S_IWOTH) mode[8] = 'w';
772 if (m & S_IXOTH) mode[9] = 'x';
774 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
775 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
776 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
782 writefilestree(FILE *fp, git_tree *tree, const char *path)
784 const git_tree_entry *entry = NULL;
785 git_submodule *module = NULL;
786 git_object *obj = NULL;
788 const char *entryname;
789 char filepath[PATH_MAX], entrypath[PATH_MAX];
793 count = git_tree_entrycount(tree);
794 for (i = 0; i < count; i++) {
795 if (!(entry = git_tree_entry_byindex(tree, i)) ||
796 !(entryname = git_tree_entry_name(entry)))
798 joinpath(entrypath, sizeof(entrypath), path, entryname);
800 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
802 if (r == -1 || (size_t)r >= sizeof(filepath))
803 errx(1, "path truncated: 'file/%s.html'", entrypath);
805 if (!git_tree_entry_to_object(&obj, repo, entry)) {
806 switch (git_object_type(obj)) {
811 ret = writefilestree(fp, (git_tree *)obj,
813 git_object_free(obj);
818 git_object_free(obj);
822 filesize = git_blob_rawsize((git_blob *)obj);
823 lc = writeblob(obj, filepath, entryname, filesize);
825 fputs("<tr><td>", fp);
826 fputs(filemode(git_tree_entry_filemode(entry)), fp);
827 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
828 xmlencode(fp, entrypath, strlen(entrypath));
829 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
831 fprintf(fp, "%dL", lc);
833 fprintf(fp, "%juB", (uintmax_t)filesize);
834 fputs("</td></tr>\n", fp);
835 git_object_free(obj);
836 } else if (!git_submodule_lookup(&module, repo, entryname)) {
837 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
839 xmlencode(fp, entrypath, strlen(entrypath));
840 git_submodule_free(module);
841 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
849 writefiles(FILE *fp, const git_oid *id)
851 git_tree *tree = NULL;
852 git_commit *commit = NULL;
855 fputs("<table id=\"files\"><thead>\n<tr>"
856 "<td><b>Mode</b></td><td><b>Name</b></td>"
857 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
858 "</tr>\n</thead><tbody>\n", fp);
860 if (!git_commit_lookup(&commit, repo, id) &&
861 !git_commit_tree(&tree, commit))
862 ret = writefilestree(fp, tree, "");
864 fputs("</tbody></table>", fp);
866 git_commit_free(commit);
873 refs_cmp(const void *v1, const void *v2)
875 git_reference *r1 = (*(git_reference **)v1);
876 git_reference *r2 = (*(git_reference **)v2);
879 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
882 return strcmp(git_reference_shorthand(r1),
883 git_reference_shorthand(r2));
889 struct commitinfo *ci;
890 const git_oid *id = NULL;
891 git_object *obj = NULL;
892 git_reference *dref = NULL, *r, *ref = NULL;
893 git_reference_iterator *it = NULL;
894 git_reference **refs = NULL;
895 size_t count, i, j, refcount;
896 const char *titles[] = { "Branches", "Tags" };
897 const char *ids[] = { "branches", "tags" };
900 if (git_reference_iterator_new(&it, repo))
903 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
904 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
906 refs[refcount] = ref;
908 git_reference_iterator_free(it);
910 /* sort by type then shorthand name */
911 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
913 for (j = 0; j < 2; j++) {
914 for (i = 0, count = 0; i < refcount; i++) {
915 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
916 !(git_reference_is_tag(refs[i]) && j == 1))
919 switch (git_reference_type(refs[i])) {
920 case GIT_REF_SYMBOLIC:
921 if (git_reference_resolve(&dref, refs[i]))
931 if (!git_reference_target(r) ||
932 git_reference_peel(&obj, r, GIT_OBJ_ANY))
934 if (!(id = git_object_id(obj)))
936 if (!(ci = commitinfo_getbyoid(id)))
939 /* print header if it has an entry (first). */
941 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
942 "<thead>\n<tr><td><b>Name</b></td>"
943 "<td><b>Last commit date</b></td>"
944 "<td><b>Author</b></td>\n</tr>\n"
950 name = git_reference_shorthand(r);
952 fputs("<tr><td>", fp);
953 xmlencode(fp, name, strlen(name));
954 fputs("</td><td>", fp);
956 printtimeshort(fp, &(ci->author->when));
957 fputs("</td><td>", fp);
959 xmlencode(fp, ci->author->name, strlen(ci->author->name));
960 fputs("</td></tr>\n", fp);
965 git_object_free(obj);
967 git_reference_free(dref);
972 fputs("</tbody></table><br/>", fp);
976 git_object_free(obj);
977 git_reference_free(dref);
979 for (i = 0; i < refcount; i++)
980 git_reference_free(refs[i]);
989 fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
994 main(int argc, char *argv[])
996 git_object *obj = NULL;
997 const git_oid *head = NULL;
998 const git_error *e = NULL;
1001 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1002 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1006 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1009 for (i = 1; i < argc; i++) {
1010 if (argv[i][0] != '-') {
1014 } else if (argv[i][1] == 'c') {
1017 cachefile = argv[++i];
1020 if (!cachefile && pledge("stdio rpath wpath cpath", NULL) == -1)
1025 if (!realpath(repodir, repodirabs))
1030 if (git_repository_open_ext(&repo, repodir,
1031 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1033 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1038 if (!git_revparse_single(&obj, repo, "HEAD"))
1039 head = git_object_id(obj);
1040 git_object_free(obj);
1042 /* use directory name as name */
1043 if ((name = strrchr(repodirabs, '/')))
1048 /* strip .git suffix */
1049 if (!(strippedname = strdup(name)))
1051 if ((p = strrchr(strippedname, '.')))
1052 if (!strcmp(p, ".git"))
1055 /* read description or .git/description */
1056 joinpath(path, sizeof(path), repodir, "description");
1057 if (!(fpread = fopen(path, "r"))) {
1058 joinpath(path, sizeof(path), repodir, ".git/description");
1059 fpread = fopen(path, "r");
1062 if (!fgets(description, sizeof(description), fpread))
1063 description[0] = '\0';
1067 /* read url or .git/url */
1068 joinpath(path, sizeof(path), repodir, "url");
1069 if (!(fpread = fopen(path, "r"))) {
1070 joinpath(path, sizeof(path), repodir, ".git/url");
1071 fpread = fopen(path, "r");
1074 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1076 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1081 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1082 git_object_type(obj) == GIT_OBJ_BLOB);
1083 git_object_free(obj);
1086 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1087 git_object_type(obj) == GIT_OBJ_BLOB);
1088 git_object_free(obj);
1090 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1091 git_object_type(obj) == GIT_OBJ_BLOB);
1092 git_object_free(obj);
1095 fp = efopen("log.html", "w");
1097 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1098 writeheader(fp, "Log");
1099 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1100 "<td><b>Commit message</b></td>"
1101 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1102 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1103 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1105 if (cachefile && head) {
1106 /* read from cache file (does not need to exist) */
1107 if ((rcachefp = fopen(cachefile, "r"))) {
1108 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1109 errx(1, "%s: no object id", cachefile);
1110 if (git_oid_fromstr(&lastoid, lastoidstr))
1111 errx(1, "%s: invalid object id", cachefile);
1114 /* write log to (temporary) cache */
1115 if ((fd = mkstemp(tmppath)) == -1)
1117 if (!(wcachefp = fdopen(fd, "w")))
1118 err(1, "fdopen: '%s'", tmppath);
1119 /* write last commit id (HEAD) */
1120 git_oid_tostr(buf, sizeof(buf), head);
1121 fprintf(wcachefp, "%s\n", buf);
1126 /* append previous log to log.html and the new cache */
1127 while (!feof(rcachefp)) {
1128 n = fread(buf, 1, sizeof(buf), rcachefp);
1129 if (ferror(rcachefp))
1131 if (fwrite(buf, 1, n, fp) != n ||
1132 fwrite(buf, 1, n, wcachefp) != n)
1143 fputs("</tbody></table>", fp);
1147 /* files for HEAD */
1148 fp = efopen("files.html", "w");
1149 writeheader(fp, "Files");
1151 writefiles(fp, head);
1155 /* summary page with branches and tags */
1156 fp = efopen("refs.html", "w");
1157 writeheader(fp, "Refs");
1163 fp = efopen("atom.xml", "w");
1167 /* rename new cache file on success */
1168 if (cachefile && head) {
1169 if (rename(tmppath, cachefile))
1170 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1171 umask((mask = umask(0)));
1172 if (chmod(cachefile,
1173 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1174 err(1, "chmod: '%s'", cachefile);
1178 git_repository_free(repo);
1179 git_libgit2_shutdown();