]> git.armaanb.net Git - stagit.git/blob - stagit.c
refs: dont make a table when there are no tags, add spacing between
[stagit.git] / stagit.c
1 #include <sys/stat.h>
2
3 #include <err.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include <libgen.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <git2.h>
14
15 #include "compat.h"
16 #include "config.h"
17
18 struct commitinfo {
19         const git_oid *id;
20
21         char oid[GIT_OID_HEXSZ + 1];
22         char parentoid[GIT_OID_HEXSZ + 1];
23
24         const git_signature *author;
25         const char *summary;
26         const char *msg;
27
28         git_diff_stats *stats;
29         git_diff       *diff;
30         git_commit     *commit;
31         git_commit     *parent;
32         git_tree       *commit_tree;
33         git_tree       *parent_tree;
34
35         size_t addcount;
36         size_t delcount;
37         size_t filecount;
38 };
39
40 static git_repository *repo;
41
42 static const char *relpath = "";
43 static const char *repodir;
44
45 static char name[255];
46 static char description[255];
47 static char cloneurl[1024];
48 static int hasreadme, haslicense;
49
50 void
51 commitinfo_free(struct commitinfo *ci)
52 {
53         if (!ci)
54                 return;
55
56         git_diff_stats_free(ci->stats);
57         git_diff_free(ci->diff);
58         git_tree_free(ci->commit_tree);
59         git_tree_free(ci->parent_tree);
60         git_commit_free(ci->commit);
61 }
62
63 struct commitinfo *
64 commitinfo_getbyoid(const git_oid *id)
65 {
66         struct commitinfo *ci;
67         git_diff_options opts;
68         int error;
69
70         if (!(ci = calloc(1, sizeof(struct commitinfo))))
71                 err(1, "calloc");
72
73         ci->id = id;
74         if (git_commit_lookup(&(ci->commit), repo, id))
75                 goto err;
76
77         git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
78         git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
79
80         ci->author = git_commit_author(ci->commit);
81         ci->summary = git_commit_summary(ci->commit);
82         ci->msg = git_commit_message(ci->commit);
83
84         if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
85                 goto err;
86         if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
87                 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
88                         goto err;
89         } else {
90                 ci->parent = NULL;
91                 ci->parent_tree = NULL;
92         }
93
94         git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
95         opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
96         if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
97                 goto err;
98         if (git_diff_get_stats(&(ci->stats), ci->diff))
99                 goto err;
100
101         ci->addcount = git_diff_stats_insertions(ci->stats);
102         ci->delcount = git_diff_stats_deletions(ci->stats);
103         ci->filecount = git_diff_stats_files_changed(ci->stats);
104
105         return ci;
106
107 err:
108         commitinfo_free(ci);
109         free(ci);
110
111         return NULL;
112 }
113
114 FILE *
115 efopen(const char *name, const char *flags)
116 {
117         FILE *fp;
118
119         if (!(fp = fopen(name, flags)))
120                 err(1, "fopen");
121
122         return fp;
123 }
124
125 /* Escape characters below as HTML 2.0 / XML 1.0. */
126 void
127 xmlencode(FILE *fp, const char *s, size_t len)
128 {
129         size_t i;
130
131         for (i = 0; *s && i < len; s++, i++) {
132                 switch(*s) {
133                 case '<':  fputs("&lt;",   fp); break;
134                 case '>':  fputs("&gt;",   fp); break;
135                 case '\'': fputs("&apos;", fp); break;
136                 case '&':  fputs("&amp;",  fp); break;
137                 case '"':  fputs("&quot;", fp); break;
138                 default:   fputc(*s, fp);
139                 }
140         }
141 }
142
143 /* Some implementations of dirname(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 dirname(3) failed, this can only happen
147  * with the OpenBSD version. */
148 char *
149 xdirname(const char *path)
150 {
151         char *p, *b;
152
153         if (!(p = strdup(path)))
154                 err(1, "strdup");
155         if (!(b = dirname(p)))
156                 err(1, "basename");
157         if (!(b = strdup(b)))
158                 err(1, "strdup");
159         free(p);
160
161         return b;
162 }
163
164 /* Some implementations of basename(3) return a pointer to a static
165  * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
166  * This is a wrapper function that is compatible with both versions.
167  * The program will error out if basename(3) failed, this can only happen
168  * with the OpenBSD version. */
169 char *
170 xbasename(const char *path)
171 {
172         char *p, *b;
173
174         if (!(p = strdup(path)))
175                 err(1, "strdup");
176         if (!(b = basename(p)))
177                 err(1, "basename");
178         if (!(b = strdup(b)))
179                 err(1, "strdup");
180         free(p);
181
182         return b;
183 }
184
185 int
186 mkdirp(const char *path)
187 {
188         char tmp[PATH_MAX], *p;
189
190         strlcpy(tmp, path, sizeof(tmp));
191         for (p = tmp + (tmp[0] == '/'); *p; p++) {
192                 if (*p != '/')
193                         continue;
194                 *p = '\0';
195                 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
196                         return -1;
197                 *p = '/';
198         }
199         if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
200                 return -1;
201         return 0;
202 }
203
204 void
205 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
206 {
207         struct tm *intm;
208         time_t t;
209         char out[32];
210
211         t = (time_t) intime->time + (intime->offset * 60);
212         intm = gmtime(&t);
213         strftime(out, sizeof(out), fmt, intm);
214         fputs(out, fp);
215 }
216
217 void
218 printtimez(FILE *fp, const git_time *intime)
219 {
220         printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
221 }
222
223 void
224 printtime(FILE *fp, const git_time *intime)
225 {
226         printtimeformat(fp, intime, "%a %b %e %T %Y");
227 }
228
229 void
230 printtimeshort(FILE *fp, const git_time *intime)
231 {
232         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
233 }
234
235 int
236 writeheader(FILE *fp)
237 {
238         fputs("<!DOCTYPE HTML>"
239                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
240                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
241                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
242         xmlencode(fp, name, strlen(name));
243         if (description[0])
244                 fputs(" - ", fp);
245         xmlencode(fp, description, strlen(description));
246         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
247         fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
248                 name, relpath);
249         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
250         fputs("</head>\n<body>\n<table><tr><td>", fp);
251         fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
252                 relpath, relpath);
253         fputs("</td><td><h1>", fp);
254         xmlencode(fp, name, strlen(name));
255         fputs("</h1><span class=\"desc\">", fp);
256         xmlencode(fp, description, strlen(description));
257         fputs("</span></td></tr>", fp);
258         if (cloneurl[0]) {
259                 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
260                 xmlencode(fp, cloneurl, strlen(cloneurl));
261                 fputs("\">", fp);
262                 xmlencode(fp, cloneurl, strlen(cloneurl));
263                 fputs("</a></td></tr>", fp);
264         }
265         fputs("<tr><td></td><td>\n", fp);
266         fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
267         fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
268         fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
269         if (hasreadme)
270                 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
271         if (haslicense)
272                 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
273         fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
274
275         return 0;
276 }
277
278 int
279 writefooter(FILE *fp)
280 {
281         return !fputs("</div></body>\n</html>", fp);
282 }
283
284 void
285 writeblobhtml(FILE *fp, const git_blob *blob)
286 {
287         off_t i;
288         size_t n = 1;
289         char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
290         const char *s = git_blob_rawcontent(blob);
291         git_off_t len = git_blob_rawsize(blob);
292
293         fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
294
295         if (len) {
296                 fprintf(fp, nfmt, n, n, n);
297                 for (i = 0; i < len - 1; i++) {
298                         if (s[i] == '\n') {
299                                 n++;
300                                 fprintf(fp, nfmt, n, n, n);
301                         }
302                 }
303         }
304
305         fputs("</pre></td><td><pre>\n", fp);
306         xmlencode(fp, s, (size_t)len);
307         fputs("</pre></td></tr></table>\n", fp);
308 }
309
310 void
311 printcommit(FILE *fp, struct commitinfo *ci)
312 {
313         fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
314                 relpath, ci->oid, ci->oid);
315
316         if (ci->parentoid[0])
317                 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
318                         relpath, ci->parentoid, ci->parentoid);
319
320 #if 0
321         if ((count = (int)git_commit_parentcount(commit)) > 1) {
322                 fputs("<b>Merge:</b>", fp);
323                 for (i = 0; i < count; i++) {
324                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
325                         fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
326                                 relpath, buf, buf);
327                 }
328                 fputc('\n', fp);
329         }
330 #endif
331         if (ci->author) {
332                 fputs("<b>Author:</b> ", fp);
333                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
334                 fputs(" &lt;<a href=\"mailto:", fp);
335                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
336                 fputs("\">", fp);
337                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
338                 fputs("</a>&gt;\n<b>Date:</b>   ", fp);
339                 printtime(fp, &(ci->author->when));
340                 fputc('\n', fp);
341         }
342         fputc('\n', fp);
343
344         if (ci->msg)
345                 xmlencode(fp, ci->msg, strlen(ci->msg));
346
347         fputc('\n', fp);
348 }
349
350 void
351 printshowfile(struct commitinfo *ci)
352 {
353         const git_diff_delta *delta;
354         const git_diff_hunk *hunk;
355         const git_diff_line *line;
356         git_patch *patch;
357         git_buf statsbuf;
358         size_t ndeltas, nhunks, nhunklines;
359         FILE *fp;
360         size_t i, j, k;
361         char path[PATH_MAX];
362
363         snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
364         /* check if file exists if so skip it */
365         if (!access(path, F_OK))
366                 return;
367
368         fp = efopen(path, "w");
369         writeheader(fp);
370         fputs("<pre>\n", fp);
371         printcommit(fp, ci);
372
373         memset(&statsbuf, 0, sizeof(statsbuf));
374
375         /* diff stat */
376         if (ci->stats) {
377                 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
378                     GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
379                         if (statsbuf.ptr && statsbuf.ptr[0]) {
380                                 fputs("<b>Diffstat:</b>\n", fp);
381                                 fputs(statsbuf.ptr, fp);
382                         }
383                 }
384         }
385
386         fputs("<hr/>", fp);
387
388         ndeltas = git_diff_num_deltas(ci->diff);
389         for (i = 0; i < ndeltas; i++) {
390                 if (git_patch_from_diff(&patch, ci->diff, i)) {
391                         git_patch_free(patch);
392                         break;
393                 }
394
395                 delta = git_patch_get_delta(patch);
396                 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
397                         relpath, delta->old_file.path, delta->old_file.path,
398                         relpath, delta->new_file.path, delta->new_file.path);
399
400                 /* check binary data */
401                 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
402                         fputs("Binary files differ\n", fp);
403                         git_patch_free(patch);
404                         continue;
405                 }
406
407                 nhunks = git_patch_num_hunks(patch);
408                 for (j = 0; j < nhunks; j++) {
409                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
410                                 break;
411
412                         fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
413
414                         for (k = 0; ; k++) {
415                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
416                                         break;
417                                 if (line->old_lineno == -1)
418                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
419                                                 j, k, j, k);
420                                 else if (line->new_lineno == -1)
421                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
422                                                 j, k, j, k);
423                                 else
424                                         fputc(' ', fp);
425                                 xmlencode(fp, line->content, line->content_len);
426                                 if (line->old_lineno == -1 || line->new_lineno == -1)
427                                         fputs("</a>", fp);
428                         }
429                 }
430                 git_patch_free(patch);
431         }
432         git_buf_free(&statsbuf);
433
434         fputs("</pre>\n", fp);
435         writefooter(fp);
436         fclose(fp);
437         return;
438 }
439
440 int
441 writelog(FILE *fp, const char *branch)
442 {
443         struct commitinfo *ci;
444         const git_oid *oid;
445         git_revwalk *w = NULL;
446         git_object *obj = NULL;
447         git_oid id;
448         size_t len;
449
450         mkdir("commit", 0755);
451
452         if (git_revparse_single(&obj, repo, branch))
453                 return -1;
454         oid = git_object_id(obj);
455
456         git_revwalk_new(&w, repo);
457         git_revwalk_push(w, oid);
458         git_revwalk_sorting(w, GIT_SORT_TIME);
459         git_revwalk_simplify_first_parent(w);
460
461         fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
462                   "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
463                   "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
464         while (!git_revwalk_next(&id, w)) {
465                 relpath = "";
466
467                 if (!(ci = commitinfo_getbyoid(&id)))
468                         break;
469
470                 fputs("<tr><td>", fp);
471                 if (ci->author)
472                         printtimeshort(fp, &(ci->author->when));
473                 fputs("</td><td>", fp);
474                 if (ci->summary) {
475                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
476                         if ((len = strlen(ci->summary)) > summarylen) {
477                                 xmlencode(fp, ci->summary, summarylen - 1);
478                                 fputs("…", fp);
479                         } else {
480                                 xmlencode(fp, ci->summary, len);
481                         }
482                         fputs("</a>", fp);
483                 }
484                 fputs("</td><td>", fp);
485                 if (ci->author)
486                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
487                 fputs("</td><td class=\"num\">", fp);
488                 fprintf(fp, "%zu", ci->filecount);
489                 fputs("</td><td class=\"num\">", fp);
490                 fprintf(fp, "+%zu", ci->addcount);
491                 fputs("</td><td class=\"num\">", fp);
492                 fprintf(fp, "-%zu", ci->delcount);
493                 fputs("</td></tr>\n", fp);
494
495                 relpath = "../";
496                 printshowfile(ci);
497
498                 commitinfo_free(ci);
499         }
500         fputs("</tbody></table>", fp);
501
502         git_revwalk_free(w);
503         git_object_free(obj);
504
505         relpath = "";
506
507         return 0;
508 }
509
510 void
511 printcommitatom(FILE *fp, struct commitinfo *ci)
512 {
513         fputs("<entry>\n", fp);
514
515         fprintf(fp, "<id>%s</id>\n", ci->oid);
516         if (ci->author) {
517                 fputs("<updated>", fp);
518                 printtimez(fp, &(ci->author->when));
519                 fputs("</updated>\n", fp);
520         }
521         if (ci->summary) {
522                 fputs("<title type=\"text\">", fp);
523                 xmlencode(fp, ci->summary, strlen(ci->summary));
524                 fputs("</title>\n", fp);
525         }
526
527         fputs("<content type=\"text\">", fp);
528         fprintf(fp, "commit %s\n", ci->oid);
529         if (ci->parentoid[0])
530                 fprintf(fp, "parent %s\n", ci->parentoid);
531
532 #if 0
533         if ((count = (int)git_commit_parentcount(commit)) > 1) {
534                 fputs("Merge:", fp);
535                 for (i = 0; i < count; i++) {
536                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
537                         fprintf(fp, " %s", buf);
538                 }
539                 fputc('\n', fp);
540         }
541 #endif
542
543         if (ci->author) {
544                 fputs("Author: ", fp);
545                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
546                 fputs(" &lt;", fp);
547                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
548                 fputs("&gt;\nDate:   ", fp);
549                 printtime(fp, &(ci->author->when));
550         }
551         fputc('\n', fp);
552
553         if (ci->msg)
554                 xmlencode(fp, ci->msg, strlen(ci->msg));
555         fputs("\n</content>\n", fp);
556         if (ci->author) {
557                 fputs("<author><name>", fp);
558                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
559                 fputs("</name>\n<email>", fp);
560                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
561                 fputs("</email>\n</author>\n", fp);
562         }
563         fputs("</entry>\n", fp);
564 }
565
566 int
567 writeatom(FILE *fp)
568 {
569         struct commitinfo *ci;
570         git_revwalk *w = NULL;
571         git_oid id;
572         size_t i, m = 100; /* max */
573
574         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
575               "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
576         xmlencode(fp, name, strlen(name));
577         fputs(", branch master</title>\n<subtitle>", fp);
578
579         xmlencode(fp, description, strlen(description));
580         fputs("</subtitle>\n", fp);
581
582         git_revwalk_new(&w, repo);
583         git_revwalk_push_head(w);
584         git_revwalk_sorting(w, GIT_SORT_TIME);
585         git_revwalk_simplify_first_parent(w);
586
587         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
588                 if (!(ci = commitinfo_getbyoid(&id)))
589                         break;
590                 printcommitatom(fp, ci);
591                 commitinfo_free(ci);
592         }
593         git_revwalk_free(w);
594
595         fputs("</feed>", fp);
596
597         return 0;
598 }
599
600 int
601 writeblob(git_object *obj, const char *filename, git_off_t filesize)
602 {
603         char fpath[PATH_MAX];
604         char tmp[PATH_MAX] = "";
605         char *d, *p;
606         FILE *fp;
607
608         snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
609         d = xdirname(fpath);
610         if (mkdirp(d)) {
611                 free(d);
612                 return 1;
613         }
614         free(d);
615
616         p = fpath;
617         while (*p) {
618                 if (*p == '/')
619                         strlcat(tmp, "../", sizeof(tmp));
620                 p++;
621         }
622         relpath = tmp;
623
624         fp = efopen(fpath, "w");
625         writeheader(fp);
626         fputs("<p> ", fp);
627         xmlencode(fp, filename, strlen(filename));
628         fprintf(fp, " (%" PRIu32 "b)", filesize);
629         fputs("</p><hr/>", fp);
630
631         if (git_blob_is_binary((git_blob *)obj)) {
632                 fputs("<p>Binary file</p>\n", fp);
633         } else {
634                 writeblobhtml(fp, (git_blob *)obj);
635                 if (ferror(fp))
636                         err(1, "fwrite");
637         }
638         writefooter(fp);
639         fclose(fp);
640
641         relpath = "";
642
643         return 0;
644 }
645
646 const char *
647 filemode(git_filemode_t m)
648 {
649         static char mode[11];
650
651         memset(mode, '-', sizeof(mode) - 1);
652         mode[10] = '\0';
653
654         if (S_ISREG(m))
655                 mode[0] = '-';
656         else if (S_ISBLK(m))
657                 mode[0] = 'b';
658         else if (S_ISCHR(m))
659                 mode[0] = 'c';
660         else if (S_ISDIR(m))
661                 mode[0] = 'd';
662         else if (S_ISFIFO(m))
663                 mode[0] = 'p';
664         else if (S_ISLNK(m))
665                 mode[0] = 'l';
666         else if (S_ISSOCK(m))
667                 mode[0] = 's';
668         else
669                 mode[0] = '?';
670
671         if (m & S_IRUSR) mode[1] = 'r';
672         if (m & S_IWUSR) mode[2] = 'w';
673         if (m & S_IXUSR) mode[3] = 'x';
674         if (m & S_IRGRP) mode[4] = 'r';
675         if (m & S_IWGRP) mode[5] = 'w';
676         if (m & S_IXGRP) mode[6] = 'x';
677         if (m & S_IROTH) mode[7] = 'r';
678         if (m & S_IWOTH) mode[8] = 'w';
679         if (m & S_IXOTH) mode[9] = 'x';
680
681         if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
682         if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
683         if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
684
685         return mode;
686 }
687
688 int
689 writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
690 {
691         const git_tree_entry *entry = NULL;
692         const char *filename;
693         char filepath[PATH_MAX];
694         git_object *obj = NULL;
695         git_off_t filesize;
696         size_t count, i;
697         int ret;
698
699         count = git_tree_entrycount(tree);
700         for (i = 0; i < count; i++) {
701                 if (!(entry = git_tree_entry_byindex(tree, i)))
702                         return -1;
703                 if (git_tree_entry_to_object(&obj, repo, entry))
704                         return -1;
705                 filename = git_tree_entry_name(entry);
706                 switch (git_object_type(obj)) {
707                 case GIT_OBJ_BLOB:
708                         break;
709                 case GIT_OBJ_TREE:
710                         ret = writefilestree(fp, (git_tree *)obj, branch,
711                                              filename);
712                         git_object_free(obj);
713                         if (ret)
714                                 return ret;
715                         continue;
716                 default:
717                         git_object_free(obj);
718                         continue;
719                 }
720                 if (path[0]) {
721                         snprintf(filepath, sizeof(filepath), "%s/%s",
722                                  path, filename);
723                         filename = filepath;
724                 }
725
726                 filesize = git_blob_rawsize((git_blob *)obj);
727
728                 fputs("<tr><td>", fp);
729                 fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry)));
730                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
731                 xmlencode(fp, filename, strlen(filename));
732                 fputs(".html\">", fp);
733                 xmlencode(fp, filename, strlen(filename));
734                 fputs("</a></td><td class=\"num\">", fp);
735                 fprintf(fp, "%" PRIu32, filesize);
736                 fputs("</td></tr>\n", fp);
737
738                 writeblob(obj, filename, filesize);
739         }
740
741         return 0;
742 }
743
744 int
745 writefiles(FILE *fp, const char *branch)
746 {
747         const git_oid *id;
748         git_tree *tree = NULL;
749         git_object *obj = NULL;
750         git_commit *commit = NULL;
751         int ret = -1;
752
753         fputs("<table id=\"files\"><thead>\n<tr>"
754               "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
755               "</tr>\n</thead><tbody>\n", fp);
756
757         if (git_revparse_single(&obj, repo, branch))
758                 goto err;
759         id = git_object_id(obj);
760         if (git_commit_lookup(&commit, repo, id))
761                 goto err;
762         if (git_commit_tree(&tree, commit)) {
763                 git_commit_free(commit);
764                 goto err;
765         }
766         ret = writefilestree(fp, tree, branch, "");
767
768 err:
769         fputs("</tbody></table>", fp);
770
771         git_object_free(obj);
772         git_commit_free(commit);
773         git_tree_free(tree);
774
775         return ret;
776 }
777
778 int
779 writebranches(FILE *fp)
780 {
781         struct commitinfo *ci;
782         git_branch_iterator *it = NULL;
783         git_branch_t branch;
784         git_reference *ref = NULL, *dref = NULL;
785         const git_oid *id = NULL;
786         const char *branchname = NULL;
787         size_t len;
788         int ret = -1;
789
790         /* log for local branches */
791         if (git_branch_iterator_new(&it, repo, GIT_BRANCH_LOCAL))
792                 return -1;
793
794         fputs("<h2>Branches</h2><table id=\"branches\"><thead>\n<tr><td>Branch</td><td>Age</td>"
795                   "<td>Commit message</td>"
796                   "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
797                   "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
798
799         while (!git_branch_next(&ref, &branch, it)) {
800                 if (git_branch_name(&branchname, ref))
801                         continue;
802
803                 id = NULL;
804                 switch (git_reference_type(ref)) {
805                 case GIT_REF_SYMBOLIC:
806                         if (git_reference_resolve(&dref, ref))
807                                 goto err;
808                         id = git_reference_target(dref);
809                         break;
810                 case GIT_REF_OID:
811                         id = git_reference_target(ref);
812                         break;
813                 default:
814                         continue;
815                 }
816                 if (!id)
817                         goto err;
818                 if (!(ci = commitinfo_getbyoid(id)))
819                         break;
820
821                 relpath = "";
822
823                 fputs("<tr><td>", fp);
824                 xmlencode(fp, branchname, strlen(branchname));
825                 fputs("</td><td>", fp);
826                 if (ci->author)
827                         printtimeshort(fp, &(ci->author->when));
828                 fputs("</td><td>", fp);
829                 if (ci->summary) {
830                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
831                         if ((len = strlen(ci->summary)) > summarylen) {
832                                 xmlencode(fp, ci->summary, summarylen - 1);
833                                 fputs("…", fp);
834                         } else {
835                                 xmlencode(fp, ci->summary, len);
836                         }
837                         fputs("</a>", fp);
838                 }
839                 fputs("</td><td>", fp);
840                 if (ci->author)
841                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
842                 fputs("</td><td class=\"num\">", fp);
843                 fprintf(fp, "%zu", ci->filecount);
844                 fputs("</td><td class=\"num\">", fp);
845                 fprintf(fp, "+%zu", ci->addcount);
846                 fputs("</td><td class=\"num\">", fp);
847                 fprintf(fp, "-%zu", ci->delcount);
848                 fputs("</td></tr>\n", fp);
849
850                 relpath = "../";
851
852                 commitinfo_free(ci);
853                 git_reference_free(ref);
854                 git_reference_free(dref);
855                 ref = NULL;
856                 dref = NULL;
857         }
858         ret = 0;
859
860 err:
861         fputs("</tbody></table>", fp);
862         git_reference_free(ref);
863         git_reference_free(dref);
864         git_branch_iterator_free(it);
865
866         return ret;
867 }
868
869 int
870 tagcompare(void *s1, void *s2)
871 {
872         return strcmp(*(char **)s1, *(char **)s2);
873 }
874
875 int
876 writetags(FILE *fp)
877 {
878         struct commitinfo *ci;
879         git_strarray tagnames;
880         git_object *obj = NULL;
881         const git_oid *id = NULL;
882         size_t i, len;
883
884         /* summary page with branches and tags */
885         memset(&tagnames, 0, sizeof(tagnames));
886         if (git_tag_list(&tagnames, repo))
887                 return -1;
888         if (!tagnames.count) {
889                 git_strarray_free(&tagnames);
890                 return 0;
891         }
892
893         /* sort names */
894         qsort(tagnames.strings, tagnames.count, sizeof(char *),
895               (int (*)(const void *, const void *))&tagcompare);
896
897         fputs("<h2>Tags</h2><table id=\"branches\"><thead>\n<tr><td>Tag</td>"
898               "<td>Age</td><td>Commit message</td>"
899               "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
900               "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
901
902         for (i = 0; i < tagnames.count; i++) {
903                 if (git_revparse_single(&obj, repo, tagnames.strings[i]))
904                         continue;
905                 id = git_object_id(obj);
906                 if (!(ci = commitinfo_getbyoid(id)))
907                         break;
908
909                 relpath = "";
910
911                 fputs("<tr><td>", fp);
912                 xmlencode(fp, tagnames.strings[i], strlen(tagnames.strings[i]));
913                 fputs("</td><td>", fp);
914                 if (ci->author)
915                         printtimeshort(fp, &(ci->author->when));
916                 fputs("</td><td>", fp);
917                 if (ci->summary) {
918                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
919                         if ((len = strlen(ci->summary)) > summarylen) {
920                                 xmlencode(fp, ci->summary, summarylen - 1);
921                                 fputs("…", fp);
922                         } else {
923                                 xmlencode(fp, ci->summary, len);
924                         }
925                         fputs("</a>", fp);
926                 }
927                 fputs("</td><td>", fp);
928                 if (ci->author)
929                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
930                 fputs("</td><td class=\"num\">", fp);
931                 fprintf(fp, "%zu", ci->filecount);
932                 fputs("</td><td class=\"num\">", fp);
933                 fprintf(fp, "+%zu", ci->addcount);
934                 fputs("</td><td class=\"num\">", fp);
935                 fprintf(fp, "-%zu", ci->delcount);
936                 fputs("</td></tr>\n", fp);
937
938                 relpath = "../";
939
940                 commitinfo_free(ci);
941                 git_object_free(obj);
942         }
943         fputs("</tbody></table>", fp);
944         git_strarray_free(&tagnames);
945
946         return 0;
947 }
948
949 int
950 writerefs(FILE *fp)
951 {
952         int ret;
953
954         if ((ret = writebranches(fp)))
955                 return ret;
956         fputs("<br/>", fp);
957         return writetags(fp);
958 }
959
960 int
961 main(int argc, char *argv[])
962 {
963         git_object *obj = NULL;
964         git_branch_iterator *it = NULL;
965         git_branch_t branch;
966         git_reference *ref = NULL;
967         const char *branchname = NULL;
968         const git_error *e = NULL;
969         FILE *fp, *fpread;
970         char path[PATH_MAX], *p;
971         int status;
972
973         if (argc != 2) {
974                 fprintf(stderr, "%s <repodir>\n", argv[0]);
975                 return 1;
976         }
977         repodir = argv[1];
978
979         git_libgit2_init();
980
981         if ((status = git_repository_open_ext(&repo, repodir,
982                 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
983                 e = giterr_last();
984                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
985                 return status;
986         }
987
988         /* use directory name as name */
989         p = xbasename(repodir);
990         snprintf(name, sizeof(name), "%s", p);
991         free(p);
992
993         /* read description or .git/description */
994         snprintf(path, sizeof(path), "%s%s%s",
995                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
996         if (!(fpread = fopen(path, "r"))) {
997                 snprintf(path, sizeof(path), "%s%s%s",
998                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
999                 fpread = fopen(path, "r");
1000         }
1001         if (fpread) {
1002                 if (!fgets(description, sizeof(description), fpread))
1003                         description[0] = '\0';
1004                 fclose(fpread);
1005         }
1006
1007         /* read url or .git/url */
1008         snprintf(path, sizeof(path), "%s%s%s",
1009                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "url");
1010         if (!(fpread = fopen(path, "r"))) {
1011                 snprintf(path, sizeof(path), "%s%s%s",
1012                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/url");
1013                 fpread = fopen(path, "r");
1014         }
1015         if (fpread) {
1016                 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1017                         cloneurl[0] = '\0';
1018                 fclose(fpread);
1019         }
1020
1021         /* check LICENSE */
1022         haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
1023         git_object_free(obj);
1024         /* check README */
1025         hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
1026         git_object_free(obj);
1027
1028         /* log for HEAD */
1029         fp = efopen("log.html", "w");
1030         writeheader(fp);
1031         writelog(fp, "HEAD");
1032         writefooter(fp);
1033         fclose(fp);
1034
1035         /* log for local branches */
1036         if (git_branch_iterator_new(&it, repo, GIT_BRANCH_LOCAL))
1037                 err(1, "git_branch_iterator_new");
1038
1039         while (!git_branch_next(&ref, &branch, it)) {
1040                 if (git_branch_name(&branchname, ref))
1041                         continue;
1042
1043                 snprintf(path, sizeof(path), "log-%s.html", branchname);
1044
1045                 fp = efopen(path, "w");
1046                 writeheader(fp);
1047                 writelog(fp, branchname);
1048                 writefooter(fp);
1049                 fclose(fp);
1050         }
1051         git_reference_free(ref);
1052         git_branch_iterator_free(it);
1053
1054         /* files for HEAD */
1055         fp = efopen("files.html", "w");
1056         writeheader(fp);
1057         writefiles(fp, "HEAD");
1058         writefooter(fp);
1059         fclose(fp);
1060
1061         /* summary page with branches and tags */
1062         fp = efopen("refs.html", "w");
1063         writeheader(fp);
1064         writerefs(fp);
1065         writefooter(fp);
1066         fclose(fp);
1067
1068         /* Atom feed */
1069         fp = efopen("atom.xml", "w");
1070         writeatom(fp);
1071         fclose(fp);
1072
1073         /* cleanup */
1074         git_repository_free(repo);
1075         git_libgit2_shutdown();
1076
1077         return 0;
1078 }