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\">\n<head>\n"
27 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
28 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n", fp);
29 fprintf(fp, "<title>%s%s%s</title>\n", name, description[0] ? " - " : "", description);
30 fprintf(fp, "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
31 fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
33 fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
34 fputs("</head>\n<body>\n", fp);
35 fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" /> %s <span class=\"desc\">%s</span></h1>\n",
36 relpath, name, 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("\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];
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, " <<a href=\"mailto:");
188 xmlencode(fp, sig->email, strlen(sig->email));
190 xmlencode(fp, sig->email, strlen(sig->email));
191 fputs("</a>>\nDate: ", fp);
192 printtime(fp, &sig->when);
197 for (scan = git_commit_message(commit); scan && *scan;) {
198 for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */
201 fprintf(fp, " %.*s\n", (int) (eol - scan), scan);
202 scan = *eol ? eol + 1 : NULL;
208 printshowfile(git_commit *commit)
210 const git_diff_delta *delta = NULL;
211 const git_diff_hunk *hunk = NULL;
212 const git_diff_line *line = NULL;
213 git_commit *parent = NULL;
214 git_tree *commit_tree = NULL, *parent_tree = NULL;
215 git_patch *patch = NULL;
216 git_diff *diff = NULL;
217 git_diff_stats *diffstats = NULL;
218 git_buf diffstatsbuf;
219 size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
220 char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
224 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
225 snprintf(path, sizeof(path), "commit/%s.html", buf);
226 fp = efopen(path, "w+b");
228 memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
231 printcommit(fp, commit);
233 if ((error = git_commit_parent(&parent, commit, 0)))
235 if ((error = git_commit_tree(&commit_tree, commit)))
237 if ((error = git_commit_tree(&parent_tree, parent)))
239 if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
243 if (!git_diff_get_stats(&diffstats, diff)) {
244 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
245 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
247 fprintf(fp, "Diffstat:\n");
248 fputs(diffstatsbuf.ptr, fp);
250 git_diff_stats_free(diffstats);
254 ndeltas = git_diff_num_deltas(diff);
255 for (i = 0; i < ndeltas; i++) {
256 if (git_patch_from_diff(&patch, diff, i)) {
257 git_patch_free(patch);
258 break; /* TODO: handle error */
261 delta = git_patch_get_delta(patch);
262 fprintf(fp, "diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a>\n",
263 relpath, delta->old_file.path, delta->old_file.path,
264 relpath, delta->new_file.path, delta->new_file.path);
267 switch (delta->flags) {
268 case GIT_DIFF_FLAG_BINARY:
269 /* "Binary files /dev/null and b/favicon.png differ" or so */
270 continue; /* TODO: binary data */
271 case GIT_DIFF_FLAG_NOT_BINARY: break;
272 case GIT_DIFF_FLAG_VALID_ID: break; /* TODO: check */
273 case GIT_DIFF_FLAG_EXISTS: break; /* TODO: check */
277 nhunks = git_patch_num_hunks(patch);
278 for (j = 0; j < nhunks; j++) {
279 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
280 break; /* TODO: handle error ? */
282 fprintf(fp, "%s\n", hunk->header);
285 if (git_patch_get_line_in_hunk(&line, patch, j, k))
287 if (line->old_lineno == -1)
288 fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
290 else if (line->new_lineno == -1)
291 fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
295 xmlencode(fp, line->content, line->content_len);
296 if (line->old_lineno == -1 || line->new_lineno == -1)
297 fputs("</a></span>", fp);
300 git_patch_free(patch);
309 git_buf_free(&diffstatsbuf);
316 git_revwalk *w = NULL;
318 git_commit *commit = NULL;
319 const git_signature *author;
320 git_diff_stats *stats;
321 git_tree *commit_tree = NULL, *parent_tree = NULL;
322 git_commit *parent = NULL;
323 git_diff *diff = NULL;
324 size_t i, nfiles, ndel, nadd;
326 char buf[GIT_OID_HEXSZ + 1];
329 mkdir("commit", 0755);
331 git_revwalk_new(&w, repo);
332 git_revwalk_push_head(w);
334 /* TODO: also make "expanded" log ? (with message body) */
336 fputs("<table><thead><tr><td>Summary</td><td>Author</td><td align=\"right\">Age</td>"
337 "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr></thead><tbody>", fp);
338 while (!git_revwalk_next(&id, w)) {
339 if (git_commit_lookup(&commit, repo, &id))
340 return 1; /* TODO: error */
342 if ((error = git_commit_parent(&parent, commit, 0)))
343 continue; /* TODO: handle error */
344 if ((error = git_commit_tree(&commit_tree, commit)))
345 continue; /* TODO: handle error */
346 if ((error = git_commit_tree(&parent_tree, parent)))
347 continue; /* TODO: handle error */
348 if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
349 continue; /* TODO: handle error */
350 if (git_diff_get_stats(&stats, diff))
351 continue; /* TODO: handle error */
353 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
355 ndel = git_diff_stats_deletions(stats);
356 nadd = git_diff_stats_insertions(stats);
357 nfiles = git_diff_stats_files_changed(stats);
359 /* TODO: show tag when commit has it */
361 /* TODO: collect stats per author and make stats.html page */
362 author = git_commit_author(commit);
363 summary = git_commit_summary(commit);
365 fputs("<tr><td>", fp);
367 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, buf);
368 xmlencode(fp, summary, strlen(summary));
371 fputs("</td><td>", fp);
373 xmlencode(fp, author->name, strlen(author->name));
374 fputs("</td><td align=\"right\">", fp);
375 printtime(fp, &author->when);
376 fputs("</td><td align=\"right\">", fp);
377 fprintf(fp, "%zu", nfiles);
378 fputs("</td><td align=\"right\">", fp);
379 fprintf(fp, "+%zu", nadd);
380 fputs("</td><td align=\"right\">", fp);
381 fprintf(fp, "-%zu", ndel);
382 fputs("</td></tr>", fp);
384 printshowfile(commit);
387 git_commit_free(commit);
394 fprintf(fp, "</tbody></table>");
401 printcommitatom(FILE *fp, git_commit *commit)
403 const git_signature *sig;
404 char buf[GIT_OID_HEXSZ + 1];
406 const char *scan, *eol, *summary;
408 fputs("<entry>\n", fp);
410 /* TODO: show tag when commit has it */
411 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
412 fprintf(fp, "<id>%s</id>\n", buf);
414 sig = git_commit_author(commit);
417 fputs("<updated>", fp);
418 printtimez(fp, &sig->when);
419 fputs("</updated>\n", fp);
422 if ((summary = git_commit_summary(commit))) {
423 fputs("<title type=\"text\">", fp);
424 xmlencode(fp, summary, strlen(summary));
425 fputs("</title>\n", fp);
428 fputs("<content type=\"text\">", fp);
429 fprintf(fp, "commit %s\n", buf);
430 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)))
431 fprintf(fp, "parent %s\n", buf);
433 if ((count = (int)git_commit_parentcount(commit)) > 1) {
434 fprintf(fp, "Merge:");
435 for (i = 0; i < count; ++i) {
436 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
437 fprintf(fp, " %s", buf);
443 fprintf(fp, "Author: ");
444 xmlencode(fp, sig->name, strlen(sig->name));
445 fprintf(fp, " <");
446 xmlencode(fp, sig->email, strlen(sig->email));
447 fprintf(fp, ">\nDate: ");
448 printtime(fp, &sig->when);
452 for (scan = git_commit_message(commit); scan && *scan;) {
453 for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */
456 fprintf(fp, " %.*s\n", (int) (eol - scan), scan);
457 scan = *eol ? eol + 1 : NULL;
460 fputs("</content>\n", fp);
462 fputs("<author><name>", fp);
463 xmlencode(fp, sig->name, strlen(sig->name));
464 fputs("</name>\n<email>", fp);
465 xmlencode(fp, sig->email, strlen(sig->email));
466 fputs("</email>\n</author>\n", fp);
468 fputs("</entry>\n", fp);
474 git_revwalk *w = NULL;
476 git_commit *c = NULL;
477 size_t i, m = 100; /* max */
479 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
480 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
481 xmlencode(fp, name, strlen(name));
482 fputs(", branch master</title>\n<subtitle>", fp);
484 xmlencode(fp, description, strlen(description));
485 fputs("</subtitle>\n", fp);
487 git_revwalk_new(&w, repo);
488 git_revwalk_push_head(w);
490 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
491 if (git_commit_lookup(&c, repo, &id))
492 return 1; /* TODO: error */
493 printcommitatom(fp, c);
498 fputs("</feed>", fp);
507 const git_index_entry *entry;
510 git_repository_index(&index, repo);
512 count = git_index_entrycount(index);
513 fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
515 for (i = 0; i < count; i++) {
516 entry = git_index_get_byindex(index, i);
517 fputs("<tr><td>", fp);
518 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
519 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
520 xmlencode(fp, entry->path, strlen(entry->path));
522 xmlencode(fp, entry->path, strlen(entry->path));
523 fputs("</a></td><td align=\"right\">", fp);
524 fprintf(fp, "%" PRIu64, entry->file_size);
525 fputs("</td></tr>\n", fp);
527 fputs("</tbody></table>", fp);
533 writeblobhtml(FILE *fp, const git_blob *blob)
535 xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
539 main(int argc, char *argv[])
541 git_object *obj = NULL;
542 const git_error *e = NULL;
544 char path[PATH_MAX], *p;
548 fprintf(stderr, "%s <repodir>\n", argv[0]);
555 if ((status = git_repository_open(&repo, repodir)) < 0) {
557 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
561 /* use directory name as name */
562 p = xbasename(repodir);
563 snprintf(name, sizeof(name), "%s", p);
566 /* read description or .git/description */
567 snprintf(path, sizeof(path), "%s%s%s",
568 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
569 if (!(fpread = fopen(path, "r+b"))) {
570 snprintf(path, sizeof(path), "%s%s%s",
571 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
572 fpread = fopen(path, "r+b");
575 if (!fgets(description, sizeof(description), fpread))
576 description[0] = '\0';
581 if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
582 fp = efopen("license.html", "w+b");
584 writeblobhtml(fp, (git_blob *)obj);
595 if (!git_revparse_single(&obj, repo, "HEAD:README")) {
596 fp = efopen("readme.html", "w+b");
598 writeblobhtml(fp, (git_blob *)obj);
607 fp = efopen("log.html", "w+b");
613 fp = efopen("files.html", "w+b");
620 fp = efopen("atom.xml", "w+b");
625 git_repository_free(repo);
626 git_libgit2_shutdown();