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 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
73 r = snprintf(buf, bufsiz, "%s%s%s",
74 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
75 if (r == -1 || (size_t)r >= bufsiz)
76 errx(1, "path truncated: '%s%s%s'",
77 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
81 deltainfo_free(struct deltainfo *di)
85 git_patch_free(di->patch);
86 memset(di, 0, sizeof(*di));
91 commitinfo_getstats(struct commitinfo *ci)
94 git_diff_options opts;
95 const git_diff_delta *delta;
96 const git_diff_hunk *hunk;
97 const git_diff_line *line;
98 git_patch *patch = NULL;
99 size_t ndeltas, nhunks, nhunklines;
102 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
104 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
105 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
107 ci->parent_tree = NULL;
111 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
112 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
113 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
116 ndeltas = git_diff_num_deltas(ci->diff);
117 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
120 for (i = 0; i < ndeltas; i++) {
121 if (git_patch_from_diff(&patch, ci->diff, i))
123 if (!(di = calloc(1, sizeof(struct deltainfo))))
128 delta = git_patch_get_delta(patch);
130 /* skip stats for binary data */
131 if (delta->flags & GIT_DIFF_FLAG_BINARY)
134 nhunks = git_patch_num_hunks(patch);
135 for (j = 0; j < nhunks; j++) {
136 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
139 if (git_patch_get_line_in_hunk(&line, patch, j, k))
141 if (line->old_lineno == -1) {
144 } else if (line->new_lineno == -1) {
157 git_diff_free(ci->diff);
159 git_tree_free(ci->commit_tree);
160 ci->commit_tree = NULL;
161 git_tree_free(ci->parent_tree);
162 ci->parent_tree = NULL;
163 git_commit_free(ci->parent);
167 for (i = 0; i < ci->ndeltas; i++)
168 deltainfo_free(ci->deltas[i]);
180 commitinfo_free(struct commitinfo *ci)
187 for (i = 0; i < ci->ndeltas; i++)
188 deltainfo_free(ci->deltas[i]);
191 git_diff_free(ci->diff);
192 git_tree_free(ci->commit_tree);
193 git_tree_free(ci->parent_tree);
194 git_commit_free(ci->commit);
195 git_commit_free(ci->parent);
196 memset(ci, 0, sizeof(*ci));
201 commitinfo_getbyoid(const git_oid *id)
203 struct commitinfo *ci;
205 if (!(ci = calloc(1, sizeof(struct commitinfo))))
208 if (git_commit_lookup(&(ci->commit), repo, id))
212 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
213 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
215 ci->author = git_commit_author(ci->commit);
216 ci->committer = git_commit_committer(ci->commit);
217 ci->summary = git_commit_summary(ci->commit);
218 ci->msg = git_commit_message(ci->commit);
229 efopen(const char *name, const char *flags)
233 if (!(fp = fopen(name, flags)))
234 err(1, "fopen: '%s'", name);
239 /* Escape characters below as HTML 2.0 / XML 1.0. */
241 xmlencode(FILE *fp, const char *s, size_t len)
245 for (i = 0; *s && i < len; s++, i++) {
247 case '<': fputs("<", fp); break;
248 case '>': fputs(">", fp); break;
249 case '\'': fputs("'", fp); break;
250 case '&': fputs("&", fp); break;
251 case '"': fputs(""", fp); break;
252 default: fputc(*s, fp);
258 mkdirp(const char *path)
260 char tmp[PATH_MAX], *p;
262 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
263 errx(1, "path truncated: '%s'", path);
264 for (p = tmp + (tmp[0] == '/'); *p; p++) {
268 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
272 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
278 printtimez(FILE *fp, const git_time *intime)
284 t = (time_t)intime->time;
285 if (!(intm = gmtime(&t)))
287 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
292 printtime(FILE *fp, const git_time *intime)
298 t = (time_t)intime->time + (intime->offset * 60);
299 if (!(intm = gmtime(&t)))
301 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
302 if (intime->offset < 0)
303 fprintf(fp, "%s -%02d%02d", out,
304 -(intime->offset) / 60, -(intime->offset) % 60);
306 fprintf(fp, "%s +%02d%02d", out,
307 intime->offset / 60, intime->offset % 60);
311 printtimeshort(FILE *fp, const git_time *intime)
317 t = (time_t)intime->time;
318 if (!(intm = gmtime(&t)))
320 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
325 writeheader(FILE *fp, const char *title)
327 fputs("<!DOCTYPE html>\n"
329 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
331 xmlencode(fp, title, strlen(title));
332 if (title[0] && strippedname[0])
334 xmlencode(fp, strippedname, strlen(strippedname));
337 xmlencode(fp, description, strlen(description));
338 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
339 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
341 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
342 fputs("</head>\n<body>\n<table><tr><td>", fp);
343 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
345 fputs("</td><td><h1>", fp);
346 xmlencode(fp, strippedname, strlen(strippedname));
347 fputs("</h1><span class=\"desc\">", fp);
348 xmlencode(fp, description, strlen(description));
349 fputs("</span></td></tr>", fp);
351 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
352 xmlencode(fp, cloneurl, strlen(cloneurl));
354 xmlencode(fp, cloneurl, strlen(cloneurl));
355 fputs("</a></td></tr>", fp);
357 fputs("<tr><td></td><td>\n", fp);
358 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
359 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
360 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
362 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
364 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
366 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
367 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
371 writefooter(FILE *fp)
373 fputs("</div>\n</body>\n</html>\n", fp);
377 writeblobhtml(FILE *fp, const git_blob *blob)
379 size_t n = 0, i, prev;
380 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
381 const char *s = git_blob_rawcontent(blob);
382 git_off_t len = git_blob_rawsize(blob);
384 fputs("<pre id=\"blob\">\n", fp);
387 for (i = 0, prev = 0; i < (size_t)len; i++) {
391 fprintf(fp, nfmt, n, n, n);
392 xmlencode(fp, &s[prev], i - prev + 1);
396 if ((len - prev) > 0) {
398 fprintf(fp, nfmt, n, n, n);
399 xmlencode(fp, &s[prev], len - prev);
403 fputs("</pre>\n", fp);
409 printcommit(FILE *fp, struct commitinfo *ci)
411 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
412 relpath, ci->oid, ci->oid);
414 if (ci->parentoid[0])
415 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
416 relpath, ci->parentoid, ci->parentoid);
419 fputs("<b>Author:</b> ", fp);
420 xmlencode(fp, ci->author->name, strlen(ci->author->name));
421 fputs(" <<a href=\"mailto:", fp);
422 xmlencode(fp, ci->author->email, strlen(ci->author->email));
424 xmlencode(fp, ci->author->email, strlen(ci->author->email));
425 fputs("</a>>\n<b>Date:</b> ", fp);
426 printtime(fp, &(ci->author->when));
431 xmlencode(fp, ci->msg, strlen(ci->msg));
437 printshowfile(FILE *fp, struct commitinfo *ci)
439 const git_diff_delta *delta;
440 const git_diff_hunk *hunk;
441 const git_diff_line *line;
443 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
451 if (ci->filecount > 1000 ||
452 ci->ndeltas > 1000 ||
453 ci->addcount > 100000 ||
454 ci->delcount > 100000) {
455 fputs("Diff is too large, output suppressed.\n", fp);
460 fputs("<b>Diffstat:</b>\n<table>", fp);
461 for (i = 0; i < ci->ndeltas; i++) {
462 delta = git_patch_get_delta(ci->deltas[i]->patch);
463 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
464 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
465 if (strcmp(delta->old_file.path, delta->new_file.path)) {
466 fputs(" -> ", fp);
467 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
470 add = ci->deltas[i]->addcount;
471 del = ci->deltas[i]->delcount;
473 total = sizeof(linestr) - 2;
474 if (changed > total) {
476 add = ((float)total / changed * add) + 1;
478 del = ((float)total / changed * del) + 1;
480 memset(&linestr, '+', add);
481 memset(&linestr[add], '-', del);
483 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
484 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
485 fwrite(&linestr, 1, add, fp);
486 fputs("</span><span class=\"d\">", fp);
487 fwrite(&linestr[add], 1, del, fp);
488 fputs("</span></td></tr>\n", fp);
490 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
491 ci->filecount, ci->filecount == 1 ? "" : "s",
492 ci->addcount, ci->addcount == 1 ? "" : "s",
493 ci->delcount, ci->delcount == 1 ? "" : "s");
497 for (i = 0; i < ci->ndeltas; i++) {
498 patch = ci->deltas[i]->patch;
499 delta = git_patch_get_delta(patch);
500 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",
501 i, relpath, delta->old_file.path, delta->old_file.path,
502 relpath, delta->new_file.path, delta->new_file.path);
504 /* check binary data */
505 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
506 fputs("Binary files differ.\n", fp);
510 nhunks = git_patch_num_hunks(patch);
511 for (j = 0; j < nhunks; j++) {
512 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
515 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
516 xmlencode(fp, hunk->header, hunk->header_len);
520 if (git_patch_get_line_in_hunk(&line, patch, j, k))
522 if (line->old_lineno == -1)
523 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
525 else if (line->new_lineno == -1)
526 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
530 xmlencode(fp, line->content, line->content_len);
531 if (line->old_lineno == -1 || line->new_lineno == -1)
539 writelogline(FILE *fp, struct commitinfo *ci)
541 fputs("<tr><td>", fp);
543 printtimeshort(fp, &(ci->author->when));
544 fputs("</td><td>", fp);
546 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
547 xmlencode(fp, ci->summary, strlen(ci->summary));
550 fputs("</td><td>", fp);
552 xmlencode(fp, ci->author->name, strlen(ci->author->name));
553 fputs("</td><td class=\"num\" align=\"right\">", fp);
554 fprintf(fp, "%zu", ci->filecount);
555 fputs("</td><td class=\"num\" align=\"right\">", fp);
556 fprintf(fp, "+%zu", ci->addcount);
557 fputs("</td><td class=\"num\" align=\"right\">", fp);
558 fprintf(fp, "-%zu", ci->delcount);
559 fputs("</td></tr>\n", fp);
563 writelog(FILE *fp, const git_oid *oid)
565 struct commitinfo *ci;
566 git_revwalk *w = NULL;
568 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
572 git_revwalk_new(&w, repo);
573 git_revwalk_push(w, oid);
574 git_revwalk_sorting(w, GIT_SORT_TIME);
575 git_revwalk_simplify_first_parent(w);
577 while (!git_revwalk_next(&id, w)) {
580 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
583 git_oid_tostr(oidstr, sizeof(oidstr), &id);
584 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
585 if (r == -1 || (size_t)r >= sizeof(path))
586 errx(1, "path truncated: 'commit/%s.html'", oidstr);
587 r = access(path, F_OK);
589 /* optimization: if there are no log lines to write and
590 the commit file already exists: skip the diffstat */
591 if (!nlogcommits && !r)
594 if (!(ci = commitinfo_getbyoid(&id)))
596 /* diffstat: for stagit HTML required for the log.html line */
597 if (commitinfo_getstats(ci) == -1)
600 if (nlogcommits < 0) {
601 writelogline(fp, ci);
602 } else if (nlogcommits > 0) {
603 writelogline(fp, ci);
605 if (!nlogcommits && ci->parentoid[0])
606 fputs("<tr><td></td><td colspan=\"5\">"
607 "More commits remaining [...]</td>"
612 writelogline(wcachefp, ci);
614 /* check if file exists if so skip it */
617 fpfile = efopen(path, "w");
618 writeheader(fpfile, ci->summary);
619 fputs("<pre>", fpfile);
620 printshowfile(fpfile, ci);
621 fputs("</pre>\n", fpfile);
636 printcommitatom(FILE *fp, struct commitinfo *ci)
638 fputs("<entry>\n", fp);
640 fprintf(fp, "<id>%s</id>\n", ci->oid);
642 fputs("<published>", fp);
643 printtimez(fp, &(ci->author->when));
644 fputs("</published>\n", fp);
647 fputs("<updated>", fp);
648 printtimez(fp, &(ci->committer->when));
649 fputs("</updated>\n", fp);
652 fputs("<title type=\"text\">", fp);
653 xmlencode(fp, ci->summary, strlen(ci->summary));
654 fputs("</title>\n", fp);
656 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
660 fputs("<author><name>", fp);
661 xmlencode(fp, ci->author->name, strlen(ci->author->name));
662 fputs("</name>\n<email>", fp);
663 xmlencode(fp, ci->author->email, strlen(ci->author->email));
664 fputs("</email>\n</author>\n", fp);
667 fputs("<content type=\"text\">", fp);
668 fprintf(fp, "commit %s\n", ci->oid);
669 if (ci->parentoid[0])
670 fprintf(fp, "parent %s\n", ci->parentoid);
672 fputs("Author: ", fp);
673 xmlencode(fp, ci->author->name, strlen(ci->author->name));
675 xmlencode(fp, ci->author->email, strlen(ci->author->email));
676 fputs(">\nDate: ", fp);
677 printtime(fp, &(ci->author->when));
682 xmlencode(fp, ci->msg, strlen(ci->msg));
684 fputs("\n</content>\n</entry>\n", fp);
690 struct commitinfo *ci;
691 git_revwalk *w = NULL;
693 size_t i, m = 100; /* last 'm' commits */
695 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
696 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
697 xmlencode(fp, strippedname, strlen(strippedname));
698 fputs(", branch HEAD</title>\n<subtitle>", fp);
699 xmlencode(fp, description, strlen(description));
700 fputs("</subtitle>\n", fp);
702 git_revwalk_new(&w, repo);
703 git_revwalk_push_head(w);
704 git_revwalk_sorting(w, GIT_SORT_TIME);
705 git_revwalk_simplify_first_parent(w);
707 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
708 if (!(ci = commitinfo_getbyoid(&id)))
710 printcommitatom(fp, ci);
715 fputs("</feed>\n", fp);
721 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
723 char tmp[PATH_MAX] = "", *d;
728 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
729 errx(1, "path truncated: '%s'", fpath);
730 if (!(d = dirname(tmp)))
735 for (p = fpath, tmp[0] = '\0'; *p; p++) {
736 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
737 errx(1, "path truncated: '../%s'", tmp);
741 fp = efopen(fpath, "w");
742 writeheader(fp, filename);
744 xmlencode(fp, filename, strlen(filename));
745 fprintf(fp, " (%juB)", (uintmax_t)filesize);
746 fputs("</p><hr/>", fp);
748 if (git_blob_is_binary((git_blob *)obj)) {
749 fputs("<p>Binary file.</p>\n", fp);
751 lc = writeblobhtml(fp, (git_blob *)obj);
764 filemode(git_filemode_t m)
766 static char mode[11];
768 memset(mode, '-', sizeof(mode) - 1);
779 else if (S_ISFIFO(m))
783 else if (S_ISSOCK(m))
788 if (m & S_IRUSR) mode[1] = 'r';
789 if (m & S_IWUSR) mode[2] = 'w';
790 if (m & S_IXUSR) mode[3] = 'x';
791 if (m & S_IRGRP) mode[4] = 'r';
792 if (m & S_IWGRP) mode[5] = 'w';
793 if (m & S_IXGRP) mode[6] = 'x';
794 if (m & S_IROTH) mode[7] = 'r';
795 if (m & S_IWOTH) mode[8] = 'w';
796 if (m & S_IXOTH) mode[9] = 'x';
798 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
799 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
800 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
806 writefilestree(FILE *fp, git_tree *tree, const char *path)
808 const git_tree_entry *entry = NULL;
809 git_submodule *module = NULL;
810 git_object *obj = NULL;
812 const char *entryname;
813 char filepath[PATH_MAX], entrypath[PATH_MAX];
817 count = git_tree_entrycount(tree);
818 for (i = 0; i < count; i++) {
819 if (!(entry = git_tree_entry_byindex(tree, i)) ||
820 !(entryname = git_tree_entry_name(entry)))
822 joinpath(entrypath, sizeof(entrypath), path, entryname);
824 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
826 if (r == -1 || (size_t)r >= sizeof(filepath))
827 errx(1, "path truncated: 'file/%s.html'", entrypath);
829 if (!git_tree_entry_to_object(&obj, repo, entry)) {
830 switch (git_object_type(obj)) {
835 ret = writefilestree(fp, (git_tree *)obj,
837 git_object_free(obj);
842 git_object_free(obj);
846 filesize = git_blob_rawsize((git_blob *)obj);
847 lc = writeblob(obj, filepath, entryname, filesize);
849 fputs("<tr><td>", fp);
850 fputs(filemode(git_tree_entry_filemode(entry)), fp);
851 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
852 xmlencode(fp, entrypath, strlen(entrypath));
853 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
855 fprintf(fp, "%dL", lc);
857 fprintf(fp, "%juB", (uintmax_t)filesize);
858 fputs("</td></tr>\n", fp);
859 git_object_free(obj);
860 } else if (!git_submodule_lookup(&module, repo, entryname)) {
861 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
863 xmlencode(fp, entrypath, strlen(entrypath));
864 git_submodule_free(module);
865 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
873 writefiles(FILE *fp, const git_oid *id)
875 git_tree *tree = NULL;
876 git_commit *commit = NULL;
879 fputs("<table id=\"files\"><thead>\n<tr>"
880 "<td><b>Mode</b></td><td><b>Name</b></td>"
881 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
882 "</tr>\n</thead><tbody>\n", fp);
884 if (!git_commit_lookup(&commit, repo, id) &&
885 !git_commit_tree(&tree, commit))
886 ret = writefilestree(fp, tree, "");
888 fputs("</tbody></table>", fp);
890 git_commit_free(commit);
897 refs_cmp(const void *v1, const void *v2)
899 git_reference *r1 = (*(git_reference **)v1);
900 git_reference *r2 = (*(git_reference **)v2);
903 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
906 return strcmp(git_reference_shorthand(r1),
907 git_reference_shorthand(r2));
913 struct commitinfo *ci;
914 const git_oid *id = NULL;
915 git_object *obj = NULL;
916 git_reference *dref = NULL, *r, *ref = NULL;
917 git_reference_iterator *it = NULL;
918 git_reference **refs = NULL;
919 size_t count, i, j, refcount;
920 const char *titles[] = { "Branches", "Tags" };
921 const char *ids[] = { "branches", "tags" };
924 if (git_reference_iterator_new(&it, repo))
927 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
928 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
930 refs[refcount] = ref;
932 git_reference_iterator_free(it);
934 /* sort by type then shorthand name */
935 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
937 for (j = 0; j < 2; j++) {
938 for (i = 0, count = 0; i < refcount; i++) {
939 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
940 !(git_reference_is_tag(refs[i]) && j == 1))
943 switch (git_reference_type(refs[i])) {
944 case GIT_REF_SYMBOLIC:
945 if (git_reference_resolve(&dref, refs[i]))
955 if (!git_reference_target(r) ||
956 git_reference_peel(&obj, r, GIT_OBJ_ANY))
958 if (!(id = git_object_id(obj)))
960 if (!(ci = commitinfo_getbyoid(id)))
963 /* print header if it has an entry (first). */
965 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
966 "<thead>\n<tr><td><b>Name</b></td>"
967 "<td><b>Last commit date</b></td>"
968 "<td><b>Author</b></td>\n</tr>\n"
974 name = git_reference_shorthand(r);
976 fputs("<tr><td>", fp);
977 xmlencode(fp, name, strlen(name));
978 fputs("</td><td>", fp);
980 printtimeshort(fp, &(ci->author->when));
981 fputs("</td><td>", fp);
983 xmlencode(fp, ci->author->name, strlen(ci->author->name));
984 fputs("</td></tr>\n", fp);
989 git_object_free(obj);
991 git_reference_free(dref);
996 fputs("</tbody></table><br/>", fp);
1000 git_object_free(obj);
1001 git_reference_free(dref);
1003 for (i = 0; i < refcount; i++)
1004 git_reference_free(refs[i]);
1013 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1018 main(int argc, char *argv[])
1020 git_object *obj = NULL;
1021 const git_oid *head = NULL;
1022 const git_error *e = NULL;
1025 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1026 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1030 for (i = 1; i < argc; i++) {
1031 if (argv[i][0] != '-') {
1035 } else if (argv[i][1] == 'c') {
1036 if (nlogcommits > 0 || i + 1 >= argc)
1038 cachefile = argv[++i];
1039 } else if (argv[i][1] == 'l') {
1040 if (cachefile || i + 1 >= argc)
1043 nlogcommits = strtoll(argv[++i], &p, 10);
1044 if (argv[i][0] == '\0' || *p != '\0' ||
1047 if (errno == ERANGE && (nlogcommits == LLONG_MAX ||
1048 nlogcommits == LLONG_MIN))
1055 if (!realpath(repodir, repodirabs))
1062 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1065 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1070 if (git_repository_open_ext(&repo, repodir,
1071 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1073 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1078 if (!git_revparse_single(&obj, repo, "HEAD"))
1079 head = git_object_id(obj);
1080 git_object_free(obj);
1082 /* use directory name as name */
1083 if ((name = strrchr(repodirabs, '/')))
1088 /* strip .git suffix */
1089 if (!(strippedname = strdup(name)))
1091 if ((p = strrchr(strippedname, '.')))
1092 if (!strcmp(p, ".git"))
1095 /* read description or .git/description */
1096 joinpath(path, sizeof(path), repodir, "description");
1097 if (!(fpread = fopen(path, "r"))) {
1098 joinpath(path, sizeof(path), repodir, ".git/description");
1099 fpread = fopen(path, "r");
1102 if (!fgets(description, sizeof(description), fpread))
1103 description[0] = '\0';
1107 /* read url or .git/url */
1108 joinpath(path, sizeof(path), repodir, "url");
1109 if (!(fpread = fopen(path, "r"))) {
1110 joinpath(path, sizeof(path), repodir, ".git/url");
1111 fpread = fopen(path, "r");
1114 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1116 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1121 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1122 git_object_type(obj) == GIT_OBJ_BLOB);
1123 git_object_free(obj);
1126 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1127 git_object_type(obj) == GIT_OBJ_BLOB);
1128 git_object_free(obj);
1130 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1131 git_object_type(obj) == GIT_OBJ_BLOB);
1132 git_object_free(obj);
1135 fp = efopen("log.html", "w");
1137 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1138 writeheader(fp, "Log");
1139 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1140 "<td><b>Commit message</b></td>"
1141 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1142 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1143 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1145 if (cachefile && head) {
1146 /* read from cache file (does not need to exist) */
1147 if ((rcachefp = fopen(cachefile, "r"))) {
1148 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1149 errx(1, "%s: no object id", cachefile);
1150 if (git_oid_fromstr(&lastoid, lastoidstr))
1151 errx(1, "%s: invalid object id", cachefile);
1154 /* write log to (temporary) cache */
1155 if ((fd = mkstemp(tmppath)) == -1)
1157 if (!(wcachefp = fdopen(fd, "w")))
1158 err(1, "fdopen: '%s'", tmppath);
1159 /* write last commit id (HEAD) */
1160 git_oid_tostr(buf, sizeof(buf), head);
1161 fprintf(wcachefp, "%s\n", buf);
1166 /* append previous log to log.html and the new cache */
1167 while (!feof(rcachefp)) {
1168 n = fread(buf, 1, sizeof(buf), rcachefp);
1169 if (ferror(rcachefp))
1171 if (fwrite(buf, 1, n, fp) != n ||
1172 fwrite(buf, 1, n, wcachefp) != n)
1183 fputs("</tbody></table>", fp);
1187 /* files for HEAD */
1188 fp = efopen("files.html", "w");
1189 writeheader(fp, "Files");
1191 writefiles(fp, head);
1195 /* summary page with branches and tags */
1196 fp = efopen("refs.html", "w");
1197 writeheader(fp, "Refs");
1203 fp = efopen("atom.xml", "w");
1207 /* rename new cache file on success */
1208 if (cachefile && head) {
1209 if (rename(tmppath, cachefile))
1210 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1211 umask((mask = umask(0)));
1212 if (chmod(cachefile,
1213 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1214 err(1, "chmod: '%s'", cachefile);
1218 git_repository_free(repo);
1219 git_libgit2_shutdown();