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 */
76 static git_oid lastoid;
77 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
78 static FILE *rcachefp, *wcachefp;
79 static const char *cachefile;
82 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
86 r = snprintf(buf, bufsiz, "%s%s%s",
87 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
88 if (r < 0 || (size_t)r >= bufsiz)
89 errx(1, "path truncated: '%s%s%s'",
90 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
94 deltainfo_free(struct deltainfo *di)
98 git_patch_free(di->patch);
99 memset(di, 0, sizeof(*di));
104 commitinfo_getstats(struct commitinfo *ci)
106 struct deltainfo *di;
107 git_diff_options opts;
108 git_diff_find_options fopts;
109 const git_diff_delta *delta;
110 const git_diff_hunk *hunk;
111 const git_diff_line *line;
112 git_patch *patch = NULL;
113 size_t ndeltas, nhunks, nhunklines;
116 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
118 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
119 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
121 ci->parent_tree = NULL;
125 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
126 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
127 GIT_DIFF_IGNORE_SUBMODULES |
128 GIT_DIFF_INCLUDE_TYPECHANGE;
129 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
132 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
134 /* find renames and copies, exact matches (no heuristic) for renames. */
135 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
136 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
137 if (git_diff_find_similar(ci->diff, &fopts))
140 ndeltas = git_diff_num_deltas(ci->diff);
141 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
144 for (i = 0; i < ndeltas; i++) {
145 if (git_patch_from_diff(&patch, ci->diff, i))
148 if (!(di = calloc(1, sizeof(struct deltainfo))))
153 delta = git_patch_get_delta(patch);
155 /* skip stats for binary data */
156 if (delta->flags & GIT_DIFF_FLAG_BINARY)
159 nhunks = git_patch_num_hunks(patch);
160 for (j = 0; j < nhunks; j++) {
161 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
164 if (git_patch_get_line_in_hunk(&line, patch, j, k))
166 if (line->old_lineno == -1) {
169 } else if (line->new_lineno == -1) {
182 git_diff_free(ci->diff);
184 git_tree_free(ci->commit_tree);
185 ci->commit_tree = NULL;
186 git_tree_free(ci->parent_tree);
187 ci->parent_tree = NULL;
188 git_commit_free(ci->parent);
192 for (i = 0; i < ci->ndeltas; i++)
193 deltainfo_free(ci->deltas[i]);
205 commitinfo_free(struct commitinfo *ci)
212 for (i = 0; i < ci->ndeltas; i++)
213 deltainfo_free(ci->deltas[i]);
216 git_diff_free(ci->diff);
217 git_tree_free(ci->commit_tree);
218 git_tree_free(ci->parent_tree);
219 git_commit_free(ci->commit);
220 git_commit_free(ci->parent);
221 memset(ci, 0, sizeof(*ci));
226 commitinfo_getbyoid(const git_oid *id)
228 struct commitinfo *ci;
230 if (!(ci = calloc(1, sizeof(struct commitinfo))))
233 if (git_commit_lookup(&(ci->commit), repo, id))
237 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
238 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
240 ci->author = git_commit_author(ci->commit);
241 ci->committer = git_commit_committer(ci->commit);
242 ci->summary = git_commit_summary(ci->commit);
243 ci->msg = git_commit_message(ci->commit);
254 efopen(const char *name, const char *flags)
258 if (!(fp = fopen(name, flags)))
259 err(1, "fopen: '%s'", name);
264 /* Escape characters below as HTML 2.0 / XML 1.0. */
266 xmlencode(FILE *fp, const char *s, size_t len)
270 for (i = 0; *s && i < len; s++, i++) {
272 case '<': fputs("<", fp); break;
273 case '>': fputs(">", fp); break;
274 case '\'': fputs("'", fp); break;
275 case '&': fputs("&", fp); break;
276 case '"': fputs(""", fp); break;
277 default: fputc(*s, fp);
283 mkdirp(const char *path)
285 char tmp[PATH_MAX], *p;
287 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
288 errx(1, "path truncated: '%s'", path);
289 for (p = tmp + (tmp[0] == '/'); *p; p++) {
293 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
297 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
303 printtimez(FILE *fp, const git_time *intime)
309 t = (time_t)intime->time;
310 if (!(intm = gmtime(&t)))
312 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
317 printtime(FILE *fp, const git_time *intime)
323 t = (time_t)intime->time + (intime->offset * 60);
324 if (!(intm = gmtime(&t)))
326 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
327 if (intime->offset < 0)
328 fprintf(fp, "%s -%02d%02d", out,
329 -(intime->offset) / 60, -(intime->offset) % 60);
331 fprintf(fp, "%s +%02d%02d", out,
332 intime->offset / 60, intime->offset % 60);
336 printtimeshort(FILE *fp, const git_time *intime)
342 t = (time_t)intime->time;
343 if (!(intm = gmtime(&t)))
345 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
350 writeheader(FILE *fp, const char *title)
352 fputs("<!DOCTYPE html>\n"
353 "<html lang=\"en\">\n<head>\n"
354 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
355 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
357 xmlencode(fp, title, strlen(title));
358 if (title[0] && strippedname[0])
360 xmlencode(fp, strippedname, strlen(strippedname));
363 xmlencode(fp, description, strlen(description));
364 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
365 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
367 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
368 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
369 fputs("</head>\n<body>\n<table><tr><td>", fp);
370 fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
372 fputs("</td><td><h1>", fp);
373 xmlencode(fp, strippedname, strlen(strippedname));
374 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
375 xmlencode(fp, description, strlen(description));
376 fputs("</span></td></tr>", fp);
378 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
379 xmlencode(fp, cloneurl, strlen(cloneurl));
381 xmlencode(fp, cloneurl, strlen(cloneurl));
382 fputs("</a></span></td></tr>", fp);
384 fputs("<tr><td></td><td>\n", fp);
385 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
386 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
387 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
389 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
390 relpath, submodules);
392 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
395 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
397 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
398 relpath, strippedname);
399 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
403 writefooter(FILE *fp)
405 fputs("</div>\n</body>\n</html>\n", fp);
409 get_ext(const char *filename)
411 const char *dot = strrchr(filename, '.');
412 if(!dot || dot == filename) return "";
417 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
425 html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
426 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
433 int stdout_copy = dup(1);
438 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
439 strncat(cmd, filename, strlen(filename) + 1);
440 FILE *child = popen(cmd, "w");
442 printf("child is null: %s", strerror(errno));
446 // Give code to highlight through STDIN:
448 for (i = 0; *s && i < len; s++, i++) {
449 fprintf(child, "%c", *s);
456 dup2(stdout_copy, 1);
459 fprintf(fp, "%s", html);
462 fprintf(fp, "<pre>%s</pre>", s);
467 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
469 const char *s = git_blob_rawcontent(blob);
470 git_off_t len = git_blob_rawsize(blob);
473 call_chroma(filename, fp, s, len);
478 printcommit(FILE *fp, struct commitinfo *ci)
480 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
481 relpath, ci->oid, ci->oid);
483 if (ci->parentoid[0])
484 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
485 relpath, ci->parentoid, ci->parentoid);
488 fputs("<b>Author:</b> ", fp);
489 xmlencode(fp, ci->author->name, strlen(ci->author->name));
490 fputs(" <<a href=\"mailto:", fp);
491 xmlencode(fp, ci->author->email, strlen(ci->author->email));
493 xmlencode(fp, ci->author->email, strlen(ci->author->email));
494 fputs("</a>>\n<b>Date:</b> ", fp);
495 printtime(fp, &(ci->author->when));
500 xmlencode(fp, ci->msg, strlen(ci->msg));
506 printshowfile(FILE *fp, struct commitinfo *ci)
508 const git_diff_delta *delta;
509 const git_diff_hunk *hunk;
510 const git_diff_line *line;
512 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
521 if (ci->filecount > 1000 ||
522 ci->ndeltas > 1000 ||
523 ci->addcount > 100000 ||
524 ci->delcount > 100000) {
525 fputs("Diff is too large, output suppressed.\n", fp);
530 fputs("<b>Diffstat:</b>\n<table>", fp);
531 for (i = 0; i < ci->ndeltas; i++) {
532 delta = git_patch_get_delta(ci->deltas[i]->patch);
534 switch (delta->status) {
535 case GIT_DELTA_ADDED: c = 'A'; break;
536 case GIT_DELTA_COPIED: c = 'C'; break;
537 case GIT_DELTA_DELETED: c = 'D'; break;
538 case GIT_DELTA_MODIFIED: c = 'M'; break;
539 case GIT_DELTA_RENAMED: c = 'R'; break;
540 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
541 default: c = ' '; break;
544 fprintf(fp, "<tr><td>%c", c);
546 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
548 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
549 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
550 if (strcmp(delta->old_file.path, delta->new_file.path)) {
551 fputs(" -> ", fp);
552 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
555 add = ci->deltas[i]->addcount;
556 del = ci->deltas[i]->delcount;
558 total = sizeof(linestr) - 2;
559 if (changed > total) {
561 add = ((float)total / changed * add) + 1;
563 del = ((float)total / changed * del) + 1;
565 memset(&linestr, '+', add);
566 memset(&linestr[add], '-', del);
568 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
569 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
570 fwrite(&linestr, 1, add, fp);
571 fputs("</span><span class=\"d\">", fp);
572 fwrite(&linestr[add], 1, del, fp);
573 fputs("</span></td></tr>\n", fp);
575 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
576 ci->filecount, ci->filecount == 1 ? "" : "s",
577 ci->addcount, ci->addcount == 1 ? "" : "s",
578 ci->delcount, ci->delcount == 1 ? "" : "s");
582 for (i = 0; i < ci->ndeltas; i++) {
583 patch = ci->deltas[i]->patch;
584 delta = git_patch_get_delta(patch);
585 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
586 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
587 fputs(".html\">", fp);
588 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
589 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
590 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
591 fprintf(fp, ".html\">");
592 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
593 fprintf(fp, "</a></b>\n");
595 /* check binary data */
596 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
597 fputs("Binary files differ.\n", fp);
601 nhunks = git_patch_num_hunks(patch);
602 for (j = 0; j < nhunks; j++) {
603 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
606 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
607 xmlencode(fp, hunk->header, hunk->header_len);
611 if (git_patch_get_line_in_hunk(&line, patch, j, k))
613 if (line->old_lineno == -1)
614 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
616 else if (line->new_lineno == -1)
617 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
621 xmlencode(fp, line->content, line->content_len);
622 if (line->old_lineno == -1 || line->new_lineno == -1)
630 writelogline(FILE *fp, struct commitinfo *ci)
632 fputs("<tr><td>", fp);
634 printtimeshort(fp, &(ci->author->when));
635 fputs("</td><td>", fp);
637 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
638 xmlencode(fp, ci->summary, strlen(ci->summary));
641 fputs("</td><td>", fp);
643 xmlencode(fp, ci->author->name, strlen(ci->author->name));
644 fputs("</td><td class=\"num\" align=\"right\">", fp);
645 fprintf(fp, "%zu", ci->filecount);
646 fputs("</td><td class=\"num\" align=\"right\">", fp);
647 fprintf(fp, "+%zu", ci->addcount);
648 fputs("</td><td class=\"num\" align=\"right\">", fp);
649 fprintf(fp, "-%zu", ci->delcount);
650 fputs("</td></tr>\n", fp);
654 writelog(FILE *fp, const git_oid *oid)
656 struct commitinfo *ci;
657 git_revwalk *w = NULL;
659 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
663 git_revwalk_new(&w, repo);
664 git_revwalk_push(w, oid);
665 git_revwalk_simplify_first_parent(w);
667 while (!git_revwalk_next(&id, w)) {
670 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
673 git_oid_tostr(oidstr, sizeof(oidstr), &id);
674 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
675 if (r < 0 || (size_t)r >= sizeof(path))
676 errx(1, "path truncated: 'commit/%s.html'", oidstr);
677 r = access(path, F_OK);
679 /* optimization: if there are no log lines to write and
680 the commit file already exists: skip the diffstat */
681 if (!nlogcommits && !r)
684 if (!(ci = commitinfo_getbyoid(&id)))
686 /* diffstat: for stagit HTML required for the log.html line */
687 if (commitinfo_getstats(ci) == -1)
690 if (nlogcommits < 0) {
691 writelogline(fp, ci);
692 } else if (nlogcommits > 0) {
693 writelogline(fp, ci);
695 if (!nlogcommits && ci->parentoid[0])
696 fputs("<tr><td></td><td colspan=\"5\">"
697 "More commits remaining [...]</td>"
702 writelogline(wcachefp, ci);
704 /* check if file exists if so skip it */
707 fpfile = efopen(path, "w");
708 writeheader(fpfile, ci->summary);
709 fputs("<pre>", fpfile);
710 printshowfile(fpfile, ci);
711 fputs("</pre>\n", fpfile);
726 printcommitatom(FILE *fp, struct commitinfo *ci)
728 fputs("<entry>\n", fp);
730 fprintf(fp, "<id>%s</id>\n", ci->oid);
732 fputs("<published>", fp);
733 printtimez(fp, &(ci->author->when));
734 fputs("</published>\n", fp);
737 fputs("<updated>", fp);
738 printtimez(fp, &(ci->committer->when));
739 fputs("</updated>\n", fp);
742 fputs("<title type=\"text\">", fp);
743 xmlencode(fp, ci->summary, strlen(ci->summary));
744 fputs("</title>\n", fp);
746 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
750 fputs("<author>\n<name>", fp);
751 xmlencode(fp, ci->author->name, strlen(ci->author->name));
752 fputs("</name>\n<email>", fp);
753 xmlencode(fp, ci->author->email, strlen(ci->author->email));
754 fputs("</email>\n</author>\n", fp);
757 fputs("<content type=\"text\">", fp);
758 fprintf(fp, "commit %s\n", ci->oid);
759 if (ci->parentoid[0])
760 fprintf(fp, "parent %s\n", ci->parentoid);
762 fputs("Author: ", fp);
763 xmlencode(fp, ci->author->name, strlen(ci->author->name));
765 xmlencode(fp, ci->author->email, strlen(ci->author->email));
766 fputs(">\nDate: ", fp);
767 printtime(fp, &(ci->author->when));
772 xmlencode(fp, ci->msg, strlen(ci->msg));
774 fputs("\n</content>\n</entry>\n", fp);
780 struct commitinfo *ci;
781 git_revwalk *w = NULL;
783 size_t i, m = 100; /* last 'm' commits */
785 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
786 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
787 xmlencode(fp, strippedname, strlen(strippedname));
788 fputs(", branch HEAD</title>\n<subtitle>", fp);
789 xmlencode(fp, description, strlen(description));
790 fputs("</subtitle>\n", fp);
792 git_revwalk_new(&w, repo);
793 git_revwalk_push_head(w);
794 git_revwalk_simplify_first_parent(w);
796 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
797 if (!(ci = commitinfo_getbyoid(&id)))
799 printcommitatom(fp, ci);
804 fputs("</feed>\n", fp);
812 int value = var * 10 + .5;
817 convertbytes(int bytes)
819 bytes = (float)bytes;
820 static char outp[25];
821 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
822 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
823 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
828 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
830 char tmp[PATH_MAX] = "", *d;
834 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
835 errx(1, "path truncated: '%s'", fpath);
836 if (!(d = dirname(tmp)))
840 for (p = fpath, tmp[0] = '\0'; *p; p++) {
841 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
842 errx(1, "path truncated: '../%s'", tmp);
846 fp = efopen(fpath, "w");
847 writeheader(fp, filename);
849 xmlencode(fp, filename, strlen(filename));
850 fprintf(fp, " (%s)", convertbytes((int)filesize));
851 fputs("</p><hr/>", fp);
853 if (git_blob_is_binary((git_blob *)obj)) {
854 fputs("<p>Binary file.</p>\n", fp);
856 writeblobhtml(filename, fp, (git_blob *)obj);
861 char newfpath[PATH_MAX];
862 strcat(newfpath, fpath);
863 char newfilename[PATH_MAX];
864 strcat(newfilename, filename);
865 writeblob(obj, strcat(newfpath, "-raw"),
866 strcat(newfilename, "-raw"), filesize);
867 // TODO: Add view-raw button
878 filemode(git_filemode_t m)
880 static char mode[11];
882 memset(mode, '-', sizeof(mode) - 1);
893 else if (S_ISFIFO(m))
897 else if (S_ISSOCK(m))
902 if (m & S_IRUSR) mode[1] = 'r';
903 if (m & S_IWUSR) mode[2] = 'w';
904 if (m & S_IXUSR) mode[3] = 'x';
905 if (m & S_IRGRP) mode[4] = 'r';
906 if (m & S_IWGRP) mode[5] = 'w';
907 if (m & S_IXGRP) mode[6] = 'x';
908 if (m & S_IROTH) mode[7] = 'r';
909 if (m & S_IWOTH) mode[8] = 'w';
910 if (m & S_IXOTH) mode[9] = 'x';
912 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
913 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
914 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
920 writefilestree(FILE *fp, git_tree *tree, const char *path)
922 const git_tree_entry *entry = NULL;
923 git_submodule *module = NULL;
924 git_object *obj = NULL;
926 const char *entryname;
927 char filepath[PATH_MAX], entrypath[PATH_MAX];
931 count = git_tree_entrycount(tree);
932 for (i = 0; i < count; i++) {
933 if (!(entry = git_tree_entry_byindex(tree, i)) ||
934 !(entryname = git_tree_entry_name(entry)))
936 joinpath(entrypath, sizeof(entrypath), path, entryname);
938 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
940 if (r < 0 || (size_t)r >= sizeof(filepath))
941 errx(1, "path truncated: 'file/%s.html'", entrypath);
943 if (!git_tree_entry_to_object(&obj, repo, entry)) {
944 switch (git_object_type(obj)) {
949 ret = writefilestree(fp, (git_tree *)obj,
951 git_object_free(obj);
956 git_object_free(obj);
960 filesize = git_blob_rawsize((git_blob *)obj);
961 writeblob(obj, filepath, entryname, filesize);
963 fputs("<tr><td>", fp);
964 fputs(filemode(git_tree_entry_filemode(entry)), fp);
965 fprintf(fp, "</td><td><a href=\"%s", relpath);
966 xmlencode(fp, filepath, strlen(filepath));
968 xmlencode(fp, entrypath, strlen(entrypath));
969 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
970 fprintf(fp, "%s", convertbytes((int)filesize));
971 fputs("</td></tr>\n", fp);
972 git_object_free(obj);
973 } else if (!git_submodule_lookup(&module, repo, entryname)) {
974 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
976 xmlencode(fp, entrypath, strlen(entrypath));
977 git_submodule_free(module);
978 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
986 writefiles(FILE *fp, const git_oid *id)
988 git_tree *tree = NULL;
989 git_commit *commit = NULL;
992 fputs("<table id=\"files\"><thead>\n<tr>"
993 "<td><b>Mode</b></td><td><b>Name</b></td>"
994 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
995 "</tr>\n</thead><tbody>\n", fp);
997 if (!git_commit_lookup(&commit, repo, id) &&
998 !git_commit_tree(&tree, commit))
999 ret = writefilestree(fp, tree, "");
1001 fputs("</tbody></table>", fp);
1003 git_commit_free(commit);
1004 git_tree_free(tree);
1010 refs_cmp(const void *v1, const void *v2)
1012 git_reference *r1 = (*(git_reference **)v1);
1013 git_reference *r2 = (*(git_reference **)v2);
1016 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1019 return strcmp(git_reference_shorthand(r1),
1020 git_reference_shorthand(r2));
1026 struct commitinfo *ci;
1027 const git_oid *id = NULL;
1028 git_object *obj = NULL;
1029 git_reference *dref = NULL, *r, *ref = NULL;
1030 git_reference_iterator *it = NULL;
1031 git_reference **refs = NULL;
1032 size_t count, i, j, refcount;
1033 const char *titles[] = { "Branches", "Tags" };
1034 const char *ids[] = { "branches", "tags" };
1037 if (git_reference_iterator_new(&it, repo))
1040 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1041 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1043 refs[refcount] = ref;
1045 git_reference_iterator_free(it);
1047 /* sort by type then shorthand name */
1048 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1050 for (j = 0; j < 2; j++) {
1051 for (i = 0, count = 0; i < refcount; i++) {
1052 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1053 !(git_reference_is_tag(refs[i]) && j == 1))
1056 switch (git_reference_type(refs[i])) {
1057 case GIT_REF_SYMBOLIC:
1058 if (git_reference_resolve(&dref, refs[i]))
1068 if (!git_reference_target(r) ||
1069 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1071 if (!(id = git_object_id(obj)))
1073 if (!(ci = commitinfo_getbyoid(id)))
1076 /* print header if it has an entry (first). */
1078 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1079 "<thead>\n<tr><td><b>Name</b></td>"
1080 "<td><b>Last commit date</b></td>"
1081 "<td><b>Author</b></td>\n</tr>\n"
1082 "</thead><tbody>\n",
1087 name = git_reference_shorthand(r);
1089 fputs("<tr><td>", fp);
1090 xmlencode(fp, name, strlen(name));
1091 fputs("</td><td>", fp);
1093 printtimeshort(fp, &(ci->author->when));
1094 fputs("</td><td>", fp);
1096 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1097 fputs("</td></tr>\n", fp);
1101 commitinfo_free(ci);
1102 git_object_free(obj);
1104 git_reference_free(dref);
1109 fputs("</tbody></table><br/>", fp);
1113 git_object_free(obj);
1114 git_reference_free(dref);
1116 for (i = 0; i < refcount; i++)
1117 git_reference_free(refs[i]);
1126 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1131 main(int argc, char *argv[])
1133 git_object *obj = NULL;
1134 const git_oid *head = NULL;
1137 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1138 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1142 for (i = 1; i < argc; i++) {
1143 if (argv[i][0] != '-') {
1147 } else if (argv[i][1] == 'c') {
1148 if (nlogcommits > 0 || i + 1 >= argc)
1150 cachefile = argv[++i];
1151 } else if (argv[i][1] == 'l') {
1152 if (cachefile || i + 1 >= argc)
1155 nlogcommits = strtoll(argv[++i], &p, 10);
1156 if (argv[i][0] == '\0' || *p != '\0' ||
1157 nlogcommits <= 0 || errno)
1164 if (!realpath(repodir, repodirabs))
1170 if (unveil(repodir, "r") == -1)
1171 err(1, "unveil: %s", repodir);
1172 if (unveil(".", "rwc") == -1)
1173 err(1, "unveil: .");
1174 if (cachefile && unveil(cachefile, "rwc") == -1)
1175 err(1, "unveil: %s", cachefile);
1178 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1181 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1186 if (git_repository_open_ext(&repo, repodir,
1187 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1188 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1193 if (!git_revparse_single(&obj, repo, "HEAD"))
1194 head = git_object_id(obj);
1195 git_object_free(obj);
1197 /* use directory name as name */
1198 if ((name = strrchr(repodirabs, '/')))
1205 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1206 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1207 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1208 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1210 /* strip .git suffix */
1211 if (!(strippedname = strdup(name)))
1213 if ((p = strrchr(strippedname, '.')))
1214 if (!strcmp(p, ".git"))
1217 /* read description or .git/description */
1218 joinpath(path, sizeof(path), repodir, "description");
1219 if (!(fpread = fopen(path, "r"))) {
1220 joinpath(path, sizeof(path), repodir, ".git/description");
1221 fpread = fopen(path, "r");
1224 if (!fgets(description, sizeof(description), fpread))
1225 description[0] = '\0';
1229 /* read url or .git/url */
1230 joinpath(path, sizeof(path), repodir, "url");
1231 if (!(fpread = fopen(path, "r"))) {
1232 joinpath(path, sizeof(path), repodir, ".git/url");
1233 fpread = fopen(path, "r");
1236 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1238 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1243 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1244 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1245 git_object_type(obj) == GIT_OBJ_BLOB)
1246 license = licensefiles[i] + strlen("HEAD:");
1247 git_object_free(obj);
1251 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1252 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1253 git_object_type(obj) == GIT_OBJ_BLOB)
1254 readme = readmefiles[i] + strlen("HEAD:");
1255 git_object_free(obj);
1258 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1259 git_object_type(obj) == GIT_OBJ_BLOB)
1260 submodules = ".gitmodules";
1261 git_object_free(obj);
1263 /* Generate tarball */
1265 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1266 strippedname, repodir);
1270 fp = efopen("log.html", "w");
1272 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1273 writeheader(fp, "Log");
1274 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1275 "<td><b>Commit</b></td>"
1276 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1277 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1278 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1280 if (cachefile && head) {
1281 /* read from cache file (does not need to exist) */
1282 if ((rcachefp = fopen(cachefile, "r"))) {
1283 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1284 errx(1, "%s: no object id", cachefile);
1285 if (git_oid_fromstr(&lastoid, lastoidstr))
1286 errx(1, "%s: invalid object id", cachefile);
1289 /* write log to (temporary) cache */
1290 if ((fd = mkstemp(tmppath)) == -1)
1292 if (!(wcachefp = fdopen(fd, "w")))
1293 err(1, "fdopen: '%s'", tmppath);
1294 /* write last commit id (HEAD) */
1295 git_oid_tostr(buf, sizeof(buf), head);
1296 fprintf(wcachefp, "%s\n", buf);
1301 /* append previous log to log.html and the new cache */
1302 while (!feof(rcachefp)) {
1303 n = fread(buf, 1, sizeof(buf), rcachefp);
1304 if (ferror(rcachefp))
1306 if (fwrite(buf, 1, n, fp) != n ||
1307 fwrite(buf, 1, n, wcachefp) != n)
1318 fputs("</tbody></table>", fp);
1322 /* files for HEAD */
1323 fp = efopen("files.html", "w");
1324 writeheader(fp, "Files");
1326 writefiles(fp, head);
1330 cp("files.html", "index.html");
1332 /* summary page with branches and tags */
1333 fp = efopen("refs.html", "w");
1334 writeheader(fp, "Refs");
1340 fp = efopen("atom.xml", "w");
1344 /* rename new cache file on success */
1345 if (cachefile && head) {
1346 if (rename(tmppath, cachefile))
1347 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1348 umask((mask = umask(0)));
1349 if (chmod(cachefile,
1350 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1351 err(1, "chmod: '%s'", cachefile);
1355 git_repository_free(repo);
1356 git_libgit2_shutdown();