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 *baseurl = ""; /* base URL to make absolute RSS/Atom URI */
60 static const char *relpath = "";
61 static const char *repodir;
63 static char *name = "";
64 static char *strippedname = "";
65 static char description[255];
66 static char cloneurl[1024];
67 static char *submodules;
68 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
70 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
72 static long long nlogcommits = -1; /* < 0 indicates not used */
74 bool htmlized; /* true if markdoown converted to HTML */
75 static char oldfilename[PATH_MAX]; /* filename of the last file */
78 static git_oid lastoid;
79 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
80 static FILE *rcachefp, *wcachefp;
81 static const char *cachefile;
84 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
88 r = snprintf(buf, bufsiz, "%s%s%s",
89 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
90 if (r < 0 || (size_t)r >= bufsiz)
91 errx(1, "path truncated: '%s%s%s'",
92 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
96 deltainfo_free(struct deltainfo *di)
100 git_patch_free(di->patch);
101 memset(di, 0, sizeof(*di));
106 commitinfo_getstats(struct commitinfo *ci)
108 struct deltainfo *di;
109 git_diff_options opts;
110 git_diff_find_options fopts;
111 const git_diff_delta *delta;
112 const git_diff_hunk *hunk;
113 const git_diff_line *line;
114 git_patch *patch = NULL;
115 size_t ndeltas, nhunks, nhunklines;
118 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
120 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
121 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
123 ci->parent_tree = NULL;
127 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
128 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
129 GIT_DIFF_IGNORE_SUBMODULES |
130 GIT_DIFF_INCLUDE_TYPECHANGE;
131 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
134 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
136 /* find renames and copies, exact matches (no heuristic) for renames. */
137 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
138 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
139 if (git_diff_find_similar(ci->diff, &fopts))
142 ndeltas = git_diff_num_deltas(ci->diff);
143 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
146 for (i = 0; i < ndeltas; i++) {
147 if (git_patch_from_diff(&patch, ci->diff, i))
150 if (!(di = calloc(1, sizeof(struct deltainfo))))
155 delta = git_patch_get_delta(patch);
157 /* skip stats for binary data */
158 if (delta->flags & GIT_DIFF_FLAG_BINARY)
161 nhunks = git_patch_num_hunks(patch);
162 for (j = 0; j < nhunks; j++) {
163 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
166 if (git_patch_get_line_in_hunk(&line, patch, j, k))
168 if (line->old_lineno == -1) {
171 } else if (line->new_lineno == -1) {
184 git_diff_free(ci->diff);
186 git_tree_free(ci->commit_tree);
187 ci->commit_tree = NULL;
188 git_tree_free(ci->parent_tree);
189 ci->parent_tree = NULL;
190 git_commit_free(ci->parent);
194 for (i = 0; i < ci->ndeltas; i++)
195 deltainfo_free(ci->deltas[i]);
207 commitinfo_free(struct commitinfo *ci)
214 for (i = 0; i < ci->ndeltas; i++)
215 deltainfo_free(ci->deltas[i]);
218 git_diff_free(ci->diff);
219 git_tree_free(ci->commit_tree);
220 git_tree_free(ci->parent_tree);
221 git_commit_free(ci->commit);
222 git_commit_free(ci->parent);
223 memset(ci, 0, sizeof(*ci));
228 commitinfo_getbyoid(const git_oid *id)
230 struct commitinfo *ci;
232 if (!(ci = calloc(1, sizeof(struct commitinfo))))
235 if (git_commit_lookup(&(ci->commit), repo, id))
239 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
240 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
242 ci->author = git_commit_author(ci->commit);
243 ci->committer = git_commit_committer(ci->commit);
244 ci->summary = git_commit_summary(ci->commit);
245 ci->msg = git_commit_message(ci->commit);
256 efopen(const char *name, const char *flags)
260 if (!(fp = fopen(name, flags)))
261 err(1, "fopen: '%s'", name);
266 /* Escape characters below as HTML 2.0 / XML 1.0. */
268 xmlencode(FILE *fp, const char *s, size_t len)
272 for (i = 0; *s && i < len; s++, i++) {
274 case '<': fputs("<", fp); break;
275 case '>': fputs(">", fp); break;
276 case '\'': fputs("'", fp); break;
277 case '&': fputs("&", fp); break;
278 case '"': fputs(""", fp); break;
279 default: fputc(*s, fp);
284 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
286 xmlencodeline(FILE *fp, const char *s, size_t len)
290 for (i = 0; *s && i < len; s++, i++) {
292 case '<': fputs("<", fp); break;
293 case '>': fputs(">", fp); break;
294 case '\'': fputs("'", fp); break;
295 case '&': fputs("&", fp); break;
296 case '"': fputs(""", fp); break;
297 case '\r': break; /* ignore CR */
298 case '\n': break; /* ignore LF */
299 default: putc(*s, fp);
304 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
306 xmlencodeline(FILE *fp, const char *s, size_t len)
310 for (i = 0; *s && i < len; s++, i++) {
312 case '<': fputs("<", fp); break;
313 case '>': fputs(">", fp); break;
314 case '\'': fputs("'", fp); break;
315 case '&': fputs("&", fp); break;
316 case '"': fputs(""", fp); break;
317 case '\r': break; /* ignore CR */
318 case '\n': break; /* ignore LF */
319 default: putc(*s, fp);
324 /* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
326 xmlencodeline(FILE *fp, const char *s, size_t len)
330 for (i = 0; *s && i < len; s++, i++) {
332 case '<': fputs("<", fp); break;
333 case '>': fputs(">", fp); break;
334 case '\'': fputs("'", fp); break;
335 case '&': fputs("&", fp); break;
336 case '"': fputs(""", fp); break;
337 case '\r': break; /* ignore CR */
338 case '\n': break; /* ignore LF */
339 default: putc(*s, fp);
345 mkdirp(const char *path)
347 char tmp[PATH_MAX], *p;
349 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
350 errx(1, "path truncated: '%s'", path);
351 for (p = tmp + (tmp[0] == '/'); *p; p++) {
355 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
359 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
365 printtimez(FILE *fp, const git_time *intime)
371 t = (time_t)intime->time;
372 if (!(intm = gmtime(&t)))
374 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
379 printtime(FILE *fp, const git_time *intime)
385 t = (time_t)intime->time + (intime->offset * 60);
386 if (!(intm = gmtime(&t)))
388 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
389 if (intime->offset < 0)
390 fprintf(fp, "%s -%02d%02d", out,
391 -(intime->offset) / 60, -(intime->offset) % 60);
393 fprintf(fp, "%s +%02d%02d", out,
394 intime->offset / 60, intime->offset % 60);
398 printtimeshort(FILE *fp, const git_time *intime)
404 t = (time_t)intime->time;
405 if (!(intm = gmtime(&t)))
407 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
412 writeheader(FILE *fp, const char *title)
414 fputs("<!DOCTYPE html>\n"
415 "<html lang=\"en\">\n<head>\n"
416 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
417 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
419 xmlencode(fp, title, strlen(title));
420 if (title[0] && strippedname[0])
422 xmlencode(fp, strippedname, strlen(strippedname));
425 xmlencode(fp, description, strlen(description));
426 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
427 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
429 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
430 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
431 fputs("</head>\n<body>\n<table><tr><td>", fp);
432 fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
434 fputs("</td><td><h1>", fp);
435 xmlencode(fp, strippedname, strlen(strippedname));
436 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
437 xmlencode(fp, description, strlen(description));
438 fputs("</span></td></tr>", fp);
440 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
441 xmlencode(fp, cloneurl, strlen(cloneurl));
443 xmlencode(fp, cloneurl, strlen(cloneurl));
444 fputs("</a></span></td></tr>", fp);
446 fputs("<tr><td></td><td>\n", fp);
447 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
448 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
449 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
451 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
452 relpath, submodules);
454 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
457 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
459 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
460 relpath, strippedname);
461 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
465 writefooter(FILE *fp)
467 fputs("</div>\n</body>\n</html>\n", fp);
471 get_ext(const char *filename)
473 const char *dot = strrchr(filename, '.');
474 if(!dot || dot == filename) return "";
479 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
487 html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
488 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
494 int stdout_copy = dup(1);
499 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
500 strncat(cmd, filename, strlen(filename) + 1);
501 FILE *child = popen(cmd, "w");
503 printf("child is null: %s", strerror(errno));
507 // Give code to highlight through STDIN:
509 for (i = 0; *s && i < len; s++, i++) {
510 fprintf(child, "%c", *s);
517 dup2(stdout_copy, 1);
520 fprintf(fp, "%s", html);
523 fprintf(fp, "<pre>%s</pre>", s);
528 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
530 const char *s = git_blob_rawcontent(blob);
531 git_off_t len = git_blob_rawsize(blob);
534 call_chroma(filename, fp, s, len);
539 printcommit(FILE *fp, struct commitinfo *ci)
541 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
542 relpath, ci->oid, ci->oid);
544 if (ci->parentoid[0])
545 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
546 relpath, ci->parentoid, ci->parentoid);
549 fputs("<b>Author:</b> ", fp);
550 xmlencode(fp, ci->author->name, strlen(ci->author->name));
551 fputs(" <<a href=\"mailto:", fp);
552 xmlencode(fp, ci->author->email, strlen(ci->author->email));
554 xmlencode(fp, ci->author->email, strlen(ci->author->email));
555 fputs("</a>>\n<b>Date:</b> ", fp);
556 printtime(fp, &(ci->author->when));
561 xmlencode(fp, ci->msg, strlen(ci->msg));
567 printshowfile(FILE *fp, struct commitinfo *ci)
569 const git_diff_delta *delta;
570 const git_diff_hunk *hunk;
571 const git_diff_line *line;
573 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
582 if (ci->filecount > 1000 ||
583 ci->ndeltas > 1000 ||
584 ci->addcount > 100000 ||
585 ci->delcount > 100000) {
586 fputs("Diff is too large, output suppressed.\n", fp);
591 fputs("<b>Diffstat:</b>\n<table>", fp);
592 for (i = 0; i < ci->ndeltas; i++) {
593 delta = git_patch_get_delta(ci->deltas[i]->patch);
595 switch (delta->status) {
596 case GIT_DELTA_ADDED: c = 'A'; break;
597 case GIT_DELTA_COPIED: c = 'C'; break;
598 case GIT_DELTA_DELETED: c = 'D'; break;
599 case GIT_DELTA_MODIFIED: c = 'M'; break;
600 case GIT_DELTA_RENAMED: c = 'R'; break;
601 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
602 default: c = ' '; break;
605 fprintf(fp, "<tr><td>%c", c);
607 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
609 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
610 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
611 if (strcmp(delta->old_file.path, delta->new_file.path)) {
612 fputs(" -> ", fp);
613 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
616 add = ci->deltas[i]->addcount;
617 del = ci->deltas[i]->delcount;
619 total = sizeof(linestr) - 2;
620 if (changed > total) {
622 add = ((float)total / changed * add) + 1;
624 del = ((float)total / changed * del) + 1;
626 memset(&linestr, '+', add);
627 memset(&linestr[add], '-', del);
629 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
630 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
631 fwrite(&linestr, 1, add, fp);
632 fputs("</span><span class=\"d\">", fp);
633 fwrite(&linestr[add], 1, del, fp);
634 fputs("</span></td></tr>\n", fp);
636 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
637 ci->filecount, ci->filecount == 1 ? "" : "s",
638 ci->addcount, ci->addcount == 1 ? "" : "s",
639 ci->delcount, ci->delcount == 1 ? "" : "s");
643 for (i = 0; i < ci->ndeltas; i++) {
644 patch = ci->deltas[i]->patch;
645 delta = git_patch_get_delta(patch);
646 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
647 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
648 fputs(".html\">", fp);
649 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
650 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
651 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
652 fprintf(fp, ".html\">");
653 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
654 fprintf(fp, "</a></b>\n");
656 /* check binary data */
657 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
658 fputs("Binary files differ.\n", fp);
662 nhunks = git_patch_num_hunks(patch);
663 for (j = 0; j < nhunks; j++) {
664 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
667 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
668 xmlencode(fp, hunk->header, hunk->header_len);
672 if (git_patch_get_line_in_hunk(&line, patch, j, k))
674 if (line->old_lineno == -1)
675 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
677 else if (line->new_lineno == -1)
678 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
682 xmlencodeline(fp, line->content, line->content_len);
684 if (line->old_lineno == -1 || line->new_lineno == -1)
692 writelogline(FILE *fp, struct commitinfo *ci)
694 fputs("<tr><td>", fp);
696 printtimeshort(fp, &(ci->author->when));
697 fputs("</td><td>", fp);
699 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
700 xmlencode(fp, ci->summary, strlen(ci->summary));
703 fputs("</td><td>", fp);
705 xmlencode(fp, ci->author->name, strlen(ci->author->name));
706 fputs("</td><td class=\"num\" align=\"right\">", fp);
707 fprintf(fp, "%zu", ci->filecount);
708 fputs("</td><td class=\"num\" align=\"right\">", fp);
709 fprintf(fp, "+%zu", ci->addcount);
710 fputs("</td><td class=\"num\" align=\"right\">", fp);
711 fprintf(fp, "-%zu", ci->delcount);
712 fputs("</td></tr>\n", fp);
716 writelog(FILE *fp, const git_oid *oid)
718 struct commitinfo *ci;
719 git_revwalk *w = NULL;
721 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
725 git_revwalk_new(&w, repo);
726 git_revwalk_push(w, oid);
727 git_revwalk_simplify_first_parent(w);
729 while (!git_revwalk_next(&id, w)) {
732 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
735 git_oid_tostr(oidstr, sizeof(oidstr), &id);
736 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
737 if (r < 0 || (size_t)r >= sizeof(path))
738 errx(1, "path truncated: 'commit/%s.html'", oidstr);
739 r = access(path, F_OK);
741 /* optimization: if there are no log lines to write and
742 the commit file already exists: skip the diffstat */
743 if (!nlogcommits && !r)
746 if (!(ci = commitinfo_getbyoid(&id)))
748 /* diffstat: for stagit HTML required for the log.html line */
749 if (commitinfo_getstats(ci) == -1)
752 if (nlogcommits < 0) {
753 writelogline(fp, ci);
754 } else if (nlogcommits > 0) {
755 writelogline(fp, ci);
757 if (!nlogcommits && ci->parentoid[0])
758 fputs("<tr><td></td><td colspan=\"5\">"
759 "More commits remaining [...]</td>"
764 writelogline(wcachefp, ci);
766 /* check if file exists if so skip it */
769 fpfile = efopen(path, "w");
770 writeheader(fpfile, ci->summary);
771 fputs("<pre>", fpfile);
772 printshowfile(fpfile, ci);
773 fputs("</pre>\n", fpfile);
788 printcommitatom(FILE *fp, struct commitinfo *ci)
790 fputs("<entry>\n", fp);
792 fprintf(fp, "<id>%s</id>\n", ci->oid);
794 fputs("<published>", fp);
795 printtimez(fp, &(ci->author->when));
796 fputs("</published>\n", fp);
799 fputs("<updated>", fp);
800 printtimez(fp, &(ci->committer->when));
801 fputs("</updated>\n", fp);
804 fputs("<title type=\"text\">", fp);
805 xmlencode(fp, ci->summary, strlen(ci->summary));
806 fputs("</title>\n", fp);
808 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n",
812 fputs("<author>\n<name>", fp);
813 xmlencode(fp, ci->author->name, strlen(ci->author->name));
814 fputs("</name>\n<email>", fp);
815 xmlencode(fp, ci->author->email, strlen(ci->author->email));
816 fputs("</email>\n</author>\n", fp);
819 fputs("<content type=\"text\">", fp);
820 fprintf(fp, "commit %s\n", ci->oid);
821 if (ci->parentoid[0])
822 fprintf(fp, "parent %s\n", ci->parentoid);
824 fputs("Author: ", fp);
825 xmlencode(fp, ci->author->name, strlen(ci->author->name));
827 xmlencode(fp, ci->author->email, strlen(ci->author->email));
828 fputs(">\nDate: ", fp);
829 printtime(fp, &(ci->author->when));
834 xmlencode(fp, ci->msg, strlen(ci->msg));
836 fputs("\n</content>\n</entry>\n", fp);
842 struct commitinfo *ci;
843 git_revwalk *w = NULL;
845 size_t i, m = 100; /* last 'm' commits */
847 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
848 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
849 xmlencode(fp, strippedname, strlen(strippedname));
850 fputs(", branch HEAD</title>\n<subtitle>", fp);
851 xmlencode(fp, description, strlen(description));
852 fputs("</subtitle>\n", fp);
854 git_revwalk_new(&w, repo);
855 git_revwalk_push_head(w);
856 git_revwalk_simplify_first_parent(w);
858 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
859 if (!(ci = commitinfo_getbyoid(&id)))
861 printcommitatom(fp, ci);
866 fputs("</feed>\n", fp);
874 int value = var * 10 + .5;
879 convertbytes(int bytes)
881 bytes = (float)bytes;
882 static char outp[25];
883 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
884 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
885 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
890 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
892 char tmp[PATH_MAX] = "", *d;
896 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
897 errx(1, "path truncated: '%s'", fpath);
898 if (!(d = dirname(tmp)))
902 for (p = fpath, tmp[0] = '\0'; *p; p++) {
903 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
904 errx(1, "path truncated: '../%s'", tmp);
908 fp = efopen(fpath, "w");
909 writeheader(fp, filename);
911 xmlencode(fp, filename, strlen(filename));
912 fprintf(fp, " (%s)", convertbytes((int)filesize));
915 char newfpath[PATH_MAX];
916 char newfilename[PATH_MAX];
917 if (strcmp(get_ext(filename), "md") == 0) {
918 fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename);
919 strcpy(newfpath, fpath);
920 strcat(newfpath, "-raw");
922 strcpy(newfilename, filename);
923 strcat(newfilename, "-raw");
924 strcpy(oldfilename, filename);
927 writeblob(obj, newfpath, newfilename, filesize);
928 } else if (strcmp(get_ext(filename), "md-raw" ) == 0) {
929 fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename);
933 fputs(".</p><hr/>", fp);
935 if (git_blob_is_binary((git_blob *)obj)) {
936 fputs("<p>Binary file.</p>\n", fp);
938 writeblobhtml(filename, fp, (git_blob *)obj);
950 filemode(git_filemode_t m)
952 static char mode[11];
954 memset(mode, '-', sizeof(mode) - 1);
965 else if (S_ISFIFO(m))
969 else if (S_ISSOCK(m))
974 if (m & S_IRUSR) mode[1] = 'r';
975 if (m & S_IWUSR) mode[2] = 'w';
976 if (m & S_IXUSR) mode[3] = 'x';
977 if (m & S_IRGRP) mode[4] = 'r';
978 if (m & S_IWGRP) mode[5] = 'w';
979 if (m & S_IXGRP) mode[6] = 'x';
980 if (m & S_IROTH) mode[7] = 'r';
981 if (m & S_IWOTH) mode[8] = 'w';
982 if (m & S_IXOTH) mode[9] = 'x';
984 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
985 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
986 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
992 writefilestree(FILE *fp, git_tree *tree, const char *path)
994 const git_tree_entry *entry = NULL;
995 git_submodule *module = NULL;
996 git_object *obj = NULL;
998 const char *entryname;
999 char filepath[PATH_MAX], entrypath[PATH_MAX];
1003 count = git_tree_entrycount(tree);
1004 for (i = 0; i < count; i++) {
1005 if (!(entry = git_tree_entry_byindex(tree, i)) ||
1006 !(entryname = git_tree_entry_name(entry)))
1008 joinpath(entrypath, sizeof(entrypath), path, entryname);
1010 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
1012 if (r < 0 || (size_t)r >= sizeof(filepath))
1013 errx(1, "path truncated: 'file/%s.html'", entrypath);
1015 if (!git_tree_entry_to_object(&obj, repo, entry)) {
1016 switch (git_object_type(obj)) {
1020 /* NOTE: recurses */
1021 ret = writefilestree(fp, (git_tree *)obj,
1023 git_object_free(obj);
1028 git_object_free(obj);
1032 filesize = git_blob_rawsize((git_blob *)obj);
1033 writeblob(obj, filepath, entryname, filesize);
1035 fputs("<tr><td>", fp);
1036 fputs(filemode(git_tree_entry_filemode(entry)), fp);
1037 fprintf(fp, "</td><td><a href=\"%s", relpath);
1038 xmlencode(fp, filepath, strlen(filepath));
1040 xmlencode(fp, entrypath, strlen(entrypath));
1041 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
1042 fprintf(fp, "%s", convertbytes((int)filesize));
1043 fputs("</td></tr>\n", fp);
1044 git_object_free(obj);
1045 } else if (!git_submodule_lookup(&module, repo, entryname)) {
1046 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
1048 xmlencode(fp, entrypath, strlen(entrypath));
1049 git_submodule_free(module);
1050 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
1058 writefiles(FILE *fp, const git_oid *id)
1060 git_tree *tree = NULL;
1061 git_commit *commit = NULL;
1064 fputs("<table id=\"files\"><thead>\n<tr>"
1065 "<td><b>Mode</b></td><td><b>Name</b></td>"
1066 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
1067 "</tr>\n</thead><tbody>\n", fp);
1069 if (!git_commit_lookup(&commit, repo, id) &&
1070 !git_commit_tree(&tree, commit))
1071 ret = writefilestree(fp, tree, "");
1073 fputs("</tbody></table>", fp);
1075 git_commit_free(commit);
1076 git_tree_free(tree);
1082 refs_cmp(const void *v1, const void *v2)
1084 git_reference *r1 = (*(git_reference **)v1);
1085 git_reference *r2 = (*(git_reference **)v2);
1088 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1091 return strcmp(git_reference_shorthand(r1),
1092 git_reference_shorthand(r2));
1098 struct commitinfo *ci;
1099 const git_oid *id = NULL;
1100 git_object *obj = NULL;
1101 git_reference *dref = NULL, *r, *ref = NULL;
1102 git_reference_iterator *it = NULL;
1103 git_reference **refs = NULL;
1104 size_t count, i, j, refcount;
1105 const char *titles[] = { "Branches", "Tags" };
1106 const char *ids[] = { "branches", "tags" };
1109 if (git_reference_iterator_new(&it, repo))
1112 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1113 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1115 refs[refcount] = ref;
1117 git_reference_iterator_free(it);
1119 /* sort by type then shorthand name */
1120 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1122 for (j = 0; j < 2; j++) {
1123 for (i = 0, count = 0; i < refcount; i++) {
1124 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1125 !(git_reference_is_tag(refs[i]) && j == 1))
1128 switch (git_reference_type(refs[i])) {
1129 case GIT_REF_SYMBOLIC:
1130 if (git_reference_resolve(&dref, refs[i]))
1140 if (!git_reference_target(r) ||
1141 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1143 if (!(id = git_object_id(obj)))
1145 if (!(ci = commitinfo_getbyoid(id)))
1148 /* print header if it has an entry (first). */
1150 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1151 "<thead>\n<tr><td><b>Name</b></td>"
1152 "<td><b>Last commit date</b></td>"
1153 "<td><b>Author</b></td>\n</tr>\n"
1154 "</thead><tbody>\n",
1159 name = git_reference_shorthand(r);
1161 fputs("<tr><td>", fp);
1162 xmlencode(fp, name, strlen(name));
1163 fputs("</td><td>", fp);
1165 printtimeshort(fp, &(ci->author->when));
1166 fputs("</td><td>", fp);
1168 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1169 fputs("</td></tr>\n", fp);
1173 commitinfo_free(ci);
1174 git_object_free(obj);
1176 git_reference_free(dref);
1181 fputs("</tbody></table><br/>", fp);
1185 git_object_free(obj);
1186 git_reference_free(dref);
1188 for (i = 0; i < refcount; i++)
1189 git_reference_free(refs[i]);
1198 fprintf(stderr, "%s [-c cachefile | -l commits] "
1199 "[-u baseurl] repodir\n", argv0);
1204 main(int argc, char *argv[])
1206 git_object *obj = NULL;
1207 const git_oid *head = NULL;
1210 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1211 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1215 for (i = 1; i < argc; i++) {
1216 if (argv[i][0] != '-') {
1220 } else if (argv[i][1] == 'c') {
1221 if (nlogcommits > 0 || i + 1 >= argc)
1223 cachefile = argv[++i];
1224 } else if (argv[i][1] == 'l') {
1225 if (cachefile || i + 1 >= argc)
1228 nlogcommits = strtoll(argv[++i], &p, 10);
1229 if (argv[i][0] == '\0' || *p != '\0' ||
1230 nlogcommits <= 0 || errno)
1232 } else if (argv[i][1] == 'u') {
1235 baseurl = argv[++i];
1241 if (!realpath(repodir, repodirabs))
1247 if (unveil(repodir, "r") == -1)
1248 err(1, "unveil: %s", repodir);
1249 if (unveil(".", "rwc") == -1)
1250 err(1, "unveil: .");
1251 if (cachefile && unveil(cachefile, "rwc") == -1)
1252 err(1, "unveil: %s", cachefile);
1255 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1258 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1263 if (git_repository_open_ext(&repo, repodir,
1264 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1265 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1270 if (!git_revparse_single(&obj, repo, "HEAD"))
1271 head = git_object_id(obj);
1272 git_object_free(obj);
1274 /* use directory name as name */
1275 if ((name = strrchr(repodirabs, '/')))
1282 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1283 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1284 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1285 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1287 /* strip .git suffix */
1288 if (!(strippedname = strdup(name)))
1290 if ((p = strrchr(strippedname, '.')))
1291 if (!strcmp(p, ".git"))
1294 /* read description or .git/description */
1295 joinpath(path, sizeof(path), repodir, "description");
1296 if (!(fpread = fopen(path, "r"))) {
1297 joinpath(path, sizeof(path), repodir, ".git/description");
1298 fpread = fopen(path, "r");
1301 if (!fgets(description, sizeof(description), fpread))
1302 description[0] = '\0';
1306 /* read url or .git/url */
1307 joinpath(path, sizeof(path), repodir, "url");
1308 if (!(fpread = fopen(path, "r"))) {
1309 joinpath(path, sizeof(path), repodir, ".git/url");
1310 fpread = fopen(path, "r");
1313 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1315 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1320 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1321 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1322 git_object_type(obj) == GIT_OBJ_BLOB)
1323 license = licensefiles[i] + strlen("HEAD:");
1324 git_object_free(obj);
1328 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1329 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1330 git_object_type(obj) == GIT_OBJ_BLOB)
1331 readme = readmefiles[i] + strlen("HEAD:");
1332 git_object_free(obj);
1335 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1336 git_object_type(obj) == GIT_OBJ_BLOB)
1337 submodules = ".gitmodules";
1338 git_object_free(obj);
1340 /* Generate tarball */
1342 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1343 strippedname, repodir);
1347 fp = efopen("log.html", "w");
1349 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1350 writeheader(fp, "Log");
1351 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1352 "<td><b>Commit</b></td>"
1353 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1354 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1355 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1357 if (cachefile && head) {
1358 /* read from cache file (does not need to exist) */
1359 if ((rcachefp = fopen(cachefile, "r"))) {
1360 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1361 errx(1, "%s: no object id", cachefile);
1362 if (git_oid_fromstr(&lastoid, lastoidstr))
1363 errx(1, "%s: invalid object id", cachefile);
1366 /* write log to (temporary) cache */
1367 if ((fd = mkstemp(tmppath)) == -1)
1369 if (!(wcachefp = fdopen(fd, "w")))
1370 err(1, "fdopen: '%s'", tmppath);
1371 /* write last commit id (HEAD) */
1372 git_oid_tostr(buf, sizeof(buf), head);
1373 fprintf(wcachefp, "%s\n", buf);
1378 /* append previous log to log.html and the new cache */
1379 while (!feof(rcachefp)) {
1380 n = fread(buf, 1, sizeof(buf), rcachefp);
1381 if (ferror(rcachefp))
1383 if (fwrite(buf, 1, n, fp) != n ||
1384 fwrite(buf, 1, n, wcachefp) != n)
1395 fputs("</tbody></table>", fp);
1399 /* files for HEAD */
1400 fp = efopen("files.html", "w");
1401 writeheader(fp, "Files");
1403 writefiles(fp, head);
1407 cp("files.html", "index.html");
1409 /* summary page with branches and tags */
1410 fp = efopen("refs.html", "w");
1411 writeheader(fp, "Refs");
1417 fp = efopen("atom.xml", "w");
1421 /* rename new cache file on success */
1422 if (cachefile && head) {
1423 if (rename(tmppath, cachefile))
1424 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1425 umask((mask = umask(0)));
1426 if (chmod(cachefile,
1427 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1428 err(1, "chmod: '%s'", cachefile);
1432 git_repository_free(repo);
1433 git_libgit2_shutdown();