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