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 syntax_highlight(const char *filename, FILE *fp, const char *s, size_t len)
403 int stdout_copy = dup(1);
407 // Python Pygments script for syntax highlighting.
408 FILE *child = popen("./highlight", "w");
410 printf("child is null: %s", strerror(errno));
413 // Give filename through STDIN:
414 fprintf(child, "%s\n", filename);
415 // Give code to highlight through STDIN:
418 for (i = 0; *s && i < len; s++, i++) {
419 if (*s == '\n') lc++;
420 fprintf(child, "%c", *s);
426 dup2(stdout_copy, 1);
431 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
434 const char *s = git_blob_rawcontent(blob);
435 git_off_t len = git_blob_rawsize(blob);
437 fputs("<div id=\"blob\">\n", fp);
440 lc = syntax_highlight(filename, fp, s, len);
443 fputs("</div>\n", fp);
449 printcommit(FILE *fp, struct commitinfo *ci)
451 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
452 relpath, ci->oid, ci->oid);
454 if (ci->parentoid[0])
455 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
456 relpath, ci->parentoid, ci->parentoid);
459 fputs("<b>Author:</b> ", fp);
460 xmlencode(fp, ci->author->name, strlen(ci->author->name));
461 fputs(" <<a href=\"mailto:", fp);
462 xmlencode(fp, ci->author->email, strlen(ci->author->email));
464 xmlencode(fp, ci->author->email, strlen(ci->author->email));
465 fputs("</a>>\n<b>Date:</b> ", fp);
466 printtime(fp, &(ci->author->when));
471 xmlencode(fp, ci->msg, strlen(ci->msg));
477 printshowfile(FILE *fp, struct commitinfo *ci)
479 const git_diff_delta *delta;
480 const git_diff_hunk *hunk;
481 const git_diff_line *line;
483 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
492 if (ci->filecount > 1000 ||
493 ci->ndeltas > 1000 ||
494 ci->addcount > 100000 ||
495 ci->delcount > 100000) {
496 fputs("Diff is too large, output suppressed.\n", fp);
501 fputs("<b>Diffstat:</b>\n<table>", fp);
502 for (i = 0; i < ci->ndeltas; i++) {
503 delta = git_patch_get_delta(ci->deltas[i]->patch);
505 switch (delta->status) {
506 case GIT_DELTA_ADDED: c = 'A'; break;
507 case GIT_DELTA_COPIED: c = 'C'; break;
508 case GIT_DELTA_DELETED: c = 'D'; break;
509 case GIT_DELTA_MODIFIED: c = 'M'; break;
510 case GIT_DELTA_RENAMED: c = 'R'; break;
511 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
512 default: c = ' '; break;
515 fprintf(fp, "<tr><td>%c", c);
517 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
519 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
520 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
521 if (strcmp(delta->old_file.path, delta->new_file.path)) {
522 fputs(" -> ", fp);
523 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
526 add = ci->deltas[i]->addcount;
527 del = ci->deltas[i]->delcount;
529 total = sizeof(linestr) - 2;
530 if (changed > total) {
532 add = ((float)total / changed * add) + 1;
534 del = ((float)total / changed * del) + 1;
536 memset(&linestr, '+', add);
537 memset(&linestr[add], '-', del);
539 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
540 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
541 fwrite(&linestr, 1, add, fp);
542 fputs("</span><span class=\"d\">", fp);
543 fwrite(&linestr[add], 1, del, fp);
544 fputs("</span></td></tr>\n", fp);
546 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
547 ci->filecount, ci->filecount == 1 ? "" : "s",
548 ci->addcount, ci->addcount == 1 ? "" : "s",
549 ci->delcount, ci->delcount == 1 ? "" : "s");
553 for (i = 0; i < ci->ndeltas; i++) {
554 patch = ci->deltas[i]->patch;
555 delta = git_patch_get_delta(patch);
556 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
557 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
558 fputs(".html\">", fp);
559 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
560 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
561 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
562 fprintf(fp, ".html\">");
563 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
564 fprintf(fp, "</a></b>\n");
566 /* check binary data */
567 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
568 fputs("Binary files differ.\n", fp);
572 nhunks = git_patch_num_hunks(patch);
573 for (j = 0; j < nhunks; j++) {
574 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
577 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
578 xmlencode(fp, hunk->header, hunk->header_len);
582 if (git_patch_get_line_in_hunk(&line, patch, j, k))
584 if (line->old_lineno == -1)
585 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
587 else if (line->new_lineno == -1)
588 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
592 xmlencode(fp, line->content, line->content_len);
593 if (line->old_lineno == -1 || line->new_lineno == -1)
601 writelogline(FILE *fp, struct commitinfo *ci)
603 fputs("<tr><td>", fp);
605 printtimeshort(fp, &(ci->author->when));
606 fputs("</td><td>", fp);
608 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
609 xmlencode(fp, ci->summary, strlen(ci->summary));
612 fputs("</td><td>", fp);
614 xmlencode(fp, ci->author->name, strlen(ci->author->name));
615 fputs("</td><td class=\"num\" align=\"right\">", fp);
616 fprintf(fp, "%zu", ci->filecount);
617 fputs("</td><td class=\"num\" align=\"right\">", fp);
618 fprintf(fp, "+%zu", ci->addcount);
619 fputs("</td><td class=\"num\" align=\"right\">", fp);
620 fprintf(fp, "-%zu", ci->delcount);
621 fputs("</td></tr>\n", fp);
625 writelog(FILE *fp, const git_oid *oid)
627 struct commitinfo *ci;
628 git_revwalk *w = NULL;
630 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
634 git_revwalk_new(&w, repo);
635 git_revwalk_push(w, oid);
636 git_revwalk_simplify_first_parent(w);
638 while (!git_revwalk_next(&id, w)) {
641 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
644 git_oid_tostr(oidstr, sizeof(oidstr), &id);
645 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
646 if (r < 0 || (size_t)r >= sizeof(path))
647 errx(1, "path truncated: 'commit/%s.html'", oidstr);
648 r = access(path, F_OK);
650 /* optimization: if there are no log lines to write and
651 the commit file already exists: skip the diffstat */
652 if (!nlogcommits && !r)
655 if (!(ci = commitinfo_getbyoid(&id)))
657 /* diffstat: for stagit HTML required for the log.html line */
658 if (commitinfo_getstats(ci) == -1)
661 if (nlogcommits < 0) {
662 writelogline(fp, ci);
663 } else if (nlogcommits > 0) {
664 writelogline(fp, ci);
666 if (!nlogcommits && ci->parentoid[0])
667 fputs("<tr><td></td><td colspan=\"5\">"
668 "More commits remaining [...]</td>"
673 writelogline(wcachefp, ci);
675 /* check if file exists if so skip it */
678 fpfile = efopen(path, "w");
679 writeheader(fpfile, ci->summary);
680 fputs("<pre>", fpfile);
681 printshowfile(fpfile, ci);
682 fputs("</pre>\n", fpfile);
697 printcommitatom(FILE *fp, struct commitinfo *ci)
699 fputs("<entry>\n", fp);
701 fprintf(fp, "<id>%s</id>\n", ci->oid);
703 fputs("<published>", fp);
704 printtimez(fp, &(ci->author->when));
705 fputs("</published>\n", fp);
708 fputs("<updated>", fp);
709 printtimez(fp, &(ci->committer->when));
710 fputs("</updated>\n", fp);
713 fputs("<title type=\"text\">", fp);
714 xmlencode(fp, ci->summary, strlen(ci->summary));
715 fputs("</title>\n", fp);
717 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
721 fputs("<author>\n<name>", fp);
722 xmlencode(fp, ci->author->name, strlen(ci->author->name));
723 fputs("</name>\n<email>", fp);
724 xmlencode(fp, ci->author->email, strlen(ci->author->email));
725 fputs("</email>\n</author>\n", fp);
728 fputs("<content type=\"text\">", fp);
729 fprintf(fp, "commit %s\n", ci->oid);
730 if (ci->parentoid[0])
731 fprintf(fp, "parent %s\n", ci->parentoid);
733 fputs("Author: ", fp);
734 xmlencode(fp, ci->author->name, strlen(ci->author->name));
736 xmlencode(fp, ci->author->email, strlen(ci->author->email));
737 fputs(">\nDate: ", fp);
738 printtime(fp, &(ci->author->when));
743 xmlencode(fp, ci->msg, strlen(ci->msg));
745 fputs("\n</content>\n</entry>\n", fp);
751 struct commitinfo *ci;
752 git_revwalk *w = NULL;
754 size_t i, m = 100; /* last 'm' commits */
756 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
757 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
758 xmlencode(fp, strippedname, strlen(strippedname));
759 fputs(", branch HEAD</title>\n<subtitle>", fp);
760 xmlencode(fp, description, strlen(description));
761 fputs("</subtitle>\n", fp);
763 git_revwalk_new(&w, repo);
764 git_revwalk_push_head(w);
765 git_revwalk_simplify_first_parent(w);
767 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
768 if (!(ci = commitinfo_getbyoid(&id)))
770 printcommitatom(fp, ci);
775 fputs("</feed>\n", fp);
781 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
783 char tmp[PATH_MAX] = "", *d;
788 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
789 errx(1, "path truncated: '%s'", fpath);
790 if (!(d = dirname(tmp)))
795 for (p = fpath, tmp[0] = '\0'; *p; p++) {
796 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
797 errx(1, "path truncated: '../%s'", tmp);
801 fp = efopen(fpath, "w");
802 writeheader(fp, filename);
804 xmlencode(fp, filename, strlen(filename));
805 fprintf(fp, " (%juB)", (uintmax_t)filesize);
806 fputs("</p><hr/>", fp);
808 if (git_blob_is_binary((git_blob *)obj)) {
809 fputs("<p>Binary file.</p>\n", fp);
811 lc = writeblobhtml(fpath, fp, (git_blob *)obj);
824 filemode(git_filemode_t m)
826 static char mode[11];
828 memset(mode, '-', sizeof(mode) - 1);
839 else if (S_ISFIFO(m))
843 else if (S_ISSOCK(m))
848 if (m & S_IRUSR) mode[1] = 'r';
849 if (m & S_IWUSR) mode[2] = 'w';
850 if (m & S_IXUSR) mode[3] = 'x';
851 if (m & S_IRGRP) mode[4] = 'r';
852 if (m & S_IWGRP) mode[5] = 'w';
853 if (m & S_IXGRP) mode[6] = 'x';
854 if (m & S_IROTH) mode[7] = 'r';
855 if (m & S_IWOTH) mode[8] = 'w';
856 if (m & S_IXOTH) mode[9] = 'x';
858 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
859 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
860 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
866 writefilestree(FILE *fp, git_tree *tree, const char *path)
868 const git_tree_entry *entry = NULL;
869 git_submodule *module = NULL;
870 git_object *obj = NULL;
872 const char *entryname;
873 char filepath[PATH_MAX], entrypath[PATH_MAX];
877 count = git_tree_entrycount(tree);
878 for (i = 0; i < count; i++) {
879 if (!(entry = git_tree_entry_byindex(tree, i)) ||
880 !(entryname = git_tree_entry_name(entry)))
882 joinpath(entrypath, sizeof(entrypath), path, entryname);
884 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
886 if (r < 0 || (size_t)r >= sizeof(filepath))
887 errx(1, "path truncated: 'file/%s.html'", entrypath);
889 if (!git_tree_entry_to_object(&obj, repo, entry)) {
890 switch (git_object_type(obj)) {
895 ret = writefilestree(fp, (git_tree *)obj,
897 git_object_free(obj);
902 git_object_free(obj);
906 filesize = git_blob_rawsize((git_blob *)obj);
907 lc = writeblob(obj, filepath, entryname, filesize);
909 fputs("<tr><td>", fp);
910 fputs(filemode(git_tree_entry_filemode(entry)), fp);
911 fprintf(fp, "</td><td><a href=\"%s", relpath);
912 xmlencode(fp, filepath, strlen(filepath));
914 xmlencode(fp, entrypath, strlen(entrypath));
915 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
917 fprintf(fp, "%dL", lc);
919 fprintf(fp, "%juB", (uintmax_t)filesize);
920 fputs("</td></tr>\n", fp);
921 git_object_free(obj);
922 } else if (!git_submodule_lookup(&module, repo, entryname)) {
923 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
925 xmlencode(fp, entrypath, strlen(entrypath));
926 git_submodule_free(module);
927 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
935 writefiles(FILE *fp, const git_oid *id)
937 git_tree *tree = NULL;
938 git_commit *commit = NULL;
941 fputs("<table id=\"files\"><thead>\n<tr>"
942 "<td><b>Mode</b></td><td><b>Name</b></td>"
943 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
944 "</tr>\n</thead><tbody>\n", fp);
946 if (!git_commit_lookup(&commit, repo, id) &&
947 !git_commit_tree(&tree, commit))
948 ret = writefilestree(fp, tree, "");
950 fputs("</tbody></table>", fp);
952 git_commit_free(commit);
959 refs_cmp(const void *v1, const void *v2)
961 git_reference *r1 = (*(git_reference **)v1);
962 git_reference *r2 = (*(git_reference **)v2);
965 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
968 return strcmp(git_reference_shorthand(r1),
969 git_reference_shorthand(r2));
975 struct commitinfo *ci;
976 const git_oid *id = NULL;
977 git_object *obj = NULL;
978 git_reference *dref = NULL, *r, *ref = NULL;
979 git_reference_iterator *it = NULL;
980 git_reference **refs = NULL;
981 size_t count, i, j, refcount;
982 const char *titles[] = { "Branches", "Tags" };
983 const char *ids[] = { "branches", "tags" };
986 if (git_reference_iterator_new(&it, repo))
989 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
990 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
992 refs[refcount] = ref;
994 git_reference_iterator_free(it);
996 /* sort by type then shorthand name */
997 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
999 for (j = 0; j < 2; j++) {
1000 for (i = 0, count = 0; i < refcount; i++) {
1001 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1002 !(git_reference_is_tag(refs[i]) && j == 1))
1005 switch (git_reference_type(refs[i])) {
1006 case GIT_REF_SYMBOLIC:
1007 if (git_reference_resolve(&dref, refs[i]))
1017 if (!git_reference_target(r) ||
1018 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1020 if (!(id = git_object_id(obj)))
1022 if (!(ci = commitinfo_getbyoid(id)))
1025 /* print header if it has an entry (first). */
1027 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1028 "<thead>\n<tr><td><b>Name</b></td>"
1029 "<td><b>Last commit date</b></td>"
1030 "<td><b>Author</b></td>\n</tr>\n"
1031 "</thead><tbody>\n",
1036 name = git_reference_shorthand(r);
1038 fputs("<tr><td>", fp);
1039 xmlencode(fp, name, strlen(name));
1040 fputs("</td><td>", fp);
1042 printtimeshort(fp, &(ci->author->when));
1043 fputs("</td><td>", fp);
1045 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1046 fputs("</td></tr>\n", fp);
1050 commitinfo_free(ci);
1051 git_object_free(obj);
1053 git_reference_free(dref);
1058 fputs("</tbody></table><br/>", fp);
1062 git_object_free(obj);
1063 git_reference_free(dref);
1065 for (i = 0; i < refcount; i++)
1066 git_reference_free(refs[i]);
1075 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1080 main(int argc, char *argv[])
1082 git_object *obj = NULL;
1083 const git_oid *head = NULL;
1086 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1087 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1091 for (i = 1; i < argc; i++) {
1092 if (argv[i][0] != '-') {
1096 } else if (argv[i][1] == 'c') {
1097 if (nlogcommits > 0 || i + 1 >= argc)
1099 cachefile = argv[++i];
1100 } else if (argv[i][1] == 'l') {
1101 if (cachefile || i + 1 >= argc)
1104 nlogcommits = strtoll(argv[++i], &p, 10);
1105 if (argv[i][0] == '\0' || *p != '\0' ||
1106 nlogcommits <= 0 || errno)
1113 if (!realpath(repodir, repodirabs))
1119 if (unveil(repodir, "r") == -1)
1120 err(1, "unveil: %s", repodir);
1121 if (unveil(".", "rwc") == -1)
1122 err(1, "unveil: .");
1123 if (cachefile && unveil(cachefile, "rwc") == -1)
1124 err(1, "unveil: %s", cachefile);
1127 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1130 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1135 if (git_repository_open_ext(&repo, repodir,
1136 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1137 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1142 if (!git_revparse_single(&obj, repo, "HEAD"))
1143 head = git_object_id(obj);
1144 git_object_free(obj);
1146 /* use directory name as name */
1147 if ((name = strrchr(repodirabs, '/')))
1152 /* strip .git suffix */
1153 if (!(strippedname = strdup(name)))
1155 if ((p = strrchr(strippedname, '.')))
1156 if (!strcmp(p, ".git"))
1159 /* read description or .git/description */
1160 joinpath(path, sizeof(path), repodir, "description");
1161 if (!(fpread = fopen(path, "r"))) {
1162 joinpath(path, sizeof(path), repodir, ".git/description");
1163 fpread = fopen(path, "r");
1166 if (!fgets(description, sizeof(description), fpread))
1167 description[0] = '\0';
1171 /* read url or .git/url */
1172 joinpath(path, sizeof(path), repodir, "url");
1173 if (!(fpread = fopen(path, "r"))) {
1174 joinpath(path, sizeof(path), repodir, ".git/url");
1175 fpread = fopen(path, "r");
1178 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1180 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1185 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1186 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1187 git_object_type(obj) == GIT_OBJ_BLOB)
1188 license = licensefiles[i] + strlen("HEAD:");
1189 git_object_free(obj);
1193 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1194 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1195 git_object_type(obj) == GIT_OBJ_BLOB)
1196 readme = readmefiles[i] + strlen("HEAD:");
1197 git_object_free(obj);
1200 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1201 git_object_type(obj) == GIT_OBJ_BLOB)
1202 submodules = ".gitmodules";
1203 git_object_free(obj);
1206 fp = efopen("log.html", "w");
1208 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1209 writeheader(fp, "Log");
1210 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1211 "<td><b>Commit</b></td>"
1212 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1213 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1214 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1216 if (cachefile && head) {
1217 /* read from cache file (does not need to exist) */
1218 if ((rcachefp = fopen(cachefile, "r"))) {
1219 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1220 errx(1, "%s: no object id", cachefile);
1221 if (git_oid_fromstr(&lastoid, lastoidstr))
1222 errx(1, "%s: invalid object id", cachefile);
1225 /* write log to (temporary) cache */
1226 if ((fd = mkstemp(tmppath)) == -1)
1228 if (!(wcachefp = fdopen(fd, "w")))
1229 err(1, "fdopen: '%s'", tmppath);
1230 /* write last commit id (HEAD) */
1231 git_oid_tostr(buf, sizeof(buf), head);
1232 fprintf(wcachefp, "%s\n", buf);
1237 /* append previous log to log.html and the new cache */
1238 while (!feof(rcachefp)) {
1239 n = fread(buf, 1, sizeof(buf), rcachefp);
1240 if (ferror(rcachefp))
1242 if (fwrite(buf, 1, n, fp) != n ||
1243 fwrite(buf, 1, n, wcachefp) != n)
1254 fputs("</tbody></table>", fp);
1258 /* files for HEAD */
1259 fp = efopen("files.html", "w");
1260 writeheader(fp, "Files");
1262 writefiles(fp, head);
1266 /* summary page with branches and tags */
1267 fp = efopen("refs.html", "w");
1268 writeheader(fp, "Refs");
1274 fp = efopen("atom.xml", "w");
1278 /* rename new cache file on success */
1279 if (cachefile && head) {
1280 if (rename(tmppath, cachefile))
1281 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1282 umask((mask = umask(0)));
1283 if (chmod(cachefile,
1284 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1285 err(1, "chmod: '%s'", cachefile);
1289 git_repository_free(repo);
1290 git_libgit2_shutdown();