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