30 char oid[GIT_OID_HEXSZ + 1];
31 char parentoid[GIT_OID_HEXSZ + 1];
33 const git_signature *author;
34 const git_signature *committer;
41 git_tree *commit_tree;
42 git_tree *parent_tree;
48 struct deltainfo **deltas;
52 static git_repository *repo;
54 static const char *relpath = "";
55 static const char *repodir;
57 static char *name = "";
58 static char *strippedname = "";
59 static char description[255];
60 static char cloneurl[1024];
61 static char *submodules;
62 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
64 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
66 static long long nlogcommits = -1; /* < 0 indicates not used */
69 static git_oid lastoid;
70 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
71 static FILE *rcachefp, *wcachefp;
72 static const char *cachefile;
75 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
79 r = snprintf(buf, bufsiz, "%s%s%s",
80 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
81 if (r < 0 || (size_t)r >= bufsiz)
82 errx(1, "path truncated: '%s%s%s'",
83 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
87 deltainfo_free(struct deltainfo *di)
91 git_patch_free(di->patch);
92 memset(di, 0, sizeof(*di));
97 commitinfo_getstats(struct commitinfo *ci)
100 git_diff_options opts;
101 git_diff_find_options fopts;
102 const git_diff_delta *delta;
103 const git_diff_hunk *hunk;
104 const git_diff_line *line;
105 git_patch *patch = NULL;
106 size_t ndeltas, nhunks, nhunklines;
109 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
111 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
112 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
114 ci->parent_tree = NULL;
118 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
119 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
120 GIT_DIFF_IGNORE_SUBMODULES |
121 GIT_DIFF_INCLUDE_TYPECHANGE;
122 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
125 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
127 /* find renames and copies, exact matches (no heuristic) for renames. */
128 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES |
129 GIT_DIFF_FIND_EXACT_MATCH_ONLY;
130 if (git_diff_find_similar(ci->diff, &fopts))
133 ndeltas = git_diff_num_deltas(ci->diff);
134 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
137 for (i = 0; i < ndeltas; i++) {
138 if (git_patch_from_diff(&patch, ci->diff, i))
141 if (!(di = calloc(1, sizeof(struct deltainfo))))
146 delta = git_patch_get_delta(patch);
148 /* skip stats for binary data */
149 if (delta->flags & GIT_DIFF_FLAG_BINARY)
152 nhunks = git_patch_num_hunks(patch);
153 for (j = 0; j < nhunks; j++) {
154 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
157 if (git_patch_get_line_in_hunk(&line, patch, j, k))
159 if (line->old_lineno == -1) {
162 } else if (line->new_lineno == -1) {
175 git_diff_free(ci->diff);
177 git_tree_free(ci->commit_tree);
178 ci->commit_tree = NULL;
179 git_tree_free(ci->parent_tree);
180 ci->parent_tree = NULL;
181 git_commit_free(ci->parent);
185 for (i = 0; i < ci->ndeltas; i++)
186 deltainfo_free(ci->deltas[i]);
198 commitinfo_free(struct commitinfo *ci)
205 for (i = 0; i < ci->ndeltas; i++)
206 deltainfo_free(ci->deltas[i]);
209 git_diff_free(ci->diff);
210 git_tree_free(ci->commit_tree);
211 git_tree_free(ci->parent_tree);
212 git_commit_free(ci->commit);
213 git_commit_free(ci->parent);
214 memset(ci, 0, sizeof(*ci));
219 commitinfo_getbyoid(const git_oid *id)
221 struct commitinfo *ci;
223 if (!(ci = calloc(1, sizeof(struct commitinfo))))
226 if (git_commit_lookup(&(ci->commit), repo, id))
230 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
231 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
233 ci->author = git_commit_author(ci->commit);
234 ci->committer = git_commit_committer(ci->commit);
235 ci->summary = git_commit_summary(ci->commit);
236 ci->msg = git_commit_message(ci->commit);
247 efopen(const char *name, const char *flags)
251 if (!(fp = fopen(name, flags)))
252 err(1, "fopen: '%s'", name);
257 /* Escape characters below as HTML 2.0 / XML 1.0. */
259 xmlencode(FILE *fp, const char *s, size_t len)
263 for (i = 0; *s && i < len; s++, i++) {
265 case '<': fputs("<", fp); break;
266 case '>': fputs(">", fp); break;
267 case '\'': fputs("'", fp); break;
268 case '&': fputs("&", fp); break;
269 case '"': fputs(""", fp); break;
270 default: fputc(*s, fp);
276 mkdirp(const char *path)
278 char tmp[PATH_MAX], *p;
280 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
281 errx(1, "path truncated: '%s'", path);
282 for (p = tmp + (tmp[0] == '/'); *p; p++) {
286 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
290 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
296 printtimez(FILE *fp, const git_time *intime)
302 t = (time_t)intime->time;
303 if (!(intm = gmtime(&t)))
305 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
310 printtime(FILE *fp, const git_time *intime)
316 t = (time_t)intime->time + (intime->offset * 60);
317 if (!(intm = gmtime(&t)))
319 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
320 if (intime->offset < 0)
321 fprintf(fp, "%s -%02d%02d", out,
322 -(intime->offset) / 60, -(intime->offset) % 60);
324 fprintf(fp, "%s +%02d%02d", out,
325 intime->offset / 60, intime->offset % 60);
329 printtimeshort(FILE *fp, const git_time *intime)
335 t = (time_t)intime->time;
336 if (!(intm = gmtime(&t)))
338 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
343 writeheader(FILE *fp, const char *title)
345 fputs("<!DOCTYPE html>\n"
347 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
348 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
350 xmlencode(fp, title, strlen(title));
351 if (title[0] && strippedname[0])
353 xmlencode(fp, strippedname, strlen(strippedname));
356 xmlencode(fp, description, strlen(description));
357 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
358 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
360 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
361 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
362 fputs("</head>\n<body>\n<table><tr><td>", fp);
363 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
365 fputs("</td><td><h1>", fp);
366 xmlencode(fp, strippedname, strlen(strippedname));
367 fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
368 xmlencode(fp, description, strlen(description));
369 fputs("</span></td></tr>", fp);
371 fputs("<tr class=\"url\"><td></td><td><span class=\"clone\">git clone <a href=\"", fp);
372 xmlencode(fp, cloneurl, strlen(cloneurl));
374 xmlencode(fp, cloneurl, strlen(cloneurl));
375 fputs("</a></span></td></tr>", fp);
377 fputs("<tr><td></td><td>\n", fp);
378 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
379 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
380 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
382 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
383 relpath, submodules);
385 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
388 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
390 fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
391 relpath, strippedname);
392 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
396 writefooter(FILE *fp)
398 fputs("</div>\n</body>\n</html>\n", fp);
402 call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
407 int stdout_copy = dup(1);
411 char cmd[] = "chroma --html --html-only --html-lines --html-lines-table --filename ";
412 strcat(cmd, filename);
414 FILE *child = popen(cmd, "w");
416 printf("child is null: %s", strerror(errno));
419 // Give filename through STDIN:
420 fprintf(child, "%s\n", filename);
421 // Give code to highlight through STDIN:
424 for (i = 0; *s && i < len; s++, i++) {
425 if (*s == '\n') lc++;
426 fprintf(child, "%c", *s);
432 dup2(stdout_copy, 1);
437 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
440 const char *s = git_blob_rawcontent(blob);
441 git_off_t len = git_blob_rawsize(blob);
444 lc = call_chroma(filename, fp, s, len);
451 printcommit(FILE *fp, struct commitinfo *ci)
453 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
454 relpath, ci->oid, ci->oid);
456 if (ci->parentoid[0])
457 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
458 relpath, ci->parentoid, ci->parentoid);
461 fputs("<b>Author:</b> ", fp);
462 xmlencode(fp, ci->author->name, strlen(ci->author->name));
463 fputs(" <<a href=\"mailto:", fp);
464 xmlencode(fp, ci->author->email, strlen(ci->author->email));
466 xmlencode(fp, ci->author->email, strlen(ci->author->email));
467 fputs("</a>>\n<b>Date:</b> ", fp);
468 printtime(fp, &(ci->author->when));
473 xmlencode(fp, ci->msg, strlen(ci->msg));
479 printshowfile(FILE *fp, struct commitinfo *ci)
481 const git_diff_delta *delta;
482 const git_diff_hunk *hunk;
483 const git_diff_line *line;
485 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
494 if (ci->filecount > 1000 ||
495 ci->ndeltas > 1000 ||
496 ci->addcount > 100000 ||
497 ci->delcount > 100000) {
498 fputs("Diff is too large, output suppressed.\n", fp);
503 fputs("<b>Diffstat:</b>\n<table>", fp);
504 for (i = 0; i < ci->ndeltas; i++) {
505 delta = git_patch_get_delta(ci->deltas[i]->patch);
507 switch (delta->status) {
508 case GIT_DELTA_ADDED: c = 'A'; break;
509 case GIT_DELTA_COPIED: c = 'C'; break;
510 case GIT_DELTA_DELETED: c = 'D'; break;
511 case GIT_DELTA_MODIFIED: c = 'M'; break;
512 case GIT_DELTA_RENAMED: c = 'R'; break;
513 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
514 default: c = ' '; break;
517 fprintf(fp, "<tr><td>%c", c);
519 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
521 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
522 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
523 if (strcmp(delta->old_file.path, delta->new_file.path)) {
524 fputs(" -> ", fp);
525 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
528 add = ci->deltas[i]->addcount;
529 del = ci->deltas[i]->delcount;
531 total = sizeof(linestr) - 2;
532 if (changed > total) {
534 add = ((float)total / changed * add) + 1;
536 del = ((float)total / changed * del) + 1;
538 memset(&linestr, '+', add);
539 memset(&linestr[add], '-', del);
541 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
542 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
543 fwrite(&linestr, 1, add, fp);
544 fputs("</span><span class=\"d\">", fp);
545 fwrite(&linestr[add], 1, del, fp);
546 fputs("</span></td></tr>\n", fp);
548 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
549 ci->filecount, ci->filecount == 1 ? "" : "s",
550 ci->addcount, ci->addcount == 1 ? "" : "s",
551 ci->delcount, ci->delcount == 1 ? "" : "s");
555 for (i = 0; i < ci->ndeltas; i++) {
556 patch = ci->deltas[i]->patch;
557 delta = git_patch_get_delta(patch);
558 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
559 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
560 fputs(".html\">", fp);
561 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
562 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
563 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
564 fprintf(fp, ".html\">");
565 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
566 fprintf(fp, "</a></b>\n");
568 /* check binary data */
569 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
570 fputs("Binary files differ.\n", fp);
574 nhunks = git_patch_num_hunks(patch);
575 for (j = 0; j < nhunks; j++) {
576 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
579 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
580 xmlencode(fp, hunk->header, hunk->header_len);
584 if (git_patch_get_line_in_hunk(&line, patch, j, k))
586 if (line->old_lineno == -1)
587 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
589 else if (line->new_lineno == -1)
590 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
594 xmlencode(fp, line->content, line->content_len);
595 if (line->old_lineno == -1 || line->new_lineno == -1)
603 writelogline(FILE *fp, struct commitinfo *ci)
605 fputs("<tr><td>", fp);
607 printtimeshort(fp, &(ci->author->when));
608 fputs("</td><td>", fp);
610 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
611 xmlencode(fp, ci->summary, strlen(ci->summary));
614 fputs("</td><td>", fp);
616 xmlencode(fp, ci->author->name, strlen(ci->author->name));
617 fputs("</td><td class=\"num\" align=\"right\">", fp);
618 fprintf(fp, "%zu", ci->filecount);
619 fputs("</td><td class=\"num\" align=\"right\">", fp);
620 fprintf(fp, "+%zu", ci->addcount);
621 fputs("</td><td class=\"num\" align=\"right\">", fp);
622 fprintf(fp, "-%zu", ci->delcount);
623 fputs("</td></tr>\n", fp);
627 writelog(FILE *fp, const git_oid *oid)
629 struct commitinfo *ci;
630 git_revwalk *w = NULL;
632 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
636 git_revwalk_new(&w, repo);
637 git_revwalk_push(w, oid);
638 git_revwalk_simplify_first_parent(w);
640 while (!git_revwalk_next(&id, w)) {
643 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
646 git_oid_tostr(oidstr, sizeof(oidstr), &id);
647 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
648 if (r < 0 || (size_t)r >= sizeof(path))
649 errx(1, "path truncated: 'commit/%s.html'", oidstr);
650 r = access(path, F_OK);
652 /* optimization: if there are no log lines to write and
653 the commit file already exists: skip the diffstat */
654 if (!nlogcommits && !r)
657 if (!(ci = commitinfo_getbyoid(&id)))
659 /* diffstat: for stagit HTML required for the log.html line */
660 if (commitinfo_getstats(ci) == -1)
663 if (nlogcommits < 0) {
664 writelogline(fp, ci);
665 } else if (nlogcommits > 0) {
666 writelogline(fp, ci);
668 if (!nlogcommits && ci->parentoid[0])
669 fputs("<tr><td></td><td colspan=\"5\">"
670 "More commits remaining [...]</td>"
675 writelogline(wcachefp, ci);
677 /* check if file exists if so skip it */
680 fpfile = efopen(path, "w");
681 writeheader(fpfile, ci->summary);
682 fputs("<pre>", fpfile);
683 printshowfile(fpfile, ci);
684 fputs("</pre>\n", fpfile);
699 printcommitatom(FILE *fp, struct commitinfo *ci)
701 fputs("<entry>\n", fp);
703 fprintf(fp, "<id>%s</id>\n", ci->oid);
705 fputs("<published>", fp);
706 printtimez(fp, &(ci->author->when));
707 fputs("</published>\n", fp);
710 fputs("<updated>", fp);
711 printtimez(fp, &(ci->committer->when));
712 fputs("</updated>\n", fp);
715 fputs("<title type=\"text\">", fp);
716 xmlencode(fp, ci->summary, strlen(ci->summary));
717 fputs("</title>\n", fp);
719 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
723 fputs("<author>\n<name>", fp);
724 xmlencode(fp, ci->author->name, strlen(ci->author->name));
725 fputs("</name>\n<email>", fp);
726 xmlencode(fp, ci->author->email, strlen(ci->author->email));
727 fputs("</email>\n</author>\n", fp);
730 fputs("<content type=\"text\">", fp);
731 fprintf(fp, "commit %s\n", ci->oid);
732 if (ci->parentoid[0])
733 fprintf(fp, "parent %s\n", ci->parentoid);
735 fputs("Author: ", fp);
736 xmlencode(fp, ci->author->name, strlen(ci->author->name));
738 xmlencode(fp, ci->author->email, strlen(ci->author->email));
739 fputs(">\nDate: ", fp);
740 printtime(fp, &(ci->author->when));
745 xmlencode(fp, ci->msg, strlen(ci->msg));
747 fputs("\n</content>\n</entry>\n", fp);
753 struct commitinfo *ci;
754 git_revwalk *w = NULL;
756 size_t i, m = 100; /* last 'm' commits */
758 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
759 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
760 xmlencode(fp, strippedname, strlen(strippedname));
761 fputs(", branch HEAD</title>\n<subtitle>", fp);
762 xmlencode(fp, description, strlen(description));
763 fputs("</subtitle>\n", fp);
765 git_revwalk_new(&w, repo);
766 git_revwalk_push_head(w);
767 git_revwalk_simplify_first_parent(w);
769 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
770 if (!(ci = commitinfo_getbyoid(&id)))
772 printcommitatom(fp, ci);
777 fputs("</feed>\n", fp);
783 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
785 char tmp[PATH_MAX] = "", *d;
790 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
791 errx(1, "path truncated: '%s'", fpath);
792 if (!(d = dirname(tmp)))
797 for (p = fpath, tmp[0] = '\0'; *p; p++) {
798 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
799 errx(1, "path truncated: '../%s'", tmp);
803 fp = efopen(fpath, "w");
804 writeheader(fp, filename);
806 xmlencode(fp, filename, strlen(filename));
807 fprintf(fp, " (%juB)", (uintmax_t)filesize);
808 fputs("</p><hr/>", fp);
810 if (git_blob_is_binary((git_blob *)obj)) {
811 fputs("<p>Binary file.</p>\n", fp);
813 lc = writeblobhtml(filename, fp, (git_blob *)obj);
826 filemode(git_filemode_t m)
828 static char mode[11];
830 memset(mode, '-', sizeof(mode) - 1);
841 else if (S_ISFIFO(m))
845 else if (S_ISSOCK(m))
850 if (m & S_IRUSR) mode[1] = 'r';
851 if (m & S_IWUSR) mode[2] = 'w';
852 if (m & S_IXUSR) mode[3] = 'x';
853 if (m & S_IRGRP) mode[4] = 'r';
854 if (m & S_IWGRP) mode[5] = 'w';
855 if (m & S_IXGRP) mode[6] = 'x';
856 if (m & S_IROTH) mode[7] = 'r';
857 if (m & S_IWOTH) mode[8] = 'w';
858 if (m & S_IXOTH) mode[9] = 'x';
860 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
861 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
862 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
868 writefilestree(FILE *fp, git_tree *tree, const char *path)
870 const git_tree_entry *entry = NULL;
871 git_submodule *module = NULL;
872 git_object *obj = NULL;
874 const char *entryname;
875 char filepath[PATH_MAX], entrypath[PATH_MAX];
879 count = git_tree_entrycount(tree);
880 for (i = 0; i < count; i++) {
881 if (!(entry = git_tree_entry_byindex(tree, i)) ||
882 !(entryname = git_tree_entry_name(entry)))
884 joinpath(entrypath, sizeof(entrypath), path, entryname);
886 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
888 if (r < 0 || (size_t)r >= sizeof(filepath))
889 errx(1, "path truncated: 'file/%s.html'", entrypath);
891 if (!git_tree_entry_to_object(&obj, repo, entry)) {
892 switch (git_object_type(obj)) {
897 ret = writefilestree(fp, (git_tree *)obj,
899 git_object_free(obj);
904 git_object_free(obj);
908 filesize = git_blob_rawsize((git_blob *)obj);
909 lc = writeblob(obj, filepath, entryname, filesize);
911 fputs("<tr><td>", fp);
912 fputs(filemode(git_tree_entry_filemode(entry)), fp);
913 fprintf(fp, "</td><td><a href=\"%s", relpath);
914 xmlencode(fp, filepath, strlen(filepath));
916 xmlencode(fp, entrypath, strlen(entrypath));
917 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
919 fprintf(fp, "%dL", lc);
921 fprintf(fp, "%juB", (uintmax_t)filesize);
922 fputs("</td></tr>\n", fp);
923 git_object_free(obj);
924 } else if (!git_submodule_lookup(&module, repo, entryname)) {
925 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
927 xmlencode(fp, entrypath, strlen(entrypath));
928 git_submodule_free(module);
929 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
937 writefiles(FILE *fp, const git_oid *id)
939 git_tree *tree = NULL;
940 git_commit *commit = NULL;
943 fputs("<table id=\"files\"><thead>\n<tr>"
944 "<td><b>Mode</b></td><td><b>Name</b></td>"
945 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
946 "</tr>\n</thead><tbody>\n", fp);
948 if (!git_commit_lookup(&commit, repo, id) &&
949 !git_commit_tree(&tree, commit))
950 ret = writefilestree(fp, tree, "");
952 fputs("</tbody></table>", fp);
954 git_commit_free(commit);
961 refs_cmp(const void *v1, const void *v2)
963 git_reference *r1 = (*(git_reference **)v1);
964 git_reference *r2 = (*(git_reference **)v2);
967 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
970 return strcmp(git_reference_shorthand(r1),
971 git_reference_shorthand(r2));
977 struct commitinfo *ci;
978 const git_oid *id = NULL;
979 git_object *obj = NULL;
980 git_reference *dref = NULL, *r, *ref = NULL;
981 git_reference_iterator *it = NULL;
982 git_reference **refs = NULL;
983 size_t count, i, j, refcount;
984 const char *titles[] = { "Branches", "Tags" };
985 const char *ids[] = { "branches", "tags" };
988 if (git_reference_iterator_new(&it, repo))
991 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
992 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
994 refs[refcount] = ref;
996 git_reference_iterator_free(it);
998 /* sort by type then shorthand name */
999 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
1001 for (j = 0; j < 2; j++) {
1002 for (i = 0, count = 0; i < refcount; i++) {
1003 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1004 !(git_reference_is_tag(refs[i]) && j == 1))
1007 switch (git_reference_type(refs[i])) {
1008 case GIT_REF_SYMBOLIC:
1009 if (git_reference_resolve(&dref, refs[i]))
1019 if (!git_reference_target(r) ||
1020 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1022 if (!(id = git_object_id(obj)))
1024 if (!(ci = commitinfo_getbyoid(id)))
1027 /* print header if it has an entry (first). */
1029 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1030 "<thead>\n<tr><td><b>Name</b></td>"
1031 "<td><b>Last commit date</b></td>"
1032 "<td><b>Author</b></td>\n</tr>\n"
1033 "</thead><tbody>\n",
1038 name = git_reference_shorthand(r);
1040 fputs("<tr><td>", fp);
1041 xmlencode(fp, name, strlen(name));
1042 fputs("</td><td>", fp);
1044 printtimeshort(fp, &(ci->author->when));
1045 fputs("</td><td>", fp);
1047 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1048 fputs("</td></tr>\n", fp);
1052 commitinfo_free(ci);
1053 git_object_free(obj);
1055 git_reference_free(dref);
1060 fputs("</tbody></table><br/>", fp);
1064 git_object_free(obj);
1065 git_reference_free(dref);
1067 for (i = 0; i < refcount; i++)
1068 git_reference_free(refs[i]);
1077 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1082 main(int argc, char *argv[])
1084 git_object *obj = NULL;
1085 const git_oid *head = NULL;
1088 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1089 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1093 for (i = 1; i < argc; i++) {
1094 if (argv[i][0] != '-') {
1098 } else if (argv[i][1] == 'c') {
1099 if (nlogcommits > 0 || i + 1 >= argc)
1101 cachefile = argv[++i];
1102 } else if (argv[i][1] == 'l') {
1103 if (cachefile || i + 1 >= argc)
1106 nlogcommits = strtoll(argv[++i], &p, 10);
1107 if (argv[i][0] == '\0' || *p != '\0' ||
1108 nlogcommits <= 0 || errno)
1115 if (!realpath(repodir, repodirabs))
1121 if (unveil(repodir, "r") == -1)
1122 err(1, "unveil: %s", repodir);
1123 if (unveil(".", "rwc") == -1)
1124 err(1, "unveil: .");
1125 if (cachefile && unveil(cachefile, "rwc") == -1)
1126 err(1, "unveil: %s", cachefile);
1129 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1132 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1137 if (git_repository_open_ext(&repo, repodir,
1138 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1139 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1144 if (!git_revparse_single(&obj, repo, "HEAD"))
1145 head = git_object_id(obj);
1146 git_object_free(obj);
1148 /* use directory name as name */
1149 if ((name = strrchr(repodirabs, '/')))
1156 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1157 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1158 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1159 cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
1161 /* strip .git suffix */
1162 if (!(strippedname = strdup(name)))
1164 if ((p = strrchr(strippedname, '.')))
1165 if (!strcmp(p, ".git"))
1168 /* read description or .git/description */
1169 joinpath(path, sizeof(path), repodir, "description");
1170 if (!(fpread = fopen(path, "r"))) {
1171 joinpath(path, sizeof(path), repodir, ".git/description");
1172 fpread = fopen(path, "r");
1175 if (!fgets(description, sizeof(description), fpread))
1176 description[0] = '\0';
1180 /* read url or .git/url */
1181 joinpath(path, sizeof(path), repodir, "url");
1182 if (!(fpread = fopen(path, "r"))) {
1183 joinpath(path, sizeof(path), repodir, ".git/url");
1184 fpread = fopen(path, "r");
1187 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1189 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1194 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1195 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1196 git_object_type(obj) == GIT_OBJ_BLOB)
1197 license = licensefiles[i] + strlen("HEAD:");
1198 git_object_free(obj);
1202 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1203 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1204 git_object_type(obj) == GIT_OBJ_BLOB)
1205 readme = readmefiles[i] + strlen("HEAD:");
1206 git_object_free(obj);
1209 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1210 git_object_type(obj) == GIT_OBJ_BLOB)
1211 submodules = ".gitmodules";
1212 git_object_free(obj);
1214 /* Generate tarball */
1216 sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s",
1217 strippedname, repodir);
1221 fp = efopen("log.html", "w");
1223 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1224 writeheader(fp, "Log");
1225 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1226 "<td><b>Commit</b></td>"
1227 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1228 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1229 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1231 if (cachefile && head) {
1232 /* read from cache file (does not need to exist) */
1233 if ((rcachefp = fopen(cachefile, "r"))) {
1234 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1235 errx(1, "%s: no object id", cachefile);
1236 if (git_oid_fromstr(&lastoid, lastoidstr))
1237 errx(1, "%s: invalid object id", cachefile);
1240 /* write log to (temporary) cache */
1241 if ((fd = mkstemp(tmppath)) == -1)
1243 if (!(wcachefp = fdopen(fd, "w")))
1244 err(1, "fdopen: '%s'", tmppath);
1245 /* write last commit id (HEAD) */
1246 git_oid_tostr(buf, sizeof(buf), head);
1247 fprintf(wcachefp, "%s\n", buf);
1252 /* append previous log to log.html and the new cache */
1253 while (!feof(rcachefp)) {
1254 n = fread(buf, 1, sizeof(buf), rcachefp);
1255 if (ferror(rcachefp))
1257 if (fwrite(buf, 1, n, fp) != n ||
1258 fwrite(buf, 1, n, wcachefp) != n)
1269 fputs("</tbody></table>", fp);
1273 /* files for HEAD */
1274 fp = efopen("files.html", "w");
1275 writeheader(fp, "Files");
1277 writefiles(fp, head);
1281 /* summary page with branches and tags */
1282 fp = efopen("refs.html", "w");
1283 writeheader(fp, "Refs");
1289 fp = efopen("atom.xml", "w");
1293 /* rename new cache file on success */
1294 if (cachefile && head) {
1295 if (rename(tmppath, cachefile))
1296 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1297 umask((mask = umask(0)));
1298 if (chmod(cachefile,
1299 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1300 err(1, "chmod: '%s'", cachefile);
1304 git_repository_free(repo);
1305 git_libgit2_shutdown();