19 char oid[GIT_OID_HEXSZ + 1];
20 char parentoid[GIT_OID_HEXSZ + 1];
22 const git_signature *author;
26 git_diff_stats *stats;
30 git_tree *commit_tree;
31 git_tree *parent_tree;
38 static git_repository *repo;
40 static const char *relpath = "";
41 static const char *repodir;
43 static char name[255];
44 static char description[255];
45 static int hasreadme, haslicense;
48 commitinfo_free(struct commitinfo *ci)
53 git_diff_stats_free(ci->stats);
54 git_diff_free(ci->diff);
55 git_tree_free(ci->commit_tree);
56 git_tree_free(ci->parent_tree);
57 git_commit_free(ci->commit);
61 commitinfo_getbyoid(const git_oid *id)
63 struct commitinfo *ci;
64 git_diff_options opts;
67 if (!(ci = calloc(1, sizeof(struct commitinfo))))
71 if (git_commit_lookup(&(ci->commit), repo, id))
74 /* TODO: show tags when commit has it */
75 git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
76 git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
78 ci->author = git_commit_author(ci->commit);
79 ci->summary = git_commit_summary(ci->commit);
80 ci->msg = git_commit_message(ci->commit);
82 if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
83 goto err; /* TODO: handle error */
84 if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
85 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
89 ci->parent_tree = NULL;
92 git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
93 opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
94 if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
96 if (git_diff_get_stats(&(ci->stats), ci->diff))
99 ci->addcount = git_diff_stats_insertions(ci->stats);
100 ci->delcount = git_diff_stats_deletions(ci->stats);
101 ci->filecount = git_diff_stats_files_changed(ci->stats);
103 /* TODO: show tag when commit has it */
115 efopen(const char *name, const char *flags)
119 if (!(fp = fopen(name, flags)))
125 /* Escape characters below as HTML 2.0 / XML 1.0. */
127 xmlencode(FILE *fp, const char *s, size_t len)
131 for (i = 0; *s && i < len; s++, i++) {
133 case '<': fputs("<", fp); break;
134 case '>': fputs(">", fp); break;
135 case '\'': fputs("'", fp); break;
136 case '&': fputs("&", fp); break;
137 case '"': fputs(""", fp); break;
138 default: fputc(*s, fp);
143 /* Some implementations of basename(3) return a pointer to a static
144 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
145 * This is a wrapper function that is compatible with both versions.
146 * The program will error out if basename(3) failed, this can only happen
147 * with the OpenBSD version. */
149 xbasename(const char *path)
153 if (!(p = strdup(path)))
155 if (!(b = basename(p)))
157 if (!(b = strdup(b)))
165 mkdirp(const char *path)
167 char tmp[PATH_MAX], *p;
169 strlcpy(tmp, path, sizeof(tmp)); /* TODO: bring in libutil? */
170 for (p = tmp + (tmp[0] == '/'); *p; p++) {
174 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
178 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
184 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
190 t = (time_t) intime->time + (intime->offset * 60);
192 strftime(out, sizeof(out), fmt, intm);
197 printtimez(FILE *fp, const git_time *intime)
199 printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
203 printtime(FILE *fp, const git_time *intime)
205 printtimeformat(fp, intime, "%a %b %e %T %Y");
209 printtimeshort(FILE *fp, const git_time *intime)
211 printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
215 writeheader(FILE *fp)
217 fputs("<!DOCTYPE HTML>"
218 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
219 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
220 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
221 xmlencode(fp, name, strlen(name));
224 xmlencode(fp, description, strlen(description));
225 fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
226 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
228 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
229 fputs("</head>\n<body>\n\n<table><tr><td>", fp);
230 fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
232 fputs("</td><td><h1>", fp);
233 xmlencode(fp, name, strlen(name));
234 fputs("</h1><span class=\"desc\">", fp);
235 xmlencode(fp, description, strlen(description));
236 fputs("</span></td></tr><tr><td></td><td>\n", fp);
237 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
238 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
240 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
242 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
243 fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
249 writefooter(FILE *fp)
251 return !fputs("</div></body>\n</html>", fp);
255 writeblobhtml(FILE *fp, const git_blob *blob)
259 char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
260 const char *s = git_blob_rawcontent(blob);
261 git_off_t len = git_blob_rawsize(blob);
263 fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
266 fprintf(fp, nfmt, n, n, n);
267 while (i < len - 1) {
270 fprintf(fp, nfmt, n, n, n);
276 fputs("</pre></td><td><pre>\n", fp);
277 xmlencode(fp, s, (size_t)len);
278 fputs("</pre></td></tr></table>\n", fp);
282 printcommit(FILE *fp, struct commitinfo *ci)
284 /* TODO: show tag when commit has it */
285 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
286 relpath, ci->oid, ci->oid);
288 if (ci->parentoid[0])
289 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
290 relpath, ci->parentoid, ci->parentoid);
293 if ((count = (int)git_commit_parentcount(commit)) > 1) {
294 fprintf(fp, "<b>Merge:</b>");
295 for (i = 0; i < count; i++) {
296 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
297 fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
304 fprintf(fp, "<b>Author:</b> ");
305 xmlencode(fp, ci->author->name, strlen(ci->author->name));
306 fprintf(fp, " <<a href=\"mailto:");
307 xmlencode(fp, ci->author->email, strlen(ci->author->email));
309 xmlencode(fp, ci->author->email, strlen(ci->author->email));
310 fputs("</a>>\n<b>Date:</b> ", fp);
311 printtime(fp, &(ci->author->when));
317 xmlencode(fp, ci->msg, strlen(ci->msg));
323 printshowfile(struct commitinfo *ci)
325 const git_diff_delta *delta;
326 const git_diff_hunk *hunk;
327 const git_diff_line *line;
330 size_t ndeltas, nhunks, nhunklines;
335 snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
336 /* check if file exists if so skip it */
337 if (!access(path, F_OK))
340 fp = efopen(path, "w");
342 fputs("<pre>\n", fp);
345 memset(&statsbuf, 0, sizeof(statsbuf));
349 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
350 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
351 if (statsbuf.ptr && statsbuf.ptr[0]) {
352 fprintf(fp, "<b>Diffstat:</b>\n");
353 fputs(statsbuf.ptr, fp);
360 ndeltas = git_diff_num_deltas(ci->diff);
361 for (i = 0; i < ndeltas; i++) {
362 if (git_patch_from_diff(&patch, ci->diff, i)) {
363 git_patch_free(patch);
364 break; /* TODO: handle error */
367 delta = git_patch_get_delta(patch);
368 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
369 relpath, delta->old_file.path, delta->old_file.path,
370 relpath, delta->new_file.path, delta->new_file.path);
372 /* check binary data */
373 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
374 fputs("Binary files differ\n", fp);
375 git_patch_free(patch);
379 nhunks = git_patch_num_hunks(patch);
380 for (j = 0; j < nhunks; j++) {
381 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
382 break; /* TODO: handle error ? */
384 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
387 if (git_patch_get_line_in_hunk(&line, patch, j, k))
389 if (line->old_lineno == -1)
390 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
392 else if (line->new_lineno == -1)
393 fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
397 xmlencode(fp, line->content, line->content_len);
398 if (line->old_lineno == -1 || line->new_lineno == -1)
402 git_patch_free(patch);
404 git_buf_free(&statsbuf);
406 fputs( "</pre>\n", fp);
415 struct commitinfo *ci;
416 git_revwalk *w = NULL;
420 mkdir("commit", 0755);
422 git_revwalk_new(&w, repo);
423 git_revwalk_push_head(w);
424 git_revwalk_sorting(w, GIT_SORT_TIME);
425 git_revwalk_simplify_first_parent(w);
427 /* TODO: also make "expanded" log ? (with message body) */
428 fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
429 "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
430 "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
431 while (!git_revwalk_next(&id, w)) {
434 if (!(ci = commitinfo_getbyoid(&id)))
437 fputs("<tr><td>", fp);
439 printtimeshort(fp, &(ci->author->when));
440 fputs("</td><td>", fp);
442 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
443 if ((len = strlen(ci->summary)) > summarylen) {
444 xmlencode(fp, ci->summary, summarylen - 1);
447 xmlencode(fp, ci->summary, len);
451 fputs("</td><td>", fp);
453 xmlencode(fp, ci->author->name, strlen(ci->author->name));
454 fputs("</td><td class=\"num\">", fp);
455 fprintf(fp, "%zu", ci->filecount);
456 fputs("</td><td class=\"num\">", fp);
457 fprintf(fp, "+%zu", ci->addcount);
458 fputs("</td><td class=\"num\">", fp);
459 fprintf(fp, "-%zu", ci->delcount);
460 fputs("</td></tr>\n", fp);
467 fprintf(fp, "</tbody></table>");
474 printcommitatom(FILE *fp, struct commitinfo *ci)
476 fputs("<entry>\n", fp);
478 fprintf(fp, "<id>%s</id>\n", ci->oid);
480 fputs("<updated>", fp);
481 printtimez(fp, &(ci->author->when));
482 fputs("</updated>\n", fp);
485 fputs("<title type=\"text\">", fp);
486 xmlencode(fp, ci->summary, strlen(ci->summary));
487 fputs("</title>\n", fp);
490 fputs("<content type=\"text\">", fp);
491 fprintf(fp, "commit %s\n", ci->oid);
492 if (ci->parentoid[0])
493 fprintf(fp, "parent %s\n", ci->parentoid);
496 if ((count = (int)git_commit_parentcount(commit)) > 1) {
497 fprintf(fp, "Merge:");
498 for (i = 0; i < count; i++) {
499 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
500 fprintf(fp, " %s", buf);
507 fprintf(fp, "Author: ");
508 xmlencode(fp, ci->author->name, strlen(ci->author->name));
509 fprintf(fp, " <");
510 xmlencode(fp, ci->author->email, strlen(ci->author->email));
511 fprintf(fp, ">\nDate: ");
512 printtime(fp, &(ci->author->when));
517 xmlencode(fp, ci->msg, strlen(ci->msg));
518 fputs("\n</content>\n", fp);
520 fputs("<author><name>", fp);
521 xmlencode(fp, ci->author->name, strlen(ci->author->name));
522 fputs("</name>\n<email>", fp);
523 xmlencode(fp, ci->author->email, strlen(ci->author->email));
524 fputs("</email>\n</author>\n", fp);
526 fputs("</entry>\n", fp);
532 struct commitinfo *ci;
533 git_revwalk *w = NULL;
535 size_t i, m = 100; /* max */
537 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
538 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
539 xmlencode(fp, name, strlen(name));
540 fputs(", branch master</title>\n<subtitle>", fp);
542 xmlencode(fp, description, strlen(description));
543 fputs("</subtitle>\n", fp);
545 git_revwalk_new(&w, repo);
546 git_revwalk_push_head(w);
547 git_revwalk_sorting(w, GIT_SORT_TIME);
548 git_revwalk_simplify_first_parent(w);
550 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
551 if (!(ci = commitinfo_getbyoid(&id)))
553 printcommitatom(fp, ci);
558 fputs("</feed>", fp);
564 writeblob(const git_index_entry *entry)
566 char fpath[PATH_MAX];
568 char tmp[PATH_MAX] = "";
570 git_object *obj = NULL;
573 snprintf(fpath, sizeof(fpath), "file/%s.html", entry->path);
574 snprintf(ref, sizeof(ref), "HEAD:%s", entry->path);
576 if (git_revparse_single(&obj, repo, ref))
579 if (mkdirp(dirname(fpath)))
585 strlcat(tmp, "../", sizeof(tmp));
590 fp = efopen(fpath, "w");
592 fprintf(fp, "<p>%s (%" PRIu32 "b)</p><hr/>", entry->path, entry->file_size);
593 if (git_blob_is_binary((git_blob *)obj)) {
594 fprintf(fp, "<p>Binary file</p>\n");
596 writeblobhtml(fp, (git_blob *)obj);
600 git_object_free(obj);
612 const git_index_entry *entry;
616 fputs("<table id=\"files\"><thead>\n"
617 "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
618 "</thead><tbody>\n", fp);
620 git_repository_index(&index, repo);
621 count = git_index_entrycount(index);
623 for (i = 0; i < count; i++) {
624 entry = git_index_get_byindex(index, i);
626 fputs("<tr><td>", fp);
627 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
628 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
629 xmlencode(fp, entry->path, strlen(entry->path));
630 fputs(".html\">", fp);
631 xmlencode(fp, entry->path, strlen(entry->path));
632 fputs("</a></td><td class=\"num\">", fp);
633 fprintf(fp, "%" PRIu32, entry->file_size);
634 fputs("</td></tr>\n", fp);
639 fputs("</tbody></table>", fp);
645 main(int argc, char *argv[])
647 git_object *obj = NULL;
648 const git_error *e = NULL;
650 char path[PATH_MAX], *p;
654 fprintf(stderr, "%s <repodir>\n", argv[0]);
661 if ((status = git_repository_open_ext(&repo, repodir,
662 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
664 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
668 /* use directory name as name */
669 p = xbasename(repodir);
670 snprintf(name, sizeof(name), "%s", p);
673 /* read description or .git/description */
674 snprintf(path, sizeof(path), "%s%s%s",
675 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
676 if (!(fpread = fopen(path, "r"))) {
677 snprintf(path, sizeof(path), "%s%s%s",
678 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
679 fpread = fopen(path, "r");
682 if (!fgets(description, sizeof(description), fpread))
683 description[0] = '\0';
688 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
689 git_object_free(obj);
691 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
692 git_object_free(obj);
694 fp = efopen("log.html", "w");
700 fp = efopen("files.html", "w");
707 fp = efopen("atom.xml", "w");
712 git_repository_free(repo);
713 git_libgit2_shutdown();