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.min.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><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 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
394 writefooter(FILE *fp)
396 fputs("</div>\n</body>\n</html>\n", fp);
400 call_py(const char *filename, FILE *fp, const char *s, size_t len)
405 int stdout_copy = dup(1);
409 // Python Pygments script for syntax highlighting.
410 FILE *child = popen("/usr/local/share/stagit/highlight.py", "w");
412 printf("child is null: %s", strerror(errno));
415 // Give filename through STDIN:
416 fprintf(child, "%s\n", filename);
417 // Give code to highlight through STDIN:
420 for (i = 0; *s && i < len; s++, i++) {
421 if (*s == '\n') lc++;
422 fprintf(child, "%c", *s);
428 dup2(stdout_copy, 1);
433 writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
436 const char *s = git_blob_rawcontent(blob);
437 git_off_t len = git_blob_rawsize(blob);
440 lc = call_py(filename, fp, s, len);
447 printcommit(FILE *fp, struct commitinfo *ci)
449 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
450 relpath, ci->oid, ci->oid);
452 if (ci->parentoid[0])
453 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
454 relpath, ci->parentoid, ci->parentoid);
457 fputs("<b>Author:</b> ", fp);
458 xmlencode(fp, ci->author->name, strlen(ci->author->name));
459 fputs(" <<a href=\"mailto:", fp);
460 xmlencode(fp, ci->author->email, strlen(ci->author->email));
462 xmlencode(fp, ci->author->email, strlen(ci->author->email));
463 fputs("</a>>\n<b>Date:</b> ", fp);
464 printtime(fp, &(ci->author->when));
469 xmlencode(fp, ci->msg, strlen(ci->msg));
475 printshowfile(FILE *fp, struct commitinfo *ci)
477 const git_diff_delta *delta;
478 const git_diff_hunk *hunk;
479 const git_diff_line *line;
481 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
490 if (ci->filecount > 1000 ||
491 ci->ndeltas > 1000 ||
492 ci->addcount > 100000 ||
493 ci->delcount > 100000) {
494 fputs("Diff is too large, output suppressed.\n", fp);
499 fputs("<b>Diffstat:</b>\n<table>", fp);
500 for (i = 0; i < ci->ndeltas; i++) {
501 delta = git_patch_get_delta(ci->deltas[i]->patch);
503 switch (delta->status) {
504 case GIT_DELTA_ADDED: c = 'A'; break;
505 case GIT_DELTA_COPIED: c = 'C'; break;
506 case GIT_DELTA_DELETED: c = 'D'; break;
507 case GIT_DELTA_MODIFIED: c = 'M'; break;
508 case GIT_DELTA_RENAMED: c = 'R'; break;
509 case GIT_DELTA_TYPECHANGE: c = 'T'; break;
510 default: c = ' '; break;
513 fprintf(fp, "<tr><td>%c", c);
515 fprintf(fp, "<tr><td class=\"%c\">%c", c, c);
517 fprintf(fp, "</td><td><a href=\"#h%zu\">", i);
518 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
519 if (strcmp(delta->old_file.path, delta->new_file.path)) {
520 fputs(" -> ", fp);
521 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
524 add = ci->deltas[i]->addcount;
525 del = ci->deltas[i]->delcount;
527 total = sizeof(linestr) - 2;
528 if (changed > total) {
530 add = ((float)total / changed * add) + 1;
532 del = ((float)total / changed * del) + 1;
534 memset(&linestr, '+', add);
535 memset(&linestr[add], '-', del);
537 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
538 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
539 fwrite(&linestr, 1, add, fp);
540 fputs("</span><span class=\"d\">", fp);
541 fwrite(&linestr[add], 1, del, fp);
542 fputs("</span></td></tr>\n", fp);
544 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
545 ci->filecount, ci->filecount == 1 ? "" : "s",
546 ci->addcount, ci->addcount == 1 ? "" : "s",
547 ci->delcount, ci->delcount == 1 ? "" : "s");
551 for (i = 0; i < ci->ndeltas; i++) {
552 patch = ci->deltas[i]->patch;
553 delta = git_patch_get_delta(patch);
554 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/", i, relpath);
555 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
556 fputs(".html\">", fp);
557 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
558 fprintf(fp, "</a> b/<a href=\"%sfile/", relpath);
559 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
560 fprintf(fp, ".html\">");
561 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
562 fprintf(fp, "</a></b>\n");
564 /* check binary data */
565 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
566 fputs("Binary files differ.\n", fp);
570 nhunks = git_patch_num_hunks(patch);
571 for (j = 0; j < nhunks; j++) {
572 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
575 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
576 xmlencode(fp, hunk->header, hunk->header_len);
580 if (git_patch_get_line_in_hunk(&line, patch, j, k))
582 if (line->old_lineno == -1)
583 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
585 else if (line->new_lineno == -1)
586 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
590 xmlencode(fp, line->content, line->content_len);
591 if (line->old_lineno == -1 || line->new_lineno == -1)
599 writelogline(FILE *fp, struct commitinfo *ci)
601 fputs("<tr><td>", fp);
603 printtimeshort(fp, &(ci->author->when));
604 fputs("</td><td>", fp);
606 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
607 xmlencode(fp, ci->summary, strlen(ci->summary));
610 fputs("</td><td>", fp);
612 xmlencode(fp, ci->author->name, strlen(ci->author->name));
613 fputs("</td><td class=\"num\" align=\"right\">", fp);
614 fprintf(fp, "%zu", ci->filecount);
615 fputs("</td><td class=\"num\" align=\"right\">", fp);
616 fprintf(fp, "+%zu", ci->addcount);
617 fputs("</td><td class=\"num\" align=\"right\">", fp);
618 fprintf(fp, "-%zu", ci->delcount);
619 fputs("</td></tr>\n", fp);
623 writelog(FILE *fp, const git_oid *oid)
625 struct commitinfo *ci;
626 git_revwalk *w = NULL;
628 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
632 git_revwalk_new(&w, repo);
633 git_revwalk_push(w, oid);
634 git_revwalk_simplify_first_parent(w);
636 while (!git_revwalk_next(&id, w)) {
639 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
642 git_oid_tostr(oidstr, sizeof(oidstr), &id);
643 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
644 if (r < 0 || (size_t)r >= sizeof(path))
645 errx(1, "path truncated: 'commit/%s.html'", oidstr);
646 r = access(path, F_OK);
648 /* optimization: if there are no log lines to write and
649 the commit file already exists: skip the diffstat */
650 if (!nlogcommits && !r)
653 if (!(ci = commitinfo_getbyoid(&id)))
655 /* diffstat: for stagit HTML required for the log.html line */
656 if (commitinfo_getstats(ci) == -1)
659 if (nlogcommits < 0) {
660 writelogline(fp, ci);
661 } else if (nlogcommits > 0) {
662 writelogline(fp, ci);
664 if (!nlogcommits && ci->parentoid[0])
665 fputs("<tr><td></td><td colspan=\"5\">"
666 "More commits remaining [...]</td>"
671 writelogline(wcachefp, ci);
673 /* check if file exists if so skip it */
676 fpfile = efopen(path, "w");
677 writeheader(fpfile, ci->summary);
678 fputs("<pre>", fpfile);
679 printshowfile(fpfile, ci);
680 fputs("</pre>\n", fpfile);
695 printcommitatom(FILE *fp, struct commitinfo *ci)
697 fputs("<entry>\n", fp);
699 fprintf(fp, "<id>%s</id>\n", ci->oid);
701 fputs("<published>", fp);
702 printtimez(fp, &(ci->author->when));
703 fputs("</published>\n", fp);
706 fputs("<updated>", fp);
707 printtimez(fp, &(ci->committer->when));
708 fputs("</updated>\n", fp);
711 fputs("<title type=\"text\">", fp);
712 xmlencode(fp, ci->summary, strlen(ci->summary));
713 fputs("</title>\n", fp);
715 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />\n",
719 fputs("<author>\n<name>", fp);
720 xmlencode(fp, ci->author->name, strlen(ci->author->name));
721 fputs("</name>\n<email>", fp);
722 xmlencode(fp, ci->author->email, strlen(ci->author->email));
723 fputs("</email>\n</author>\n", fp);
726 fputs("<content type=\"text\">", fp);
727 fprintf(fp, "commit %s\n", ci->oid);
728 if (ci->parentoid[0])
729 fprintf(fp, "parent %s\n", ci->parentoid);
731 fputs("Author: ", fp);
732 xmlencode(fp, ci->author->name, strlen(ci->author->name));
734 xmlencode(fp, ci->author->email, strlen(ci->author->email));
735 fputs(">\nDate: ", fp);
736 printtime(fp, &(ci->author->when));
741 xmlencode(fp, ci->msg, strlen(ci->msg));
743 fputs("\n</content>\n</entry>\n", fp);
749 struct commitinfo *ci;
750 git_revwalk *w = NULL;
752 size_t i, m = 100; /* last 'm' commits */
754 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
755 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
756 xmlencode(fp, strippedname, strlen(strippedname));
757 fputs(", branch HEAD</title>\n<subtitle>", fp);
758 xmlencode(fp, description, strlen(description));
759 fputs("</subtitle>\n", fp);
761 git_revwalk_new(&w, repo);
762 git_revwalk_push_head(w);
763 git_revwalk_simplify_first_parent(w);
765 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
766 if (!(ci = commitinfo_getbyoid(&id)))
768 printcommitatom(fp, ci);
773 fputs("</feed>\n", fp);
779 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
781 char tmp[PATH_MAX] = "", *d;
786 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
787 errx(1, "path truncated: '%s'", fpath);
788 if (!(d = dirname(tmp)))
793 for (p = fpath, tmp[0] = '\0'; *p; p++) {
794 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
795 errx(1, "path truncated: '../%s'", tmp);
799 fp = efopen(fpath, "w");
800 writeheader(fp, filename);
802 xmlencode(fp, filename, strlen(filename));
803 fprintf(fp, " (%juB)", (uintmax_t)filesize);
804 fputs("</p><hr/>", fp);
806 if (git_blob_is_binary((git_blob *)obj)) {
807 fputs("<p>Binary file.</p>\n", fp);
809 lc = writeblobhtml(filename, fp, (git_blob *)obj);
822 filemode(git_filemode_t m)
824 static char mode[11];
826 memset(mode, '-', sizeof(mode) - 1);
837 else if (S_ISFIFO(m))
841 else if (S_ISSOCK(m))
846 if (m & S_IRUSR) mode[1] = 'r';
847 if (m & S_IWUSR) mode[2] = 'w';
848 if (m & S_IXUSR) mode[3] = 'x';
849 if (m & S_IRGRP) mode[4] = 'r';
850 if (m & S_IWGRP) mode[5] = 'w';
851 if (m & S_IXGRP) mode[6] = 'x';
852 if (m & S_IROTH) mode[7] = 'r';
853 if (m & S_IWOTH) mode[8] = 'w';
854 if (m & S_IXOTH) mode[9] = 'x';
856 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
857 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
858 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
864 writefilestree(FILE *fp, git_tree *tree, const char *path)
866 const git_tree_entry *entry = NULL;
867 git_submodule *module = NULL;
868 git_object *obj = NULL;
870 const char *entryname;
871 char filepath[PATH_MAX], entrypath[PATH_MAX];
875 count = git_tree_entrycount(tree);
876 for (i = 0; i < count; i++) {
877 if (!(entry = git_tree_entry_byindex(tree, i)) ||
878 !(entryname = git_tree_entry_name(entry)))
880 joinpath(entrypath, sizeof(entrypath), path, entryname);
882 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
884 if (r < 0 || (size_t)r >= sizeof(filepath))
885 errx(1, "path truncated: 'file/%s.html'", entrypath);
887 if (!git_tree_entry_to_object(&obj, repo, entry)) {
888 switch (git_object_type(obj)) {
893 ret = writefilestree(fp, (git_tree *)obj,
895 git_object_free(obj);
900 git_object_free(obj);
904 filesize = git_blob_rawsize((git_blob *)obj);
905 lc = writeblob(obj, filepath, entryname, filesize);
907 fputs("<tr><td>", fp);
908 fputs(filemode(git_tree_entry_filemode(entry)), fp);
909 fprintf(fp, "</td><td><a href=\"%s", relpath);
910 xmlencode(fp, filepath, strlen(filepath));
912 xmlencode(fp, entrypath, strlen(entrypath));
913 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
915 fprintf(fp, "%dL", lc);
917 fprintf(fp, "%juB", (uintmax_t)filesize);
918 fputs("</td></tr>\n", fp);
919 git_object_free(obj);
920 } else if (!git_submodule_lookup(&module, repo, entryname)) {
921 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
923 xmlencode(fp, entrypath, strlen(entrypath));
924 git_submodule_free(module);
925 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
933 writefiles(FILE *fp, const git_oid *id)
935 git_tree *tree = NULL;
936 git_commit *commit = NULL;
939 fputs("<table id=\"files\"><thead>\n<tr>"
940 "<td><b>Mode</b></td><td><b>Name</b></td>"
941 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
942 "</tr>\n</thead><tbody>\n", fp);
944 if (!git_commit_lookup(&commit, repo, id) &&
945 !git_commit_tree(&tree, commit))
946 ret = writefilestree(fp, tree, "");
948 fputs("</tbody></table>", fp);
950 git_commit_free(commit);
957 refs_cmp(const void *v1, const void *v2)
959 git_reference *r1 = (*(git_reference **)v1);
960 git_reference *r2 = (*(git_reference **)v2);
963 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
966 return strcmp(git_reference_shorthand(r1),
967 git_reference_shorthand(r2));
973 struct commitinfo *ci;
974 const git_oid *id = NULL;
975 git_object *obj = NULL;
976 git_reference *dref = NULL, *r, *ref = NULL;
977 git_reference_iterator *it = NULL;
978 git_reference **refs = NULL;
979 size_t count, i, j, refcount;
980 const char *titles[] = { "Branches", "Tags" };
981 const char *ids[] = { "branches", "tags" };
984 if (git_reference_iterator_new(&it, repo))
987 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
988 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
990 refs[refcount] = ref;
992 git_reference_iterator_free(it);
994 /* sort by type then shorthand name */
995 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
997 for (j = 0; j < 2; j++) {
998 for (i = 0, count = 0; i < refcount; i++) {
999 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
1000 !(git_reference_is_tag(refs[i]) && j == 1))
1003 switch (git_reference_type(refs[i])) {
1004 case GIT_REF_SYMBOLIC:
1005 if (git_reference_resolve(&dref, refs[i]))
1015 if (!git_reference_target(r) ||
1016 git_reference_peel(&obj, r, GIT_OBJ_ANY))
1018 if (!(id = git_object_id(obj)))
1020 if (!(ci = commitinfo_getbyoid(id)))
1023 /* print header if it has an entry (first). */
1025 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
1026 "<thead>\n<tr><td><b>Name</b></td>"
1027 "<td><b>Last commit date</b></td>"
1028 "<td><b>Author</b></td>\n</tr>\n"
1029 "</thead><tbody>\n",
1034 name = git_reference_shorthand(r);
1036 fputs("<tr><td>", fp);
1037 xmlencode(fp, name, strlen(name));
1038 fputs("</td><td>", fp);
1040 printtimeshort(fp, &(ci->author->when));
1041 fputs("</td><td>", fp);
1043 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1044 fputs("</td></tr>\n", fp);
1048 commitinfo_free(ci);
1049 git_object_free(obj);
1051 git_reference_free(dref);
1056 fputs("</tbody></table><br/>", fp);
1060 git_object_free(obj);
1061 git_reference_free(dref);
1063 for (i = 0; i < refcount; i++)
1064 git_reference_free(refs[i]);
1073 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1078 main(int argc, char *argv[])
1080 git_object *obj = NULL;
1081 const git_oid *head = NULL;
1084 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1085 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1089 for (i = 1; i < argc; i++) {
1090 if (argv[i][0] != '-') {
1094 } else if (argv[i][1] == 'c') {
1095 if (nlogcommits > 0 || i + 1 >= argc)
1097 cachefile = argv[++i];
1098 } else if (argv[i][1] == 'l') {
1099 if (cachefile || i + 1 >= argc)
1102 nlogcommits = strtoll(argv[++i], &p, 10);
1103 if (argv[i][0] == '\0' || *p != '\0' ||
1104 nlogcommits <= 0 || errno)
1111 if (!realpath(repodir, repodirabs))
1117 if (unveil(repodir, "r") == -1)
1118 err(1, "unveil: %s", repodir);
1119 if (unveil(".", "rwc") == -1)
1120 err(1, "unveil: .");
1121 if (cachefile && unveil(cachefile, "rwc") == -1)
1122 err(1, "unveil: %s", cachefile);
1125 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1128 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1133 if (git_repository_open_ext(&repo, repodir,
1134 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1135 fprintf(stderr, "%s: cannot open repository\n", argv[0]);
1140 if (!git_revparse_single(&obj, repo, "HEAD"))
1141 head = git_object_id(obj);
1142 git_object_free(obj);
1144 /* use directory name as name */
1145 if ((name = strrchr(repodirabs, '/')))
1152 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1153 cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
1154 strcpy(cwd, getcwd(cwd, sizeof(cwd)));
1155 cp("/usr/local/share/stagit/style.min.css", strcat(cwd, "/style.min.css"));
1157 /* strip .git suffix */
1158 if (!(strippedname = strdup(name)))
1160 if ((p = strrchr(strippedname, '.')))
1161 if (!strcmp(p, ".git"))
1164 /* read description or .git/description */
1165 joinpath(path, sizeof(path), repodir, "description");
1166 if (!(fpread = fopen(path, "r"))) {
1167 joinpath(path, sizeof(path), repodir, ".git/description");
1168 fpread = fopen(path, "r");
1171 if (!fgets(description, sizeof(description), fpread))
1172 description[0] = '\0';
1176 /* read url or .git/url */
1177 joinpath(path, sizeof(path), repodir, "url");
1178 if (!(fpread = fopen(path, "r"))) {
1179 joinpath(path, sizeof(path), repodir, ".git/url");
1180 fpread = fopen(path, "r");
1183 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1185 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1190 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1191 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1192 git_object_type(obj) == GIT_OBJ_BLOB)
1193 license = licensefiles[i] + strlen("HEAD:");
1194 git_object_free(obj);
1198 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1199 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1200 git_object_type(obj) == GIT_OBJ_BLOB)
1201 readme = readmefiles[i] + strlen("HEAD:");
1202 git_object_free(obj);
1205 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1206 git_object_type(obj) == GIT_OBJ_BLOB)
1207 submodules = ".gitmodules";
1208 git_object_free(obj);
1211 fp = efopen("log.html", "w");
1213 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1214 writeheader(fp, "Log");
1215 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1216 "<td><b>Commit</b></td>"
1217 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1218 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1219 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1221 if (cachefile && head) {
1222 /* read from cache file (does not need to exist) */
1223 if ((rcachefp = fopen(cachefile, "r"))) {
1224 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1225 errx(1, "%s: no object id", cachefile);
1226 if (git_oid_fromstr(&lastoid, lastoidstr))
1227 errx(1, "%s: invalid object id", cachefile);
1230 /* write log to (temporary) cache */
1231 if ((fd = mkstemp(tmppath)) == -1)
1233 if (!(wcachefp = fdopen(fd, "w")))
1234 err(1, "fdopen: '%s'", tmppath);
1235 /* write last commit id (HEAD) */
1236 git_oid_tostr(buf, sizeof(buf), head);
1237 fprintf(wcachefp, "%s\n", buf);
1242 /* append previous log to log.html and the new cache */
1243 while (!feof(rcachefp)) {
1244 n = fread(buf, 1, sizeof(buf), rcachefp);
1245 if (ferror(rcachefp))
1247 if (fwrite(buf, 1, n, fp) != n ||
1248 fwrite(buf, 1, n, wcachefp) != n)
1259 fputs("</tbody></table>", fp);
1263 /* files for HEAD */
1264 fp = efopen("files.html", "w");
1265 writeheader(fp, "Files");
1267 writefiles(fp, head);
1271 /* summary page with branches and tags */
1272 fp = efopen("refs.html", "w");
1273 writeheader(fp, "Refs");
1279 fp = efopen("atom.xml", "w");
1283 /* rename new cache file on success */
1284 if (cachefile && head) {
1285 if (rename(tmppath, cachefile))
1286 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1287 umask((mask = umask(0)));
1288 if (chmod(cachefile,
1289 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1290 err(1, "chmod: '%s'", cachefile);
1294 git_repository_free(repo);
1295 git_libgit2_shutdown();