28 char oid[GIT_OID_HEXSZ + 1];
29 char parentoid[GIT_OID_HEXSZ + 1];
31 const git_signature *author;
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 *stripped_name;
56 static char description[255];
57 static char cloneurl[1024];
58 static int haslicense, hasreadme, hassubmodules;
61 deltainfo_free(struct deltainfo *di)
65 git_patch_free(di->patch);
71 commitinfo_getstats(struct commitinfo *ci)
74 const git_diff_delta *delta;
75 const git_diff_hunk *hunk;
76 const git_diff_line *line;
77 git_patch *patch = NULL;
78 size_t ndeltas, nhunks, nhunklines;
81 ndeltas = git_diff_num_deltas(ci->diff);
82 if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
85 for (i = 0; i < ndeltas; i++) {
86 if (!(di = calloc(1, sizeof(struct deltainfo))))
88 if (git_patch_from_diff(&patch, ci->diff, i)) {
89 git_patch_free(patch);
95 delta = git_patch_get_delta(patch);
97 /* check binary data */
98 if (delta->flags & GIT_DIFF_FLAG_BINARY)
101 nhunks = git_patch_num_hunks(patch);
102 for (j = 0; j < nhunks; j++) {
103 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
106 if (git_patch_get_line_in_hunk(&line, patch, j, k))
108 if (line->old_lineno == -1) {
111 } else if (line->new_lineno == -1) {
125 for (i = 0; i < ci->ndeltas; i++)
126 deltainfo_free(ci->deltas[i]);
138 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;
164 if (!(ci = calloc(1, sizeof(struct commitinfo))))
168 if (git_commit_lookup(&(ci->commit), repo, id))
171 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
172 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
174 ci->author = git_commit_author(ci->commit);
175 ci->summary = git_commit_summary(ci->commit);
176 ci->msg = git_commit_message(ci->commit);
178 oid = git_commit_tree_id(ci->commit);
179 if ((error = git_tree_lookup(&(ci->commit_tree), repo, oid)))
181 if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
182 oid = git_commit_tree_id(ci->parent);
183 if ((error = git_tree_lookup(&(ci->parent_tree), repo, oid))) {
185 ci->parent_tree = NULL;
189 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
190 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
191 if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
194 if (commitinfo_getstats(ci) == -1)
207 efopen(const char *name, const char *flags)
211 if (!(fp = fopen(name, flags)))
217 /* Escape characters below as HTML 2.0 / XML 1.0. */
219 xmlencode(FILE *fp, const char *s, size_t len)
223 for (i = 0; *s && i < len; s++, i++) {
225 case '<': fputs("<", fp); break;
226 case '>': fputs(">", fp); break;
227 case '\'': fputs("'", fp); break;
228 case '&': fputs("&", fp); break;
229 case '"': fputs(""", fp); break;
230 default: fputc(*s, fp);
235 /* Some implementations of dirname(3) return a pointer to a static
236 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
237 * This is a wrapper function that is compatible with both versions.
238 * The program will error out if dirname(3) failed, this can only happen
239 * with the OpenBSD version. */
241 xdirname(const char *path)
245 if (!(p = strdup(path)))
247 if (!(b = dirname(p)))
249 if (!(b = strdup(b)))
257 mkdirp(const char *path)
259 char tmp[PATH_MAX], *p;
261 if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
262 errx(1, "path truncated: '%s'", path);
263 for (p = tmp + (tmp[0] == '/'); *p; p++) {
267 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
271 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
277 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
283 t = (time_t) intime->time + (intime->offset * 60);
284 if (!(intm = gmtime(&t)))
286 strftime(out, sizeof(out), fmt, intm);
291 printtimez(FILE *fp, const git_time *intime)
293 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
297 printtime(FILE *fp, const git_time *intime)
299 printtimeformat(fp, intime, "%a %b %e %T %Y");
303 printtimeshort(FILE *fp, const git_time *intime)
305 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
309 writeheader(FILE *fp, const char *title)
311 fputs("<!DOCTYPE html>\n"
312 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
313 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
314 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
315 xmlencode(fp, title, strlen(title));
316 if (title[0] && stripped_name[0])
318 xmlencode(fp, stripped_name, strlen(stripped_name));
321 xmlencode(fp, description, strlen(description));
322 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
323 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
325 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
326 fputs("</head>\n<body>\n<table><tr><td>", fp);
327 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
329 fputs("</td><td><h1>", fp);
330 xmlencode(fp, stripped_name, strlen(stripped_name));
331 fputs("</h1><span class=\"desc\">", fp);
332 xmlencode(fp, description, strlen(description));
333 fputs("</span></td></tr>", fp);
335 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
336 xmlencode(fp, cloneurl, strlen(cloneurl));
338 xmlencode(fp, cloneurl, strlen(cloneurl));
339 fputs("</a></td></tr>", fp);
341 fputs("<tr><td></td><td>\n", fp);
342 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
343 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
344 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
346 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
348 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
350 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
351 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
357 writefooter(FILE *fp)
359 return !fputs("</div>\n</body>\n</html>\n", fp);
363 writeblobhtml(FILE *fp, const git_blob *blob)
367 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
368 const char *s = git_blob_rawcontent(blob);
369 git_off_t len = git_blob_rawsize(blob);
371 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
375 fprintf(fp, nfmt, n, n, n);
376 for (i = 0; i < len - 1; i++) {
379 fprintf(fp, nfmt, n, n, n);
384 fputs("</pre></td><td><pre>\n", fp);
385 xmlencode(fp, s, (size_t)len);
386 fputs("</pre></td></tr></table>\n", fp);
392 printcommit(FILE *fp, struct commitinfo *ci)
394 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
395 relpath, ci->oid, ci->oid);
397 if (ci->parentoid[0])
398 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
399 relpath, ci->parentoid, ci->parentoid);
402 fputs("<b>Author:</b> ", fp);
403 xmlencode(fp, ci->author->name, strlen(ci->author->name));
404 fputs(" <<a href=\"mailto:", fp);
405 xmlencode(fp, ci->author->email, strlen(ci->author->email));
407 xmlencode(fp, ci->author->email, strlen(ci->author->email));
408 fputs("</a>>\n<b>Date:</b> ", fp);
409 printtime(fp, &(ci->author->when));
414 xmlencode(fp, ci->msg, strlen(ci->msg));
420 printshowfile(FILE *fp, struct commitinfo *ci)
422 const git_diff_delta *delta;
423 const git_diff_hunk *hunk;
424 const git_diff_line *line;
426 size_t nhunks, nhunklines, changed, add, del, total;
435 if (ci->filecount > 1000 ||
436 ci->ndeltas > 1000 ||
437 ci->addcount > 100000 ||
438 ci->delcount > 100000) {
439 fprintf(fp, "(diff is too large, output suppressed)");
444 fputs("<b>Diffstat:</b>\n<table>", fp);
445 for (i = 0; i < ci->ndeltas; i++) {
446 delta = git_patch_get_delta(ci->deltas[i]->patch);
447 fputs("<tr><td>", fp);
448 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
449 if (strcmp(delta->old_file.path, delta->new_file.path)) {
450 fputs(" -> ", fp);
451 xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
454 add = ci->deltas[i]->addcount;
455 del = ci->deltas[i]->delcount;
457 total = sizeof(linestr) - 2;
458 if (changed > total) {
460 add = ((float)total / changed * add) + 1;
462 del = ((float)total / changed * del) + 1;
464 memset(&linestr, '+', add);
465 memset(&linestr[add], '-', del);
467 fprintf(fp, "</td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
468 ci->deltas[i]->addcount + ci->deltas[i]->delcount);
469 fwrite(&linestr, 1, add, fp);
470 fputs("</span><span class=\"d\">", fp);
471 fwrite(&linestr[add], 1, del, fp);
472 fputs("</span></td></tr>\n", fp);
474 fprintf(fp, "</table>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
475 ci->filecount, ci->filecount == 1 ? "" : "s",
476 ci->addcount, ci->addcount == 1 ? "" : "s",
477 ci->delcount, ci->delcount == 1 ? "" : "s");
481 for (i = 0; i < ci->ndeltas; i++) {
482 patch = ci->deltas[i]->patch;
483 delta = git_patch_get_delta(patch);
484 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
485 relpath, delta->old_file.path, delta->old_file.path,
486 relpath, delta->new_file.path, delta->new_file.path);
488 /* check binary data */
489 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
490 fputs("Binary files differ\n", fp);
494 nhunks = git_patch_num_hunks(patch);
495 for (j = 0; j < nhunks; j++) {
496 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
499 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
500 xmlencode(fp, hunk->header, hunk->header_len);
504 if (git_patch_get_line_in_hunk(&line, patch, j, k))
506 if (line->old_lineno == -1)
507 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
509 else if (line->new_lineno == -1)
510 fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
514 xmlencode(fp, line->content, line->content_len);
515 if (line->old_lineno == -1 || line->new_lineno == -1)
523 writelog(FILE *fp, const git_oid *oid)
525 struct commitinfo *ci;
526 git_revwalk *w = NULL;
533 git_revwalk_new(&w, repo);
534 git_revwalk_push(w, oid);
535 git_revwalk_sorting(w, GIT_SORT_TIME);
536 git_revwalk_simplify_first_parent(w);
538 fputs("<table id=\"log\"><thead>\n<tr><td>Date</td><td>Commit message</td>"
539 "<td>Author</td><td class=\"num\">Files</td><td class=\"num\">+</td>"
540 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
542 while (!git_revwalk_next(&id, w)) {
545 if (!(ci = commitinfo_getbyoid(&id)))
548 fputs("<tr><td>", fp);
550 printtimeshort(fp, &(ci->author->when));
551 fputs("</td><td>", fp);
553 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
554 if ((len = strlen(ci->summary)) > summarylen) {
555 xmlencode(fp, ci->summary, summarylen - 1);
558 xmlencode(fp, ci->summary, len);
562 fputs("</td><td>", fp);
564 xmlencode(fp, ci->author->name, strlen(ci->author->name));
565 fputs("</td><td class=\"num\">", fp);
566 fprintf(fp, "%zu", ci->filecount);
567 fputs("</td><td class=\"num\">", fp);
568 fprintf(fp, "+%zu", ci->addcount);
569 fputs("</td><td class=\"num\">", fp);
570 fprintf(fp, "-%zu", ci->delcount);
571 fputs("</td></tr>\n", fp);
575 r = snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
576 if (r == -1 || (size_t)r >= sizeof(path))
577 errx(1, "path truncated: 'commit/%s.html'", ci->oid);
579 /* check if file exists if so skip it */
580 if (access(path, F_OK)) {
581 fpfile = efopen(path, "w");
582 writeheader(fpfile, ci->summary);
583 fputs("<pre>", fpfile);
584 printshowfile(fpfile, ci);
585 fputs("</pre>\n", fpfile);
591 fputs("</tbody></table>", fp);
601 printcommitatom(FILE *fp, struct commitinfo *ci)
603 fputs("<entry>\n", fp);
605 fprintf(fp, "<id>%s</id>\n", ci->oid);
607 fputs("<updated>", fp);
608 printtimez(fp, &(ci->author->when));
609 fputs("</updated>\n", fp);
612 fputs("<title type=\"text\">", fp);
613 xmlencode(fp, ci->summary, strlen(ci->summary));
614 fputs("</title>\n", fp);
616 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
620 fputs("<author><name>", fp);
621 xmlencode(fp, ci->author->name, strlen(ci->author->name));
622 fputs("</name>\n<email>", fp);
623 xmlencode(fp, ci->author->email, strlen(ci->author->email));
624 fputs("</email>\n</author>\n", fp);
627 fputs("<content type=\"text\">", fp);
628 fprintf(fp, "commit %s\n", ci->oid);
629 if (ci->parentoid[0])
630 fprintf(fp, "parent %s\n", ci->parentoid);
632 fputs("Author: ", fp);
633 xmlencode(fp, ci->author->name, strlen(ci->author->name));
635 xmlencode(fp, ci->author->email, strlen(ci->author->email));
636 fputs(">\nDate: ", fp);
637 printtime(fp, &(ci->author->when));
642 xmlencode(fp, ci->msg, strlen(ci->msg));
644 fputs("\n</content>\n", fp);
646 fputs("</entry>\n", fp);
652 struct commitinfo *ci;
653 git_revwalk *w = NULL;
655 size_t i, m = 100; /* max */
657 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
658 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
659 xmlencode(fp, stripped_name, strlen(stripped_name));
660 fputs(", branch HEAD</title>\n<subtitle>", fp);
661 xmlencode(fp, description, strlen(description));
662 fputs("</subtitle>\n", fp);
664 git_revwalk_new(&w, repo);
665 git_revwalk_push_head(w);
666 git_revwalk_sorting(w, GIT_SORT_TIME);
667 git_revwalk_simplify_first_parent(w);
669 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
670 if (!(ci = commitinfo_getbyoid(&id)))
672 printcommitatom(fp, ci);
677 fputs("</feed>", fp);
683 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
685 char tmp[PATH_MAX] = "";
700 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
701 errx(1, "path truncated: '../%s'", tmp);
706 fp = efopen(fpath, "w");
707 writeheader(fp, filename);
709 xmlencode(fp, filename, strlen(filename));
710 fprintf(fp, " (%juB)", (uintmax_t)filesize);
711 fputs("</p><hr/>", fp);
713 if (git_blob_is_binary((git_blob *)obj)) {
714 fputs("<p>Binary file</p>\n", fp);
716 lc = writeblobhtml(fp, (git_blob *)obj);
729 filemode(git_filemode_t m)
731 static char mode[11];
733 memset(mode, '-', sizeof(mode) - 1);
744 else if (S_ISFIFO(m))
748 else if (S_ISSOCK(m))
753 if (m & S_IRUSR) mode[1] = 'r';
754 if (m & S_IWUSR) mode[2] = 'w';
755 if (m & S_IXUSR) mode[3] = 'x';
756 if (m & S_IRGRP) mode[4] = 'r';
757 if (m & S_IWGRP) mode[5] = 'w';
758 if (m & S_IXGRP) mode[6] = 'x';
759 if (m & S_IROTH) mode[7] = 'r';
760 if (m & S_IWOTH) mode[8] = 'w';
761 if (m & S_IXOTH) mode[9] = 'x';
763 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
764 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
765 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
771 writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
773 const git_tree_entry *entry = NULL;
774 git_submodule *module = NULL;
775 const char *entryname;
776 char filepath[PATH_MAX], entrypath[PATH_MAX];
777 git_object *obj = NULL;
782 count = git_tree_entrycount(tree);
783 for (i = 0; i < count; i++) {
784 if (!(entry = git_tree_entry_byindex(tree, i)) ||
785 !(entryname = git_tree_entry_name(entry)))
787 r = snprintf(entrypath, sizeof(entrypath), "%s%s%s",
788 path, path[0] ? "/" : "", entryname);
789 if (r == -1 || (size_t)r >= sizeof(entrypath))
790 errx(1, "path truncated: '%s%s%s'",
791 path, path[0] ? "/" : "", entryname);
793 r = snprintf(filepath, sizeof(filepath), "file/%s%s%s.html",
794 path, path[0] ? "/" : "", entryname);
795 if (r == -1 || (size_t)r >= sizeof(filepath))
796 errx(1, "path truncated: 'file/%s%s%s.html'",
797 path, path[0] ? "/" : "", entryname);
799 if (!git_tree_entry_to_object(&obj, repo, entry)) {
800 switch (git_object_type(obj)) {
805 ret = writefilestree(fp, (git_tree *)obj, branch,
807 git_object_free(obj);
812 git_object_free(obj);
816 filesize = git_blob_rawsize((git_blob *)obj);
817 lc = writeblob(obj, filepath, entryname, filesize);
819 fputs("<tr><td>", fp);
820 fputs(filemode(git_tree_entry_filemode(entry)), fp);
821 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
822 xmlencode(fp, entrypath, strlen(entrypath));
823 fputs("</a></td><td class=\"num\">", fp);
824 if (showlinecount && lc > 0)
825 fprintf(fp, "%dL", lc);
827 fprintf(fp, "%juB", (uintmax_t)filesize);
828 fputs("</td></tr>\n", fp);
829 } else if (!git_submodule_lookup(&module, repo, entryname)) {
830 fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
832 xmlencode(fp, entrypath, strlen(entrypath));
833 git_submodule_free(module);
834 fputs("</a></td><td class=\"num\"></td></tr>\n", fp);
842 writefiles(FILE *fp, const git_oid *id, const char *branch)
844 git_tree *tree = NULL;
845 git_commit *commit = NULL;
848 fputs("<table id=\"files\"><thead>\n<tr>"
849 "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
850 "</tr>\n</thead><tbody>\n", fp);
852 if (git_commit_lookup(&commit, repo, id) ||
853 git_commit_tree(&tree, commit))
855 ret = writefilestree(fp, tree, branch, "");
858 fputs("</tbody></table>", fp);
860 git_commit_free(commit);
867 refs_cmp(const void *v1, const void *v2)
869 git_reference *r1 = (*(git_reference **)v1);
870 git_reference *r2 = (*(git_reference **)v2);
873 t1 = git_reference_is_branch(r1);
874 t2 = 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 = 0;
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 (!(id = git_reference_target(r)))
930 if (git_reference_peel(&obj, r, GIT_OBJ_ANY))
932 if (!(id = git_object_id(obj)))
934 if (!(ci = commitinfo_getbyoid(id)))
937 /* print header if it has an entry (first). */
939 fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>Name</td>"
940 "<td>Last commit date</td><td>Author</td>\n</tr>\n</thead><tbody>\n",
945 name = git_reference_shorthand(r);
947 fputs("<tr><td>", fp);
948 xmlencode(fp, name, strlen(name));
949 fputs("</td><td>", fp);
951 printtimeshort(fp, &(ci->author->when));
952 fputs("</td><td>", fp);
954 xmlencode(fp, ci->author->name, strlen(ci->author->name));
955 fputs("</td></tr>\n", fp);
960 git_object_free(obj);
962 git_reference_free(dref);
967 fputs("</tbody></table><br/>", fp);
971 git_object_free(obj);
972 git_reference_free(dref);
974 for (i = 0; i < refcount; i++)
975 git_reference_free(refs[i]);
982 main(int argc, char *argv[])
984 git_object *obj = NULL;
985 const git_oid *head = NULL;
986 const git_error *e = NULL;
988 char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
992 fprintf(stderr, "%s <repodir>\n", argv[0]);
996 if (!realpath(repodir, repodirabs))
1001 if ((status = git_repository_open_ext(&repo, repodir,
1002 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
1004 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1009 if (git_revparse_single(&obj, repo, "HEAD"))
1011 head = git_object_id(obj);
1012 git_object_free(obj);
1014 /* use directory name as name */
1015 if ((name = strrchr(repodirabs, '/')))
1020 /* strip .git suffix */
1021 if (!(stripped_name = strdup(name)))
1023 if ((p = strrchr(stripped_name, '.')))
1024 if (!strcmp(p, ".git"))
1027 /* read description or .git/description */
1028 r = snprintf(path, sizeof(path), "%s%s%s",
1029 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
1030 if (r == -1 || (size_t)r >= sizeof(path))
1031 errx(1, "path truncated: '%s%s%s'",
1032 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
1033 if (!(fpread = fopen(path, "r"))) {
1034 r = snprintf(path, sizeof(path), "%s%s%s",
1035 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
1036 if (r == -1 || (size_t)r >= sizeof(path))
1037 errx(1, "path truncated: '%s%s%s'",
1038 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
1039 fpread = fopen(path, "r");
1042 if (!fgets(description, sizeof(description), fpread))
1043 description[0] = '\0';
1047 /* read url or .git/url */
1048 r = snprintf(path, sizeof(path), "%s%s%s",
1049 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "url");
1050 if (r == -1 || (size_t)r >= sizeof(path))
1051 errx(1, "path truncated: '%s%s%s'",
1052 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "url");
1053 if (!(fpread = fopen(path, "r"))) {
1054 r = snprintf(path, sizeof(path), "%s%s%s",
1055 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/url");
1056 if (r == -1 || (size_t)r >= sizeof(path))
1057 errx(1, "path truncated: '%s%s%s'",
1058 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/url");
1059 fpread = fopen(path, "r");
1062 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1064 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1069 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
1070 git_object_free(obj);
1072 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
1073 git_object_free(obj);
1074 hassubmodules = !git_revparse_single(&obj, repo, "HEAD:.gitmodules");
1075 git_object_free(obj);
1078 fp = efopen("log.html", "w");
1080 writeheader(fp, "Log");
1081 mkdir("commit", 0755);
1086 /* files for HEAD */
1087 fp = efopen("files.html", "w");
1088 writeheader(fp, "Files");
1089 writefiles(fp, head, "HEAD");
1093 /* summary page with branches and tags */
1094 fp = efopen("refs.html", "w");
1095 writeheader(fp, "Refs");
1101 fp = efopen("atom.xml", "w");
1106 git_repository_free(repo);
1107 git_libgit2_shutdown();