]> git.armaanb.net Git - stagit.git/blob - urmoms.c
remove TODO from code, see TODO file
[stagit.git] / urmoms.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 int hasreadme, haslicense;
48
49 void
50 commitinfo_free(struct commitinfo *ci)
51 {
52         if (!ci)
53                 return;
54
55         git_diff_stats_free(ci->stats);
56         git_diff_free(ci->diff);
57         git_tree_free(ci->commit_tree);
58         git_tree_free(ci->parent_tree);
59         git_commit_free(ci->commit);
60 }
61
62 struct commitinfo *
63 commitinfo_getbyoid(const git_oid *id)
64 {
65         struct commitinfo *ci;
66         git_diff_options opts;
67         int error;
68
69         if (!(ci = calloc(1, sizeof(struct commitinfo))))
70                 err(1, "calloc");
71
72         ci->id = id;
73         if (git_commit_lookup(&(ci->commit), repo, id))
74                 goto err;
75
76         git_oid_tostr(ci->oid, sizeof(ci->oid), git_commit_id(ci->commit));
77         git_oid_tostr(ci->parentoid, sizeof(ci->parentoid), git_commit_parent_id(ci->commit, 0));
78
79         ci->author = git_commit_author(ci->commit);
80         ci->summary = git_commit_summary(ci->commit);
81         ci->msg = git_commit_message(ci->commit);
82
83         if ((error = git_commit_tree(&(ci->commit_tree), ci->commit)))
84                 goto err;
85         if (!(error = git_commit_parent(&(ci->parent), ci->commit, 0))) {
86                 if ((error = git_commit_tree(&(ci->parent_tree), ci->parent)))
87                         goto err;
88         } else {
89                 ci->parent = NULL;
90                 ci->parent_tree = NULL;
91         }
92
93         git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION);
94         opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
95         if ((error = git_diff_tree_to_tree(&(ci->diff), repo, ci->parent_tree, ci->commit_tree, &opts)))
96                 goto err;
97         if (git_diff_get_stats(&(ci->stats), ci->diff))
98                 goto err;
99
100         ci->addcount = git_diff_stats_insertions(ci->stats);
101         ci->delcount = git_diff_stats_deletions(ci->stats);
102         ci->filecount = git_diff_stats_files_changed(ci->stats);
103
104         return ci;
105
106 err:
107         commitinfo_free(ci);
108         free(ci);
109
110         return NULL;
111 }
112
113 FILE *
114 efopen(const char *name, const char *flags)
115 {
116         FILE *fp;
117
118         if (!(fp = fopen(name, flags)))
119                 err(1, "fopen");
120
121         return fp;
122 }
123
124 /* Escape characters below as HTML 2.0 / XML 1.0. */
125 void
126 xmlencode(FILE *fp, const char *s, size_t len)
127 {
128         size_t i;
129
130         for (i = 0; *s && i < len; s++, i++) {
131                 switch(*s) {
132                 case '<':  fputs("&lt;",   fp); break;
133                 case '>':  fputs("&gt;",   fp); break;
134                 case '\'': fputs("&apos;", fp); break;
135                 case '&':  fputs("&amp;",  fp); break;
136                 case '"':  fputs("&quot;", fp); break;
137                 default:   fputc(*s, fp);
138                 }
139         }
140 }
141
142 /* Some implementations of basename(3) return a pointer to a static
143  * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
144  * This is a wrapper function that is compatible with both versions.
145  * The program will error out if basename(3) failed, this can only happen
146  * with the OpenBSD version. */
147 char *
148 xbasename(const char *path)
149 {
150         char *p, *b;
151
152         if (!(p = strdup(path)))
153                 err(1, "strdup");
154         if (!(b = basename(p)))
155                 err(1, "basename");
156         if (!(b = strdup(b)))
157                 err(1, "strdup");
158         free(p);
159
160         return b;
161 }
162
163 int
164 mkdirp(const char *path)
165 {
166         char tmp[PATH_MAX], *p;
167
168         strlcpy(tmp, path, sizeof(tmp));
169         for (p = tmp + (tmp[0] == '/'); *p; p++) {
170                 if (*p != '/')
171                         continue;
172                 *p = '\0';
173                 if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
174                         return -1;
175                 *p = '/';
176         }
177         if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
178                 return -1;
179         return 0;
180 }
181
182 void
183 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
184 {
185         struct tm *intm;
186         time_t t;
187         char out[32];
188
189         t = (time_t) intime->time + (intime->offset * 60);
190         intm = gmtime(&t);
191         strftime(out, sizeof(out), fmt, intm);
192         fputs(out, fp);
193 }
194
195 void
196 printtimez(FILE *fp, const git_time *intime)
197 {
198         printtimeformat(fp, intime, "%Y-%m-%dT%H:%M:%SZ");
199 }
200
201 void
202 printtime(FILE *fp, const git_time *intime)
203 {
204         printtimeformat(fp, intime, "%a %b %e %T %Y");
205 }
206
207 void
208 printtimeshort(FILE *fp, const git_time *intime)
209 {
210         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
211 }
212
213 int
214 writeheader(FILE *fp)
215 {
216         fputs("<!DOCTYPE HTML>"
217                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
218                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
219                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
220         xmlencode(fp, name, strlen(name));
221         if (description[0])
222                 fputs(" - ", fp);
223         xmlencode(fp, description, strlen(description));
224         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
225         fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
226                 name, relpath);
227         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
228         fputs("</head>\n<body>\n<table><tr><td>", fp);
229         fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
230                 relpath, relpath);
231         fputs("</td><td><h1>", fp);
232         xmlencode(fp, name, strlen(name));
233         fputs("</h1><span class=\"desc\">", fp);
234         xmlencode(fp, description, strlen(description));
235         fputs("</span></td></tr><tr><td></td><td>\n", fp);
236         fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
237         fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
238         if (hasreadme)
239                 fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
240         if (haslicense)
241                 fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
242         fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
243
244         return 0;
245 }
246
247 int
248 writefooter(FILE *fp)
249 {
250         return !fputs("</div></body>\n</html>", fp);
251 }
252
253 void
254 writeblobhtml(FILE *fp, const git_blob *blob)
255 {
256         off_t i = 0;
257         size_t n = 1;
258         char *nfmt = "<a href=\"#l%d\" id=\"l%d\">%d</a>\n";
259         const char *s = git_blob_rawcontent(blob);
260         git_off_t len = git_blob_rawsize(blob);
261
262         fputs("<table id=\"blob\"><tr><td class=\"num\"><pre>\n", fp);
263
264         if (len) {
265                 fprintf(fp, nfmt, n, n, n);
266                 while (i < len - 1) {
267                         if (s[i] == '\n') {
268                                 n++;
269                                 fprintf(fp, nfmt, n, n, n);
270                         }
271                         i++;
272                 }
273         }
274
275         fputs("</pre></td><td><pre>\n", fp);
276         xmlencode(fp, s, (size_t)len);
277         fputs("</pre></td></tr></table>\n", fp);
278 }
279
280 void
281 printcommit(FILE *fp, struct commitinfo *ci)
282 {
283         fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
284                 relpath, ci->oid, ci->oid);
285
286         if (ci->parentoid[0])
287                 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
288                         relpath, ci->parentoid, ci->parentoid);
289
290 #if 0
291         if ((count = (int)git_commit_parentcount(commit)) > 1) {
292                 fprintf(fp, "<b>Merge:</b>");
293                 for (i = 0; i < count; i++) {
294                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
295                         fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
296                                 relpath, buf, buf);
297                 }
298                 fputc('\n', fp);
299         }
300 #endif
301         if (ci->author) {
302                 fprintf(fp, "<b>Author:</b> ");
303                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
304                 fprintf(fp, " &lt;<a href=\"mailto:");
305                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
306                 fputs("\">", fp);
307                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
308                 fputs("</a>&gt;\n<b>Date:</b>   ", fp);
309                 printtime(fp, &(ci->author->when));
310                 fputc('\n', fp);
311         }
312         fputc('\n', fp);
313
314         if (ci->msg)
315                 xmlencode(fp, ci->msg, strlen(ci->msg));
316
317         fputc('\n', fp);
318 }
319
320 void
321 printshowfile(struct commitinfo *ci)
322 {
323         const git_diff_delta *delta;
324         const git_diff_hunk *hunk;
325         const git_diff_line *line;
326         git_patch *patch;
327         git_buf statsbuf;
328         size_t ndeltas, nhunks, nhunklines;
329         FILE *fp;
330         size_t i, j, k;
331         char path[PATH_MAX];
332
333         snprintf(path, sizeof(path), "commit/%s.html", ci->oid);
334         /* check if file exists if so skip it */
335         if (!access(path, F_OK))
336                 return;
337
338         fp = efopen(path, "w");
339         writeheader(fp);
340         fputs("<pre>\n", fp);
341         printcommit(fp, ci);
342
343         memset(&statsbuf, 0, sizeof(statsbuf));
344
345         /* diff stat */
346         if (ci->stats) {
347                 if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
348                     GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
349                         if (statsbuf.ptr && statsbuf.ptr[0]) {
350                                 fprintf(fp, "<b>Diffstat:</b>\n");
351                                 fputs(statsbuf.ptr, fp);
352                         }
353                 }
354         }
355
356         fputs("<hr/>", fp);
357
358         ndeltas = git_diff_num_deltas(ci->diff);
359         for (i = 0; i < ndeltas; i++) {
360                 if (git_patch_from_diff(&patch, ci->diff, i)) {
361                         git_patch_free(patch);
362                         break;
363                 }
364
365                 delta = git_patch_get_delta(patch);
366                 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
367                         relpath, delta->old_file.path, delta->old_file.path,
368                         relpath, delta->new_file.path, delta->new_file.path);
369
370                 /* check binary data */
371                 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
372                         fputs("Binary files differ\n", fp);
373                         git_patch_free(patch);
374                         continue;
375                 }
376
377                 nhunks = git_patch_num_hunks(patch);
378                 for (j = 0; j < nhunks; j++) {
379                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
380                                 break;
381
382                         fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
383
384                         for (k = 0; ; k++) {
385                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
386                                         break;
387                                 if (line->old_lineno == -1)
388                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"i\">+",
389                                                 j, k, j, k);
390                                 else if (line->new_lineno == -1)
391                                         fprintf(fp, "<a href=\"#h%zu-%zu\" id=\"h%zu-%zu\" class=\"d\">-",
392                                                 j, k, j, k);
393                                 else
394                                         fputc(' ', fp);
395                                 xmlencode(fp, line->content, line->content_len);
396                                 if (line->old_lineno == -1 || line->new_lineno == -1)
397                                         fputs("</a>", fp);
398                         }
399                 }
400                 git_patch_free(patch);
401         }
402         git_buf_free(&statsbuf);
403
404         fputs( "</pre>\n", fp);
405         writefooter(fp);
406         fclose(fp);
407         return;
408 }
409
410 void
411 writelog(FILE *fp)
412 {
413         struct commitinfo *ci;
414         git_revwalk *w = NULL;
415         git_oid id;
416         size_t len;
417
418         mkdir("commit", 0755);
419
420         git_revwalk_new(&w, repo);
421         git_revwalk_push_head(w);
422         git_revwalk_sorting(w, GIT_SORT_TIME);
423         git_revwalk_simplify_first_parent(w);
424
425         fputs("<table id=\"log\"><thead>\n<tr><td>Age</td><td>Commit message</td>"
426                   "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
427                   "<td class=\"num\">-</td></tr>\n</thead><tbody>\n", fp);
428         while (!git_revwalk_next(&id, w)) {
429                 relpath = "";
430
431                 if (!(ci = commitinfo_getbyoid(&id)))
432                         break;
433
434                 fputs("<tr><td>", fp);
435                 if (ci->author)
436                         printtimeshort(fp, &(ci->author->when));
437                 fputs("</td><td>", fp);
438                 if (ci->summary) {
439                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, ci->oid);
440                         if ((len = strlen(ci->summary)) > summarylen) {
441                                 xmlencode(fp, ci->summary, summarylen - 1);
442                                 fputs("…", fp);
443                         } else {
444                                 xmlencode(fp, ci->summary, len);
445                         }
446                         fputs("</a>", fp);
447                 }
448                 fputs("</td><td>", fp);
449                 if (ci->author)
450                         xmlencode(fp, ci->author->name, strlen(ci->author->name));
451                 fputs("</td><td class=\"num\">", fp);
452                 fprintf(fp, "%zu", ci->filecount);
453                 fputs("</td><td class=\"num\">", fp);
454                 fprintf(fp, "+%zu", ci->addcount);
455                 fputs("</td><td class=\"num\">", fp);
456                 fprintf(fp, "-%zu", ci->delcount);
457                 fputs("</td></tr>\n", fp);
458
459                 relpath = "../";
460                 printshowfile(ci);
461
462                 commitinfo_free(ci);
463         }
464         fprintf(fp, "</tbody></table>");
465
466         git_revwalk_free(w);
467         relpath = "";
468 }
469
470 void
471 printcommitatom(FILE *fp, struct commitinfo *ci)
472 {
473         fputs("<entry>\n", fp);
474
475         fprintf(fp, "<id>%s</id>\n", ci->oid);
476         if (ci->author) {
477                 fputs("<updated>", fp);
478                 printtimez(fp, &(ci->author->when));
479                 fputs("</updated>\n", fp);
480         }
481         if (ci->summary) {
482                 fputs("<title type=\"text\">", fp);
483                 xmlencode(fp, ci->summary, strlen(ci->summary));
484                 fputs("</title>\n", fp);
485         }
486
487         fputs("<content type=\"text\">", fp);
488         fprintf(fp, "commit %s\n", ci->oid);
489         if (ci->parentoid[0])
490                 fprintf(fp, "parent %s\n", ci->parentoid);
491
492 #if 0
493         if ((count = (int)git_commit_parentcount(commit)) > 1) {
494                 fprintf(fp, "Merge:");
495                 for (i = 0; i < count; i++) {
496                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
497                         fprintf(fp, " %s", buf);
498                 }
499                 fputc('\n', fp);
500         }
501 #endif
502
503         if (ci->author) {
504                 fprintf(fp, "Author: ");
505                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
506                 fprintf(fp, " &lt;");
507                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
508                 fprintf(fp, "&gt;\nDate:   ");
509                 printtime(fp, &(ci->author->when));
510         }
511         fputc('\n', fp);
512
513         if (ci->msg)
514                 xmlencode(fp, ci->msg, strlen(ci->msg));
515         fputs("\n</content>\n", fp);
516         if (ci->author) {
517                 fputs("<author><name>", fp);
518                 xmlencode(fp, ci->author->name, strlen(ci->author->name));
519                 fputs("</name>\n<email>", fp);
520                 xmlencode(fp, ci->author->email, strlen(ci->author->email));
521                 fputs("</email>\n</author>\n", fp);
522         }
523         fputs("</entry>\n", fp);
524 }
525
526 int
527 writeatom(FILE *fp)
528 {
529         struct commitinfo *ci;
530         git_revwalk *w = NULL;
531         git_oid id;
532         size_t i, m = 100; /* max */
533
534         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
535         fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
536         xmlencode(fp, name, strlen(name));
537         fputs(", branch master</title>\n<subtitle>", fp);
538
539         xmlencode(fp, description, strlen(description));
540         fputs("</subtitle>\n", fp);
541
542         git_revwalk_new(&w, repo);
543         git_revwalk_push_head(w);
544         git_revwalk_sorting(w, GIT_SORT_TIME);
545         git_revwalk_simplify_first_parent(w);
546
547         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
548                 if (!(ci = commitinfo_getbyoid(&id)))
549                         break;
550                 printcommitatom(fp, ci);
551                 commitinfo_free(ci);
552         }
553         git_revwalk_free(w);
554
555         fputs("</feed>", fp);
556
557         return 0;
558 }
559
560 int
561 writeblob(git_object *obj, const char *filename, git_off_t filesize)
562 {
563         char fpath[PATH_MAX];
564         char tmp[PATH_MAX] = "";
565         char *p;
566         FILE *fp;
567
568         snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
569         if (mkdirp(dirname(fpath)))
570                 return 1;
571
572         p = fpath;
573         while (*p) {
574                 if (*p == '/')
575                         strlcat(tmp, "../", sizeof(tmp));
576                 p++;
577         }
578         relpath = tmp;
579
580         fp = efopen(fpath, "w");
581         writeheader(fp);
582         fputs("<p> ", fp);
583         xmlencode(fp, filename, strlen(filename));
584         fprintf(fp, " (%" PRIu32 "b)", filesize);
585         fputs("</p><hr/>", fp);
586
587         if (git_blob_is_binary((git_blob *)obj)) {
588                 fprintf(fp, "<p>Binary file</p>\n");
589         } else {
590                 writeblobhtml(fp, (git_blob *)obj);
591                 if (ferror(fp))
592                         err(1, "fwrite");
593         }
594         writefooter(fp);
595         fclose(fp);
596
597         relpath = "";
598
599         return 0;
600 }
601
602 int
603 writefilestree(FILE *fp, git_tree *tree, const char *path)
604 {
605         const git_tree_entry *entry = NULL;
606         const char *filename;
607         char filepath[PATH_MAX];
608         git_object *obj = NULL;
609         git_off_t filesize;
610         size_t count, i;
611         int ret;
612
613         count = git_tree_entrycount(tree);
614         for (i = 0; i < count; i++) {
615                 if (!(entry = git_tree_entry_byindex(tree, i)))
616                         return -1;
617
618                 filename = git_tree_entry_name(entry);
619                 if (git_tree_entry_to_object(&obj, repo, entry))
620                         return -1;
621                 switch (git_object_type(obj)) {
622                 case GIT_OBJ_BLOB:
623                         break;
624                 case GIT_OBJ_TREE:
625                         ret = writefilestree(fp, (git_tree *)obj, filename);
626                         git_object_free(obj);
627                         if (ret)
628                                 return ret;
629                         continue;
630                 default:
631                         git_object_free(obj);
632                         continue;
633                 }
634                 if (path[0]) {
635                         snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
636                         filename = filepath;
637                 }
638
639                 filesize = git_blob_rawsize((git_blob *)obj);
640
641                 fputs("<tr><td>", fp);
642                 fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
643                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
644                 xmlencode(fp, filename, strlen(filename));
645                 fputs(".html\">", fp);
646                 xmlencode(fp, filename, strlen(filename));
647                 fputs("</a></td><td class=\"num\">", fp);
648                 fprintf(fp, "%" PRIu32, filesize);
649                 fputs("</td></tr>\n", fp);
650
651                 writeblob(obj, filename, filesize);
652         }
653
654         return 0;
655 }
656
657 int
658 writefiles(FILE *fp)
659 {
660         const git_oid *id;
661         git_tree *tree = NULL;
662         git_object *obj = NULL;
663         git_commit *commit = NULL;
664
665         fputs("<table id=\"files\"><thead>\n"
666               "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
667               "</thead><tbody>\n", fp);
668
669         if (git_revparse_single(&obj, repo, "HEAD"))
670                 return -1;
671         id = git_object_id(obj);
672         if (git_commit_lookup(&commit, repo, id))
673                 return -1;
674         if (git_commit_tree(&tree, commit)) {
675                 git_commit_free(commit);
676                 return -1;
677         }
678         git_commit_free(commit);
679
680         writefilestree(fp, tree, "");
681
682         git_commit_free(commit);
683         git_tree_free(tree);
684
685         fputs("</tbody></table>", fp);
686
687         return 0;
688 }
689
690 int
691 main(int argc, char *argv[])
692 {
693         git_object *obj = NULL;
694         const git_error *e = NULL;
695         FILE *fp, *fpread;
696         char path[PATH_MAX], *p;
697         int status;
698
699         if (argc != 2) {
700                 fprintf(stderr, "%s <repodir>\n", argv[0]);
701                 return 1;
702         }
703         repodir = argv[1];
704
705         git_libgit2_init();
706
707         if ((status = git_repository_open_ext(&repo, repodir,
708                 GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) < 0) {
709                 e = giterr_last();
710                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
711                 return status;
712         }
713
714         /* use directory name as name */
715         p = xbasename(repodir);
716         snprintf(name, sizeof(name), "%s", p);
717         free(p);
718
719         /* read description or .git/description */
720         snprintf(path, sizeof(path), "%s%s%s",
721                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
722         if (!(fpread = fopen(path, "r"))) {
723                 snprintf(path, sizeof(path), "%s%s%s",
724                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
725                 fpread = fopen(path, "r");
726         }
727         if (fpread) {
728                 if (!fgets(description, sizeof(description), fpread))
729                         description[0] = '\0';
730                 fclose(fpread);
731         }
732
733         /* check LICENSE */
734         haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
735         git_object_free(obj);
736         /* check README */
737         hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
738         git_object_free(obj);
739
740         fp = efopen("log.html", "w");
741         writeheader(fp);
742         writelog(fp);
743         writefooter(fp);
744         fclose(fp);
745
746         fp = efopen("files.html", "w");
747         writeheader(fp);
748         writefiles(fp);
749         writefooter(fp);
750         fclose(fp);
751
752         /* Atom feed */
753         fp = efopen("atom.xml", "w");
754         writeatom(fp);
755         fclose(fp);
756
757         /* cleanup */
758         git_repository_free(repo);
759         git_libgit2_shutdown();
760
761         return 0;
762 }