]> git.armaanb.net Git - stagit.git/blob - stagit.c
fix cast to uintmax_t
[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/branches</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, "<a href=\"#h%zu\" id=\"h%zu\" class=\"h\">", j, j);
413                         xmlencode(fp, hunk->header, hunk->header_len);
414                         fputs("</a>", fp);
415
416                         for (k = 0; ; k++) {
417                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
418                                         break;
419                                 if (line->old_lineno == -1)
420                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
421                                                 j, k, j, k);
422                                 else if (line->new_lineno == -1)
423                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
424                                                 j, k, j, k);
425                                 else
426                                         fputc(' ', fp);
427                                 xmlencode(fp, line->content, line->content_len);
428                                 if (line->old_lineno == -1 || line->new_lineno == -1)
429                                         fputs("</a>", fp);
430                         }
431                 }
432                 git_patch_free(patch);
433         }
434         git_buf_free(&statsbuf);
435
436         fputs("</pre>\n", fp);
437         writefooter(fp);
438         fclose(fp);
439         return;
440 }
441
442 int
443 writelog(FILE *fp, const char *branch)
444 {
445         struct commitinfo *ci;
446         const git_oid *oid;
447         git_revwalk *w = NULL;
448         git_object *obj = NULL;
449         git_oid id;
450         size_t len;
451
452         mkdir("commit", 0755);
453
454         if (git_revparse_single(&obj, repo, branch))
455                 return -1;
456         oid = git_object_id(obj);
457
458         git_revwalk_new(&w, repo);
459         git_revwalk_push(w, oid);
460         git_revwalk_sorting(w, GIT_SORT_TIME);
461         git_revwalk_simplify_first_parent(w);
462
463         fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
464                   "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
465                   "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
466
467         while (!git_revwalk_next(&id, w)) {
468                 relpath = "";
469
470                 if (!(ci = commitinfo_getbyoid(&id)))
471                         break;
472
473                 fputs("<tr><td>", fp);
474                 if (ci->author)
475                         printtimeshort(fp, &(ci->author->when));
476                 fputs("</td><td>", fp);
477                 if (ci->summary) {
478                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
479                         if ((len = strlen(ci->summary)) > summarylen) {
480                                 xmlencode(fp, ci->summary, summarylen - 1);
481                                 fputs("…", fp);
482                         } else {
483                                 xmlencode(fp, ci->summary, len);
484                         }
485                         fputs("</a>", fp);
486                 }
487                 fputs("</td><td>", fp);
488                 if (ci->author)
489                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
490                 fputs("</td><td class=\"num\">", fp);
491                 fprintf(fp, "%zu", ci->filecount);
492                 fputs("</td><td class=\"num\">", fp);
493                 fprintf(fp, "+%zu", ci->addcount);
494                 fputs("</td><td class=\"num\">", fp);
495                 fprintf(fp, "-%zu", ci->delcount);
496                 fputs("</td></tr>\n", fp);
497
498                 relpath = "../";
499                 printshowfile(ci);
500
501                 commitinfo_free(ci);
502         }
503         fputs("</tbody></table>", fp);
504
505         git_revwalk_free(w);
506         git_object_free(obj);
507
508         relpath = "";
509
510         return 0;
511 }
512
513 void
514 printcommitatom(FILE *fp, struct commitinfo *ci)
515 {
516         fputs("<entry>\n", fp);
517
518         fprintf(fp, "<id>%s</id>\n", ci->oid);
519         if (ci->author) {
520                 fputs("<updated>", fp);
521                 printtimez(fp, &(ci->author->when));
522                 fputs("</updated>\n", fp);
523         }
524         if (ci->summary) {
525                 fputs("<title type=\"text\">", fp);
526                 xmlencode(fp, ci->summary, strlen(ci->summary));
527                 fputs("</title>\n", fp);
528         }
529
530         fputs("<content type=\"text\">", fp);
531         fprintf(fp, "commit %s\n", ci->oid);
532         if (ci->parentoid[0])
533                 fprintf(fp, "parent %s\n", ci->parentoid);
534
535 #if 0
536         if ((count = (int)git_commit_parentcount(commit)) > 1) {
537                 fputs("Merge:", fp);
538                 for (i = 0; i < count; i++) {
539                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
540                         fprintf(fp, " %s", buf);
541                 }
542                 fputc('\n', fp);
543         }
544 #endif
545
546         if (ci->author) {
547                 fputs("Author: ", fp);
548                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
549                 fputs(" &lt;", fp);
550                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
551                 fputs("&gt;\nDate:   ", fp);
552                 printtime(fp, &(ci->author->when));
553         }
554         fputc('\n', fp);
555
556         if (ci->msg)
557                 xmlencode(fp, ci->msg, strlen(ci->msg));
558         fputs("\n</content>\n", fp);
559         if (ci->author) {
560                 fputs("<author><name>", fp);
561                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
562                 fputs("</name>\n<email>", fp);
563                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
564                 fputs("</email>\n</author>\n", fp);
565         }
566         fputs("</entry>\n", fp);
567 }
568
569 int
570 writeatom(FILE *fp)
571 {
572         struct commitinfo *ci;
573         git_revwalk *w = NULL;
574         git_oid id;
575         size_t i, m = 100; /* max */
576
577         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
578               "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
579         xmlencode(fp, name, strlen(name));
580         fputs(", branch master</title>\n<subtitle>", fp);
581
582         xmlencode(fp, description, strlen(description));
583         fputs("</subtitle>\n", fp);
584
585         git_revwalk_new(&w, repo);
586         git_revwalk_push_head(w);
587         git_revwalk_sorting(w, GIT_SORT_TIME);
588         git_revwalk_simplify_first_parent(w);
589
590         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
591                 if (!(ci = commitinfo_getbyoid(&id)))
592                         break;
593                 printcommitatom(fp, ci);
594                 commitinfo_free(ci);
595         }
596         git_revwalk_free(w);
597
598         fputs("</feed>", fp);
599
600         return 0;
601 }
602
603 int
604 writeblob(git_object *obj, const char *filename, git_off_t filesize)
605 {
606         char fpath[PATH_MAX];
607         char tmp[PATH_MAX] = "";
608         char *d, *p;
609         FILE *fp;
610
611         snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
612         d = xdirname(fpath);
613         if (mkdirp(d)) {
614                 free(d);
615                 return 1;
616         }
617         free(d);
618
619         p = fpath;
620         while (*p) {
621                 if (*p == '/')
622                         strlcat(tmp, "../", sizeof(tmp));
623                 p++;
624         }
625         relpath = tmp;
626
627         fp = efopen(fpath, "w");
628         writeheader(fp);
629         fputs("<p> ", fp);
630         xmlencode(fp, filename, strlen(filename));
631         fprintf(fp, " (%jub)", (uintmax_t)filesize);
632         fputs("</p><hr/>", fp);
633
634         if (git_blob_is_binary((git_blob *)obj)) {
635                 fputs("<p>Binary file</p>\n", fp);
636         } else {
637                 writeblobhtml(fp, (git_blob *)obj);
638                 if (ferror(fp))
639                         err(1, "fwrite");
640         }
641         writefooter(fp);
642         fclose(fp);
643
644         relpath = "";
645
646         return 0;
647 }
648
649 const char *
650 filemode(git_filemode_t m)
651 {
652         static char mode[11];
653
654         memset(mode, '-', sizeof(mode) - 1);
655         mode[10] = '\0';
656
657         if (S_ISREG(m))
658                 mode[0] = '-';
659         else if (S_ISBLK(m))
660                 mode[0] = 'b';
661         else if (S_ISCHR(m))
662                 mode[0] = 'c';
663         else if (S_ISDIR(m))
664                 mode[0] = 'd';
665         else if (S_ISFIFO(m))
666                 mode[0] = 'p';
667         else if (S_ISLNK(m))
668                 mode[0] = 'l';
669         else if (S_ISSOCK(m))
670                 mode[0] = 's';
671         else
672                 mode[0] = '?';
673
674         if (m & S_IRUSR) mode[1] = 'r';
675         if (m & S_IWUSR) mode[2] = 'w';
676         if (m & S_IXUSR) mode[3] = 'x';
677         if (m & S_IRGRP) mode[4] = 'r';
678         if (m & S_IWGRP) mode[5] = 'w';
679         if (m & S_IXGRP) mode[6] = 'x';
680         if (m & S_IROTH) mode[7] = 'r';
681         if (m & S_IWOTH) mode[8] = 'w';
682         if (m & S_IXOTH) mode[9] = 'x';
683
684         if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
685         if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
686         if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
687
688         return mode;
689 }
690
691 int
692 writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
693 {
694         const git_tree_entry *entry = NULL;
695         const char *filename;
696         char filepath[PATH_MAX];
697         git_object *obj = NULL;
698         git_off_t filesize;
699         size_t count, i;
700         int ret;
701
702         count = git_tree_entrycount(tree);
703         for (i = 0; i < count; i++) {
704                 if (!(entry = git_tree_entry_byindex(tree, i)) ||
705                     git_tree_entry_to_object(&obj, repo, entry))
706                         return -1;
707                 filename = git_tree_entry_name(entry);
708                 switch (git_object_type(obj)) {
709                 case GIT_OBJ_BLOB:
710                         break;
711                 case GIT_OBJ_TREE:
712                         /* NOTE: recurses */
713                         ret = writefilestree(fp, (git_tree *)obj, branch,
714                                              filename);
715                         git_object_free(obj);
716                         if (ret)
717                                 return ret;
718                         continue;
719                 default:
720                         git_object_free(obj);
721                         continue;
722                 }
723                 if (path[0]) {
724                         snprintf(filepath, sizeof(filepath), "%s/%s",
725                                  path, filename);
726                         filename = filepath;
727                 }
728                 filesize = git_blob_rawsize((git_blob *)obj);
729
730                 fputs("<tr><td>", fp);
731                 fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry)));
732                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
733                 xmlencode(fp, filename, strlen(filename));
734                 fputs(".html\">", fp);
735                 xmlencode(fp, filename, strlen(filename));
736                 fputs("</a></td><td class=\"num\">", fp);
737                 fprintf(fp, "%ju", (uintmax_t)filesize);
738                 fputs("</td></tr>\n", fp);
739
740                 writeblob(obj, filename, filesize);
741         }
742
743         return 0;
744 }
745
746 int
747 writefiles(FILE *fp, const char *branch)
748 {
749         const git_oid *id;
750         git_tree *tree = NULL;
751         git_object *obj = NULL;
752         git_commit *commit = NULL;
753         int ret = -1;
754
755         fputs("<table id=\"files\"><thead>\n<tr>"
756               "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
757               "</tr>\n</thead><tbody>\n", fp);
758
759         if (git_revparse_single(&obj, repo, branch))
760                 goto err;
761         id = git_object_id(obj);
762         if (git_commit_lookup(&commit, repo, id) ||
763             git_commit_tree(&tree, commit))
764                 goto err;
765         ret = writefilestree(fp, tree, branch, "");
766
767 err:
768         fputs("</tbody></table>", fp);
769
770         git_object_free(obj);
771         git_commit_free(commit);
772         git_tree_free(tree);
773
774         return ret;
775 }
776
777 int
778 writebranches(FILE *fp)
779 {
780         struct commitinfo *ci;
781         git_branch_iterator *it = NULL;
782         git_branch_t branch;
783         git_reference *ref = NULL, *dref = NULL;
784         const git_oid *id = NULL;
785         const char *branchname = NULL;
786         size_t len;
787         int ret = -1;
788
789         /* log for local branches */
790         if (git_branch_iterator_new(&it, repo, GIT_BRANCH_LOCAL))
791                 return -1;
792
793         fputs("<h2>Branches</h2><table id=\"branches\"><thead>\n<tr><td>Branch</td><td>Age</td>"
794                   "<td>Commit message</td>"
795                   "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
796                   "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
797
798         while (!git_branch_next(&ref, &branch, it)) {
799                 if (git_branch_name(&branchname, ref))
800                         continue;
801
802                 id = NULL;
803                 switch (git_reference_type(ref)) {
804                 case GIT_REF_SYMBOLIC:
805                         if (git_reference_resolve(&dref, ref))
806                                 goto err;
807                         id = git_reference_target(dref);
808                         break;
809                 case GIT_REF_OID:
810                         id = git_reference_target(ref);
811                         break;
812                 default:
813                         continue;
814                 }
815                 if (!id)
816                         goto err;
817                 if (!(ci = commitinfo_getbyoid(id)))
818                         break;
819
820                 relpath = "";
821
822                 fputs("<tr><td>", fp);
823                 xmlencode(fp, branchname, strlen(branchname));
824                 fputs("</td><td>", fp);
825                 if (ci->author)
826                         printtimeshort(fp, &(ci->author->when));
827                 fputs("</td><td>", fp);
828                 if (ci->summary) {
829                         if ((len = strlen(ci->summary)) > summarylen) {
830                                 xmlencode(fp, ci->summary, summarylen - 1);
831                                 fputs("…", fp);
832                         } else {
833                                 xmlencode(fp, ci->summary, len);
834                         }
835                 }
836                 fputs("</td><td>", fp);
837                 if (ci->author)
838                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
839                 fputs("</td><td class=\"num\">", fp);
840                 fprintf(fp, "%zu", ci->filecount);
841                 fputs("</td><td class=\"num\">", fp);
842                 fprintf(fp, "+%zu", ci->addcount);
843                 fputs("</td><td class=\"num\">", fp);
844                 fprintf(fp, "-%zu", ci->delcount);
845                 fputs("</td></tr>\n", fp);
846
847                 relpath = "../";
848
849                 commitinfo_free(ci);
850                 git_reference_free(ref);
851                 git_reference_free(dref);
852                 ref = NULL;
853                 dref = NULL;
854         }
855         ret = 0;
856
857 err:
858         fputs("</tbody></table>", fp);
859         git_reference_free(ref);
860         git_reference_free(dref);
861         git_branch_iterator_free(it);
862
863         return ret;
864 }
865
866 int
867 tagcompare(void *s1, void *s2)
868 {
869         return strcmp(*(char **)s1, *(char **)s2);
870 }
871
872 int
873 writetags(FILE *fp)
874 {
875         struct commitinfo *ci;
876         git_strarray tagnames;
877         git_object *obj = NULL;
878         git_tag *tag = NULL;
879         const git_oid *id = NULL;
880         size_t i, len;
881
882         /* summary page with branches and tags */
883         memset(&tagnames, 0, sizeof(tagnames));
884         if (git_tag_list(&tagnames, repo))
885                 return -1;
886         if (!tagnames.count) {
887                 git_strarray_free(&tagnames);
888                 return 0;
889         }
890
891         /* sort names */
892         qsort(tagnames.strings, tagnames.count, sizeof(char *),
893               (int (*)(const void *, const void *))&tagcompare);
894
895         fputs("<h2>Tags</h2><table id=\"tags\"><thead>\n<tr><td>Tag</td>"
896               "<td>Age</td><td>Commit message</td>"
897               "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
898               "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
899
900         for (i = 0; i < tagnames.count; i++) {
901                 if (git_revparse_single(&obj, repo, tagnames.strings[i]))
902                         continue;
903                 id = git_object_id(obj);
904
905                 /* lookup actual commit (from annotated tag etc) */
906                 if (!git_tag_lookup(&tag, repo, id)) {
907                         git_object_free(obj);
908                         obj = NULL;
909                         if (git_tag_peel(&obj, tag))
910                                 break;
911                         git_tag_free(tag);
912                         tag = NULL;
913                         id = git_object_id(obj);
914                 }
915
916                 if (!(ci = commitinfo_getbyoid(id)))
917                         break;
918
919                 relpath = "";
920
921                 fputs("<tr><td>", fp);
922                 xmlencode(fp, tagnames.strings[i], strlen(tagnames.strings[i]));
923                 fputs("</td><td>", fp);
924                 if (ci->author)
925                         printtimeshort(fp, &(ci->author->when));
926                 fputs("</td><td>", fp);
927                 if (ci->summary) {
928                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
929                         if ((len = strlen(ci->summary)) > summarylen) {
930                                 xmlencode(fp, ci->summary, summarylen - 1);
931                                 fputs("…", fp);
932                         } else {
933                                 xmlencode(fp, ci->summary, len);
934                         }
935                         fputs("</a>", fp);
936                 }
937                 fputs("</td><td>", fp);
938                 if (ci->author)
939                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
940                 fputs("</td><td class=\"num\">", fp);
941                 fprintf(fp, "%zu", ci->filecount);
942                 fputs("</td><td class=\"num\">", fp);
943                 fprintf(fp, "+%zu", ci->addcount);
944                 fputs("</td><td class=\"num\">", fp);
945                 fprintf(fp, "-%zu", ci->delcount);
946                 fputs("</td></tr>\n", fp);
947
948                 relpath = "../";
949
950                 commitinfo_free(ci);
951                 git_object_free(obj);
952                 obj = NULL;
953         }
954         fputs("</tbody></table>", fp);
955         git_strarray_free(&tagnames);
956         git_tag_free(tag);
957         git_object_free(obj);
958
959         return 0;
960 }
961
962 int
963 writerefs(FILE *fp)
964 {
965         int ret;
966
967         if ((ret = writebranches(fp)))
968                 return ret;
969         fputs("<br/>", fp);
970         return writetags(fp);
971 }
972
973 int
974 main(int argc, char *argv[])
975 {
976         git_object *obj = NULL;
977         const git_error *e = NULL;
978         FILE *fp, *fpread;
979         char path[PATH_MAX], *p;
980         int status;
981
982         if (argc != 2) {
983                 fprintf(stderr, "%s <repodir>\n", argv[0]);
984                 return 1;
985         }
986         repodir = argv[1];
987
988         git_libgit2_init();
989
990         if ((status = git_repository_open_ext(&repo, repodir,
991                 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
992                 e = giterr_last();
993                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
994                 return status;
995         }
996
997         /* use directory name as name */
998         p = xbasename(repodir);
999         snprintf(name, sizeof(name), "%s", p);
1000         free(p);
1001
1002         /* read description or .git/description */
1003         snprintf(path, sizeof(path), "%s%s%s",
1004                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
1005         if (!(fpread = fopen(path, "r"))) {
1006                 snprintf(path, sizeof(path), "%s%s%s",
1007                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
1008                 fpread = fopen(path, "r");
1009         }
1010         if (fpread) {
1011                 if (!fgets(description, sizeof(description), fpread))
1012                         description[0] = '\0';
1013                 fclose(fpread);
1014         }
1015
1016         /* read url or .git/url */
1017         snprintf(path, sizeof(path), "%s%s%s",
1018                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "url");
1019         if (!(fpread = fopen(path, "r"))) {
1020                 snprintf(path, sizeof(path), "%s%s%s",
1021                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/url");
1022                 fpread = fopen(path, "r");
1023         }
1024         if (fpread) {
1025                 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1026                         cloneurl[0] = '\0';
1027                 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1028                 fclose(fpread);
1029         }
1030
1031         /* check LICENSE */
1032         haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
1033         git_object_free(obj);
1034         /* check README */
1035         hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
1036         git_object_free(obj);
1037
1038         /* log for HEAD */
1039         mkdir("log", 0755);
1040         fp = efopen("log.html", "w");
1041         relpath = "";
1042         writeheader(fp);
1043         writelog(fp, "HEAD");
1044         writefooter(fp);
1045         fclose(fp);
1046
1047         /* files for HEAD */
1048         fp = efopen("files.html", "w");
1049         writeheader(fp);
1050         writefiles(fp, "HEAD");
1051         writefooter(fp);
1052         fclose(fp);
1053
1054         /* summary page with branches and tags */
1055         fp = efopen("refs.html", "w");
1056         writeheader(fp);
1057         writerefs(fp);
1058         writefooter(fp);
1059         fclose(fp);
1060
1061         /* Atom feed */
1062         fp = efopen("atom.xml", "w");
1063         writeatom(fp);
1064         fclose(fp);
1065
1066         /* cleanup */
1067         git_repository_free(repo);
1068         git_libgit2_shutdown();
1069
1070         return 0;
1071 }