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