]> git.armaanb.net Git - stagit.git/blob - stagit.c
detect copies and renames based on the threshold of changes
[stagit.git] / stagit.c
1 #include <sys/stat.h>
2 #include <sys/types.h>
3
4 #include <err.h>
5 #include <errno.h>
6 #include <inttypes.h>
7 #include <libgen.h>
8 #include <limits.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <git2.h>
15
16 #include "compat.h"
17
18 struct deltainfo {
19         git_patch *patch;
20
21         size_t addcount;
22         size_t delcount;
23 };
24
25 struct commitinfo {
26         const git_oid *id;
27
28         char oid[GIT_OID_HEXSZ + 1];
29         char parentoid[GIT_OID_HEXSZ + 1];
30
31         const git_signature *author;
32         const git_signature *committer;
33         const char          *summary;
34         const char          *msg;
35
36         git_diff   *diff;
37         git_commit *commit;
38         git_commit *parent;
39         git_tree   *commit_tree;
40         git_tree   *parent_tree;
41
42         size_t addcount;
43         size_t delcount;
44         size_t filecount;
45
46         struct deltainfo **deltas;
47         size_t ndeltas;
48 };
49
50 static git_repository *repo;
51
52 static const char *relpath = "";
53 static const char *repodir;
54
55 static char *name = "";
56 static char *strippedname = "";
57 static char description[255];
58 static char cloneurl[1024];
59 static int haslicense, hasreadme, hassubmodules;
60 static long long nlogcommits = -1; /* < 0 indicates not used */
61
62 /* cache */
63 static git_oid lastoid;
64 static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + nul byte */
65 static FILE *rcachefp, *wcachefp;
66 static const char *cachefile;
67
68 void
69 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
70 {
71         int r;
72
73         r = snprintf(buf, bufsiz, "%s%s%s",
74                 path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
75         if (r == -1 || (size_t)r >= bufsiz)
76                 errx(1, "path truncated: '%s%s%s'",
77                         path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
78 }
79
80 void
81 deltainfo_free(struct deltainfo *di)
82 {
83         if (!di)
84                 return;
85         git_patch_free(di->patch);
86         memset(di, 0, sizeof(*di));
87         free(di);
88 }
89
90 int
91 commitinfo_getstats(struct commitinfo *ci)
92 {
93         struct deltainfo *di;
94         git_diff_options opts;
95         git_diff_find_options fopts;
96         const git_diff_delta *delta;
97         const git_diff_hunk *hunk;
98         const git_diff_line *line;
99         git_patch *patch = NULL;
100         size_t ndeltas, nhunks, nhunklines;
101         size_t i, j, k;
102
103         if (git_tree_lookup(&(ci->commit_tree), repo, git_commit_tree_id(ci->commit)))
104                 goto err;
105         if (!git_commit_parent(&(ci->parent), ci->commit, 0)) {
106                 if (git_tree_lookup(&(ci->parent_tree), repo, git_commit_tree_id(ci->parent))) {
107                         ci->parent = NULL;
108                         ci->parent_tree = NULL;
109                 }
110         }
111
112         git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
113         opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
114         if (git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts))
115                 goto err;
116
117         if (git_diff_find_init_options(&fopts, GIT_DIFF_FIND_OPTIONS_VERSION))
118                 goto err;
119         fopts.flags |= GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
120         if (git_diff_find_similar(ci->diff, &fopts))
121                 goto err;
122
123         ndeltas = git_diff_num_deltas(ci->diff);
124         if (ndeltas && !(ci->deltas = calloc(ndeltas, sizeof(struct deltainfo *))))
125                 err(1, "calloc");
126
127         for (i = 0; i < ndeltas; i++) {
128                 if (git_patch_from_diff(&patch, ci->diff, i))
129                         goto err;
130
131                 if (!(di = calloc(1, sizeof(struct deltainfo))))
132                         err(1, "calloc");
133                 di->patch = patch;
134                 ci->deltas[i] = di;
135
136                 delta = git_patch_get_delta(patch);
137
138                 /* skip stats for binary data */
139                 if (delta->flags & GIT_DIFF_FLAG_BINARY)
140                         continue;
141
142                 nhunks = git_patch_num_hunks(patch);
143                 for (j = 0; j < nhunks; j++) {
144                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
145                                 break;
146                         for (k = 0; ; k++) {
147                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
148                                         break;
149                                 if (line->old_lineno == -1) {
150                                         di->addcount++;
151                                         ci->addcount++;
152                                 } else if (line->new_lineno == -1) {
153                                         di->delcount++;
154                                         ci->delcount++;
155                                 }
156                         }
157                 }
158         }
159         ci->ndeltas = i;
160         ci->filecount = i;
161
162         return 0;
163
164 err:
165         git_diff_free(ci->diff);
166         ci->diff = NULL;
167         git_tree_free(ci->commit_tree);
168         ci->commit_tree = NULL;
169         git_tree_free(ci->parent_tree);
170         ci->parent_tree = NULL;
171         git_commit_free(ci->parent);
172         ci->parent = NULL;
173
174         if (ci->deltas)
175                 for (i = 0; i < ci->ndeltas; i++)
176                         deltainfo_free(ci->deltas[i]);
177         free(ci->deltas);
178         ci->deltas = NULL;
179         ci->ndeltas = 0;
180         ci->addcount = 0;
181         ci->delcount = 0;
182         ci->filecount = 0;
183
184         return -1;
185 }
186
187 void
188 commitinfo_free(struct commitinfo *ci)
189 {
190         size_t i;
191
192         if (!ci)
193                 return;
194         if (ci->deltas)
195                 for (i = 0; i < ci->ndeltas; i++)
196                         deltainfo_free(ci->deltas[i]);
197
198         free(ci->deltas);
199         git_diff_free(ci->diff);
200         git_tree_free(ci->commit_tree);
201         git_tree_free(ci->parent_tree);
202         git_commit_free(ci->commit);
203         git_commit_free(ci->parent);
204         memset(ci, 0, sizeof(*ci));
205         free(ci);
206 }
207
208 struct commitinfo *
209 commitinfo_getbyoid(const git_oid *id)
210 {
211         struct commitinfo *ci;
212
213         if (!(ci = calloc(1, sizeof(struct commitinfo))))
214                 err(1, "calloc");
215
216         if (git_commit_lookup(&(ci->commit), repo, id))
217                 goto err;
218         ci->id = id;
219
220         git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
221         git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
222
223         ci->author = git_commit_author(ci->commit);
224         ci->committer = git_commit_committer(ci->commit);
225         ci->summary = git_commit_summary(ci->commit);
226         ci->msg = git_commit_message(ci->commit);
227
228         return ci;
229
230 err:
231         commitinfo_free(ci);
232
233         return NULL;
234 }
235
236 FILE *
237 efopen(const char *name, const char *flags)
238 {
239         FILE *fp;
240
241         if (!(fp = fopen(name, flags)))
242                 err(1, "fopen: '%s'", name);
243
244         return fp;
245 }
246
247 /* Escape characters below as HTML 2.0 / XML 1.0. */
248 void
249 xmlencode(FILE *fp, const char *s, size_t len)
250 {
251         size_t i;
252
253         for (i = 0; *s && i < len; s++, i++) {
254                 switch(*s) {
255                 case '<':  fputs("&lt;",   fp); break;
256                 case '>':  fputs("&gt;",   fp); break;
257                 case '\'': fputs("&#39;",  fp); break;
258                 case '&':  fputs("&amp;",  fp); break;
259                 case '"':  fputs("&quot;", fp); break;
260                 default:   fputc(*s, fp);
261                 }
262         }
263 }
264
265 int
266 mkdirp(const char *path)
267 {
268         char tmp[PATH_MAX], *p;
269
270         if (strlcpy(tmp, path, sizeof(tmp)) >= sizeof(tmp))
271                 errx(1, "path truncated: '%s'", path);
272         for (p = tmp + (tmp[0] == '/'); *p; p++) {
273                 if (*p != '/')
274                         continue;
275                 *p = '\0';
276                 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
277                         return -1;
278                 *p = '/';
279         }
280         if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
281                 return -1;
282         return 0;
283 }
284
285 void
286 printtimez(FILE *fp, const git_time *intime)
287 {
288         struct tm *intm;
289         time_t t;
290         char out[32];
291
292         t = (time_t)intime->time;
293         if (!(intm = gmtime(&t)))
294                 return;
295         strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
296         fputs(out, fp);
297 }
298
299 void
300 printtime(FILE *fp, const git_time *intime)
301 {
302         struct tm *intm;
303         time_t t;
304         char out[32];
305
306         t = (time_t)intime->time + (intime->offset * 60);
307         if (!(intm = gmtime(&t)))
308                 return;
309         strftime(out, sizeof(out), "%a, %e %b %Y %H:%M:%S", intm);
310         if (intime->offset < 0)
311                 fprintf(fp, "%s -%02d%02d", out,
312                             -(intime->offset) / 60, -(intime->offset) % 60);
313         else
314                 fprintf(fp, "%s +%02d%02d", out,
315                             intime->offset / 60, intime->offset % 60);
316 }
317
318 void
319 printtimeshort(FILE *fp, const git_time *intime)
320 {
321         struct tm *intm;
322         time_t t;
323         char out[32];
324
325         t = (time_t)intime->time;
326         if (!(intm = gmtime(&t)))
327                 return;
328         strftime(out, sizeof(out), "%Y-%m-%d %H:%M", intm);
329         fputs(out, fp);
330 }
331
332 void
333 writeheader(FILE *fp, const char *title)
334 {
335         fputs("<!DOCTYPE html>\n"
336                 "<html>\n<head>\n"
337                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
338                 "<title>", fp);
339         xmlencode(fp, title, strlen(title));
340         if (title[0] && strippedname[0])
341                 fputs(" - ", fp);
342         xmlencode(fp, strippedname, strlen(strippedname));
343         if (description[0])
344                 fputs(" - ", fp);
345         xmlencode(fp, description, strlen(description));
346         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
347         fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
348                 name, relpath);
349         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
350         fputs("</head>\n<body>\n<table><tr><td>", fp);
351         fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
352                 relpath, relpath);
353         fputs("</td><td><h1>", fp);
354         xmlencode(fp, strippedname, strlen(strippedname));
355         fputs("</h1><span class=\"desc\">", fp);
356         xmlencode(fp, description, strlen(description));
357         fputs("</span></td></tr>", fp);
358         if (cloneurl[0]) {
359                 fputs("<tr class=\"url\"><td></td><td>git clone <a href=\"", fp);
360                 xmlencode(fp, cloneurl, strlen(cloneurl));
361                 fputs("\">", fp);
362                 xmlencode(fp, cloneurl, strlen(cloneurl));
363                 fputs("</a></td></tr>", fp);
364         }
365         fputs("<tr><td></td><td>\n", fp);
366         fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
367         fprintf(fp, "<a href=\"%sfiles.html\">Files</a> | ", relpath);
368         fprintf(fp, "<a href=\"%srefs.html\">Refs</a>", relpath);
369         if (hassubmodules)
370                 fprintf(fp, " | <a href=\"%sfile/.gitmodules.html\">Submodules</a>", relpath);
371         if (hasreadme)
372                 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
373         if (haslicense)
374                 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
375         fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
376 }
377
378 void
379 writefooter(FILE *fp)
380 {
381         fputs("</div>\n</body>\n</html>\n", fp);
382 }
383
384 int
385 writeblobhtml(FILE *fp, const git_blob *blob)
386 {
387         size_t n = 0, i, prev;
388         const char *nfmt = "<a href=\"#l%d\" class=\"line\" id=\"l%d\">%7d</a> ";
389         const char *s = git_blob_rawcontent(blob);
390         git_off_t len = git_blob_rawsize(blob);
391
392         fputs("<pre id=\"blob\">\n", fp);
393
394         if (len > 0) {
395                 for (i = 0, prev = 0; i < (size_t)len; i++) {
396                         if (s[i] != '\n')
397                                 continue;
398                         n++;
399                         fprintf(fp, nfmt, n, n, n);
400                         xmlencode(fp, &s[prev], i - prev + 1);
401                         prev = i + 1;
402                 }
403                 /* trailing data */
404                 if ((len - prev) > 0) {
405                         n++;
406                         fprintf(fp, nfmt, n, n, n);
407                         xmlencode(fp, &s[prev], len - prev);
408                 }
409         }
410
411         fputs("</pre>\n", fp);
412
413         return n;
414 }
415
416 void
417 printcommit(FILE *fp, struct commitinfo *ci)
418 {
419         fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
420                 relpath, ci->oid, ci->oid);
421
422         if (ci->parentoid[0])
423                 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
424                         relpath, ci->parentoid, ci->parentoid);
425
426         if (ci->author) {
427                 fputs("<b>Author:</b> ", fp);
428                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
429                 fputs(" &lt;<a href=\"mailto:", fp);
430                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
431                 fputs("\">", fp);
432                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
433                 fputs("</a>&gt;\n<b>Date:</b>   ", fp);
434                 printtime(fp, &(ci->author->when));
435                 fputc('\n', fp);
436         }
437         if (ci->msg) {
438                 fputc('\n', fp);
439                 xmlencode(fp, ci->msg, strlen(ci->msg));
440                 fputc('\n', fp);
441         }
442 }
443
444 void
445 printshowfile(FILE *fp, struct commitinfo *ci)
446 {
447         const git_diff_delta *delta;
448         const git_diff_hunk *hunk;
449         const git_diff_line *line;
450         git_patch *patch;
451         size_t nhunks, nhunklines, changed, add, del, total, i, j, k;
452         char linestr[80];
453
454         printcommit(fp, ci);
455
456         if (!ci->deltas)
457                 return;
458
459         if (ci->filecount > 1000   ||
460             ci->ndeltas   > 1000   ||
461             ci->addcount  > 100000 ||
462             ci->delcount  > 100000) {
463                 fputs("Diff is too large, output suppressed.\n", fp);
464                 return;
465         }
466
467         /* diff stat */
468         fputs("<b>Diffstat:</b>\n<table>", fp);
469         for (i = 0; i < ci->ndeltas; i++) {
470                 delta = git_patch_get_delta(ci->deltas[i]->patch);
471
472                 fprintf(fp, "<tr><td><a href=\"#h%zu\">", i);
473                 xmlencode(fp, delta->old_file.path, strlen(delta->old_file.path));
474                 if (strcmp(delta->old_file.path, delta->new_file.path)) {
475                         fputs(" -&gt; ", fp);
476                         xmlencode(fp, delta->new_file.path, strlen(delta->new_file.path));
477                 }
478
479                 add = ci->deltas[i]->addcount;
480                 del = ci->deltas[i]->delcount;
481                 changed = add + del;
482                 total = sizeof(linestr) - 2;
483                 if (changed > total) {
484                         if (add)
485                                 add = ((float)total / changed * add) + 1;
486                         if (del)
487                                 del = ((float)total / changed * del) + 1;
488                 }
489                 memset(&linestr, '+', add);
490                 memset(&linestr[add], '-', del);
491
492                 fprintf(fp, "</a></td><td> | </td><td class=\"num\">%zu</td><td><span class=\"i\">",
493                         ci->deltas[i]->addcount + ci->deltas[i]->delcount);
494                 fwrite(&linestr, 1, add, fp);
495                 fputs("</span><span class=\"d\">", fp);
496                 fwrite(&linestr[add], 1, del, fp);
497                 fputs("</span></td></tr>\n", fp);
498         }
499         fprintf(fp, "</table></pre><pre>%zu file%s changed, %zu insertion%s(+), %zu deletion%s(-)\n",
500                 ci->filecount, ci->filecount == 1 ? "" : "s",
501                 ci->addcount,  ci->addcount  == 1 ? "" : "s",
502                 ci->delcount,  ci->delcount  == 1 ? "" : "s");
503
504         fputs("<hr/>", fp);
505
506         for (i = 0; i < ci->ndeltas; i++) {
507                 patch = ci->deltas[i]->patch;
508                 delta = git_patch_get_delta(patch);
509                 fprintf(fp, "<b>diff --git a/<a id=\"h%zu\" href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
510                         i, relpath, delta->old_file.path, delta->old_file.path,
511                         relpath, delta->new_file.path, delta->new_file.path);
512
513                 /* check binary data */
514                 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
515                         fputs("Binary files differ.\n", fp);
516                         continue;
517                 }
518
519                 nhunks = git_patch_num_hunks(patch);
520                 for (j = 0; j < nhunks; j++) {
521                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
522                                 break;
523
524                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"h\">", i, j, i, j);
525                         xmlencode(fp, hunk->header, hunk->header_len);
526                         fputs("</a>", fp);
527
528                         for (k = 0; ; k++) {
529                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
530                                         break;
531                                 if (line->old_lineno == -1)
532                                         fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"i\">+",
533                                                 i, j, k, i, j, k);
534                                 else if (line->new_lineno == -1)
535                                         fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-",
536                                                 i, j, k, i, j, k);
537                                 else
538                                         fputc(' ', fp);
539                                 xmlencode(fp, line->content, line->content_len);
540                                 if (line->old_lineno == -1 || line->new_lineno == -1)
541                                         fputs("</a>", fp);
542                         }
543                 }
544         }
545 }
546
547 void
548 writelogline(FILE *fp, struct commitinfo *ci)
549 {
550         fputs("<tr><td>", fp);
551         if (ci->author)
552                 printtimeshort(fp, &(ci->author->when));
553         fputs("</td><td>", fp);
554         if (ci->summary) {
555                 fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
556                 xmlencode(fp, ci->summary, strlen(ci->summary));
557                 fputs("</a>", fp);
558         }
559         fputs("</td><td>", fp);
560         if (ci->author)
561                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
562         fputs("</td><td class=\"num\" align=\"right\">", fp);
563         fprintf(fp, "%zu", ci->filecount);
564         fputs("</td><td class=\"num\" align=\"right\">", fp);
565         fprintf(fp, "+%zu", ci->addcount);
566         fputs("</td><td class=\"num\" align=\"right\">", fp);
567         fprintf(fp, "-%zu", ci->delcount);
568         fputs("</td></tr>\n", fp);
569 }
570
571 int
572 writelog(FILE *fp, const git_oid *oid)
573 {
574         struct commitinfo *ci;
575         git_revwalk *w = NULL;
576         git_oid id;
577         char path[PATH_MAX], oidstr[GIT_OID_HEXSZ + 1];
578         FILE *fpfile;
579         int r;
580
581         git_revwalk_new(&w, repo);
582         git_revwalk_push(w, oid);
583         git_revwalk_sorting(w, GIT_SORT_TIME);
584         git_revwalk_simplify_first_parent(w);
585
586         while (!git_revwalk_next(&id, w)) {
587                 relpath = "";
588
589                 if (cachefile && !memcmp(&id, &lastoid, sizeof(id)))
590                         break;
591
592                 git_oid_tostr(oidstr, sizeof(oidstr), &id);
593                 r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
594                 if (r == -1 || (size_t)r >= sizeof(path))
595                         errx(1, "path truncated: 'commit/%s.html'", oidstr);
596                 r = access(path, F_OK);
597
598                 /* optimization: if there are no log lines to write and
599                    the commit file already exists: skip the diffstat */
600                 if (!nlogcommits && !r)
601                         continue;
602
603                 if (!(ci = commitinfo_getbyoid(&id)))
604                         break;
605                 /* diffstat: for stagit HTML required for the log.html line */
606                 if (commitinfo_getstats(ci) == -1)
607                         goto err;
608
609                 if (nlogcommits < 0) {
610                         writelogline(fp, ci);
611                 } else if (nlogcommits > 0) {
612                         writelogline(fp, ci);
613                         nlogcommits--;
614                         if (!nlogcommits && ci->parentoid[0])
615                                 fputs("<tr><td></td><td colspan=\"5\">"
616                                       "More commits remaining [...]</td>"
617                                       "</tr>\n", fp);
618                 }
619
620                 if (cachefile)
621                         writelogline(wcachefp, ci);
622
623                 /* check if file exists if so skip it */
624                 if (r) {
625                         relpath = "../";
626                         fpfile = efopen(path, "w");
627                         writeheader(fpfile, ci->summary);
628                         fputs("<pre>", fpfile);
629                         printshowfile(fpfile, ci);
630                         fputs("</pre>\n", fpfile);
631                         writefooter(fpfile);
632                         fclose(fpfile);
633                 }
634 err:
635                 commitinfo_free(ci);
636         }
637         git_revwalk_free(w);
638
639         relpath = "";
640
641         return 0;
642 }
643
644 void
645 printcommitatom(FILE *fp, struct commitinfo *ci)
646 {
647         fputs("<entry>\n", fp);
648
649         fprintf(fp, "<id>%s</id>\n", ci->oid);
650         if (ci->author) {
651                 fputs("<published>", fp);
652                 printtimez(fp, &(ci->author->when));
653                 fputs("</published>\n", fp);
654         }
655         if (ci->committer) {
656                 fputs("<updated>", fp);
657                 printtimez(fp, &(ci->committer->when));
658                 fputs("</updated>\n", fp);
659         }
660         if (ci->summary) {
661                 fputs("<title type=\"text\">", fp);
662                 xmlencode(fp, ci->summary, strlen(ci->summary));
663                 fputs("</title>\n", fp);
664         }
665         fprintf(fp, "<link rel=\"alternate\" type=\"text/html\" href=\"commit/%s.html\" />",
666                 ci->oid);
667
668         if (ci->author) {
669                 fputs("<author><name>", fp);
670                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
671                 fputs("</name>\n<email>", fp);
672                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
673                 fputs("</email>\n</author>\n", fp);
674         }
675
676         fputs("<content type=\"text\">", fp);
677         fprintf(fp, "commit %s\n", ci->oid);
678         if (ci->parentoid[0])
679                 fprintf(fp, "parent %s\n", ci->parentoid);
680         if (ci->author) {
681                 fputs("Author: ", fp);
682                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
683                 fputs(" &lt;", fp);
684                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
685                 fputs("&gt;\nDate:   ", fp);
686                 printtime(fp, &(ci->author->when));
687                 fputc('\n', fp);
688         }
689         if (ci->msg) {
690                 fputc('\n', fp);
691                 xmlencode(fp, ci->msg, strlen(ci->msg));
692         }
693         fputs("\n</content>\n</entry>\n", fp);
694 }
695
696 int
697 writeatom(FILE *fp)
698 {
699         struct commitinfo *ci;
700         git_revwalk *w = NULL;
701         git_oid id;
702         size_t i, m = 100; /* last 'm' commits */
703
704         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
705               "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
706         xmlencode(fp, strippedname, strlen(strippedname));
707         fputs(", branch HEAD</title>\n<subtitle>", fp);
708         xmlencode(fp, description, strlen(description));
709         fputs("</subtitle>\n", fp);
710
711         git_revwalk_new(&w, repo);
712         git_revwalk_push_head(w);
713         git_revwalk_sorting(w, GIT_SORT_TIME);
714         git_revwalk_simplify_first_parent(w);
715
716         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
717                 if (!(ci = commitinfo_getbyoid(&id)))
718                         break;
719                 printcommitatom(fp, ci);
720                 commitinfo_free(ci);
721         }
722         git_revwalk_free(w);
723
724         fputs("</feed>\n", fp);
725
726         return 0;
727 }
728
729 int
730 writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
731 {
732         char tmp[PATH_MAX] = "", *d;
733         const char *p;
734         int lc = 0;
735         FILE *fp;
736
737         if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp))
738                 errx(1, "path truncated: '%s'", fpath);
739         if (!(d = dirname(tmp)))
740                 err(1, "dirname");
741         if (mkdirp(d))
742                 return -1;
743
744         for (p = fpath, tmp[0] = '\0'; *p; p++) {
745                 if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp))
746                         errx(1, "path truncated: '../%s'", tmp);
747         }
748         relpath = tmp;
749
750         fp = efopen(fpath, "w");
751         writeheader(fp, filename);
752         fputs("<p> ", fp);
753         xmlencode(fp, filename, strlen(filename));
754         fprintf(fp, " (%juB)", (uintmax_t)filesize);
755         fputs("</p><hr/>", fp);
756
757         if (git_blob_is_binary((git_blob *)obj)) {
758                 fputs("<p>Binary file.</p>\n", fp);
759         } else {
760                 lc = writeblobhtml(fp, (git_blob *)obj);
761                 if (ferror(fp))
762                         err(1, "fwrite");
763         }
764         writefooter(fp);
765         fclose(fp);
766
767         relpath = "";
768
769         return lc;
770 }
771
772 const char *
773 filemode(git_filemode_t m)
774 {
775         static char mode[11];
776
777         memset(mode, '-', sizeof(mode) - 1);
778         mode[10] = '\0';
779
780         if (S_ISREG(m))
781                 mode[0] = '-';
782         else if (S_ISBLK(m))
783                 mode[0] = 'b';
784         else if (S_ISCHR(m))
785                 mode[0] = 'c';
786         else if (S_ISDIR(m))
787                 mode[0] = 'd';
788         else if (S_ISFIFO(m))
789                 mode[0] = 'p';
790         else if (S_ISLNK(m))
791                 mode[0] = 'l';
792         else if (S_ISSOCK(m))
793                 mode[0] = 's';
794         else
795                 mode[0] = '?';
796
797         if (m & S_IRUSR) mode[1] = 'r';
798         if (m & S_IWUSR) mode[2] = 'w';
799         if (m & S_IXUSR) mode[3] = 'x';
800         if (m & S_IRGRP) mode[4] = 'r';
801         if (m & S_IWGRP) mode[5] = 'w';
802         if (m & S_IXGRP) mode[6] = 'x';
803         if (m & S_IROTH) mode[7] = 'r';
804         if (m & S_IWOTH) mode[8] = 'w';
805         if (m & S_IXOTH) mode[9] = 'x';
806
807         if (m & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
808         if (m & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
809         if (m & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
810
811         return mode;
812 }
813
814 int
815 writefilestree(FILE *fp, git_tree *tree, const char *path)
816 {
817         const git_tree_entry *entry = NULL;
818         git_submodule *module = NULL;
819         git_object *obj = NULL;
820         git_off_t filesize;
821         const char *entryname;
822         char filepath[PATH_MAX], entrypath[PATH_MAX];
823         size_t count, i;
824         int lc, r, ret;
825
826         count = git_tree_entrycount(tree);
827         for (i = 0; i < count; i++) {
828                 if (!(entry = git_tree_entry_byindex(tree, i)) ||
829                     !(entryname = git_tree_entry_name(entry)))
830                         return -1;
831                 joinpath(entrypath, sizeof(entrypath), path, entryname);
832
833                 r = snprintf(filepath, sizeof(filepath), "file/%s.html",
834                          entrypath);
835                 if (r == -1 || (size_t)r >= sizeof(filepath))
836                         errx(1, "path truncated: 'file/%s.html'", entrypath);
837
838                 if (!git_tree_entry_to_object(&obj, repo, entry)) {
839                         switch (git_object_type(obj)) {
840                         case GIT_OBJ_BLOB:
841                                 break;
842                         case GIT_OBJ_TREE:
843                                 /* NOTE: recurses */
844                                 ret = writefilestree(fp, (git_tree *)obj,
845                                                      entrypath);
846                                 git_object_free(obj);
847                                 if (ret)
848                                         return ret;
849                                 continue;
850                         default:
851                                 git_object_free(obj);
852                                 continue;
853                         }
854
855                         filesize = git_blob_rawsize((git_blob *)obj);
856                         lc = writeblob(obj, filepath, entryname, filesize);
857
858                         fputs("<tr><td>", fp);
859                         fputs(filemode(git_tree_entry_filemode(entry)), fp);
860                         fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
861                         xmlencode(fp, entrypath, strlen(entrypath));
862                         fputs("</a></td><td class=\"num\" align=\"right\">", fp);
863                         if (lc > 0)
864                                 fprintf(fp, "%dL", lc);
865                         else
866                                 fprintf(fp, "%juB", (uintmax_t)filesize);
867                         fputs("</td></tr>\n", fp);
868                         git_object_free(obj);
869                 } else if (!git_submodule_lookup(&module, repo, entryname)) {
870                         fprintf(fp, "<tr><td>m---------</td><td><a href=\"%sfile/.gitmodules.html\">",
871                                 relpath);
872                         xmlencode(fp, entrypath, strlen(entrypath));
873                         git_submodule_free(module);
874                         fputs("</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp);
875                 }
876         }
877
878         return 0;
879 }
880
881 int
882 writefiles(FILE *fp, const git_oid *id)
883 {
884         git_tree *tree = NULL;
885         git_commit *commit = NULL;
886         int ret = -1;
887
888         fputs("<table id=\"files\"><thead>\n<tr>"
889               "<td><b>Mode</b></td><td><b>Name</b></td>"
890               "<td class=\"num\" align=\"right\"><b>Size</b></td>"
891               "</tr>\n</thead><tbody>\n", fp);
892
893         if (!git_commit_lookup(&commit, repo, id) &&
894             !git_commit_tree(&tree, commit))
895                 ret = writefilestree(fp, tree, "");
896
897         fputs("</tbody></table>", fp);
898
899         git_commit_free(commit);
900         git_tree_free(tree);
901
902         return ret;
903 }
904
905 int
906 refs_cmp(const void *v1, const void *v2)
907 {
908         git_reference *r1 = (*(git_reference **)v1);
909         git_reference *r2 = (*(git_reference **)v2);
910         int r;
911
912         if ((r = git_reference_is_branch(r1) - git_reference_is_branch(r2)))
913                 return r;
914
915         return strcmp(git_reference_shorthand(r1),
916                       git_reference_shorthand(r2));
917 }
918
919 int
920 writerefs(FILE *fp)
921 {
922         struct commitinfo *ci;
923         const git_oid *id = NULL;
924         git_object *obj = NULL;
925         git_reference *dref = NULL, *r, *ref = NULL;
926         git_reference_iterator *it = NULL;
927         git_reference **refs = NULL;
928         size_t count, i, j, refcount;
929         const char *titles[] = { "Branches", "Tags" };
930         const char *ids[] = { "branches", "tags" };
931         const char *name;
932
933         if (git_reference_iterator_new(&it, repo))
934                 return -1;
935
936         for (refcount = 0; !git_reference_next(&ref, it); refcount++) {
937                 if (!(refs = reallocarray(refs, refcount + 1, sizeof(git_reference *))))
938                         err(1, "realloc");
939                 refs[refcount] = ref;
940         }
941         git_reference_iterator_free(it);
942
943         /* sort by type then shorthand name */
944         qsort(refs, refcount, sizeof(git_reference *), refs_cmp);
945
946         for (j = 0; j < 2; j++) {
947                 for (i = 0, count = 0; i < refcount; i++) {
948                         if (!(git_reference_is_branch(refs[i]) && j == 0) &&
949                             !(git_reference_is_tag(refs[i]) && j == 1))
950                                 continue;
951
952                         switch (git_reference_type(refs[i])) {
953                         case GIT_REF_SYMBOLIC:
954                                 if (git_reference_resolve(&dref, refs[i]))
955                                         goto err;
956                                 r = dref;
957                                 break;
958                         case GIT_REF_OID:
959                                 r = refs[i];
960                                 break;
961                         default:
962                                 continue;
963                         }
964                         if (!git_reference_target(r) ||
965                             git_reference_peel(&obj, r, GIT_OBJ_ANY))
966                                 goto err;
967                         if (!(id = git_object_id(obj)))
968                                 goto err;
969                         if (!(ci = commitinfo_getbyoid(id)))
970                                 break;
971
972                         /* print header if it has an entry (first). */
973                         if (++count == 1) {
974                                 fprintf(fp, "<h2>%s</h2><table id=\"%s\">"
975                                         "<thead>\n<tr><td><b>Name</b></td>"
976                                         "<td><b>Last commit date</b></td>"
977                                         "<td><b>Author</b></td>\n</tr>\n"
978                                         "</thead><tbody>\n",
979                                          titles[j], ids[j]);
980                         }
981
982                         relpath = "";
983                         name = git_reference_shorthand(r);
984
985                         fputs("<tr><td>", fp);
986                         xmlencode(fp, name, strlen(name));
987                         fputs("</td><td>", fp);
988                         if (ci->author)
989                                 printtimeshort(fp, &(ci->author->when));
990                         fputs("</td><td>", fp);
991                         if (ci->author)
992                                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
993                         fputs("</td></tr>\n", fp);
994
995                         relpath = "../";
996
997                         commitinfo_free(ci);
998                         git_object_free(obj);
999                         obj = NULL;
1000                         git_reference_free(dref);
1001                         dref = NULL;
1002                 }
1003                 /* table footer */
1004                 if (count)
1005                         fputs("</tbody></table><br/>", fp);
1006         }
1007
1008 err:
1009         git_object_free(obj);
1010         git_reference_free(dref);
1011
1012         for (i = 0; i < refcount; i++)
1013                 git_reference_free(refs[i]);
1014         free(refs);
1015
1016         return 0;
1017 }
1018
1019 void
1020 usage(char *argv0)
1021 {
1022         fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0);
1023         exit(1);
1024 }
1025
1026 int
1027 main(int argc, char *argv[])
1028 {
1029         git_object *obj = NULL;
1030         const git_oid *head = NULL;
1031         const git_error *e = NULL;
1032         mode_t mask;
1033         FILE *fp, *fpread;
1034         char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
1035         char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ];
1036         size_t n;
1037         int i, fd;
1038
1039         for (i = 1; i < argc; i++) {
1040                 if (argv[i][0] != '-') {
1041                         if (repodir)
1042                                 usage(argv[0]);
1043                         repodir = argv[i];
1044                 } else if (argv[i][1] == 'c') {
1045                         if (nlogcommits > 0 || i + 1 >= argc)
1046                                 usage(argv[0]);
1047                         cachefile = argv[++i];
1048                 } else if (argv[i][1] == 'l') {
1049                         if (cachefile || i + 1 >= argc)
1050                                 usage(argv[0]);
1051                         errno = 0;
1052                         nlogcommits = strtoll(argv[++i], &p, 10);
1053                         if (argv[i][0] == '\0' || *p != '\0' ||
1054                             nlogcommits <= 0 || errno)
1055                                 usage(argv[0]);
1056                 }
1057         }
1058         if (!repodir)
1059                 usage(argv[0]);
1060
1061         if (!realpath(repodir, repodirabs))
1062                 err(1, "realpath");
1063
1064         git_libgit2_init();
1065
1066 #ifdef __OpenBSD__
1067         if (cachefile) {
1068                 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
1069                         err(1, "pledge");
1070         } else {
1071                 if (pledge("stdio rpath wpath cpath", NULL) == -1)
1072                         err(1, "pledge");
1073         }
1074 #endif
1075
1076         if (git_repository_open_ext(&repo, repodir,
1077                 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) < 0) {
1078                 e = giterr_last();
1079                 fprintf(stderr, "%s: %s\n", argv[0], e->message);
1080                 return 1;
1081         }
1082
1083         /* find HEAD */
1084         if (!git_revparse_single(&obj, repo, "HEAD"))
1085                 head = git_object_id(obj);
1086         git_object_free(obj);
1087
1088         /* use directory name as name */
1089         if ((name = strrchr(repodirabs, '/')))
1090                 name++;
1091         else
1092                 name = "";
1093
1094         /* strip .git suffix */
1095         if (!(strippedname = strdup(name)))
1096                 err(1, "strdup");
1097         if ((p = strrchr(strippedname, '.')))
1098                 if (!strcmp(p, ".git"))
1099                         *p = '\0';
1100
1101         /* read description or .git/description */
1102         joinpath(path, sizeof(path), repodir, "description");
1103         if (!(fpread = fopen(path, "r"))) {
1104                 joinpath(path, sizeof(path), repodir, ".git/description");
1105                 fpread = fopen(path, "r");
1106         }
1107         if (fpread) {
1108                 if (!fgets(description, sizeof(description), fpread))
1109                         description[0] = '\0';
1110                 fclose(fpread);
1111         }
1112
1113         /* read url or .git/url */
1114         joinpath(path, sizeof(path), repodir, "url");
1115         if (!(fpread = fopen(path, "r"))) {
1116                 joinpath(path, sizeof(path), repodir, ".git/url");
1117                 fpread = fopen(path, "r");
1118         }
1119         if (fpread) {
1120                 if (!fgets(cloneurl, sizeof(cloneurl), fpread))
1121                         cloneurl[0] = '\0';
1122                 cloneurl[strcspn(cloneurl, "\n")] = '\0';
1123                 fclose(fpread);
1124         }
1125
1126         /* check LICENSE */
1127         haslicense = (!git_revparse_single(&obj, repo, "HEAD:LICENSE") &&
1128                 git_object_type(obj) == GIT_OBJ_BLOB);
1129         git_object_free(obj);
1130
1131         /* check README */
1132         hasreadme = (!git_revparse_single(&obj, repo, "HEAD:README") &&
1133                 git_object_type(obj) == GIT_OBJ_BLOB);
1134         git_object_free(obj);
1135
1136         hassubmodules = (!git_revparse_single(&obj, repo, "HEAD:.gitmodules") &&
1137                 git_object_type(obj) == GIT_OBJ_BLOB);
1138         git_object_free(obj);
1139
1140         /* log for HEAD */
1141         fp = efopen("log.html", "w");
1142         relpath = "";
1143         mkdir("commit", S_IRWXU | S_IRWXG | S_IRWXO);
1144         writeheader(fp, "Log");
1145         fputs("<table id=\"log\"><thead>\n<tr><td><b>Date</b></td>"
1146               "<td><b>Commit message</b></td>"
1147               "<td><b>Author</b></td><td class=\"num\" align=\"right\"><b>Files</b></td>"
1148               "<td class=\"num\" align=\"right\"><b>+</b></td>"
1149               "<td class=\"num\" align=\"right\"><b>-</b></td></tr>\n</thead><tbody>\n", fp);
1150
1151         if (cachefile && head) {
1152                 /* read from cache file (does not need to exist) */
1153                 if ((rcachefp = fopen(cachefile, "r"))) {
1154                         if (!fgets(lastoidstr, sizeof(lastoidstr), rcachefp))
1155                                 errx(1, "%s: no object id", cachefile);
1156                         if (git_oid_fromstr(&lastoid, lastoidstr))
1157                                 errx(1, "%s: invalid object id", cachefile);
1158                 }
1159
1160                 /* write log to (temporary) cache */
1161                 if ((fd = mkstemp(tmppath)) == -1)
1162                         err(1, "mkstemp");
1163                 if (!(wcachefp = fdopen(fd, "w")))
1164                         err(1, "fdopen: '%s'", tmppath);
1165                 /* write last commit id (HEAD) */
1166                 git_oid_tostr(buf, sizeof(buf), head);
1167                 fprintf(wcachefp, "%s\n", buf);
1168
1169                 writelog(fp, head);
1170
1171                 if (rcachefp) {
1172                         /* append previous log to log.html and the new cache */
1173                         while (!feof(rcachefp)) {
1174                                 n = fread(buf, 1, sizeof(buf), rcachefp);
1175                                 if (ferror(rcachefp))
1176                                         err(1, "fread");
1177                                 if (fwrite(buf, 1, n, fp) != n ||
1178                                     fwrite(buf, 1, n, wcachefp) != n)
1179                                         err(1, "fwrite");
1180                         }
1181                         fclose(rcachefp);
1182                 }
1183                 fclose(wcachefp);
1184         } else {
1185                 if (head)
1186                         writelog(fp, head);
1187         }
1188
1189         fputs("</tbody></table>", fp);
1190         writefooter(fp);
1191         fclose(fp);
1192
1193         /* files for HEAD */
1194         fp = efopen("files.html", "w");
1195         writeheader(fp, "Files");
1196         if (head)
1197                 writefiles(fp, head);
1198         writefooter(fp);
1199         fclose(fp);
1200
1201         /* summary page with branches and tags */
1202         fp = efopen("refs.html", "w");
1203         writeheader(fp, "Refs");
1204         writerefs(fp);
1205         writefooter(fp);
1206         fclose(fp);
1207
1208         /* Atom feed */
1209         fp = efopen("atom.xml", "w");
1210         writeatom(fp);
1211         fclose(fp);
1212
1213         /* rename new cache file on success */
1214         if (cachefile && head) {
1215                 if (rename(tmppath, cachefile))
1216                         err(1, "rename: '%s' to '%s'", tmppath, cachefile);
1217                 umask((mask = umask(0)));
1218                 if (chmod(cachefile,
1219                     (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mask))
1220                         err(1, "chmod: '%s'", cachefile);
1221         }
1222
1223         /* cleanup */
1224         git_repository_free(repo);
1225         git_libgit2_shutdown();
1226
1227         return 0;
1228 }