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 git_diff_find_options fopts;
96 const git_diff_delta *delta;
97 const git_diff_hunk *hunk;
98 const git_diff_line *line;
99 git_patch *patch = NULL;
100 size_t ndeltas, nhunks, nhunklines;
103 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
105 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
106 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
108 ci->parent_tree = NULL;
112 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
113 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
114 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
117 if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
119 fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
120 if (git_diff_find_similar(ci->diff, &fopts))
123 ndeltas = git_diff_num_deltas(ci->diff);
124 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
127 for (i = 0; i < ndeltas; i++) {
128 if (git_patch_from_diff(&patch, ci->diff, i))
131 if (!(di = calloc(1, sizeof(struct deltainfo))))
136 delta = git_patch_get_delta(patch);
138 /* skip stats for binary data */
139 if (delta->flags & GIT_DIFF_FLAG_BINARY)
142 nhunks = git_patch_num_hunks(patch);
143 for (j = 0; j < nhunks; j++) {
144 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
147 if (git_patch_get_line_in_hunk(&line, patch, j, k))
149 if (line->old_lineno == -1) {
152 } else if (line->new_lineno == -1) {
165 git_diff_free(ci->diff);
167 git_tree_free(ci->commit_tree);
168 ci->commit_tree = NULL;
169 git_tree_free(ci->parent_tree);
170 ci->parent_tree = NULL;
171 git_commit_free(ci->parent);
175 for (i = 0; i < ci->ndeltas; i++)
176 deltainfo_free(ci->deltas[i]);
188 commitinfo_free(struct commitinfo *ci)
195 for (i = 0; i < ci->ndeltas; i++)
196 deltainfo_free(ci->deltas[i]);
199 git_diff_free(ci->diff);
200 git_tree_free(ci->commit_tree);
201 git_tree_free(ci->parent_tree);
202 git_commit_free(ci->commit);
203 git_commit_free(ci->parent);
204 memset(ci, 0, sizeof(*ci));
209 commitinfo_getbyoid(const git_oid *id)
211 struct commitinfo *ci;
213 if (!(ci = calloc(1, sizeof(struct commitinfo))))
216 if (git_commit_lookup(&(ci->commit), repo, id))
220 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
221 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
223 ci->author = git_commit_author(ci->commit);
224 ci->committer = git_commit_committer(ci->commit);
225 ci->summary = git_commit_summary(ci->commit);
226 ci->msg = git_commit_message(ci->commit);
237 efopen(const char *name, const char *flags)
241 if (!(fp = fopen(name, flags)))
242 err(1, "fopen: '%s'", name);
247 /* Escape characters below as HTML 2.0 / XML 1.0. */
249 xmlencode(FILE *fp, const char *s, size_t len)
253 for (i = 0; *s && i < len; s++, i++) {
255 case '<': fputs("<", fp); break;
256 case '>': fputs(">", fp); break;
257 case '\'': fputs("'", fp); break;
258 case '&': fputs("&", fp); break;
259 case '"': fputs(""", fp); break;
260 default: fputc(*s, fp);
266 mkdirp(const char *path)
268 char tmp[PATH_MAX], *p;
270 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
271 errx(1, "path truncated: '%s'", path);
272 for (p = tmp + (tmp[0] == '/'); *p; p++) {
276 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
280 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
286 printtimez(FILE *fp, const git_time *intime)
292 t = (time_t)intime->time;
293 if (!(intm = gmtime(&t)))
295 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
300 printtime(FILE *fp, const git_time *intime)
306 t = (time_t)intime->time + (intime->offset * 60);
307 if (!(intm = gmtime(&t)))
309 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
310 if (intime->offset < 0)
311 fprintf(fp, "%s -%02d%02d", out,
312 -(intime->offset) / 60, -(intime->offset) % 60);
314 fprintf(fp, "%s +%02d%02d", out,
315 intime->offset / 60, intime->offset % 60);
319 printtimeshort(FILE *fp, const git_time *intime)
325 t = (time_t)intime->time;
326 if (!(intm = gmtime(&t)))
328 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
333 writeheader(FILE *fp, const char *title)
335 fputs("<!DOCTYPE html>\n"
337 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
339 xmlencode(fp, title, strlen(title));
340 if (title[0] && strippedname[0])
342 xmlencode(fp, strippedname, strlen(strippedname));
345 xmlencode(fp, description, strlen(description));
346 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
347 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
349 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
350 fputs("</head>\n<body>\n<table><tr><td>", fp);
351 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
353 fputs("</td><td><h1>", fp);
354 xmlencode(fp, strippedname, strlen(strippedname));
355 fputs("</h1><span class=\"desc\">", fp);
356 xmlencode(fp, description, strlen(description));
357 fputs("</span></td></tr>", fp);
359 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
360 xmlencode(fp, cloneurl, strlen(cloneurl));
362 xmlencode(fp, cloneurl, strlen(cloneurl));
363 fputs("</a></td></tr>", fp);
365 fputs("<tr><td></td><td>\n", fp);
366 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
367 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
368 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
370 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
372 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
374 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
375 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
379 writefooter(FILE *fp)
381 fputs("</div>\n</body>\n</html>\n", fp);
385 writeblobhtml(FILE *fp, const git_blob *blob)
387 size_t n = 0, i, prev;
388 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
389 const char *s = git_blob_rawcontent(blob);
390 git_off_t len = git_blob_rawsize(blob);
392 fputs("<pre id=\"blob\">\n", fp);
395 for (i = 0, prev = 0; i < (size_t)len; i++) {
399 fprintf(fp, nfmt, n, n, n);
400 xmlencode(fp, &s[prev], i - prev + 1);
404 if ((len - prev) > 0) {
406 fprintf(fp, nfmt, n, n, n);
407 xmlencode(fp, &s[prev], len - prev);
411 fputs("</pre>\n", fp);
417 printcommit(FILE *fp, struct commitinfo *ci)
419 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
420 relpath, ci->oid, ci->oid);
422 if (ci->parentoid[0])
423 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
424 relpath, ci->parentoid, ci->parentoid);
427 fputs("<b>Author:</b> ", fp);
428 xmlencode(fp, ci->author->name, strlen(ci->author->name));
429 fputs(" <<a href=\"mailto:", fp);
430 xmlencode(fp, ci->author->email, strlen(ci->author->email));
432 xmlencode(fp, ci->author->email, strlen(ci->author->email));
433 fputs("</a>>\n<b>Date:</b> ", fp);
434 printtime(fp, &(ci->author->when));
439 xmlencode(fp, ci->msg, strlen(ci->msg));
445 printshowfile(FILE *fp, struct commitinfo *ci)
447 const git_diff_delta *delta;
448 const git_diff_hunk *hunk;
449 const git_diff_line *line;
451 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
459 if (ci->filecount > 1000 ||
460 ci->ndeltas > 1000 ||
461 ci->addcount > 100000 ||
462 ci->delcount > 100000) {
463 fputs("Diff is too large, output suppressed.\n", fp);
468 fputs("<b>Diffstat:</b>\n<table>", fp);
469 for (i = 0; i < ci->ndeltas; i++) {
470 delta = git_patch_get_delta(ci->deltas[i]->patch);
472 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
473 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
474 if (strcmp(delta->old_file.path, delta->new_file.path)) {
475 fputs(" -> ", fp);
476 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
479 add = ci->deltas[i]->addcount;
480 del = ci->deltas[i]->delcount;
482 total = sizeof(linestr) - 2;
483 if (changed > total) {
485 add = ((float)total / changed * add) + 1;
487 del = ((float)total / changed * del) + 1;
489 memset(&linestr, '+', add);
490 memset(&linestr[add], '-', del);
492 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
493 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
494 fwrite(&linestr, 1, add, fp);
495 fputs("</span><span class=\"d\">", fp);
496 fwrite(&linestr[add], 1, del, fp);
497 fputs("</span></td></tr>\n", fp);
499 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
500 ci->filecount, ci->filecount == 1 ? "" : "s",
501 ci->addcount, ci->addcount == 1 ? "" : "s",
502 ci->delcount, ci->delcount == 1 ? "" : "s");
506 for (i = 0; i < ci->ndeltas; i++) {
507 patch = ci->deltas[i]->patch;
508 delta = git_patch_get_delta(patch);
509 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",
510 i, relpath, delta->old_file.path, delta->old_file.path,
511 relpath, delta->new_file.path, delta->new_file.path);
513 /* check binary data */
514 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
515 fputs("Binary files differ.\n", fp);
519 nhunks = git_patch_num_hunks(patch);
520 for (j = 0; j < nhunks; j++) {
521 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
524 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
525 xmlencode(fp, hunk->header, hunk->header_len);
529 if (git_patch_get_line_in_hunk(&line, patch, j, k))
531 if (line->old_lineno == -1)
532 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
534 else if (line->new_lineno == -1)
535 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
539 xmlencode(fp, line->content, line->content_len);
540 if (line->old_lineno == -1 || line->new_lineno == -1)
548 writelogline(FILE *fp, struct commitinfo *ci)
550 fputs("<tr><td>", fp);
552 printtimeshort(fp, &(ci->author->when));
553 fputs("</td><td>", fp);
555 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
556 xmlencode(fp, ci->summary, strlen(ci->summary));
559 fputs("</td><td>", fp);
561 xmlencode(fp, ci->author->name, strlen(ci->author->name));
562 fputs("</td><td class=\"num\" align=\"right\">", fp);
563 fprintf(fp, "%zu", ci->filecount);
564 fputs("</td><td class=\"num\" align=\"right\">", fp);
565 fprintf(fp, "+%zu", ci->addcount);
566 fputs("</td><td class=\"num\" align=\"right\">", fp);
567 fprintf(fp, "-%zu", ci->delcount);
568 fputs("</td></tr>\n", fp);
572 writelog(FILE *fp, const git_oid *oid)
574 struct commitinfo *ci;
575 git_revwalk *w = NULL;
577 char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
581 git_revwalk_new(&w, repo);
582 git_revwalk_push(w, oid);
583 git_revwalk_sorting(w, GIT_SORT_TIME);
584 git_revwalk_simplify_first_parent(w);
586 while (!git_revwalk_next(&id, w)) {
589 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
592 git_oid_tostr(oidstr, sizeof(oidstr), &id);
593 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
594 if (r == -1 || (size_t)r >= sizeof(path))
595 errx(1, "path truncated: 'commit/%s.html'", oidstr);
596 r = access(path, F_OK);
598 /* optimization: if there are no log lines to write and
599 the commit file already exists: skip the diffstat */
600 if (!nlogcommits && !r)
603 if (!(ci = commitinfo_getbyoid(&id)))
605 /* diffstat: for stagit HTML required for the log.html line */
606 if (commitinfo_getstats(ci) == -1)
609 if (nlogcommits < 0) {
610 writelogline(fp, ci);
611 } else if (nlogcommits > 0) {
612 writelogline(fp, ci);
614 if (!nlogcommits && ci->parentoid[0])
615 fputs("<tr><td></td><td colspan=\"5\">"
616 "More commits remaining [...]</td>"
621 writelogline(wcachefp, ci);
623 /* check if file exists if so skip it */
626 fpfile = efopen(path, "w");
627 writeheader(fpfile, ci->summary);
628 fputs("<pre>", fpfile);
629 printshowfile(fpfile, ci);
630 fputs("</pre>\n", fpfile);
645 printcommitatom(FILE *fp, struct commitinfo *ci)
647 fputs("<entry>\n", fp);
649 fprintf(fp, "<id>%s</id>\n", ci->oid);
651 fputs("<published>", fp);
652 printtimez(fp, &(ci->author->when));
653 fputs("</published>\n", fp);
656 fputs("<updated>", fp);
657 printtimez(fp, &(ci->committer->when));
658 fputs("</updated>\n", fp);
661 fputs("<title type=\"text\">", fp);
662 xmlencode(fp, ci->summary, strlen(ci->summary));
663 fputs("</title>\n", fp);
665 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
669 fputs("<author><name>", fp);
670 xmlencode(fp, ci->author->name, strlen(ci->author->name));
671 fputs("</name>\n<email>", fp);
672 xmlencode(fp, ci->author->email, strlen(ci->author->email));
673 fputs("</email>\n</author>\n", fp);
676 fputs("<content type=\"text\">", fp);
677 fprintf(fp, "commit %s\n", ci->oid);
678 if (ci->parentoid[0])
679 fprintf(fp, "parent %s\n", ci->parentoid);
681 fputs("Author: ", fp);
682 xmlencode(fp, ci->author->name, strlen(ci->author->name));
684 xmlencode(fp, ci->author->email, strlen(ci->author->email));
685 fputs(">\nDate: ", fp);
686 printtime(fp, &(ci->author->when));
691 xmlencode(fp, ci->msg, strlen(ci->msg));
693 fputs("\n</content>\n</entry>\n", fp);
699 struct commitinfo *ci;
700 git_revwalk *w = NULL;
702 size_t i, m = 100; /* last 'm' commits */
704 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
705 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
706 xmlencode(fp, strippedname, strlen(strippedname));
707 fputs(", branch HEAD</title>\n<subtitle>", fp);
708 xmlencode(fp, description, strlen(description));
709 fputs("</subtitle>\n", fp);
711 git_revwalk_new(&w, repo);
712 git_revwalk_push_head(w);
713 git_revwalk_sorting(w, GIT_SORT_TIME);
714 git_revwalk_simplify_first_parent(w);
716 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
717 if (!(ci = commitinfo_getbyoid(&id)))
719 printcommitatom(fp, ci);
724 fputs("</feed>\n", fp);
730 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
732 char tmp[PATH_MAX] = "", *d;
737 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
738 errx(1, "path truncated: '%s'", fpath);
739 if (!(d = dirname(tmp)))
744 for (p = fpath, tmp[0] = '\0'; *p; p++) {
745 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
746 errx(1, "path truncated: '../%s'", tmp);
750 fp = efopen(fpath, "w");
751 writeheader(fp, filename);
753 xmlencode(fp, filename, strlen(filename));
754 fprintf(fp, " (%juB)", (uintmax_t)filesize);
755 fputs("</p><hr/>", fp);
757 if (git_blob_is_binary((git_blob *)obj)) {
758 fputs("<p>Binary file.</p>\n", fp);
760 lc = writeblobhtml(fp, (git_blob *)obj);
773 filemode(git_filemode_t m)
775 static char mode[11];
777 memset(mode, '-', sizeof(mode) - 1);
788 else if (S_ISFIFO(m))
792 else if (S_ISSOCK(m))
797 if (m & S_IRUSR) mode[1] = 'r';
798 if (m & S_IWUSR) mode[2] = 'w';
799 if (m & S_IXUSR) mode[3] = 'x';
800 if (m & S_IRGRP) mode[4] = 'r';
801 if (m & S_IWGRP) mode[5] = 'w';
802 if (m & S_IXGRP) mode[6] = 'x';
803 if (m & S_IROTH) mode[7] = 'r';
804 if (m & S_IWOTH) mode[8] = 'w';
805 if (m & S_IXOTH) mode[9] = 'x';
807 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
808 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
809 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
815 writefilestree(FILE *fp, git_tree *tree, const char *path)
817 const git_tree_entry *entry = NULL;
818 git_submodule *module = NULL;
819 git_object *obj = NULL;
821 const char *entryname;
822 char filepath[PATH_MAX], entrypath[PATH_MAX];
826 count = git_tree_entrycount(tree);
827 for (i = 0; i < count; i++) {
828 if (!(entry = git_tree_entry_byindex(tree, i)) ||
829 !(entryname = git_tree_entry_name(entry)))
831 joinpath(entrypath, sizeof(entrypath), path, entryname);
833 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
835 if (r == -1 || (size_t)r >= sizeof(filepath))
836 errx(1, "path truncated: 'file/%s.html'", entrypath);
838 if (!git_tree_entry_to_object(&obj, repo, entry)) {
839 switch (git_object_type(obj)) {
844 ret = writefilestree(fp, (git_tree *)obj,
846 git_object_free(obj);
851 git_object_free(obj);
855 filesize = git_blob_rawsize((git_blob *)obj);
856 lc = writeblob(obj, filepath, entryname, filesize);
858 fputs("<tr><td>", fp);
859 fputs(filemode(git_tree_entry_filemode(entry)), fp);
860 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
861 xmlencode(fp, entrypath, strlen(entrypath));
862 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
864 fprintf(fp, "%dL", lc);
866 fprintf(fp, "%juB", (uintmax_t)filesize);
867 fputs("</td></tr>\n", fp);
868 git_object_free(obj);
869 } else if (!git_submodule_lookup(&module, repo, entryname)) {
870 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
872 xmlencode(fp, entrypath, strlen(entrypath));
873 git_submodule_free(module);
874 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
882 writefiles(FILE *fp, const git_oid *id)
884 git_tree *tree = NULL;
885 git_commit *commit = NULL;
888 fputs("<table id=\"files\"><thead>\n<tr>"
889 "<td><b>Mode</b></td><td><b>Name</b></td>"
890 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
891 "</tr>\n</thead><tbody>\n", fp);
893 if (!git_commit_lookup(&commit, repo, id) &&
894 !git_commit_tree(&tree, commit))
895 ret = writefilestree(fp, tree, "");
897 fputs("</tbody></table>", fp);
899 git_commit_free(commit);
906 refs_cmp(const void *v1, const void *v2)
908 git_reference *r1 = (*(git_reference **)v1);
909 git_reference *r2 = (*(git_reference **)v2);
912 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
915 return strcmp(git_reference_shorthand(r1),
916 git_reference_shorthand(r2));
922 struct commitinfo *ci;
923 const git_oid *id = NULL;
924 git_object *obj = NULL;
925 git_reference *dref = NULL, *r, *ref = NULL;
926 git_reference_iterator *it = NULL;
927 git_reference **refs = NULL;
928 size_t count, i, j, refcount;
929 const char *titles[] = { "Branches", "Tags" };
930 const char *ids[] = { "branches", "tags" };
933 if (git_reference_iterator_new(&it, repo))
936 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
937 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
939 refs[refcount] = ref;
941 git_reference_iterator_free(it);
943 /* sort by type then shorthand name */
944 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
946 for (j = 0; j < 2; j++) {
947 for (i = 0, count = 0; i < refcount; i++) {
948 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
949 !(git_reference_is_tag(refs[i]) && j == 1))
952 switch (git_reference_type(refs[i])) {
953 case GIT_REF_SYMBOLIC:
954 if (git_reference_resolve(&dref, refs[i]))
964 if (!git_reference_target(r) ||
965 git_reference_peel(&obj, r, GIT_OBJ_ANY))
967 if (!(id = git_object_id(obj)))
969 if (!(ci = commitinfo_getbyoid(id)))
972 /* print header if it has an entry (first). */
974 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
975 "<thead>\n<tr><td><b>Name</b></td>"
976 "<td><b>Last commit date</b></td>"
977 "<td><b>Author</b></td>\n</tr>\n"
983 name = git_reference_shorthand(r);
985 fputs("<tr><td>", fp);
986 xmlencode(fp, name, strlen(name));
987 fputs("</td><td>", fp);
989 printtimeshort(fp, &(ci->author->when));
990 fputs("</td><td>", fp);
992 xmlencode(fp, ci->author->name, strlen(ci->author->name));
993 fputs("</td></tr>\n", fp);
998 git_object_free(obj);
1000 git_reference_free(dref);
1005 fputs("</tbody></table><br/>", fp);
1009 git_object_free(obj);
1010 git_reference_free(dref);
1012 for (i = 0; i < refcount; i++)
1013 git_reference_free(refs[i]);
1022 fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1027 main(int argc, char *argv[])
1029 git_object *obj = NULL;
1030 const git_oid *head = NULL;
1031 const git_error *e = NULL;
1034 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1035 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1039 for (i = 1; i < argc; i++) {
1040 if (argv[i][0] != '-') {
1044 } else if (argv[i][1] == 'c') {
1045 if (nlogcommits > 0 || i + 1 >= argc)
1047 cachefile = argv[++i];
1048 } else if (argv[i][1] == 'l') {
1049 if (cachefile || i + 1 >= argc)
1052 nlogcommits = strtoll(argv[++i], &p, 10);
1053 if (argv[i][0] == '\0' || *p != '\0' ||
1054 nlogcommits <= 0 || errno)
1061 if (!realpath(repodir, repodirabs))
1068 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1071 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1076 if (git_repository_open_ext(&repo, repodir,
1077 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1079 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1084 if (!git_revparse_single(&obj, repo, "HEAD"))
1085 head = git_object_id(obj);
1086 git_object_free(obj);
1088 /* use directory name as name */
1089 if ((name = strrchr(repodirabs, '/')))
1094 /* strip .git suffix */
1095 if (!(strippedname = strdup(name)))
1097 if ((p = strrchr(strippedname, '.')))
1098 if (!strcmp(p, ".git"))
1101 /* read description or .git/description */
1102 joinpath(path, sizeof(path), repodir, "description");
1103 if (!(fpread = fopen(path, "r"))) {
1104 joinpath(path, sizeof(path), repodir, ".git/description");
1105 fpread = fopen(path, "r");
1108 if (!fgets(description, sizeof(description), fpread))
1109 description[0] = '\0';
1113 /* read url or .git/url */
1114 joinpath(path, sizeof(path), repodir, "url");
1115 if (!(fpread = fopen(path, "r"))) {
1116 joinpath(path, sizeof(path), repodir, ".git/url");
1117 fpread = fopen(path, "r");
1120 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1122 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1127 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1128 git_object_type(obj) == GIT_OBJ_BLOB);
1129 git_object_free(obj);
1132 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1133 git_object_type(obj) == GIT_OBJ_BLOB);
1134 git_object_free(obj);
1136 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1137 git_object_type(obj) == GIT_OBJ_BLOB);
1138 git_object_free(obj);
1141 fp = efopen("log.html", "w");
1143 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1144 writeheader(fp, "Log");
1145 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1146 "<td><b>Commit message</b></td>"
1147 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1148 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1149 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1151 if (cachefile && head) {
1152 /* read from cache file (does not need to exist) */
1153 if ((rcachefp = fopen(cachefile, "r"))) {
1154 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1155 errx(1, "%s: no object id", cachefile);
1156 if (git_oid_fromstr(&lastoid, lastoidstr))
1157 errx(1, "%s: invalid object id", cachefile);
1160 /* write log to (temporary) cache */
1161 if ((fd = mkstemp(tmppath)) == -1)
1163 if (!(wcachefp = fdopen(fd, "w")))
1164 err(1, "fdopen: '%s'", tmppath);
1165 /* write last commit id (HEAD) */
1166 git_oid_tostr(buf, sizeof(buf), head);
1167 fprintf(wcachefp, "%s\n", buf);
1172 /* append previous log to log.html and the new cache */
1173 while (!feof(rcachefp)) {
1174 n = fread(buf, 1, sizeof(buf), rcachefp);
1175 if (ferror(rcachefp))
1177 if (fwrite(buf, 1, n, fp) != n ||
1178 fwrite(buf, 1, n, wcachefp) != n)
1189 fputs("</tbody></table>", fp);
1193 /* files for HEAD */
1194 fp = efopen("files.html", "w");
1195 writeheader(fp, "Files");
1197 writefiles(fp, head);
1201 /* summary page with branches and tags */
1202 fp = efopen("refs.html", "w");
1203 writeheader(fp, "Refs");
1209 fp = efopen("atom.xml", "w");
1213 /* rename new cache file on success */
1214 if (cachefile && head) {
1215 if (rename(tmppath, cachefile))
1216 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1217 umask((mask = umask(0)));
1218 if (chmod(cachefile,
1219 (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1220 err(1, "chmod: '%s'", cachefile);
1224 git_repository_free(repo);
1225 git_libgit2_shutdown();