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);
808 float value = (int)(var * 10 + .5);
809 return (float)value / 10;
813 convertbytes(int bytes)
816 static char outp[255] = "hi";
817 if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B");
818 else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K");
819 else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M");
824 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
826 char tmp[PATH_MAX] = "", *d;
831 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
832 errx(1, "path truncated: '%s'", fpath);
833 if (!(d = dirname(tmp)))
838 for (p = fpath, tmp[0] = '\0'; *p; p++) {
839 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
840 errx(1, "path truncated: '../%s'", tmp);
844 fp = efopen(fpath, "w");
845 writeheader(fp, filename);
847 xmlencode(fp, filename, strlen(filename));
848 fprintf(fp, " (%s)", convertbytes((int)filesize));
850 fputs("</p><hr/>", fp);
852 if (git_blob_is_binary((git_blob *)obj)) {
853 fputs("<p>Binary file.</p>\n", fp);
855 lc = writeblobhtml(filename, fp, (git_blob *)obj);
869 filemode(git_filemode_t m)
871 static char mode[11];
873 memset(mode, '-', sizeof(mode) - 1);
884 else if (S_ISFIFO(m))
888 else if (S_ISSOCK(m))
893 if (m & S_IRUSR) mode[1] = 'r';
894 if (m & S_IWUSR) mode[2] = 'w';
895 if (m & S_IXUSR) mode[3] = 'x';
896 if (m & S_IRGRP) mode[4] = 'r';
897 if (m & S_IWGRP) mode[5] = 'w';
898 if (m & S_IXGRP) mode[6] = 'x';
899 if (m & S_IROTH) mode[7] = 'r';
900 if (m & S_IWOTH) mode[8] = 'w';
901 if (m & S_IXOTH) mode[9] = 'x';
903 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
904 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
905 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
911 writefilestree(FILE *fp, git_tree *tree, const char *path)
913 const git_tree_entry *entry = NULL;
914 git_submodule *module = NULL;
915 git_object *obj = NULL;
917 const char *entryname;
918 char filepath[PATH_MAX], entrypath[PATH_MAX];
922 count = git_tree_entrycount(tree);
923 for (i = 0; i < count; i++) {
924 if (!(entry = git_tree_entry_byindex(tree, i)) ||
925 !(entryname = git_tree_entry_name(entry)))
927 joinpath(entrypath, sizeof(entrypath), path, entryname);
929 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
931 if (r < 0 || (size_t)r >= sizeof(filepath))
932 errx(1, "path truncated: 'file/%s.html'", entrypath);
934 if (!git_tree_entry_to_object(&obj, repo, entry)) {
935 switch (git_object_type(obj)) {
940 ret = writefilestree(fp, (git_tree *)obj,
942 git_object_free(obj);
947 git_object_free(obj);
951 filesize = git_blob_rawsize((git_blob *)obj);
952 lc = writeblob(obj, filepath, entryname, filesize);
954 fputs("<tr><td>", fp);
955 fputs(filemode(git_tree_entry_filemode(entry)), fp);
956 fprintf(fp, "</td><td><a href=\"%s", relpath);
957 xmlencode(fp, filepath, strlen(filepath));
959 xmlencode(fp, entrypath, strlen(entrypath));
960 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
962 fprintf(fp, "%dL", lc);
964 fprintf(fp, "%s", convertbytes((int)filesize));
965 fputs("</td></tr>\n", fp);
966 git_object_free(obj);
967 } else if (!git_submodule_lookup(&module, repo, entryname)) {
968 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
970 xmlencode(fp, entrypath, strlen(entrypath));
971 git_submodule_free(module);
972 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
980 writefiles(FILE *fp, const git_oid *id)
982 git_tree *tree = NULL;
983 git_commit *commit = NULL;
986 fputs("<table id=\"files\"><thead>\n<tr>"
987 "<td><b>Mode</b></td><td><b>Name</b></td>"
988 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
989 "</tr>\n</thead><tbody>\n", fp);
991 if (!git_commit_lookup(&commit, repo, id) &&
992 !git_commit_tree(&tree, commit))
993 ret = writefilestree(fp, tree, "");
995 fputs("</tbody></table>", fp);
997 git_commit_free(commit);
1004 refs_cmp(const void *v1, const void *v2)
1006 git_reference *r1 = (*(git_reference **)v1);
1007 git_reference *r2 = (*(git_reference **)v2);
1010 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
1013 return strcmp(git_reference_shorthand(r1),
1014 git_reference_shorthand(r2));
1020 struct commitinfo *ci;
1021 const git_oid *id = NULL;
1022 git_object *obj = NULL;
1023 git_reference *dref = NULL, *r, *ref = NULL;
1024 git_reference_iterator *it = NULL;
1025 git_reference **refs = NULL;
1026 size_t count, i, j, refcount;
1027 const char *titles[] = { "Branches", "Tags" };
1028 const char *ids[] = { "branches", "tags" };
1031 if (git_reference_iterator_new(&it, repo))
1034 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
1035 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
1037 refs[refcount] = ref;
1039 git_reference_iterator_free(it);
1041 /* sort by type then shorthand name */
1042 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1044 for (j = 0; j < 2; j++) {
1045 for (i = 0, count = 0; i < refcount; i++) {
1046 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1047 !(git_reference_is_tag(refs[i]) && j == 1))
1050 switch (git_reference_type(refs[i])) {
1051 case GIT_REF_SYMBOLIC:
1052 if (git_reference_resolve(&dref, refs[i]))
1062 if (!git_reference_target(r) ||
1063 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1065 if (!(id = git_object_id(obj)))
1067 if (!(ci = commitinfo_getbyoid(id)))
1070 /* print header if it has an entry (first). */
1072 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1073 "<thead>\n<tr><td><b>Name</b></td>"
1074 "<td><b>Last commit date</b></td>"
1075 "<td><b>Author</b></td>\n</tr>\n"
1076 "</thead><tbody>\n",
1081 name = git_reference_shorthand(r);
1083 fputs("<tr><td>", fp);
1084 xmlencode(fp, name, strlen(name));
1085 fputs("</td><td>", fp);
1087 printtimeshort(fp, &(ci->author->when));
1088 fputs("</td><td>", fp);
1090 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1091 fputs("</td></tr>\n", fp);
1095 commitinfo_free(ci);
1096 git_object_free(obj);
1098 git_reference_free(dref);
1103 fputs("</tbody></table><br/>", fp);
1107 git_object_free(obj);
1108 git_reference_free(dref);
1110 for (i = 0; i < refcount; i++)
1111 git_reference_free(refs[i]);
1120 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1125 main(int argc, char *argv[])
1127 git_object *obj = NULL;
1128 const git_oid *head = NULL;
1131 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1132 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1136 for (i = 1; i < argc; i++) {
1137 if (argv[i][0] != '-') {
1141 } else if (argv[i][1] == 'c') {
1142 if (nlogcommits > 0 || i + 1 >= argc)
1144 cachefile = argv[++i];
1145 } else if (argv[i][1] == 'l') {
1146 if (cachefile || i + 1 >= argc)
1149 nlogcommits = strtoll(argv[++i], &p, 10);
1150 if (argv[i][0] == '\0' || *p != '\0' ||
1151 nlogcommits <= 0 || errno)
1158 if (!realpath(repodir, repodirabs))
1164 if (unveil(repodir, "r") == -1)
1165 err(1, "unveil: %s", repodir);
1166 if (unveil(".", "rwc") == -1)
1167 err(1, "unveil: .");
1168 if (cachefile && unveil(cachefile, "rwc") == -1)
1169 err(1, "unveil: %s", cachefile);
1172 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1175 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1180 if (git_repository_open_ext(&repo, repodir,
1181 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1182 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1187 if (!git_revparse_single(&obj, repo, "HEAD"))
1188 head = git_object_id(obj);
1189 git_object_free(obj);
1191 /* use directory name as name */
1192 if ((name = strrchr(repodirabs, '/')))
1199 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1200 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1201 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1202 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1204 /* strip .git suffix */
1205 if (!(strippedname = strdup(name)))
1207 if ((p = strrchr(strippedname, '.')))
1208 if (!strcmp(p, ".git"))
1211 /* read description or .git/description */
1212 joinpath(path, sizeof(path), repodir, "description");
1213 if (!(fpread = fopen(path, "r"))) {
1214 joinpath(path, sizeof(path), repodir, ".git/description");
1215 fpread = fopen(path, "r");
1218 if (!fgets(description, sizeof(description), fpread))
1219 description[0] = '\0';
1223 /* read url or .git/url */
1224 joinpath(path, sizeof(path), repodir, "url");
1225 if (!(fpread = fopen(path, "r"))) {
1226 joinpath(path, sizeof(path), repodir, ".git/url");
1227 fpread = fopen(path, "r");
1230 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1232 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1237 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1238 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1239 git_object_type(obj) == GIT_OBJ_BLOB)
1240 license = licensefiles[i] + strlen("HEAD:");
1241 git_object_free(obj);
1245 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1246 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1247 git_object_type(obj) == GIT_OBJ_BLOB)
1248 readme = readmefiles[i] + strlen("HEAD:");
1249 git_object_free(obj);
1252 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1253 git_object_type(obj) == GIT_OBJ_BLOB)
1254 submodules = ".gitmodules";
1255 git_object_free(obj);
1257 /* Generate tarball */
1259 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1260 strippedname, repodir);
1264 fp = efopen("log.html", "w");
1266 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1267 writeheader(fp, "Log");
1268 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1269 "<td><b>Commit</b></td>"
1270 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1271 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1272 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1274 if (cachefile && head) {
1275 /* read from cache file (does not need to exist) */
1276 if ((rcachefp = fopen(cachefile, "r"))) {
1277 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1278 errx(1, "%s: no object id", cachefile);
1279 if (git_oid_fromstr(&lastoid, lastoidstr))
1280 errx(1, "%s: invalid object id", cachefile);
1283 /* write log to (temporary) cache */
1284 if ((fd = mkstemp(tmppath)) == -1)
1286 if (!(wcachefp = fdopen(fd, "w")))
1287 err(1, "fdopen: '%s'", tmppath);
1288 /* write last commit id (HEAD) */
1289 git_oid_tostr(buf, sizeof(buf), head);
1290 fprintf(wcachefp, "%s\n", buf);
1295 /* append previous log to log.html and the new cache */
1296 while (!feof(rcachefp)) {
1297 n = fread(buf, 1, sizeof(buf), rcachefp);
1298 if (ferror(rcachefp))
1300 if (fwrite(buf, 1, n, fp) != n ||
1301 fwrite(buf, 1, n, wcachefp) != n)
1312 fputs("</tbody></table>", fp);
1316 /* files for HEAD */
1317 fp = efopen("files.html", "w");
1318 writeheader(fp, "Files");
1320 writefiles(fp, head);
1324 cp("files.html", "index.html");
1326 /* summary page with branches and tags */
1327 fp = efopen("refs.html", "w");
1328 writeheader(fp, "Refs");
1334 fp = efopen("atom.xml", "w");
1338 /* rename new cache file on success */
1339 if (cachefile && head) {
1340 if (rename(tmppath, cachefile))
1341 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1342 umask((mask = umask(0)));
1343 if (chmod(cachefile,
1344 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1345 err(1, "chmod: '%s'", cachefile);
1349 git_repository_free(repo);
1350 git_libgit2_shutdown();