add README and LICENSE file-detection
M · TODO +1, -11@@ -3,7 +3,7 @@
2 - add raw link to latest files: raw/file...
3 - add summary page?
4 - add diffstat to diff page? + and - lines summary?
5-- escape < > ' " etc, maybe even use CDATA ?
6+- escape HTML: < > ' " etc, maybe even use CDATA ?
7 - shorter date format for logs.html page.
8 - speed up generating files.
9 - for files link to the commit but make the filename a link anchor.
M · urmoms.c
+57, -5 1@@ -1,16 +1,20 @@
2 #include <err.h>
3+#include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6+#include <string.h>
7
8 #include "git2.h"
9
10+static git_repository *repo;
11+
12 static const char *relpath = "";
13 static const char *name = "";
14 static const char *description = "";
15
16 static const char *repodir = ".";
17
18-static git_repository *repo;
19+static int hasreadme, haslicense;
20
21 FILE *
22 efopen(const char *name, const char *flags)
23@@ -102,8 +106,10 @@ writeheader(FILE *fp)
24 fprintf(fp, "<a href=\"%slog.html\">Log</a> |", relpath);
25 fprintf(fp, "<a href=\"%sfiles.html\">Files</a>| ", relpath);
26 fprintf(fp, "<a href=\"%sstats.html\">Stats</a> | ", relpath);
27- fprintf(fp, "<a href=\"%sreadme.html\">README</a> | ", relpath);
28- fprintf(fp, "<a href=\"%slicense.html\">LICENSE</a>", relpath);
29+ if (hasreadme)
30+ fprintf(fp, "<a href=\"%sreadme.html\">README</a> | ", relpath);
31+ if (haslicense)
32+ fprintf(fp, "<a href=\"%slicense.html\">LICENSE</a>", relpath);
33 fprintf(fp, "</center><hr/><pre>");
34
35 return 0;
36@@ -181,12 +187,27 @@ writebranches(FILE *fp)
37 }
38 #endif
39
40+void
41+concat(FILE *fp1, FILE *fp2)
42+{
43+ char buf[BUFSIZ];
44+ size_t n;
45+
46+ while ((n = fread(buf, 1, sizeof(buf), fp1))) {
47+ fwrite(buf, 1, n, fp2);
48+
49+ if (feof(fp1) || ferror(fp1) || ferror(fp2))
50+ break;
51+ }
52+}
53+
54 int
55 main(int argc, char *argv[])
56 {
57- int status;
58 const git_error *e = NULL;
59- FILE *fp;
60+ FILE *fp, *fpread;
61+ char path[PATH_MAX];
62+ int status;
63
64 if (argc != 2) {
65 fprintf(stderr, "%s <repodir>\n", argv[0]);
66@@ -202,6 +223,37 @@ main(int argc, char *argv[])
67 exit(status);
68 }
69
70+ snprintf(path, sizeof(path), "%s%s%s",
71+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "LICENSE");
72+ if ((fpread = fopen(path, "r+b"))) {
73+ fp = efopen("license.html", "w+b");
74+ writeheader(fp);
75+ concat(fpread, fp);
76+ if (ferror(fpread) || ferror(fp))
77+ err(1, "concat");
78+ writefooter(fp);
79+
80+ fclose(fp);
81+ fclose(fpread);
82+
83+ haslicense = 1;
84+ }
85+
86+ snprintf(path, sizeof(path), "%s%s%s",
87+ repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "README");
88+ if ((fpread = fopen(path, "r+b"))) {
89+ fp = efopen("readme.html", "w+b");
90+ writeheader(fp);
91+ concat(fpread, fp);
92+ if (ferror(fpread) || ferror(fp))
93+ err(1, "concat");
94+ writefooter(fp);
95+ fclose(fp);
96+ fclose(fpread);
97+
98+ hasreadme = 1;
99+ }
100+
101 fp = efopen("logs.html", "w+b");
102 writeheader(fp);
103 writelog(fp);