14 static git_repository *repo;
16 static const char *relpath = "";
17 static const char *repodir;
19 static char name[255];
20 static char description[255];
21 static int hasreadme, haslicense;
26 fputs("<!DOCTYPE HTML>"
27 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
28 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
29 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n", fp);
30 fprintf(fp, "<title>%s%s%s</title>\n", name, description[0] ? " - " : "", description);
31 fprintf(fp, "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
32 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
34 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
35 fputs("</head>\n<body>\n<center>\n", fp);
36 fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /> %s <span class=\"desc\">%s</span></h1>\n",
37 relpath, name, description);
38 fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
39 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
41 fprintf(fp, " | <a href=\"%sreadme.html\">README</a>", relpath);
43 fprintf(fp, " | <a href=\"%slicense.html\">LICENSE</a>", relpath);
44 fputs("\n</center>\n<hr/>\n<pre>", fp);
52 return !fputs("</pre>\n</body>\n</html>", fp);
56 efopen(const char *name, const char *flags)
60 if (!(fp = fopen(name, flags)))
66 /* Escape characters below as HTML 2.0 / XML 1.0. */
68 xmlencode(FILE *fp, const char *s, size_t len)
72 for (i = 0; *s && i < len; s++, i++) {
74 case '<': fputs("<", fp); break;
75 case '>': fputs(">", fp); break;
76 case '\'': fputs("'", fp); break;
77 case '&': fputs("&", fp); break;
78 case '"': fputs(""", fp); break;
79 default: fputc(*s, fp);
84 /* Some implementations of basename(3) return a pointer to a static
85 * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
86 * This is a wrapper function that is compatible with both versions.
87 * The program will error out if basename(3) failed, this can only happen
88 * with the OpenBSD version. */
90 xbasename(const char *path)
94 if (!(p = strdup(path)))
96 if (!(b = basename(p)))
106 printtimez(FILE *fp, const git_time *intime)
110 int offset, hours, minutes;
113 offset = intime->offset;
122 minutes = offset % 60;
124 t = (time_t) intime->time + (intime->offset * 60);
127 strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
132 printtime(FILE *fp, const git_time *intime)
136 int offset, hours, minutes;
139 offset = intime->offset;
148 minutes = offset % 60;
150 t = (time_t) intime->time + (intime->offset * 60);
153 strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
155 fprintf(fp, "%s %c%02d%02d", out, sign, hours, minutes);
159 printcommit(FILE *fp, git_commit *commit)
161 const git_signature *sig;
162 char buf[GIT_OID_HEXSZ + 1];
166 /* TODO: show tag when commit has it */
167 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
168 fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
171 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
172 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
175 if ((count = (int)git_commit_parentcount(commit)) > 1) {
176 fprintf(fp, "<b>Merge:</b>");
177 for (i = 0; i < count; i++) {
178 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
179 fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
184 if ((sig = git_commit_author(commit)) != NULL) {
185 fprintf(fp, "<b>Author:</b> ");
186 xmlencode(fp, sig->name, strlen(sig->name));
187 fprintf(fp, " <<a href=\"mailto:");
188 xmlencode(fp, sig->email, strlen(sig->email));
190 xmlencode(fp, sig->email, strlen(sig->email));
191 fputs("</a>>\n<b>Date:</b> ", fp);
192 printtime(fp, &sig->when);
197 if ((msg = git_commit_message(commit)))
198 xmlencode(fp, msg, strlen(msg));
203 printshowfile(git_commit *commit)
205 const git_diff_delta *delta = NULL;
206 const git_diff_hunk *hunk = NULL;
207 const git_diff_line *line = NULL;
208 git_commit *parent = NULL;
209 git_tree *commit_tree = NULL, *parent_tree = NULL;
210 git_patch *patch = NULL;
211 git_diff *diff = NULL;
212 git_diff_stats *diffstats = NULL;
213 git_buf diffstatsbuf;
215 size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
216 char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
219 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
222 snprintf(path, sizeof(path), "commit/%s.html", buf);
223 /* check if file exists if so skip it */
224 if (!access(path, F_OK))
227 memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
229 fp = efopen(path, "w+b");
231 printcommit(fp, commit);
233 if ((error = git_commit_tree(&commit_tree, commit)))
235 if (!(error = git_commit_parent(&parent, commit, 0))) {
236 if ((error = git_commit_tree(&parent_tree, parent)))
237 goto err; /* TODO: handle error */
242 if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
246 if (!git_diff_get_stats(&diffstats, diff)) {
247 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
248 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
249 fprintf(fp, "<b>Diffstat:</b>\n");
250 fputs(diffstatsbuf.ptr, fp);
252 git_diff_stats_free(diffstats);
256 ndeltas = git_diff_num_deltas(diff);
257 for (i = 0; i < ndeltas; i++) {
258 if (git_patch_from_diff(&patch, diff, i)) {
259 git_patch_free(patch);
260 break; /* TODO: handle error */
263 delta = git_patch_get_delta(patch);
264 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a></b>\n",
265 relpath, delta->old_file.path, delta->old_file.path,
266 relpath, delta->new_file.path, delta->new_file.path);
268 /* TODO: "new file mode <mode>". */
269 /* TODO: add indexfrom...indexto + flags */
272 fputs("<b>--- ", fp);
273 if (delta->status & GIT_DELTA_ADDED)
274 fputs("/dev/null", fp);
276 fprintf(fp, "a/<a href=\"%sfile/%s\">%s</a>",
277 relpath, delta->old_file.path, delta->old_file.path);
280 if (delta->status & GIT_DELTA_DELETED)
281 fputs("/dev/null", fp);
283 fprintf(fp, "b/<a href=\"%sfile/%s\">%s</a>",
284 relpath, delta->new_file.path, delta->new_file.path);
288 /* check binary data */
289 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
290 fputs("Binary files differ\n", fp);
291 git_patch_free(patch);
295 nhunks = git_patch_num_hunks(patch);
296 for (j = 0; j < nhunks; j++) {
297 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
298 break; /* TODO: handle error ? */
300 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
303 if (git_patch_get_line_in_hunk(&line, patch, j, k))
305 if (line->old_lineno == -1)
306 fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
308 else if (line->new_lineno == -1)
309 fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
313 xmlencode(fp, line->content, line->content_len);
314 if (line->old_lineno == -1 || line->new_lineno == -1)
315 fputs("</a></span>", fp);
318 git_patch_free(patch);
327 git_buf_free(&diffstatsbuf);
334 git_revwalk *w = NULL;
336 git_commit *commit = NULL;
337 const git_signature *author;
338 git_diff_stats *stats = NULL;
339 git_tree *commit_tree = NULL, *parent_tree = NULL;
340 git_commit *parent = NULL;
341 git_diff *diff = NULL;
342 size_t nfiles, ndel, nadd;
344 char buf[GIT_OID_HEXSZ + 1];
347 mkdir("commit", 0755);
349 git_revwalk_new(&w, repo);
350 git_revwalk_push_head(w);
352 /* TODO: also make "expanded" log ? (with message body) */
353 fputs("<table><thead>\n<tr><td>Commit message</td><td>Author</td><td align=\"right\">Age</td>"
354 "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr>\n</thead><tbody>\n", fp);
355 while (!git_revwalk_next(&id, w)) {
358 if (git_commit_lookup(&commit, repo, &id)) {
362 if ((error = git_commit_tree(&commit_tree, commit)))
363 goto errdiff; /* TODO: handle error */
364 if (!(error = git_commit_parent(&parent, commit, 0))) {
365 if ((error = git_commit_tree(&parent_tree, parent)))
372 if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
374 if (git_diff_get_stats(&stats, diff))
377 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
379 ndel = git_diff_stats_deletions(stats);
380 nadd = git_diff_stats_insertions(stats);
381 nfiles = git_diff_stats_files_changed(stats);
383 /* TODO: show tag when commit has it */
385 /* TODO: collect stats per author and make stats.html page */
386 author = git_commit_author(commit);
387 summary = git_commit_summary(commit);
389 fputs("<tr><td>", fp);
391 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, buf);
392 xmlencode(fp, summary, strlen(summary));
395 fputs("</td><td>", fp);
397 xmlencode(fp, author->name, strlen(author->name));
398 fputs("</td><td align=\"right\">", fp);
399 printtime(fp, &author->when);
400 fputs("</td><td align=\"right\">", fp);
401 fprintf(fp, "%zu", nfiles);
402 fputs("</td><td align=\"right\">", fp);
403 fprintf(fp, "+%zu", nadd);
404 fputs("</td><td align=\"right\">", fp);
405 fprintf(fp, "-%zu", ndel);
406 fputs("</td></tr>\n", fp);
409 printshowfile(commit);
412 /* TODO: print error ? */
413 git_diff_stats_free(stats);
415 git_commit_free(commit);
417 fprintf(fp, "</tbody></table>");
426 printcommitatom(FILE *fp, git_commit *commit)
428 const git_signature *sig;
429 char buf[GIT_OID_HEXSZ + 1];
431 const char *msg, *summary;
433 fputs("<entry>\n", fp);
435 /* TODO: show tag when commit has it */
436 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
437 fprintf(fp, "<id>%s</id>\n", buf);
439 sig = git_commit_author(commit);
442 fputs("<updated>", fp);
443 printtimez(fp, &sig->when);
444 fputs("</updated>\n", fp);
447 if ((summary = git_commit_summary(commit))) {
448 fputs("<title type=\"text\">", fp);
449 xmlencode(fp, summary, strlen(summary));
450 fputs("</title>\n", fp);
453 fputs("<content type=\"text\">", fp);
454 fprintf(fp, "commit %s\n", buf);
455 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
456 fprintf(fp, "parent %s\n", buf);
458 if ((count = (int)git_commit_parentcount(commit)) > 1) {
459 fprintf(fp, "Merge:");
460 for (i = 0; i < count; i++) {
461 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
462 fprintf(fp, " %s", buf);
468 fprintf(fp, "Author: ");
469 xmlencode(fp, sig->name, strlen(sig->name));
470 fprintf(fp, " <");
471 xmlencode(fp, sig->email, strlen(sig->email));
472 fprintf(fp, ">\nDate: ");
473 printtime(fp, &sig->when);
477 if ((msg = git_commit_message(commit)))
478 xmlencode(fp, msg, strlen(msg));
479 fputs("\n</content>\n", fp);
481 fputs("<author><name>", fp);
482 xmlencode(fp, sig->name, strlen(sig->name));
483 fputs("</name>\n<email>", fp);
484 xmlencode(fp, sig->email, strlen(sig->email));
485 fputs("</email>\n</author>\n", fp);
487 fputs("</entry>\n", fp);
493 git_revwalk *w = NULL;
495 git_commit *c = NULL;
496 size_t i, m = 100; /* max */
498 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
499 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
500 xmlencode(fp, name, strlen(name));
501 fputs(", branch master</title>\n<subtitle>", fp);
503 xmlencode(fp, description, strlen(description));
504 fputs("</subtitle>\n", fp);
506 git_revwalk_new(&w, repo);
507 git_revwalk_push_head(w);
509 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
510 if (git_commit_lookup(&c, repo, &id))
511 return 1; /* TODO: error */
512 printcommitatom(fp, c);
517 fputs("</feed>", fp);
526 const git_index_entry *entry;
529 git_repository_index(&index, repo);
531 count = git_index_entrycount(index);
532 fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
534 for (i = 0; i < count; i++) {
535 entry = git_index_get_byindex(index, i);
536 fputs("<tr><td>", fp);
537 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
538 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
539 xmlencode(fp, entry->path, strlen(entry->path));
541 xmlencode(fp, entry->path, strlen(entry->path));
542 fputs("</a></td><td align=\"right\">", fp);
543 fprintf(fp, "%" PRIu64, entry->file_size);
544 fputs("</td></tr>\n", fp);
546 fputs("</tbody></table>", fp);
552 writeblobhtml(FILE *fp, const git_blob *blob)
554 xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
558 main(int argc, char *argv[])
560 git_object *obj = NULL;
561 const git_error *e = NULL;
563 char path[PATH_MAX], *p;
567 fprintf(stderr, "%s <repodir>\n", argv[0]);
574 if ((status = git_repository_open(&repo, repodir)) < 0) {
576 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
580 /* use directory name as name */
581 p = xbasename(repodir);
582 snprintf(name, sizeof(name), "%s", p);
585 /* read description or .git/description */
586 snprintf(path, sizeof(path), "%s%s%s",
587 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
588 if (!(fpread = fopen(path, "r+b"))) {
589 snprintf(path, sizeof(path), "%s%s%s",
590 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
591 fpread = fopen(path, "r+b");
594 if (!fgets(description, sizeof(description), fpread))
595 description[0] = '\0';
600 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
602 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
605 if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
606 fp = efopen("license.html", "w+b");
608 writeblobhtml(fp, (git_blob *)obj);
617 if (!git_revparse_single(&obj, repo, "HEAD:README")) {
618 fp = efopen("readme.html", "w+b");
620 writeblobhtml(fp, (git_blob *)obj);
627 fp = efopen("log.html", "w+b");
633 fp = efopen("files.html", "w+b");
640 fp = efopen("atom.xml", "w+b");
645 git_repository_free(repo);
646 git_libgit2_shutdown();