20 char oid[GIT_OID_HEXSZ + 1];
21 char parentoid[GIT_OID_HEXSZ + 1];
23 const git_signature *author;
27 git_diff_stats *stats;
31 git_tree *commit_tree;
32 git_tree *parent_tree;
39 static git_repository *repo;
41 static const char *relpath = "";
42 static const char *repodir;
44 static char name[255];
45 static char description[255];
46 static int hasreadme, haslicense;
49 commitinfo_free(struct commitinfo *ci)
54 git_diff_stats_free(ci->stats);
55 git_diff_free(ci->diff);
56 git_tree_free(ci->commit_tree);
57 git_tree_free(ci->parent_tree);
58 git_commit_free(ci->commit);
62 commitinfo_getbyoid(const git_oid *id)
64 struct commitinfo *ci;
65 git_diff_options opts;
68 if (!(ci = calloc(1, sizeof(struct commitinfo))))
72 if (git_commit_lookup(&(ci->commit), repo, id))
75 /* TODO: show tags when commit has it */
76 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
77 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
79 ci->author = git_commit_author(ci->commit);
80 ci->summary = git_commit_summary(ci->commit);
81 ci->msg = git_commit_message(ci->commit);
83 if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
84 goto err; /* TODO: handle error */
85 if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
86 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
90 ci->parent_tree = NULL;
93 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
94 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
95 if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
97 if (git_diff_get_stats(&(ci->stats), ci->diff))
100 ci->addcount = git_diff_stats_insertions(ci->stats);
101 ci->delcount = git_diff_stats_deletions(ci->stats);
102 ci->filecount = git_diff_stats_files_changed(ci->stats);
104 /* TODO: show tag when commit has it */
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 basename(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 basename(3) failed, this can only happen
148 * with the OpenBSD version. */
150 xbasename(const char *path)
154 if (!(p = strdup(path)))
156 if (!(b = basename(p)))
158 if (!(b = strdup(b)))
166 mkdirp(const char *path)
168 char tmp[PATH_MAX], *p;
170 strlcpy(tmp, path, sizeof(tmp)); /* TODO: bring in libutil? */
171 for (p = tmp + (tmp[0] == '/'); *p; p++) {
175 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
179 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
185 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
191 t = (time_t) intime->time + (intime->offset * 60);
193 strftime(out, sizeof(out), fmt, intm);
198 printtimez(FILE *fp, const git_time *intime)
200 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
204 printtime(FILE *fp, const git_time *intime)
206 printtimeformat(fp, intime, "%a %b %e %T %Y");
210 printtimeshort(FILE *fp, const git_time *intime)
212 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
216 writeheader(FILE *fp)
218 fputs("<!DOCTYPE HTML>"
219 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
220 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
221 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
222 xmlencode(fp, name, strlen(name));
225 xmlencode(fp, description, strlen(description));
226 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
227 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
229 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
230 fputs("</head>\n<body>\n<table><tr><td>", fp);
231 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
233 fputs("</td><td><h1>", fp);
234 xmlencode(fp, name, strlen(name));
235 fputs("</h1><span class=\"desc\">", fp);
236 xmlencode(fp, description, strlen(description));
237 fputs("</span></td></tr><tr><td></td><td>\n", fp);
238 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
239 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
241 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
243 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
244 fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
250 writefooter(FILE *fp)
252 return !fputs("</div></body>\n</html>", fp);
256 writeblobhtml(FILE *fp, const git_blob *blob)
260 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
261 const char *s = git_blob_rawcontent(blob);
262 git_off_t len = git_blob_rawsize(blob);
264 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
267 fprintf(fp, nfmt, n, n, n);
268 while (i < len - 1) {
271 fprintf(fp, nfmt, n, n, n);
277 fputs("</pre></td><td><pre>\n", fp);
278 xmlencode(fp, s, (size_t)len);
279 fputs("</pre></td></tr></table>\n", fp);
283 printcommit(FILE *fp, struct commitinfo *ci)
285 /* TODO: show tag when commit has it */
286 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
287 relpath, ci->oid, ci->oid);
289 if (ci->parentoid[0])
290 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
291 relpath, ci->parentoid, ci->parentoid);
294 if ((count = (int)git_commit_parentcount(commit)) > 1) {
295 fprintf(fp, "<b>Merge:</b>");
296 for (i = 0; i < count; i++) {
297 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
298 fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
305 fprintf(fp, "<b>Author:</b> ");
306 xmlencode(fp, ci->author->name, strlen(ci->author->name));
307 fprintf(fp, " <<a href=\"mailto:");
308 xmlencode(fp, ci->author->email, strlen(ci->author->email));
310 xmlencode(fp, ci->author->email, strlen(ci->author->email));
311 fputs("</a>>\n<b>Date:</b> ", fp);
312 printtime(fp, &(ci->author->when));
318 xmlencode(fp, ci->msg, strlen(ci->msg));
324 printshowfile(struct commitinfo *ci)
326 const git_diff_delta *delta;
327 const git_diff_hunk *hunk;
328 const git_diff_line *line;
331 size_t ndeltas, nhunks, nhunklines;
336 snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
337 /* check if file exists if so skip it */
338 if (!access(path, F_OK))
341 fp = efopen(path, "w");
343 fputs("<pre>\n", fp);
346 memset(&statsbuf, 0, sizeof(statsbuf));
350 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
351 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
352 if (statsbuf.ptr && statsbuf.ptr[0]) {
353 fprintf(fp, "<b>Diffstat:</b>\n");
354 fputs(statsbuf.ptr, fp);
361 ndeltas = git_diff_num_deltas(ci->diff);
362 for (i = 0; i < ndeltas; i++) {
363 if (git_patch_from_diff(&patch, ci->diff, i)) {
364 git_patch_free(patch);
365 break; /* TODO: handle error */
368 delta = git_patch_get_delta(patch);
369 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
370 relpath, delta->old_file.path, delta->old_file.path,
371 relpath, delta->new_file.path, delta->new_file.path);
373 /* check binary data */
374 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
375 fputs("Binary files differ\n", fp);
376 git_patch_free(patch);
380 nhunks = git_patch_num_hunks(patch);
381 for (j = 0; j < nhunks; j++) {
382 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
383 break; /* TODO: handle error ? */
385 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
388 if (git_patch_get_line_in_hunk(&line, patch, j, k))
390 if (line->old_lineno == -1)
391 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
393 else if (line->new_lineno == -1)
394 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
398 xmlencode(fp, line->content, line->content_len);
399 if (line->old_lineno == -1 || line->new_lineno == -1)
403 git_patch_free(patch);
405 git_buf_free(&statsbuf);
407 fputs( "</pre>\n", fp);
416 struct commitinfo *ci;
417 git_revwalk *w = NULL;
421 mkdir("commit", 0755);
423 git_revwalk_new(&w, repo);
424 git_revwalk_push_head(w);
425 git_revwalk_sorting(w, GIT_SORT_TIME);
426 git_revwalk_simplify_first_parent(w);
428 /* TODO: also make "expanded" log ? (with message body) */
429 fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
430 "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
431 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
432 while (!git_revwalk_next(&id, w)) {
435 if (!(ci = commitinfo_getbyoid(&id)))
438 fputs("<tr><td>", fp);
440 printtimeshort(fp, &(ci->author->when));
441 fputs("</td><td>", fp);
443 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
444 if ((len = strlen(ci->summary)) > summarylen) {
445 xmlencode(fp, ci->summary, summarylen - 1);
448 xmlencode(fp, ci->summary, len);
452 fputs("</td><td>", fp);
454 xmlencode(fp, ci->author->name, strlen(ci->author->name));
455 fputs("</td><td class=\"num\">", fp);
456 fprintf(fp, "%zu", ci->filecount);
457 fputs("</td><td class=\"num\">", fp);
458 fprintf(fp, "+%zu", ci->addcount);
459 fputs("</td><td class=\"num\">", fp);
460 fprintf(fp, "-%zu", ci->delcount);
461 fputs("</td></tr>\n", fp);
468 fprintf(fp, "</tbody></table>");
475 printcommitatom(FILE *fp, struct commitinfo *ci)
477 fputs("<entry>\n", fp);
479 fprintf(fp, "<id>%s</id>\n", ci->oid);
481 fputs("<updated>", fp);
482 printtimez(fp, &(ci->author->when));
483 fputs("</updated>\n", fp);
486 fputs("<title type=\"text\">", fp);
487 xmlencode(fp, ci->summary, strlen(ci->summary));
488 fputs("</title>\n", fp);
491 fputs("<content type=\"text\">", fp);
492 fprintf(fp, "commit %s\n", ci->oid);
493 if (ci->parentoid[0])
494 fprintf(fp, "parent %s\n", ci->parentoid);
497 if ((count = (int)git_commit_parentcount(commit)) > 1) {
498 fprintf(fp, "Merge:");
499 for (i = 0; i < count; i++) {
500 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
501 fprintf(fp, " %s", buf);
508 fprintf(fp, "Author: ");
509 xmlencode(fp, ci->author->name, strlen(ci->author->name));
510 fprintf(fp, " <");
511 xmlencode(fp, ci->author->email, strlen(ci->author->email));
512 fprintf(fp, ">\nDate: ");
513 printtime(fp, &(ci->author->when));
518 xmlencode(fp, ci->msg, strlen(ci->msg));
519 fputs("\n</content>\n", fp);
521 fputs("<author><name>", fp);
522 xmlencode(fp, ci->author->name, strlen(ci->author->name));
523 fputs("</name>\n<email>", fp);
524 xmlencode(fp, ci->author->email, strlen(ci->author->email));
525 fputs("</email>\n</author>\n", fp);
527 fputs("</entry>\n", fp);
533 struct commitinfo *ci;
534 git_revwalk *w = NULL;
536 size_t i, m = 100; /* max */
538 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
539 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
540 xmlencode(fp, name, strlen(name));
541 fputs(", branch master</title>\n<subtitle>", fp);
543 xmlencode(fp, description, strlen(description));
544 fputs("</subtitle>\n", fp);
546 git_revwalk_new(&w, repo);
547 git_revwalk_push_head(w);
548 git_revwalk_sorting(w, GIT_SORT_TIME);
549 git_revwalk_simplify_first_parent(w);
551 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
552 if (!(ci = commitinfo_getbyoid(&id)))
554 printcommitatom(fp, ci);
559 fputs("</feed>", fp);
565 writeblob(git_object *obj, const char *filename, git_off_t filesize)
567 char fpath[PATH_MAX];
568 char tmp[PATH_MAX] = "";
572 snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
573 if (mkdirp(dirname(fpath)))
579 strlcat(tmp, "../", sizeof(tmp));
584 fp = efopen(fpath, "w");
587 xmlencode(fp, filename, strlen(filename));
588 fprintf(fp, " (%" PRIu32 "b)", filesize);
589 fputs("</p><hr/>", fp);
591 if (git_blob_is_binary((git_blob *)obj)) {
592 fprintf(fp, "<p>Binary file</p>\n");
594 writeblobhtml(fp, (git_blob *)obj);
607 writefilestree(FILE *fp, git_tree *tree, const char *path)
609 const git_tree_entry *entry = NULL;
610 const char *filename;
611 char filepath[PATH_MAX];
612 git_object *obj = NULL;
617 count = git_tree_entrycount(tree);
618 for (i = 0; i < count; i++) {
619 if (!(entry = git_tree_entry_byindex(tree, i)))
622 filename = git_tree_entry_name(entry);
623 if (git_tree_entry_to_object(&obj, repo, entry))
625 switch (git_object_type(obj)) {
629 ret = writefilestree(fp, (git_tree *)obj, filename);
630 git_object_free(obj);
635 git_object_free(obj);
639 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
643 filesize = git_blob_rawsize((git_blob *)obj);
645 fputs("<tr><td>", fp);
646 /* TODO: fancy print, like: "-rw-r--r--" */
647 fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
648 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
649 xmlencode(fp, filename, strlen(filename));
650 fputs(".html\">", fp);
651 xmlencode(fp, filename, strlen(filename));
652 fputs("</a></td><td class=\"num\">", fp);
653 fprintf(fp, "%" PRIu32, filesize);
654 fputs("</td></tr>\n", fp);
656 writeblob(obj, filename, filesize);
666 git_tree *tree = NULL;
667 git_object *obj = NULL;
668 git_commit *commit = NULL;
670 fputs("<table id=\"files\"><thead>\n"
671 "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
672 "</thead><tbody>\n", fp);
674 if (git_revparse_single(&obj, repo, "HEAD"))
676 id = git_object_id(obj);
677 if (git_commit_lookup(&commit, repo, id))
679 if (git_commit_tree(&tree, commit)) {
680 git_commit_free(commit);
683 git_commit_free(commit);
685 writefilestree(fp, tree, "");
687 git_commit_free(commit);
690 fputs("</tbody></table>", fp);
696 main(int argc, char *argv[])
698 git_object *obj = NULL;
699 const git_error *e = NULL;
701 char path[PATH_MAX], *p;
705 fprintf(stderr, "%s <repodir>\n", argv[0]);
712 if ((status = git_repository_open_ext(&repo, repodir,
713 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
715 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
719 /* use directory name as name */
720 p = xbasename(repodir);
721 snprintf(name, sizeof(name), "%s", p);
724 /* read description or .git/description */
725 snprintf(path, sizeof(path), "%s%s%s",
726 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
727 if (!(fpread = fopen(path, "r"))) {
728 snprintf(path, sizeof(path), "%s%s%s",
729 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
730 fpread = fopen(path, "r");
733 if (!fgets(description, sizeof(description), fpread))
734 description[0] = '\0';
739 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
740 git_object_free(obj);
742 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
743 git_object_free(obj);
745 fp = efopen("log.html", "w");
751 fp = efopen("files.html", "w");
758 fp = efopen("atom.xml", "w");
763 git_repository_free(repo);
764 git_libgit2_shutdown();