19 #include <cmark-gfm.h>
35 char oid[GIT_OID_HEXSZ + 1];
36 char parentoid[GIT_OID_HEXSZ + 1];
38 const git_signature *author;
39 const git_signature *committer;
46 git_tree *commit_tree;
47 git_tree *parent_tree;
53 struct deltainfo **deltas;
57 static git_repository *repo;
59 static const char *relpath = "";
60 static const char *repodir;
62 static char *name = "";
63 static char *strippedname = "";
64 static char description[255];
65 static char cloneurl[1024];
66 static char *submodules;
67 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
69 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
71 static long long nlogcommits = -1; /* < 0 indicates not used */
73 bool htmlized; /* true if markdoown converted to HTML */
74 static char oldfilename[PATH_MAX]; /* filename of the last file */
77 static git_oid lastoid;
78 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
79 static FILE *rcachefp, *wcachefp;
80 static const char *cachefile;
83 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
87 r = snprintf(buf, bufsiz, "%s%s%s",
88 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
89 if (r < 0 || (size_t)r >= bufsiz)
90 errx(1, "path truncated: '%s%s%s'",
91 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
95 deltainfo_free(struct deltainfo *di)
99 git_patch_free(di->patch);
100 memset(di, 0, sizeof(*di));
105 commitinfo_getstats(struct commitinfo *ci)
107 struct deltainfo *di;
108 git_diff_options opts;
109 git_diff_find_options fopts;
110 const git_diff_delta *delta;
111 const git_diff_hunk *hunk;
112 const git_diff_line *line;
113 git_patch *patch = NULL;
114 size_t ndeltas, nhunks, nhunklines;
117 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
119 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
120 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
122 ci->parent_tree = NULL;
126 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
127 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
128 GIT_DIFF_IGNORE_SUBMODULES |
129 GIT_DIFF_INCLUDE_TYPECHANGE;
130 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
133 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
135 /* find renames and copies, exact matches (no heuristic) for renames. */
136 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
137 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
138 if (git_diff_find_similar(ci->diff, &fopts))
141 ndeltas = git_diff_num_deltas(ci->diff);
142 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
145 for (i = 0; i < ndeltas; i++) {
146 if (git_patch_from_diff(&patch, ci->diff, i))
149 if (!(di = calloc(1, sizeof(struct deltainfo))))
154 delta = git_patch_get_delta(patch);
156 /* skip stats for binary data */
157 if (delta->flags & GIT_DIFF_FLAG_BINARY)
160 nhunks = git_patch_num_hunks(patch);
161 for (j = 0; j < nhunks; j++) {
162 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
165 if (git_patch_get_line_in_hunk(&line, patch, j, k))
167 if (line->old_lineno == -1) {
170 } else if (line->new_lineno == -1) {
183 git_diff_free(ci->diff);
185 git_tree_free(ci->commit_tree);
186 ci->commit_tree = NULL;
187 git_tree_free(ci->parent_tree);
188 ci->parent_tree = NULL;
189 git_commit_free(ci->parent);
193 for (i = 0; i < ci->ndeltas; i++)
194 deltainfo_free(ci->deltas[i]);
206 commitinfo_free(struct commitinfo *ci)
213 for (i = 0; i < ci->ndeltas; i++)
214 deltainfo_free(ci->deltas[i]);
217 git_diff_free(ci->diff);
218 git_tree_free(ci->commit_tree);
219 git_tree_free(ci->parent_tree);
220 git_commit_free(ci->commit);
221 git_commit_free(ci->parent);
222 memset(ci, 0, sizeof(*ci));
227 commitinfo_getbyoid(const git_oid *id)
229 struct commitinfo *ci;
231 if (!(ci = calloc(1, sizeof(struct commitinfo))))
234 if (git_commit_lookup(&(ci->commit), repo, id))
238 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
239 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
241 ci->author = git_commit_author(ci->commit);
242 ci->committer = git_commit_committer(ci->commit);
243 ci->summary = git_commit_summary(ci->commit);
244 ci->msg = git_commit_message(ci->commit);
255 efopen(const char *name, const char *flags)
259 if (!(fp = fopen(name, flags)))
260 err(1, "fopen: '%s'", name);
265 /* Escape characters below as HTML 2.0 / XML 1.0. */
267 xmlencode(FILE *fp, const char *s, size_t len)
271 for (i = 0; *s && i < len; s++, i++) {
273 case '<': fputs("<", fp); break;
274 case '>': fputs(">", fp); break;
275 case '\'': fputs("'", fp); break;
276 case '&': fputs("&", fp); break;
277 case '"': fputs(""", fp); break;
278 default: fputc(*s, fp);
283 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
285 xmlencodeline(FILE *fp, const char *s, size_t len)
289 for (i = 0; *s && i < len; s++, i++) {
291 case '<': fputs("<", fp); break;
292 case '>': fputs(">", fp); break;
293 case '\'': fputs("'", fp); break;
294 case '&': fputs("&", fp); break;
295 case '"': fputs(""", fp); break;
296 case '\r': break; /* ignore CR */
297 case '\n': break; /* ignore LF */
298 default: putc(*s, fp);
303 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
305 xmlencodeline(FILE *fp, const char *s, size_t len)
309 for (i = 0; *s && i < len; s++, i++) {
311 case '<': fputs("<", fp); break;
312 case '>': fputs(">", fp); break;
313 case '\'': fputs("'", fp); break;
314 case '&': fputs("&", fp); break;
315 case '"': fputs(""", fp); break;
316 case '\r': break; /* ignore CR */
317 case '\n': break; /* ignore LF */
318 default: putc(*s, fp);
324 mkdirp(const char *path)
326 char tmp[PATH_MAX], *p;
328 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
329 errx(1, "path truncated: '%s'", path);
330 for (p = tmp + (tmp[0] == '/'); *p; p++) {
334 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
338 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
344 printtimez(FILE *fp, const git_time *intime)
350 t = (time_t)intime->time;
351 if (!(intm = gmtime(&t)))
353 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
358 printtime(FILE *fp, const git_time *intime)
364 t = (time_t)intime->time + (intime->offset * 60);
365 if (!(intm = gmtime(&t)))
367 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
368 if (intime->offset < 0)
369 fprintf(fp, "%s -%02d%02d", out,
370 -(intime->offset) / 60, -(intime->offset) % 60);
372 fprintf(fp, "%s +%02d%02d", out,
373 intime->offset / 60, intime->offset % 60);
377 printtimeshort(FILE *fp, const git_time *intime)
383 t = (time_t)intime->time;
384 if (!(intm = gmtime(&t)))
386 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
391 writeheader(FILE *fp, const char *title)
393 fputs("<!DOCTYPE html>\n"
394 "<html lang=\"en\">\n<head>\n"
395 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
396 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
398 xmlencode(fp, title, strlen(title));
399 if (title[0] && strippedname[0])
401 xmlencode(fp, strippedname, strlen(strippedname));
404 xmlencode(fp, description, strlen(description));
405 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
406 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
408 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
409 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
410 fputs("</head>\n<body>\n<table><tr><td>", fp);
411 fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
413 fputs("</td><td><h1>", fp);
414 xmlencode(fp, strippedname, strlen(strippedname));
415 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
416 xmlencode(fp, description, strlen(description));
417 fputs("</span></td></tr>", fp);
419 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
420 xmlencode(fp, cloneurl, strlen(cloneurl));
422 xmlencode(fp, cloneurl, strlen(cloneurl));
423 fputs("</a></span></td></tr>", fp);
425 fputs("<tr><td></td><td>\n", fp);
426 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
427 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
428 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
430 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
431 relpath, submodules);
433 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
436 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
438 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
439 relpath, strippedname);
440 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
444 writefooter(FILE *fp)
446 fputs("</div>\n</body>\n</html>\n", fp);
450 get_ext(const char *filename)
452 const char *dot = strrchr(filename, '.');
453 if(!dot || dot == filename) return "";
458 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
466 html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
467 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
473 int stdout_copy = dup(1);
478 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
479 strncat(cmd, filename, strlen(filename) + 1);
480 FILE *child = popen(cmd, "w");
482 printf("child is null: %s", strerror(errno));
486 // Give code to highlight through STDIN:
488 for (i = 0; *s && i < len; s++, i++) {
489 fprintf(child, "%c", *s);
496 dup2(stdout_copy, 1);
499 fprintf(fp, "%s", html);
502 fprintf(fp, "<pre>%s</pre>", s);
507 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
509 const char *s = git_blob_rawcontent(blob);
510 git_off_t len = git_blob_rawsize(blob);
513 call_chroma(filename, fp, s, len);
518 printcommit(FILE *fp, struct commitinfo *ci)
520 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
521 relpath, ci->oid, ci->oid);
523 if (ci->parentoid[0])
524 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
525 relpath, ci->parentoid, ci->parentoid);
528 fputs("<b>Author:</b> ", fp);
529 xmlencode(fp, ci->author->name, strlen(ci->author->name));
530 fputs(" <<a href=\"mailto:", fp);
531 xmlencode(fp, ci->author->email, strlen(ci->author->email));
533 xmlencode(fp, ci->author->email, strlen(ci->author->email));
534 fputs("</a>>\n<b>Date:</b> ", fp);
535 printtime(fp, &(ci->author->when));
540 xmlencode(fp, ci->msg, strlen(ci->msg));
546 printshowfile(FILE *fp, struct commitinfo *ci)
548 const git_diff_delta *delta;
549 const git_diff_hunk *hunk;
550 const git_diff_line *line;
552 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
561 if (ci->filecount > 1000 ||
562 ci->ndeltas > 1000 ||
563 ci->addcount > 100000 ||
564 ci->delcount > 100000) {
565 fputs("Diff is too large, output suppressed.\n", fp);
570 fputs("<b>Diffstat:</b>\n<table>", fp);
571 for (i = 0; i < ci->ndeltas; i++) {
572 delta = git_patch_get_delta(ci->deltas[i]->patch);
574 switch (delta->status) {
575 case GIT_DELTA_ADDED: c = 'A'; break;
576 case GIT_DELTA_COPIED: c = 'C'; break;
577 case GIT_DELTA_DELETED: c = 'D'; break;
578 case GIT_DELTA_MODIFIED: c = 'M'; break;
579 case GIT_DELTA_RENAMED: c = 'R'; break;
580 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
581 default: c = ' '; break;
584 fprintf(fp, "<tr><td>%c", c);
586 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
588 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
589 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
590 if (strcmp(delta->old_file.path, delta->new_file.path)) {
591 fputs(" -> ", fp);
592 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
595 add = ci->deltas[i]->addcount;
596 del = ci->deltas[i]->delcount;
598 total = sizeof(linestr) - 2;
599 if (changed > total) {
601 add = ((float)total / changed * add) + 1;
603 del = ((float)total / changed * del) + 1;
605 memset(&linestr, '+', add);
606 memset(&linestr[add], '-', del);
608 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
609 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
610 fwrite(&linestr, 1, add, fp);
611 fputs("</span><span class=\"d\">", fp);
612 fwrite(&linestr[add], 1, del, fp);
613 fputs("</span></td></tr>\n", fp);
615 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
616 ci->filecount, ci->filecount == 1 ? "" : "s",
617 ci->addcount, ci->addcount == 1 ? "" : "s",
618 ci->delcount, ci->delcount == 1 ? "" : "s");
622 for (i = 0; i < ci->ndeltas; i++) {
623 patch = ci->deltas[i]->patch;
624 delta = git_patch_get_delta(patch);
625 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
626 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
627 fputs(".html\">", fp);
628 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
629 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
630 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
631 fprintf(fp, ".html\">");
632 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
633 fprintf(fp, "</a></b>\n");
635 /* check binary data */
636 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
637 fputs("Binary files differ.\n", fp);
641 nhunks = git_patch_num_hunks(patch);
642 for (j = 0; j < nhunks; j++) {
643 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
646 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
647 xmlencode(fp, hunk->header, hunk->header_len);
651 if (git_patch_get_line_in_hunk(&line, patch, j, k))
653 if (line->old_lineno == -1)
654 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
656 else if (line->new_lineno == -1)
657 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
661 xmlencodeline(fp, line->content, line->content_len);
663 if (line->old_lineno == -1 || line->new_lineno == -1)
671 writelogline(FILE *fp, struct commitinfo *ci)
673 fputs("<tr><td>", fp);
675 printtimeshort(fp, &(ci->author->when));
676 fputs("</td><td>", fp);
678 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
679 xmlencode(fp, ci->summary, strlen(ci->summary));
682 fputs("</td><td>", fp);
684 xmlencode(fp, ci->author->name, strlen(ci->author->name));
685 fputs("</td><td class=\"num\" align=\"right\">", fp);
686 fprintf(fp, "%zu", ci->filecount);
687 fputs("</td><td class=\"num\" align=\"right\">", fp);
688 fprintf(fp, "+%zu", ci->addcount);
689 fputs("</td><td class=\"num\" align=\"right\">", fp);
690 fprintf(fp, "-%zu", ci->delcount);
691 fputs("</td></tr>\n", fp);
695 writelog(FILE *fp, const git_oid *oid)
697 struct commitinfo *ci;
698 git_revwalk *w = NULL;
700 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
704 git_revwalk_new(&w, repo);
705 git_revwalk_push(w, oid);
706 git_revwalk_simplify_first_parent(w);
708 while (!git_revwalk_next(&id, w)) {
711 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
714 git_oid_tostr(oidstr, sizeof(oidstr), &id);
715 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
716 if (r < 0 || (size_t)r >= sizeof(path))
717 errx(1, "path truncated: 'commit/%s.html'", oidstr);
718 r = access(path, F_OK);
720 /* optimization: if there are no log lines to write and
721 the commit file already exists: skip the diffstat */
722 if (!nlogcommits && !r)
725 if (!(ci = commitinfo_getbyoid(&id)))
727 /* diffstat: for stagit HTML required for the log.html line */
728 if (commitinfo_getstats(ci) == -1)
731 if (nlogcommits < 0) {
732 writelogline(fp, ci);
733 } else if (nlogcommits > 0) {
734 writelogline(fp, ci);
736 if (!nlogcommits && ci->parentoid[0])
737 fputs("<tr><td></td><td colspan=\"5\">"
738 "More commits remaining [...]</td>"
743 writelogline(wcachefp, ci);
745 /* check if file exists if so skip it */
748 fpfile = efopen(path, "w");
749 writeheader(fpfile, ci->summary);
750 fputs("<pre>", fpfile);
751 printshowfile(fpfile, ci);
752 fputs("</pre>\n", fpfile);
767 printcommitatom(FILE *fp, struct commitinfo *ci)
769 fputs("<entry>\n", fp);
771 fprintf(fp, "<id>%s</id>\n", ci->oid);
773 fputs("<published>", fp);
774 printtimez(fp, &(ci->author->when));
775 fputs("</published>\n", fp);
778 fputs("<updated>", fp);
779 printtimez(fp, &(ci->committer->when));
780 fputs("</updated>\n", fp);
783 fputs("<title type=\"text\">", fp);
784 xmlencode(fp, ci->summary, strlen(ci->summary));
785 fputs("</title>\n", fp);
787 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
791 fputs("<author>\n<name>", fp);
792 xmlencode(fp, ci->author->name, strlen(ci->author->name));
793 fputs("</name>\n<email>", fp);
794 xmlencode(fp, ci->author->email, strlen(ci->author->email));
795 fputs("</email>\n</author>\n", fp);
798 fputs("<content type=\"text\">", fp);
799 fprintf(fp, "commit %s\n", ci->oid);
800 if (ci->parentoid[0])
801 fprintf(fp, "parent %s\n", ci->parentoid);
803 fputs("Author: ", fp);
804 xmlencode(fp, ci->author->name, strlen(ci->author->name));
806 xmlencode(fp, ci->author->email, strlen(ci->author->email));
807 fputs(">\nDate: ", fp);
808 printtime(fp, &(ci->author->when));
813 xmlencode(fp, ci->msg, strlen(ci->msg));
815 fputs("\n</content>\n</entry>\n", fp);
821 struct commitinfo *ci;
822 git_revwalk *w = NULL;
824 size_t i, m = 100; /* last 'm' commits */
826 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
827 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
828 xmlencode(fp, strippedname, strlen(strippedname));
829 fputs(", branch HEAD</title>\n<subtitle>", fp);
830 xmlencode(fp, description, strlen(description));
831 fputs("</subtitle>\n", fp);
833 git_revwalk_new(&w, repo);
834 git_revwalk_push_head(w);
835 git_revwalk_simplify_first_parent(w);
837 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
838 if (!(ci = commitinfo_getbyoid(&id)))
840 printcommitatom(fp, ci);
845 fputs("</feed>\n", fp);
853 int value = var * 10 + .5;
858 convertbytes(int bytes)
860 bytes = (float)bytes;
861 static char outp[25];
862 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
863 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
864 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
869 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
871 char tmp[PATH_MAX] = "", *d;
875 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
876 errx(1, "path truncated: '%s'", fpath);
877 if (!(d = dirname(tmp)))
881 for (p = fpath, tmp[0] = '\0'; *p; p++) {
882 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
883 errx(1, "path truncated: '../%s'", tmp);
887 fp = efopen(fpath, "w");
888 writeheader(fp, filename);
890 xmlencode(fp, filename, strlen(filename));
891 fprintf(fp, " (%s)", convertbytes((int)filesize));
894 char newfpath[PATH_MAX];
895 char newfilename[PATH_MAX];
896 if (strcmp(get_ext(filename), "md") == 0) {
897 fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename);
898 strcpy(newfpath, fpath);
899 strcat(newfpath, "-raw");
901 strcpy(newfilename, filename);
902 strcat(newfilename, "-raw");
903 strcpy(oldfilename, filename);
906 writeblob(obj, newfpath, newfilename, filesize);
907 } else if (strcmp(get_ext(filename), "md-raw" ) == 0) {
908 fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename);
912 fputs(".</p><hr/>", fp);
914 if (git_blob_is_binary((git_blob *)obj)) {
915 fputs("<p>Binary file.</p>\n", fp);
917 writeblobhtml(filename, fp, (git_blob *)obj);
929 filemode(git_filemode_t m)
931 static char mode[11];
933 memset(mode, '-', sizeof(mode) - 1);
944 else if (S_ISFIFO(m))
948 else if (S_ISSOCK(m))
953 if (m & S_IRUSR) mode[1] = 'r';
954 if (m & S_IWUSR) mode[2] = 'w';
955 if (m & S_IXUSR) mode[3] = 'x';
956 if (m & S_IRGRP) mode[4] = 'r';
957 if (m & S_IWGRP) mode[5] = 'w';
958 if (m & S_IXGRP) mode[6] = 'x';
959 if (m & S_IROTH) mode[7] = 'r';
960 if (m & S_IWOTH) mode[8] = 'w';
961 if (m & S_IXOTH) mode[9] = 'x';
963 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
964 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
965 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
971 writefilestree(FILE *fp, git_tree *tree, const char *path)
973 const git_tree_entry *entry = NULL;
974 git_submodule *module = NULL;
975 git_object *obj = NULL;
977 const char *entryname;
978 char filepath[PATH_MAX], entrypath[PATH_MAX];
982 count = git_tree_entrycount(tree);
983 for (i = 0; i < count; i++) {
984 if (!(entry = git_tree_entry_byindex(tree, i)) ||
985 !(entryname = git_tree_entry_name(entry)))
987 joinpath(entrypath, sizeof(entrypath), path, entryname);
989 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
991 if (r < 0 || (size_t)r >= sizeof(filepath))
992 errx(1, "path truncated: 'file/%s.html'", entrypath);
994 if (!git_tree_entry_to_object(&obj, repo, entry)) {
995 switch (git_object_type(obj)) {
1000 ret = writefilestree(fp, (git_tree *)obj,
1002 git_object_free(obj);
1007 git_object_free(obj);
1011 filesize = git_blob_rawsize((git_blob *)obj);
1012 writeblob(obj, filepath, entryname, filesize);
1014 fputs("<tr><td>", fp);
1015 fputs(filemode(git_tree_entry_filemode(entry)), fp);
1016 fprintf(fp, "</td><td><a href=\"%s", relpath);
1017 xmlencode(fp, filepath, strlen(filepath));
1019 xmlencode(fp, entrypath, strlen(entrypath));
1020 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
1021 fprintf(fp, "%s", convertbytes((int)filesize));
1022 fputs("</td></tr>\n", fp);
1023 git_object_free(obj);
1024 } else if (!git_submodule_lookup(&module, repo, entryname)) {
1025 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
1027 xmlencode(fp, entrypath, strlen(entrypath));
1028 git_submodule_free(module);
1029 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
1037 writefiles(FILE *fp, const git_oid *id)
1039 git_tree *tree = NULL;
1040 git_commit *commit = NULL;
1043 fputs("<table id=\"files\"><thead>\n<tr>"
1044 "<td><b>Mode</b></td><td><b>Name</b></td>"
1045 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
1046 "</tr>\n</thead><tbody>\n", fp);
1048 if (!git_commit_lookup(&commit, repo, id) &&
1049 !git_commit_tree(&tree, commit))
1050 ret = writefilestree(fp, tree, "");
1052 fputs("</tbody></table>", fp);
1054 git_commit_free(commit);
1055 git_tree_free(tree);
1061 refs_cmp(const void *v1, const void *v2)
1063 git_reference *r1 = (*(git_reference **)v1);
1064 git_reference *r2 = (*(git_reference **)v2);
1067 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1070 return strcmp(git_reference_shorthand(r1),
1071 git_reference_shorthand(r2));
1077 struct commitinfo *ci;
1078 const git_oid *id = NULL;
1079 git_object *obj = NULL;
1080 git_reference *dref = NULL, *r, *ref = NULL;
1081 git_reference_iterator *it = NULL;
1082 git_reference **refs = NULL;
1083 size_t count, i, j, refcount;
1084 const char *titles[] = { "Branches", "Tags" };
1085 const char *ids[] = { "branches", "tags" };
1088 if (git_reference_iterator_new(&it, repo))
1091 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1092 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1094 refs[refcount] = ref;
1096 git_reference_iterator_free(it);
1098 /* sort by type then shorthand name */
1099 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1101 for (j = 0; j < 2; j++) {
1102 for (i = 0, count = 0; i < refcount; i++) {
1103 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1104 !(git_reference_is_tag(refs[i]) && j == 1))
1107 switch (git_reference_type(refs[i])) {
1108 case GIT_REF_SYMBOLIC:
1109 if (git_reference_resolve(&dref, refs[i]))
1119 if (!git_reference_target(r) ||
1120 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1122 if (!(id = git_object_id(obj)))
1124 if (!(ci = commitinfo_getbyoid(id)))
1127 /* print header if it has an entry (first). */
1129 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1130 "<thead>\n<tr><td><b>Name</b></td>"
1131 "<td><b>Last commit date</b></td>"
1132 "<td><b>Author</b></td>\n</tr>\n"
1133 "</thead><tbody>\n",
1138 name = git_reference_shorthand(r);
1140 fputs("<tr><td>", fp);
1141 xmlencode(fp, name, strlen(name));
1142 fputs("</td><td>", fp);
1144 printtimeshort(fp, &(ci->author->when));
1145 fputs("</td><td>", fp);
1147 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1148 fputs("</td></tr>\n", fp);
1152 commitinfo_free(ci);
1153 git_object_free(obj);
1155 git_reference_free(dref);
1160 fputs("</tbody></table><br/>", fp);
1164 git_object_free(obj);
1165 git_reference_free(dref);
1167 for (i = 0; i < refcount; i++)
1168 git_reference_free(refs[i]);
1177 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1182 main(int argc, char *argv[])
1184 git_object *obj = NULL;
1185 const git_oid *head = NULL;
1188 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1189 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1193 for (i = 1; i < argc; i++) {
1194 if (argv[i][0] != '-') {
1198 } else if (argv[i][1] == 'c') {
1199 if (nlogcommits > 0 || i + 1 >= argc)
1201 cachefile = argv[++i];
1202 } else if (argv[i][1] == 'l') {
1203 if (cachefile || i + 1 >= argc)
1206 nlogcommits = strtoll(argv[++i], &p, 10);
1207 if (argv[i][0] == '\0' || *p != '\0' ||
1208 nlogcommits <= 0 || errno)
1215 if (!realpath(repodir, repodirabs))
1221 if (unveil(repodir, "r") == -1)
1222 err(1, "unveil: %s", repodir);
1223 if (unveil(".", "rwc") == -1)
1224 err(1, "unveil: .");
1225 if (cachefile && unveil(cachefile, "rwc") == -1)
1226 err(1, "unveil: %s", cachefile);
1229 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1232 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1237 if (git_repository_open_ext(&repo, repodir,
1238 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1239 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1244 if (!git_revparse_single(&obj, repo, "HEAD"))
1245 head = git_object_id(obj);
1246 git_object_free(obj);
1248 /* use directory name as name */
1249 if ((name = strrchr(repodirabs, '/')))
1256 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1257 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1258 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1259 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1261 /* strip .git suffix */
1262 if (!(strippedname = strdup(name)))
1264 if ((p = strrchr(strippedname, '.')))
1265 if (!strcmp(p, ".git"))
1268 /* read description or .git/description */
1269 joinpath(path, sizeof(path), repodir, "description");
1270 if (!(fpread = fopen(path, "r"))) {
1271 joinpath(path, sizeof(path), repodir, ".git/description");
1272 fpread = fopen(path, "r");
1275 if (!fgets(description, sizeof(description), fpread))
1276 description[0] = '\0';
1280 /* read url or .git/url */
1281 joinpath(path, sizeof(path), repodir, "url");
1282 if (!(fpread = fopen(path, "r"))) {
1283 joinpath(path, sizeof(path), repodir, ".git/url");
1284 fpread = fopen(path, "r");
1287 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1289 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1294 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1295 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1296 git_object_type(obj) == GIT_OBJ_BLOB)
1297 license = licensefiles[i] + strlen("HEAD:");
1298 git_object_free(obj);
1302 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1303 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1304 git_object_type(obj) == GIT_OBJ_BLOB)
1305 readme = readmefiles[i] + strlen("HEAD:");
1306 git_object_free(obj);
1309 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1310 git_object_type(obj) == GIT_OBJ_BLOB)
1311 submodules = ".gitmodules";
1312 git_object_free(obj);
1314 /* Generate tarball */
1316 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1317 strippedname, repodir);
1321 fp = efopen("log.html", "w");
1323 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1324 writeheader(fp, "Log");
1325 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1326 "<td><b>Commit</b></td>"
1327 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1328 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1329 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1331 if (cachefile && head) {
1332 /* read from cache file (does not need to exist) */
1333 if ((rcachefp = fopen(cachefile, "r"))) {
1334 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1335 errx(1, "%s: no object id", cachefile);
1336 if (git_oid_fromstr(&lastoid, lastoidstr))
1337 errx(1, "%s: invalid object id", cachefile);
1340 /* write log to (temporary) cache */
1341 if ((fd = mkstemp(tmppath)) == -1)
1343 if (!(wcachefp = fdopen(fd, "w")))
1344 err(1, "fdopen: '%s'", tmppath);
1345 /* write last commit id (HEAD) */
1346 git_oid_tostr(buf, sizeof(buf), head);
1347 fprintf(wcachefp, "%s\n", buf);
1352 /* append previous log to log.html and the new cache */
1353 while (!feof(rcachefp)) {
1354 n = fread(buf, 1, sizeof(buf), rcachefp);
1355 if (ferror(rcachefp))
1357 if (fwrite(buf, 1, n, fp) != n ||
1358 fwrite(buf, 1, n, wcachefp) != n)
1369 fputs("</tbody></table>", fp);
1373 /* files for HEAD */
1374 fp = efopen("files.html", "w");
1375 writeheader(fp, "Files");
1377 writefiles(fp, head);
1381 cp("files.html", "index.html");
1383 /* summary page with branches and tags */
1384 fp = efopen("refs.html", "w");
1385 writeheader(fp, "Refs");
1391 fp = efopen("atom.xml", "w");
1395 /* rename new cache file on success */
1396 if (cachefile && head) {
1397 if (rename(tmppath, cachefile))
1398 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1399 umask((mask = umask(0)));
1400 if (chmod(cachefile,
1401 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1402 err(1, "chmod: '%s'", cachefile);
1406 git_repository_free(repo);
1407 git_libgit2_shutdown();