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 int haslicense, hasreadme, hassubmodules;
60 static long long nlogcommits = -1; /* < 0 indicates not used */
63 static git_oid lastoid;
64 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + nul byte */
65 static FILE *rcachefp, *wcachefp;
66 static const char *cachefile;
69 #define pledge(p1,p2) 0
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);
95 commitinfo_getstats(struct commitinfo *ci)
98 const git_diff_delta *delta;
99 const git_diff_hunk *hunk;
100 const git_diff_line *line;
101 git_patch *patch = NULL;
102 size_t ndeltas, nhunks, nhunklines;
105 ndeltas = git_diff_num_deltas(ci->diff);
106 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
109 for (i = 0; i < ndeltas; i++) {
110 if (git_patch_from_diff(&patch, ci->diff, i))
112 if (!(di = calloc(1, sizeof(struct deltainfo))))
117 delta = git_patch_get_delta(patch);
119 /* skip stats for binary data */
120 if (delta->flags & GIT_DIFF_FLAG_BINARY)
123 nhunks = git_patch_num_hunks(patch);
124 for (j = 0; j < nhunks; j++) {
125 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
128 if (git_patch_get_line_in_hunk(&line, patch, j, k))
130 if (line->old_lineno == -1) {
133 } else if (line->new_lineno == -1) {
147 for (i = 0; i < ci->ndeltas; i++)
148 deltainfo_free(ci->deltas[i]);
160 commitinfo_free(struct commitinfo *ci)
167 for (i = 0; i < ci->ndeltas; i++)
168 deltainfo_free(ci->deltas[i]);
171 git_diff_free(ci->diff);
172 git_tree_free(ci->commit_tree);
173 git_tree_free(ci->parent_tree);
174 git_commit_free(ci->commit);
175 git_commit_free(ci->parent);
180 commitinfo_getbyoid(const git_oid *id)
182 struct commitinfo *ci;
183 git_diff_options opts;
185 if (!(ci = calloc(1, sizeof(struct commitinfo))))
188 if (git_commit_lookup(&(ci->commit), repo, id))
192 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
193 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
195 ci->author = git_commit_author(ci->commit);
196 ci->committer = git_commit_committer(ci->commit);
197 ci->summary = git_commit_summary(ci->commit);
198 ci->msg = git_commit_message(ci->commit);
200 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
202 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
203 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
205 ci->parent_tree = NULL;
209 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
210 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
211 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
223 efopen(const char *name, const char *flags)
227 if (!(fp = fopen(name, flags)))
228 err(1, "fopen: '%s'", name);
233 /* Escape characters below as HTML 2.0 / XML 1.0. */
235 xmlencode(FILE *fp, const char *s, size_t len)
239 for (i = 0; *s && i < len; s++, i++) {
241 case '<': fputs("<", fp); break;
242 case '>': fputs(">", fp); break;
243 case '\'': fputs("'", fp); break;
244 case '&': fputs("&", fp); break;
245 case '"': fputs(""", fp); break;
246 default: fputc(*s, fp);
252 mkdirp(const char *path)
254 char tmp[PATH_MAX], *p;
256 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
257 errx(1, "path truncated: '%s'", path);
258 for (p = tmp + (tmp[0] == '/'); *p; p++) {
262 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
266 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
272 printtimez(FILE *fp, const git_time *intime)
278 t = (time_t)intime->time;
279 if (!(intm = gmtime(&t)))
281 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
286 printtime(FILE *fp, const git_time *intime)
292 t = (time_t)intime->time + (intime->offset * 60);
293 if (!(intm = gmtime(&t)))
295 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
296 if (intime->offset < 0)
297 fprintf(fp, "%s -%02d%02d", out,
298 -(intime->offset) / 60, -(intime->offset) % 60);
300 fprintf(fp, "%s +%02d%02d", out,
301 intime->offset / 60, intime->offset % 60);
305 printtimeshort(FILE *fp, const git_time *intime)
311 t = (time_t)intime->time;
312 if (!(intm = gmtime(&t)))
314 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
319 writeheader(FILE *fp, const char *title)
321 fputs("<!DOCTYPE html>\n"
323 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
325 xmlencode(fp, title, strlen(title));
326 if (title[0] && strippedname[0])
328 xmlencode(fp, strippedname, strlen(strippedname));
331 xmlencode(fp, description, strlen(description));
332 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
333 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
335 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
336 fputs("</head>\n<body>\n<table><tr><td>", fp);
337 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
339 fputs("</td><td><h1>", fp);
340 xmlencode(fp, strippedname, strlen(strippedname));
341 fputs("</h1><span class=\"desc\">", fp);
342 xmlencode(fp, description, strlen(description));
343 fputs("</span></td></tr>", fp);
345 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
346 xmlencode(fp, cloneurl, strlen(cloneurl));
348 xmlencode(fp, cloneurl, strlen(cloneurl));
349 fputs("</a></td></tr>", fp);
351 fputs("<tr><td></td><td>\n", fp);
352 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
353 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
354 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
356 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
358 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
360 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
361 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
365 writefooter(FILE *fp)
367 fputs("</div>\n</body>\n</html>\n", fp);
371 writeblobhtml(FILE *fp, const git_blob *blob)
373 size_t n = 0, i, prev;
374 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
375 const char *s = git_blob_rawcontent(blob);
376 git_off_t len = git_blob_rawsize(blob);
378 fputs("<pre id=\"blob\">\n", fp);
381 for (i = 0, prev = 0; i < (size_t)len; i++) {
385 fprintf(fp, nfmt, n, n, n);
386 xmlencode(fp, &s[prev], i - prev + 1);
390 if ((len - prev) > 0) {
392 fprintf(fp, nfmt, n, n, n);
393 xmlencode(fp, &s[prev], len - prev);
397 fputs("</pre>\n", fp);
403 printcommit(FILE *fp, struct commitinfo *ci)
405 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
406 relpath, ci->oid, ci->oid);
408 if (ci->parentoid[0])
409 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
410 relpath, ci->parentoid, ci->parentoid);
413 fputs("<b>Author:</b> ", fp);
414 xmlencode(fp, ci->author->name, strlen(ci->author->name));
415 fputs(" <<a href=\"mailto:", fp);
416 xmlencode(fp, ci->author->email, strlen(ci->author->email));
418 xmlencode(fp, ci->author->email, strlen(ci->author->email));
419 fputs("</a>>\n<b>Date:</b> ", fp);
420 printtime(fp, &(ci->author->when));
425 xmlencode(fp, ci->msg, strlen(ci->msg));
431 printshowfile(FILE *fp, struct commitinfo *ci)
433 const git_diff_delta *delta;
434 const git_diff_hunk *hunk;
435 const git_diff_line *line;
437 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
445 if (ci->filecount > 1000 ||
446 ci->ndeltas > 1000 ||
447 ci->addcount > 100000 ||
448 ci->delcount > 100000) {
449 fputs("Diff is too large, output suppressed.\n", fp);
454 fputs("<b>Diffstat:</b>\n<table>", fp);
455 for (i = 0; i < ci->ndeltas; i++) {
456 delta = git_patch_get_delta(ci->deltas[i]->patch);
457 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
458 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
459 if (strcmp(delta->old_file.path, delta->new_file.path)) {
460 fputs(" -> ", fp);
461 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
464 add = ci->deltas[i]->addcount;
465 del = ci->deltas[i]->delcount;
467 total = sizeof(linestr) - 2;
468 if (changed > total) {
470 add = ((float)total / changed * add) + 1;
472 del = ((float)total / changed * del) + 1;
474 memset(&linestr, '+', add);
475 memset(&linestr[add], '-', del);
477 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
478 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
479 fwrite(&linestr, 1, add, fp);
480 fputs("</span><span class=\"d\">", fp);
481 fwrite(&linestr[add], 1, del, fp);
482 fputs("</span></td></tr>\n", fp);
484 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
485 ci->filecount, ci->filecount == 1 ? "" : "s",
486 ci->addcount, ci->addcount == 1 ? "" : "s",
487 ci->delcount, ci->delcount == 1 ? "" : "s");
491 for (i = 0; i < ci->ndeltas; i++) {
492 patch = ci->deltas[i]->patch;
493 delta = git_patch_get_delta(patch);
494 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",
495 i, relpath, delta->old_file.path, delta->old_file.path,
496 relpath, delta->new_file.path, delta->new_file.path);
498 /* check binary data */
499 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
500 fputs("Binary files differ.\n", fp);
504 nhunks = git_patch_num_hunks(patch);
505 for (j = 0; j < nhunks; j++) {
506 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
509 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
510 xmlencode(fp, hunk->header, hunk->header_len);
514 if (git_patch_get_line_in_hunk(&line, patch, j, k))
516 if (line->old_lineno == -1)
517 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
519 else if (line->new_lineno == -1)
520 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
524 xmlencode(fp, line->content, line->content_len);
525 if (line->old_lineno == -1 || line->new_lineno == -1)
533 writelogline(FILE *fp, struct commitinfo *ci)
535 fputs("<tr><td>", fp);
537 printtimeshort(fp, &(ci->author->when));
538 fputs("</td><td>", fp);
540 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
541 xmlencode(fp, ci->summary, strlen(ci->summary));
544 fputs("</td><td>", fp);
546 xmlencode(fp, ci->author->name, strlen(ci->author->name));
547 fputs("</td><td class=\"num\" align=\"right\">", fp);
548 fprintf(fp, "%zu", ci->filecount);
549 fputs("</td><td class=\"num\" align=\"right\">", fp);
550 fprintf(fp, "+%zu", ci->addcount);
551 fputs("</td><td class=\"num\" align=\"right\">", fp);
552 fprintf(fp, "-%zu", ci->delcount);
553 fputs("</td></tr>\n", fp);
557 writelog(FILE *fp, const git_oid *oid)
559 struct commitinfo *ci;
560 git_revwalk *w = NULL;
562 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
566 git_revwalk_new(&w, repo);
567 git_revwalk_push(w, oid);
568 git_revwalk_sorting(w, GIT_SORT_TIME);
569 git_revwalk_simplify_first_parent(w);
571 while (!git_revwalk_next(&id, w)) {
574 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
577 git_oid_tostr(oidstr, sizeof(oidstr), &id);
578 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
579 if (r == -1 || (size_t)r >= sizeof(path))
580 errx(1, "path truncated: 'commit/%s.html'", oidstr);
581 r = access(path, F_OK);
583 /* optimization: if there are no log lines to write and
584 the commit file already exists: skip the diffstat */
585 if (!nlogcommits && !r)
588 if (!(ci = commitinfo_getbyoid(&id)))
590 /* diffstat: for stagit HTML required for the log.html line */
591 if (commitinfo_getstats(ci) == -1)
594 if (nlogcommits < 0) {
595 writelogline(fp, ci);
596 } else if (nlogcommits > 0) {
597 writelogline(fp, ci);
602 writelogline(wcachefp, ci);
604 /* check if file exists if so skip it */
607 fpfile = efopen(path, "w");
608 writeheader(fpfile, ci->summary);
609 fputs("<pre>", fpfile);
610 printshowfile(fpfile, ci);
611 fputs("</pre>\n", fpfile);
626 printcommitatom(FILE *fp, struct commitinfo *ci)
628 fputs("<entry>\n", fp);
630 fprintf(fp, "<id>%s</id>\n", ci->oid);
632 fputs("<published>", fp);
633 printtimez(fp, &(ci->author->when));
634 fputs("</published>\n", fp);
637 fputs("<updated>", fp);
638 printtimez(fp, &(ci->committer->when));
639 fputs("</updated>\n", fp);
642 fputs("<title type=\"text\">", fp);
643 xmlencode(fp, ci->summary, strlen(ci->summary));
644 fputs("</title>\n", fp);
646 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
650 fputs("<author><name>", fp);
651 xmlencode(fp, ci->author->name, strlen(ci->author->name));
652 fputs("</name>\n<email>", fp);
653 xmlencode(fp, ci->author->email, strlen(ci->author->email));
654 fputs("</email>\n</author>\n", fp);
657 fputs("<content type=\"text\">", fp);
658 fprintf(fp, "commit %s\n", ci->oid);
659 if (ci->parentoid[0])
660 fprintf(fp, "parent %s\n", ci->parentoid);
662 fputs("Author: ", fp);
663 xmlencode(fp, ci->author->name, strlen(ci->author->name));
665 xmlencode(fp, ci->author->email, strlen(ci->author->email));
666 fputs(">\nDate: ", fp);
667 printtime(fp, &(ci->author->when));
672 xmlencode(fp, ci->msg, strlen(ci->msg));
674 fputs("\n</content>\n</entry>\n", fp);
680 struct commitinfo *ci;
681 git_revwalk *w = NULL;
683 size_t i, m = 100; /* last 'm' commits */
685 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
686 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
687 xmlencode(fp, strippedname, strlen(strippedname));
688 fputs(", branch HEAD</title>\n<subtitle>", fp);
689 xmlencode(fp, description, strlen(description));
690 fputs("</subtitle>\n", fp);
692 git_revwalk_new(&w, repo);
693 git_revwalk_push_head(w);
694 git_revwalk_sorting(w, GIT_SORT_TIME);
695 git_revwalk_simplify_first_parent(w);
697 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
698 if (!(ci = commitinfo_getbyoid(&id)))
700 printcommitatom(fp, ci);
705 fputs("</feed>\n", fp);
711 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
713 char tmp[PATH_MAX] = "", *d;
718 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
719 errx(1, "path truncated: '%s'", fpath);
720 if (!(d = dirname(tmp)))
725 for (p = fpath, tmp[0] = '\0'; *p; p++) {
726 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
727 errx(1, "path truncated: '../%s'", tmp);
731 fp = efopen(fpath, "w");
732 writeheader(fp, filename);
734 xmlencode(fp, filename, strlen(filename));
735 fprintf(fp, " (%juB)", (uintmax_t)filesize);
736 fputs("</p><hr/>", fp);
738 if (git_blob_is_binary((git_blob *)obj)) {
739 fputs("<p>Binary file.</p>\n", fp);
741 lc = writeblobhtml(fp, (git_blob *)obj);
754 filemode(git_filemode_t m)
756 static char mode[11];
758 memset(mode, '-', sizeof(mode) - 1);
769 else if (S_ISFIFO(m))
773 else if (S_ISSOCK(m))
778 if (m & S_IRUSR) mode[1] = 'r';
779 if (m & S_IWUSR) mode[2] = 'w';
780 if (m & S_IXUSR) mode[3] = 'x';
781 if (m & S_IRGRP) mode[4] = 'r';
782 if (m & S_IWGRP) mode[5] = 'w';
783 if (m & S_IXGRP) mode[6] = 'x';
784 if (m & S_IROTH) mode[7] = 'r';
785 if (m & S_IWOTH) mode[8] = 'w';
786 if (m & S_IXOTH) mode[9] = 'x';
788 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
789 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
790 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
796 writefilestree(FILE *fp, git_tree *tree, const char *path)
798 const git_tree_entry *entry = NULL;
799 git_submodule *module = NULL;
800 git_object *obj = NULL;
802 const char *entryname;
803 char filepath[PATH_MAX], entrypath[PATH_MAX];
807 count = git_tree_entrycount(tree);
808 for (i = 0; i < count; i++) {
809 if (!(entry = git_tree_entry_byindex(tree, i)) ||
810 !(entryname = git_tree_entry_name(entry)))
812 joinpath(entrypath, sizeof(entrypath), path, entryname);
814 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
816 if (r == -1 || (size_t)r >= sizeof(filepath))
817 errx(1, "path truncated: 'file/%s.html'", entrypath);
819 if (!git_tree_entry_to_object(&obj, repo, entry)) {
820 switch (git_object_type(obj)) {
825 ret = writefilestree(fp, (git_tree *)obj,
827 git_object_free(obj);
832 git_object_free(obj);
836 filesize = git_blob_rawsize((git_blob *)obj);
837 lc = writeblob(obj, filepath, entryname, filesize);
839 fputs("<tr><td>", fp);
840 fputs(filemode(git_tree_entry_filemode(entry)), fp);
841 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
842 xmlencode(fp, entrypath, strlen(entrypath));
843 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
845 fprintf(fp, "%dL", lc);
847 fprintf(fp, "%juB", (uintmax_t)filesize);
848 fputs("</td></tr>\n", fp);
849 git_object_free(obj);
850 } else if (!git_submodule_lookup(&module, repo, entryname)) {
851 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
853 xmlencode(fp, entrypath, strlen(entrypath));
854 git_submodule_free(module);
855 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
863 writefiles(FILE *fp, const git_oid *id)
865 git_tree *tree = NULL;
866 git_commit *commit = NULL;
869 fputs("<table id=\"files\"><thead>\n<tr>"
870 "<td><b>Mode</b></td><td><b>Name</b></td>"
871 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
872 "</tr>\n</thead><tbody>\n", fp);
874 if (!git_commit_lookup(&commit, repo, id) &&
875 !git_commit_tree(&tree, commit))
876 ret = writefilestree(fp, tree, "");
878 fputs("</tbody></table>", fp);
880 git_commit_free(commit);
887 refs_cmp(const void *v1, const void *v2)
889 git_reference *r1 = (*(git_reference **)v1);
890 git_reference *r2 = (*(git_reference **)v2);
893 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
896 return strcmp(git_reference_shorthand(r1),
897 git_reference_shorthand(r2));
903 struct commitinfo *ci;
904 const git_oid *id = NULL;
905 git_object *obj = NULL;
906 git_reference *dref = NULL, *r, *ref = NULL;
907 git_reference_iterator *it = NULL;
908 git_reference **refs = NULL;
909 size_t count, i, j, refcount;
910 const char *titles[] = { "Branches", "Tags" };
911 const char *ids[] = { "branches", "tags" };
914 if (git_reference_iterator_new(&it, repo))
917 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
918 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
920 refs[refcount] = ref;
922 git_reference_iterator_free(it);
924 /* sort by type then shorthand name */
925 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
927 for (j = 0; j < 2; j++) {
928 for (i = 0, count = 0; i < refcount; i++) {
929 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
930 !(git_reference_is_tag(refs[i]) && j == 1))
933 switch (git_reference_type(refs[i])) {
934 case GIT_REF_SYMBOLIC:
935 if (git_reference_resolve(&dref, refs[i]))
945 if (!git_reference_target(r) ||
946 git_reference_peel(&obj, r, GIT_OBJ_ANY))
948 if (!(id = git_object_id(obj)))
950 if (!(ci = commitinfo_getbyoid(id)))
953 /* print header if it has an entry (first). */
955 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
956 "<thead>\n<tr><td><b>Name</b></td>"
957 "<td><b>Last commit date</b></td>"
958 "<td><b>Author</b></td>\n</tr>\n"
964 name = git_reference_shorthand(r);
966 fputs("<tr><td>", fp);
967 xmlencode(fp, name, strlen(name));
968 fputs("</td><td>", fp);
970 printtimeshort(fp, &(ci->author->when));
971 fputs("</td><td>", fp);
973 xmlencode(fp, ci->author->name, strlen(ci->author->name));
974 fputs("</td></tr>\n", fp);
979 git_object_free(obj);
981 git_reference_free(dref);
986 fputs("</tbody></table><br/>", fp);
990 git_object_free(obj);
991 git_reference_free(dref);
993 for (i = 0; i < refcount; i++)
994 git_reference_free(refs[i]);
1003 fprintf(stderr, "%s [-c cachefile] [-l commits] repodir\n", argv0);
1008 main(int argc, char *argv[])
1010 git_object *obj = NULL;
1011 const git_oid *head = NULL;
1012 const git_error *e = NULL;
1015 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1016 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1020 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1023 for (i = 1; i < argc; i++) {
1024 if (argv[i][0] != '-') {
1028 } else if (argv[i][1] == 'c') {
1029 if (nlogcommits > 0 || i + 1 >= argc)
1031 cachefile = argv[++i];
1032 } else if (argv[i][1] == 'l') {
1033 if (cachefile || i + 1 >= argc)
1036 nlogcommits = strtoll(argv[++i], &p, 10);
1037 if (argv[i][0] == '\0' || *p != '\0' ||
1040 if (errno == ERANGE && (nlogcommits == LLONG_MAX ||
1041 nlogcommits == LLONG_MIN))
1045 if (!cachefile && pledge("stdio rpath wpath cpath", NULL) == -1)
1050 if (!realpath(repodir, repodirabs))
1055 if (git_repository_open_ext(&repo, repodir,
1056 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1058 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1063 if (!git_revparse_single(&obj, repo, "HEAD"))
1064 head = git_object_id(obj);
1065 git_object_free(obj);
1067 /* use directory name as name */
1068 if ((name = strrchr(repodirabs, '/')))
1073 /* strip .git suffix */
1074 if (!(strippedname = strdup(name)))
1076 if ((p = strrchr(strippedname, '.')))
1077 if (!strcmp(p, ".git"))
1080 /* read description or .git/description */
1081 joinpath(path, sizeof(path), repodir, "description");
1082 if (!(fpread = fopen(path, "r"))) {
1083 joinpath(path, sizeof(path), repodir, ".git/description");
1084 fpread = fopen(path, "r");
1087 if (!fgets(description, sizeof(description), fpread))
1088 description[0] = '\0';
1092 /* read url or .git/url */
1093 joinpath(path, sizeof(path), repodir, "url");
1094 if (!(fpread = fopen(path, "r"))) {
1095 joinpath(path, sizeof(path), repodir, ".git/url");
1096 fpread = fopen(path, "r");
1099 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1101 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1106 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1107 git_object_type(obj) == GIT_OBJ_BLOB);
1108 git_object_free(obj);
1111 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1112 git_object_type(obj) == GIT_OBJ_BLOB);
1113 git_object_free(obj);
1115 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1116 git_object_type(obj) == GIT_OBJ_BLOB);
1117 git_object_free(obj);
1120 fp = efopen("log.html", "w");
1122 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1123 writeheader(fp, "Log");
1124 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1125 "<td><b>Commit message</b></td>"
1126 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1127 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1128 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1130 if (cachefile && head) {
1131 /* read from cache file (does not need to exist) */
1132 if ((rcachefp = fopen(cachefile, "r"))) {
1133 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1134 errx(1, "%s: no object id", cachefile);
1135 if (git_oid_fromstr(&lastoid, lastoidstr))
1136 errx(1, "%s: invalid object id", cachefile);
1139 /* write log to (temporary) cache */
1140 if ((fd = mkstemp(tmppath)) == -1)
1142 if (!(wcachefp = fdopen(fd, "w")))
1143 err(1, "fdopen: '%s'", tmppath);
1144 /* write last commit id (HEAD) */
1145 git_oid_tostr(buf, sizeof(buf), head);
1146 fprintf(wcachefp, "%s\n", buf);
1151 /* append previous log to log.html and the new cache */
1152 while (!feof(rcachefp)) {
1153 n = fread(buf, 1, sizeof(buf), rcachefp);
1154 if (ferror(rcachefp))
1156 if (fwrite(buf, 1, n, fp) != n ||
1157 fwrite(buf, 1, n, wcachefp) != n)
1168 fputs("</tbody></table>", fp);
1172 /* files for HEAD */
1173 fp = efopen("files.html", "w");
1174 writeheader(fp, "Files");
1176 writefiles(fp, head);
1180 /* summary page with branches and tags */
1181 fp = efopen("refs.html", "w");
1182 writeheader(fp, "Refs");
1188 fp = efopen("atom.xml", "w");
1192 /* rename new cache file on success */
1193 if (cachefile && head) {
1194 if (rename(tmppath, cachefile))
1195 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1196 umask((mask = umask(0)));
1197 if (chmod(cachefile,
1198 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1199 err(1, "chmod: '%s'", cachefile);
1203 git_repository_free(repo);
1204 git_libgit2_shutdown();