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 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)))
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);
114 efopen(const char *name, const char *flags)
118 if (!(fp = fopen(name, flags)))
124 /* Escape characters below as HTML 2.0 / XML 1.0. */
126 xmlencode(FILE *fp, const char *s, size_t len)
130 for (i = 0; *s && i < len; s++, i++) {
132 case '<': fputs("<", fp); break;
133 case '>': fputs(">", fp); break;
134 case '\'': fputs("'", fp); break;
135 case '&': fputs("&", fp); break;
136 case '"': fputs(""", fp); break;
137 default: fputc(*s, fp);
142 /* Some implementations of basename(3) return a pointer to a static
143 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
144 * This is a wrapper function that is compatible with both versions.
145 * The program will error out if basename(3) failed, this can only happen
146 * with the OpenBSD version. */
148 xbasename(const char *path)
152 if (!(p = strdup(path)))
154 if (!(b = basename(p)))
156 if (!(b = strdup(b)))
164 mkdirp(const char *path)
166 char tmp[PATH_MAX], *p;
168 strlcpy(tmp, path, sizeof(tmp));
169 for (p = tmp + (tmp[0] == '/'); *p; p++) {
173 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
177 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
183 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
189 t = (time_t) intime->time + (intime->offset * 60);
191 strftime(out, sizeof(out), fmt, intm);
196 printtimez(FILE *fp, const git_time *intime)
198 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
202 printtime(FILE *fp, const git_time *intime)
204 printtimeformat(fp, intime, "%a %b %e %T %Y");
208 printtimeshort(FILE *fp, const git_time *intime)
210 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
214 writeheader(FILE *fp)
216 fputs("<!DOCTYPE HTML>"
217 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
218 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
219 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
220 xmlencode(fp, name, strlen(name));
223 xmlencode(fp, description, strlen(description));
224 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
225 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
227 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
228 fputs("</head>\n<body>\n<table><tr><td>", fp);
229 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
231 fputs("</td><td><h1>", fp);
232 xmlencode(fp, name, strlen(name));
233 fputs("</h1><span class=\"desc\">", fp);
234 xmlencode(fp, description, strlen(description));
235 fputs("</span></td></tr><tr><td></td><td>\n", fp);
236 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
237 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
239 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
241 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
242 fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
248 writefooter(FILE *fp)
250 return !fputs("</div></body>\n</html>", fp);
254 writeblobhtml(FILE *fp, const git_blob *blob)
258 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
259 const char *s = git_blob_rawcontent(blob);
260 git_off_t len = git_blob_rawsize(blob);
262 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
265 fprintf(fp, nfmt, n, n, n);
266 while (i < len - 1) {
269 fprintf(fp, nfmt, n, n, n);
275 fputs("</pre></td><td><pre>\n", fp);
276 xmlencode(fp, s, (size_t)len);
277 fputs("</pre></td></tr></table>\n", fp);
281 printcommit(FILE *fp, struct commitinfo *ci)
283 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
284 relpath, ci->oid, ci->oid);
286 if (ci->parentoid[0])
287 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
288 relpath, ci->parentoid, ci->parentoid);
291 if ((count = (int)git_commit_parentcount(commit)) > 1) {
292 fprintf(fp, "<b>Merge:</b>");
293 for (i = 0; i < count; i++) {
294 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
295 fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
302 fprintf(fp, "<b>Author:</b> ");
303 xmlencode(fp, ci->author->name, strlen(ci->author->name));
304 fprintf(fp, " <<a href=\"mailto:");
305 xmlencode(fp, ci->author->email, strlen(ci->author->email));
307 xmlencode(fp, ci->author->email, strlen(ci->author->email));
308 fputs("</a>>\n<b>Date:</b> ", fp);
309 printtime(fp, &(ci->author->when));
315 xmlencode(fp, ci->msg, strlen(ci->msg));
321 printshowfile(struct commitinfo *ci)
323 const git_diff_delta *delta;
324 const git_diff_hunk *hunk;
325 const git_diff_line *line;
328 size_t ndeltas, nhunks, nhunklines;
333 snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
334 /* check if file exists if so skip it */
335 if (!access(path, F_OK))
338 fp = efopen(path, "w");
340 fputs("<pre>\n", fp);
343 memset(&statsbuf, 0, sizeof(statsbuf));
347 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
348 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
349 if (statsbuf.ptr && statsbuf.ptr[0]) {
350 fprintf(fp, "<b>Diffstat:</b>\n");
351 fputs(statsbuf.ptr, fp);
358 ndeltas = git_diff_num_deltas(ci->diff);
359 for (i = 0; i < ndeltas; i++) {
360 if (git_patch_from_diff(&patch, ci->diff, i)) {
361 git_patch_free(patch);
365 delta = git_patch_get_delta(patch);
366 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
367 relpath, delta->old_file.path, delta->old_file.path,
368 relpath, delta->new_file.path, delta->new_file.path);
370 /* check binary data */
371 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
372 fputs("Binary files differ\n", fp);
373 git_patch_free(patch);
377 nhunks = git_patch_num_hunks(patch);
378 for (j = 0; j < nhunks; j++) {
379 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
382 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
385 if (git_patch_get_line_in_hunk(&line, patch, j, k))
387 if (line->old_lineno == -1)
388 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
390 else if (line->new_lineno == -1)
391 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
395 xmlencode(fp, line->content, line->content_len);
396 if (line->old_lineno == -1 || line->new_lineno == -1)
400 git_patch_free(patch);
402 git_buf_free(&statsbuf);
404 fputs( "</pre>\n", fp);
413 struct commitinfo *ci;
414 git_revwalk *w = NULL;
418 mkdir("commit", 0755);
420 git_revwalk_new(&w, repo);
421 git_revwalk_push_head(w);
422 git_revwalk_sorting(w, GIT_SORT_TIME);
423 git_revwalk_simplify_first_parent(w);
425 fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
426 "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
427 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
428 while (!git_revwalk_next(&id, w)) {
431 if (!(ci = commitinfo_getbyoid(&id)))
434 fputs("<tr><td>", fp);
436 printtimeshort(fp, &(ci->author->when));
437 fputs("</td><td>", fp);
439 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
440 if ((len = strlen(ci->summary)) > summarylen) {
441 xmlencode(fp, ci->summary, summarylen - 1);
444 xmlencode(fp, ci->summary, len);
448 fputs("</td><td>", fp);
450 xmlencode(fp, ci->author->name, strlen(ci->author->name));
451 fputs("</td><td class=\"num\">", fp);
452 fprintf(fp, "%zu", ci->filecount);
453 fputs("</td><td class=\"num\">", fp);
454 fprintf(fp, "+%zu", ci->addcount);
455 fputs("</td><td class=\"num\">", fp);
456 fprintf(fp, "-%zu", ci->delcount);
457 fputs("</td></tr>\n", fp);
464 fprintf(fp, "</tbody></table>");
471 printcommitatom(FILE *fp, struct commitinfo *ci)
473 fputs("<entry>\n", fp);
475 fprintf(fp, "<id>%s</id>\n", ci->oid);
477 fputs("<updated>", fp);
478 printtimez(fp, &(ci->author->when));
479 fputs("</updated>\n", fp);
482 fputs("<title type=\"text\">", fp);
483 xmlencode(fp, ci->summary, strlen(ci->summary));
484 fputs("</title>\n", fp);
487 fputs("<content type=\"text\">", fp);
488 fprintf(fp, "commit %s\n", ci->oid);
489 if (ci->parentoid[0])
490 fprintf(fp, "parent %s\n", ci->parentoid);
493 if ((count = (int)git_commit_parentcount(commit)) > 1) {
494 fprintf(fp, "Merge:");
495 for (i = 0; i < count; i++) {
496 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
497 fprintf(fp, " %s", buf);
504 fprintf(fp, "Author: ");
505 xmlencode(fp, ci->author->name, strlen(ci->author->name));
506 fprintf(fp, " <");
507 xmlencode(fp, ci->author->email, strlen(ci->author->email));
508 fprintf(fp, ">\nDate: ");
509 printtime(fp, &(ci->author->when));
514 xmlencode(fp, ci->msg, strlen(ci->msg));
515 fputs("\n</content>\n", fp);
517 fputs("<author><name>", fp);
518 xmlencode(fp, ci->author->name, strlen(ci->author->name));
519 fputs("</name>\n<email>", fp);
520 xmlencode(fp, ci->author->email, strlen(ci->author->email));
521 fputs("</email>\n</author>\n", fp);
523 fputs("</entry>\n", fp);
529 struct commitinfo *ci;
530 git_revwalk *w = NULL;
532 size_t i, m = 100; /* max */
534 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
535 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
536 xmlencode(fp, name, strlen(name));
537 fputs(", branch master</title>\n<subtitle>", fp);
539 xmlencode(fp, description, strlen(description));
540 fputs("</subtitle>\n", fp);
542 git_revwalk_new(&w, repo);
543 git_revwalk_push_head(w);
544 git_revwalk_sorting(w, GIT_SORT_TIME);
545 git_revwalk_simplify_first_parent(w);
547 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
548 if (!(ci = commitinfo_getbyoid(&id)))
550 printcommitatom(fp, ci);
555 fputs("</feed>", fp);
561 writeblob(git_object *obj, const char *filename, git_off_t filesize)
563 char fpath[PATH_MAX];
564 char tmp[PATH_MAX] = "";
568 snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
569 if (mkdirp(dirname(fpath)))
575 strlcat(tmp, "../", sizeof(tmp));
580 fp = efopen(fpath, "w");
583 xmlencode(fp, filename, strlen(filename));
584 fprintf(fp, " (%" PRIu32 "b)", filesize);
585 fputs("</p><hr/>", fp);
587 if (git_blob_is_binary((git_blob *)obj)) {
588 fprintf(fp, "<p>Binary file</p>\n");
590 writeblobhtml(fp, (git_blob *)obj);
603 writefilestree(FILE *fp, git_tree *tree, const char *path)
605 const git_tree_entry *entry = NULL;
606 const char *filename;
607 char filepath[PATH_MAX];
608 git_object *obj = NULL;
613 count = git_tree_entrycount(tree);
614 for (i = 0; i < count; i++) {
615 if (!(entry = git_tree_entry_byindex(tree, i)))
618 filename = git_tree_entry_name(entry);
619 if (git_tree_entry_to_object(&obj, repo, entry))
621 switch (git_object_type(obj)) {
625 ret = writefilestree(fp, (git_tree *)obj, filename);
626 git_object_free(obj);
631 git_object_free(obj);
635 snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
639 filesize = git_blob_rawsize((git_blob *)obj);
641 fputs("<tr><td>", fp);
642 fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
643 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
644 xmlencode(fp, filename, strlen(filename));
645 fputs(".html\">", fp);
646 xmlencode(fp, filename, strlen(filename));
647 fputs("</a></td><td class=\"num\">", fp);
648 fprintf(fp, "%" PRIu32, filesize);
649 fputs("</td></tr>\n", fp);
651 writeblob(obj, filename, filesize);
661 git_tree *tree = NULL;
662 git_object *obj = NULL;
663 git_commit *commit = NULL;
665 fputs("<table id=\"files\"><thead>\n"
666 "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
667 "</thead><tbody>\n", fp);
669 if (git_revparse_single(&obj, repo, "HEAD"))
671 id = git_object_id(obj);
672 if (git_commit_lookup(&commit, repo, id))
674 if (git_commit_tree(&tree, commit)) {
675 git_commit_free(commit);
678 git_commit_free(commit);
680 writefilestree(fp, tree, "");
682 git_commit_free(commit);
685 fputs("</tbody></table>", fp);
691 main(int argc, char *argv[])
693 git_object *obj = NULL;
694 const git_error *e = NULL;
696 char path[PATH_MAX], *p;
700 fprintf(stderr, "%s <repodir>\n", argv[0]);
707 if ((status = git_repository_open_ext(&repo, repodir,
708 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
710 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
714 /* use directory name as name */
715 p = xbasename(repodir);
716 snprintf(name, sizeof(name), "%s", p);
719 /* read description or .git/description */
720 snprintf(path, sizeof(path), "%s%s%s",
721 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
722 if (!(fpread = fopen(path, "r"))) {
723 snprintf(path, sizeof(path), "%s%s%s",
724 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
725 fpread = fopen(path, "r");
728 if (!fgets(description, sizeof(description), fpread))
729 description[0] = '\0';
734 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
735 git_object_free(obj);
737 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
738 git_object_free(obj);
740 fp = efopen("log.html", "w");
746 fp = efopen("files.html", "w");
753 fp = efopen("atom.xml", "w");
758 git_repository_free(repo);
759 git_libgit2_shutdown();