]> git.armaanb.net Git - stagit.git/blob - urmoms.c
cleanup + some better error handling
[stagit.git] / urmoms.c
1 #include <sys/stat.h>
2
3 #include <err.h>
4 #include <inttypes.h>
5 #include <libgen.h>
6 #include <limits.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #include "git2.h"
13
14 static git_repository *repo;
15
16 static const char *relpath = "";
17 static const char *repodir;
18
19 static char name[255];
20 static char description[255];
21 static int hasreadme, haslicense;
22
23 int
24 writeheader(FILE *fp)
25 {
26         fputs("<!DOCTYPE HTML>"
27                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
28                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
29                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n", fp);
30         fprintf(fp, "<title>%s%s%s</title>\n", name, description[0] ? " - " : "", description);
31         fprintf(fp, "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
32         fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
33                 name, relpath);
34         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
35         fputs("</head>\n<body>\n<center>\n", fp);
36         fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /> %s <span class=\"desc\">%s</span></h1>\n",
37                 relpath, name, description);
38         fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
39         fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
40         if (hasreadme)
41                 fprintf(fp, " | <a href=\"%sreadme.html\">README</a>", relpath);
42         if (haslicense)
43                 fprintf(fp, " | <a href=\"%slicense.html\">LICENSE</a>", relpath);
44         fputs("\n</center>\n<hr/>\n<pre>", fp);
45
46         return 0;
47 }
48
49 int
50 writefooter(FILE *fp)
51 {
52         return !fputs("</pre>\n</body>\n</html>", fp);
53 }
54
55 FILE *
56 efopen(const char *name, const char *flags)
57 {
58         FILE *fp;
59
60         if (!(fp = fopen(name, flags)))
61                 err(1, "fopen");
62
63         return fp;
64 }
65
66 /* Escape characters below as HTML 2.0 / XML 1.0. */
67 void
68 xmlencode(FILE *fp, const char *s, size_t len)
69 {
70         size_t i;
71
72         for (i = 0; *s && i < len; s++, i++) {
73                 switch(*s) {
74                 case '<':  fputs("&lt;",   fp); break;
75                 case '>':  fputs("&gt;",   fp); break;
76                 case '\'': fputs("&apos;", fp); break;
77                 case '&':  fputs("&amp;",  fp); break;
78                 case '"':  fputs("&quot;", fp); break;
79                 default:   fputc(*s, fp);
80                 }
81         }
82 }
83
84 /* Some implementations of basename(3) return a pointer to a static
85  * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
86  * This is a wrapper function that is compatible with both versions.
87  * The program will error out if basename(3) failed, this can only happen
88  * with the OpenBSD version. */
89 char *
90 xbasename(const char *path)
91 {
92         char *p, *b;
93
94         if (!(p = strdup(path)))
95                 err(1, "strdup");
96         if (!(b = basename(p)))
97                 err(1, "basename");
98         if (!(b = strdup(b)))
99                 err(1, "strdup");
100         free(p);
101
102         return b;
103 }
104
105 void
106 printtimez(FILE *fp, const git_time *intime)
107 {
108         struct tm *intm;
109         time_t t;
110         int offset, hours, minutes;
111         char sign, out[32];
112
113         offset = intime->offset;
114         if (offset < 0) {
115                 sign = '-';
116                 offset = -offset;
117         } else {
118                 sign = '+';
119         }
120
121         hours = offset / 60;
122         minutes = offset % 60;
123
124         t = (time_t) intime->time + (intime->offset * 60);
125
126         intm = gmtime(&t);
127         strftime(out, sizeof(out), "%Y-%m-%dT%H:%M:%SZ", intm);
128         fputs(out, fp);
129 }
130
131 void
132 printtime(FILE *fp, const git_time *intime)
133 {
134         struct tm *intm;
135         time_t t;
136         int offset, hours, minutes;
137         char sign, out[32];
138
139         offset = intime->offset;
140         if (offset < 0) {
141                 sign = '-';
142                 offset = -offset;
143         } else {
144                 sign = '+';
145         }
146
147         hours = offset / 60;
148         minutes = offset % 60;
149
150         t = (time_t) intime->time + (intime->offset * 60);
151
152         intm = gmtime(&t);
153         strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
154
155         fprintf(fp, "%s %c%02d%02d", out, sign, hours, minutes);
156 }
157
158 void
159 printcommit(FILE *fp, git_commit *commit)
160 {
161         const git_signature *sig;
162         char buf[GIT_OID_HEXSZ + 1];
163         int i, count;
164         const char *msg;
165
166         /* TODO: show tag when commit has it */
167         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
168         fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
169                 relpath, buf, buf);
170
171         if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
172                 fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
173                         relpath, buf, buf);
174
175         if ((count = (int)git_commit_parentcount(commit)) > 1) {
176                 fprintf(fp, "<b>Merge:</b>");
177                 for (i = 0; i < count; i++) {
178                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
179                         fprintf(fp, " <a href=\"%scommit/%s.html\">%s</a>",
180                                 relpath, buf, buf);
181                 }
182                 fputc('\n', fp);
183         }
184         if ((sig = git_commit_author(commit)) != NULL) {
185                 fprintf(fp, "<b>Author:</b> ");
186                 xmlencode(fp, sig->name, strlen(sig->name));
187                 fprintf(fp, " &lt;<a href=\"mailto:");
188                 xmlencode(fp, sig->email, strlen(sig->email));
189                 fputs("\">", fp);
190                 xmlencode(fp, sig->email, strlen(sig->email));
191                 fputs("</a>&gt;\n<b>Date:</b>   ", fp);
192                 printtime(fp, &sig->when);
193                 fputc('\n', fp);
194         }
195         fputc('\n', fp);
196
197         if ((msg = git_commit_message(commit)))
198                 xmlencode(fp, msg, strlen(msg));
199         fputc('\n', fp);
200 }
201
202 void
203 printshowfile(git_commit *commit)
204 {
205         const git_diff_delta *delta = NULL;
206         const git_diff_hunk *hunk = NULL;
207         const git_diff_line *line = NULL;
208         git_commit *parent = NULL;
209         git_tree *commit_tree = NULL, *parent_tree = NULL;
210         git_patch *patch = NULL;
211         git_diff *diff = NULL;
212         git_diff_stats *diffstats = NULL;
213         git_buf diffstatsbuf;
214         FILE *fp;
215         size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
216         char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
217         int error;
218
219         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
220         if (!buf[0])
221                 return;
222         snprintf(path, sizeof(path), "commit/%s.html", buf);
223         /* check if file exists if so skip it */
224         if (!access(path, F_OK))
225                 return;
226
227         memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
228
229         fp = efopen(path, "w+b");
230         writeheader(fp);
231         printcommit(fp, commit);
232
233         if ((error = git_commit_tree(&commit_tree, commit)))
234                 goto err;
235         if (!(error = git_commit_parent(&parent, commit, 0))) {
236                 if ((error = git_commit_tree(&parent_tree, parent)))
237                         goto err; /* TODO: handle error */
238         } else {
239                 parent = NULL;
240                 parent_tree = NULL;
241         }
242         if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
243                 goto err;
244
245         /* diff stat */
246         if (!git_diff_get_stats(&diffstats, diff)) {
247                 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
248                     GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
249                         fprintf(fp, "<b>Diffstat:</b>\n");
250                         fputs(diffstatsbuf.ptr, fp);
251                 }
252                 git_diff_stats_free(diffstats);
253         }
254         fputs("<hr/>", fp);
255
256         ndeltas = git_diff_num_deltas(diff);
257         for (i = 0; i < ndeltas; i++) {
258                 if (git_patch_from_diff(&patch, diff, i)) {
259                         git_patch_free(patch);
260                         break; /* TODO: handle error */
261                 }
262
263                 delta = git_patch_get_delta(patch);
264                 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a></b>\n",
265                         relpath, delta->old_file.path, delta->old_file.path,
266                         relpath, delta->new_file.path, delta->new_file.path);
267
268                 /* TODO: "new file mode <mode>". */
269                 /* TODO: add indexfrom...indexto + flags */
270
271 #if 0
272                 fputs("<b>--- ", fp);
273                 if (delta->status & GIT_DELTA_ADDED)
274                         fputs("/dev/null", fp);
275                 else
276                         fprintf(fp, "a/<a href=\"%sfile/%s\">%s</a>",
277                                 relpath, delta->old_file.path, delta->old_file.path);
278
279                 fputs("\n+++ ", fp);
280                 if (delta->status & GIT_DELTA_DELETED)
281                         fputs("/dev/null", fp);
282                 else
283                         fprintf(fp, "b/<a href=\"%sfile/%s\">%s</a>",
284                                 relpath, delta->new_file.path, delta->new_file.path);
285                 fputs("</b>\n", fp);
286 #endif
287
288                 /* check binary data */
289                 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
290                         fputs("Binary files differ\n", fp);
291                         git_patch_free(patch);
292                         continue;
293                 }
294
295                 nhunks = git_patch_num_hunks(patch);
296                 for (j = 0; j < nhunks; j++) {
297                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
298                                 break; /* TODO: handle error ? */
299
300                         fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
301
302                         for (k = 0; ; k++) {
303                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
304                                         break;
305                                 if (line->old_lineno == -1)
306                                         fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
307                                                 j, k, j, k);
308                                 else if (line->new_lineno == -1)
309                                         fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
310                                                 j, k, j, k);
311                                 else
312                                         fputc(' ', fp);
313                                 xmlencode(fp, line->content, line->content_len);
314                                 if (line->old_lineno == -1 || line->new_lineno == -1)
315                                         fputs("</a></span>", fp);
316                         }
317                 }
318                 git_patch_free(patch);
319         }
320         git_diff_free(diff);
321
322         writefooter(fp);
323         fclose(fp);
324         return;
325
326 err:
327         git_buf_free(&diffstatsbuf);
328         fclose(fp);
329 }
330
331 int
332 writelog(FILE *fp)
333 {
334         git_revwalk *w = NULL;
335         git_oid id;
336         git_commit *commit = NULL;
337         const git_signature *author;
338         git_diff_stats *stats = NULL;
339         git_tree *commit_tree = NULL, *parent_tree = NULL;
340         git_commit *parent = NULL;
341         git_diff *diff = NULL;
342         size_t nfiles, ndel, nadd;
343         const char *summary;
344         char buf[GIT_OID_HEXSZ + 1];
345         int error, ret = 0;
346
347         mkdir("commit", 0755);
348
349         git_revwalk_new(&w, repo);
350         git_revwalk_push_head(w);
351
352         /* TODO: also make "expanded" log ? (with message body) */
353         fputs("<table><thead>\n<tr><td>Commit message</td><td>Author</td><td align=\"right\">Age</td>"
354               "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr>\n</thead><tbody>\n", fp);
355         while (!git_revwalk_next(&id, w)) {
356                 relpath = "";
357
358                 if (git_commit_lookup(&commit, repo, &id)) {
359                         ret = 1;
360                         goto err;
361                 }
362                 if ((error = git_commit_tree(&commit_tree, commit)))
363                         goto errdiff; /* TODO: handle error */
364                 if (!(error = git_commit_parent(&parent, commit, 0))) {
365                         if ((error = git_commit_tree(&parent_tree, parent)))
366                                 goto errdiff;
367                 } else {
368                         parent = NULL;
369                         parent_tree = NULL;
370                 }
371
372                 if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
373                         goto errdiff;
374                 if (git_diff_get_stats(&stats, diff))
375                         goto errdiff;
376
377                 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
378
379                 ndel = git_diff_stats_deletions(stats);
380                 nadd = git_diff_stats_insertions(stats);
381                 nfiles = git_diff_stats_files_changed(stats);
382
383                 /* TODO: show tag when commit has it */
384
385                 /* TODO: collect stats per author and make stats.html page */
386                 author = git_commit_author(commit);
387                 summary = git_commit_summary(commit);
388
389                 fputs("<tr><td>", fp);
390                 if (summary) {
391                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, buf);
392                         xmlencode(fp, summary, strlen(summary));
393                         fputs("</a>", fp);
394                 }
395                 fputs("</td><td>", fp);
396                 if (author)
397                         xmlencode(fp, author->name, strlen(author->name));
398                 fputs("</td><td align=\"right\">", fp);
399                 printtime(fp, &author->when);
400                 fputs("</td><td align=\"right\">", fp);
401                 fprintf(fp, "%zu", nfiles);
402                 fputs("</td><td align=\"right\">", fp);
403                 fprintf(fp, "+%zu", nadd);
404                 fputs("</td><td align=\"right\">", fp);
405                 fprintf(fp, "-%zu", ndel);
406                 fputs("</td></tr>\n", fp);
407
408                 relpath = "../";
409                 printshowfile(commit);
410
411 errdiff:
412                 /* TODO: print error ? */
413                 git_diff_stats_free(stats);
414                 git_diff_free(diff);
415                 git_commit_free(commit);
416         }
417         fprintf(fp, "</tbody></table>");
418 err:
419         git_revwalk_free(w);
420         relpath = "";
421
422         return ret;
423 }
424
425 void
426 printcommitatom(FILE *fp, git_commit *commit)
427 {
428         const git_signature *sig;
429         char buf[GIT_OID_HEXSZ + 1];
430         int i, count;
431         const char *msg, *summary;
432
433         fputs("<entry>\n", fp);
434
435         /* TODO: show tag when commit has it */
436         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
437         fprintf(fp, "<id>%s</id>\n", buf);
438
439         sig = git_commit_author(commit);
440
441         if (sig) {
442                 fputs("<updated>", fp);
443                 printtimez(fp, &sig->when);
444                 fputs("</updated>\n", fp);
445         }
446
447         if ((summary = git_commit_summary(commit))) {
448                 fputs("<title type=\"text\">", fp);
449                 xmlencode(fp, summary, strlen(summary));
450                 fputs("</title>\n", fp);
451         }
452
453         fputs("<content type=\"text\">", fp);
454         fprintf(fp, "commit %s\n", buf);
455         if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
456                 fprintf(fp, "parent %s\n", buf);
457
458         if ((count = (int)git_commit_parentcount(commit)) > 1) {
459                 fprintf(fp, "Merge:");
460                 for (i = 0; i < count; i++) {
461                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
462                         fprintf(fp, " %s", buf);
463                 }
464                 fputc('\n', fp);
465         }
466
467         if (sig) {
468                 fprintf(fp, "Author: ");
469                 xmlencode(fp, sig->name, strlen(sig->name));
470                 fprintf(fp, " &lt;");
471                 xmlencode(fp, sig->email, strlen(sig->email));
472                 fprintf(fp, "&gt;\nDate:   ");
473                 printtime(fp, &sig->when);
474         }
475         fputc('\n', fp);
476
477         if ((msg = git_commit_message(commit)))
478                 xmlencode(fp, msg, strlen(msg));
479         fputs("\n</content>\n", fp);
480         if (sig) {
481                 fputs("<author><name>", fp);
482                 xmlencode(fp, sig->name, strlen(sig->name));
483                 fputs("</name>\n<email>", fp);
484                 xmlencode(fp, sig->email, strlen(sig->email));
485                 fputs("</email>\n</author>\n", fp);
486         }
487         fputs("</entry>\n", fp);
488 }
489
490 int
491 writeatom(FILE *fp)
492 {
493         git_revwalk *w = NULL;
494         git_oid id;
495         git_commit *c = NULL;
496         size_t i, m = 100; /* max */
497
498         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
499         fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
500         xmlencode(fp, name, strlen(name));
501         fputs(", branch master</title>\n<subtitle>", fp);
502
503         xmlencode(fp, description, strlen(description));
504         fputs("</subtitle>\n", fp);
505
506         git_revwalk_new(&w, repo);
507         git_revwalk_push_head(w);
508
509         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
510                 if (git_commit_lookup(&c, repo, &id))
511                         return 1; /* TODO: error */
512                 printcommitatom(fp, c);
513                 git_commit_free(c);
514         }
515         git_revwalk_free(w);
516
517         fputs("</feed>", fp);
518
519         return 0;
520 }
521
522 int
523 writefiles(FILE *fp)
524 {
525         git_index *index;
526         const git_index_entry *entry;
527         size_t count, i;
528
529         git_repository_index(&index, repo);
530
531         count = git_index_entrycount(index);
532         fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
533
534         for (i = 0; i < count; i++) {
535                 entry = git_index_get_byindex(index, i);
536                 fputs("<tr><td>", fp);
537                 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
538                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
539                 xmlencode(fp, entry->path, strlen(entry->path));
540                 fputs("\">", fp);
541                 xmlencode(fp, entry->path, strlen(entry->path));
542                 fputs("</a></td><td align=\"right\">", fp);
543                 fprintf(fp, "%" PRIu64, entry->file_size);
544                 fputs("</td></tr>\n", fp);
545         }
546         fputs("</tbody></table>", fp);
547
548         return 0;
549 }
550
551 void
552 writeblobhtml(FILE *fp, const git_blob *blob)
553 {
554         xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
555 }
556
557 int
558 main(int argc, char *argv[])
559 {
560         git_object *obj = NULL;
561         const git_error *e = NULL;
562         FILE *fp, *fpread;
563         char path[PATH_MAX], *p;
564         int status;
565
566         if (argc != 2) {
567                 fprintf(stderr, "%s <repodir>\n", argv[0]);
568                 return 1;
569         }
570         repodir = argv[1];
571
572         git_libgit2_init();
573
574         if ((status = git_repository_open(&repo, repodir)) < 0) {
575                 e = giterr_last();
576                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
577                 return status;
578         }
579
580         /* use directory name as name */
581         p = xbasename(repodir);
582         snprintf(name, sizeof(name), "%s", p);
583         free(p);
584
585         /* read description or .git/description */
586         snprintf(path, sizeof(path), "%s%s%s",
587                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
588         if (!(fpread = fopen(path, "r+b"))) {
589                 snprintf(path, sizeof(path), "%s%s%s",
590                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
591                 fpread = fopen(path, "r+b");
592         }
593         if (fpread) {
594                 if (!fgets(description, sizeof(description), fpread))
595                         description[0] = '\0';
596                 fclose(fpread);
597         }
598
599         /* check LICENSE */
600         haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
601         /* check README */
602         hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
603
604         /* read LICENSE */
605         if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
606                 fp = efopen("license.html", "w+b");
607                 writeheader(fp);
608                 writeblobhtml(fp, (git_blob *)obj);
609                 if (ferror(fp))
610                         err(1, "fwrite");
611                 writefooter(fp);
612
613                 fclose(fp);
614         }
615
616         /* read README */
617         if (!git_revparse_single(&obj, repo, "HEAD:README")) {
618                 fp = efopen("readme.html", "w+b");
619                 writeheader(fp);
620                 writeblobhtml(fp, (git_blob *)obj);
621                 if (ferror(fp))
622                         err(1, "fwrite");
623                 writefooter(fp);
624                 fclose(fp);
625         }
626
627         fp = efopen("log.html", "w+b");
628         writeheader(fp);
629         writelog(fp);
630         writefooter(fp);
631         fclose(fp);
632
633         fp = efopen("files.html", "w+b");
634         writeheader(fp);
635         writefiles(fp);
636         writefooter(fp);
637         fclose(fp);
638
639         /* Atom feed */
640         fp = efopen("atom.xml", "w+b");
641         writeatom(fp);
642         fclose(fp);
643
644         /* cleanup */
645         git_repository_free(repo);
646         git_libgit2_shutdown();
647
648         return 0;
649 }