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;
45 static char name[255];
46 static char description[255];
47 static int hasreadme, haslicense;
50 commitinfo_free(struct commitinfo *ci)
55 git_diff_stats_free(ci->stats);
56 git_diff_free(ci->diff);
57 git_tree_free(ci->commit_tree);
58 git_tree_free(ci->parent_tree);
59 git_commit_free(ci->commit);
63 commitinfo_getbyoid(const git_oid *id)
65 struct commitinfo *ci;
66 git_diff_options opts;
69 if (!(ci = calloc(1, sizeof(struct commitinfo))))
73 if (git_commit_lookup(&(ci->commit), repo, id))
76 /* TODO: show tags when commit has it */
77 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
78 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
80 ci->author = git_commit_author(ci->commit);
81 ci->summary = git_commit_summary(ci->commit);
82 ci->msg = git_commit_message(ci->commit);
84 if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
85 goto err; /* TODO: handle error */
86 if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
87 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
91 ci->parent_tree = NULL;
94 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
95 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
96 if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
98 if (git_diff_get_stats(&(ci->stats), ci->diff))
101 ci->addcount = git_diff_stats_insertions(ci->stats);
102 ci->delcount = git_diff_stats_deletions(ci->stats);
103 ci->filecount = git_diff_stats_files_changed(ci->stats);
105 /* TODO: show tag when commit has it */
117 efopen(const char *name, const char *flags)
121 if (!(fp = fopen(name, flags)))
127 /* Escape characters below as HTML 2.0 / XML 1.0. */
129 xmlencode(FILE *fp, const char *s, size_t len)
133 for (i = 0; *s && i < len; s++, i++) {
135 case '<': fputs("<", fp); break;
136 case '>': fputs(">", fp); break;
137 case '\'': fputs("'", fp); break;
138 case '&': fputs("&", fp); break;
139 case '"': fputs(""", fp); break;
140 default: fputc(*s, fp);
145 /* Some implementations of basename(3) return a pointer to a static
146 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
147 * This is a wrapper function that is compatible with both versions.
148 * The program will error out if basename(3) failed, this can only happen
149 * with the OpenBSD version. */
151 xbasename(const char *path)
155 if (!(p = strdup(path)))
157 if (!(b = basename(p)))
159 if (!(b = strdup(b)))
167 mkdirp(const char *path)
169 char tmp[PATH_MAX], *p;
171 strlcpy(tmp, path, sizeof(tmp)); /* TODO: bring in libutil? */
172 for (p = tmp + (tmp[0] == '/'); *p; p++) {
176 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
180 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
186 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
192 t = (time_t) intime->time + (intime->offset * 60);
194 strftime(out, sizeof(out), fmt, intm);
199 printtimez(FILE *fp, const git_time *intime)
201 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
205 printtime(FILE *fp, const git_time *intime)
207 printtimeformat(fp, intime, "%a %b %e %T %Y");
211 printtimeshort(FILE *fp, const git_time *intime)
213 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
217 writeheader(FILE *fp)
219 fputs("<!DOCTYPE HTML>"
220 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
221 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
222 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
223 xmlencode(fp, name, strlen(name));
226 xmlencode(fp, description, strlen(description));
227 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
228 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
230 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
231 fputs("</head>\n<body>\n<table><tr><td>", fp);
232 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
234 fputs("</td><td><h1>", fp);
235 xmlencode(fp, name, strlen(name));
236 fputs("</h1><span class=\"desc\">", fp);
237 xmlencode(fp, description, strlen(description));
238 fputs("</span></td></tr><tr><td></td><td>\n", fp);
239 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
240 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
242 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
244 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
245 fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
251 writefooter(FILE *fp)
253 return !fputs("</div></body>\n</html>", fp);
257 writeblobhtml(FILE *fp, const git_blob *blob)
261 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
262 const char *s = git_blob_rawcontent(blob);
263 git_off_t len = git_blob_rawsize(blob);
265 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
268 fprintf(fp, nfmt, n, n, n);
269 while (i < len - 1) {
272 fprintf(fp, nfmt, n, n, n);
278 fputs("</pre></td><td><pre>\n", fp);
279 xmlencode(fp, s, (size_t)len);
280 fputs("</pre></td></tr></table>\n", fp);
284 printcommit(FILE *fp, struct commitinfo *ci)
286 /* TODO: show tag when commit has it */
287 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
288 relpath, ci->oid, ci->oid);
290 if (ci->parentoid[0])
291 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
292 relpath, ci->parentoid, ci->parentoid);
295 if ((count = (int)git_commit_parentcount(commit)) > 1) {
296 fprintf(fp, "<b>Merge:</b>");
297 for (i = 0; i < count; i++) {
298 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
299 fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
306 fprintf(fp, "<b>Author:</b> ");
307 xmlencode(fp, ci->author->name, strlen(ci->author->name));
308 fprintf(fp, " <<a href=\"mailto:");
309 xmlencode(fp, ci->author->email, strlen(ci->author->email));
311 xmlencode(fp, ci->author->email, strlen(ci->author->email));
312 fputs("</a>>\n<b>Date:</b> ", fp);
313 printtime(fp, &(ci->author->when));
319 xmlencode(fp, ci->msg, strlen(ci->msg));
325 printshowfile(struct commitinfo *ci)
327 const git_diff_delta *delta;
328 const git_diff_hunk *hunk;
329 const git_diff_line *line;
332 size_t ndeltas, nhunks, nhunklines;
337 snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
338 /* check if file exists if so skip it */
339 if (!access(path, F_OK))
342 fp = efopen(path, "w");
344 fputs("<pre>\n", fp);
347 memset(&statsbuf, 0, sizeof(statsbuf));
351 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
352 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
353 if (statsbuf.ptr && statsbuf.ptr[0]) {
354 fprintf(fp, "<b>Diffstat:</b>\n");
355 fputs(statsbuf.ptr, fp);
362 ndeltas = git_diff_num_deltas(ci->diff);
363 for (i = 0; i < ndeltas; i++) {
364 if (git_patch_from_diff(&patch, ci->diff, i)) {
365 git_patch_free(patch);
366 break; /* TODO: handle error */
369 delta = git_patch_get_delta(patch);
370 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
371 relpath, delta->old_file.path, delta->old_file.path,
372 relpath, delta->new_file.path, delta->new_file.path);
374 /* check binary data */
375 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
376 fputs("Binary files differ\n", fp);
377 git_patch_free(patch);
381 nhunks = git_patch_num_hunks(patch);
382 for (j = 0; j < nhunks; j++) {
383 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
384 break; /* TODO: handle error ? */
386 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
389 if (git_patch_get_line_in_hunk(&line, patch, j, k))
391 if (line->old_lineno == -1)
392 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
394 else if (line->new_lineno == -1)
395 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
399 xmlencode(fp, line->content, line->content_len);
400 if (line->old_lineno == -1 || line->new_lineno == -1)
404 git_patch_free(patch);
406 git_buf_free(&statsbuf);
408 fputs( "</pre>\n", fp);
417 struct commitinfo *ci;
418 git_revwalk *w = NULL;
422 mkdir("commit", 0755);
424 git_revwalk_new(&w, repo);
425 git_revwalk_push_head(w);
426 git_revwalk_sorting(w, GIT_SORT_TIME);
427 git_revwalk_simplify_first_parent(w);
429 /* TODO: also make "expanded" log ? (with message body) */
430 fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
431 "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
432 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
433 while (!git_revwalk_next(&id, w)) {
436 if (!(ci = commitinfo_getbyoid(&id)))
439 fputs("<tr><td>", fp);
441 printtimeshort(fp, &(ci->author->when));
442 fputs("</td><td>", fp);
444 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
445 if ((len = strlen(ci->summary)) > summarylen) {
446 xmlencode(fp, ci->summary, summarylen - 1);
449 xmlencode(fp, ci->summary, len);
453 fputs("</td><td>", fp);
455 xmlencode(fp, ci->author->name, strlen(ci->author->name));
456 fputs("</td><td class=\"num\">", fp);
457 fprintf(fp, "%zu", ci->filecount);
458 fputs("</td><td class=\"num\">", fp);
459 fprintf(fp, "+%zu", ci->addcount);
460 fputs("</td><td class=\"num\">", fp);
461 fprintf(fp, "-%zu", ci->delcount);
462 fputs("</td></tr>\n", fp);
469 fprintf(fp, "</tbody></table>");
476 printcommitatom(FILE *fp, struct commitinfo *ci)
478 fputs("<entry>\n", fp);
480 fprintf(fp, "<id>%s</id>\n", ci->oid);
482 fputs("<updated>", fp);
483 printtimez(fp, &(ci->author->when));
484 fputs("</updated>\n", fp);
487 fputs("<title type=\"text\">", fp);
488 xmlencode(fp, ci->summary, strlen(ci->summary));
489 fputs("</title>\n", fp);
492 fputs("<content type=\"text\">", fp);
493 fprintf(fp, "commit %s\n", ci->oid);
494 if (ci->parentoid[0])
495 fprintf(fp, "parent %s\n", ci->parentoid);
498 if ((count = (int)git_commit_parentcount(commit)) > 1) {
499 fprintf(fp, "Merge:");
500 for (i = 0; i < count; i++) {
501 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
502 fprintf(fp, " %s", buf);
509 fprintf(fp, "Author: ");
510 xmlencode(fp, ci->author->name, strlen(ci->author->name));
511 fprintf(fp, " <");
512 xmlencode(fp, ci->author->email, strlen(ci->author->email));
513 fprintf(fp, ">\nDate: ");
514 printtime(fp, &(ci->author->when));
519 xmlencode(fp, ci->msg, strlen(ci->msg));
520 fputs("\n</content>\n", fp);
522 fputs("<author><name>", fp);
523 xmlencode(fp, ci->author->name, strlen(ci->author->name));
524 fputs("</name>\n<email>", fp);
525 xmlencode(fp, ci->author->email, strlen(ci->author->email));
526 fputs("</email>\n</author>\n", fp);
528 fputs("</entry>\n", fp);
534 struct commitinfo *ci;
535 git_revwalk *w = NULL;
537 size_t i, m = 100; /* max */
539 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
540 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
541 xmlencode(fp, name, strlen(name));
542 fputs(", branch master</title>\n<subtitle>", fp);
544 xmlencode(fp, description, strlen(description));
545 fputs("</subtitle>\n", fp);
547 git_revwalk_new(&w, repo);
548 git_revwalk_push_head(w);
549 git_revwalk_sorting(w, GIT_SORT_TIME);
550 git_revwalk_simplify_first_parent(w);
552 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
553 if (!(ci = commitinfo_getbyoid(&id)))
555 printcommitatom(fp, ci);
560 fputs("</feed>", fp);
566 writeblob(git_object *obj, const char *filename, git_off_t filesize)
568 char fpath[PATH_MAX];
569 char tmp[PATH_MAX] = "";
573 snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
574 if (mkdirp(dirname(fpath)))
580 strlcat(tmp, "../", sizeof(tmp));
585 fp = efopen(fpath, "w");
588 xmlencode(fp, filename, strlen(filename));
589 fprintf(fp, " (%" PRIu32 "b)", filesize);
590 fputs("</p><hr/>", fp);
592 if (git_blob_is_binary((git_blob *)obj)) {
593 fprintf(fp, "<p>Binary file</p>\n");
595 writeblobhtml(fp, (git_blob *)obj);
608 writefilestree(FILE *fp, git_tree *tree, const char *path)
610 const git_tree_entry *entry = NULL;
611 const char *filename;
612 char filepath[PATH_MAX];
613 git_object *obj = NULL;
618 count = git_tree_entrycount(tree);
619 for (i = 0; i < count; i++) {
620 if (!(entry = git_tree_entry_byindex(tree, i)))
623 filename = git_tree_entry_name(entry);
624 if (git_tree_entry_to_object(&obj, repo, entry))
626 switch (git_object_type(obj)) {
630 ret = writefilestree(fp, (git_tree *)obj, filename);
631 git_object_free(obj);
636 git_object_free(obj);
640 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
644 filesize = git_blob_rawsize((git_blob *)obj);
646 fputs("<tr><td>", fp);
647 /* TODO: fancy print, like: "-rw-r--r--" */
648 fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
649 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
650 xmlencode(fp, filename, strlen(filename));
651 fputs(".html\">", fp);
652 xmlencode(fp, filename, strlen(filename));
653 fputs("</a></td><td class=\"num\">", fp);
654 fprintf(fp, "%" PRIu32, filesize);
655 fputs("</td></tr>\n", fp);
657 writeblob(obj, filename, filesize);
667 git_tree *tree = NULL;
668 git_object *obj = NULL;
669 git_commit *commit = NULL;
671 fputs("<table id=\"files\"><thead>\n"
672 "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
673 "</thead><tbody>\n", fp);
675 if (git_revparse_single(&obj, repo, "HEAD"))
677 id = git_object_id(obj);
678 if (git_commit_lookup(&commit, repo, id))
680 if (git_commit_tree(&tree, commit)) {
681 git_commit_free(commit);
684 git_commit_free(commit);
686 writefilestree(fp, tree, "");
688 git_commit_free(commit);
691 fputs("</tbody></table>", fp);
697 main(int argc, char *argv[])
699 git_object *obj = NULL;
700 const git_error *e = NULL;
702 char path[PATH_MAX], *p;
706 fprintf(stderr, "%s <repodir>\n", argv[0]);
713 if ((status = git_repository_open_ext(&repo, repodir,
714 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
716 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
720 /* use directory name as name */
721 p = xbasename(repodir);
722 snprintf(name, sizeof(name), "%s", p);
725 /* read description or .git/description */
726 snprintf(path, sizeof(path), "%s%s%s",
727 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
728 if (!(fpread = fopen(path, "r"))) {
729 snprintf(path, sizeof(path), "%s%s%s",
730 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
731 fpread = fopen(path, "r");
734 if (!fgets(description, sizeof(description), fpread))
735 description[0] = '\0';
740 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
741 git_object_free(obj);
743 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
744 git_object_free(obj);
746 fp = efopen("log.html", "w");
752 fp = efopen("files.html", "w");
759 fp = efopen("atom.xml", "w");
764 git_repository_free(repo);
765 git_libgit2_shutdown();