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)
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);
304 mkdirp(const char *path)
306 char tmp[PATH_MAX], *p;
308 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
309 errx(1, "path truncated: '%s'", path);
310 for (p = tmp + (tmp[0] == '/'); *p; p++) {
314 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
318 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
324 printtimez(FILE *fp, const git_time *intime)
330 t = (time_t)intime->time;
331 if (!(intm = gmtime(&t)))
333 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
338 printtime(FILE *fp, const git_time *intime)
344 t = (time_t)intime->time + (intime->offset * 60);
345 if (!(intm = gmtime(&t)))
347 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
348 if (intime->offset < 0)
349 fprintf(fp, "%s -%02d%02d", out,
350 -(intime->offset) / 60, -(intime->offset) % 60);
352 fprintf(fp, "%s +%02d%02d", out,
353 intime->offset / 60, intime->offset % 60);
357 printtimeshort(FILE *fp, const git_time *intime)
363 t = (time_t)intime->time;
364 if (!(intm = gmtime(&t)))
366 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
371 writeheader(FILE *fp, const char *title)
373 fputs("<!DOCTYPE html>\n"
374 "<html lang=\"en\">\n<head>\n"
375 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
376 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
378 xmlencode(fp, title, strlen(title));
379 if (title[0] && strippedname[0])
381 xmlencode(fp, strippedname, strlen(strippedname));
384 xmlencode(fp, description, strlen(description));
385 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
386 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
388 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
389 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
390 fputs("</head>\n<body>\n<table><tr><td>", fp);
391 fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
393 fputs("</td><td><h1>", fp);
394 xmlencode(fp, strippedname, strlen(strippedname));
395 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
396 xmlencode(fp, description, strlen(description));
397 fputs("</span></td></tr>", fp);
399 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
400 xmlencode(fp, cloneurl, strlen(cloneurl));
402 xmlencode(fp, cloneurl, strlen(cloneurl));
403 fputs("</a></span></td></tr>", fp);
405 fputs("<tr><td></td><td>\n", fp);
406 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
407 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
408 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
410 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
411 relpath, submodules);
413 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
416 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
418 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
419 relpath, strippedname);
420 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
424 writefooter(FILE *fp)
426 fputs("</div>\n</body>\n</html>\n", fp);
430 get_ext(const char *filename)
432 const char *dot = strrchr(filename, '.');
433 if(!dot || dot == filename) return "";
438 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
446 html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
447 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
453 int stdout_copy = dup(1);
458 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
459 strncat(cmd, filename, strlen(filename) + 1);
460 FILE *child = popen(cmd, "w");
462 printf("child is null: %s", strerror(errno));
466 // Give code to highlight through STDIN:
468 for (i = 0; *s && i < len; s++, i++) {
469 fprintf(child, "%c", *s);
476 dup2(stdout_copy, 1);
479 fprintf(fp, "%s", html);
482 fprintf(fp, "<pre>%s</pre>", s);
488 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
490 const char *s = git_blob_rawcontent(blob);
491 git_off_t len = git_blob_rawsize(blob);
494 call_chroma(filename, fp, s, len);
499 printcommit(FILE *fp, struct commitinfo *ci)
501 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
502 relpath, ci->oid, ci->oid);
504 if (ci->parentoid[0])
505 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
506 relpath, ci->parentoid, ci->parentoid);
509 fputs("<b>Author:</b> ", fp);
510 xmlencode(fp, ci->author->name, strlen(ci->author->name));
511 fputs(" <<a href=\"mailto:", fp);
512 xmlencode(fp, ci->author->email, strlen(ci->author->email));
514 xmlencode(fp, ci->author->email, strlen(ci->author->email));
515 fputs("</a>>\n<b>Date:</b> ", fp);
516 printtime(fp, &(ci->author->when));
521 xmlencode(fp, ci->msg, strlen(ci->msg));
527 printshowfile(FILE *fp, struct commitinfo *ci)
529 const git_diff_delta *delta;
530 const git_diff_hunk *hunk;
531 const git_diff_line *line;
533 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
542 if (ci->filecount > 1000 ||
543 ci->ndeltas > 1000 ||
544 ci->addcount > 100000 ||
545 ci->delcount > 100000) {
546 fputs("Diff is too large, output suppressed.\n", fp);
551 fputs("<b>Diffstat:</b>\n<table>", fp);
552 for (i = 0; i < ci->ndeltas; i++) {
553 delta = git_patch_get_delta(ci->deltas[i]->patch);
555 switch (delta->status) {
556 case GIT_DELTA_ADDED: c = 'A'; break;
557 case GIT_DELTA_COPIED: c = 'C'; break;
558 case GIT_DELTA_DELETED: c = 'D'; break;
559 case GIT_DELTA_MODIFIED: c = 'M'; break;
560 case GIT_DELTA_RENAMED: c = 'R'; break;
561 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
562 default: c = ' '; break;
565 fprintf(fp, "<tr><td>%c", c);
567 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
569 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
570 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
571 if (strcmp(delta->old_file.path, delta->new_file.path)) {
572 fputs(" -> ", fp);
573 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
576 add = ci->deltas[i]->addcount;
577 del = ci->deltas[i]->delcount;
579 total = sizeof(linestr) - 2;
580 if (changed > total) {
582 add = ((float)total / changed * add) + 1;
584 del = ((float)total / changed * del) + 1;
586 memset(&linestr, '+', add);
587 memset(&linestr[add], '-', del);
589 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
590 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
591 fwrite(&linestr, 1, add, fp);
592 fputs("</span><span class=\"d\">", fp);
593 fwrite(&linestr[add], 1, del, fp);
594 fputs("</span></td></tr>\n", fp);
596 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
597 ci->filecount, ci->filecount == 1 ? "" : "s",
598 ci->addcount, ci->addcount == 1 ? "" : "s",
599 ci->delcount, ci->delcount == 1 ? "" : "s");
603 for (i = 0; i < ci->ndeltas; i++) {
604 patch = ci->deltas[i]->patch;
605 delta = git_patch_get_delta(patch);
606 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
607 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
608 fputs(".html\">", fp);
609 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
610 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
611 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
612 fprintf(fp, ".html\">");
613 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
614 fprintf(fp, "</a></b>\n");
616 /* check binary data */
617 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
618 fputs("Binary files differ.\n", fp);
622 nhunks = git_patch_num_hunks(patch);
623 for (j = 0; j < nhunks; j++) {
624 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
627 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
628 xmlencode(fp, hunk->header, hunk->header_len);
632 if (git_patch_get_line_in_hunk(&line, patch, j, k))
634 if (line->old_lineno == -1)
635 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
637 else if (line->new_lineno == -1)
638 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
642 xmlencodeline(fp, line->content, line->content_len);
644 if (line->old_lineno == -1 || line->new_lineno == -1)
652 writelogline(FILE *fp, struct commitinfo *ci)
654 fputs("<tr><td>", fp);
656 printtimeshort(fp, &(ci->author->when));
657 fputs("</td><td>", fp);
659 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
660 xmlencode(fp, ci->summary, strlen(ci->summary));
663 fputs("</td><td>", fp);
665 xmlencode(fp, ci->author->name, strlen(ci->author->name));
666 fputs("</td><td class=\"num\" align=\"right\">", fp);
667 fprintf(fp, "%zu", ci->filecount);
668 fputs("</td><td class=\"num\" align=\"right\">", fp);
669 fprintf(fp, "+%zu", ci->addcount);
670 fputs("</td><td class=\"num\" align=\"right\">", fp);
671 fprintf(fp, "-%zu", ci->delcount);
672 fputs("</td></tr>\n", fp);
676 writelog(FILE *fp, const git_oid *oid)
678 struct commitinfo *ci;
679 git_revwalk *w = NULL;
681 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
685 git_revwalk_new(&w, repo);
686 git_revwalk_push(w, oid);
687 git_revwalk_simplify_first_parent(w);
689 while (!git_revwalk_next(&id, w)) {
692 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
695 git_oid_tostr(oidstr, sizeof(oidstr), &id);
696 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
697 if (r < 0 || (size_t)r >= sizeof(path))
698 errx(1, "path truncated: 'commit/%s.html'", oidstr);
699 r = access(path, F_OK);
701 /* optimization: if there are no log lines to write and
702 the commit file already exists: skip the diffstat */
703 if (!nlogcommits && !r)
706 if (!(ci = commitinfo_getbyoid(&id)))
708 /* diffstat: for stagit HTML required for the log.html line */
709 if (commitinfo_getstats(ci) == -1)
712 if (nlogcommits < 0) {
713 writelogline(fp, ci);
714 } else if (nlogcommits > 0) {
715 writelogline(fp, ci);
717 if (!nlogcommits && ci->parentoid[0])
718 fputs("<tr><td></td><td colspan=\"5\">"
719 "More commits remaining [...]</td>"
724 writelogline(wcachefp, ci);
726 /* check if file exists if so skip it */
729 fpfile = efopen(path, "w");
730 writeheader(fpfile, ci->summary);
731 fputs("<pre>", fpfile);
732 printshowfile(fpfile, ci);
733 fputs("</pre>\n", fpfile);
748 printcommitatom(FILE *fp, struct commitinfo *ci)
750 fputs("<entry>\n", fp);
752 fprintf(fp, "<id>%s</id>\n", ci->oid);
754 fputs("<published>", fp);
755 printtimez(fp, &(ci->author->when));
756 fputs("</published>\n", fp);
759 fputs("<updated>", fp);
760 printtimez(fp, &(ci->committer->when));
761 fputs("</updated>\n", fp);
764 fputs("<title type=\"text\">", fp);
765 xmlencode(fp, ci->summary, strlen(ci->summary));
766 fputs("</title>\n", fp);
768 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n",
772 fputs("<author>\n<name>", fp);
773 xmlencode(fp, ci->author->name, strlen(ci->author->name));
774 fputs("</name>\n<email>", fp);
775 xmlencode(fp, ci->author->email, strlen(ci->author->email));
776 fputs("</email>\n</author>\n", fp);
779 fputs("<content type=\"text\">", fp);
780 fprintf(fp, "commit %s\n", ci->oid);
781 if (ci->parentoid[0])
782 fprintf(fp, "parent %s\n", ci->parentoid);
784 fputs("Author: ", fp);
785 xmlencode(fp, ci->author->name, strlen(ci->author->name));
787 xmlencode(fp, ci->author->email, strlen(ci->author->email));
788 fputs(">\nDate: ", fp);
789 printtime(fp, &(ci->author->when));
794 xmlencode(fp, ci->msg, strlen(ci->msg));
796 fputs("\n</content>\n</entry>\n", fp);
802 struct commitinfo *ci;
803 git_revwalk *w = NULL;
805 size_t i, m = 100; /* last 'm' commits */
807 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
808 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
809 xmlencode(fp, strippedname, strlen(strippedname));
810 fputs(", branch HEAD</title>\n<subtitle>", fp);
811 xmlencode(fp, description, strlen(description));
812 fputs("</subtitle>\n", fp);
814 git_revwalk_new(&w, repo);
815 git_revwalk_push_head(w);
816 git_revwalk_simplify_first_parent(w);
818 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
819 if (!(ci = commitinfo_getbyoid(&id)))
821 printcommitatom(fp, ci);
826 fputs("</feed>\n", fp);
834 int value = var * 10 + .5;
839 convertbytes(int bytes)
841 bytes = (float)bytes;
842 static char outp[25];
843 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
844 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
845 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
850 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
852 char tmp[PATH_MAX] = "", *d;
856 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
857 errx(1, "path truncated: '%s'", fpath);
858 if (!(d = dirname(tmp)))
862 for (p = fpath, tmp[0] = '\0'; *p; p++) {
863 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
864 errx(1, "path truncated: '../%s'", tmp);
868 fp = efopen(fpath, "w");
869 writeheader(fp, filename);
871 xmlencode(fp, filename, strlen(filename));
872 fprintf(fp, " (%s)", convertbytes((int)filesize));
875 char newfpath[PATH_MAX];
876 char newfilename[PATH_MAX];
877 if (strcmp(get_ext(filename), "md") == 0) {
878 fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename);
879 strcpy(newfpath, fpath);
880 strcat(newfpath, "-raw");
882 strcpy(newfilename, filename);
883 strcat(newfilename, "-raw");
884 strcpy(oldfilename, filename);
887 writeblob(obj, newfpath, newfilename, filesize);
888 } else if (strcmp(get_ext(filename), "md-raw" ) == 0) {
889 fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename);
893 fputs(".</p><hr/>", fp);
895 if (git_blob_is_binary((git_blob *)obj)) {
896 fputs("<p>Binary file.</p>\n", fp);
898 writeblobhtml(filename, fp, (git_blob *)obj);
910 filemode(git_filemode_t m)
912 static char mode[11];
914 memset(mode, '-', sizeof(mode) - 1);
925 else if (S_ISFIFO(m))
929 else if (S_ISSOCK(m))
934 if (m & S_IRUSR) mode[1] = 'r';
935 if (m & S_IWUSR) mode[2] = 'w';
936 if (m & S_IXUSR) mode[3] = 'x';
937 if (m & S_IRGRP) mode[4] = 'r';
938 if (m & S_IWGRP) mode[5] = 'w';
939 if (m & S_IXGRP) mode[6] = 'x';
940 if (m & S_IROTH) mode[7] = 'r';
941 if (m & S_IWOTH) mode[8] = 'w';
942 if (m & S_IXOTH) mode[9] = 'x';
944 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
945 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
946 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
952 writefilestree(FILE *fp, git_tree *tree, const char *path)
954 const git_tree_entry *entry = NULL;
955 git_submodule *module = NULL;
956 git_object *obj = NULL;
958 const char *entryname;
959 char filepath[PATH_MAX], entrypath[PATH_MAX];
963 count = git_tree_entrycount(tree);
964 for (i = 0; i < count; i++) {
965 if (!(entry = git_tree_entry_byindex(tree, i)) ||
966 !(entryname = git_tree_entry_name(entry)))
968 joinpath(entrypath, sizeof(entrypath), path, entryname);
970 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
972 if (r < 0 || (size_t)r >= sizeof(filepath))
973 errx(1, "path truncated: 'file/%s.html'", entrypath);
975 if (!git_tree_entry_to_object(&obj, repo, entry)) {
976 switch (git_object_type(obj)) {
981 ret = writefilestree(fp, (git_tree *)obj,
983 git_object_free(obj);
988 git_object_free(obj);
992 filesize = git_blob_rawsize((git_blob *)obj);
993 writeblob(obj, filepath, entryname, filesize);
995 fputs("<tr><td>", fp);
996 fputs(filemode(git_tree_entry_filemode(entry)), fp);
997 fprintf(fp, "</td><td><a href=\"%s", relpath);
998 xmlencode(fp, filepath, strlen(filepath));
1000 xmlencode(fp, entrypath, strlen(entrypath));
1001 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
1002 fprintf(fp, "%s", convertbytes((int)filesize));
1003 fputs("</td></tr>\n", fp);
1004 git_object_free(obj);
1005 } else if (!git_submodule_lookup(&module, repo, entryname)) {
1006 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
1008 xmlencode(fp, entrypath, strlen(entrypath));
1009 git_submodule_free(module);
1010 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
1018 writefiles(FILE *fp, const git_oid *id)
1020 git_tree *tree = NULL;
1021 git_commit *commit = NULL;
1024 fputs("<table id=\"files\"><thead>\n<tr>"
1025 "<td><b>Mode</b></td><td><b>Name</b></td>"
1026 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
1027 "</tr>\n</thead><tbody>\n", fp);
1029 if (!git_commit_lookup(&commit, repo, id) &&
1030 !git_commit_tree(&tree, commit))
1031 ret = writefilestree(fp, tree, "");
1033 fputs("</tbody></table>", fp);
1035 git_commit_free(commit);
1036 git_tree_free(tree);
1042 refs_cmp(const void *v1, const void *v2)
1044 git_reference *r1 = (*(git_reference **)v1);
1045 git_reference *r2 = (*(git_reference **)v2);
1048 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1051 return strcmp(git_reference_shorthand(r1),
1052 git_reference_shorthand(r2));
1058 struct commitinfo *ci;
1059 const git_oid *id = NULL;
1060 git_object *obj = NULL;
1061 git_reference *dref = NULL, *r, *ref = NULL;
1062 git_reference_iterator *it = NULL;
1063 git_reference **refs = NULL;
1064 size_t count, i, j, refcount;
1065 const char *titles[] = { "Branches", "Tags" };
1066 const char *ids[] = { "branches", "tags" };
1069 if (git_reference_iterator_new(&it, repo))
1072 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1073 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1075 refs[refcount] = ref;
1077 git_reference_iterator_free(it);
1079 /* sort by type then shorthand name */
1080 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1082 for (j = 0; j < 2; j++) {
1083 for (i = 0, count = 0; i < refcount; i++) {
1084 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1085 !(git_reference_is_tag(refs[i]) && j == 1))
1088 switch (git_reference_type(refs[i])) {
1089 case GIT_REF_SYMBOLIC:
1090 if (git_reference_resolve(&dref, refs[i]))
1100 if (!git_reference_target(r) ||
1101 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1103 if (!(id = git_object_id(obj)))
1105 if (!(ci = commitinfo_getbyoid(id)))
1108 /* print header if it has an entry (first). */
1110 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1111 "<thead>\n<tr><td><b>Name</b></td>"
1112 "<td><b>Last commit date</b></td>"
1113 "<td><b>Author</b></td>\n</tr>\n"
1114 "</thead><tbody>\n",
1119 name = git_reference_shorthand(r);
1121 fputs("<tr><td>", fp);
1122 xmlencode(fp, name, strlen(name));
1123 fputs("</td><td>", fp);
1125 printtimeshort(fp, &(ci->author->when));
1126 fputs("</td><td>", fp);
1128 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1129 fputs("</td></tr>\n", fp);
1133 commitinfo_free(ci);
1134 git_object_free(obj);
1136 git_reference_free(dref);
1141 fputs("</tbody></table><br/>", fp);
1145 git_object_free(obj);
1146 git_reference_free(dref);
1148 for (i = 0; i < refcount; i++)
1149 git_reference_free(refs[i]);
1158 fprintf(stderr, "%s [-c cachefile | -l commits] "
1159 "[-u baseurl] repodir\n", argv0);
1164 main(int argc, char *argv[])
1166 git_object *obj = NULL;
1167 const git_oid *head = NULL;
1170 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1171 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1175 for (i = 1; i < argc; i++) {
1176 if (argv[i][0] != '-') {
1180 } else if (argv[i][1] == 'c') {
1181 if (nlogcommits > 0 || i + 1 >= argc)
1183 cachefile = argv[++i];
1184 } else if (argv[i][1] == 'l') {
1185 if (cachefile || i + 1 >= argc)
1188 nlogcommits = strtoll(argv[++i], &p, 10);
1189 if (argv[i][0] == '\0' || *p != '\0' ||
1190 nlogcommits <= 0 || errno)
1192 } else if (argv[i][1] == 'u') {
1195 baseurl = argv[++i];
1201 if (!realpath(repodir, repodirabs))
1207 if (unveil(repodir, "r") == -1)
1208 err(1, "unveil: %s", repodir);
1209 if (unveil(".", "rwc") == -1)
1210 err(1, "unveil: .");
1211 if (cachefile && unveil(cachefile, "rwc") == -1)
1212 err(1, "unveil: %s", cachefile);
1215 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1218 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1223 if (git_repository_open_ext(&repo, repodir,
1224 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1225 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1230 if (!git_revparse_single(&obj, repo, "HEAD"))
1231 head = git_object_id(obj);
1232 git_object_free(obj);
1234 /* use directory name as name */
1235 if ((name = strrchr(repodirabs, '/')))
1242 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1243 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1244 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1245 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1247 /* strip .git suffix */
1248 if (!(strippedname = strdup(name)))
1250 if ((p = strrchr(strippedname, '.')))
1251 if (!strcmp(p, ".git"))
1254 /* read description or .git/description */
1255 joinpath(path, sizeof(path), repodir, "description");
1256 if (!(fpread = fopen(path, "r"))) {
1257 joinpath(path, sizeof(path), repodir, ".git/description");
1258 fpread = fopen(path, "r");
1261 if (!fgets(description, sizeof(description), fpread))
1262 description[0] = '\0';
1266 /* read url or .git/url */
1267 joinpath(path, sizeof(path), repodir, "url");
1268 if (!(fpread = fopen(path, "r"))) {
1269 joinpath(path, sizeof(path), repodir, ".git/url");
1270 fpread = fopen(path, "r");
1273 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1275 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1280 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1281 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1282 git_object_type(obj) == GIT_OBJ_BLOB)
1283 license = licensefiles[i] + strlen("HEAD:");
1284 git_object_free(obj);
1288 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1289 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1290 git_object_type(obj) == GIT_OBJ_BLOB)
1291 readme = readmefiles[i] + strlen("HEAD:");
1292 git_object_free(obj);
1295 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1296 git_object_type(obj) == GIT_OBJ_BLOB)
1297 submodules = ".gitmodules";
1298 git_object_free(obj);
1300 /* Generate tarball */
1302 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1303 strippedname, repodir);
1307 fp = efopen("log.html", "w");
1309 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1310 writeheader(fp, "Log");
1311 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1312 "<td><b>Commit</b></td>"
1313 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1314 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1315 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1317 if (cachefile && head) {
1318 /* read from cache file (does not need to exist) */
1319 if ((rcachefp = fopen(cachefile, "r"))) {
1320 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1321 errx(1, "%s: no object id", cachefile);
1322 if (git_oid_fromstr(&lastoid, lastoidstr))
1323 errx(1, "%s: invalid object id", cachefile);
1326 /* write log to (temporary) cache */
1327 if ((fd = mkstemp(tmppath)) == -1)
1329 if (!(wcachefp = fdopen(fd, "w")))
1330 err(1, "fdopen: '%s'", tmppath);
1331 /* write last commit id (HEAD) */
1332 git_oid_tostr(buf, sizeof(buf), head);
1333 fprintf(wcachefp, "%s\n", buf);
1338 /* append previous log to log.html and the new cache */
1339 while (!feof(rcachefp)) {
1340 n = fread(buf, 1, sizeof(buf), rcachefp);
1341 if (ferror(rcachefp))
1343 if (fwrite(buf, 1, n, fp) != n ||
1344 fwrite(buf, 1, n, wcachefp) != n)
1355 fputs("</tbody></table>", fp);
1359 /* files for HEAD */
1360 fp = efopen("files.html", "w");
1361 writeheader(fp, "Files");
1363 writefiles(fp, head);
1367 cp("files.html", "index.html");
1369 /* summary page with branches and tags */
1370 fp = efopen("refs.html", "w");
1371 writeheader(fp, "Refs");
1377 fp = efopen("atom.xml", "w");
1381 /* rename new cache file on success */
1382 if (cachefile && head) {
1383 if (rename(tmppath, cachefile))
1384 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1385 umask((mask = umask(0)));
1386 if (chmod(cachefile,
1387 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1388 err(1, "chmod: '%s'", cachefile);
1392 git_repository_free(repo);
1393 git_libgit2_shutdown();