13 static git_repository *repo;
15 static const char *relpath = "";
16 static const char *repodir;
18 static char name[255];
19 static char description[255];
20 static int hasreadme, haslicense;
25 fputs("<!DOCTYPE HTML>"
26 "<html dir=\"ltr\" lang=\"en\"><head>"
27 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
28 "<meta http-equiv=\"Content-Language\" content=\"en\" />", fp);
29 fprintf(fp, "<title>%s%s%s</title>", name, description[0] ? " - " : "", description);
30 fprintf(fp, "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />", relpath);
31 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />",
33 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"
34 "</head><body><center>");
35 fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" /> %s</h1>", relpath, name);
36 fprintf(fp, "<span class=\"desc\">%s</span><br/>", description);
37 fprintf(fp, "<a href=\"%slog.html\">Log</a> |", relpath);
38 fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
39 fprintf(fp, "<a href=\"%sstats.html\">Stats</a>", relpath);
41 fprintf(fp, " | <a href=\"%sreadme.html\">README</a>", relpath);
43 fprintf(fp, " | <a href=\"%slicense.html\">LICENSE</a>", relpath);
44 fputs("</center><hr/><pre>", fp);
52 return !fputs("</pre></body></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\n", out, sign, hours, minutes);
159 printcommit(FILE *fp, git_commit *commit)
161 const git_signature *sig;
162 char buf[GIT_OID_HEXSZ + 1];
164 const char *scan, *eol;
166 /* TODO: show tag when commit has it */
167 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
168 fprintf(fp, "commit <a href=\"%scommit/%s.html\">%s</a>\n",
171 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)))
172 fprintf(fp, "parent <a href=\"%scommit/%s.html\">%s</a>\n",
175 if ((count = (int)git_commit_parentcount(commit)) > 1) {
176 fprintf(fp, "Merge:");
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, "Author: ");
186 xmlencode(fp, sig->name, strlen(sig->name));
187 fprintf(fp, " <");
188 xmlencode(fp, sig->email, strlen(sig->email));
189 fprintf(fp, ">\nDate: ");
190 printtime(fp, &sig->when);
194 for (scan = git_commit_message(commit); scan && *scan;) {
195 for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */
198 fprintf(fp, " %.*s\n", (int) (eol - scan), scan);
199 scan = *eol ? eol + 1 : NULL;
205 printshowfile(git_commit *commit)
207 const git_diff_delta *delta = NULL;
208 const git_diff_hunk *hunk = NULL;
209 const git_diff_line *line = NULL;
210 git_commit *parent = NULL;
211 git_tree *commit_tree = NULL, *parent_tree = NULL;
212 git_patch *patch = NULL;
213 git_diff *diff = NULL;
214 git_diff_stats *diffstats = NULL;
215 git_buf diffstatsbuf;
216 size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
217 char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
221 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
222 snprintf(path, sizeof(path), "commit/%s.html", buf);
223 fp = efopen(path, "w+b");
225 memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
228 printcommit(fp, commit);
230 if ((error = git_commit_parent(&parent, commit, 0)))
232 if ((error = git_commit_tree(&commit_tree, commit)))
234 if ((error = git_commit_tree(&parent_tree, parent)))
236 if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
240 if (!git_diff_get_stats(&diffstats, diff)) {
241 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
242 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT | GIT_DIFF_STATS_NUMBER |
243 GIT_DIFF_STATS_INCLUDE_SUMMARY, 80)) {
245 fprintf(fp, "Diffstat:\n");
246 fputs(diffstatsbuf.ptr, fp);
248 git_diff_stats_free(diffstats);
252 ndeltas = git_diff_num_deltas(diff);
253 for (i = 0; i < ndeltas; i++) {
254 if (git_patch_from_diff(&patch, diff, i)) {
255 git_patch_free(patch);
256 break; /* TODO: handle error */
259 delta = git_patch_get_delta(patch);
260 fprintf(fp, "diff --git a/<a href=\"%s%s\">%s</a> b/<a href=\"%s%s\">%s</a>\n",
261 relpath, delta->old_file.path, delta->old_file.path,
262 relpath, delta->new_file.path, delta->new_file.path);
265 switch (delta->flags) {
266 case GIT_DIFF_FLAG_BINARY:
267 /* "Binary files /dev/null and b/favicon.png differ" or so */
268 continue; /* TODO: binary data */
269 case GIT_DIFF_FLAG_NOT_BINARY: break;
270 case GIT_DIFF_FLAG_VALID_ID: break; /* TODO: check */
271 case GIT_DIFF_FLAG_EXISTS: break; /* TODO: check */
275 nhunks = git_patch_num_hunks(patch);
276 for (j = 0; j < nhunks; j++) {
277 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
278 break; /* TODO: handle error ? */
280 fprintf(fp, "%s\n", hunk->header);
283 if (git_patch_get_line_in_hunk(&line, patch, j, k))
285 if (line->old_lineno == -1)
287 else if (line->new_lineno == -1)
291 xmlencode(fp, line->content, line->content_len);
294 git_patch_free(patch);
303 git_buf_free(&diffstatsbuf);
310 git_revwalk *w = NULL;
312 git_commit *commit = NULL;
313 const git_signature *author;
314 git_diff_stats *stats;
315 git_tree *commit_tree = NULL, *parent_tree = NULL;
316 git_commit *parent = NULL;
317 git_diff *diff = NULL;
318 size_t i, nfiles, ndel, nadd;
320 char buf[GIT_OID_HEXSZ + 1];
323 mkdir("commit", 0755);
325 git_revwalk_new(&w, repo);
326 git_revwalk_push_head(w);
328 /* TODO: also make "expanded" log ? (with message body) */
330 fputs("<table><thead><tr><td>Summary</td><td>Author</td><td align=\"right\">Age</td>"
331 "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr></thead><tbody>", fp);
332 while (!git_revwalk_next(&id, w)) {
333 if (git_commit_lookup(&commit, repo, &id))
334 return 1; /* TODO: error */
336 if ((error = git_commit_parent(&parent, commit, 0)))
337 continue; /* TODO: handle error */
338 if ((error = git_commit_tree(&commit_tree, commit)))
339 continue; /* TODO: handle error */
340 if ((error = git_commit_tree(&parent_tree, parent)))
341 continue; /* TODO: handle error */
342 if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
343 continue; /* TODO: handle error */
344 if (git_diff_get_stats(&stats, diff))
345 continue; /* TODO: handle error */
347 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
349 ndel = git_diff_stats_deletions(stats);
350 nadd = git_diff_stats_insertions(stats);
351 nfiles = git_diff_stats_files_changed(stats);
353 /* TODO: show tag when commit has it */
355 /* TODO: collect stats per author and make stats.html page */
356 author = git_commit_author(commit);
357 summary = git_commit_summary(commit);
359 fputs("<tr><td>", fp);
361 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, buf);
362 xmlencode(fp, summary, strlen(summary));
365 fputs("</td><td>", fp);
367 xmlencode(fp, author->name, strlen(author->name));
368 fputs("</td><td align=\"right\">", fp);
369 printtime(fp, &author->when);
370 fputs("</td><td align=\"right\">", fp);
371 fprintf(fp, "%zu", nfiles);
372 fputs("</td><td align=\"right\">", fp);
373 fprintf(fp, "+%zu", nadd);
374 fputs("</td><td align=\"right\">", fp);
375 fprintf(fp, "-%zu", ndel);
376 fputs("</td></tr>", fp);
378 printshowfile(commit);
381 git_commit_free(commit);
388 fprintf(fp, "</tbody></table>");
395 printcommitatom(FILE *fp, git_commit *commit)
397 const git_signature *sig;
398 char buf[GIT_OID_HEXSZ + 1];
400 const char *scan, *eol, *summary;
402 fputs("<entry>", fp);
404 /* TODO: show tag when commit has it */
405 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
406 fprintf(fp, "<id>%s</id>", buf);
408 sig = git_commit_author(commit);
411 fputs("<updated>", fp);
412 printtimez(fp, &sig->when);
413 fputs("</updated>", fp);
416 if ((summary = git_commit_summary(commit))) {
417 fputs("<title>", fp);
418 xmlencode(fp, summary, strlen(summary));
419 fputs("</title>", fp);
422 fputs("<content type=\"text\">", fp);
423 fprintf(fp, "commit %s\n", buf);
424 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)))
425 fprintf(fp, "parent %s\n", buf);
427 if ((count = (int)git_commit_parentcount(commit)) > 1) {
428 fprintf(fp, "Merge:");
429 for (i = 0; i < count; ++i) {
430 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
431 fprintf(fp, " %s", buf);
437 fprintf(fp, "Author: ");
438 xmlencode(fp, sig->name, strlen(sig->name));
439 fprintf(fp, " <");
440 xmlencode(fp, sig->email, strlen(sig->email));
441 fprintf(fp, ">\nDate: ");
442 printtime(fp, &sig->when);
446 for (scan = git_commit_message(commit); scan && *scan;) {
447 for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */
450 fprintf(fp, " %.*s\n", (int) (eol - scan), scan);
451 scan = *eol ? eol + 1 : NULL;
454 fputs("</content>", fp);
456 fputs("<author><name>", fp);
457 xmlencode(fp, sig->name, strlen(sig->name));
458 fputs("</name><email>", fp);
459 xmlencode(fp, sig->email, strlen(sig->email));
460 fputs("</email></author>", fp);
462 fputs("</entry>", fp);
468 git_revwalk *w = NULL;
470 git_commit *c = NULL;
471 size_t i, m = 100; /* max */
473 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\"><title>", fp);
474 xmlencode(fp, name, strlen(name));
475 fputs(", branch master</title><subtitle>", fp);
477 xmlencode(fp, description, strlen(description));
478 fputs("</subtitle>", fp);
480 git_revwalk_new(&w, repo);
481 git_revwalk_push_head(w);
483 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
484 if (git_commit_lookup(&c, repo, &id))
485 return 1; /* TODO: error */
486 printcommitatom(fp, c);
491 fputs("</feed>", fp);
500 const git_index_entry *entry;
503 git_repository_index(&index, repo);
505 count = git_index_entrycount(index);
506 fputs("<table><thead><tr><td>Mode</td><td>Name</td><td>Size</td></tr></thead><tbody>", fp);
508 for (i = 0; i < count; i++) {
509 entry = git_index_get_byindex(index, i);
510 fputs("<tr><td>", fp);
511 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
512 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
513 xmlencode(fp, entry->path, strlen(entry->path));
515 xmlencode(fp, entry->path, strlen(entry->path));
516 fputs("</a></td><td align=\"right\">", fp);
517 fprintf(fp, "%" PRIu64, entry->file_size);
518 fputs("</td></tr>", fp);
520 fputs("</tbody></table>", fp);
526 writeblobhtml(FILE *fp, const git_blob *blob)
528 xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
532 main(int argc, char *argv[])
534 git_object *obj = NULL;
535 const git_error *e = NULL;
537 char path[PATH_MAX], *p;
541 fprintf(stderr, "%s <repodir>\n", argv[0]);
548 if ((status = git_repository_open(&repo, repodir)) < 0) {
550 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
554 /* use directory name as name */
555 p = xbasename(repodir);
556 snprintf(name, sizeof(name), "%s", p);
559 /* read description or .git/description */
560 snprintf(path, sizeof(path), "%s%s%s",
561 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
562 if (!(fpread = fopen(path, "r+b"))) {
563 snprintf(path, sizeof(path), "%s%s%s",
564 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
565 fpread = fopen(path, "r+b");
568 if (!fgets(description, sizeof(description), fpread))
569 description[0] = '\0';
574 if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
575 fp = efopen("license.html", "w+b");
577 writeblobhtml(fp, (git_blob *)obj);
588 if (!git_revparse_single(&obj, repo, "HEAD:README")) {
589 fp = efopen("readme.html", "w+b");
591 writeblobhtml(fp, (git_blob *)obj);
600 fp = efopen("log.html", "w+b");
606 fp = efopen("atom.xml", "w+b");
610 fp = efopen("files.html", "w+b");
617 git_repository_free(repo);
618 git_libgit2_shutdown();