21 char oid[GIT_OID_HEXSZ + 1];
22 char parentoid[GIT_OID_HEXSZ + 1];
24 const git_signature *author;
28 git_diff_stats *stats;
32 git_tree *commit_tree;
33 git_tree *parent_tree;
40 static git_repository *repo;
42 static const char *relpath = "";
43 static const char *repodir;
46 static char *stripped_name;
47 static char description[255];
48 static char cloneurl[1024];
49 static int hasreadme, haslicense;
52 commitinfo_free(struct commitinfo *ci)
57 git_diff_stats_free(ci->stats);
58 git_diff_free(ci->diff);
59 git_tree_free(ci->commit_tree);
60 git_tree_free(ci->parent_tree);
61 git_commit_free(ci->commit);
65 commitinfo_getbyoid(const git_oid *id)
67 struct commitinfo *ci;
68 git_diff_options opts;
71 if (!(ci = calloc(1, sizeof(struct commitinfo))))
75 if (git_commit_lookup(&(ci->commit), repo, id))
78 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
79 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
81 ci->author = git_commit_author(ci->commit);
82 ci->summary = git_commit_summary(ci->commit);
83 ci->msg = git_commit_message(ci->commit);
85 if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
87 if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
88 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
92 ci->parent_tree = NULL;
95 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
96 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
97 if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
99 if (git_diff_get_stats(&(ci->stats), ci->diff))
102 ci->addcount = git_diff_stats_insertions(ci->stats);
103 ci->delcount = git_diff_stats_deletions(ci->stats);
104 ci->filecount = git_diff_stats_files_changed(ci->stats);
116 efopen(const char *name, const char *flags)
120 if (!(fp = fopen(name, flags)))
126 /* Escape characters below as HTML 2.0 / XML 1.0. */
128 xmlencode(FILE *fp, const char *s, size_t len)
132 for (i = 0; *s && i < len; s++, i++) {
134 case '<': fputs("<", fp); break;
135 case '>': fputs(">", fp); break;
136 case '\'': fputs("'", fp); break;
137 case '&': fputs("&", fp); break;
138 case '"': fputs(""", fp); break;
139 default: fputc(*s, fp);
144 /* Some implementations of dirname(3) return a pointer to a static
145 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
146 * This is a wrapper function that is compatible with both versions.
147 * The program will error out if dirname(3) failed, this can only happen
148 * with the OpenBSD version. */
150 xdirname(const char *path)
154 if (!(p = strdup(path)))
156 if (!(b = dirname(p)))
158 if (!(b = strdup(b)))
165 /* Some implementations of basename(3) return a pointer to a static
166 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
167 * This is a wrapper function that is compatible with both versions.
168 * The program will error out if basename(3) failed, this can only happen
169 * with the OpenBSD version. */
171 xbasename(const char *path)
175 if (!(p = strdup(path)))
177 if (!(b = basename(p)))
179 if (!(b = strdup(b)))
187 mkdirp(const char *path)
189 char tmp[PATH_MAX], *p;
191 strlcpy(tmp, path, sizeof(tmp));
192 for (p = tmp + (tmp[0] == '/'); *p; p++) {
196 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
200 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
206 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
212 t = (time_t) intime->time + (intime->offset * 60);
214 strftime(out, sizeof(out), fmt, intm);
219 printtimez(FILE *fp, const git_time *intime)
221 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
225 printtime(FILE *fp, const git_time *intime)
227 printtimeformat(fp, intime, "%a %b %e %T %Y");
231 printtimeshort(FILE *fp, const git_time *intime)
233 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
237 writeheader(FILE *fp)
239 fputs("<!DOCTYPE HTML>"
240 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
241 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
242 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
243 xmlencode(fp, stripped_name, strlen(stripped_name));
246 xmlencode(fp, description, strlen(description));
247 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
248 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
250 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
251 fputs("</head>\n<body>\n<table><tr><td>", fp);
252 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
254 fputs("</td><td><h1>", fp);
255 xmlencode(fp, stripped_name, strlen(stripped_name));
256 fputs("</h1><span class=\"desc\">", fp);
257 xmlencode(fp, description, strlen(description));
258 fputs("</span></td></tr>", fp);
260 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
261 xmlencode(fp, cloneurl, strlen(cloneurl));
263 xmlencode(fp, cloneurl, strlen(cloneurl));
264 fputs("</a></td></tr>", fp);
266 fputs("<tr><td></td><td>\n", fp);
267 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
268 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
269 fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
271 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
273 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
274 fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
280 writefooter(FILE *fp)
282 return !fputs("</div>\n</body>\n</html>\n", fp);
286 writeblobhtml(FILE *fp, const git_blob *blob)
290 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
291 const char *s = git_blob_rawcontent(blob);
292 git_off_t len = git_blob_rawsize(blob);
294 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
298 fprintf(fp, nfmt, n, n, n);
299 for (i = 0; i < len - 1; i++) {
302 fprintf(fp, nfmt, n, n, n);
307 fputs("</pre></td><td><pre>\n", fp);
308 xmlencode(fp, s, (size_t)len);
309 fputs("</pre></td></tr></table>\n", fp);
315 printcommit(FILE *fp, struct commitinfo *ci)
317 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
318 relpath, ci->oid, ci->oid);
320 if (ci->parentoid[0])
321 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
322 relpath, ci->parentoid, ci->parentoid);
325 fputs("<b>Author:</b> ", fp);
326 xmlencode(fp, ci->author->name, strlen(ci->author->name));
327 fputs(" <<a href=\"mailto:", fp);
328 xmlencode(fp, ci->author->email, strlen(ci->author->email));
330 xmlencode(fp, ci->author->email, strlen(ci->author->email));
331 fputs("</a>>\n<b>Date:</b> ", fp);
332 printtime(fp, &(ci->author->when));
337 xmlencode(fp, ci->msg, strlen(ci->msg));
343 printshowfile(FILE *fp, struct commitinfo *ci)
345 const git_diff_delta *delta;
346 const git_diff_hunk *hunk;
347 const git_diff_line *line;
350 size_t ndeltas, nhunks, nhunklines;
355 memset(&statsbuf, 0, sizeof(statsbuf));
359 !git_diff_stats_to_buf(&statsbuf, ci->stats,
360 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
361 if (statsbuf.ptr && statsbuf.ptr[0]) {
362 fputs("<b>Diffstat:</b>\n", fp);
363 xmlencode(fp, statsbuf.ptr, strlen(statsbuf.ptr));
369 ndeltas = git_diff_num_deltas(ci->diff);
370 for (i = 0; i < ndeltas; i++) {
371 if (git_patch_from_diff(&patch, ci->diff, i)) {
372 git_patch_free(patch);
376 delta = git_patch_get_delta(patch);
377 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
378 relpath, delta->old_file.path, delta->old_file.path,
379 relpath, delta->new_file.path, delta->new_file.path);
381 /* check binary data */
382 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
383 fputs("Binary files differ\n", fp);
384 git_patch_free(patch);
388 nhunks = git_patch_num_hunks(patch);
389 for (j = 0; j < nhunks; j++) {
390 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
393 fprintf(fp, "<a href=\"#h%zu\" id=\"h%zu\" class=\"h\">", j, j);
394 xmlencode(fp, hunk->header, hunk->header_len);
398 if (git_patch_get_line_in_hunk(&line, patch, j, k))
400 if (line->old_lineno == -1)
401 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
403 else if (line->new_lineno == -1)
404 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
408 xmlencode(fp, line->content, line->content_len);
409 if (line->old_lineno == -1 || line->new_lineno == -1)
413 git_patch_free(patch);
415 git_buf_free(&statsbuf);
421 writelog(FILE *fp, const git_oid *oid)
423 struct commitinfo *ci;
424 git_revwalk *w = NULL;
430 git_revwalk_new(&w, repo);
431 git_revwalk_push(w, oid);
432 git_revwalk_sorting(w, GIT_SORT_TIME);
433 git_revwalk_simplify_first_parent(w);
435 fputs("<table id=\"log\"><thead>\n<tr><td>Date</td><td>Commit message</td>"
436 "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
437 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
439 while (!git_revwalk_next(&id, w)) {
442 if (!(ci = commitinfo_getbyoid(&id)))
445 fputs("<tr><td>", fp);
447 printtimeshort(fp, &(ci->author->when));
448 fputs("</td><td>", fp);
450 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
451 if ((len = strlen(ci->summary)) > summarylen) {
452 xmlencode(fp, ci->summary, summarylen - 1);
455 xmlencode(fp, ci->summary, len);
459 fputs("</td><td>", fp);
461 xmlencode(fp, ci->author->name, strlen(ci->author->name));
462 fputs("</td><td class=\"num\">", fp);
463 fprintf(fp, "%zu", ci->filecount);
464 fputs("</td><td class=\"num\">", fp);
465 fprintf(fp, "+%zu", ci->addcount);
466 fputs("</td><td class=\"num\">", fp);
467 fprintf(fp, "-%zu", ci->delcount);
468 fputs("</td></tr>\n", fp);
472 snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
473 /* check if file exists if so skip it */
474 if (access(path, F_OK)) {
475 fpfile = efopen(path, "w");
477 fputs("<pre>", fpfile);
478 printshowfile(fpfile, ci);
479 fputs("</pre>\n", fpfile);
485 fputs("</tbody></table>", fp);
495 printcommitatom(FILE *fp, struct commitinfo *ci)
497 fputs("<entry>\n", fp);
499 fprintf(fp, "<id>%s</id>\n", ci->oid);
501 fputs("<updated>", fp);
502 printtimez(fp, &(ci->author->when));
503 fputs("</updated>\n", fp);
506 fputs("<title type=\"text\">", fp);
507 xmlencode(fp, ci->summary, strlen(ci->summary));
508 fputs("</title>\n", fp);
510 fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
514 fputs("<author><name>", fp);
515 xmlencode(fp, ci->author->name, strlen(ci->author->name));
516 fputs("</name>\n<email>", fp);
517 xmlencode(fp, ci->author->email, strlen(ci->author->email));
518 fputs("</email>\n</author>\n", fp);
521 fputs("<content type=\"text\">", fp);
522 fprintf(fp, "commit %s\n", ci->oid);
523 if (ci->parentoid[0])
524 fprintf(fp, "parent %s\n", ci->parentoid);
526 fputs("Author: ", fp);
527 xmlencode(fp, ci->author->name, strlen(ci->author->name));
529 xmlencode(fp, ci->author->email, strlen(ci->author->email));
530 fputs(">\nDate: ", fp);
531 printtime(fp, &(ci->author->when));
536 xmlencode(fp, ci->msg, strlen(ci->msg));
538 fputs("\n</content>\n", fp);
540 fputs("</entry>\n", fp);
546 struct commitinfo *ci;
547 git_revwalk *w = NULL;
549 size_t i, m = 100; /* max */
551 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
552 "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
553 xmlencode(fp, stripped_name, strlen(stripped_name));
554 fputs(", branch HEAD</title>\n<subtitle>", fp);
555 xmlencode(fp, description, strlen(description));
556 fputs("</subtitle>\n", fp);
558 git_revwalk_new(&w, repo);
559 git_revwalk_push_head(w);
560 git_revwalk_sorting(w, GIT_SORT_TIME);
561 git_revwalk_simplify_first_parent(w);
563 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
564 if (!(ci = commitinfo_getbyoid(&id)))
566 printcommitatom(fp, ci);
571 fputs("</feed>", fp);
577 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
579 char tmp[PATH_MAX] = "";
595 strlcat(tmp, "../", sizeof(tmp));
600 fp = efopen(fpath, "w");
603 xmlencode(fp, filename, strlen(filename));
604 fprintf(fp, " (%juB)", (uintmax_t)filesize);
605 fputs("</p><hr/>", fp);
607 if (git_blob_is_binary((git_blob *)obj)) {
608 fputs("<p>Binary file</p>\n", fp);
610 lc = writeblobhtml(fp, (git_blob *)obj);
623 filemode(git_filemode_t m)
625 static char mode[11];
627 memset(mode, '-', sizeof(mode) - 1);
638 else if (S_ISFIFO(m))
642 else if (S_ISSOCK(m))
647 if (m & S_IRUSR) mode[1] = 'r';
648 if (m & S_IWUSR) mode[2] = 'w';
649 if (m & S_IXUSR) mode[3] = 'x';
650 if (m & S_IRGRP) mode[4] = 'r';
651 if (m & S_IWGRP) mode[5] = 'w';
652 if (m & S_IXGRP) mode[6] = 'x';
653 if (m & S_IROTH) mode[7] = 'r';
654 if (m & S_IWOTH) mode[8] = 'w';
655 if (m & S_IXOTH) mode[9] = 'x';
657 if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
658 if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
659 if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
665 writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
667 const git_tree_entry *entry = NULL;
668 const char *entryname;
669 char filepath[PATH_MAX], entrypath[PATH_MAX];
670 git_object *obj = NULL;
675 count = git_tree_entrycount(tree);
676 for (i = 0; i < count; i++) {
677 if (!(entry = git_tree_entry_byindex(tree, i)) ||
678 git_tree_entry_to_object(&obj, repo, entry))
680 entryname = git_tree_entry_name(entry);
681 snprintf(entrypath, sizeof(entrypath), "%s%s%s",
682 path, path[0] ? "/" : "", entryname);
683 switch (git_object_type(obj)) {
688 ret = writefilestree(fp, (git_tree *)obj, branch,
690 git_object_free(obj);
695 git_object_free(obj);
699 snprintf(filepath, sizeof(filepath), "file/%s/%s.html",
702 snprintf(filepath, sizeof(filepath), "file/%s.html",
704 filesize = git_blob_rawsize((git_blob *)obj);
706 lc = writeblob(obj, filepath, entryname, filesize);
708 fputs("<tr><td>", fp);
709 fputs(filemode(git_tree_entry_filemode(entry)), fp);
710 fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
711 xmlencode(fp, entrypath, strlen(entrypath));
712 fputs("</a></td><td class=\"num\">", fp);
713 if (showlinecount && lc > 0)
714 fprintf(fp, "%dL", lc);
716 fprintf(fp, "%juB", (uintmax_t)filesize);
717 fputs("</td></tr>\n", fp);
724 writefiles(FILE *fp, const git_oid *id, const char *branch)
726 git_tree *tree = NULL;
727 git_commit *commit = NULL;
730 fputs("<table id=\"files\"><thead>\n<tr>"
731 "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
732 "</tr>\n</thead><tbody>\n", fp);
734 if (git_commit_lookup(&commit, repo, id) ||
735 git_commit_tree(&tree, commit))
737 ret = writefilestree(fp, tree, branch, "");
740 fputs("</tbody></table>", fp);
742 git_commit_free(commit);
749 refs_cmp(const void *v1, const void *v2)
751 git_reference *r1 = (*(git_reference **)v1);
752 git_reference *r2 = (*(git_reference **)v2);
755 t1 = git_reference_is_branch(r1);
756 t2 = git_reference_is_branch(r2);
761 return strcmp(git_reference_shorthand(r1),
762 git_reference_shorthand(r2));
768 struct commitinfo *ci;
769 const git_oid *id = NULL;
770 git_object *obj = NULL;
771 git_reference *dref = NULL, *r, *ref = NULL;
772 git_reference_iterator *it = NULL;
773 git_reference **refs = NULL;
774 size_t count, i, j, refcount = 0;
775 const char *titles[] = { "Branches", "Tags" };
776 const char *ids[] = { "branches", "tags" };
779 if (git_reference_iterator_new(&it, repo))
782 for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
783 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
785 refs[refcount] = ref;
787 git_reference_iterator_free(it);
789 /* sort by type then shorthand name */
790 qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
792 for (j = 0; j < 2; j++) {
793 for (i = 0, count = 0; i < refcount; i++) {
794 if (!(git_reference_is_branch(refs[i]) && j == 0) &&
795 !(git_reference_is_tag(refs[i]) && j == 1))
798 switch (git_reference_type(refs[i])) {
799 case GIT_REF_SYMBOLIC:
800 if (git_reference_resolve(&dref, refs[i]))
810 if (!(id = git_reference_target(r)))
812 if (git_reference_peel(&obj, r, GIT_OBJ_ANY))
814 if (!(id = git_object_id(obj)))
816 if (!(ci = commitinfo_getbyoid(id)))
819 /* print header if it has an entry (first). */
821 fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>Name</td>"
822 "<td>Last commit date</td><td>Author</td>\n</tr>\n</thead><tbody>\n",
827 name = git_reference_shorthand(r);
829 fputs("<tr><td>", fp);
830 xmlencode(fp, name, strlen(name));
831 fputs("</td><td>", fp);
833 printtimeshort(fp, &(ci->author->when));
834 fputs("</td><td>", fp);
836 xmlencode(fp, ci->author->name, strlen(ci->author->name));
837 fputs("</td></tr>\n", fp);
842 git_object_free(obj);
844 git_reference_free(dref);
849 fputs("</tbody></table><br/>", fp);
853 git_object_free(obj);
854 git_reference_free(dref);
856 for (i = 0; i < refcount; i++)
857 git_reference_free(refs[i]);
864 main(int argc, char *argv[])
866 git_object *obj = NULL;
867 const git_oid *head = NULL;
868 const git_error *e = NULL;
870 char path[PATH_MAX], *p;
874 fprintf(stderr, "%s <repodir>\n", argv[0]);
881 if ((status = git_repository_open_ext(&repo, repodir,
882 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
884 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
889 if (git_revparse_single(&obj, repo, "HEAD"))
891 head = git_object_id(obj);
892 git_object_free(obj);
894 /* use directory name as name */
895 name = xbasename(repodir);
897 /* strip .git suffix */
898 if (!(stripped_name = strdup(name)))
900 if ((p = strrchr(stripped_name, '.')))
901 if (!strcmp(p, ".git"))
904 /* read description or .git/description */
905 snprintf(path, sizeof(path), "%s%s%s",
906 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
907 if (!(fpread = fopen(path, "r"))) {
908 snprintf(path, sizeof(path), "%s%s%s",
909 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
910 fpread = fopen(path, "r");
913 if (!fgets(description, sizeof(description), fpread))
914 description[0] = '\0';
918 /* read url or .git/url */
919 snprintf(path, sizeof(path), "%s%s%s",
920 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "url");
921 if (!(fpread = fopen(path, "r"))) {
922 snprintf(path, sizeof(path), "%s%s%s",
923 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/url");
924 fpread = fopen(path, "r");
927 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
929 cloneurl[strcspn(cloneurl, "\n")] = '\0';
934 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
935 git_object_free(obj);
937 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
938 git_object_free(obj);
941 fp = efopen("log.html", "w");
944 mkdir("commit", 0755);
950 fp = efopen("files.html", "w");
952 writefiles(fp, head, "HEAD");
956 /* summary page with branches and tags */
957 fp = efopen("refs.html", "w");
964 fp = efopen("atom.xml", "w");
969 git_repository_free(repo);
970 git_libgit2_shutdown();