ctucx.git: stagit

ctucx' stagit fork

commit 7294b10b65ff8a91bd330108af98441ec4a05307
parent 415e3fdd55b2ecdf2f35680694362a4b35fd1a05
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 5 Dec 2015 20:43:29 +0100

add README and LICENSE file-detection
2 files changed, 58 insertions(+), 6 deletions(-)
M
TODO
|
2
+-
M
urmoms.c
|
62
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
diff --git a/TODO b/TODO
@@ -3,7 +3,7 @@
 - add raw link to latest files: raw/file...
 - add summary page?
 - add diffstat to diff page? + and - lines summary?
-- escape < > ' " etc, maybe even use CDATA ?
+- escape HTML: < > ' " etc, maybe even use CDATA ?
 - shorter date format for logs.html page.
 - speed up generating files.
 - for files link to the commit but make the filename a link anchor.
diff --git a/urmoms.c b/urmoms.c
@@ -1,16 +1,20 @@
 #include <err.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "git2.h"
 
+static git_repository *repo;
+
 static const char *relpath = "";
 static const char *name = "";
 static const char *description = "";
 
 static const char *repodir = ".";
 
-static git_repository *repo;
+static int hasreadme, haslicense;
 
 FILE *
 efopen(const char *name, const char *flags)

@@ -102,8 +106,10 @@ writeheader(FILE *fp)
 	fprintf(fp, "<a href=\"%slog.html\">Log</a> |", relpath);
 	fprintf(fp, "<a href=\"%sfiles.html\">Files</a>| ", relpath);
 	fprintf(fp, "<a href=\"%sstats.html\">Stats</a> | ", relpath);
-	fprintf(fp, "<a href=\"%sreadme.html\">README</a> | ", relpath);
-	fprintf(fp, "<a href=\"%slicense.html\">LICENSE</a>", relpath);
+	if (hasreadme)
+		fprintf(fp, "<a href=\"%sreadme.html\">README</a> | ", relpath);
+	if (haslicense)
+		fprintf(fp, "<a href=\"%slicense.html\">LICENSE</a>", relpath);
 	fprintf(fp, "</center><hr/><pre>");
 
 	return 0;

@@ -181,12 +187,27 @@ writebranches(FILE *fp)
 }
 #endif
 
+void
+concat(FILE *fp1, FILE *fp2)
+{
+	char buf[BUFSIZ];
+	size_t n;
+
+	while ((n = fread(buf, 1, sizeof(buf), fp1))) {
+		fwrite(buf, 1, n, fp2);
+
+		if (feof(fp1) || ferror(fp1) || ferror(fp2))
+			break;
+	}
+}
+
 int
 main(int argc, char *argv[])
 {
-	int status;
 	const git_error *e = NULL;
-	FILE *fp;
+	FILE *fp, *fpread;
+	char path[PATH_MAX];
+	int status;
 
 	if (argc != 2) {
 		fprintf(stderr, "%s <repodir>\n", argv[0]);

@@ -202,6 +223,37 @@ main(int argc, char *argv[])
 		exit(status);
 	}
 
+	snprintf(path, sizeof(path), "%s%s%s",
+		repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "LICENSE");
+	if ((fpread = fopen(path, "r+b"))) {
+		fp = efopen("license.html", "w+b");
+		writeheader(fp);
+		concat(fpread, fp);
+		if (ferror(fpread) || ferror(fp))
+			err(1, "concat");
+		writefooter(fp);
+
+		fclose(fp);
+		fclose(fpread);
+
+		haslicense = 1;
+	}
+
+	snprintf(path, sizeof(path), "%s%s%s",
+		repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "README");
+	if ((fpread = fopen(path, "r+b"))) {
+		fp = efopen("readme.html", "w+b");
+		writeheader(fp);
+		concat(fpread, fp);
+		if (ferror(fpread) || ferror(fp))
+			err(1, "concat");
+		writefooter(fp);
+		fclose(fp);
+		fclose(fpread);
+
+		hasreadme = 1;
+	}
+
 	fp = efopen("logs.html", "w+b");
 	writeheader(fp);
 	writelog(fp);