From 7294b10b65ff8a91bd330108af98441ec4a05307 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sat, 5 Dec 2015 20:43:29 +0100 Subject: [PATCH] add README and LICENSE file-detection --- TODO | 2 +- urmoms.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 868678c..ce740a7 100644 --- 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 index c0d2ab3..2d385f1 100644 --- a/urmoms.c +++ b/urmoms.c @@ -1,16 +1,20 @@ #include +#include #include #include +#include #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, "Log |", relpath); fprintf(fp, "Files| ", relpath); fprintf(fp, "Stats | ", relpath); - fprintf(fp, "README | ", relpath); - fprintf(fp, "LICENSE", relpath); + if (hasreadme) + fprintf(fp, "README | ", relpath); + if (haslicense) + fprintf(fp, "LICENSE", relpath); fprintf(fp, "
");
 
 	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 \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);
--
libgit2 1.1.1