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 char *submodules;
60 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
62 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
64 static long long nlogcommits = -1; /* < 0 indicates not used */
67 static git_oid lastoid;
68 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
69 static FILE *rcachefp, *wcachefp;
70 static const char *cachefile;
73 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
77 r = snprintf(buf, bufsiz, "%s%s%s",
78 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
79 if (r < 0 || (size_t)r >= bufsiz)
80 errx(1, "path truncated: '%s%s%s'",
81 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
85 deltainfo_free(struct deltainfo *di)
89 git_patch_free(di->patch);
90 memset(di, 0, sizeof(*di));
95 commitinfo_getstats(struct commitinfo *ci)
98 git_diff_options opts;
99 git_diff_find_options fopts;
100 const git_diff_delta *delta;
101 const git_diff_hunk *hunk;
102 const git_diff_line *line;
103 git_patch *patch = NULL;
104 size_t ndeltas, nhunks, nhunklines;
107 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
109 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
110 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
112 ci->parent_tree = NULL;
116 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
117 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
118 GIT_DIFF_IGNORE_SUBMODULES |
119 GIT_DIFF_INCLUDE_TYPECHANGE;
120 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
123 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
125 /* find renames and copies, exact matches (no heuristic) for renames. */
126 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
127 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
128 if (git_diff_find_similar(ci->diff, &fopts))
131 ndeltas = git_diff_num_deltas(ci->diff);
132 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
135 for (i = 0; i < ndeltas; i++) {
136 if (git_patch_from_diff(&patch, ci->diff, i))
139 if (!(di = calloc(1, sizeof(struct deltainfo))))
144 delta = git_patch_get_delta(patch);
146 /* skip stats for binary data */
147 if (delta->flags & GIT_DIFF_FLAG_BINARY)
150 nhunks = git_patch_num_hunks(patch);
151 for (j = 0; j < nhunks; j++) {
152 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
155 if (git_patch_get_line_in_hunk(&line, patch, j, k))
157 if (line->old_lineno == -1) {
160 } else if (line->new_lineno == -1) {
173 git_diff_free(ci->diff);
175 git_tree_free(ci->commit_tree);
176 ci->commit_tree = NULL;
177 git_tree_free(ci->parent_tree);
178 ci->parent_tree = NULL;
179 git_commit_free(ci->parent);
183 for (i = 0; i < ci->ndeltas; i++)
184 deltainfo_free(ci->deltas[i]);
196 commitinfo_free(struct commitinfo *ci)
203 for (i = 0; i < ci->ndeltas; i++)
204 deltainfo_free(ci->deltas[i]);
207 git_diff_free(ci->diff);
208 git_tree_free(ci->commit_tree);
209 git_tree_free(ci->parent_tree);
210 git_commit_free(ci->commit);
211 git_commit_free(ci->parent);
212 memset(ci, 0, sizeof(*ci));
217 commitinfo_getbyoid(const git_oid *id)
219 struct commitinfo *ci;
221 if (!(ci = calloc(1, sizeof(struct commitinfo))))
224 if (git_commit_lookup(&(ci->commit), repo, id))
228 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
229 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
231 ci->author = git_commit_author(ci->commit);
232 ci->committer = git_commit_committer(ci->commit);
233 ci->summary = git_commit_summary(ci->commit);
234 ci->msg = git_commit_message(ci->commit);
245 efopen(const char *name, const char *flags)
249 if (!(fp = fopen(name, flags)))
250 err(1, "fopen: '%s'", name);
255 /* Escape characters below as HTML 2.0 / XML 1.0. */
257 xmlencode(FILE *fp, const char *s, size_t len)
261 for (i = 0; *s && i < len; s++, i++) {
263 case '<': fputs("<", fp); break;
264 case '>': fputs(">", fp); break;
265 case '\'': fputs("'", fp); break;
266 case '&': fputs("&", fp); break;
267 case '"': fputs(""", fp); break;
268 default: fputc(*s, fp);
274 mkdirp(const char *path)
276 char tmp[PATH_MAX], *p;
278 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
279 errx(1, "path truncated: '%s'", path);
280 for (p = tmp + (tmp[0] == '/'); *p; p++) {
284 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
288 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
294 printtimez(FILE *fp, const git_time *intime)
300 t = (time_t)intime->time;
301 if (!(intm = gmtime(&t)))
303 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
308 printtime(FILE *fp, const git_time *intime)
314 t = (time_t)intime->time + (intime->offset * 60);
315 if (!(intm = gmtime(&t)))
317 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
318 if (intime->offset < 0)
319 fprintf(fp, "%s -%02d%02d", out,
320 -(intime->offset) / 60, -(intime->offset) % 60);
322 fprintf(fp, "%s +%02d%02d", out,
323 intime->offset / 60, intime->offset % 60);
327 printtimeshort(FILE *fp, const git_time *intime)
333 t = (time_t)intime->time;
334 if (!(intm = gmtime(&t)))
336 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
341 writeheader(FILE *fp, const char *title)
343 fputs("<!DOCTYPE html>\n"
345 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
347 xmlencode(fp, title, strlen(title));
348 if (title[0] && strippedname[0])
350 xmlencode(fp, strippedname, strlen(strippedname));
353 xmlencode(fp, description, strlen(description));
354 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
355 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
357 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
358 fputs("</head>\n<body>\n<table><tr><td>", fp);
359 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
361 fputs("</td><td><h1>", fp);
362 xmlencode(fp, strippedname, strlen(strippedname));
363 fputs("</h1><span class=\"desc\">", fp);
364 xmlencode(fp, description, strlen(description));
365 fputs("</span></td></tr>", fp);
367 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
368 xmlencode(fp, cloneurl, strlen(cloneurl));
370 xmlencode(fp, cloneurl, strlen(cloneurl));
371 fputs("</a></td></tr>", fp);
373 fputs("<tr><td></td><td>\n", fp);
374 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
375 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
376 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
378 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
379 relpath, submodules);
381 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
384 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
386 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
390 writefooter(FILE *fp)
392 fputs("</div>\n</body>\n</html>\n", fp);
396 writeblobhtml(FILE *fp, const git_blob *blob)
398 size_t n = 0, i, prev;
399 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
400 const char *s = git_blob_rawcontent(blob);
401 git_off_t len = git_blob_rawsize(blob);
403 fputs("<pre id=\"blob\">\n", fp);
406 for (i = 0, prev = 0; i < (size_t)len; i++) {
410 fprintf(fp, nfmt, n, n, n);
411 xmlencode(fp, &s[prev], i - prev + 1);
415 if ((len - prev) > 0) {
417 fprintf(fp, nfmt, n, n, n);
418 xmlencode(fp, &s[prev], len - prev);
422 fputs("</pre>\n", fp);
428 printcommit(FILE *fp, struct commitinfo *ci)
430 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
431 relpath, ci->oid, ci->oid);
433 if (ci->parentoid[0])
434 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
435 relpath, ci->parentoid, ci->parentoid);
438 fputs("<b>Author:</b> ", fp);
439 xmlencode(fp, ci->author->name, strlen(ci->author->name));
440 fputs(" <<a href=\"mailto:", fp);
441 xmlencode(fp, ci->author->email, strlen(ci->author->email));
443 xmlencode(fp, ci->author->email, strlen(ci->author->email));
444 fputs("</a>>\n<b>Date:</b> ", fp);
445 printtime(fp, &(ci->author->when));
450 xmlencode(fp, ci->msg, strlen(ci->msg));
456 printshowfile(FILE *fp, struct commitinfo *ci)
458 const git_diff_delta *delta;
459 const git_diff_hunk *hunk;
460 const git_diff_line *line;
462 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
471 if (ci->filecount > 1000 ||
472 ci->ndeltas > 1000 ||
473 ci->addcount > 100000 ||
474 ci->delcount > 100000) {
475 fputs("Diff is too large, output suppressed.\n", fp);
480 fputs("<b>Diffstat:</b>\n<table>", fp);
481 for (i = 0; i < ci->ndeltas; i++) {
482 delta = git_patch_get_delta(ci->deltas[i]->patch);
484 switch (delta->status) {
485 case GIT_DELTA_ADDED: c = 'A'; break;
486 case GIT_DELTA_COPIED: c = 'C'; break;
487 case GIT_DELTA_DELETED: c = 'D'; break;
488 case GIT_DELTA_MODIFIED: c = 'M'; break;
489 case GIT_DELTA_RENAMED: c = 'R'; break;
490 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
491 default: c = ' '; break;
494 fprintf(fp, "<tr><td>%c", c);
496 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
498 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
499 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
500 if (strcmp(delta->old_file.path, delta->new_file.path)) {
501 fputs(" -> ", fp);
502 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
505 add = ci->deltas[i]->addcount;
506 del = ci->deltas[i]->delcount;
508 total = sizeof(linestr) - 2;
509 if (changed > total) {
511 add = ((float)total / changed * add) + 1;
513 del = ((float)total / changed * del) + 1;
515 memset(&linestr, '+', add);
516 memset(&linestr[add], '-', del);
518 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
519 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
520 fwrite(&linestr, 1, add, fp);
521 fputs("</span><span class=\"d\">", fp);
522 fwrite(&linestr[add], 1, del, fp);
523 fputs("</span></td></tr>\n", fp);
525 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
526 ci->filecount, ci->filecount == 1 ? "" : "s",
527 ci->addcount, ci->addcount == 1 ? "" : "s",
528 ci->delcount, ci->delcount == 1 ? "" : "s");
532 for (i = 0; i < ci->ndeltas; i++) {
533 patch = ci->deltas[i]->patch;
534 delta = git_patch_get_delta(patch);
535 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
536 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
537 fputs(".html\">", fp);
538 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
539 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
540 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
541 fprintf(fp, ".html\">");
542 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
543 fprintf(fp, "</a></b>\n");
545 /* check binary data */
546 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
547 fputs("Binary files differ.\n", fp);
551 nhunks = git_patch_num_hunks(patch);
552 for (j = 0; j < nhunks; j++) {
553 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
556 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
557 xmlencode(fp, hunk->header, hunk->header_len);
561 if (git_patch_get_line_in_hunk(&line, patch, j, k))
563 if (line->old_lineno == -1)
564 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
566 else if (line->new_lineno == -1)
567 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
571 xmlencode(fp, line->content, line->content_len);
572 if (line->old_lineno == -1 || line->new_lineno == -1)
580 writelogline(FILE *fp, struct commitinfo *ci)
582 fputs("<tr><td>", fp);
584 printtimeshort(fp, &(ci->author->when));
585 fputs("</td><td>", fp);
587 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
588 xmlencode(fp, ci->summary, strlen(ci->summary));
591 fputs("</td><td>", fp);
593 xmlencode(fp, ci->author->name, strlen(ci->author->name));
594 fputs("</td><td class=\"num\" align=\"right\">", fp);
595 fprintf(fp, "%zu", ci->filecount);
596 fputs("</td><td class=\"num\" align=\"right\">", fp);
597 fprintf(fp, "+%zu", ci->addcount);
598 fputs("</td><td class=\"num\" align=\"right\">", fp);
599 fprintf(fp, "-%zu", ci->delcount);
600 fputs("</td></tr>\n", fp);
604 writelog(FILE *fp, const git_oid *oid)
606 struct commitinfo *ci;
607 git_revwalk *w = NULL;
609 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
613 git_revwalk_new(&w, repo);
614 git_revwalk_push(w, oid);
615 git_revwalk_simplify_first_parent(w);
617 while (!git_revwalk_next(&id, w)) {
620 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
623 git_oid_tostr(oidstr, sizeof(oidstr), &id);
624 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
625 if (r < 0 || (size_t)r >= sizeof(path))
626 errx(1, "path truncated: 'commit/%s.html'", oidstr);
627 r = access(path, F_OK);
629 /* optimization: if there are no log lines to write and
630 the commit file already exists: skip the diffstat */
631 if (!nlogcommits && !r)
634 if (!(ci = commitinfo_getbyoid(&id)))
636 /* diffstat: for stagit HTML required for the log.html line */
637 if (commitinfo_getstats(ci) == -1)
640 if (nlogcommits < 0) {
641 writelogline(fp, ci);
642 } else if (nlogcommits > 0) {
643 writelogline(fp, ci);
645 if (!nlogcommits && ci->parentoid[0])
646 fputs("<tr><td></td><td colspan=\"5\">"
647 "More commits remaining [...]</td>"
652 writelogline(wcachefp, ci);
654 /* check if file exists if so skip it */
657 fpfile = efopen(path, "w");
658 writeheader(fpfile, ci->summary);
659 fputs("<pre>", fpfile);
660 printshowfile(fpfile, ci);
661 fputs("</pre>\n", fpfile);
676 printcommitatom(FILE *fp, struct commitinfo *ci)
678 fputs("<entry>\n", fp);
680 fprintf(fp, "<id>%s</id>\n", ci->oid);
682 fputs("<published>", fp);
683 printtimez(fp, &(ci->author->when));
684 fputs("</published>\n", fp);
687 fputs("<updated>", fp);
688 printtimez(fp, &(ci->committer->when));
689 fputs("</updated>\n", fp);
692 fputs("<title type=\"text\">", fp);
693 xmlencode(fp, ci->summary, strlen(ci->summary));
694 fputs("</title>\n", fp);
696 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
700 fputs("<author><name>", fp);
701 xmlencode(fp, ci->author->name, strlen(ci->author->name));
702 fputs("</name>\n<email>", fp);
703 xmlencode(fp, ci->author->email, strlen(ci->author->email));
704 fputs("</email>\n</author>\n", fp);
707 fputs("<content type=\"text\">", fp);
708 fprintf(fp, "commit %s\n", ci->oid);
709 if (ci->parentoid[0])
710 fprintf(fp, "parent %s\n", ci->parentoid);
712 fputs("Author: ", fp);
713 xmlencode(fp, ci->author->name, strlen(ci->author->name));
715 xmlencode(fp, ci->author->email, strlen(ci->author->email));
716 fputs(">\nDate: ", fp);
717 printtime(fp, &(ci->author->when));
722 xmlencode(fp, ci->msg, strlen(ci->msg));
724 fputs("\n</content>\n</entry>\n", fp);
730 struct commitinfo *ci;
731 git_revwalk *w = NULL;
733 size_t i, m = 100; /* last 'm' commits */
735 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
736 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
737 xmlencode(fp, strippedname, strlen(strippedname));
738 fputs(", branch HEAD</title>\n<subtitle>", fp);
739 xmlencode(fp, description, strlen(description));
740 fputs("</subtitle>\n", fp);
742 git_revwalk_new(&w, repo);
743 git_revwalk_push_head(w);
744 git_revwalk_simplify_first_parent(w);
746 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
747 if (!(ci = commitinfo_getbyoid(&id)))
749 printcommitatom(fp, ci);
754 fputs("</feed>\n", fp);
760 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
762 char tmp[PATH_MAX] = "", *d;
767 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
768 errx(1, "path truncated: '%s'", fpath);
769 if (!(d = dirname(tmp)))
774 for (p = fpath, tmp[0] = '\0'; *p; p++) {
775 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
776 errx(1, "path truncated: '../%s'", tmp);
780 fp = efopen(fpath, "w");
781 writeheader(fp, filename);
783 xmlencode(fp, filename, strlen(filename));
784 fprintf(fp, " (%juB)", (uintmax_t)filesize);
785 fputs("</p><hr/>", fp);
787 if (git_blob_is_binary((git_blob *)obj)) {
788 fputs("<p>Binary file.</p>\n", fp);
790 lc = writeblobhtml(fp, (git_blob *)obj);
803 filemode(git_filemode_t m)
805 static char mode[11];
807 memset(mode, '-', sizeof(mode) - 1);
818 else if (S_ISFIFO(m))
822 else if (S_ISSOCK(m))
827 if (m & S_IRUSR) mode[1] = 'r';
828 if (m & S_IWUSR) mode[2] = 'w';
829 if (m & S_IXUSR) mode[3] = 'x';
830 if (m & S_IRGRP) mode[4] = 'r';
831 if (m & S_IWGRP) mode[5] = 'w';
832 if (m & S_IXGRP) mode[6] = 'x';
833 if (m & S_IROTH) mode[7] = 'r';
834 if (m & S_IWOTH) mode[8] = 'w';
835 if (m & S_IXOTH) mode[9] = 'x';
837 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
838 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
839 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
845 writefilestree(FILE *fp, git_tree *tree, const char *path)
847 const git_tree_entry *entry = NULL;
848 git_submodule *module = NULL;
849 git_object *obj = NULL;
851 const char *entryname;
852 char filepath[PATH_MAX], entrypath[PATH_MAX];
856 count = git_tree_entrycount(tree);
857 for (i = 0; i < count; i++) {
858 if (!(entry = git_tree_entry_byindex(tree, i)) ||
859 !(entryname = git_tree_entry_name(entry)))
861 joinpath(entrypath, sizeof(entrypath), path, entryname);
863 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
865 if (r < 0 || (size_t)r >= sizeof(filepath))
866 errx(1, "path truncated: 'file/%s.html'", entrypath);
868 if (!git_tree_entry_to_object(&obj, repo, entry)) {
869 switch (git_object_type(obj)) {
874 ret = writefilestree(fp, (git_tree *)obj,
876 git_object_free(obj);
881 git_object_free(obj);
885 filesize = git_blob_rawsize((git_blob *)obj);
886 lc = writeblob(obj, filepath, entryname, filesize);
888 fputs("<tr><td>", fp);
889 fputs(filemode(git_tree_entry_filemode(entry)), fp);
890 fprintf(fp, "</td><td><a href=\"%s", relpath);
891 xmlencode(fp, filepath, strlen(filepath));
893 xmlencode(fp, entrypath, strlen(entrypath));
894 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
896 fprintf(fp, "%dL", lc);
898 fprintf(fp, "%juB", (uintmax_t)filesize);
899 fputs("</td></tr>\n", fp);
900 git_object_free(obj);
901 } else if (!git_submodule_lookup(&module, repo, entryname)) {
902 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
904 xmlencode(fp, entrypath, strlen(entrypath));
905 git_submodule_free(module);
906 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
914 writefiles(FILE *fp, const git_oid *id)
916 git_tree *tree = NULL;
917 git_commit *commit = NULL;
920 fputs("<table id=\"files\"><thead>\n<tr>"
921 "<td><b>Mode</b></td><td><b>Name</b></td>"
922 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
923 "</tr>\n</thead><tbody>\n", fp);
925 if (!git_commit_lookup(&commit, repo, id) &&
926 !git_commit_tree(&tree, commit))
927 ret = writefilestree(fp, tree, "");
929 fputs("</tbody></table>", fp);
931 git_commit_free(commit);
938 refs_cmp(const void *v1, const void *v2)
940 git_reference *r1 = (*(git_reference **)v1);
941 git_reference *r2 = (*(git_reference **)v2);
944 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
947 return strcmp(git_reference_shorthand(r1),
948 git_reference_shorthand(r2));
954 struct commitinfo *ci;
955 const git_oid *id = NULL;
956 git_object *obj = NULL;
957 git_reference *dref = NULL, *r, *ref = NULL;
958 git_reference_iterator *it = NULL;
959 git_reference **refs = NULL;
960 size_t count, i, j, refcount;
961 const char *titles[] = { "Branches", "Tags" };
962 const char *ids[] = { "branches", "tags" };
965 if (git_reference_iterator_new(&it, repo))
968 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
969 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
971 refs[refcount] = ref;
973 git_reference_iterator_free(it);
975 /* sort by type then shorthand name */
976 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
978 for (j = 0; j < 2; j++) {
979 for (i = 0, count = 0; i < refcount; i++) {
980 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
981 !(git_reference_is_tag(refs[i]) && j == 1))
984 switch (git_reference_type(refs[i])) {
985 case GIT_REF_SYMBOLIC:
986 if (git_reference_resolve(&dref, refs[i]))
996 if (!git_reference_target(r) ||
997 git_reference_peel(&obj, r, GIT_OBJ_ANY))
999 if (!(id = git_object_id(obj)))
1001 if (!(ci = commitinfo_getbyoid(id)))
1004 /* print header if it has an entry (first). */
1006 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1007 "<thead>\n<tr><td><b>Name</b></td>"
1008 "<td><b>Last commit date</b></td>"
1009 "<td><b>Author</b></td>\n</tr>\n"
1010 "</thead><tbody>\n",
1015 name = git_reference_shorthand(r);
1017 fputs("<tr><td>", fp);
1018 xmlencode(fp, name, strlen(name));
1019 fputs("</td><td>", fp);
1021 printtimeshort(fp, &(ci->author->when));
1022 fputs("</td><td>", fp);
1024 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1025 fputs("</td></tr>\n", fp);
1029 commitinfo_free(ci);
1030 git_object_free(obj);
1032 git_reference_free(dref);
1037 fputs("</tbody></table><br/>", fp);
1041 git_object_free(obj);
1042 git_reference_free(dref);
1044 for (i = 0; i < refcount; i++)
1045 git_reference_free(refs[i]);
1054 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1059 main(int argc, char *argv[])
1061 git_object *obj = NULL;
1062 const git_oid *head = NULL;
1065 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1066 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1070 for (i = 1; i < argc; i++) {
1071 if (argv[i][0] != '-') {
1075 } else if (argv[i][1] == 'c') {
1076 if (nlogcommits > 0 || i + 1 >= argc)
1078 cachefile = argv[++i];
1079 } else if (argv[i][1] == 'l') {
1080 if (cachefile || i + 1 >= argc)
1083 nlogcommits = strtoll(argv[++i], &p, 10);
1084 if (argv[i][0] == '\0' || *p != '\0' ||
1085 nlogcommits <= 0 || errno)
1092 if (!realpath(repodir, repodirabs))
1098 if (unveil(repodir, "r") == -1)
1099 err(1, "unveil: %s", repodir);
1100 if (unveil(".", "rwc") == -1)
1101 err(1, "unveil: .");
1102 if (cachefile && unveil(cachefile, "rwc") == -1)
1103 err(1, "unveil: %s", cachefile);
1106 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1109 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1114 if (git_repository_open_ext(&repo, repodir,
1115 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1116 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1121 if (!git_revparse_single(&obj, repo, "HEAD"))
1122 head = git_object_id(obj);
1123 git_object_free(obj);
1125 /* use directory name as name */
1126 if ((name = strrchr(repodirabs, '/')))
1131 /* strip .git suffix */
1132 if (!(strippedname = strdup(name)))
1134 if ((p = strrchr(strippedname, '.')))
1135 if (!strcmp(p, ".git"))
1138 /* read description or .git/description */
1139 joinpath(path, sizeof(path), repodir, "description");
1140 if (!(fpread = fopen(path, "r"))) {
1141 joinpath(path, sizeof(path), repodir, ".git/description");
1142 fpread = fopen(path, "r");
1145 if (!fgets(description, sizeof(description), fpread))
1146 description[0] = '\0';
1150 /* read url or .git/url */
1151 joinpath(path, sizeof(path), repodir, "url");
1152 if (!(fpread = fopen(path, "r"))) {
1153 joinpath(path, sizeof(path), repodir, ".git/url");
1154 fpread = fopen(path, "r");
1157 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1159 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1164 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1165 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1166 git_object_type(obj) == GIT_OBJ_BLOB)
1167 license = licensefiles[i] + strlen("HEAD:");
1168 git_object_free(obj);
1172 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1173 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1174 git_object_type(obj) == GIT_OBJ_BLOB)
1175 readme = readmefiles[i] + strlen("HEAD:");
1176 git_object_free(obj);
1179 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1180 git_object_type(obj) == GIT_OBJ_BLOB)
1181 submodules = ".gitmodules";
1182 git_object_free(obj);
1185 fp = efopen("log.html", "w");
1187 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1188 writeheader(fp, "Log");
1189 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1190 "<td><b>Commit message</b></td>"
1191 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1192 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1193 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1195 if (cachefile && head) {
1196 /* read from cache file (does not need to exist) */
1197 if ((rcachefp = fopen(cachefile, "r"))) {
1198 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1199 errx(1, "%s: no object id", cachefile);
1200 if (git_oid_fromstr(&lastoid, lastoidstr))
1201 errx(1, "%s: invalid object id", cachefile);
1204 /* write log to (temporary) cache */
1205 if ((fd = mkstemp(tmppath)) == -1)
1207 if (!(wcachefp = fdopen(fd, "w")))
1208 err(1, "fdopen: '%s'", tmppath);
1209 /* write last commit id (HEAD) */
1210 git_oid_tostr(buf, sizeof(buf), head);
1211 fprintf(wcachefp, "%s\n", buf);
1216 /* append previous log to log.html and the new cache */
1217 while (!feof(rcachefp)) {
1218 n = fread(buf, 1, sizeof(buf), rcachefp);
1219 if (ferror(rcachefp))
1221 if (fwrite(buf, 1, n, fp) != n ||
1222 fwrite(buf, 1, n, wcachefp) != n)
1233 fputs("</tbody></table>", fp);
1237 /* files for HEAD */
1238 fp = efopen("files.html", "w");
1239 writeheader(fp, "Files");
1241 writefiles(fp, head);
1245 /* summary page with branches and tags */
1246 fp = efopen("refs.html", "w");
1247 writeheader(fp, "Refs");
1253 fp = efopen("atom.xml", "w");
1257 /* rename new cache file on success */
1258 if (cachefile && head) {
1259 if (rename(tmppath, cachefile))
1260 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1261 umask((mask = umask(0)));
1262 if (chmod(cachefile,
1263 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1264 err(1, "chmod: '%s'", cachefile);
1268 git_repository_free(repo);
1269 git_libgit2_shutdown();