29 char oid[GIT_OID_HEXSZ + 1];
30 char parentoid[GIT_OID_HEXSZ + 1];
32 const git_signature *author;
33 const git_signature *committer;
40 git_tree *commit_tree;
41 git_tree *parent_tree;
47 struct deltainfo **deltas;
51 static git_repository *repo;
53 static const char *relpath = "";
54 static const char *repodir;
56 static char *name = "";
57 static char *strippedname = "";
58 static char description[255];
59 static char cloneurl[1024];
60 static char *submodules;
61 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
63 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
65 static long long nlogcommits = -1; /* < 0 indicates not used */
68 static git_oid lastoid;
69 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
70 static FILE *rcachefp, *wcachefp;
71 static const char *cachefile;
74 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
78 r = snprintf(buf, bufsiz, "%s%s%s",
79 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
80 if (r < 0 || (size_t)r >= bufsiz)
81 errx(1, "path truncated: '%s%s%s'",
82 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
86 deltainfo_free(struct deltainfo *di)
90 git_patch_free(di->patch);
91 memset(di, 0, sizeof(*di));
96 commitinfo_getstats(struct commitinfo *ci)
99 git_diff_options opts;
100 git_diff_find_options fopts;
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 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
110 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
111 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
113 ci->parent_tree = NULL;
117 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
118 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
119 GIT_DIFF_IGNORE_SUBMODULES |
120 GIT_DIFF_INCLUDE_TYPECHANGE;
121 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
124 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
126 /* find renames and copies, exact matches (no heuristic) for renames. */
127 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
128 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
129 if (git_diff_find_similar(ci->diff, &fopts))
132 ndeltas = git_diff_num_deltas(ci->diff);
133 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
136 for (i = 0; i < ndeltas; i++) {
137 if (git_patch_from_diff(&patch, ci->diff, i))
140 if (!(di = calloc(1, sizeof(struct deltainfo))))
145 delta = git_patch_get_delta(patch);
147 /* skip stats for binary data */
148 if (delta->flags & GIT_DIFF_FLAG_BINARY)
151 nhunks = git_patch_num_hunks(patch);
152 for (j = 0; j < nhunks; j++) {
153 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
156 if (git_patch_get_line_in_hunk(&line, patch, j, k))
158 if (line->old_lineno == -1) {
161 } else if (line->new_lineno == -1) {
174 git_diff_free(ci->diff);
176 git_tree_free(ci->commit_tree);
177 ci->commit_tree = NULL;
178 git_tree_free(ci->parent_tree);
179 ci->parent_tree = NULL;
180 git_commit_free(ci->parent);
184 for (i = 0; i < ci->ndeltas; i++)
185 deltainfo_free(ci->deltas[i]);
197 commitinfo_free(struct commitinfo *ci)
204 for (i = 0; i < ci->ndeltas; i++)
205 deltainfo_free(ci->deltas[i]);
208 git_diff_free(ci->diff);
209 git_tree_free(ci->commit_tree);
210 git_tree_free(ci->parent_tree);
211 git_commit_free(ci->commit);
212 git_commit_free(ci->parent);
213 memset(ci, 0, sizeof(*ci));
218 commitinfo_getbyoid(const git_oid *id)
220 struct commitinfo *ci;
222 if (!(ci = calloc(1, sizeof(struct commitinfo))))
225 if (git_commit_lookup(&(ci->commit), repo, id))
229 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
230 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
232 ci->author = git_commit_author(ci->commit);
233 ci->committer = git_commit_committer(ci->commit);
234 ci->summary = git_commit_summary(ci->commit);
235 ci->msg = git_commit_message(ci->commit);
246 efopen(const char *name, const char *flags)
250 if (!(fp = fopen(name, flags)))
251 err(1, "fopen: '%s'", name);
256 /* Escape characters below as HTML 2.0 / XML 1.0. */
258 xmlencode(FILE *fp, const char *s, size_t len)
262 for (i = 0; *s && i < len; s++, i++) {
264 case '<': fputs("<", fp); break;
265 case '>': fputs(">", fp); break;
266 case '\'': fputs("'", fp); break;
267 case '&': fputs("&", fp); break;
268 case '"': fputs(""", fp); break;
269 default: fputc(*s, fp);
275 mkdirp(const char *path)
277 char tmp[PATH_MAX], *p;
279 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
280 errx(1, "path truncated: '%s'", path);
281 for (p = tmp + (tmp[0] == '/'); *p; p++) {
285 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
289 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
295 printtimez(FILE *fp, const git_time *intime)
301 t = (time_t)intime->time;
302 if (!(intm = gmtime(&t)))
304 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
309 printtime(FILE *fp, const git_time *intime)
315 t = (time_t)intime->time + (intime->offset * 60);
316 if (!(intm = gmtime(&t)))
318 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
319 if (intime->offset < 0)
320 fprintf(fp, "%s -%02d%02d", out,
321 -(intime->offset) / 60, -(intime->offset) % 60);
323 fprintf(fp, "%s +%02d%02d", out,
324 intime->offset / 60, intime->offset % 60);
328 printtimeshort(FILE *fp, const git_time *intime)
334 t = (time_t)intime->time;
335 if (!(intm = gmtime(&t)))
337 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
342 writeheader(FILE *fp, const char *title)
344 fputs("<!DOCTYPE html>\n"
346 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
347 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
349 xmlencode(fp, title, strlen(title));
350 if (title[0] && strippedname[0])
352 xmlencode(fp, strippedname, strlen(strippedname));
355 xmlencode(fp, description, strlen(description));
356 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
357 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
359 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
360 fputs("</head>\n<body>\n<table><tr><td>", fp);
361 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
363 fputs("</td><td><h1>", fp);
364 xmlencode(fp, strippedname, strlen(strippedname));
365 fputs("</h1><span class=\"desc\">", fp);
366 xmlencode(fp, description, strlen(description));
367 fputs("</span></td></tr>", fp);
369 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
370 xmlencode(fp, cloneurl, strlen(cloneurl));
372 xmlencode(fp, cloneurl, strlen(cloneurl));
373 fputs("</a></span></td></tr>", fp);
375 fputs("<tr><td></td><td>\n", fp);
376 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
377 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
378 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
380 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
381 relpath, submodules);
383 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
386 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
388 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
392 writefooter(FILE *fp)
394 fputs("</div>\n</body>\n</html>\n", fp);
398 writeblobhtml(FILE *fp, const git_blob *blob)
400 size_t n = 0, i, prev;
401 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a><span class=\"loc\">";
402 const char *s = git_blob_rawcontent(blob);
403 git_off_t len = git_blob_rawsize(blob);
405 fputs("<pre id=\"blob\">\n", fp);
408 for (i = 0, prev = 0; i < (size_t)len; i++) {
412 fprintf(fp, nfmt, n, n, n);
413 xmlencode(fp, &s[prev], i - prev + 1);
414 fprintf(fp, "</span>");
418 if ((len - prev) > 0) {
420 fprintf(fp, nfmt, n, n, n);
421 xmlencode(fp, &s[prev], len - prev);
422 fprintf(fp, "</span>");
426 fputs("</pre>\n", fp);
432 printcommit(FILE *fp, struct commitinfo *ci)
434 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
435 relpath, ci->oid, ci->oid);
437 if (ci->parentoid[0])
438 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
439 relpath, ci->parentoid, ci->parentoid);
442 fputs("<b>Author:</b> ", fp);
443 xmlencode(fp, ci->author->name, strlen(ci->author->name));
444 fputs(" <<a href=\"mailto:", fp);
445 xmlencode(fp, ci->author->email, strlen(ci->author->email));
447 xmlencode(fp, ci->author->email, strlen(ci->author->email));
448 fputs("</a>>\n<b>Date:</b> ", fp);
449 printtime(fp, &(ci->author->when));
454 xmlencode(fp, ci->msg, strlen(ci->msg));
460 printshowfile(FILE *fp, struct commitinfo *ci)
462 const git_diff_delta *delta;
463 const git_diff_hunk *hunk;
464 const git_diff_line *line;
466 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
475 if (ci->filecount > 1000 ||
476 ci->ndeltas > 1000 ||
477 ci->addcount > 100000 ||
478 ci->delcount > 100000) {
479 fputs("Diff is too large, output suppressed.\n", fp);
484 fputs("<b>Diffstat:</b>\n<table>", fp);
485 for (i = 0; i < ci->ndeltas; i++) {
486 delta = git_patch_get_delta(ci->deltas[i]->patch);
488 switch (delta->status) {
489 case GIT_DELTA_ADDED: c = 'A'; break;
490 case GIT_DELTA_COPIED: c = 'C'; break;
491 case GIT_DELTA_DELETED: c = 'D'; break;
492 case GIT_DELTA_MODIFIED: c = 'M'; break;
493 case GIT_DELTA_RENAMED: c = 'R'; break;
494 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
495 default: c = ' '; break;
498 fprintf(fp, "<tr><td>%c", c);
500 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
502 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
503 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
504 if (strcmp(delta->old_file.path, delta->new_file.path)) {
505 fputs(" -> ", fp);
506 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
509 add = ci->deltas[i]->addcount;
510 del = ci->deltas[i]->delcount;
512 total = sizeof(linestr) - 2;
513 if (changed > total) {
515 add = ((float)total / changed * add) + 1;
517 del = ((float)total / changed * del) + 1;
519 memset(&linestr, '+', add);
520 memset(&linestr[add], '-', del);
522 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
523 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
524 fwrite(&linestr, 1, add, fp);
525 fputs("</span><span class=\"d\">", fp);
526 fwrite(&linestr[add], 1, del, fp);
527 fputs("</span></td></tr>\n", fp);
529 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
530 ci->filecount, ci->filecount == 1 ? "" : "s",
531 ci->addcount, ci->addcount == 1 ? "" : "s",
532 ci->delcount, ci->delcount == 1 ? "" : "s");
536 for (i = 0; i < ci->ndeltas; i++) {
537 patch = ci->deltas[i]->patch;
538 delta = git_patch_get_delta(patch);
539 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
540 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
541 fputs(".html\">", fp);
542 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
543 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
544 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
545 fprintf(fp, ".html\">");
546 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
547 fprintf(fp, "</a></b>\n");
549 /* check binary data */
550 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
551 fputs("Binary files differ.\n", fp);
555 nhunks = git_patch_num_hunks(patch);
556 for (j = 0; j < nhunks; j++) {
557 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
560 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
561 xmlencode(fp, hunk->header, hunk->header_len);
565 if (git_patch_get_line_in_hunk(&line, patch, j, k))
567 if (line->old_lineno == -1)
568 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
570 else if (line->new_lineno == -1)
571 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
575 xmlencode(fp, line->content, line->content_len);
576 if (line->old_lineno == -1 || line->new_lineno == -1)
584 writelogline(FILE *fp, struct commitinfo *ci)
586 fputs("<tr><td>", fp);
588 printtimeshort(fp, &(ci->author->when));
589 fputs("</td><td>", fp);
591 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
592 xmlencode(fp, ci->summary, strlen(ci->summary));
595 fputs("</td><td>", fp);
597 xmlencode(fp, ci->author->name, strlen(ci->author->name));
598 fputs("</td><td class=\"num\" align=\"right\">", fp);
599 fprintf(fp, "%zu", ci->filecount);
600 fputs("</td><td class=\"num\" align=\"right\">", fp);
601 fprintf(fp, "+%zu", ci->addcount);
602 fputs("</td><td class=\"num\" align=\"right\">", fp);
603 fprintf(fp, "-%zu", ci->delcount);
604 fputs("</td></tr>\n", fp);
608 writelog(FILE *fp, const git_oid *oid)
610 struct commitinfo *ci;
611 git_revwalk *w = NULL;
613 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
617 git_revwalk_new(&w, repo);
618 git_revwalk_push(w, oid);
619 git_revwalk_simplify_first_parent(w);
621 while (!git_revwalk_next(&id, w)) {
624 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
627 git_oid_tostr(oidstr, sizeof(oidstr), &id);
628 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
629 if (r < 0 || (size_t)r >= sizeof(path))
630 errx(1, "path truncated: 'commit/%s.html'", oidstr);
631 r = access(path, F_OK);
633 /* optimization: if there are no log lines to write and
634 the commit file already exists: skip the diffstat */
635 if (!nlogcommits && !r)
638 if (!(ci = commitinfo_getbyoid(&id)))
640 /* diffstat: for stagit HTML required for the log.html line */
641 if (commitinfo_getstats(ci) == -1)
644 if (nlogcommits < 0) {
645 writelogline(fp, ci);
646 } else if (nlogcommits > 0) {
647 writelogline(fp, ci);
649 if (!nlogcommits && ci->parentoid[0])
650 fputs("<tr><td></td><td colspan=\"5\">"
651 "More commits remaining [...]</td>"
656 writelogline(wcachefp, ci);
658 /* check if file exists if so skip it */
661 fpfile = efopen(path, "w");
662 writeheader(fpfile, ci->summary);
663 fputs("<pre>", fpfile);
664 printshowfile(fpfile, ci);
665 fputs("</pre>\n", fpfile);
680 printcommitatom(FILE *fp, struct commitinfo *ci)
682 fputs("<entry>\n", fp);
684 fprintf(fp, "<id>%s</id>\n", ci->oid);
686 fputs("<published>", fp);
687 printtimez(fp, &(ci->author->when));
688 fputs("</published>\n", fp);
691 fputs("<updated>", fp);
692 printtimez(fp, &(ci->committer->when));
693 fputs("</updated>\n", fp);
696 fputs("<title type=\"text\">", fp);
697 xmlencode(fp, ci->summary, strlen(ci->summary));
698 fputs("</title>\n", fp);
700 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
704 fputs("<author>\n<name>", fp);
705 xmlencode(fp, ci->author->name, strlen(ci->author->name));
706 fputs("</name>\n<email>", fp);
707 xmlencode(fp, ci->author->email, strlen(ci->author->email));
708 fputs("</email>\n</author>\n", fp);
711 fputs("<content type=\"text\">", fp);
712 fprintf(fp, "commit %s\n", ci->oid);
713 if (ci->parentoid[0])
714 fprintf(fp, "parent %s\n", ci->parentoid);
716 fputs("Author: ", fp);
717 xmlencode(fp, ci->author->name, strlen(ci->author->name));
719 xmlencode(fp, ci->author->email, strlen(ci->author->email));
720 fputs(">\nDate: ", fp);
721 printtime(fp, &(ci->author->when));
726 xmlencode(fp, ci->msg, strlen(ci->msg));
728 fputs("\n</content>\n</entry>\n", fp);
734 struct commitinfo *ci;
735 git_revwalk *w = NULL;
737 size_t i, m = 100; /* last 'm' commits */
739 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
740 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
741 xmlencode(fp, strippedname, strlen(strippedname));
742 fputs(", branch HEAD</title>\n<subtitle>", fp);
743 xmlencode(fp, description, strlen(description));
744 fputs("</subtitle>\n", fp);
746 git_revwalk_new(&w, repo);
747 git_revwalk_push_head(w);
748 git_revwalk_simplify_first_parent(w);
750 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
751 if (!(ci = commitinfo_getbyoid(&id)))
753 printcommitatom(fp, ci);
758 fputs("</feed>\n", fp);
764 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
766 char tmp[PATH_MAX] = "", *d;
771 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
772 errx(1, "path truncated: '%s'", fpath);
773 if (!(d = dirname(tmp)))
778 for (p = fpath, tmp[0] = '\0'; *p; p++) {
779 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
780 errx(1, "path truncated: '../%s'", tmp);
784 fp = efopen(fpath, "w");
785 writeheader(fp, filename);
787 xmlencode(fp, filename, strlen(filename));
788 fprintf(fp, " (%juB)", (uintmax_t)filesize);
789 fputs("</p><hr/>", fp);
791 if (git_blob_is_binary((git_blob *)obj)) {
792 fputs("<p>Binary file.</p>\n", fp);
794 lc = writeblobhtml(fp, (git_blob *)obj);
807 filemode(git_filemode_t m)
809 static char mode[11];
811 memset(mode, '-', sizeof(mode) - 1);
822 else if (S_ISFIFO(m))
826 else if (S_ISSOCK(m))
831 if (m & S_IRUSR) mode[1] = 'r';
832 if (m & S_IWUSR) mode[2] = 'w';
833 if (m & S_IXUSR) mode[3] = 'x';
834 if (m & S_IRGRP) mode[4] = 'r';
835 if (m & S_IWGRP) mode[5] = 'w';
836 if (m & S_IXGRP) mode[6] = 'x';
837 if (m & S_IROTH) mode[7] = 'r';
838 if (m & S_IWOTH) mode[8] = 'w';
839 if (m & S_IXOTH) mode[9] = 'x';
841 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
842 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
843 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
849 writefilestree(FILE *fp, git_tree *tree, const char *path)
851 const git_tree_entry *entry = NULL;
852 git_submodule *module = NULL;
853 git_object *obj = NULL;
855 const char *entryname;
856 char filepath[PATH_MAX], entrypath[PATH_MAX];
860 count = git_tree_entrycount(tree);
861 for (i = 0; i < count; i++) {
862 if (!(entry = git_tree_entry_byindex(tree, i)) ||
863 !(entryname = git_tree_entry_name(entry)))
865 joinpath(entrypath, sizeof(entrypath), path, entryname);
867 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
869 if (r < 0 || (size_t)r >= sizeof(filepath))
870 errx(1, "path truncated: 'file/%s.html'", entrypath);
872 if (!git_tree_entry_to_object(&obj, repo, entry)) {
873 switch (git_object_type(obj)) {
878 ret = writefilestree(fp, (git_tree *)obj,
880 git_object_free(obj);
885 git_object_free(obj);
889 filesize = git_blob_rawsize((git_blob *)obj);
890 lc = writeblob(obj, filepath, entryname, filesize);
892 fputs("<tr><td>", fp);
893 fputs(filemode(git_tree_entry_filemode(entry)), fp);
894 fprintf(fp, "</td><td><a href=\"%s", relpath);
895 xmlencode(fp, filepath, strlen(filepath));
897 xmlencode(fp, entrypath, strlen(entrypath));
898 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
900 fprintf(fp, "%dL", lc);
902 fprintf(fp, "%juB", (uintmax_t)filesize);
903 fputs("</td></tr>\n", fp);
904 git_object_free(obj);
905 } else if (!git_submodule_lookup(&module, repo, entryname)) {
906 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
908 xmlencode(fp, entrypath, strlen(entrypath));
909 git_submodule_free(module);
910 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
918 writefiles(FILE *fp, const git_oid *id)
920 git_tree *tree = NULL;
921 git_commit *commit = NULL;
924 fputs("<table id=\"files\"><thead>\n<tr>"
925 "<td><b>Mode</b></td><td><b>Name</b></td>"
926 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
927 "</tr>\n</thead><tbody>\n", fp);
929 if (!git_commit_lookup(&commit, repo, id) &&
930 !git_commit_tree(&tree, commit))
931 ret = writefilestree(fp, tree, "");
933 fputs("</tbody></table>", fp);
935 git_commit_free(commit);
942 refs_cmp(const void *v1, const void *v2)
944 git_reference *r1 = (*(git_reference **)v1);
945 git_reference *r2 = (*(git_reference **)v2);
948 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
951 return strcmp(git_reference_shorthand(r1),
952 git_reference_shorthand(r2));
958 struct commitinfo *ci;
959 const git_oid *id = NULL;
960 git_object *obj = NULL;
961 git_reference *dref = NULL, *r, *ref = NULL;
962 git_reference_iterator *it = NULL;
963 git_reference **refs = NULL;
964 size_t count, i, j, refcount;
965 const char *titles[] = { "Branches", "Tags" };
966 const char *ids[] = { "branches", "tags" };
969 if (git_reference_iterator_new(&it, repo))
972 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
973 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
975 refs[refcount] = ref;
977 git_reference_iterator_free(it);
979 /* sort by type then shorthand name */
980 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
982 for (j = 0; j < 2; j++) {
983 for (i = 0, count = 0; i < refcount; i++) {
984 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
985 !(git_reference_is_tag(refs[i]) && j == 1))
988 switch (git_reference_type(refs[i])) {
989 case GIT_REF_SYMBOLIC:
990 if (git_reference_resolve(&dref, refs[i]))
1000 if (!git_reference_target(r) ||
1001 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1003 if (!(id = git_object_id(obj)))
1005 if (!(ci = commitinfo_getbyoid(id)))
1008 /* print header if it has an entry (first). */
1010 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1011 "<thead>\n<tr><td><b>Name</b></td>"
1012 "<td><b>Last commit date</b></td>"
1013 "<td><b>Author</b></td>\n</tr>\n"
1014 "</thead><tbody>\n",
1019 name = git_reference_shorthand(r);
1021 fputs("<tr><td>", fp);
1022 xmlencode(fp, name, strlen(name));
1023 fputs("</td><td>", fp);
1025 printtimeshort(fp, &(ci->author->when));
1026 fputs("</td><td>", fp);
1028 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1029 fputs("</td></tr>\n", fp);
1033 commitinfo_free(ci);
1034 git_object_free(obj);
1036 git_reference_free(dref);
1041 fputs("</tbody></table><br/>", fp);
1045 git_object_free(obj);
1046 git_reference_free(dref);
1048 for (i = 0; i < refcount; i++)
1049 git_reference_free(refs[i]);
1058 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1063 main(int argc, char *argv[])
1065 git_object *obj = NULL;
1066 const git_oid *head = NULL;
1069 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1070 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1074 for (i = 1; i < argc; i++) {
1075 if (argv[i][0] != '-') {
1079 } else if (argv[i][1] == 'c') {
1080 if (nlogcommits > 0 || i + 1 >= argc)
1082 cachefile = argv[++i];
1083 } else if (argv[i][1] == 'l') {
1084 if (cachefile || i + 1 >= argc)
1087 nlogcommits = strtoll(argv[++i], &p, 10);
1088 if (argv[i][0] == '\0' || *p != '\0' ||
1089 nlogcommits <= 0 || errno)
1096 if (!realpath(repodir, repodirabs))
1102 if (unveil(repodir, "r") == -1)
1103 err(1, "unveil: %s", repodir);
1104 if (unveil(".", "rwc") == -1)
1105 err(1, "unveil: .");
1106 if (cachefile && unveil(cachefile, "rwc") == -1)
1107 err(1, "unveil: %s", cachefile);
1110 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1113 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1118 if (git_repository_open_ext(&repo, repodir,
1119 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1120 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1125 if (!git_revparse_single(&obj, repo, "HEAD"))
1126 head = git_object_id(obj);
1127 git_object_free(obj);
1129 /* use directory name as name */
1130 if ((name = strrchr(repodirabs, '/')))
1135 /* strip .git suffix */
1136 if (!(strippedname = strdup(name)))
1138 if ((p = strrchr(strippedname, '.')))
1139 if (!strcmp(p, ".git"))
1142 /* read description or .git/description */
1143 joinpath(path, sizeof(path), repodir, "description");
1144 if (!(fpread = fopen(path, "r"))) {
1145 joinpath(path, sizeof(path), repodir, ".git/description");
1146 fpread = fopen(path, "r");
1149 if (!fgets(description, sizeof(description), fpread))
1150 description[0] = '\0';
1154 /* read url or .git/url */
1155 joinpath(path, sizeof(path), repodir, "url");
1156 if (!(fpread = fopen(path, "r"))) {
1157 joinpath(path, sizeof(path), repodir, ".git/url");
1158 fpread = fopen(path, "r");
1161 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1163 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1168 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1169 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1170 git_object_type(obj) == GIT_OBJ_BLOB)
1171 license = licensefiles[i] + strlen("HEAD:");
1172 git_object_free(obj);
1176 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1177 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1178 git_object_type(obj) == GIT_OBJ_BLOB)
1179 readme = readmefiles[i] + strlen("HEAD:");
1180 git_object_free(obj);
1183 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1184 git_object_type(obj) == GIT_OBJ_BLOB)
1185 submodules = ".gitmodules";
1186 git_object_free(obj);
1189 fp = efopen("log.html", "w");
1191 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1192 writeheader(fp, "Log");
1193 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1194 "<td><b>Commit</b></td>"
1195 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1196 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1197 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1199 if (cachefile && head) {
1200 /* read from cache file (does not need to exist) */
1201 if ((rcachefp = fopen(cachefile, "r"))) {
1202 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1203 errx(1, "%s: no object id", cachefile);
1204 if (git_oid_fromstr(&lastoid, lastoidstr))
1205 errx(1, "%s: invalid object id", cachefile);
1208 /* write log to (temporary) cache */
1209 if ((fd = mkstemp(tmppath)) == -1)
1211 if (!(wcachefp = fdopen(fd, "w")))
1212 err(1, "fdopen: '%s'", tmppath);
1213 /* write last commit id (HEAD) */
1214 git_oid_tostr(buf, sizeof(buf), head);
1215 fprintf(wcachefp, "%s\n", buf);
1220 /* append previous log to log.html and the new cache */
1221 while (!feof(rcachefp)) {
1222 n = fread(buf, 1, sizeof(buf), rcachefp);
1223 if (ferror(rcachefp))
1225 if (fwrite(buf, 1, n, fp) != n ||
1226 fwrite(buf, 1, n, wcachefp) != n)
1237 fputs("</tbody></table>", fp);
1241 /* files for HEAD */
1242 fp = efopen("files.html", "w");
1243 writeheader(fp, "Files");
1245 writefiles(fp, head);
1249 /* summary page with branches and tags */
1250 fp = efopen("refs.html", "w");
1251 writeheader(fp, "Refs");
1257 fp = efopen("atom.xml", "w");
1261 /* rename new cache file on success */
1262 if (cachefile && head) {
1263 if (rename(tmppath, cachefile))
1264 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1265 umask((mask = umask(0)));
1266 if (chmod(cachefile,
1267 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1268 err(1, "chmod: '%s'", cachefile);
1272 git_repository_free(repo);
1273 git_libgit2_shutdown();