]> git.armaanb.net Git - stagit.git/blob - urmoms.c
64a7737351b7a67481dbe66da701d5f85302575c
[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
11 #include "git2.h"
12
13 static git_repository *repo;
14
15 static const char *relpath = "";
16 static const char *repodir;
17
18 static char name[255];
19 static char description[255];
20 static int hasreadme, haslicense;
21
22 int
23 writeheader(FILE *fp)
24 {
25         fputs("<!DOCTYPE HTML>"
26                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
27                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
28                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n", fp);
29         fprintf(fp, "<title>%s%s%s</title>\n", name, description[0] ? " - " : "", description);
30         fprintf(fp, "<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
31         fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
32                 name, relpath);
33         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
34         fputs("</head>\n<body>\n", fp);
35         fprintf(fp, "<h1><img src=\"%slogo.png\" alt=\"\" /> %s <span class=\"desc\">%s</span></h1>\n",
36                 relpath, name, description);
37         fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
38         fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
39         /*fprintf(fp, "| <a href=\"%sstats.html\">Stats</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<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         size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
215         char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
216         FILE *fp;
217         int error;
218
219         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
220         snprintf(path, sizeof(path), "commit/%s.html", buf);
221         fp = efopen(path, "w+b");
222
223         memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
224
225         writeheader(fp);
226         printcommit(fp, commit);
227
228         if ((error = git_commit_tree(&commit_tree, commit)))
229                 goto err;
230         if (!(error = git_commit_parent(&parent, commit, 0))) {
231                 if ((error = git_commit_tree(&parent_tree, parent)))
232                         goto err; /* TODO: handle error */
233         } else {
234                 parent = NULL;
235                 parent_tree = NULL;
236         }
237         if ((error = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, NULL)))
238                 goto err;
239
240         /* diff stat */
241         if (!git_diff_get_stats(&diffstats, diff)) {
242                 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
243                     GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
244                         fprintf(fp, "<b>Diffstat:</b>\n");
245                         fputs(diffstatsbuf.ptr, fp);
246                 }
247                 git_diff_stats_free(diffstats);
248         }
249         fputs("<hr/>", fp);
250
251         ndeltas = git_diff_num_deltas(diff);
252         for (i = 0; i < ndeltas; i++) {
253                 if (git_patch_from_diff(&patch, diff, i)) {
254                         git_patch_free(patch);
255                         break; /* TODO: handle error */
256                 }
257
258                 delta = git_patch_get_delta(patch);
259                 fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a></b>\n",
260                         relpath, delta->old_file.path, delta->old_file.path,
261                         relpath, delta->new_file.path, delta->new_file.path);
262
263                 /* TODO: "new file mode <mode>". */
264                 /* TODO: add indexfrom...indexto + flags */
265
266 #if 0
267                 fputs("<b>--- ", fp);
268                 if (delta->status & GIT_DELTA_ADDED)
269                         fputs("/dev/null", fp);
270                 else
271                         fprintf(fp, "a/<a href=\"%sfile/%s\">%s</a>",
272                                 relpath, delta->old_file.path, delta->old_file.path);
273
274                 fputs("\n+++ ", fp);
275                 if (delta->status & GIT_DELTA_DELETED)
276                         fputs("/dev/null", fp);
277                 else
278                         fprintf(fp, "b/<a href=\"%sfile/%s\">%s</a>",
279                                 relpath, delta->new_file.path, delta->new_file.path);
280                 fputs("</b>\n", fp);
281 #endif
282
283                 /* check binary data */
284                 if (delta->flags & GIT_DIFF_FLAG_BINARY) {
285                         fputs("Binary files differ\n", fp);
286                         git_patch_free(patch);
287                         continue;
288                 }
289
290                 nhunks = git_patch_num_hunks(patch);
291                 for (j = 0; j < nhunks; j++) {
292                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
293                                 break; /* TODO: handle error ? */
294
295                         fprintf(fp, "<span class=\"h\">%s</span>\n", hunk->header);
296
297                         for (k = 0; ; k++) {
298                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
299                                         break;
300                                 if (line->old_lineno == -1)
301                                         fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
302                                                 j, k, j, k);
303                                 else if (line->new_lineno == -1)
304                                         fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
305                                                 j, k, j, k);
306                                 else
307                                         fputc(' ', fp);
308                                 xmlencode(fp, line->content, line->content_len);
309                                 if (line->old_lineno == -1 || line->new_lineno == -1)
310                                         fputs("</a></span>", fp);
311                         }
312                 }
313                 git_patch_free(patch);
314         }
315         git_diff_free(diff);
316
317         writefooter(fp);
318         fclose(fp);
319         return;
320
321 err:
322         git_buf_free(&diffstatsbuf);
323         fclose(fp);
324 }
325
326 int
327 writelog(FILE *fp)
328 {
329         git_revwalk *w = NULL;
330         git_oid id;
331         git_commit *commit = NULL;
332         const git_signature *author;
333         git_diff_stats *stats;
334         git_tree *commit_tree = NULL, *parent_tree = NULL;
335         git_commit *parent = NULL;
336         git_diff *diff = NULL;
337         size_t i, nfiles, ndel, nadd;
338         const char *summary;
339         char buf[GIT_OID_HEXSZ + 1];
340         int error, ret = 0;
341
342         mkdir("commit", 0755);
343
344         git_revwalk_new(&w, repo);
345         git_revwalk_push_head(w);
346
347         /* TODO: also make "expanded" log ? (with message body) */
348         i = 0; /* DEBUG: to limit commits */
349         fputs("<table><thead>\n<tr><td>Commit message</td><td>Author</td><td align=\"right\">Age</td>"
350               "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr>\n</thead><tbody>\n", fp);
351         while (!git_revwalk_next(&id, w)) {
352                 /* DEBUG */
353 /*              if (i++ > 100)
354                         break;*/
355
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; /* TODO: handle error */
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                         continue; /* TODO: handle error */
374                 if (git_diff_get_stats(&stats, diff))
375                         continue; /* TODO: handle error */
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                 git_diff_free(diff);
413                 git_commit_free(commit);
414         }
415         fprintf(fp, "</tbody></table>");
416 err:
417         git_revwalk_free(w);
418         relpath = "";
419
420         return ret;
421 }
422
423 void
424 printcommitatom(FILE *fp, git_commit *commit)
425 {
426         const git_signature *sig;
427         char buf[GIT_OID_HEXSZ + 1];
428         int i, count;
429         const char *msg, *summary;
430
431         fputs("<entry>\n", fp);
432
433         /* TODO: show tag when commit has it */
434         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
435         fprintf(fp, "<id>%s</id>\n", buf);
436
437         sig = git_commit_author(commit);
438
439         if (sig) {
440                 fputs("<updated>", fp);
441                 printtimez(fp, &sig->when);
442                 fputs("</updated>\n", fp);
443         }
444
445         if ((summary = git_commit_summary(commit))) {
446                 fputs("<title type=\"text\">", fp);
447                 xmlencode(fp, summary, strlen(summary));
448                 fputs("</title>\n", fp);
449         }
450
451         fputs("<content type=\"text\">", fp);
452         fprintf(fp, "commit %s\n", buf);
453         if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)) && buf[0])
454                 fprintf(fp, "parent %s\n", buf);
455
456         if ((count = (int)git_commit_parentcount(commit)) > 1) {
457                 fprintf(fp, "Merge:");
458                 for (i = 0; i < count; i++) {
459                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
460                         fprintf(fp, " %s", buf);
461                 }
462                 fputc('\n', fp);
463         }
464
465         if (sig) {
466                 fprintf(fp, "Author: ");
467                 xmlencode(fp, sig->name, strlen(sig->name));
468                 fprintf(fp, " &lt;");
469                 xmlencode(fp, sig->email, strlen(sig->email));
470                 fprintf(fp, "&gt;\nDate:   ");
471                 printtime(fp, &sig->when);
472         }
473         fputc('\n', fp);
474
475         if ((msg = git_commit_message(commit)))
476                 xmlencode(fp, msg, strlen(msg));
477         fputs("\n</content>\n", fp);
478         if (sig) {
479                 fputs("<author><name>", fp);
480                 xmlencode(fp, sig->name, strlen(sig->name));
481                 fputs("</name>\n<email>", fp);
482                 xmlencode(fp, sig->email, strlen(sig->email));
483                 fputs("</email>\n</author>\n", fp);
484         }
485         fputs("</entry>\n", fp);
486 }
487
488 int
489 writeatom(FILE *fp)
490 {
491         git_revwalk *w = NULL;
492         git_oid id;
493         git_commit *c = NULL;
494         size_t i, m = 100; /* max */
495
496         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
497         fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
498         xmlencode(fp, name, strlen(name));
499         fputs(", branch master</title>\n<subtitle>", fp);
500
501         xmlencode(fp, description, strlen(description));
502         fputs("</subtitle>\n", fp);
503
504         git_revwalk_new(&w, repo);
505         git_revwalk_push_head(w);
506
507         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
508                 if (git_commit_lookup(&c, repo, &id))
509                         return 1; /* TODO: error */
510                 printcommitatom(fp, c);
511                 git_commit_free(c);
512         }
513         git_revwalk_free(w);
514
515         fputs("</feed>", fp);
516
517         return 0;
518 }
519
520 int
521 writefiles(FILE *fp)
522 {
523         git_index *index;
524         const git_index_entry *entry;
525         size_t count, i;
526
527         git_repository_index(&index, repo);
528
529         count = git_index_entrycount(index);
530         fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
531
532         for (i = 0; i < count; i++) {
533                 entry = git_index_get_byindex(index, i);
534                 fputs("<tr><td>", fp);
535                 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
536                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
537                 xmlencode(fp, entry->path, strlen(entry->path));
538                 fputs("\">", fp);
539                 xmlencode(fp, entry->path, strlen(entry->path));
540                 fputs("</a></td><td align=\"right\">", fp);
541                 fprintf(fp, "%" PRIu64, entry->file_size);
542                 fputs("</td></tr>\n", fp);
543         }
544         fputs("</tbody></table>", fp);
545
546         return 0;
547 }
548
549 void
550 writeblobhtml(FILE *fp, const git_blob *blob)
551 {
552         xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
553 }
554
555 int
556 main(int argc, char *argv[])
557 {
558         git_object *obj = NULL;
559         const git_error *e = NULL;
560         FILE *fp, *fpread;
561         char path[PATH_MAX], *p;
562         int status;
563
564         if (argc != 2) {
565                 fprintf(stderr, "%s <repodir>\n", argv[0]);
566                 return 1;
567         }
568         repodir = argv[1];
569
570         git_libgit2_init();
571
572         if ((status = git_repository_open(&repo, repodir)) < 0) {
573                 e = giterr_last();
574                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
575                 return status;
576         }
577
578         /* use directory name as name */
579         p = xbasename(repodir);
580         snprintf(name, sizeof(name), "%s", p);
581         free(p);
582
583         /* read description or .git/description */
584         snprintf(path, sizeof(path), "%s%s%s",
585                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
586         if (!(fpread = fopen(path, "r+b"))) {
587                 snprintf(path, sizeof(path), "%s%s%s",
588                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
589                 fpread = fopen(path, "r+b");
590         }
591         if (fpread) {
592                 if (!fgets(description, sizeof(description), fpread))
593                         description[0] = '\0';
594                 fclose(fpread);
595         }
596
597         /* check LICENSE */
598         haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
599         /* check README */
600         hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
601
602         /* read LICENSE */
603         if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
604                 fp = efopen("license.html", "w+b");
605                 writeheader(fp);
606                 writeblobhtml(fp, (git_blob *)obj);
607                 if (ferror(fp))
608                         err(1, "fwrite");
609                 writefooter(fp);
610
611                 fclose(fp);
612         }
613
614         /* read README */
615         if (!git_revparse_single(&obj, repo, "HEAD:README")) {
616                 fp = efopen("readme.html", "w+b");
617                 writeheader(fp);
618                 writeblobhtml(fp, (git_blob *)obj);
619                 if (ferror(fp))
620                         err(1, "fwrite");
621                 writefooter(fp);
622                 fclose(fp);
623         }
624
625         fp = efopen("log.html", "w+b");
626         writeheader(fp);
627         writelog(fp);
628         writefooter(fp);
629         fclose(fp);
630
631         fp = efopen("files.html", "w+b");
632         writeheader(fp);
633         writefiles(fp);
634         writefooter(fp);
635         fclose(fp);
636
637         /* Atom feed */
638         fp = efopen("atom.xml", "w+b");
639         writeatom(fp);
640         fclose(fp);
641
642         /* cleanup */
643         git_repository_free(repo);
644         git_libgit2_shutdown();
645
646         return 0;
647 }