17 #include <cmark-gfm.h>
32 char oid[GIT_OID_HEXSZ + 1];
33 char parentoid[GIT_OID_HEXSZ + 1];
35 const git_signature *author;
36 const git_signature *committer;
43 git_tree *commit_tree;
44 git_tree *parent_tree;
50 struct deltainfo **deltas;
54 static git_repository *repo;
56 static const char *relpath = "";
57 static const char *repodir;
59 static char *name = "";
60 static char *strippedname = "";
61 static char description[255];
62 static char cloneurl[1024];
63 static char *submodules;
64 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
66 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
68 static long long nlogcommits = -1; /* < 0 indicates not used */
70 bool htmlized; /* true if markdoown converted to HTML */
73 static git_oid lastoid;
74 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
75 static FILE *rcachefp, *wcachefp;
76 static const char *cachefile;
79 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
83 r = snprintf(buf, bufsiz, "%s%s%s",
84 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
85 if (r < 0 || (size_t)r >= bufsiz)
86 errx(1, "path truncated: '%s%s%s'",
87 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
91 deltainfo_free(struct deltainfo *di)
95 git_patch_free(di->patch);
96 memset(di, 0, sizeof(*di));
101 commitinfo_getstats(struct commitinfo *ci)
103 struct deltainfo *di;
104 git_diff_options opts;
105 git_diff_find_options fopts;
106 const git_diff_delta *delta;
107 const git_diff_hunk *hunk;
108 const git_diff_line *line;
109 git_patch *patch = NULL;
110 size_t ndeltas, nhunks, nhunklines;
113 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
115 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
116 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
118 ci->parent_tree = NULL;
122 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
123 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
124 GIT_DIFF_IGNORE_SUBMODULES |
125 GIT_DIFF_INCLUDE_TYPECHANGE;
126 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
129 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
131 /* find renames and copies, exact matches (no heuristic) for renames. */
132 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
133 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
134 if (git_diff_find_similar(ci->diff, &fopts))
137 ndeltas = git_diff_num_deltas(ci->diff);
138 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
141 for (i = 0; i < ndeltas; i++) {
142 if (git_patch_from_diff(&patch, ci->diff, i))
145 if (!(di = calloc(1, sizeof(struct deltainfo))))
150 delta = git_patch_get_delta(patch);
152 /* skip stats for binary data */
153 if (delta->flags & GIT_DIFF_FLAG_BINARY)
156 nhunks = git_patch_num_hunks(patch);
157 for (j = 0; j < nhunks; j++) {
158 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
161 if (git_patch_get_line_in_hunk(&line, patch, j, k))
163 if (line->old_lineno == -1) {
166 } else if (line->new_lineno == -1) {
179 git_diff_free(ci->diff);
181 git_tree_free(ci->commit_tree);
182 ci->commit_tree = NULL;
183 git_tree_free(ci->parent_tree);
184 ci->parent_tree = NULL;
185 git_commit_free(ci->parent);
189 for (i = 0; i < ci->ndeltas; i++)
190 deltainfo_free(ci->deltas[i]);
202 commitinfo_free(struct commitinfo *ci)
209 for (i = 0; i < ci->ndeltas; i++)
210 deltainfo_free(ci->deltas[i]);
213 git_diff_free(ci->diff);
214 git_tree_free(ci->commit_tree);
215 git_tree_free(ci->parent_tree);
216 git_commit_free(ci->commit);
217 git_commit_free(ci->parent);
218 memset(ci, 0, sizeof(*ci));
223 commitinfo_getbyoid(const git_oid *id)
225 struct commitinfo *ci;
227 if (!(ci = calloc(1, sizeof(struct commitinfo))))
230 if (git_commit_lookup(&(ci->commit), repo, id))
234 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
235 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
237 ci->author = git_commit_author(ci->commit);
238 ci->committer = git_commit_committer(ci->commit);
239 ci->summary = git_commit_summary(ci->commit);
240 ci->msg = git_commit_message(ci->commit);
251 efopen(const char *name, const char *flags)
255 if (!(fp = fopen(name, flags)))
256 err(1, "fopen: '%s'", name);
261 /* Escape characters below as HTML 2.0 / XML 1.0. */
263 xmlencode(FILE *fp, const char *s, size_t len)
267 for (i = 0; *s && i < len; s++, i++) {
269 case '<': fputs("<", fp); break;
270 case '>': fputs(">", fp); break;
271 case '\'': fputs("'", fp); break;
272 case '&': fputs("&", fp); break;
273 case '"': fputs(""", fp); break;
274 default: fputc(*s, fp);
280 mkdirp(const char *path)
282 char tmp[PATH_MAX], *p;
284 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
285 errx(1, "path truncated: '%s'", path);
286 for (p = tmp + (tmp[0] == '/'); *p; p++) {
290 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
294 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
300 printtimez(FILE *fp, const git_time *intime)
306 t = (time_t)intime->time;
307 if (!(intm = gmtime(&t)))
309 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
314 printtime(FILE *fp, const git_time *intime)
320 t = (time_t)intime->time + (intime->offset * 60);
321 if (!(intm = gmtime(&t)))
323 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
324 if (intime->offset < 0)
325 fprintf(fp, "%s -%02d%02d", out,
326 -(intime->offset) / 60, -(intime->offset) % 60);
328 fprintf(fp, "%s +%02d%02d", out,
329 intime->offset / 60, intime->offset % 60);
333 printtimeshort(FILE *fp, const git_time *intime)
339 t = (time_t)intime->time;
340 if (!(intm = gmtime(&t)))
342 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
347 writeheader(FILE *fp, const char *title)
349 fputs("<!DOCTYPE html>\n"
351 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
352 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
354 xmlencode(fp, title, strlen(title));
355 if (title[0] && strippedname[0])
357 xmlencode(fp, strippedname, strlen(strippedname));
360 xmlencode(fp, description, strlen(description));
361 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
362 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
364 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
365 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
366 fputs("</head>\n<body>\n<table><tr><td>", fp);
367 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
369 fputs("</td><td><h1>", fp);
370 xmlencode(fp, strippedname, strlen(strippedname));
371 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
372 xmlencode(fp, description, strlen(description));
373 fputs("</span></td></tr>", fp);
375 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
376 xmlencode(fp, cloneurl, strlen(cloneurl));
378 xmlencode(fp, cloneurl, strlen(cloneurl));
379 fputs("</a></span></td></tr>", fp);
381 fputs("<tr><td></td><td>\n", fp);
382 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
383 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
384 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
386 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
387 relpath, submodules);
389 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
392 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
394 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
395 relpath, strippedname);
396 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
400 writefooter(FILE *fp)
402 fputs("</div>\n</body>\n</html>\n", fp);
406 get_ext(const char *filename)
408 const char *dot = strrchr(filename, '.');
409 if(!dot || dot == filename) return "";
414 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
420 char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
421 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
426 int stdout_copy = dup(1);
431 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
432 strncat(cmd, filename, strlen(filename) + 1);
433 FILE *child = popen(cmd, "w");
435 printf("child is null: %s", strerror(errno));
439 // Give code to highlight through STDIN:
441 for (i = 0; *s && i < len; s++, i++) {
442 if (*s == '\n') lc++;
443 fprintf(child, "%c", *s);
450 dup2(stdout_copy, 1);
453 fprintf(fp, "%s", html);
460 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
463 const char *s = git_blob_rawcontent(blob);
464 git_off_t len = git_blob_rawsize(blob);
467 lc = call_chroma(filename, fp, s, len);
474 printcommit(FILE *fp, struct commitinfo *ci)
476 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
477 relpath, ci->oid, ci->oid);
479 if (ci->parentoid[0])
480 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
481 relpath, ci->parentoid, ci->parentoid);
484 fputs("<b>Author:</b> ", fp);
485 xmlencode(fp, ci->author->name, strlen(ci->author->name));
486 fputs(" <<a href=\"mailto:", fp);
487 xmlencode(fp, ci->author->email, strlen(ci->author->email));
489 xmlencode(fp, ci->author->email, strlen(ci->author->email));
490 fputs("</a>>\n<b>Date:</b> ", fp);
491 printtime(fp, &(ci->author->when));
496 xmlencode(fp, ci->msg, strlen(ci->msg));
502 printshowfile(FILE *fp, struct commitinfo *ci)
504 const git_diff_delta *delta;
505 const git_diff_hunk *hunk;
506 const git_diff_line *line;
508 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
517 if (ci->filecount > 1000 ||
518 ci->ndeltas > 1000 ||
519 ci->addcount > 100000 ||
520 ci->delcount > 100000) {
521 fputs("Diff is too large, output suppressed.\n", fp);
526 fputs("<b>Diffstat:</b>\n<table>", fp);
527 for (i = 0; i < ci->ndeltas; i++) {
528 delta = git_patch_get_delta(ci->deltas[i]->patch);
530 switch (delta->status) {
531 case GIT_DELTA_ADDED: c = 'A'; break;
532 case GIT_DELTA_COPIED: c = 'C'; break;
533 case GIT_DELTA_DELETED: c = 'D'; break;
534 case GIT_DELTA_MODIFIED: c = 'M'; break;
535 case GIT_DELTA_RENAMED: c = 'R'; break;
536 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
537 default: c = ' '; break;
540 fprintf(fp, "<tr><td>%c", c);
542 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
544 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
545 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
546 if (strcmp(delta->old_file.path, delta->new_file.path)) {
547 fputs(" -> ", fp);
548 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
551 add = ci->deltas[i]->addcount;
552 del = ci->deltas[i]->delcount;
554 total = sizeof(linestr) - 2;
555 if (changed > total) {
557 add = ((float)total / changed * add) + 1;
559 del = ((float)total / changed * del) + 1;
561 memset(&linestr, '+', add);
562 memset(&linestr[add], '-', del);
564 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
565 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
566 fwrite(&linestr, 1, add, fp);
567 fputs("</span><span class=\"d\">", fp);
568 fwrite(&linestr[add], 1, del, fp);
569 fputs("</span></td></tr>\n", fp);
571 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
572 ci->filecount, ci->filecount == 1 ? "" : "s",
573 ci->addcount, ci->addcount == 1 ? "" : "s",
574 ci->delcount, ci->delcount == 1 ? "" : "s");
578 for (i = 0; i < ci->ndeltas; i++) {
579 patch = ci->deltas[i]->patch;
580 delta = git_patch_get_delta(patch);
581 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
582 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
583 fputs(".html\">", fp);
584 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
585 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
586 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
587 fprintf(fp, ".html\">");
588 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
589 fprintf(fp, "</a></b>\n");
591 /* check binary data */
592 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
593 fputs("Binary files differ.\n", fp);
597 nhunks = git_patch_num_hunks(patch);
598 for (j = 0; j < nhunks; j++) {
599 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
602 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
603 xmlencode(fp, hunk->header, hunk->header_len);
607 if (git_patch_get_line_in_hunk(&line, patch, j, k))
609 if (line->old_lineno == -1)
610 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
612 else if (line->new_lineno == -1)
613 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
617 xmlencode(fp, line->content, line->content_len);
618 if (line->old_lineno == -1 || line->new_lineno == -1)
626 writelogline(FILE *fp, struct commitinfo *ci)
628 fputs("<tr><td>", fp);
630 printtimeshort(fp, &(ci->author->when));
631 fputs("</td><td>", fp);
633 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
634 xmlencode(fp, ci->summary, strlen(ci->summary));
637 fputs("</td><td>", fp);
639 xmlencode(fp, ci->author->name, strlen(ci->author->name));
640 fputs("</td><td class=\"num\" align=\"right\">", fp);
641 fprintf(fp, "%zu", ci->filecount);
642 fputs("</td><td class=\"num\" align=\"right\">", fp);
643 fprintf(fp, "+%zu", ci->addcount);
644 fputs("</td><td class=\"num\" align=\"right\">", fp);
645 fprintf(fp, "-%zu", ci->delcount);
646 fputs("</td></tr>\n", fp);
650 writelog(FILE *fp, const git_oid *oid)
652 struct commitinfo *ci;
653 git_revwalk *w = NULL;
655 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
659 git_revwalk_new(&w, repo);
660 git_revwalk_push(w, oid);
661 git_revwalk_simplify_first_parent(w);
663 while (!git_revwalk_next(&id, w)) {
666 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
669 git_oid_tostr(oidstr, sizeof(oidstr), &id);
670 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
671 if (r < 0 || (size_t)r >= sizeof(path))
672 errx(1, "path truncated: 'commit/%s.html'", oidstr);
673 r = access(path, F_OK);
675 /* optimization: if there are no log lines to write and
676 the commit file already exists: skip the diffstat */
677 if (!nlogcommits && !r)
680 if (!(ci = commitinfo_getbyoid(&id)))
682 /* diffstat: for stagit HTML required for the log.html line */
683 if (commitinfo_getstats(ci) == -1)
686 if (nlogcommits < 0) {
687 writelogline(fp, ci);
688 } else if (nlogcommits > 0) {
689 writelogline(fp, ci);
691 if (!nlogcommits && ci->parentoid[0])
692 fputs("<tr><td></td><td colspan=\"5\">"
693 "More commits remaining [...]</td>"
698 writelogline(wcachefp, ci);
700 /* check if file exists if so skip it */
703 fpfile = efopen(path, "w");
704 writeheader(fpfile, ci->summary);
705 fputs("<pre>", fpfile);
706 printshowfile(fpfile, ci);
707 fputs("</pre>\n", fpfile);
722 printcommitatom(FILE *fp, struct commitinfo *ci)
724 fputs("<entry>\n", fp);
726 fprintf(fp, "<id>%s</id>\n", ci->oid);
728 fputs("<published>", fp);
729 printtimez(fp, &(ci->author->when));
730 fputs("</published>\n", fp);
733 fputs("<updated>", fp);
734 printtimez(fp, &(ci->committer->when));
735 fputs("</updated>\n", fp);
738 fputs("<title type=\"text\">", fp);
739 xmlencode(fp, ci->summary, strlen(ci->summary));
740 fputs("</title>\n", fp);
742 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
746 fputs("<author>\n<name>", fp);
747 xmlencode(fp, ci->author->name, strlen(ci->author->name));
748 fputs("</name>\n<email>", fp);
749 xmlencode(fp, ci->author->email, strlen(ci->author->email));
750 fputs("</email>\n</author>\n", fp);
753 fputs("<content type=\"text\">", fp);
754 fprintf(fp, "commit %s\n", ci->oid);
755 if (ci->parentoid[0])
756 fprintf(fp, "parent %s\n", ci->parentoid);
758 fputs("Author: ", fp);
759 xmlencode(fp, ci->author->name, strlen(ci->author->name));
761 xmlencode(fp, ci->author->email, strlen(ci->author->email));
762 fputs(">\nDate: ", fp);
763 printtime(fp, &(ci->author->when));
768 xmlencode(fp, ci->msg, strlen(ci->msg));
770 fputs("\n</content>\n</entry>\n", fp);
776 struct commitinfo *ci;
777 git_revwalk *w = NULL;
779 size_t i, m = 100; /* last 'm' commits */
781 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
782 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
783 xmlencode(fp, strippedname, strlen(strippedname));
784 fputs(", branch HEAD</title>\n<subtitle>", fp);
785 xmlencode(fp, description, strlen(description));
786 fputs("</subtitle>\n", fp);
788 git_revwalk_new(&w, repo);
789 git_revwalk_push_head(w);
790 git_revwalk_simplify_first_parent(w);
792 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
793 if (!(ci = commitinfo_getbyoid(&id)))
795 printcommitatom(fp, ci);
800 fputs("</feed>\n", fp);
806 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
808 char tmp[PATH_MAX] = "", *d;
813 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
814 errx(1, "path truncated: '%s'", fpath);
815 if (!(d = dirname(tmp)))
820 for (p = fpath, tmp[0] = '\0'; *p; p++) {
821 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
822 errx(1, "path truncated: '../%s'", tmp);
826 fp = efopen(fpath, "w");
827 writeheader(fp, filename);
829 xmlencode(fp, filename, strlen(filename));
830 fprintf(fp, " (%juB)", (uintmax_t)filesize);
832 fputs("</p><hr/>", fp);
834 if (git_blob_is_binary((git_blob *)obj)) {
835 fputs("<p>Binary file.</p>\n", fp);
837 lc = writeblobhtml(filename, fp, (git_blob *)obj);
851 filemode(git_filemode_t m)
853 static char mode[11];
855 memset(mode, '-', sizeof(mode) - 1);
866 else if (S_ISFIFO(m))
870 else if (S_ISSOCK(m))
875 if (m & S_IRUSR) mode[1] = 'r';
876 if (m & S_IWUSR) mode[2] = 'w';
877 if (m & S_IXUSR) mode[3] = 'x';
878 if (m & S_IRGRP) mode[4] = 'r';
879 if (m & S_IWGRP) mode[5] = 'w';
880 if (m & S_IXGRP) mode[6] = 'x';
881 if (m & S_IROTH) mode[7] = 'r';
882 if (m & S_IWOTH) mode[8] = 'w';
883 if (m & S_IXOTH) mode[9] = 'x';
885 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
886 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
887 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
893 writefilestree(FILE *fp, git_tree *tree, const char *path)
895 const git_tree_entry *entry = NULL;
896 git_submodule *module = NULL;
897 git_object *obj = NULL;
899 const char *entryname;
900 char filepath[PATH_MAX], entrypath[PATH_MAX];
904 count = git_tree_entrycount(tree);
905 for (i = 0; i < count; i++) {
906 if (!(entry = git_tree_entry_byindex(tree, i)) ||
907 !(entryname = git_tree_entry_name(entry)))
909 joinpath(entrypath, sizeof(entrypath), path, entryname);
911 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
913 if (r < 0 || (size_t)r >= sizeof(filepath))
914 errx(1, "path truncated: 'file/%s.html'", entrypath);
916 if (!git_tree_entry_to_object(&obj, repo, entry)) {
917 switch (git_object_type(obj)) {
922 ret = writefilestree(fp, (git_tree *)obj,
924 git_object_free(obj);
929 git_object_free(obj);
933 filesize = git_blob_rawsize((git_blob *)obj);
934 lc = writeblob(obj, filepath, entryname, filesize);
936 fputs("<tr><td>", fp);
937 fputs(filemode(git_tree_entry_filemode(entry)), fp);
938 fprintf(fp, "</td><td><a href=\"%s", relpath);
939 xmlencode(fp, filepath, strlen(filepath));
941 xmlencode(fp, entrypath, strlen(entrypath));
942 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
944 fprintf(fp, "%dL", lc);
946 fprintf(fp, "%juB", (uintmax_t)filesize);
947 fputs("</td></tr>\n", fp);
948 git_object_free(obj);
949 } else if (!git_submodule_lookup(&module, repo, entryname)) {
950 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
952 xmlencode(fp, entrypath, strlen(entrypath));
953 git_submodule_free(module);
954 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
962 writefiles(FILE *fp, const git_oid *id)
964 git_tree *tree = NULL;
965 git_commit *commit = NULL;
968 fputs("<table id=\"files\"><thead>\n<tr>"
969 "<td><b>Mode</b></td><td><b>Name</b></td>"
970 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
971 "</tr>\n</thead><tbody>\n", fp);
973 if (!git_commit_lookup(&commit, repo, id) &&
974 !git_commit_tree(&tree, commit))
975 ret = writefilestree(fp, tree, "");
977 fputs("</tbody></table>", fp);
979 git_commit_free(commit);
986 refs_cmp(const void *v1, const void *v2)
988 git_reference *r1 = (*(git_reference **)v1);
989 git_reference *r2 = (*(git_reference **)v2);
992 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
995 return strcmp(git_reference_shorthand(r1),
996 git_reference_shorthand(r2));
1002 struct commitinfo *ci;
1003 const git_oid *id = NULL;
1004 git_object *obj = NULL;
1005 git_reference *dref = NULL, *r, *ref = NULL;
1006 git_reference_iterator *it = NULL;
1007 git_reference **refs = NULL;
1008 size_t count, i, j, refcount;
1009 const char *titles[] = { "Branches", "Tags" };
1010 const char *ids[] = { "branches", "tags" };
1013 if (git_reference_iterator_new(&it, repo))
1016 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1017 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1019 refs[refcount] = ref;
1021 git_reference_iterator_free(it);
1023 /* sort by type then shorthand name */
1024 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1026 for (j = 0; j < 2; j++) {
1027 for (i = 0, count = 0; i < refcount; i++) {
1028 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1029 !(git_reference_is_tag(refs[i]) && j == 1))
1032 switch (git_reference_type(refs[i])) {
1033 case GIT_REF_SYMBOLIC:
1034 if (git_reference_resolve(&dref, refs[i]))
1044 if (!git_reference_target(r) ||
1045 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1047 if (!(id = git_object_id(obj)))
1049 if (!(ci = commitinfo_getbyoid(id)))
1052 /* print header if it has an entry (first). */
1054 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1055 "<thead>\n<tr><td><b>Name</b></td>"
1056 "<td><b>Last commit date</b></td>"
1057 "<td><b>Author</b></td>\n</tr>\n"
1058 "</thead><tbody>\n",
1063 name = git_reference_shorthand(r);
1065 fputs("<tr><td>", fp);
1066 xmlencode(fp, name, strlen(name));
1067 fputs("</td><td>", fp);
1069 printtimeshort(fp, &(ci->author->when));
1070 fputs("</td><td>", fp);
1072 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1073 fputs("</td></tr>\n", fp);
1077 commitinfo_free(ci);
1078 git_object_free(obj);
1080 git_reference_free(dref);
1085 fputs("</tbody></table><br/>", fp);
1089 git_object_free(obj);
1090 git_reference_free(dref);
1092 for (i = 0; i < refcount; i++)
1093 git_reference_free(refs[i]);
1102 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1107 main(int argc, char *argv[])
1109 git_object *obj = NULL;
1110 const git_oid *head = NULL;
1113 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1114 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1118 for (i = 1; i < argc; i++) {
1119 if (argv[i][0] != '-') {
1123 } else if (argv[i][1] == 'c') {
1124 if (nlogcommits > 0 || i + 1 >= argc)
1126 cachefile = argv[++i];
1127 } else if (argv[i][1] == 'l') {
1128 if (cachefile || i + 1 >= argc)
1131 nlogcommits = strtoll(argv[++i], &p, 10);
1132 if (argv[i][0] == '\0' || *p != '\0' ||
1133 nlogcommits <= 0 || errno)
1140 if (!realpath(repodir, repodirabs))
1146 if (unveil(repodir, "r") == -1)
1147 err(1, "unveil: %s", repodir);
1148 if (unveil(".", "rwc") == -1)
1149 err(1, "unveil: .");
1150 if (cachefile && unveil(cachefile, "rwc") == -1)
1151 err(1, "unveil: %s", cachefile);
1154 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1157 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1162 if (git_repository_open_ext(&repo, repodir,
1163 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1164 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1169 if (!git_revparse_single(&obj, repo, "HEAD"))
1170 head = git_object_id(obj);
1171 git_object_free(obj);
1173 /* use directory name as name */
1174 if ((name = strrchr(repodirabs, '/')))
1181 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1182 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1183 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1184 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1186 /* strip .git suffix */
1187 if (!(strippedname = strdup(name)))
1189 if ((p = strrchr(strippedname, '.')))
1190 if (!strcmp(p, ".git"))
1193 /* read description or .git/description */
1194 joinpath(path, sizeof(path), repodir, "description");
1195 if (!(fpread = fopen(path, "r"))) {
1196 joinpath(path, sizeof(path), repodir, ".git/description");
1197 fpread = fopen(path, "r");
1200 if (!fgets(description, sizeof(description), fpread))
1201 description[0] = '\0';
1205 /* read url or .git/url */
1206 joinpath(path, sizeof(path), repodir, "url");
1207 if (!(fpread = fopen(path, "r"))) {
1208 joinpath(path, sizeof(path), repodir, ".git/url");
1209 fpread = fopen(path, "r");
1212 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1214 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1219 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1220 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1221 git_object_type(obj) == GIT_OBJ_BLOB)
1222 license = licensefiles[i] + strlen("HEAD:");
1223 git_object_free(obj);
1227 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1228 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1229 git_object_type(obj) == GIT_OBJ_BLOB)
1230 readme = readmefiles[i] + strlen("HEAD:");
1231 git_object_free(obj);
1234 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1235 git_object_type(obj) == GIT_OBJ_BLOB)
1236 submodules = ".gitmodules";
1237 git_object_free(obj);
1239 /* Generate tarball */
1241 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1242 strippedname, repodir);
1246 fp = efopen("log.html", "w");
1248 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1249 writeheader(fp, "Log");
1250 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1251 "<td><b>Commit</b></td>"
1252 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1253 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1254 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1256 if (cachefile && head) {
1257 /* read from cache file (does not need to exist) */
1258 if ((rcachefp = fopen(cachefile, "r"))) {
1259 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1260 errx(1, "%s: no object id", cachefile);
1261 if (git_oid_fromstr(&lastoid, lastoidstr))
1262 errx(1, "%s: invalid object id", cachefile);
1265 /* write log to (temporary) cache */
1266 if ((fd = mkstemp(tmppath)) == -1)
1268 if (!(wcachefp = fdopen(fd, "w")))
1269 err(1, "fdopen: '%s'", tmppath);
1270 /* write last commit id (HEAD) */
1271 git_oid_tostr(buf, sizeof(buf), head);
1272 fprintf(wcachefp, "%s\n", buf);
1277 /* append previous log to log.html and the new cache */
1278 while (!feof(rcachefp)) {
1279 n = fread(buf, 1, sizeof(buf), rcachefp);
1280 if (ferror(rcachefp))
1282 if (fwrite(buf, 1, n, fp) != n ||
1283 fwrite(buf, 1, n, wcachefp) != n)
1294 fputs("</tbody></table>", fp);
1298 /* files for HEAD */
1299 fp = efopen("files.html", "w");
1300 writeheader(fp, "Files");
1302 writefiles(fp, head);
1306 cp("files.html", "index.html");
1308 /* summary page with branches and tags */
1309 fp = efopen("refs.html", "w");
1310 writeheader(fp, "Refs");
1316 fp = efopen("atom.xml", "w");
1320 /* rename new cache file on success */
1321 if (cachefile && head) {
1322 if (rename(tmppath, cachefile))
1323 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1324 umask((mask = umask(0)));
1325 if (chmod(cachefile,
1326 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1327 err(1, "chmod: '%s'", cachefile);
1331 git_repository_free(repo);
1332 git_libgit2_shutdown();