]> git.armaanb.net Git - stagit.git/blob - urmoms.c
improve HTML code
[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 *scan, *eol;
165
166         /* TODO: show tag when commit has it */
167         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
168         fprintf(fp, "commit <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)))
172                 fprintf(fp, "parent <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, "Merge:");
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, "Author: ");
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;\nDate:   ", fp);
192                 printtime(fp, &sig->when);
193                 fputc('\n', fp);
194         }
195         fputc('\n', fp);
196
197         for (scan = git_commit_message(commit); scan && *scan;) {
198                 for (eol = scan; *eol && *eol != '\n'; ++eol)   /* find eol */
199                         ;
200
201                 fprintf(fp, "    %.*s\n", (int) (eol - scan), scan);
202                 scan = *eol ? eol + 1 : NULL;
203         }
204         fputc('\n', fp);
205 }
206
207 void
208 printshowfile(git_commit *commit)
209 {
210         const git_diff_delta *delta = NULL;
211         const git_diff_hunk *hunk = NULL;
212         const git_diff_line *line = NULL;
213         git_commit *parent = NULL;
214         git_tree *commit_tree = NULL, *parent_tree = NULL;
215         git_patch *patch = NULL;
216         git_diff *diff = NULL;
217         git_diff_stats *diffstats = NULL;
218         git_buf diffstatsbuf;
219         size_t i, j, k, ndeltas, nhunks = 0, nhunklines = 0;
220         char buf[GIT_OID_HEXSZ + 1], path[PATH_MAX];
221         FILE *fp;
222         int error;
223
224         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
225         snprintf(path, sizeof(path), "commit/%s.html", buf);
226         fp = efopen(path, "w+b");
227
228         memset(&diffstatsbuf, 0, sizeof(diffstatsbuf));
229
230         writeheader(fp);
231         printcommit(fp, commit);
232
233         if ((error = git_commit_parent(&parent, commit, 0)))
234                 return;
235         if ((error = git_commit_tree(&commit_tree, commit)))
236                 goto err;
237         if ((error = git_commit_tree(&parent_tree, parent)))
238                 goto err;
239         if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
240                 goto err;
241
242         /* diff stat */
243         if (!git_diff_get_stats(&diffstats, diff)) {
244                 if (!git_diff_stats_to_buf(&diffstatsbuf, diffstats,
245                     GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
246                         fputs("<hr/>", fp);
247                         fprintf(fp, "Diffstat:\n");
248                         fputs(diffstatsbuf.ptr, fp);
249                 }
250                 git_diff_stats_free(diffstats);
251         }
252         fputs("<hr/>", fp);
253
254         ndeltas = git_diff_num_deltas(diff);
255         for (i = 0; i < ndeltas; i++) {
256                 if (git_patch_from_diff(&patch, diff, i)) {
257                         git_patch_free(patch);
258                         break; /* TODO: handle error */
259                 }
260
261                 delta = git_patch_get_delta(patch);
262                 fprintf(fp, "diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a>\n",
263                         relpath, delta->old_file.path, delta->old_file.path,
264                         relpath, delta->new_file.path, delta->new_file.path);
265
266 #if 0
267                 switch (delta->flags) {
268                 case GIT_DIFF_FLAG_BINARY:
269                         /* "Binary files /dev/null and b/favicon.png differ" or so */
270                         continue; /* TODO: binary data */
271                 case GIT_DIFF_FLAG_NOT_BINARY:   break;
272                 case GIT_DIFF_FLAG_VALID_ID:     break; /* TODO: check */
273                 case GIT_DIFF_FLAG_EXISTS:       break; /* TODO: check */
274                 }
275 #endif
276
277                 nhunks = git_patch_num_hunks(patch);
278                 for (j = 0; j < nhunks; j++) {
279                         if (git_patch_get_hunk(&hunk, &nhunklines, patch, j))
280                                 break; /* TODO: handle error ? */
281
282                         fprintf(fp, "%s\n", hunk->header);
283
284                         for (k = 0; ; k++) {
285                                 if (git_patch_get_line_in_hunk(&line, patch, j, k))
286                                         break;
287                                 if (line->old_lineno == -1)
288                                         fprintf(fp, "<span class=\"i\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">+",
289                                                 j, k, j, k);
290                                 else if (line->new_lineno == -1)
291                                         fprintf(fp, "<span class=\"d\"><a href=\"#h%zu-%zu\" id=\"h%zu-%zu\">-",
292                                                 j, k, j, k);
293                                 else
294                                         fputc(' ', fp);
295                                 xmlencode(fp, line->content, line->content_len);
296                                 if (line->old_lineno == -1 || line->new_lineno == -1)
297                                         fputs("</a></span>", fp);
298                         }
299                 }
300                 git_patch_free(patch);
301         }
302         git_diff_free(diff);
303
304         writefooter(fp);
305         fclose(fp);
306         return;
307
308 err:
309         git_buf_free(&diffstatsbuf);
310         fclose(fp);
311 }
312
313 int
314 writelog(FILE *fp)
315 {
316         git_revwalk *w = NULL;
317         git_oid id;
318         git_commit *commit = NULL;
319         const git_signature *author;
320         git_diff_stats *stats;
321         git_tree *commit_tree = NULL, *parent_tree = NULL;
322         git_commit *parent = NULL;
323         git_diff *diff = NULL;
324         size_t i, nfiles, ndel, nadd;
325         const char *summary;
326         char buf[GIT_OID_HEXSZ + 1];
327         int error;
328
329         mkdir("commit", 0755);
330
331         git_revwalk_new(&w, repo);
332         git_revwalk_push_head(w);
333
334         /* TODO: also make "expanded" log ? (with message body) */
335         i = 0;
336         fputs("<table><thead><tr><td>Summary</td><td>Author</td><td align=\"right\">Age</td>"
337               "<td align=\"right\">Files</td><td align=\"right\">+</td><td align=\"right\">-</td></tr></thead><tbody>", fp);
338         while (!git_revwalk_next(&id, w)) {
339                 if (git_commit_lookup(&commit, repo, &id))
340                         return 1; /* TODO: error */
341
342                 if ((error = git_commit_parent(&parent, commit, 0)))
343                         continue; /* TODO: handle error */
344                 if ((error = git_commit_tree(&commit_tree, commit)))
345                         continue; /* TODO: handle error */
346                 if ((error = git_commit_tree(&parent_tree, parent)))
347                         continue; /* TODO: handle error */
348                 if ((error = git_diff_tree_to_tree(&diff, repo, commit_tree, parent_tree, NULL)))
349                         continue; /* TODO: handle error */
350                 if (git_diff_get_stats(&stats, diff))
351                         continue; /* TODO: handle error */
352
353                 git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
354
355                 ndel = git_diff_stats_deletions(stats);
356                 nadd = git_diff_stats_insertions(stats);
357                 nfiles = git_diff_stats_files_changed(stats);
358
359                 /* TODO: show tag when commit has it */
360
361                 /* TODO: collect stats per author and make stats.html page */
362                 author = git_commit_author(commit);
363                 summary = git_commit_summary(commit);
364
365                 fputs("<tr><td>", fp);
366                 if (summary) {
367                         fprintf(fp, "<a href=\"%scommit/%s.html\">", relpath, buf);
368                         xmlencode(fp, summary, strlen(summary));
369                         fputs("</a>", fp);
370                 }
371                 fputs("</td><td>", fp);
372                 if (author)
373                         xmlencode(fp, author->name, strlen(author->name));
374                 fputs("</td><td align=\"right\">", fp);
375                 printtime(fp, &author->when);
376                 fputs("</td><td align=\"right\">", fp);
377                 fprintf(fp, "%zu", nfiles);
378                 fputs("</td><td align=\"right\">", fp);
379                 fprintf(fp, "+%zu", nadd);
380                 fputs("</td><td align=\"right\">", fp);
381                 fprintf(fp, "-%zu", ndel);
382                 fputs("</td></tr>", fp);
383
384                 printshowfile(commit);
385
386                 git_diff_free(diff);
387                 git_commit_free(commit);
388
389                 /* DEBUG */
390                 i++;
391                 if (i > 100)
392                         break;
393         }
394         fprintf(fp, "</tbody></table>");
395         git_revwalk_free(w);
396
397         return 0;
398 }
399
400 void
401 printcommitatom(FILE *fp, git_commit *commit)
402 {
403         const git_signature *sig;
404         char buf[GIT_OID_HEXSZ + 1];
405         int i, count;
406         const char *scan, *eol, *summary;
407
408         fputs("<entry>\n", fp);
409
410         /* TODO: show tag when commit has it */
411         git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
412         fprintf(fp, "<id>%s</id>\n", buf);
413
414         sig = git_commit_author(commit);
415
416         if (sig) {
417                 fputs("<updated>", fp);
418                 printtimez(fp, &sig->when);
419                 fputs("</updated>\n", fp);
420         }
421
422         if ((summary = git_commit_summary(commit))) {
423                 fputs("<title type=\"text\">", fp);
424                 xmlencode(fp, summary, strlen(summary));
425                 fputs("</title>\n", fp);
426         }
427
428         fputs("<content type=\"text\">", fp);
429         fprintf(fp, "commit %s\n", buf);
430         if (git_oid_tostr(buf, sizeof(buf), git_commit_parent_id(commit, 0)))
431                 fprintf(fp, "parent %s\n", buf);
432
433         if ((count = (int)git_commit_parentcount(commit)) > 1) {
434                 fprintf(fp, "Merge:");
435                 for (i = 0; i < count; ++i) {
436                         git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
437                         fprintf(fp, " %s", buf);
438                 }
439                 fputc('\n', fp);
440         }
441
442         if (sig) {
443                 fprintf(fp, "Author: ");
444                 xmlencode(fp, sig->name, strlen(sig->name));
445                 fprintf(fp, " &lt;");
446                 xmlencode(fp, sig->email, strlen(sig->email));
447                 fprintf(fp, "&gt;\nDate:   ");
448                 printtime(fp, &sig->when);
449         }
450         fputc('\n', fp);
451
452         for (scan = git_commit_message(commit); scan && *scan;) {
453                 for (eol = scan; *eol && *eol != '\n'; ++eol)   /* find eol */
454                         ;
455
456                 fprintf(fp, "    %.*s\n", (int) (eol - scan), scan);
457                 scan = *eol ? eol + 1 : NULL;
458         }
459         fputc('\n', fp);
460         fputs("</content>\n", fp);
461         if (sig) {
462                 fputs("<author><name>", fp);
463                 xmlencode(fp, sig->name, strlen(sig->name));
464                 fputs("</name>\n<email>", fp);
465                 xmlencode(fp, sig->email, strlen(sig->email));
466                 fputs("</email>\n</author>\n", fp);
467         }
468         fputs("</entry>\n", fp);
469 }
470
471 int
472 writeatom(FILE *fp)
473 {
474         git_revwalk *w = NULL;
475         git_oid id;
476         git_commit *c = NULL;
477         size_t i, m = 100; /* max */
478
479         fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp);
480         fputs("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n<title>", fp);
481         xmlencode(fp, name, strlen(name));
482         fputs(", branch master</title>\n<subtitle>", fp);
483
484         xmlencode(fp, description, strlen(description));
485         fputs("</subtitle>\n", fp);
486
487         git_revwalk_new(&w, repo);
488         git_revwalk_push_head(w);
489
490         for (i = 0; i < m && !git_revwalk_next(&id, w); i++) {
491                 if (git_commit_lookup(&c, repo, &id))
492                         return 1; /* TODO: error */
493                 printcommitatom(fp, c);
494                 git_commit_free(c);
495         }
496         git_revwalk_free(w);
497
498         fputs("</feed>", fp);
499
500         return 0;
501 }
502
503 int
504 writefiles(FILE *fp)
505 {
506         git_index *index;
507         const git_index_entry *entry;
508         size_t count, i;
509
510         git_repository_index(&index, repo);
511
512         count = git_index_entrycount(index);
513         fputs("<table><thead>\n<tr><td>Mode</td><td>Name</td><td align=\"right\">Size</td></tr>\n</thead><tbody>\n", fp);
514
515         for (i = 0; i < count; i++) {
516                 entry = git_index_get_byindex(index, i);
517                 fputs("<tr><td>", fp);
518                 fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
519                 fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
520                 xmlencode(fp, entry->path, strlen(entry->path));
521                 fputs("\">", fp);
522                 xmlencode(fp, entry->path, strlen(entry->path));
523                 fputs("</a></td><td align=\"right\">", fp);
524                 fprintf(fp, "%" PRIu64, entry->file_size);
525                 fputs("</td></tr>\n", fp);
526         }
527         fputs("</tbody></table>", fp);
528
529         return 0;
530 }
531
532 void
533 writeblobhtml(FILE *fp, const git_blob *blob)
534 {
535         xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
536 }
537
538 int
539 main(int argc, char *argv[])
540 {
541         git_object *obj = NULL;
542         const git_error *e = NULL;
543         FILE *fp, *fpread;
544         char path[PATH_MAX], *p;
545         int status;
546
547         if (argc != 2) {
548                 fprintf(stderr, "%s <repodir>\n", argv[0]);
549                 return 1;
550         }
551         repodir = argv[1];
552
553         git_libgit2_init();
554
555         if ((status = git_repository_open(&repo, repodir)) < 0) {
556                 e = giterr_last();
557                 fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
558                 return status;
559         }
560
561         /* use directory name as name */
562         p = xbasename(repodir);
563         snprintf(name, sizeof(name), "%s", p);
564         free(p);
565
566         /* read description or .git/description */
567         snprintf(path, sizeof(path), "%s%s%s",
568                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
569         if (!(fpread = fopen(path, "r+b"))) {
570                 snprintf(path, sizeof(path), "%s%s%s",
571                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
572                 fpread = fopen(path, "r+b");
573         }
574         if (fpread) {
575                 if (!fgets(description, sizeof(description), fpread))
576                         description[0] = '\0';
577                 fclose(fpread);
578         }
579
580         /* read LICENSE */
581         if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
582                 fp = efopen("license.html", "w+b");
583                 writeheader(fp);
584                 writeblobhtml(fp, (git_blob *)obj);
585                 if (ferror(fp))
586                         err(1, "fwrite");
587                 writefooter(fp);
588
589                 fclose(fp);
590
591                 haslicense = 1;
592         }
593
594         /* read README */
595         if (!git_revparse_single(&obj, repo, "HEAD:README")) {
596                 fp = efopen("readme.html", "w+b");
597                 writeheader(fp);
598                 writeblobhtml(fp, (git_blob *)obj);
599                 if (ferror(fp))
600                         err(1, "fwrite");
601                 writefooter(fp);
602                 fclose(fp);
603
604                 hasreadme = 1;
605         }
606
607         fp = efopen("log.html", "w+b");
608         writeheader(fp);
609         writelog(fp);
610         writefooter(fp);
611         fclose(fp);
612
613         fp = efopen("files.html", "w+b");
614         writeheader(fp);
615         writefiles(fp);
616         writefooter(fp);
617         fclose(fp);
618
619         /* Atom feed */
620         fp = efopen("atom.xml", "w+b");
621         writeatom(fp);
622         fclose(fp);
623
624         /* cleanup */
625         git_repository_free(repo);
626         git_libgit2_shutdown();
627
628         return 0;
629 }