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 <<<<<<< HEAD:src/stagit.c
338 case '\r': break; /* ignore CR */
339 case '\n': break; /* ignore LF */
341 >>>>>>> 722f836 (micro-optimization: fputc (function) -> putc (macro/inline function)):stagit.c
342 default: putc(*s, fp);
348 mkdirp(const char *path)
350 char tmp[PATH_MAX], *p;
352 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
353 errx(1, "path truncated: '%s'", path);
354 for (p = tmp + (tmp[0] == '/'); *p; p++) {
358 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
362 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
368 printtimez(FILE *fp, const git_time *intime)
374 t = (time_t)intime->time;
375 if (!(intm = gmtime(&t)))
377 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
382 printtime(FILE *fp, const git_time *intime)
388 t = (time_t)intime->time + (intime->offset * 60);
389 if (!(intm = gmtime(&t)))
391 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
392 if (intime->offset < 0)
393 fprintf(fp, "%s -%02d%02d", out,
394 -(intime->offset) / 60, -(intime->offset) % 60);
396 fprintf(fp, "%s +%02d%02d", out,
397 intime->offset / 60, intime->offset % 60);
401 printtimeshort(FILE *fp, const git_time *intime)
407 t = (time_t)intime->time;
408 if (!(intm = gmtime(&t)))
410 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
415 writeheader(FILE *fp, const char *title)
417 fputs("<!DOCTYPE html>\n"
418 "<html lang=\"en\">\n<head>\n"
419 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
420 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
422 xmlencode(fp, title, strlen(title));
423 if (title[0] && strippedname[0])
425 xmlencode(fp, strippedname, strlen(strippedname));
428 xmlencode(fp, description, strlen(description));
429 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
430 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
432 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
433 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
434 fputs("</head>\n<body>\n<table><tr><td>", fp);
435 fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
437 fputs("</td><td><h1>", fp);
438 xmlencode(fp, strippedname, strlen(strippedname));
439 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
440 xmlencode(fp, description, strlen(description));
441 fputs("</span></td></tr>", fp);
443 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
444 xmlencode(fp, cloneurl, strlen(cloneurl));
446 xmlencode(fp, cloneurl, strlen(cloneurl));
447 fputs("</a></span></td></tr>", fp);
449 fputs("<tr><td></td><td>\n", fp);
450 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
451 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
452 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
454 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
455 relpath, submodules);
457 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
460 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
462 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
463 relpath, strippedname);
464 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
468 writefooter(FILE *fp)
470 fputs("</div>\n</body>\n</html>\n", fp);
474 get_ext(const char *filename)
476 const char *dot = strrchr(filename, '.');
477 if(!dot || dot == filename) return "";
482 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
490 html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
491 if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
497 int stdout_copy = dup(1);
502 char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
503 strncat(cmd, filename, strlen(filename) + 1);
504 FILE *child = popen(cmd, "w");
506 printf("child is null: %s", strerror(errno));
510 // Give code to highlight through STDIN:
512 for (i = 0; *s && i < len; s++, i++) {
513 fprintf(child, "%c", *s);
520 dup2(stdout_copy, 1);
523 fprintf(fp, "%s", html);
526 fprintf(fp, "<pre>%s</pre>", s);
531 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
533 const char *s = git_blob_rawcontent(blob);
534 git_off_t len = git_blob_rawsize(blob);
537 call_chroma(filename, fp, s, len);
542 printcommit(FILE *fp, struct commitinfo *ci)
544 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
545 relpath, ci->oid, ci->oid);
547 if (ci->parentoid[0])
548 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
549 relpath, ci->parentoid, ci->parentoid);
552 fputs("<b>Author:</b> ", fp);
553 xmlencode(fp, ci->author->name, strlen(ci->author->name));
554 fputs(" <<a href=\"mailto:", fp);
555 xmlencode(fp, ci->author->email, strlen(ci->author->email));
557 xmlencode(fp, ci->author->email, strlen(ci->author->email));
558 fputs("</a>>\n<b>Date:</b> ", fp);
559 printtime(fp, &(ci->author->when));
564 xmlencode(fp, ci->msg, strlen(ci->msg));
570 printshowfile(FILE *fp, struct commitinfo *ci)
572 const git_diff_delta *delta;
573 const git_diff_hunk *hunk;
574 const git_diff_line *line;
576 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
585 if (ci->filecount > 1000 ||
586 ci->ndeltas > 1000 ||
587 ci->addcount > 100000 ||
588 ci->delcount > 100000) {
589 fputs("Diff is too large, output suppressed.\n", fp);
594 fputs("<b>Diffstat:</b>\n<table>", fp);
595 for (i = 0; i < ci->ndeltas; i++) {
596 delta = git_patch_get_delta(ci->deltas[i]->patch);
598 switch (delta->status) {
599 case GIT_DELTA_ADDED: c = 'A'; break;
600 case GIT_DELTA_COPIED: c = 'C'; break;
601 case GIT_DELTA_DELETED: c = 'D'; break;
602 case GIT_DELTA_MODIFIED: c = 'M'; break;
603 case GIT_DELTA_RENAMED: c = 'R'; break;
604 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
605 default: c = ' '; break;
608 fprintf(fp, "<tr><td>%c", c);
610 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
612 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
613 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
614 if (strcmp(delta->old_file.path, delta->new_file.path)) {
615 fputs(" -> ", fp);
616 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
619 add = ci->deltas[i]->addcount;
620 del = ci->deltas[i]->delcount;
622 total = sizeof(linestr) - 2;
623 if (changed > total) {
625 add = ((float)total / changed * add) + 1;
627 del = ((float)total / changed * del) + 1;
629 memset(&linestr, '+', add);
630 memset(&linestr[add], '-', del);
632 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
633 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
634 fwrite(&linestr, 1, add, fp);
635 fputs("</span><span class=\"d\">", fp);
636 fwrite(&linestr[add], 1, del, fp);
637 fputs("</span></td></tr>\n", fp);
639 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
640 ci->filecount, ci->filecount == 1 ? "" : "s",
641 ci->addcount, ci->addcount == 1 ? "" : "s",
642 ci->delcount, ci->delcount == 1 ? "" : "s");
646 for (i = 0; i < ci->ndeltas; i++) {
647 patch = ci->deltas[i]->patch;
648 delta = git_patch_get_delta(patch);
649 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
650 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
651 fputs(".html\">", fp);
652 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
653 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
654 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
655 fprintf(fp, ".html\">");
656 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
657 fprintf(fp, "</a></b>\n");
659 /* check binary data */
660 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
661 fputs("Binary files differ.\n", fp);
665 nhunks = git_patch_num_hunks(patch);
666 for (j = 0; j < nhunks; j++) {
667 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
670 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
671 xmlencode(fp, hunk->header, hunk->header_len);
675 if (git_patch_get_line_in_hunk(&line, patch, j, k))
677 if (line->old_lineno == -1)
678 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
680 else if (line->new_lineno == -1)
681 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
685 xmlencodeline(fp, line->content, line->content_len);
687 if (line->old_lineno == -1 || line->new_lineno == -1)
695 writelogline(FILE *fp, struct commitinfo *ci)
697 fputs("<tr><td>", fp);
699 printtimeshort(fp, &(ci->author->when));
700 fputs("</td><td>", fp);
702 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
703 xmlencode(fp, ci->summary, strlen(ci->summary));
706 fputs("</td><td>", fp);
708 xmlencode(fp, ci->author->name, strlen(ci->author->name));
709 fputs("</td><td class=\"num\" align=\"right\">", fp);
710 fprintf(fp, "%zu", ci->filecount);
711 fputs("</td><td class=\"num\" align=\"right\">", fp);
712 fprintf(fp, "+%zu", ci->addcount);
713 fputs("</td><td class=\"num\" align=\"right\">", fp);
714 fprintf(fp, "-%zu", ci->delcount);
715 fputs("</td></tr>\n", fp);
719 writelog(FILE *fp, const git_oid *oid)
721 struct commitinfo *ci;
722 git_revwalk *w = NULL;
724 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
728 git_revwalk_new(&w, repo);
729 git_revwalk_push(w, oid);
730 git_revwalk_simplify_first_parent(w);
732 while (!git_revwalk_next(&id, w)) {
735 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
738 git_oid_tostr(oidstr, sizeof(oidstr), &id);
739 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
740 if (r < 0 || (size_t)r >= sizeof(path))
741 errx(1, "path truncated: 'commit/%s.html'", oidstr);
742 r = access(path, F_OK);
744 /* optimization: if there are no log lines to write and
745 the commit file already exists: skip the diffstat */
746 if (!nlogcommits && !r)
749 if (!(ci = commitinfo_getbyoid(&id)))
751 /* diffstat: for stagit HTML required for the log.html line */
752 if (commitinfo_getstats(ci) == -1)
755 if (nlogcommits < 0) {
756 writelogline(fp, ci);
757 } else if (nlogcommits > 0) {
758 writelogline(fp, ci);
760 if (!nlogcommits && ci->parentoid[0])
761 fputs("<tr><td></td><td colspan=\"5\">"
762 "More commits remaining [...]</td>"
767 writelogline(wcachefp, ci);
769 /* check if file exists if so skip it */
772 fpfile = efopen(path, "w");
773 writeheader(fpfile, ci->summary);
774 fputs("<pre>", fpfile);
775 printshowfile(fpfile, ci);
776 fputs("</pre>\n", fpfile);
791 printcommitatom(FILE *fp, struct commitinfo *ci)
793 fputs("<entry>\n", fp);
795 fprintf(fp, "<id>%s</id>\n", ci->oid);
797 fputs("<published>", fp);
798 printtimez(fp, &(ci->author->when));
799 fputs("</published>\n", fp);
802 fputs("<updated>", fp);
803 printtimez(fp, &(ci->committer->when));
804 fputs("</updated>\n", fp);
807 fputs("<title type=\"text\">", fp);
808 xmlencode(fp, ci->summary, strlen(ci->summary));
809 fputs("</title>\n", fp);
811 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"%scommit/%s.html\" />\n",
815 fputs("<author>\n<name>", fp);
816 xmlencode(fp, ci->author->name, strlen(ci->author->name));
817 fputs("</name>\n<email>", fp);
818 xmlencode(fp, ci->author->email, strlen(ci->author->email));
819 fputs("</email>\n</author>\n", fp);
822 fputs("<content type=\"text\">", fp);
823 fprintf(fp, "commit %s\n", ci->oid);
824 if (ci->parentoid[0])
825 fprintf(fp, "parent %s\n", ci->parentoid);
827 fputs("Author: ", fp);
828 xmlencode(fp, ci->author->name, strlen(ci->author->name));
830 xmlencode(fp, ci->author->email, strlen(ci->author->email));
831 fputs(">\nDate: ", fp);
832 printtime(fp, &(ci->author->when));
837 xmlencode(fp, ci->msg, strlen(ci->msg));
839 fputs("\n</content>\n</entry>\n", fp);
845 struct commitinfo *ci;
846 git_revwalk *w = NULL;
848 size_t i, m = 100; /* last 'm' commits */
850 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
851 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
852 xmlencode(fp, strippedname, strlen(strippedname));
853 fputs(", branch HEAD</title>\n<subtitle>", fp);
854 xmlencode(fp, description, strlen(description));
855 fputs("</subtitle>\n", fp);
857 git_revwalk_new(&w, repo);
858 git_revwalk_push_head(w);
859 git_revwalk_simplify_first_parent(w);
861 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
862 if (!(ci = commitinfo_getbyoid(&id)))
864 printcommitatom(fp, ci);
869 fputs("</feed>\n", fp);
877 int value = var * 10 + .5;
882 convertbytes(int bytes)
884 bytes = (float)bytes;
885 static char outp[25];
886 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
887 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
888 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
893 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
895 char tmp[PATH_MAX] = "", *d;
899 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
900 errx(1, "path truncated: '%s'", fpath);
901 if (!(d = dirname(tmp)))
905 for (p = fpath, tmp[0] = '\0'; *p; p++) {
906 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
907 errx(1, "path truncated: '../%s'", tmp);
911 fp = efopen(fpath, "w");
912 writeheader(fp, filename);
914 xmlencode(fp, filename, strlen(filename));
915 fprintf(fp, " (%s)", convertbytes((int)filesize));
918 char newfpath[PATH_MAX];
919 char newfilename[PATH_MAX];
920 if (strcmp(get_ext(filename), "md") == 0) {
921 fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename);
922 strcpy(newfpath, fpath);
923 strcat(newfpath, "-raw");
925 strcpy(newfilename, filename);
926 strcat(newfilename, "-raw");
927 strcpy(oldfilename, filename);
930 writeblob(obj, newfpath, newfilename, filesize);
931 } else if (strcmp(get_ext(filename), "md-raw" ) == 0) {
932 fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename);
936 fputs(".</p><hr/>", fp);
938 if (git_blob_is_binary((git_blob *)obj)) {
939 fputs("<p>Binary file.</p>\n", fp);
941 writeblobhtml(filename, fp, (git_blob *)obj);
953 filemode(git_filemode_t m)
955 static char mode[11];
957 memset(mode, '-', sizeof(mode) - 1);
968 else if (S_ISFIFO(m))
972 else if (S_ISSOCK(m))
977 if (m & S_IRUSR) mode[1] = 'r';
978 if (m & S_IWUSR) mode[2] = 'w';
979 if (m & S_IXUSR) mode[3] = 'x';
980 if (m & S_IRGRP) mode[4] = 'r';
981 if (m & S_IWGRP) mode[5] = 'w';
982 if (m & S_IXGRP) mode[6] = 'x';
983 if (m & S_IROTH) mode[7] = 'r';
984 if (m & S_IWOTH) mode[8] = 'w';
985 if (m & S_IXOTH) mode[9] = 'x';
987 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
988 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
989 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
995 writefilestree(FILE *fp, git_tree *tree, const char *path)
997 const git_tree_entry *entry = NULL;
998 git_submodule *module = NULL;
999 git_object *obj = NULL;
1001 const char *entryname;
1002 char filepath[PATH_MAX], entrypath[PATH_MAX];
1006 count = git_tree_entrycount(tree);
1007 for (i = 0; i < count; i++) {
1008 if (!(entry = git_tree_entry_byindex(tree, i)) ||
1009 !(entryname = git_tree_entry_name(entry)))
1011 joinpath(entrypath, sizeof(entrypath), path, entryname);
1013 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
1015 if (r < 0 || (size_t)r >= sizeof(filepath))
1016 errx(1, "path truncated: 'file/%s.html'", entrypath);
1018 if (!git_tree_entry_to_object(&obj, repo, entry)) {
1019 switch (git_object_type(obj)) {
1023 /* NOTE: recurses */
1024 ret = writefilestree(fp, (git_tree *)obj,
1026 git_object_free(obj);
1031 git_object_free(obj);
1035 filesize = git_blob_rawsize((git_blob *)obj);
1036 writeblob(obj, filepath, entryname, filesize);
1038 fputs("<tr><td>", fp);
1039 fputs(filemode(git_tree_entry_filemode(entry)), fp);
1040 fprintf(fp, "</td><td><a href=\"%s", relpath);
1041 xmlencode(fp, filepath, strlen(filepath));
1043 xmlencode(fp, entrypath, strlen(entrypath));
1044 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
1045 fprintf(fp, "%s", convertbytes((int)filesize));
1046 fputs("</td></tr>\n", fp);
1047 git_object_free(obj);
1048 } else if (!git_submodule_lookup(&module, repo, entryname)) {
1049 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
1051 xmlencode(fp, entrypath, strlen(entrypath));
1052 git_submodule_free(module);
1053 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
1061 writefiles(FILE *fp, const git_oid *id)
1063 git_tree *tree = NULL;
1064 git_commit *commit = NULL;
1067 fputs("<table id=\"files\"><thead>\n<tr>"
1068 "<td><b>Mode</b></td><td><b>Name</b></td>"
1069 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
1070 "</tr>\n</thead><tbody>\n", fp);
1072 if (!git_commit_lookup(&commit, repo, id) &&
1073 !git_commit_tree(&tree, commit))
1074 ret = writefilestree(fp, tree, "");
1076 fputs("</tbody></table>", fp);
1078 git_commit_free(commit);
1079 git_tree_free(tree);
1085 refs_cmp(const void *v1, const void *v2)
1087 git_reference *r1 = (*(git_reference **)v1);
1088 git_reference *r2 = (*(git_reference **)v2);
1091 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1094 return strcmp(git_reference_shorthand(r1),
1095 git_reference_shorthand(r2));
1101 struct commitinfo *ci;
1102 const git_oid *id = NULL;
1103 git_object *obj = NULL;
1104 git_reference *dref = NULL, *r, *ref = NULL;
1105 git_reference_iterator *it = NULL;
1106 git_reference **refs = NULL;
1107 size_t count, i, j, refcount;
1108 const char *titles[] = { "Branches", "Tags" };
1109 const char *ids[] = { "branches", "tags" };
1112 if (git_reference_iterator_new(&it, repo))
1115 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1116 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1118 refs[refcount] = ref;
1120 git_reference_iterator_free(it);
1122 /* sort by type then shorthand name */
1123 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1125 for (j = 0; j < 2; j++) {
1126 for (i = 0, count = 0; i < refcount; i++) {
1127 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1128 !(git_reference_is_tag(refs[i]) && j == 1))
1131 switch (git_reference_type(refs[i])) {
1132 case GIT_REF_SYMBOLIC:
1133 if (git_reference_resolve(&dref, refs[i]))
1143 if (!git_reference_target(r) ||
1144 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1146 if (!(id = git_object_id(obj)))
1148 if (!(ci = commitinfo_getbyoid(id)))
1151 /* print header if it has an entry (first). */
1153 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1154 "<thead>\n<tr><td><b>Name</b></td>"
1155 "<td><b>Last commit date</b></td>"
1156 "<td><b>Author</b></td>\n</tr>\n"
1157 "</thead><tbody>\n",
1162 name = git_reference_shorthand(r);
1164 fputs("<tr><td>", fp);
1165 xmlencode(fp, name, strlen(name));
1166 fputs("</td><td>", fp);
1168 printtimeshort(fp, &(ci->author->when));
1169 fputs("</td><td>", fp);
1171 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1172 fputs("</td></tr>\n", fp);
1176 commitinfo_free(ci);
1177 git_object_free(obj);
1179 git_reference_free(dref);
1184 fputs("</tbody></table><br/>", fp);
1188 git_object_free(obj);
1189 git_reference_free(dref);
1191 for (i = 0; i < refcount; i++)
1192 git_reference_free(refs[i]);
1201 fprintf(stderr, "%s [-c cachefile | -l commits] "
1202 "[-u baseurl] repodir\n", argv0);
1207 main(int argc, char *argv[])
1209 git_object *obj = NULL;
1210 const git_oid *head = NULL;
1213 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1214 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1218 for (i = 1; i < argc; i++) {
1219 if (argv[i][0] != '-') {
1223 } else if (argv[i][1] == 'c') {
1224 if (nlogcommits > 0 || i + 1 >= argc)
1226 cachefile = argv[++i];
1227 } else if (argv[i][1] == 'l') {
1228 if (cachefile || i + 1 >= argc)
1231 nlogcommits = strtoll(argv[++i], &p, 10);
1232 if (argv[i][0] == '\0' || *p != '\0' ||
1233 nlogcommits <= 0 || errno)
1235 } else if (argv[i][1] == 'u') {
1238 baseurl = argv[++i];
1244 if (!realpath(repodir, repodirabs))
1250 if (unveil(repodir, "r") == -1)
1251 err(1, "unveil: %s", repodir);
1252 if (unveil(".", "rwc") == -1)
1253 err(1, "unveil: .");
1254 if (cachefile && unveil(cachefile, "rwc") == -1)
1255 err(1, "unveil: %s", cachefile);
1258 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1261 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1266 if (git_repository_open_ext(&repo, repodir,
1267 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1268 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1273 if (!git_revparse_single(&obj, repo, "HEAD"))
1274 head = git_object_id(obj);
1275 git_object_free(obj);
1277 /* use directory name as name */
1278 if ((name = strrchr(repodirabs, '/')))
1285 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1286 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1287 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1288 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1290 /* strip .git suffix */
1291 if (!(strippedname = strdup(name)))
1293 if ((p = strrchr(strippedname, '.')))
1294 if (!strcmp(p, ".git"))
1297 /* read description or .git/description */
1298 joinpath(path, sizeof(path), repodir, "description");
1299 if (!(fpread = fopen(path, "r"))) {
1300 joinpath(path, sizeof(path), repodir, ".git/description");
1301 fpread = fopen(path, "r");
1304 if (!fgets(description, sizeof(description), fpread))
1305 description[0] = '\0';
1309 /* read url or .git/url */
1310 joinpath(path, sizeof(path), repodir, "url");
1311 if (!(fpread = fopen(path, "r"))) {
1312 joinpath(path, sizeof(path), repodir, ".git/url");
1313 fpread = fopen(path, "r");
1316 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1318 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1323 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1324 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1325 git_object_type(obj) == GIT_OBJ_BLOB)
1326 license = licensefiles[i] + strlen("HEAD:");
1327 git_object_free(obj);
1331 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1332 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1333 git_object_type(obj) == GIT_OBJ_BLOB)
1334 readme = readmefiles[i] + strlen("HEAD:");
1335 git_object_free(obj);
1338 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1339 git_object_type(obj) == GIT_OBJ_BLOB)
1340 submodules = ".gitmodules";
1341 git_object_free(obj);
1343 /* Generate tarball */
1345 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1346 strippedname, repodir);
1350 fp = efopen("log.html", "w");
1352 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1353 writeheader(fp, "Log");
1354 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1355 "<td><b>Commit</b></td>"
1356 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1357 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1358 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1360 if (cachefile && head) {
1361 /* read from cache file (does not need to exist) */
1362 if ((rcachefp = fopen(cachefile, "r"))) {
1363 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1364 errx(1, "%s: no object id", cachefile);
1365 if (git_oid_fromstr(&lastoid, lastoidstr))
1366 errx(1, "%s: invalid object id", cachefile);
1369 /* write log to (temporary) cache */
1370 if ((fd = mkstemp(tmppath)) == -1)
1372 if (!(wcachefp = fdopen(fd, "w")))
1373 err(1, "fdopen: '%s'", tmppath);
1374 /* write last commit id (HEAD) */
1375 git_oid_tostr(buf, sizeof(buf), head);
1376 fprintf(wcachefp, "%s\n", buf);
1381 /* append previous log to log.html and the new cache */
1382 while (!feof(rcachefp)) {
1383 n = fread(buf, 1, sizeof(buf), rcachefp);
1384 if (ferror(rcachefp))
1386 if (fwrite(buf, 1, n, fp) != n ||
1387 fwrite(buf, 1, n, wcachefp) != n)
1398 fputs("</tbody></table>", fp);
1402 /* files for HEAD */
1403 fp = efopen("files.html", "w");
1404 writeheader(fp, "Files");
1406 writefiles(fp, head);
1410 cp("files.html", "index.html");
1412 /* summary page with branches and tags */
1413 fp = efopen("refs.html", "w");
1414 writeheader(fp, "Refs");
1420 fp = efopen("atom.xml", "w");
1424 /* rename new cache file on success */
1425 if (cachefile && head) {
1426 if (rename(tmppath, cachefile))
1427 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1428 umask((mask = umask(0)));
1429 if (chmod(cachefile,
1430 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1431 err(1, "chmod: '%s'", cachefile);
1435 git_repository_free(repo);
1436 git_libgit2_shutdown();