27 char oid[GIT_OID_HEXSZ + 1];
28 char parentoid[GIT_OID_HEXSZ + 1];
30 const git_signature *author;
31 const git_signature *committer;
38 git_tree *commit_tree;
39 git_tree *parent_tree;
45 struct deltainfo **deltas;
49 static git_repository *repo;
51 static const char *relpath = "";
52 static const char *repodir;
54 static char *name = "";
55 static char *strippedname = "";
56 static char description[255];
57 static char cloneurl[1024];
58 static int haslicense, hasreadme, hassubmodules;
61 static git_oid lastoid;
62 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + nul byte */
63 static FILE *rcachefp, *wcachefp;
64 static const char *cachefile;
67 #define pledge(p1,p2) 0
71 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
75 r = snprintf(buf, bufsiz, "%s%s%s",
76 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
77 if (r == -1 || (size_t)r >= bufsiz)
78 errx(1, "path truncated: '%s%s%s'",
79 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
83 deltainfo_free(struct deltainfo *di)
87 git_patch_free(di->patch);
93 commitinfo_getstats(struct commitinfo *ci)
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 ndeltas = git_diff_num_deltas(ci->diff);
104 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
107 for (i = 0; i < ndeltas; i++) {
108 if (git_patch_from_diff(&patch, ci->diff, i))
110 if (!(di = calloc(1, sizeof(struct deltainfo))))
115 delta = git_patch_get_delta(patch);
117 /* skip stats for binary data */
118 if (delta->flags & GIT_DIFF_FLAG_BINARY)
121 nhunks = git_patch_num_hunks(patch);
122 for (j = 0; j < nhunks; j++) {
123 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
126 if (git_patch_get_line_in_hunk(&line, patch, j, k))
128 if (line->old_lineno == -1) {
131 } else if (line->new_lineno == -1) {
145 for (i = 0; i < ci->ndeltas; i++)
146 deltainfo_free(ci->deltas[i]);
158 commitinfo_free(struct commitinfo *ci)
165 for (i = 0; i < ci->ndeltas; i++)
166 deltainfo_free(ci->deltas[i]);
169 git_diff_free(ci->diff);
170 git_tree_free(ci->commit_tree);
171 git_tree_free(ci->parent_tree);
172 git_commit_free(ci->commit);
173 git_commit_free(ci->parent);
178 commitinfo_getbyoid(const git_oid *id)
180 struct commitinfo *ci;
181 git_diff_options opts;
183 if (!(ci = calloc(1, sizeof(struct commitinfo))))
186 if (git_commit_lookup(&(ci->commit), repo, id))
190 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
191 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
193 ci->author = git_commit_author(ci->commit);
194 ci->committer = git_commit_committer(ci->commit);
195 ci->summary = git_commit_summary(ci->commit);
196 ci->msg = git_commit_message(ci->commit);
198 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
200 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
201 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
203 ci->parent_tree = NULL;
207 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
208 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
209 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
221 efopen(const char *name, const char *flags)
225 if (!(fp = fopen(name, flags)))
231 /* Escape characters below as HTML 2.0 / XML 1.0. */
233 xmlencode(FILE *fp, const char *s, size_t len)
237 for (i = 0; *s && i < len; s++, i++) {
239 case '<': fputs("<", fp); break;
240 case '>': fputs(">", fp); break;
241 case '\'': fputs("'", fp); break;
242 case '&': fputs("&", fp); break;
243 case '"': fputs(""", fp); break;
244 default: fputc(*s, fp);
250 mkdirp(const char *path)
252 char tmp[PATH_MAX], *p;
254 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
255 errx(1, "path truncated: '%s'", path);
256 for (p = tmp + (tmp[0] == '/'); *p; p++) {
260 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
264 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
270 printtimez(FILE *fp, const git_time *intime)
276 t = (time_t)intime->time;
277 if (!(intm = gmtime(&t)))
279 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
284 printtime(FILE *fp, const git_time *intime)
290 t = (time_t)intime->time + (intime->offset * 60);
291 if (!(intm = gmtime(&t)))
293 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
294 if (intime->offset < 0)
295 fprintf(fp, "%s -%02d%02d", out,
296 -(intime->offset) / 60, -(intime->offset) % 60);
298 fprintf(fp, "%s +%02d%02d", out,
299 intime->offset / 60, intime->offset % 60);
303 printtimeshort(FILE *fp, const git_time *intime)
309 t = (time_t)intime->time;
310 if (!(intm = gmtime(&t)))
312 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
317 writeheader(FILE *fp, const char *title)
319 fputs("<!DOCTYPE html>\n"
321 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
323 xmlencode(fp, title, strlen(title));
324 if (title[0] && strippedname[0])
326 xmlencode(fp, strippedname, strlen(strippedname));
329 xmlencode(fp, description, strlen(description));
330 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
331 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
333 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
334 fputs("</head>\n<body>\n<table><tr><td>", fp);
335 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
337 fputs("</td><td><h1>", fp);
338 xmlencode(fp, strippedname, strlen(strippedname));
339 fputs("</h1><span class=\"desc\">", fp);
340 xmlencode(fp, description, strlen(description));
341 fputs("</span></td></tr>", fp);
343 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
344 xmlencode(fp, cloneurl, strlen(cloneurl));
346 xmlencode(fp, cloneurl, strlen(cloneurl));
347 fputs("</a></td></tr>", fp);
349 fputs("<tr><td></td><td>\n", fp);
350 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
351 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
352 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
354 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
356 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
358 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
359 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
363 writefooter(FILE *fp)
365 fputs("</div>\n</body>\n</html>\n", fp);
369 writeblobhtml(FILE *fp, const git_blob *blob)
371 size_t n = 0, i, prev;
372 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
373 const char *s = git_blob_rawcontent(blob);
374 git_off_t len = git_blob_rawsize(blob);
376 fputs("<pre id=\"blob\">\n", fp);
379 for (i = 0, prev = 0; i < (size_t)len; i++) {
383 fprintf(fp, nfmt, n, n, n);
384 xmlencode(fp, &s[prev], i - prev + 1);
388 if ((len - prev) > 0) {
390 fprintf(fp, nfmt, n, n, n);
391 xmlencode(fp, &s[prev], len - prev);
395 fputs("</pre>\n", fp);
401 printcommit(FILE *fp, struct commitinfo *ci)
403 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
404 relpath, ci->oid, ci->oid);
406 if (ci->parentoid[0])
407 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
408 relpath, ci->parentoid, ci->parentoid);
411 fputs("<b>Author:</b> ", fp);
412 xmlencode(fp, ci->author->name, strlen(ci->author->name));
413 fputs(" <<a href=\"mailto:", fp);
414 xmlencode(fp, ci->author->email, strlen(ci->author->email));
416 xmlencode(fp, ci->author->email, strlen(ci->author->email));
417 fputs("</a>>\n<b>Date:</b> ", fp);
418 printtime(fp, &(ci->author->when));
423 xmlencode(fp, ci->msg, strlen(ci->msg));
429 printshowfile(FILE *fp, struct commitinfo *ci)
431 const git_diff_delta *delta;
432 const git_diff_hunk *hunk;
433 const git_diff_line *line;
435 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
443 if (ci->filecount > 1000 ||
444 ci->ndeltas > 1000 ||
445 ci->addcount > 100000 ||
446 ci->delcount > 100000) {
447 fputs("Diff is too large, output suppressed.\n", fp);
452 fputs("<b>Diffstat:</b>\n<table>", fp);
453 for (i = 0; i < ci->ndeltas; i++) {
454 delta = git_patch_get_delta(ci->deltas[i]->patch);
455 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
456 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
457 if (strcmp(delta->old_file.path, delta->new_file.path)) {
458 fputs(" -> ", fp);
459 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
462 add = ci->deltas[i]->addcount;
463 del = ci->deltas[i]->delcount;
465 total = sizeof(linestr) - 2;
466 if (changed > total) {
468 add = ((float)total / changed * add) + 1;
470 del = ((float)total / changed * del) + 1;
472 memset(&linestr, '+', add);
473 memset(&linestr[add], '-', del);
475 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
476 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
477 fwrite(&linestr, 1, add, fp);
478 fputs("</span><span class=\"d\">", fp);
479 fwrite(&linestr[add], 1, del, fp);
480 fputs("</span></td></tr>\n", fp);
482 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
483 ci->filecount, ci->filecount == 1 ? "" : "s",
484 ci->addcount, ci->addcount == 1 ? "" : "s",
485 ci->delcount, ci->delcount == 1 ? "" : "s");
489 for (i = 0; i < ci->ndeltas; i++) {
490 patch = ci->deltas[i]->patch;
491 delta = git_patch_get_delta(patch);
492 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",
493 i, relpath, delta->old_file.path, delta->old_file.path,
494 relpath, delta->new_file.path, delta->new_file.path);
496 /* check binary data */
497 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
498 fputs("Binary files differ.\n", fp);
502 nhunks = git_patch_num_hunks(patch);
503 for (j = 0; j < nhunks; j++) {
504 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
507 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
508 xmlencode(fp, hunk->header, hunk->header_len);
512 if (git_patch_get_line_in_hunk(&line, patch, j, k))
514 if (line->old_lineno == -1)
515 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
517 else if (line->new_lineno == -1)
518 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
522 xmlencode(fp, line->content, line->content_len);
523 if (line->old_lineno == -1 || line->new_lineno == -1)
531 writelogline(FILE *fp, struct commitinfo *ci)
533 fputs("<tr><td>", fp);
535 printtimeshort(fp, &(ci->author->when));
536 fputs("</td><td>", fp);
538 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
539 xmlencode(fp, ci->summary, strlen(ci->summary));
542 fputs("</td><td>", fp);
544 xmlencode(fp, ci->author->name, strlen(ci->author->name));
545 fputs("</td><td class=\"num\" align=\"right\">", fp);
546 fprintf(fp, "%zu", ci->filecount);
547 fputs("</td><td class=\"num\" align=\"right\">", fp);
548 fprintf(fp, "+%zu", ci->addcount);
549 fputs("</td><td class=\"num\" align=\"right\">", fp);
550 fprintf(fp, "-%zu", ci->delcount);
551 fputs("</td></tr>\n", fp);
555 writelog(FILE *fp, const git_oid *oid)
557 struct commitinfo *ci;
558 git_revwalk *w = NULL;
564 git_revwalk_new(&w, repo);
565 git_revwalk_push(w, oid);
566 git_revwalk_sorting(w, GIT_SORT_TIME);
567 git_revwalk_simplify_first_parent(w);
569 while (!git_revwalk_next(&id, w)) {
572 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
574 if (!(ci = commitinfo_getbyoid(&id)))
576 /* lookup stats: only required here */
577 if (commitinfo_getstats(ci) == -1)
580 writelogline(fp, ci);
582 writelogline(wcachefp, ci);
586 r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
587 if (r == -1 || (size_t)r >= sizeof(path))
588 errx(1, "path truncated: 'commit/%s.html'", ci->oid);
590 /* check if file exists if so skip it */
591 if (access(path, F_OK)) {
592 fpfile = efopen(path, "w");
593 writeheader(fpfile, ci->summary);
594 fputs("<pre>", fpfile);
595 printshowfile(fpfile, ci);
596 fputs("</pre>\n", fpfile);
611 printcommitatom(FILE *fp, struct commitinfo *ci)
613 fputs("<entry>\n", fp);
615 fprintf(fp, "<id>%s</id>\n", ci->oid);
617 fputs("<published>", fp);
618 printtimez(fp, &(ci->author->when));
619 fputs("</published>\n", fp);
622 fputs("<updated>", fp);
623 printtimez(fp, &(ci->committer->when));
624 fputs("</updated>\n", fp);
627 fputs("<title type=\"text\">", fp);
628 xmlencode(fp, ci->summary, strlen(ci->summary));
629 fputs("</title>\n", fp);
631 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
635 fputs("<author><name>", fp);
636 xmlencode(fp, ci->author->name, strlen(ci->author->name));
637 fputs("</name>\n<email>", fp);
638 xmlencode(fp, ci->author->email, strlen(ci->author->email));
639 fputs("</email>\n</author>\n", fp);
642 fputs("<content type=\"text\">", fp);
643 fprintf(fp, "commit %s\n", ci->oid);
644 if (ci->parentoid[0])
645 fprintf(fp, "parent %s\n", ci->parentoid);
647 fputs("Author: ", fp);
648 xmlencode(fp, ci->author->name, strlen(ci->author->name));
650 xmlencode(fp, ci->author->email, strlen(ci->author->email));
651 fputs(">\nDate: ", fp);
652 printtime(fp, &(ci->author->when));
657 xmlencode(fp, ci->msg, strlen(ci->msg));
659 fputs("\n</content>\n</entry>\n", fp);
665 struct commitinfo *ci;
666 git_revwalk *w = NULL;
668 size_t i, m = 100; /* last 'm' commits */
670 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
671 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
672 xmlencode(fp, strippedname, strlen(strippedname));
673 fputs(", branch HEAD</title>\n<subtitle>", fp);
674 xmlencode(fp, description, strlen(description));
675 fputs("</subtitle>\n", fp);
677 git_revwalk_new(&w, repo);
678 git_revwalk_push_head(w);
679 git_revwalk_sorting(w, GIT_SORT_TIME);
680 git_revwalk_simplify_first_parent(w);
682 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
683 if (!(ci = commitinfo_getbyoid(&id)))
685 printcommitatom(fp, ci);
690 fputs("</feed>\n", fp);
696 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
698 char tmp[PATH_MAX] = "", *d;
703 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
704 errx(1, "path truncated: '%s'", fpath);
705 if (!(d = dirname(tmp)))
710 for (p = fpath, tmp[0] = '\0'; *p; p++) {
711 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
712 errx(1, "path truncated: '../%s'", tmp);
716 fp = efopen(fpath, "w");
717 writeheader(fp, filename);
719 xmlencode(fp, filename, strlen(filename));
720 fprintf(fp, " (%juB)", (uintmax_t)filesize);
721 fputs("</p><hr/>", fp);
723 if (git_blob_is_binary((git_blob *)obj)) {
724 fputs("<p>Binary file.</p>\n", fp);
726 lc = writeblobhtml(fp, (git_blob *)obj);
739 filemode(git_filemode_t m)
741 static char mode[11];
743 memset(mode, '-', sizeof(mode) - 1);
754 else if (S_ISFIFO(m))
758 else if (S_ISSOCK(m))
763 if (m & S_IRUSR) mode[1] = 'r';
764 if (m & S_IWUSR) mode[2] = 'w';
765 if (m & S_IXUSR) mode[3] = 'x';
766 if (m & S_IRGRP) mode[4] = 'r';
767 if (m & S_IWGRP) mode[5] = 'w';
768 if (m & S_IXGRP) mode[6] = 'x';
769 if (m & S_IROTH) mode[7] = 'r';
770 if (m & S_IWOTH) mode[8] = 'w';
771 if (m & S_IXOTH) mode[9] = 'x';
773 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
774 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
775 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
781 writefilestree(FILE *fp, git_tree *tree, const char *path)
783 const git_tree_entry *entry = NULL;
784 git_submodule *module = NULL;
785 git_object *obj = NULL;
787 const char *entryname;
788 char filepath[PATH_MAX], entrypath[PATH_MAX];
792 count = git_tree_entrycount(tree);
793 for (i = 0; i < count; i++) {
794 if (!(entry = git_tree_entry_byindex(tree, i)) ||
795 !(entryname = git_tree_entry_name(entry)))
797 joinpath(entrypath, sizeof(entrypath), path, entryname);
799 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
801 if (r == -1 || (size_t)r >= sizeof(filepath))
802 errx(1, "path truncated: 'file/%s.html'", entrypath);
804 if (!git_tree_entry_to_object(&obj, repo, entry)) {
805 switch (git_object_type(obj)) {
810 ret = writefilestree(fp, (git_tree *)obj,
812 git_object_free(obj);
817 git_object_free(obj);
821 filesize = git_blob_rawsize((git_blob *)obj);
822 lc = writeblob(obj, filepath, entryname, filesize);
824 fputs("<tr><td>", fp);
825 fputs(filemode(git_tree_entry_filemode(entry)), fp);
826 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
827 xmlencode(fp, entrypath, strlen(entrypath));
828 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
830 fprintf(fp, "%dL", lc);
832 fprintf(fp, "%juB", (uintmax_t)filesize);
833 fputs("</td></tr>\n", fp);
834 git_object_free(obj);
835 } else if (!git_submodule_lookup(&module, repo, entryname)) {
836 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
838 xmlencode(fp, entrypath, strlen(entrypath));
839 git_submodule_free(module);
840 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
848 writefiles(FILE *fp, const git_oid *id)
850 git_tree *tree = NULL;
851 git_commit *commit = NULL;
854 fputs("<table id=\"files\"><thead>\n<tr>"
855 "<td><b>Mode</b></td><td><b>Name</b></td>"
856 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
857 "</tr>\n</thead><tbody>\n", fp);
859 if (!git_commit_lookup(&commit, repo, id) &&
860 !git_commit_tree(&tree, commit))
861 ret = writefilestree(fp, tree, "");
863 fputs("</tbody></table>", fp);
865 git_commit_free(commit);
872 refs_cmp(const void *v1, const void *v2)
874 git_reference *r1 = (*(git_reference **)v1);
875 git_reference *r2 = (*(git_reference **)v2);
878 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
881 return strcmp(git_reference_shorthand(r1),
882 git_reference_shorthand(r2));
888 struct commitinfo *ci;
889 const git_oid *id = NULL;
890 git_object *obj = NULL;
891 git_reference *dref = NULL, *r, *ref = NULL;
892 git_reference_iterator *it = NULL;
893 git_reference **refs = NULL;
894 size_t count, i, j, refcount;
895 const char *titles[] = { "Branches", "Tags" };
896 const char *ids[] = { "branches", "tags" };
899 if (git_reference_iterator_new(&it, repo))
902 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
903 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
905 refs[refcount] = ref;
907 git_reference_iterator_free(it);
909 /* sort by type then shorthand name */
910 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
912 for (j = 0; j < 2; j++) {
913 for (i = 0, count = 0; i < refcount; i++) {
914 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
915 !(git_reference_is_tag(refs[i]) && j == 1))
918 switch (git_reference_type(refs[i])) {
919 case GIT_REF_SYMBOLIC:
920 if (git_reference_resolve(&dref, refs[i]))
930 if (!git_reference_target(r) ||
931 git_reference_peel(&obj, r, GIT_OBJ_ANY))
933 if (!(id = git_object_id(obj)))
935 if (!(ci = commitinfo_getbyoid(id)))
938 /* print header if it has an entry (first). */
940 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
941 "<thead>\n<tr><td><b>Name</b></td>"
942 "<td><b>Last commit date</b></td>"
943 "<td><b>Author</b></td>\n</tr>\n"
949 name = git_reference_shorthand(r);
951 fputs("<tr><td>", fp);
952 xmlencode(fp, name, strlen(name));
953 fputs("</td><td>", fp);
955 printtimeshort(fp, &(ci->author->when));
956 fputs("</td><td>", fp);
958 xmlencode(fp, ci->author->name, strlen(ci->author->name));
959 fputs("</td></tr>\n", fp);
964 git_object_free(obj);
966 git_reference_free(dref);
971 fputs("</tbody></table><br/>", fp);
975 git_object_free(obj);
976 git_reference_free(dref);
978 for (i = 0; i < refcount; i++)
979 git_reference_free(refs[i]);
988 fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
993 main(int argc, char *argv[])
995 git_object *obj = NULL;
996 const git_oid *head = NULL;
997 const git_error *e = NULL;
999 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1000 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1004 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1007 for (i = 1; i < argc; i++) {
1008 if (argv[i][0] != '-') {
1012 } else if (argv[i][1] == 'c') {
1015 cachefile = argv[++i];
1021 if (!realpath(repodir, repodirabs))
1026 if (git_repository_open_ext(&repo, repodir,
1027 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1029 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1034 if (!git_revparse_single(&obj, repo, "HEAD"))
1035 head = git_object_id(obj);
1036 git_object_free(obj);
1038 /* don't cache if there is no HEAD */
1042 /* use directory name as name */
1043 if ((name = strrchr(repodirabs, '/')))
1048 /* strip .git suffix */
1049 if (!(strippedname = strdup(name)))
1051 if ((p = strrchr(strippedname, '.')))
1052 if (!strcmp(p, ".git"))
1055 /* read description or .git/description */
1056 joinpath(path, sizeof(path), repodir, "description");
1057 if (!(fpread = fopen(path, "r"))) {
1058 joinpath(path, sizeof(path), repodir, ".git/description");
1059 fpread = fopen(path, "r");
1062 if (!fgets(description, sizeof(description), fpread))
1063 description[0] = '\0';
1067 /* read url or .git/url */
1068 joinpath(path, sizeof(path), repodir, "url");
1069 if (!(fpread = fopen(path, "r"))) {
1070 joinpath(path, sizeof(path), repodir, ".git/url");
1071 fpread = fopen(path, "r");
1074 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1076 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1081 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1082 git_object_type(obj) == GIT_OBJ_BLOB);
1083 git_object_free(obj);
1086 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1087 git_object_type(obj) == GIT_OBJ_BLOB);
1088 git_object_free(obj);
1090 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1091 git_object_type(obj) == GIT_OBJ_BLOB);
1092 git_object_free(obj);
1095 fp = efopen("log.html", "w");
1097 mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1098 writeheader(fp, "Log");
1099 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1100 "<td><b>Commit message</b></td>"
1101 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1102 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1103 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1106 /* read from cache file (does not need to exist) */
1107 if ((rcachefp = fopen(cachefile, "r"))) {
1108 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1109 errx(1, "%s: no object id", cachefile);
1110 if (git_oid_fromstr(&lastoid, lastoidstr))
1111 errx(1, "%s: invalid object id", cachefile);
1114 /* write log to (temporary) cache */
1115 if ((fd = mkstemp(tmppath)) == -1)
1117 if (!(wcachefp = fdopen(fd, "w")))
1119 /* write last commit id (HEAD) */
1120 git_oid_tostr(buf, sizeof(buf), head);
1121 fprintf(wcachefp, "%s\n", buf);
1126 /* append previous log to log.html and the new cache */
1127 while (!feof(rcachefp)) {
1128 n = fread(buf, 1, sizeof(buf), rcachefp);
1129 if (ferror(rcachefp))
1131 if (fwrite(buf, 1, n, fp) != n ||
1132 fwrite(buf, 1, n, wcachefp) != n)
1143 fputs("</tbody></table>", fp);
1147 /* files for HEAD */
1148 fp = efopen("files.html", "w");
1149 writeheader(fp, "Files");
1151 writefiles(fp, head);
1155 /* summary page with branches and tags */
1156 fp = efopen("refs.html", "w");
1157 writeheader(fp, "Refs");
1163 fp = efopen("atom.xml", "w");
1167 /* rename new cache file on success */
1168 if (cachefile && rename(tmppath, cachefile))
1169 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1172 git_repository_free(repo);
1173 git_libgit2_shutdown();