28 char oid[GIT_OID_HEXSZ + 1];
29 char parentoid[GIT_OID_HEXSZ + 1];
31 const git_signature *author;
32 const git_signature *committer;
39 git_tree *commit_tree;
40 git_tree *parent_tree;
46 struct deltainfo **deltas;
50 static git_repository *repo;
52 static const char *relpath = "";
53 static const char *repodir;
55 static char *name = "";
56 static char *strippedname = "";
57 static char description[255];
58 static char cloneurl[1024];
59 static char *submodules;
60 static char *licensefiles[] = { "HEAD:LICENSE", "HEAD:LICENSE.md", "HEAD:COPYING" };
62 static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" };
64 static long long nlogcommits = -1; /* < 0 indicates not used */
67 static git_oid lastoid;
68 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
69 static FILE *rcachefp, *wcachefp;
70 static const char *cachefile;
73 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
77 r = snprintf(buf, bufsiz, "%s%s%s",
78 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
79 if (r == -1 || (size_t)r >= bufsiz)
80 errx(1, "path truncated: '%s%s%s'",
81 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
85 deltainfo_free(struct deltainfo *di)
89 git_patch_free(di->patch);
90 memset(di, 0, sizeof(*di));
95 commitinfo_getstats(struct commitinfo *ci)
98 git_diff_options opts;
99 git_diff_find_options fopts;
100 const git_diff_delta *delta;
101 const git_diff_hunk *hunk;
102 const git_diff_line *line;
103 git_patch *patch = NULL;
104 size_t ndeltas, nhunks, nhunklines;
107 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
109 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
110 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
112 ci->parent_tree = NULL;
116 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
117 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH |
118 GIT_DIFF_IGNORE_SUBMODULES |
119 GIT_DIFF_INCLUDE_TYPECHANGE;
120 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
123 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
125 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
126 if (git_diff_find_similar(ci->diff, &fopts))
129 ndeltas = git_diff_num_deltas(ci->diff);
130 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
133 for (i = 0; i < ndeltas; i++) {
134 if (git_patch_from_diff(&patch, ci->diff, i))
137 if (!(di = calloc(1, sizeof(struct deltainfo))))
142 delta = git_patch_get_delta(patch);
144 /* skip stats for binary data */
145 if (delta->flags & GIT_DIFF_FLAG_BINARY)
148 nhunks = git_patch_num_hunks(patch);
149 for (j = 0; j < nhunks; j++) {
150 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
153 if (git_patch_get_line_in_hunk(&line, patch, j, k))
155 if (line->old_lineno == -1) {
158 } else if (line->new_lineno == -1) {
171 git_diff_free(ci->diff);
173 git_tree_free(ci->commit_tree);
174 ci->commit_tree = NULL;
175 git_tree_free(ci->parent_tree);
176 ci->parent_tree = NULL;
177 git_commit_free(ci->parent);
181 for (i = 0; i < ci->ndeltas; i++)
182 deltainfo_free(ci->deltas[i]);
194 commitinfo_free(struct commitinfo *ci)
201 for (i = 0; i < ci->ndeltas; i++)
202 deltainfo_free(ci->deltas[i]);
205 git_diff_free(ci->diff);
206 git_tree_free(ci->commit_tree);
207 git_tree_free(ci->parent_tree);
208 git_commit_free(ci->commit);
209 git_commit_free(ci->parent);
210 memset(ci, 0, sizeof(*ci));
215 commitinfo_getbyoid(const git_oid *id)
217 struct commitinfo *ci;
219 if (!(ci = calloc(1, sizeof(struct commitinfo))))
222 if (git_commit_lookup(&(ci->commit), repo, id))
226 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
227 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
229 ci->author = git_commit_author(ci->commit);
230 ci->committer = git_commit_committer(ci->commit);
231 ci->summary = git_commit_summary(ci->commit);
232 ci->msg = git_commit_message(ci->commit);
243 efopen(const char *name, const char *flags)
247 if (!(fp = fopen(name, flags)))
248 err(1, "fopen: '%s'", name);
253 /* Escape characters below as HTML 2.0 / XML 1.0. */
255 xmlencode(FILE *fp, const char *s, size_t len)
259 for (i = 0; *s && i < len; s++, i++) {
261 case '<': fputs("<", fp); break;
262 case '>': fputs(">", fp); break;
263 case '\'': fputs("'", fp); break;
264 case '&': fputs("&", fp); break;
265 case '"': fputs(""", fp); break;
266 default: fputc(*s, fp);
272 mkdirp(const char *path)
274 char tmp[PATH_MAX], *p;
276 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
277 errx(1, "path truncated: '%s'", path);
278 for (p = tmp + (tmp[0] == '/'); *p; p++) {
282 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
286 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
292 printtimez(FILE *fp, const git_time *intime)
298 t = (time_t)intime->time;
299 if (!(intm = gmtime(&t)))
301 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
306 printtime(FILE *fp, const git_time *intime)
312 t = (time_t)intime->time + (intime->offset * 60);
313 if (!(intm = gmtime(&t)))
315 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
316 if (intime->offset < 0)
317 fprintf(fp, "%s -%02d%02d", out,
318 -(intime->offset) / 60, -(intime->offset) % 60);
320 fprintf(fp, "%s +%02d%02d", out,
321 intime->offset / 60, intime->offset % 60);
325 printtimeshort(FILE *fp, const git_time *intime)
331 t = (time_t)intime->time;
332 if (!(intm = gmtime(&t)))
334 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
339 writeheader(FILE *fp, const char *title)
341 fputs("<!DOCTYPE html>\n"
343 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
345 xmlencode(fp, title, strlen(title));
346 if (title[0] && strippedname[0])
348 xmlencode(fp, strippedname, strlen(strippedname));
351 xmlencode(fp, description, strlen(description));
352 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
353 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
355 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
356 fputs("</head>\n<body>\n<table><tr><td>", fp);
357 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
359 fputs("</td><td><h1>", fp);
360 xmlencode(fp, strippedname, strlen(strippedname));
361 fputs("</h1><span class=\"desc\">", fp);
362 xmlencode(fp, description, strlen(description));
363 fputs("</span></td></tr>", fp);
365 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
366 xmlencode(fp, cloneurl, strlen(cloneurl));
368 xmlencode(fp, cloneurl, strlen(cloneurl));
369 fputs("</a></td></tr>", fp);
371 fputs("<tr><td></td><td>\n", fp);
372 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
373 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
374 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
376 fprintf(fp, " | <a href=\"%sfile/%s.html\">Submodules</a>",
377 relpath, submodules);
379 fprintf(fp, " | <a href=\"%sfile/%s.html\">README</a>",
382 fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
384 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
388 writefooter(FILE *fp)
390 fputs("</div>\n</body>\n</html>\n", fp);
394 writeblobhtml(FILE *fp, const git_blob *blob)
396 size_t n = 0, i, prev;
397 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
398 const char *s = git_blob_rawcontent(blob);
399 git_off_t len = git_blob_rawsize(blob);
401 fputs("<pre id=\"blob\">\n", fp);
404 for (i = 0, prev = 0; i < (size_t)len; i++) {
408 fprintf(fp, nfmt, n, n, n);
409 xmlencode(fp, &s[prev], i - prev + 1);
413 if ((len - prev) > 0) {
415 fprintf(fp, nfmt, n, n, n);
416 xmlencode(fp, &s[prev], len - prev);
420 fputs("</pre>\n", fp);
426 printcommit(FILE *fp, struct commitinfo *ci)
428 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
429 relpath, ci->oid, ci->oid);
431 if (ci->parentoid[0])
432 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
433 relpath, ci->parentoid, ci->parentoid);
436 fputs("<b>Author:</b> ", fp);
437 xmlencode(fp, ci->author->name, strlen(ci->author->name));
438 fputs(" <<a href=\"mailto:", fp);
439 xmlencode(fp, ci->author->email, strlen(ci->author->email));
441 xmlencode(fp, ci->author->email, strlen(ci->author->email));
442 fputs("</a>>\n<b>Date:</b> ", fp);
443 printtime(fp, &(ci->author->when));
448 xmlencode(fp, ci->msg, strlen(ci->msg));
454 printshowfile(FILE *fp, struct commitinfo *ci)
456 const git_diff_delta *delta;
457 const git_diff_hunk *hunk;
458 const git_diff_line *line;
460 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
468 if (ci->filecount > 1000 ||
469 ci->ndeltas > 1000 ||
470 ci->addcount > 100000 ||
471 ci->delcount > 100000) {
472 fputs("Diff is too large, output suppressed.\n", fp);
477 fputs("<b>Diffstat:</b>\n<table>", fp);
478 for (i = 0; i < ci->ndeltas; i++) {
479 delta = git_patch_get_delta(ci->deltas[i]->patch);
481 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
482 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
483 if (strcmp(delta->old_file.path, delta->new_file.path)) {
484 fputs(" -> ", fp);
485 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
488 add = ci->deltas[i]->addcount;
489 del = ci->deltas[i]->delcount;
491 total = sizeof(linestr) - 2;
492 if (changed > total) {
494 add = ((float)total / changed * add) + 1;
496 del = ((float)total / changed * del) + 1;
498 memset(&linestr, '+', add);
499 memset(&linestr[add], '-', del);
501 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
502 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
503 fwrite(&linestr, 1, add, fp);
504 fputs("</span><span class=\"d\">", fp);
505 fwrite(&linestr[add], 1, del, fp);
506 fputs("</span></td></tr>\n", fp);
508 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
509 ci->filecount, ci->filecount == 1 ? "" : "s",
510 ci->addcount, ci->addcount == 1 ? "" : "s",
511 ci->delcount, ci->delcount == 1 ? "" : "s");
515 for (i = 0; i < ci->ndeltas; i++) {
516 patch = ci->deltas[i]->patch;
517 delta = git_patch_get_delta(patch);
518 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
519 i, relpath, delta->old_file.path, delta->old_file.path,
520 relpath, delta->new_file.path, delta->new_file.path);
522 /* check binary data */
523 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
524 fputs("Binary files differ.\n", fp);
528 nhunks = git_patch_num_hunks(patch);
529 for (j = 0; j < nhunks; j++) {
530 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
533 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
534 xmlencode(fp, hunk->header, hunk->header_len);
538 if (git_patch_get_line_in_hunk(&line, patch, j, k))
540 if (line->old_lineno == -1)
541 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
543 else if (line->new_lineno == -1)
544 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
548 xmlencode(fp, line->content, line->content_len);
549 if (line->old_lineno == -1 || line->new_lineno == -1)
557 writelogline(FILE *fp, struct commitinfo *ci)
559 fputs("<tr><td>", fp);
561 printtimeshort(fp, &(ci->author->when));
562 fputs("</td><td>", fp);
564 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
565 xmlencode(fp, ci->summary, strlen(ci->summary));
568 fputs("</td><td>", fp);
570 xmlencode(fp, ci->author->name, strlen(ci->author->name));
571 fputs("</td><td class=\"num\" align=\"right\">", fp);
572 fprintf(fp, "%zu", ci->filecount);
573 fputs("</td><td class=\"num\" align=\"right\">", fp);
574 fprintf(fp, "+%zu", ci->addcount);
575 fputs("</td><td class=\"num\" align=\"right\">", fp);
576 fprintf(fp, "-%zu", ci->delcount);
577 fputs("</td></tr>\n", fp);
581 writelog(FILE *fp, const git_oid *oid)
583 struct commitinfo *ci;
584 git_revwalk *w = NULL;
586 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
590 git_revwalk_new(&w, repo);
591 git_revwalk_push(w, oid);
592 git_revwalk_sorting(w, GIT_SORT_TIME);
593 git_revwalk_simplify_first_parent(w);
595 while (!git_revwalk_next(&id, w)) {
598 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
601 git_oid_tostr(oidstr, sizeof(oidstr), &id);
602 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
603 if (r == -1 || (size_t)r >= sizeof(path))
604 errx(1, "path truncated: 'commit/%s.html'", oidstr);
605 r = access(path, F_OK);
607 /* optimization: if there are no log lines to write and
608 the commit file already exists: skip the diffstat */
609 if (!nlogcommits && !r)
612 if (!(ci = commitinfo_getbyoid(&id)))
614 /* diffstat: for stagit HTML required for the log.html line */
615 if (commitinfo_getstats(ci) == -1)
618 if (nlogcommits < 0) {
619 writelogline(fp, ci);
620 } else if (nlogcommits > 0) {
621 writelogline(fp, ci);
623 if (!nlogcommits && ci->parentoid[0])
624 fputs("<tr><td></td><td colspan=\"5\">"
625 "More commits remaining [...]</td>"
630 writelogline(wcachefp, ci);
632 /* check if file exists if so skip it */
635 fpfile = efopen(path, "w");
636 writeheader(fpfile, ci->summary);
637 fputs("<pre>", fpfile);
638 printshowfile(fpfile, ci);
639 fputs("</pre>\n", fpfile);
654 printcommitatom(FILE *fp, struct commitinfo *ci)
656 fputs("<entry>\n", fp);
658 fprintf(fp, "<id>%s</id>\n", ci->oid);
660 fputs("<published>", fp);
661 printtimez(fp, &(ci->author->when));
662 fputs("</published>\n", fp);
665 fputs("<updated>", fp);
666 printtimez(fp, &(ci->committer->when));
667 fputs("</updated>\n", fp);
670 fputs("<title type=\"text\">", fp);
671 xmlencode(fp, ci->summary, strlen(ci->summary));
672 fputs("</title>\n", fp);
674 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
678 fputs("<author><name>", fp);
679 xmlencode(fp, ci->author->name, strlen(ci->author->name));
680 fputs("</name>\n<email>", fp);
681 xmlencode(fp, ci->author->email, strlen(ci->author->email));
682 fputs("</email>\n</author>\n", fp);
685 fputs("<content type=\"text\">", fp);
686 fprintf(fp, "commit %s\n", ci->oid);
687 if (ci->parentoid[0])
688 fprintf(fp, "parent %s\n", ci->parentoid);
690 fputs("Author: ", fp);
691 xmlencode(fp, ci->author->name, strlen(ci->author->name));
693 xmlencode(fp, ci->author->email, strlen(ci->author->email));
694 fputs(">\nDate: ", fp);
695 printtime(fp, &(ci->author->when));
700 xmlencode(fp, ci->msg, strlen(ci->msg));
702 fputs("\n</content>\n</entry>\n", fp);
708 struct commitinfo *ci;
709 git_revwalk *w = NULL;
711 size_t i, m = 100; /* last 'm' commits */
713 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
714 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
715 xmlencode(fp, strippedname, strlen(strippedname));
716 fputs(", branch HEAD</title>\n<subtitle>", fp);
717 xmlencode(fp, description, strlen(description));
718 fputs("</subtitle>\n", fp);
720 git_revwalk_new(&w, repo);
721 git_revwalk_push_head(w);
722 git_revwalk_sorting(w, GIT_SORT_TIME);
723 git_revwalk_simplify_first_parent(w);
725 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
726 if (!(ci = commitinfo_getbyoid(&id)))
728 printcommitatom(fp, ci);
733 fputs("</feed>\n", fp);
739 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
741 char tmp[PATH_MAX] = "", *d;
746 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
747 errx(1, "path truncated: '%s'", fpath);
748 if (!(d = dirname(tmp)))
753 for (p = fpath, tmp[0] = '\0'; *p; p++) {
754 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
755 errx(1, "path truncated: '../%s'", tmp);
759 fp = efopen(fpath, "w");
760 writeheader(fp, filename);
762 xmlencode(fp, filename, strlen(filename));
763 fprintf(fp, " (%juB)", (uintmax_t)filesize);
764 fputs("</p><hr/>", fp);
766 if (git_blob_is_binary((git_blob *)obj)) {
767 fputs("<p>Binary file.</p>\n", fp);
769 lc = writeblobhtml(fp, (git_blob *)obj);
782 filemode(git_filemode_t m)
784 static char mode[11];
786 memset(mode, '-', sizeof(mode) - 1);
797 else if (S_ISFIFO(m))
801 else if (S_ISSOCK(m))
806 if (m & S_IRUSR) mode[1] = 'r';
807 if (m & S_IWUSR) mode[2] = 'w';
808 if (m & S_IXUSR) mode[3] = 'x';
809 if (m & S_IRGRP) mode[4] = 'r';
810 if (m & S_IWGRP) mode[5] = 'w';
811 if (m & S_IXGRP) mode[6] = 'x';
812 if (m & S_IROTH) mode[7] = 'r';
813 if (m & S_IWOTH) mode[8] = 'w';
814 if (m & S_IXOTH) mode[9] = 'x';
816 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
817 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
818 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
824 writefilestree(FILE *fp, git_tree *tree, const char *path)
826 const git_tree_entry *entry = NULL;
827 git_submodule *module = NULL;
828 git_object *obj = NULL;
830 const char *entryname;
831 char filepath[PATH_MAX], entrypath[PATH_MAX];
835 count = git_tree_entrycount(tree);
836 for (i = 0; i < count; i++) {
837 if (!(entry = git_tree_entry_byindex(tree, i)) ||
838 !(entryname = git_tree_entry_name(entry)))
840 joinpath(entrypath, sizeof(entrypath), path, entryname);
842 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
844 if (r == -1 || (size_t)r >= sizeof(filepath))
845 errx(1, "path truncated: 'file/%s.html'", entrypath);
847 if (!git_tree_entry_to_object(&obj, repo, entry)) {
848 switch (git_object_type(obj)) {
853 ret = writefilestree(fp, (git_tree *)obj,
855 git_object_free(obj);
860 git_object_free(obj);
864 filesize = git_blob_rawsize((git_blob *)obj);
865 lc = writeblob(obj, filepath, entryname, filesize);
867 fputs("<tr><td>", fp);
868 fputs(filemode(git_tree_entry_filemode(entry)), fp);
869 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
870 xmlencode(fp, entrypath, strlen(entrypath));
871 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
873 fprintf(fp, "%dL", lc);
875 fprintf(fp, "%juB", (uintmax_t)filesize);
876 fputs("</td></tr>\n", fp);
877 git_object_free(obj);
878 } else if (!git_submodule_lookup(&module, repo, entryname)) {
879 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
881 xmlencode(fp, entrypath, strlen(entrypath));
882 git_submodule_free(module);
883 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
891 writefiles(FILE *fp, const git_oid *id)
893 git_tree *tree = NULL;
894 git_commit *commit = NULL;
897 fputs("<table id=\"files\"><thead>\n<tr>"
898 "<td><b>Mode</b></td><td><b>Name</b></td>"
899 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
900 "</tr>\n</thead><tbody>\n", fp);
902 if (!git_commit_lookup(&commit, repo, id) &&
903 !git_commit_tree(&tree, commit))
904 ret = writefilestree(fp, tree, "");
906 fputs("</tbody></table>", fp);
908 git_commit_free(commit);
915 refs_cmp(const void *v1, const void *v2)
917 git_reference *r1 = (*(git_reference **)v1);
918 git_reference *r2 = (*(git_reference **)v2);
921 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
924 return strcmp(git_reference_shorthand(r1),
925 git_reference_shorthand(r2));
931 struct commitinfo *ci;
932 const git_oid *id = NULL;
933 git_object *obj = NULL;
934 git_reference *dref = NULL, *r, *ref = NULL;
935 git_reference_iterator *it = NULL;
936 git_reference **refs = NULL;
937 size_t count, i, j, refcount;
938 const char *titles[] = { "Branches", "Tags" };
939 const char *ids[] = { "branches", "tags" };
942 if (git_reference_iterator_new(&it, repo))
945 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
946 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
948 refs[refcount] = ref;
950 git_reference_iterator_free(it);
952 /* sort by type then shorthand name */
953 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
955 for (j = 0; j < 2; j++) {
956 for (i = 0, count = 0; i < refcount; i++) {
957 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
958 !(git_reference_is_tag(refs[i]) && j == 1))
961 switch (git_reference_type(refs[i])) {
962 case GIT_REF_SYMBOLIC:
963 if (git_reference_resolve(&dref, refs[i]))
973 if (!git_reference_target(r) ||
974 git_reference_peel(&obj, r, GIT_OBJ_ANY))
976 if (!(id = git_object_id(obj)))
978 if (!(ci = commitinfo_getbyoid(id)))
981 /* print header if it has an entry (first). */
983 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
984 "<thead>\n<tr><td><b>Name</b></td>"
985 "<td><b>Last commit date</b></td>"
986 "<td><b>Author</b></td>\n</tr>\n"
992 name = git_reference_shorthand(r);
994 fputs("<tr><td>", fp);
995 xmlencode(fp, name, strlen(name));
996 fputs("</td><td>", fp);
998 printtimeshort(fp, &(ci->author->when));
999 fputs("</td><td>", fp);
1001 xmlencode(fp, ci->author->name, strlen(ci->author->name));
1002 fputs("</td></tr>\n", fp);
1006 commitinfo_free(ci);
1007 git_object_free(obj);
1009 git_reference_free(dref);
1014 fputs("</tbody></table><br/>", fp);
1018 git_object_free(obj);
1019 git_reference_free(dref);
1021 for (i = 0; i < refcount; i++)
1022 git_reference_free(refs[i]);
1031 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1036 main(int argc, char *argv[])
1038 git_object *obj = NULL;
1039 const git_oid *head = NULL;
1040 const git_error *e = NULL;
1043 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1044 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1048 for (i = 1; i < argc; i++) {
1049 if (argv[i][0] != '-') {
1053 } else if (argv[i][1] == 'c') {
1054 if (nlogcommits > 0 || i + 1 >= argc)
1056 cachefile = argv[++i];
1057 } else if (argv[i][1] == 'l') {
1058 if (cachefile || i + 1 >= argc)
1061 nlogcommits = strtoll(argv[++i], &p, 10);
1062 if (argv[i][0] == '\0' || *p != '\0' ||
1063 nlogcommits <= 0 || errno)
1070 if (!realpath(repodir, repodirabs))
1077 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1080 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1085 if (git_repository_open_ext(&repo, repodir,
1086 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1088 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1093 if (!git_revparse_single(&obj, repo, "HEAD"))
1094 head = git_object_id(obj);
1095 git_object_free(obj);
1097 /* use directory name as name */
1098 if ((name = strrchr(repodirabs, '/')))
1103 /* strip .git suffix */
1104 if (!(strippedname = strdup(name)))
1106 if ((p = strrchr(strippedname, '.')))
1107 if (!strcmp(p, ".git"))
1110 /* read description or .git/description */
1111 joinpath(path, sizeof(path), repodir, "description");
1112 if (!(fpread = fopen(path, "r"))) {
1113 joinpath(path, sizeof(path), repodir, ".git/description");
1114 fpread = fopen(path, "r");
1117 if (!fgets(description, sizeof(description), fpread))
1118 description[0] = '\0';
1122 /* read url or .git/url */
1123 joinpath(path, sizeof(path), repodir, "url");
1124 if (!(fpread = fopen(path, "r"))) {
1125 joinpath(path, sizeof(path), repodir, ".git/url");
1126 fpread = fopen(path, "r");
1129 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1131 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1136 for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {
1137 if (!git_revparse_single(&obj, repo, licensefiles[i]) &&
1138 git_object_type(obj) == GIT_OBJ_BLOB)
1139 license = licensefiles[i] + strlen("HEAD:");
1140 git_object_free(obj);
1144 for (i = 0; i < sizeof(readmefiles) / sizeof(*readmefiles) && !readme; i++) {
1145 if (!git_revparse_single(&obj, repo, readmefiles[i]) &&
1146 git_object_type(obj) == GIT_OBJ_BLOB)
1147 readme = readmefiles[i] + strlen("HEAD:");
1148 git_object_free(obj);
1151 if (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1152 git_object_type(obj) == GIT_OBJ_BLOB)
1153 submodules = ".gitmodules";
1154 git_object_free(obj);
1157 fp = efopen("log.html", "w");
1159 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1160 writeheader(fp, "Log");
1161 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1162 "<td><b>Commit message</b></td>"
1163 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1164 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1165 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1167 if (cachefile && head) {
1168 /* read from cache file (does not need to exist) */
1169 if ((rcachefp = fopen(cachefile, "r"))) {
1170 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1171 errx(1, "%s: no object id", cachefile);
1172 if (git_oid_fromstr(&lastoid, lastoidstr))
1173 errx(1, "%s: invalid object id", cachefile);
1176 /* write log to (temporary) cache */
1177 if ((fd = mkstemp(tmppath)) == -1)
1179 if (!(wcachefp = fdopen(fd, "w")))
1180 err(1, "fdopen: '%s'", tmppath);
1181 /* write last commit id (HEAD) */
1182 git_oid_tostr(buf, sizeof(buf), head);
1183 fprintf(wcachefp, "%s\n", buf);
1188 /* append previous log to log.html and the new cache */
1189 while (!feof(rcachefp)) {
1190 n = fread(buf, 1, sizeof(buf), rcachefp);
1191 if (ferror(rcachefp))
1193 if (fwrite(buf, 1, n, fp) != n ||
1194 fwrite(buf, 1, n, wcachefp) != n)
1205 fputs("</tbody></table>", fp);
1209 /* files for HEAD */
1210 fp = efopen("files.html", "w");
1211 writeheader(fp, "Files");
1213 writefiles(fp, head);
1217 /* summary page with branches and tags */
1218 fp = efopen("refs.html", "w");
1219 writeheader(fp, "Refs");
1225 fp = efopen("atom.xml", "w");
1229 /* rename new cache file on success */
1230 if (cachefile && head) {
1231 if (rename(tmppath, cachefile))
1232 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1233 umask((mask = umask(0)));
1234 if (chmod(cachefile,
1235 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1236 err(1, "chmod: '%s'", cachefile);
1240 git_repository_free(repo);
1241 git_libgit2_shutdown();