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 *stripped_name;
57 static char description[255];
58 static char cloneurl[1024];
59 static int haslicense, hasreadme, hassubmodules;
62 deltainfo_free(struct deltainfo *di)
66 git_patch_free(di->patch);
72 commitinfo_getstats(struct commitinfo *ci)
75 const git_diff_delta *delta;
76 const git_diff_hunk *hunk;
77 const git_diff_line *line;
78 git_patch *patch = NULL;
79 size_t ndeltas, nhunks, nhunklines;
82 ndeltas = git_diff_num_deltas(ci->diff);
83 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
86 for (i = 0; i < ndeltas; i++) {
87 if (!(di = calloc(1, sizeof(struct deltainfo))))
89 if (git_patch_from_diff(&patch, ci->diff, i)) {
90 git_patch_free(patch);
96 delta = git_patch_get_delta(patch);
98 /* check binary data */
99 if (delta->flags & GIT_DIFF_FLAG_BINARY)
102 nhunks = git_patch_num_hunks(patch);
103 for (j = 0; j < nhunks; j++) {
104 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
107 if (git_patch_get_line_in_hunk(&line, patch, j, k))
109 if (line->old_lineno == -1) {
112 } else if (line->new_lineno == -1) {
126 for (i = 0; i < ci->ndeltas; i++)
127 deltainfo_free(ci->deltas[i]);
139 commitinfo_free(struct commitinfo *ci)
146 for (i = 0; i < ci->ndeltas; i++)
147 deltainfo_free(ci->deltas[i]);
150 git_diff_free(ci->diff);
151 git_tree_free(ci->commit_tree);
152 git_tree_free(ci->parent_tree);
153 git_commit_free(ci->commit);
157 commitinfo_getbyoid(const git_oid *id)
159 struct commitinfo *ci;
160 git_diff_options opts;
162 if (!(ci = calloc(1, sizeof(struct commitinfo))))
165 if (git_commit_lookup(&(ci->commit), repo, id))
169 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
170 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
172 ci->author = git_commit_author(ci->commit);
173 ci->committer = git_commit_committer(ci->commit);
174 ci->summary = git_commit_summary(ci->commit);
175 ci->msg = git_commit_message(ci->commit);
177 if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
179 if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
180 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
182 ci->parent_tree = NULL;
186 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
187 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
188 if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
190 if (commitinfo_getstats(ci) == -1)
203 efopen(const char *name, const char *flags)
207 if (!(fp = fopen(name, flags)))
213 /* Escape characters below as HTML 2.0 / XML 1.0. */
215 xmlencode(FILE *fp, const char *s, size_t len)
219 for (i = 0; *s && i < len; s++, i++) {
221 case '<': fputs("<", fp); break;
222 case '>': fputs(">", fp); break;
223 case '\'': fputs("'", fp); break;
224 case '&': fputs("&", fp); break;
225 case '"': fputs(""", fp); break;
226 default: fputc(*s, fp);
231 /* Some implementations of dirname(3) return a pointer to a static
232 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
233 * This is a wrapper function that is compatible with both versions.
234 * The program will error out if dirname(3) failed, this can only happen
235 * with the OpenBSD version. */
237 xdirname(const char *path)
241 if (!(p = strdup(path)))
243 if (!(b = dirname(p)))
245 if (!(b = strdup(b)))
253 mkdirp(const char *path)
255 char tmp[PATH_MAX], *p;
257 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
258 errx(1, "path truncated: '%s'", path);
259 for (p = tmp + (tmp[0] == '/'); *p; p++) {
263 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
267 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
273 printtimez(FILE *fp, const git_time *intime)
279 t = (time_t)intime->time;
280 if (!(intm = gmtime(&t)))
282 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
287 printtime(FILE *fp, const git_time *intime)
291 int offset, sign = '+';
294 offset = intime->offset * 60;
295 t = (time_t)intime->time + offset;
296 if (!(intm = gmtime(&t)))
298 strftime(out, sizeof(out), "%a %b %e %H:%M:%S", intm);
303 fprintf(fp, "%s %c%02d%02d", out, sign, offset / 60, offset % 60);
307 printtimeshort(FILE *fp, const git_time *intime)
313 t = (time_t)intime->time;
314 if (!(intm = gmtime(&t)))
316 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
321 writeheader(FILE *fp, const char *title)
323 fputs("<!DOCTYPE html>\n"
324 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
325 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
326 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
327 xmlencode(fp, title, strlen(title));
328 if (title[0] && stripped_name[0])
330 xmlencode(fp, stripped_name, strlen(stripped_name));
333 xmlencode(fp, description, strlen(description));
334 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
335 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
337 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
338 fputs("</head>\n<body>\n<table><tr><td>", fp);
339 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
341 fputs("</td><td><h1>", fp);
342 xmlencode(fp, stripped_name, strlen(stripped_name));
343 fputs("</h1><span class=\"desc\">", fp);
344 xmlencode(fp, description, strlen(description));
345 fputs("</span></td></tr>", fp);
347 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
348 xmlencode(fp, cloneurl, strlen(cloneurl));
350 xmlencode(fp, cloneurl, strlen(cloneurl));
351 fputs("</a></td></tr>", fp);
353 fputs("<tr><td></td><td>\n", fp);
354 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
355 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
356 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
358 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
360 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
362 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
363 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
369 writefooter(FILE *fp)
371 return !fputs("</div>\n</body>\n</html>\n", fp);
375 writeblobhtml(FILE *fp, const git_blob *blob)
379 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
380 const char *s = git_blob_rawcontent(blob);
381 git_off_t len = git_blob_rawsize(blob);
383 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
387 fprintf(fp, nfmt, n, n, n);
388 for (i = 0; i < len - 1; i++) {
391 fprintf(fp, nfmt, n, n, n);
396 fputs("</pre></td><td><pre>\n", fp);
397 xmlencode(fp, s, (size_t)len);
398 fputs("</pre></td></tr></table>\n", fp);
404 printcommit(FILE *fp, struct commitinfo *ci)
406 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
407 relpath, ci->oid, ci->oid);
409 if (ci->parentoid[0])
410 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
411 relpath, ci->parentoid, ci->parentoid);
414 fputs("<b>Author:</b> ", fp);
415 xmlencode(fp, ci->author->name, strlen(ci->author->name));
416 fputs(" <<a href=\"mailto:", fp);
417 xmlencode(fp, ci->author->email, strlen(ci->author->email));
419 xmlencode(fp, ci->author->email, strlen(ci->author->email));
420 fputs("</a>>\n<b>Date:</b> ", fp);
421 printtime(fp, &(ci->author->when));
426 xmlencode(fp, ci->msg, strlen(ci->msg));
432 printshowfile(FILE *fp, struct commitinfo *ci)
434 const git_diff_delta *delta;
435 const git_diff_hunk *hunk;
436 const git_diff_line *line;
438 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
446 if (ci->filecount > 1000 ||
447 ci->ndeltas > 1000 ||
448 ci->addcount > 100000 ||
449 ci->delcount > 100000) {
450 fprintf(fp, "(diff is too large, output suppressed)");
455 fputs("<b>Diffstat:</b>\n<table>", fp);
456 for (i = 0; i < ci->ndeltas; i++) {
457 delta = git_patch_get_delta(ci->deltas[i]->patch);
458 fputs("<tr><td>", fp);
459 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
460 if (strcmp(delta->old_file.path, delta->new_file.path)) {
461 fputs(" -> ", fp);
462 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
465 add = ci->deltas[i]->addcount;
466 del = ci->deltas[i]->delcount;
468 total = sizeof(linestr) - 2;
469 if (changed > total) {
471 add = ((float)total / changed * add) + 1;
473 del = ((float)total / changed * del) + 1;
475 memset(&linestr, '+', add);
476 memset(&linestr[add], '-', del);
478 fprintf(fp, "</td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
479 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
480 fwrite(&linestr, 1, add, fp);
481 fputs("</span><span class=\"d\">", fp);
482 fwrite(&linestr[add], 1, del, fp);
483 fputs("</span></td></tr>\n", fp);
485 fprintf(fp, "</table>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
486 ci->filecount, ci->filecount == 1 ? "" : "s",
487 ci->addcount, ci->addcount == 1 ? "" : "s",
488 ci->delcount, ci->delcount == 1 ? "" : "s");
492 for (i = 0; i < ci->ndeltas; i++) {
493 patch = ci->deltas[i]->patch;
494 delta = git_patch_get_delta(patch);
495 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
496 relpath, delta->old_file.path, delta->old_file.path,
497 relpath, delta->new_file.path, delta->new_file.path);
499 /* check binary data */
500 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
501 fputs("Binary files differ\n", fp);
505 nhunks = git_patch_num_hunks(patch);
506 for (j = 0; j < nhunks; j++) {
507 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
510 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
511 xmlencode(fp, hunk->header, hunk->header_len);
515 if (git_patch_get_line_in_hunk(&line, patch, j, k))
517 if (line->old_lineno == -1)
518 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
520 else if (line->new_lineno == -1)
521 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
525 xmlencode(fp, line->content, line->content_len);
526 if (line->old_lineno == -1 || line->new_lineno == -1)
534 writelog(FILE *fp, const git_oid *oid)
536 struct commitinfo *ci;
537 git_revwalk *w = NULL;
544 git_revwalk_new(&w, repo);
545 git_revwalk_push(w, oid);
546 git_revwalk_sorting(w, GIT_SORT_TIME);
547 git_revwalk_simplify_first_parent(w);
549 fputs("<table id=\"log\"><thead>\n<tr><td>Date</td><td>Commit message</td>"
550 "<td>Author</td><td class=\"num\">Files</td><td class=\"num\">+</td>"
551 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
553 while (!git_revwalk_next(&id, w)) {
556 if (!(ci = commitinfo_getbyoid(&id)))
559 fputs("<tr><td>", fp);
561 printtimeshort(fp, &(ci->author->when));
562 fputs("</td><td>", fp);
564 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
565 if ((len = strlen(ci->summary)) > summarylen) {
566 xmlencode(fp, ci->summary, summarylen - 1);
569 xmlencode(fp, ci->summary, len);
573 fputs("</td><td>", fp);
575 xmlencode(fp, ci->author->name, strlen(ci->author->name));
576 fputs("</td><td class=\"num\">", fp);
577 fprintf(fp, "%zu", ci->filecount);
578 fputs("</td><td class=\"num\">", fp);
579 fprintf(fp, "+%zu", ci->addcount);
580 fputs("</td><td class=\"num\">", fp);
581 fprintf(fp, "-%zu", ci->delcount);
582 fputs("</td></tr>\n", fp);
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);
602 fputs("</tbody></table>", fp);
612 printcommitatom(FILE *fp, struct commitinfo *ci)
614 fputs("<entry>\n", fp);
616 fprintf(fp, "<id>%s</id>\n", ci->oid);
618 fputs("<published>", fp);
619 printtimez(fp, &(ci->author->when));
620 fputs("</published>\n", fp);
623 fputs("<updated>", fp);
624 printtimez(fp, &(ci->committer->when));
625 fputs("</updated>\n", fp);
628 fputs("<title type=\"text\">", fp);
629 xmlencode(fp, ci->summary, strlen(ci->summary));
630 fputs("</title>\n", fp);
632 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
636 fputs("<author><name>", fp);
637 xmlencode(fp, ci->author->name, strlen(ci->author->name));
638 fputs("</name>\n<email>", fp);
639 xmlencode(fp, ci->author->email, strlen(ci->author->email));
640 fputs("</email>\n</author>\n", fp);
643 fputs("<content type=\"text\">", fp);
644 fprintf(fp, "commit %s\n", ci->oid);
645 if (ci->parentoid[0])
646 fprintf(fp, "parent %s\n", ci->parentoid);
648 fputs("Author: ", fp);
649 xmlencode(fp, ci->author->name, strlen(ci->author->name));
651 xmlencode(fp, ci->author->email, strlen(ci->author->email));
652 fputs(">\nDate: ", fp);
653 printtime(fp, &(ci->author->when));
658 xmlencode(fp, ci->msg, strlen(ci->msg));
660 fputs("\n</content>\n", fp);
662 fputs("</entry>\n", fp);
668 struct commitinfo *ci;
669 git_revwalk *w = NULL;
671 size_t i, m = 100; /* last 'm' commits */
673 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
674 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
675 xmlencode(fp, stripped_name, strlen(stripped_name));
676 fputs(", branch HEAD</title>\n<subtitle>", fp);
677 xmlencode(fp, description, strlen(description));
678 fputs("</subtitle>\n", fp);
680 git_revwalk_new(&w, repo);
681 git_revwalk_push_head(w);
682 git_revwalk_sorting(w, GIT_SORT_TIME);
683 git_revwalk_simplify_first_parent(w);
685 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
686 if (!(ci = commitinfo_getbyoid(&id)))
688 printcommitatom(fp, ci);
693 fputs("</feed>", fp);
699 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
701 char tmp[PATH_MAX] = "", *d;
715 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
716 errx(1, "path truncated: '../%s'", tmp);
721 fp = efopen(fpath, "w");
722 writeheader(fp, filename);
724 xmlencode(fp, filename, strlen(filename));
725 fprintf(fp, " (%juB)", (uintmax_t)filesize);
726 fputs("</p><hr/>", fp);
728 if (git_blob_is_binary((git_blob *)obj)) {
729 fputs("<p>Binary file</p>\n", fp);
731 lc = writeblobhtml(fp, (git_blob *)obj);
744 filemode(git_filemode_t m)
746 static char mode[11];
748 memset(mode, '-', sizeof(mode) - 1);
759 else if (S_ISFIFO(m))
763 else if (S_ISSOCK(m))
768 if (m & S_IRUSR) mode[1] = 'r';
769 if (m & S_IWUSR) mode[2] = 'w';
770 if (m & S_IXUSR) mode[3] = 'x';
771 if (m & S_IRGRP) mode[4] = 'r';
772 if (m & S_IWGRP) mode[5] = 'w';
773 if (m & S_IXGRP) mode[6] = 'x';
774 if (m & S_IROTH) mode[7] = 'r';
775 if (m & S_IWOTH) mode[8] = 'w';
776 if (m & S_IXOTH) mode[9] = 'x';
778 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
779 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
780 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
786 writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
788 const git_tree_entry *entry = NULL;
789 git_submodule *module = NULL;
790 git_object *obj = NULL;
792 const char *entryname;
793 char filepath[PATH_MAX], entrypath[PATH_MAX];
797 count = git_tree_entrycount(tree);
798 for (i = 0; i < count; i++) {
799 if (!(entry = git_tree_entry_byindex(tree, i)) ||
800 !(entryname = git_tree_entry_name(entry)))
802 r = snprintf(entrypath, sizeof(entrypath), "%s%s%s",
803 path, path[0] ? "/" : "", entryname);
804 if (r == -1 || (size_t)r >= sizeof(entrypath))
805 errx(1, "path truncated: '%s%s%s'",
806 path, path[0] ? "/" : "", entryname);
808 r = snprintf(filepath, sizeof(filepath), "file/%s%s%s.html",
809 path, path[0] ? "/" : "", entryname);
810 if (r == -1 || (size_t)r >= sizeof(filepath))
811 errx(1, "path truncated: 'file/%s%s%s.html'",
812 path, path[0] ? "/" : "", entryname);
814 if (!git_tree_entry_to_object(&obj, repo, entry)) {
815 switch (git_object_type(obj)) {
820 ret = writefilestree(fp, (git_tree *)obj, branch,
822 git_object_free(obj);
827 git_object_free(obj);
831 filesize = git_blob_rawsize((git_blob *)obj);
832 lc = writeblob(obj, filepath, entryname, filesize);
834 fputs("<tr><td>", fp);
835 fputs(filemode(git_tree_entry_filemode(entry)), fp);
836 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
837 xmlencode(fp, entrypath, strlen(entrypath));
838 fputs("</a></td><td class=\"num\">", fp);
839 if (showlinecount && lc > 0)
840 fprintf(fp, "%dL", lc);
842 fprintf(fp, "%juB", (uintmax_t)filesize);
843 fputs("</td></tr>\n", fp);
844 } else if (!git_submodule_lookup(&module, repo, entryname)) {
845 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
847 xmlencode(fp, entrypath, strlen(entrypath));
848 git_submodule_free(module);
849 fputs("</a></td><td class=\"num\"></td></tr>\n", fp);
857 writefiles(FILE *fp, const git_oid *id, const char *branch)
859 git_tree *tree = NULL;
860 git_commit *commit = NULL;
863 fputs("<table id=\"files\"><thead>\n<tr>"
864 "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
865 "</tr>\n</thead><tbody>\n", fp);
867 if (git_commit_lookup(&commit, repo, id) ||
868 git_commit_tree(&tree, commit))
870 ret = writefilestree(fp, tree, branch, "");
873 fputs("</tbody></table>", fp);
875 git_commit_free(commit);
882 refs_cmp(const void *v1, const void *v2)
884 git_reference *r1 = (*(git_reference **)v1);
885 git_reference *r2 = (*(git_reference **)v2);
888 t1 = git_reference_is_branch(r1);
889 t2 = git_reference_is_branch(r2);
894 return strcmp(git_reference_shorthand(r1),
895 git_reference_shorthand(r2));
901 struct commitinfo *ci;
902 const git_oid *id = NULL;
903 git_object *obj = NULL;
904 git_reference *dref = NULL, *r, *ref = NULL;
905 git_reference_iterator *it = NULL;
906 git_reference **refs = NULL;
907 size_t count, i, j, refcount;
908 const char *titles[] = { "Branches", "Tags" };
909 const char *ids[] = { "branches", "tags" };
912 if (git_reference_iterator_new(&it, repo))
915 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
916 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
918 refs[refcount] = ref;
920 git_reference_iterator_free(it);
922 /* sort by type then shorthand name */
923 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
925 for (j = 0; j < 2; j++) {
926 for (i = 0, count = 0; i < refcount; i++) {
927 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
928 !(git_reference_is_tag(refs[i]) && j == 1))
931 switch (git_reference_type(refs[i])) {
932 case GIT_REF_SYMBOLIC:
933 if (git_reference_resolve(&dref, refs[i]))
943 if (!(id = git_reference_target(r)))
945 if (git_reference_peel(&obj, r, GIT_OBJ_ANY))
947 if (!(id = git_object_id(obj)))
949 if (!(ci = commitinfo_getbyoid(id)))
952 /* print header if it has an entry (first). */
954 fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>Name</td>"
955 "<td>Last commit date</td><td>Author</td>\n</tr>\n</thead><tbody>\n",
960 name = git_reference_shorthand(r);
962 fputs("<tr><td>", fp);
963 xmlencode(fp, name, strlen(name));
964 fputs("</td><td>", fp);
966 printtimeshort(fp, &(ci->author->when));
967 fputs("</td><td>", fp);
969 xmlencode(fp, ci->author->name, strlen(ci->author->name));
970 fputs("</td></tr>\n", fp);
975 git_object_free(obj);
977 git_reference_free(dref);
982 fputs("</tbody></table><br/>", fp);
986 git_object_free(obj);
987 git_reference_free(dref);
989 for (i = 0; i < refcount; i++)
990 git_reference_free(refs[i]);
997 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
1001 r = snprintf(buf, bufsiz, "%s%s%s",
1002 repodir, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
1003 if (r == -1 || (size_t)r >= bufsiz)
1004 errx(1, "path truncated: '%s%s%s'",
1005 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
1009 main(int argc, char *argv[])
1011 git_object *obj = NULL;
1012 const git_oid *head = NULL;
1013 const git_error *e = NULL;
1015 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1019 fprintf(stderr, "%s <repodir>\n", argv[0]);
1024 if (!realpath(repodir, repodirabs))
1029 if ((status = git_repository_open_ext(&repo, repodir,
1030 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
1032 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1037 if (git_revparse_single(&obj, repo, "HEAD"))
1039 head = git_object_id(obj);
1040 git_object_free(obj);
1042 /* use directory name as name */
1043 if ((name = strrchr(repodirabs, '/')))
1048 /* strip .git suffix */
1049 if (!(stripped_name = strdup(name)))
1051 if ((p = strrchr(stripped_name, '.')))
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_free(obj);
1084 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
1085 git_object_free(obj);
1086 hassubmodules = !git_revparse_single(&obj, repo, "HEAD:.gitmodules");
1087 git_object_free(obj);
1090 fp = efopen("log.html", "w");
1092 writeheader(fp, "Log");
1093 mkdir("commit", 0755);
1098 /* files for HEAD */
1099 fp = efopen("files.html", "w");
1100 writeheader(fp, "Files");
1101 writefiles(fp, head, "HEAD");
1105 /* summary page with branches and tags */
1106 fp = efopen("refs.html", "w");
1107 writeheader(fp, "Refs");
1113 fp = efopen("atom.xml", "w");
1118 git_repository_free(repo);
1119 git_libgit2_shutdown();