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))
211 if (commitinfo_getstats(ci) == -1)
223 efopen(const char *name, const char *flags)
227 if (!(fp = fopen(name, flags)))
233 /* Escape characters below as HTML 2.0 / XML 1.0. */
235 xmlencode(FILE *fp, const char *s, size_t len)
239 for (i = 0; *s && i < len; s++, i++) {
241 case '<': fputs("<", fp); break;
242 case '>': fputs(">", fp); break;
243 case '\'': fputs("'", fp); break;
244 case '&': fputs("&", fp); break;
245 case '"': fputs(""", fp); break;
246 default: fputc(*s, fp);
252 mkdirp(const char *path)
254 char tmp[PATH_MAX], *p;
256 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
257 errx(1, "path truncated: '%s'", path);
258 for (p = tmp + (tmp[0] == '/'); *p; p++) {
262 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
266 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
272 printtimez(FILE *fp, const git_time *intime)
278 t = (time_t)intime->time;
279 if (!(intm = gmtime(&t)))
281 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
286 printtime(FILE *fp, const git_time *intime)
292 t = (time_t)intime->time + (intime->offset * 60);
293 if (!(intm = gmtime(&t)))
295 strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
296 if (intime->offset < 0)
297 fprintf(fp, "%s -%02d%02d", out,
298 -(intime->offset) / 60, -(intime->offset) % 60);
300 fprintf(fp, "%s +%02d%02d", out,
301 intime->offset / 60, intime->offset % 60);
305 printtimeshort(FILE *fp, const git_time *intime)
311 t = (time_t)intime->time;
312 if (!(intm = gmtime(&t)))
314 strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
319 writeheader(FILE *fp, const char *title)
321 fputs("<!DOCTYPE html>\n"
323 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
325 xmlencode(fp, title, strlen(title));
326 if (title[0] && strippedname[0])
328 xmlencode(fp, strippedname, strlen(strippedname));
331 xmlencode(fp, description, strlen(description));
332 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
333 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
335 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
336 fputs("</head>\n<body>\n<table><tr><td>", fp);
337 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
339 fputs("</td><td><h1>", fp);
340 xmlencode(fp, strippedname, strlen(strippedname));
341 fputs("</h1><span class=\"desc\">", fp);
342 xmlencode(fp, description, strlen(description));
343 fputs("</span></td></tr>", fp);
345 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
346 xmlencode(fp, cloneurl, strlen(cloneurl));
348 xmlencode(fp, cloneurl, strlen(cloneurl));
349 fputs("</a></td></tr>", fp);
351 fputs("<tr><td></td><td>\n", fp);
352 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
353 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
354 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
356 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
358 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
360 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
361 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
365 writefooter(FILE *fp)
367 fputs("</div>\n</body>\n</html>\n", fp);
371 writeblobhtml(FILE *fp, const git_blob *blob)
373 size_t n = 0, i, prev;
374 const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
375 const char *s = git_blob_rawcontent(blob);
376 git_off_t len = git_blob_rawsize(blob);
378 fputs("<pre id=\"blob\">\n", fp);
381 for (i = 0, prev = 0; i < (size_t)len; i++) {
385 fprintf(fp, nfmt, n, n, n);
386 xmlencode(fp, &s[prev], i - prev + 1);
390 if ((len - prev) > 0) {
392 fprintf(fp, nfmt, n, n, n);
393 xmlencode(fp, &s[prev], len - prev);
397 fputs("</pre>\n", fp);
403 printcommit(FILE *fp, struct commitinfo *ci)
405 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
406 relpath, ci->oid, ci->oid);
408 if (ci->parentoid[0])
409 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
410 relpath, ci->parentoid, ci->parentoid);
413 fputs("<b>Author:</b> ", fp);
414 xmlencode(fp, ci->author->name, strlen(ci->author->name));
415 fputs(" <<a href=\"mailto:", fp);
416 xmlencode(fp, ci->author->email, strlen(ci->author->email));
418 xmlencode(fp, ci->author->email, strlen(ci->author->email));
419 fputs("</a>>\n<b>Date:</b> ", fp);
420 printtime(fp, &(ci->author->when));
425 xmlencode(fp, ci->msg, strlen(ci->msg));
431 printshowfile(FILE *fp, struct commitinfo *ci)
433 const git_diff_delta *delta;
434 const git_diff_hunk *hunk;
435 const git_diff_line *line;
437 size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
445 if (ci->filecount > 1000 ||
446 ci->ndeltas > 1000 ||
447 ci->addcount > 100000 ||
448 ci->delcount > 100000) {
449 fputs("Diff is too large, output suppressed.\n", fp);
454 fputs("<b>Diffstat:</b>\n<table>", fp);
455 for (i = 0; i < ci->ndeltas; i++) {
456 delta = git_patch_get_delta(ci->deltas[i]->patch);
457 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
458 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
459 if (strcmp(delta->old_file.path, delta->new_file.path)) {
460 fputs(" -> ", fp);
461 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
464 add = ci->deltas[i]->addcount;
465 del = ci->deltas[i]->delcount;
467 total = sizeof(linestr) - 2;
468 if (changed > total) {
470 add = ((float)total / changed * add) + 1;
472 del = ((float)total / changed * del) + 1;
474 memset(&linestr, '+', add);
475 memset(&linestr[add], '-', del);
477 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
478 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
479 fwrite(&linestr, 1, add, fp);
480 fputs("</span><span class=\"d\">", fp);
481 fwrite(&linestr[add], 1, del, fp);
482 fputs("</span></td></tr>\n", fp);
484 fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
485 ci->filecount, ci->filecount == 1 ? "" : "s",
486 ci->addcount, ci->addcount == 1 ? "" : "s",
487 ci->delcount, ci->delcount == 1 ? "" : "s");
491 for (i = 0; i < ci->ndeltas; i++) {
492 patch = ci->deltas[i]->patch;
493 delta = git_patch_get_delta(patch);
494 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
495 i, relpath, delta->old_file.path, delta->old_file.path,
496 relpath, delta->new_file.path, delta->new_file.path);
498 /* check binary data */
499 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
500 fputs("Binary files differ.\n", fp);
504 nhunks = git_patch_num_hunks(patch);
505 for (j = 0; j < nhunks; j++) {
506 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
509 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
510 xmlencode(fp, hunk->header, hunk->header_len);
514 if (git_patch_get_line_in_hunk(&line, patch, j, k))
516 if (line->old_lineno == -1)
517 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
519 else if (line->new_lineno == -1)
520 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
524 xmlencode(fp, line->content, line->content_len);
525 if (line->old_lineno == -1 || line->new_lineno == -1)
533 writelogline(FILE *fp, struct commitinfo *ci)
535 fputs("<tr><td>", fp);
537 printtimeshort(fp, &(ci->author->when));
538 fputs("</td><td>", fp);
540 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
541 xmlencode(fp, ci->summary, strlen(ci->summary));
544 fputs("</td><td>", fp);
546 xmlencode(fp, ci->author->name, strlen(ci->author->name));
547 fputs("</td><td class=\"num\" align=\"right\">", fp);
548 fprintf(fp, "%zu", ci->filecount);
549 fputs("</td><td class=\"num\" align=\"right\">", fp);
550 fprintf(fp, "+%zu", ci->addcount);
551 fputs("</td><td class=\"num\" align=\"right\">", fp);
552 fprintf(fp, "-%zu", ci->delcount);
553 fputs("</td></tr>\n", fp);
557 writelog(FILE *fp, const git_oid *oid)
559 struct commitinfo *ci;
560 git_revwalk *w = NULL;
566 git_revwalk_new(&w, repo);
567 git_revwalk_push(w, oid);
568 git_revwalk_sorting(w, GIT_SORT_TIME);
569 git_revwalk_simplify_first_parent(w);
571 while (!git_revwalk_next(&id, w)) {
574 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
576 if (!(ci = commitinfo_getbyoid(&id)))
579 writelogline(fp, ci);
581 writelogline(wcachefp, ci);
585 r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
586 if (r == -1 || (size_t)r >= sizeof(path))
587 errx(1, "path truncated: 'commit/%s.html'", ci->oid);
589 /* check if file exists if so skip it */
590 if (access(path, F_OK)) {
591 fpfile = efopen(path, "w");
592 writeheader(fpfile, ci->summary);
593 fputs("<pre>", fpfile);
594 printshowfile(fpfile, ci);
595 fputs("</pre>\n", fpfile);
609 printcommitatom(FILE *fp, struct commitinfo *ci)
611 fputs("<entry>\n", fp);
613 fprintf(fp, "<id>%s</id>\n", ci->oid);
615 fputs("<published>", fp);
616 printtimez(fp, &(ci->author->when));
617 fputs("</published>\n", fp);
620 fputs("<updated>", fp);
621 printtimez(fp, &(ci->committer->when));
622 fputs("</updated>\n", fp);
625 fputs("<title type=\"text\">", fp);
626 xmlencode(fp, ci->summary, strlen(ci->summary));
627 fputs("</title>\n", fp);
629 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
633 fputs("<author><name>", fp);
634 xmlencode(fp, ci->author->name, strlen(ci->author->name));
635 fputs("</name>\n<email>", fp);
636 xmlencode(fp, ci->author->email, strlen(ci->author->email));
637 fputs("</email>\n</author>\n", fp);
640 fputs("<content type=\"text\">", fp);
641 fprintf(fp, "commit %s\n", ci->oid);
642 if (ci->parentoid[0])
643 fprintf(fp, "parent %s\n", ci->parentoid);
645 fputs("Author: ", fp);
646 xmlencode(fp, ci->author->name, strlen(ci->author->name));
648 xmlencode(fp, ci->author->email, strlen(ci->author->email));
649 fputs(">\nDate: ", fp);
650 printtime(fp, &(ci->author->when));
655 xmlencode(fp, ci->msg, strlen(ci->msg));
657 fputs("\n</content>\n</entry>\n", fp);
663 struct commitinfo *ci;
664 git_revwalk *w = NULL;
666 size_t i, m = 100; /* last 'm' commits */
668 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
669 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
670 xmlencode(fp, strippedname, strlen(strippedname));
671 fputs(", branch HEAD</title>\n<subtitle>", fp);
672 xmlencode(fp, description, strlen(description));
673 fputs("</subtitle>\n", fp);
675 git_revwalk_new(&w, repo);
676 git_revwalk_push_head(w);
677 git_revwalk_sorting(w, GIT_SORT_TIME);
678 git_revwalk_simplify_first_parent(w);
680 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
681 if (!(ci = commitinfo_getbyoid(&id)))
683 printcommitatom(fp, ci);
688 fputs("</feed>\n", fp);
694 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
696 char tmp[PATH_MAX] = "", *d;
701 if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
702 errx(1, "path truncated: '%s'", fpath);
703 if (!(d = dirname(tmp)))
708 for (p = fpath, tmp[0] = '\0'; *p; p++) {
709 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
710 errx(1, "path truncated: '../%s'", tmp);
714 fp = efopen(fpath, "w");
715 writeheader(fp, filename);
717 xmlencode(fp, filename, strlen(filename));
718 fprintf(fp, " (%juB)", (uintmax_t)filesize);
719 fputs("</p><hr/>", fp);
721 if (git_blob_is_binary((git_blob *)obj)) {
722 fputs("<p>Binary file.</p>\n", fp);
724 lc = writeblobhtml(fp, (git_blob *)obj);
737 filemode(git_filemode_t m)
739 static char mode[11];
741 memset(mode, '-', sizeof(mode) - 1);
752 else if (S_ISFIFO(m))
756 else if (S_ISSOCK(m))
761 if (m & S_IRUSR) mode[1] = 'r';
762 if (m & S_IWUSR) mode[2] = 'w';
763 if (m & S_IXUSR) mode[3] = 'x';
764 if (m & S_IRGRP) mode[4] = 'r';
765 if (m & S_IWGRP) mode[5] = 'w';
766 if (m & S_IXGRP) mode[6] = 'x';
767 if (m & S_IROTH) mode[7] = 'r';
768 if (m & S_IWOTH) mode[8] = 'w';
769 if (m & S_IXOTH) mode[9] = 'x';
771 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
772 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
773 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
779 writefilestree(FILE *fp, git_tree *tree, const char *path)
781 const git_tree_entry *entry = NULL;
782 git_submodule *module = NULL;
783 git_object *obj = NULL;
785 const char *entryname;
786 char filepath[PATH_MAX], entrypath[PATH_MAX];
790 count = git_tree_entrycount(tree);
791 for (i = 0; i < count; i++) {
792 if (!(entry = git_tree_entry_byindex(tree, i)) ||
793 !(entryname = git_tree_entry_name(entry)))
795 joinpath(entrypath, sizeof(entrypath), path, entryname);
797 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
799 if (r == -1 || (size_t)r >= sizeof(filepath))
800 errx(1, "path truncated: 'file/%s.html'", entrypath);
802 if (!git_tree_entry_to_object(&obj, repo, entry)) {
803 switch (git_object_type(obj)) {
808 ret = writefilestree(fp, (git_tree *)obj,
810 git_object_free(obj);
815 git_object_free(obj);
819 filesize = git_blob_rawsize((git_blob *)obj);
820 lc = writeblob(obj, filepath, entryname, filesize);
822 fputs("<tr><td>", fp);
823 fputs(filemode(git_tree_entry_filemode(entry)), fp);
824 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
825 xmlencode(fp, entrypath, strlen(entrypath));
826 fputs("</a></td><td class=\"num\" align=\"right\">", fp);
828 fprintf(fp, "%dL", lc);
830 fprintf(fp, "%juB", (uintmax_t)filesize);
831 fputs("</td></tr>\n", fp);
832 git_object_free(obj);
833 } else if (!git_submodule_lookup(&module, repo, entryname)) {
834 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
836 xmlencode(fp, entrypath, strlen(entrypath));
837 git_submodule_free(module);
838 fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
846 writefiles(FILE *fp, const git_oid *id)
848 git_tree *tree = NULL;
849 git_commit *commit = NULL;
852 fputs("<table id=\"files\"><thead>\n<tr>"
853 "<td><b>Mode</b></td><td><b>Name</b></td>"
854 "<td class=\"num\" align=\"right\"><b>Size</b></td>"
855 "</tr>\n</thead><tbody>\n", fp);
857 if (!git_commit_lookup(&commit, repo, id) &&
858 !git_commit_tree(&tree, commit))
859 ret = writefilestree(fp, tree, "");
861 fputs("</tbody></table>", fp);
863 git_commit_free(commit);
870 refs_cmp(const void *v1, const void *v2)
872 git_reference *r1 = (*(git_reference **)v1);
873 git_reference *r2 = (*(git_reference **)v2);
876 if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
879 return strcmp(git_reference_shorthand(r1),
880 git_reference_shorthand(r2));
886 struct commitinfo *ci;
887 const git_oid *id = NULL;
888 git_object *obj = NULL;
889 git_reference *dref = NULL, *r, *ref = NULL;
890 git_reference_iterator *it = NULL;
891 git_reference **refs = NULL;
892 size_t count, i, j, refcount;
893 const char *titles[] = { "Branches", "Tags" };
894 const char *ids[] = { "branches", "tags" };
897 if (git_reference_iterator_new(&it, repo))
900 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
901 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
903 refs[refcount] = ref;
905 git_reference_iterator_free(it);
907 /* sort by type then shorthand name */
908 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
910 for (j = 0; j < 2; j++) {
911 for (i = 0, count = 0; i < refcount; i++) {
912 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
913 !(git_reference_is_tag(refs[i]) && j == 1))
916 switch (git_reference_type(refs[i])) {
917 case GIT_REF_SYMBOLIC:
918 if (git_reference_resolve(&dref, refs[i]))
928 if (!git_reference_target(r) ||
929 git_reference_peel(&obj, r, GIT_OBJ_ANY))
931 if (!(id = git_object_id(obj)))
933 if (!(ci = commitinfo_getbyoid(id)))
936 /* print header if it has an entry (first). */
938 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
939 "<thead>\n<tr><td><b>Name</b></td>"
940 "<td><b>Last commit date</b></td>"
941 "<td><b>Author</b></td>\n</tr>\n"
947 name = git_reference_shorthand(r);
949 fputs("<tr><td>", fp);
950 xmlencode(fp, name, strlen(name));
951 fputs("</td><td>", fp);
953 printtimeshort(fp, &(ci->author->when));
954 fputs("</td><td>", fp);
956 xmlencode(fp, ci->author->name, strlen(ci->author->name));
957 fputs("</td></tr>\n", fp);
962 git_object_free(obj);
964 git_reference_free(dref);
969 fputs("</tbody></table><br/>", fp);
973 git_object_free(obj);
974 git_reference_free(dref);
976 for (i = 0; i < refcount; i++)
977 git_reference_free(refs[i]);
986 fprintf(stderr, "%s [-c cachefile] repodir\n", argv0);
991 main(int argc, char *argv[])
993 git_object *obj = NULL;
994 const git_oid *head = NULL;
995 const git_error *e = NULL;
997 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
998 char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1002 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1005 for (i = 1; i < argc; i++) {
1006 if (argv[i][0] != '-') {
1010 } else if (argv[i][1] == 'c') {
1013 cachefile = argv[++i];
1019 if (!realpath(repodir, repodirabs))
1024 if (git_repository_open_ext(&repo, repodir,
1025 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1027 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1032 if (!git_revparse_single(&obj, repo, "HEAD"))
1033 head = git_object_id(obj);
1034 git_object_free(obj);
1036 /* don't cache if there is no HEAD */
1040 /* use directory name as name */
1041 if ((name = strrchr(repodirabs, '/')))
1046 /* strip .git suffix */
1047 if (!(strippedname = strdup(name)))
1049 if ((p = strrchr(strippedname, '.')))
1050 if (!strcmp(p, ".git"))
1053 /* read description or .git/description */
1054 joinpath(path, sizeof(path), repodir, "description");
1055 if (!(fpread = fopen(path, "r"))) {
1056 joinpath(path, sizeof(path), repodir, ".git/description");
1057 fpread = fopen(path, "r");
1060 if (!fgets(description, sizeof(description), fpread))
1061 description[0] = '\0';
1065 /* read url or .git/url */
1066 joinpath(path, sizeof(path), repodir, "url");
1067 if (!(fpread = fopen(path, "r"))) {
1068 joinpath(path, sizeof(path), repodir, ".git/url");
1069 fpread = fopen(path, "r");
1072 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1074 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1079 haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1080 git_object_type(obj) == GIT_OBJ_BLOB);
1081 git_object_free(obj);
1084 hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1085 git_object_type(obj) == GIT_OBJ_BLOB);
1086 git_object_free(obj);
1088 hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1089 git_object_type(obj) == GIT_OBJ_BLOB);
1090 git_object_free(obj);
1093 fp = efopen("log.html", "w");
1095 mkdir("commit", 0755);
1096 writeheader(fp, "Log");
1097 fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1098 "<td><b>Commit message</b></td>"
1099 "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1100 "<td class=\"num\" align=\"right\"><b>+</b></td>"
1101 "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1104 /* read from cache file (does not need to exist) */
1105 if ((rcachefp = fopen(cachefile, "r"))) {
1106 if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1107 errx(1, "%s: no object id", cachefile);
1108 if (git_oid_fromstr(&lastoid, lastoidstr))
1109 errx(1, "%s: invalid object id", cachefile);
1112 /* write log to (temporary) cache */
1113 if ((fd = mkstemp(tmppath)) == -1)
1115 if (!(wcachefp = fdopen(fd, "w")))
1117 /* write last commit id (HEAD) */
1118 git_oid_tostr(buf, sizeof(buf), head);
1119 fprintf(wcachefp, "%s\n", buf);
1124 /* append previous log to log.html and the new cache */
1125 while (!feof(rcachefp)) {
1126 n = fread(buf, 1, sizeof(buf), rcachefp);
1127 if (ferror(rcachefp))
1129 if (fwrite(buf, 1, n, fp) != n ||
1130 fwrite(buf, 1, n, wcachefp) != n)
1141 fputs("</tbody></table>", fp);
1145 /* files for HEAD */
1146 fp = efopen("files.html", "w");
1147 writeheader(fp, "Files");
1149 writefiles(fp, head);
1153 /* summary page with branches and tags */
1154 fp = efopen("refs.html", "w");
1155 writeheader(fp, "Refs");
1161 fp = efopen("atom.xml", "w");
1165 /* rename new cache file on success */
1166 if (cachefile && rename(tmppath, cachefile))
1167 err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1170 git_repository_free(repo);
1171 git_libgit2_shutdown();