27 char oid[GIT_OID_HEXSZ + 1];
28 char parentoid[GIT_OID_HEXSZ + 1];
30 const git_signature *author;
31 const git_signature *committer;
38 git_tree *commit_tree;
39 git_tree *parent_tree;
45 struct deltainfo **deltas;
49 /* summary length (bytes) in the log */
50 static const unsigned summarylen = 70;
51 /* display line count or file size in file tree index */
52 static const int showlinecount = 1;
54 static git_repository *repo;
56 static const char *relpath = "";
57 static const char *repodir;
59 static char *name = "";
60 static char *strippedname = "";
61 static char description[255];
62 static char cloneurl[1024];
63 static int haslicense, hasreadme, hassubmodules;
66 static git_oid lastoid;
67 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + nul byte */
68 static FILE *rcachefp, *wcachefp;
69 static const char *cachefile;
72 #define pledge(p1,p2) 0
76 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
80 r = snprintf(buf, bufsiz, "%s%s%s",
81 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
82 if (r == -1 || (size_t)r >= bufsiz)
83 errx(1, "path truncated: '%s%s%s'",
84 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
88 deltainfo_free(struct deltainfo *di)
92 git_patch_free(di->patch);
98 commitinfo_getstats(struct commitinfo *ci)
100 struct deltainfo *di;
101 const git_diff_delta *delta;
102 const git_diff_hunk *hunk;
103 const git_diff_line *line;
104 git_patch *patch = NULL;
105 size_t ndeltas, nhunks, nhunklines;
108 ndeltas = git_diff_num_deltas(ci->diff);
109 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
112 for (i = 0; i < ndeltas; i++) {
113 if (!(di = calloc(1, sizeof(struct deltainfo))))
115 if (git_patch_from_diff(&patch, ci->diff, i)) {
116 git_patch_free(patch);
123 delta = git_patch_get_delta(patch);
125 /* skip stats for binary data */
126 if (delta->flags & GIT_DIFF_FLAG_BINARY)
129 nhunks = git_patch_num_hunks(patch);
130 for (j = 0; j < nhunks; j++) {
131 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
134 if (git_patch_get_line_in_hunk(&line, patch, j, k))
136 if (line->old_lineno == -1) {
139 } else if (line->new_lineno == -1) {
153 for (i = 0; i < ci->ndeltas; i++)
154 deltainfo_free(ci->deltas[i]);
166 commitinfo_free(struct commitinfo *ci)
173 for (i = 0; i < ci->ndeltas; i++)
174 deltainfo_free(ci->deltas[i]);
177 git_diff_free(ci->diff);
178 git_tree_free(ci->commit_tree);
179 git_tree_free(ci->parent_tree);
180 git_commit_free(ci->commit);
181 git_commit_free(ci->parent);
186 commitinfo_getbyoid(const git_oid *id)
188 struct commitinfo *ci;
189 git_diff_options opts;
191 if (!(ci = calloc(1, sizeof(struct commitinfo))))
194 if (git_commit_lookup(&(ci->commit), repo, id))
198 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
199 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
201 ci->author = git_commit_author(ci->commit);
202 ci->committer = git_commit_committer(ci->commit);
203 ci->summary = git_commit_summary(ci->commit);
204 ci->msg = git_commit_message(ci->commit);
206 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
208 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
209 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
211 ci->parent_tree = NULL;
215 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
216 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
217 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
219 if (commitinfo_getstats(ci) == -1)
231 efopen(const char *name, const char *flags)
235 if (!(fp = fopen(name, flags)))
241 /* Escape characters below as HTML 2.0 / XML 1.0. */
243 xmlencode(FILE *fp, const char *s, size_t len)
247 for (i = 0; *s && i < len; s++, i++) {
249 case '<': fputs("<", fp); break;
250 case '>': fputs(">", fp); break;
251 case '\'': fputs("'", fp); break;
252 case '&': fputs("&", fp); break;
253 case '"': fputs(""", fp); break;
254 default: fputc(*s, fp);
260 mkdirp(const char *path)
262 char tmp[PATH_MAX], *p;
264 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
265 errx(1, "path truncated: '%s'", path);
266 for (p = tmp + (tmp[0] == '/'); *p; p++) {
270 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
274 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
280 printtimez(FILE *fp, const git_time *intime)
286 t = (time_t)intime->time;
287 if (!(intm = gmtime(&t)))
289 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
294 printtime(FILE *fp, const git_time *intime)
300 t = (time_t)intime->time + (intime->offset * 60);
301 if (!(intm = gmtime(&t)))
303 strftime(out, sizeof(out), "%a %b %e %H:%M:%S", intm);
304 if (intime->offset < 0)
305 fprintf(fp, "%s -%02d%02d", out,
306 -(intime->offset) / 60, -(intime->offset) % 60);
308 fprintf(fp, "%s +%02d%02d", out,
309 intime->offset / 60, intime->offset % 60);
313 printtimeshort(FILE *fp, const git_time *intime)
319 t = (time_t)intime->time;
320 if (!(intm = gmtime(&t)))
322 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
327 writeheader(FILE *fp, const char *title)
329 fputs("<!DOCTYPE html>\n"
331 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
333 xmlencode(fp, title, strlen(title));
334 if (title[0] && strippedname[0])
336 xmlencode(fp, strippedname, strlen(strippedname));
339 xmlencode(fp, description, strlen(description));
340 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
341 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
343 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
344 fputs("</head>\n<body>\n<table><tr><td>", fp);
345 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
347 fputs("</td><td><h1>", fp);
348 xmlencode(fp, strippedname, strlen(strippedname));
349 fputs("</h1><span class=\"desc\">", fp);
350 xmlencode(fp, description, strlen(description));
351 fputs("</span></td></tr>", fp);
353 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
354 xmlencode(fp, cloneurl, strlen(cloneurl));
356 xmlencode(fp, cloneurl, strlen(cloneurl));
357 fputs("</a></td></tr>", fp);
359 fputs("<tr><td></td><td>\n", fp);
360 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
361 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
362 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
364 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
366 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
368 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
369 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
373 writefooter(FILE *fp)
375 fputs("</div>\n</body>\n</html>\n", fp);
379 writeblobhtml(FILE *fp, const git_blob *blob)
381 size_t n = 0, i, prev;
382 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
383 const char *s = git_blob_rawcontent(blob);
384 git_off_t len = git_blob_rawsize(blob);
386 fputs("<pre id=\"blob\">\n", fp);
389 for (i = 0, prev = 0; i < (size_t)len; i++) {
393 fprintf(fp, nfmt, n, n, n);
394 xmlencode(fp, &s[prev], i - prev + 1);
398 if ((len - prev) > 0) {
400 fprintf(fp, nfmt, n, n, n);
401 xmlencode(fp, &s[prev], len - prev);
405 fputs("</pre>\n", fp);
411 printcommit(FILE *fp, struct commitinfo *ci)
413 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
414 relpath, ci->oid, ci->oid);
416 if (ci->parentoid[0])
417 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
418 relpath, ci->parentoid, ci->parentoid);
421 fputs("<b>Author:</b> ", fp);
422 xmlencode(fp, ci->author->name, strlen(ci->author->name));
423 fputs(" <<a href=\"mailto:", fp);
424 xmlencode(fp, ci->author->email, strlen(ci->author->email));
426 xmlencode(fp, ci->author->email, strlen(ci->author->email));
427 fputs("</a>>\n<b>Date:</b> ", fp);
428 printtime(fp, &(ci->author->when));
433 xmlencode(fp, ci->msg, strlen(ci->msg));
439 printshowfile(FILE *fp, struct commitinfo *ci)
441 const git_diff_delta *delta;
442 const git_diff_hunk *hunk;
443 const git_diff_line *line;
445 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
453 if (ci->filecount > 1000 ||
454 ci->ndeltas > 1000 ||
455 ci->addcount > 100000 ||
456 ci->delcount > 100000) {
457 fputs("Diff is too large, output suppressed.\n", fp);
462 fputs("<b>Diffstat:</b>\n<table>", fp);
463 for (i = 0; i < ci->ndeltas; i++) {
464 delta = git_patch_get_delta(ci->deltas[i]->patch);
465 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
466 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
467 if (strcmp(delta->old_file.path, delta->new_file.path)) {
468 fputs(" -> ", fp);
469 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
472 add = ci->deltas[i]->addcount;
473 del = ci->deltas[i]->delcount;
475 total = sizeof(linestr) - 2;
476 if (changed > total) {
478 add = ((float)total / changed * add) + 1;
480 del = ((float)total / changed * del) + 1;
482 memset(&linestr, '+', add);
483 memset(&linestr[add], '-', del);
485 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
486 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
487 fwrite(&linestr, 1, add, fp);
488 fputs("</span><span class=\"d\">", fp);
489 fwrite(&linestr[add], 1, del, fp);
490 fputs("</span></td></tr>\n", fp);
492 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
493 ci->filecount, ci->filecount == 1 ? "" : "s",
494 ci->addcount, ci->addcount == 1 ? "" : "s",
495 ci->delcount, ci->delcount == 1 ? "" : "s");
499 for (i = 0; i < ci->ndeltas; i++) {
500 patch = ci->deltas[i]->patch;
501 delta = git_patch_get_delta(patch);
502 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",
503 i, relpath, delta->old_file.path, delta->old_file.path,
504 relpath, delta->new_file.path, delta->new_file.path);
506 /* check binary data */
507 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
508 fputs("Binary files differ.\n", fp);
512 nhunks = git_patch_num_hunks(patch);
513 for (j = 0; j < nhunks; j++) {
514 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
517 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
518 xmlencode(fp, hunk->header, hunk->header_len);
522 if (git_patch_get_line_in_hunk(&line, patch, j, k))
524 if (line->old_lineno == -1)
525 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
527 else if (line->new_lineno == -1)
528 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
532 xmlencode(fp, line->content, line->content_len);
533 if (line->old_lineno == -1 || line->new_lineno == -1)
541 writelogline(FILE *fp, struct commitinfo *ci)
545 fputs("<tr><td>", fp);
547 printtimeshort(fp, &(ci->author->when));
548 fputs("</td><td>", fp);
550 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
551 if ((len = strlen(ci->summary)) > summarylen) {
552 xmlencode(fp, ci->summary, summarylen - 1);
555 xmlencode(fp, ci->summary, len);
559 fputs("</td><td>", fp);
561 xmlencode(fp, ci->author->name, strlen(ci->author->name));
562 fputs("</td><td class=\"num\" align=\"right\">", fp);
563 fprintf(fp, "%zu", ci->filecount);
564 fputs("</td><td class=\"num\" align=\"right\">", fp);
565 fprintf(fp, "+%zu", ci->addcount);
566 fputs("</td><td class=\"num\" align=\"right\">", fp);
567 fprintf(fp, "-%zu", ci->delcount);
568 fputs("</td></tr>\n", fp);
572 writelog(FILE *fp, const git_oid *oid)
574 struct commitinfo *ci;
575 git_revwalk *w = NULL;
581 git_revwalk_new(&w, repo);
582 git_revwalk_push(w, oid);
583 git_revwalk_sorting(w, GIT_SORT_TIME);
584 git_revwalk_simplify_first_parent(w);
586 while (!git_revwalk_next(&id, w)) {
589 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
591 if (!(ci = commitinfo_getbyoid(&id)))
594 writelogline(fp, ci);
596 writelogline(wcachefp, ci);
600 r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
601 if (r == -1 || (size_t)r >= sizeof(path))
602 errx(1, "path truncated: 'commit/%s.html'", ci->oid);
604 /* check if file exists if so skip it */
605 if (access(path, F_OK)) {
606 fpfile = efopen(path, "w");
607 writeheader(fpfile, ci->summary);
608 fputs("<pre>", fpfile);
609 printshowfile(fpfile, ci);
610 fputs("</pre>\n", fpfile);
624 printcommitatom(FILE *fp, struct commitinfo *ci)
626 fputs("<entry>\n", fp);
628 fprintf(fp, "<id>%s</id>\n", ci->oid);
630 fputs("<published>", fp);
631 printtimez(fp, &(ci->author->when));
632 fputs("</published>\n", fp);
635 fputs("<updated>", fp);
636 printtimez(fp, &(ci->committer->when));
637 fputs("</updated>\n", fp);
640 fputs("<title type=\"text\">", fp);
641 xmlencode(fp, ci->summary, strlen(ci->summary));
642 fputs("</title>\n", fp);
644 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
648 fputs("<author><name>", fp);
649 xmlencode(fp, ci->author->name, strlen(ci->author->name));
650 fputs("</name>\n<email>", fp);
651 xmlencode(fp, ci->author->email, strlen(ci->author->email));
652 fputs("</email>\n</author>\n", fp);
655 fputs("<content type=\"text\">", fp);
656 fprintf(fp, "commit %s\n", ci->oid);
657 if (ci->parentoid[0])
658 fprintf(fp, "parent %s\n", ci->parentoid);
660 fputs("Author: ", fp);
661 xmlencode(fp, ci->author->name, strlen(ci->author->name));
663 xmlencode(fp, ci->author->email, strlen(ci->author->email));
664 fputs(">\nDate: ", fp);
665 printtime(fp, &(ci->author->when));
670 xmlencode(fp, ci->msg, strlen(ci->msg));
672 fputs("\n</content>\n</entry>\n", fp);
678 struct commitinfo *ci;
679 git_revwalk *w = NULL;
681 size_t i, m = 100; /* last 'm' commits */
683 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
684 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
685 xmlencode(fp, strippedname, strlen(strippedname));
686 fputs(", branch HEAD</title>\n<subtitle>", fp);
687 xmlencode(fp, description, strlen(description));
688 fputs("</subtitle>\n", fp);
690 git_revwalk_new(&w, repo);
691 git_revwalk_push_head(w);
692 git_revwalk_sorting(w, GIT_SORT_TIME);
693 git_revwalk_simplify_first_parent(w);
695 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
696 if (!(ci = commitinfo_getbyoid(&id)))
698 printcommitatom(fp, ci);
703 fputs("</feed>\n", fp);
709 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
711 char tmp[PATH_MAX] = "", *d;
716 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
717 errx(1, "path truncated: '%s'", fpath);
718 if (!(d = dirname(tmp)))
723 for (p = fpath, tmp[0] = '\0'; *p; p++) {
724 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
725 errx(1, "path truncated: '../%s'", tmp);
729 fp = efopen(fpath, "w");
730 writeheader(fp, filename);
732 xmlencode(fp, filename, strlen(filename));
733 fprintf(fp, " (%juB)", (uintmax_t)filesize);
734 fputs("</p><hr/>", fp);
736 if (git_blob_is_binary((git_blob *)obj)) {
737 fputs("<p>Binary file.</p>\n", fp);
739 lc = writeblobhtml(fp, (git_blob *)obj);
752 filemode(git_filemode_t m)
754 static char mode[11];
756 memset(mode, '-', sizeof(mode) - 1);
767 else if (S_ISFIFO(m))
771 else if (S_ISSOCK(m))
776 if (m & S_IRUSR) mode[1] = 'r';
777 if (m & S_IWUSR) mode[2] = 'w';
778 if (m & S_IXUSR) mode[3] = 'x';
779 if (m & S_IRGRP) mode[4] = 'r';
780 if (m & S_IWGRP) mode[5] = 'w';
781 if (m & S_IXGRP) mode[6] = 'x';
782 if (m & S_IROTH) mode[7] = 'r';
783 if (m & S_IWOTH) mode[8] = 'w';
784 if (m & S_IXOTH) mode[9] = 'x';
786 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
787 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
788 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
794 writefilestree(FILE *fp, git_tree *tree, const char *path)
796 const git_tree_entry *entry = NULL;
797 git_submodule *module = NULL;
798 git_object *obj = NULL;
800 const char *entryname;
801 char filepath[PATH_MAX], entrypath[PATH_MAX];
805 count = git_tree_entrycount(tree);
806 for (i = 0; i < count; i++) {
807 if (!(entry = git_tree_entry_byindex(tree, i)) ||
808 !(entryname = git_tree_entry_name(entry)))
810 joinpath(entrypath, sizeof(entrypath), path, entryname);
812 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
814 if (r == -1 || (size_t)r >= sizeof(filepath))
815 errx(1, "path truncated: 'file/%s.html'", entrypath);
817 if (!git_tree_entry_to_object(&obj, repo, entry)) {
818 switch (git_object_type(obj)) {
823 ret = writefilestree(fp, (git_tree *)obj,
825 git_object_free(obj);
830 git_object_free(obj);
834 filesize = git_blob_rawsize((git_blob *)obj);
835 lc = writeblob(obj, filepath, entryname, filesize);
837 fputs("<tr><td>", fp);
838 fputs(filemode(git_tree_entry_filemode(entry)), fp);
839 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
840 xmlencode(fp, entrypath, strlen(entrypath));
841 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
842 if (showlinecount && lc > 0)
843 fprintf(fp, "%dL", lc);
845 fprintf(fp, "%juB", (uintmax_t)filesize);
846 fputs("</td></tr>\n", fp);
847 git_object_free(obj);
848 } else if (!git_submodule_lookup(&module, repo, entryname)) {
849 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
851 xmlencode(fp, entrypath, strlen(entrypath));
852 git_submodule_free(module);
853 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
861 writefiles(FILE *fp, const git_oid *id)
863 git_tree *tree = NULL;
864 git_commit *commit = NULL;
867 fputs("<table id=\"files\"><thead>\n<tr>"
868 "<td><b>Mode</b></td><td><b>Name</b></td>"
869 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
870 "</tr>\n</thead><tbody>\n", fp);
872 if (!git_commit_lookup(&commit, repo, id) &&
873 !git_commit_tree(&tree, commit))
874 ret = writefilestree(fp, tree, "");
876 fputs("</tbody></table>", fp);
878 git_commit_free(commit);
885 refs_cmp(const void *v1, const void *v2)
887 git_reference *r1 = (*(git_reference **)v1);
888 git_reference *r2 = (*(git_reference **)v2);
891 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
894 return strcmp(git_reference_shorthand(r1),
895 git_reference_shorthand(r2));
901 struct commitinfo *ci;
902 const git_oid *id = NULL;
903 git_object *obj = NULL;
904 git_reference *dref = NULL, *r, *ref = NULL;
905 git_reference_iterator *it = NULL;
906 git_reference **refs = NULL;
907 size_t count, i, j, refcount;
908 const char *titles[] = { "Branches", "Tags" };
909 const char *ids[] = { "branches", "tags" };
912 if (git_reference_iterator_new(&it, repo))
915 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
916 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
918 refs[refcount] = ref;
920 git_reference_iterator_free(it);
922 /* sort by type then shorthand name */
923 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
925 for (j = 0; j < 2; j++) {
926 for (i = 0, count = 0; i < refcount; i++) {
927 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
928 !(git_reference_is_tag(refs[i]) && j == 1))
931 switch (git_reference_type(refs[i])) {
932 case GIT_REF_SYMBOLIC:
933 if (git_reference_resolve(&dref, refs[i]))
943 if (!git_reference_target(r) ||
944 git_reference_peel(&obj, r, GIT_OBJ_ANY))
946 if (!(id = git_object_id(obj)))
948 if (!(ci = commitinfo_getbyoid(id)))
951 /* print header if it has an entry (first). */
953 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
954 "<thead>\n<tr><td><b>Name</b></td>"
955 "<td><b>Last commit date</b></td>"
956 "<td><b>Author</b></td>\n</tr>\n"
962 name = git_reference_shorthand(r);
964 fputs("<tr><td>", fp);
965 xmlencode(fp, name, strlen(name));
966 fputs("</td><td>", fp);
968 printtimeshort(fp, &(ci->author->when));
969 fputs("</td><td>", fp);
971 xmlencode(fp, ci->author->name, strlen(ci->author->name));
972 fputs("</td></tr>\n", fp);
977 git_object_free(obj);
979 git_reference_free(dref);
984 fputs("</tbody></table><br/>", fp);
988 git_object_free(obj);
989 git_reference_free(dref);
991 for (i = 0; i < refcount; i++)
992 git_reference_free(refs[i]);
1001 fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
1006 main(int argc, char *argv[])
1008 git_object *obj = NULL;
1009 const git_oid *head = NULL;
1010 const git_error *e = NULL;
1012 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1013 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1017 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1020 for (i = 1; i < argc; i++) {
1021 if (argv[i][0] != '-') {
1025 } else if (argv[i][1] == 'c') {
1028 cachefile = argv[++i];
1034 if (!realpath(repodir, repodirabs))
1039 if (git_repository_open_ext(&repo, repodir,
1040 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1042 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1047 if (!git_revparse_single(&obj, repo, "HEAD"))
1048 head = git_object_id(obj);
1049 git_object_free(obj);
1051 /* don't cache if there is no HEAD */
1055 /* use directory name as name */
1056 if ((name = strrchr(repodirabs, '/')))
1061 /* strip .git suffix */
1062 if (!(strippedname = strdup(name)))
1064 if ((p = strrchr(strippedname, '.')))
1065 if (!strcmp(p, ".git"))
1068 /* read description or .git/description */
1069 joinpath(path, sizeof(path), repodir, "description");
1070 if (!(fpread = fopen(path, "r"))) {
1071 joinpath(path, sizeof(path), repodir, ".git/description");
1072 fpread = fopen(path, "r");
1075 if (!fgets(description, sizeof(description), fpread))
1076 description[0] = '\0';
1080 /* read url or .git/url */
1081 joinpath(path, sizeof(path), repodir, "url");
1082 if (!(fpread = fopen(path, "r"))) {
1083 joinpath(path, sizeof(path), repodir, ".git/url");
1084 fpread = fopen(path, "r");
1087 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1089 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1094 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1095 git_object_type(obj) == GIT_OBJ_BLOB);
1096 git_object_free(obj);
1099 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1100 git_object_type(obj) == GIT_OBJ_BLOB);
1101 git_object_free(obj);
1103 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1104 git_object_type(obj) == GIT_OBJ_BLOB);
1105 git_object_free(obj);
1108 fp = efopen("log.html", "w");
1110 mkdir("commit", 0755);
1111 writeheader(fp, "Log");
1112 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1113 "<td><b>Commit message</b></td>"
1114 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1115 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1116 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1119 /* read from cache file (does not need to exist) */
1120 if ((rcachefp = fopen(cachefile, "r"))) {
1121 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1122 errx(1, "%s: no object id", cachefile);
1123 if (git_oid_fromstr(&lastoid, lastoidstr))
1124 errx(1, "%s: invalid object id", cachefile);
1127 /* write log to (temporary) cache */
1128 if ((fd = mkstemp(tmppath)) == -1)
1130 if (!(wcachefp = fdopen(fd, "w")))
1132 /* write last commit id (HEAD) */
1133 git_oid_tostr(buf, sizeof(buf), head);
1134 fprintf(wcachefp, "%s\n", buf);
1139 /* append previous log to log.html and the new cache */
1140 while (!feof(rcachefp)) {
1141 n = fread(buf, 1, sizeof(buf), rcachefp);
1142 if (ferror(rcachefp))
1144 if (fwrite(buf, 1, n, fp) != n ||
1145 fwrite(buf, 1, n, wcachefp) != n)
1156 fputs("</tbody></table>", fp);
1160 /* files for HEAD */
1161 fp = efopen("files.html", "w");
1162 writeheader(fp, "Files");
1164 writefiles(fp, head);
1168 /* summary page with branches and tags */
1169 fp = efopen("refs.html", "w");
1170 writeheader(fp, "Refs");
1176 fp = efopen("atom.xml", "w");
1180 /* rename new cache file on success */
1181 if (cachefile && rename(tmppath, cachefile))
1182 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1185 git_repository_free(repo);
1186 git_libgit2_shutdown();