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<center>\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</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;
214 size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
215 char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
219 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
220 snprintf(path, sizeof(path), "commit/%s.html", buf);
221 fp = efopen(path, "w+b");
223 memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
226 printcommit(fp, commit);
228 if ((error = git_commit_tree(&commit_tree, commit)))
230 if (!(error = git_commit_parent(&parent, commit, 0))) {
231 if ((error = git_commit_tree(&parent_tree, parent)))
232 goto err; /* TODO: handle error */
237 if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
241 if (!git_diff_get_stats(&diffstats, diff)) {
242 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
243 GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
244 fprintf(fp, "<b>Diffstat:</b>\n");
245 fputs(diffstatsbuf.ptr, fp);
247 git_diff_stats_free(diffstats);
251 ndeltas = git_diff_num_deltas(diff);
252 for (i = 0; i < ndeltas; i++) {
253 if (git_patch_from_diff(&patch, diff, i)) {
254 git_patch_free(patch);
255 break; /* TODO: handle error */
258 delta = git_patch_get_delta(patch);
259 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a></b>\n",
260 relpath, delta->old_file.path, delta->old_file.path,
261 relpath, delta->new_file.path, delta->new_file.path);
263 /* TODO: "new file mode <mode>". */
264 /* TODO: add indexfrom...indexto + flags */
267 fputs("<b>--- ", fp);
268 if (delta->status & GIT_DELTA_ADDED)
269 fputs("/dev/null", fp);
271 fprintf(fp, "a/<a href=\"%sfile/%s\">%s</a>",
272 relpath, delta->old_file.path, delta->old_file.path);
275 if (delta->status & GIT_DELTA_DELETED)
276 fputs("/dev/null", fp);
278 fprintf(fp, "b/<a href=\"%sfile/%s\">%s</a>",
279 relpath, delta->new_file.path, delta->new_file.path);
283 /* check binary data */
284 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
285 fputs("Binary files differ\n", fp);
286 git_patch_free(patch);
290 nhunks = git_patch_num_hunks(patch);
291 for (j = 0; j < nhunks; j++) {
292 if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
293 break; /* TODO: handle error ? */
295 fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
298 if (git_patch_get_line_in_hunk(&line, patch, j, k))
300 if (line->old_lineno == -1)
301 fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
303 else if (line->new_lineno == -1)
304 fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
308 xmlencode(fp, line->content, line->content_len);
309 if (line->old_lineno == -1 || line->new_lineno == -1)
310 fputs("</a></span>", fp);
313 git_patch_free(patch);
322 git_buf_free(&diffstatsbuf);
329 git_revwalk *w = NULL;
331 git_commit *commit = NULL;
332 const git_signature *author;
333 git_diff_stats *stats;
334 git_tree *commit_tree = NULL, *parent_tree = NULL;
335 git_commit *parent = NULL;
336 git_diff *diff = NULL;
337 size_t i, nfiles, ndel, nadd;
339 char buf[GIT_OID_HEXSZ + 1];
342 mkdir("commit", 0755);
344 git_revwalk_new(&w, repo);
345 git_revwalk_push_head(w);
347 /* TODO: also make "expanded" log ? (with message body) */
348 i = 0; /* DEBUG: to limit commits */
349 fputs("<table><thead>\n<tr><td>Commit message</td><td>Author</td><td align=\"right\">Age</td>"
350 "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr>\n</thead><tbody>\n", fp);
351 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)))
366 goto errdiff; /* TODO: handle error */
372 if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
373 continue; /* TODO: handle error */
374 if (git_diff_get_stats(&stats, diff))
375 continue; /* TODO: handle error */
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);
413 git_commit_free(commit);
415 fprintf(fp, "</tbody></table>");
424 printcommitatom(FILE *fp, git_commit *commit)
426 const git_signature *sig;
427 char buf[GIT_OID_HEXSZ + 1];
429 const char *msg, *summary;
431 fputs("<entry>\n", fp);
433 /* TODO: show tag when commit has it */
434 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
435 fprintf(fp, "<id>%s</id>\n", buf);
437 sig = git_commit_author(commit);
440 fputs("<updated>", fp);
441 printtimez(fp, &sig->when);
442 fputs("</updated>\n", fp);
445 if ((summary = git_commit_summary(commit))) {
446 fputs("<title type=\"text\">", fp);
447 xmlencode(fp, summary, strlen(summary));
448 fputs("</title>\n", fp);
451 fputs("<content type=\"text\">", fp);
452 fprintf(fp, "commit %s\n", buf);
453 if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
454 fprintf(fp, "parent %s\n", buf);
456 if ((count = (int)git_commit_parentcount(commit)) > 1) {
457 fprintf(fp, "Merge:");
458 for (i = 0; i < count; i++) {
459 git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
460 fprintf(fp, " %s", buf);
466 fprintf(fp, "Author: ");
467 xmlencode(fp, sig->name, strlen(sig->name));
468 fprintf(fp, " <");
469 xmlencode(fp, sig->email, strlen(sig->email));
470 fprintf(fp, ">\nDate: ");
471 printtime(fp, &sig->when);
475 if ((msg = git_commit_message(commit)))
476 xmlencode(fp, msg, strlen(msg));
477 fputs("\n</content>\n", fp);
479 fputs("<author><name>", fp);
480 xmlencode(fp, sig->name, strlen(sig->name));
481 fputs("</name>\n<email>", fp);
482 xmlencode(fp, sig->email, strlen(sig->email));
483 fputs("</email>\n</author>\n", fp);
485 fputs("</entry>\n", fp);
491 git_revwalk *w = NULL;
493 git_commit *c = NULL;
494 size_t i, m = 100; /* max */
496 fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
497 fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
498 xmlencode(fp, name, strlen(name));
499 fputs(", branch master</title>\n<subtitle>", fp);
501 xmlencode(fp, description, strlen(description));
502 fputs("</subtitle>\n", fp);
504 git_revwalk_new(&w, repo);
505 git_revwalk_push_head(w);
507 for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
508 if (git_commit_lookup(&c, repo, &id))
509 return 1; /* TODO: error */
510 printcommitatom(fp, c);
515 fputs("</feed>", fp);
524 const git_index_entry *entry;
527 git_repository_index(&index, repo);
529 count = git_index_entrycount(index);
530 fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
532 for (i = 0; i < count; i++) {
533 entry = git_index_get_byindex(index, i);
534 fputs("<tr><td>", fp);
535 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
536 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
537 xmlencode(fp, entry->path, strlen(entry->path));
539 xmlencode(fp, entry->path, strlen(entry->path));
540 fputs("</a></td><td align=\"right\">", fp);
541 fprintf(fp, "%" PRIu64, entry->file_size);
542 fputs("</td></tr>\n", fp);
544 fputs("</tbody></table>", fp);
550 writeblobhtml(FILE *fp, const git_blob *blob)
552 xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
556 main(int argc, char *argv[])
558 git_object *obj = NULL;
559 const git_error *e = NULL;
561 char path[PATH_MAX], *p;
565 fprintf(stderr, "%s <repodir>\n", argv[0]);
572 if ((status = git_repository_open(&repo, repodir)) < 0) {
574 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
578 /* use directory name as name */
579 p = xbasename(repodir);
580 snprintf(name, sizeof(name), "%s", p);
583 /* read description or .git/description */
584 snprintf(path, sizeof(path), "%s%s%s",
585 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
586 if (!(fpread = fopen(path, "r+b"))) {
587 snprintf(path, sizeof(path), "%s%s%s",
588 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
589 fpread = fopen(path, "r+b");
592 if (!fgets(description, sizeof(description), fpread))
593 description[0] = '\0';
598 haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
600 hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
603 if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
604 fp = efopen("license.html", "w+b");
606 writeblobhtml(fp, (git_blob *)obj);
615 if (!git_revparse_single(&obj, repo, "HEAD:README")) {
616 fp = efopen("readme.html", "w+b");
618 writeblobhtml(fp, (git_blob *)obj);
625 fp = efopen("log.html", "w+b");
631 fp = efopen("files.html", "w+b");
638 fp = efopen("atom.xml", "w+b");
643 git_repository_free(repo);
644 git_libgit2_shutdown();