stagit

read README and LICENSE from repo, escape as HTML

Hiltjo Posthuma contact@arjunchoudhary.com

commit: d9ba256 parent: 9c3041f
1 files changed, 67 insertions(+), 26 deletions(-)
Murmoms.c+67-26
M · urmoms.c +67, -26
  1@@ -28,17 +28,21 @@ efopen(const char *name, const char *flags)
  2 	return fp;
  3 }
  4 
  5+/* Escape characters below as HTML 2.0 / XML 1.0. */
  6 void
  7-concat(FILE *fp1, FILE *fp2)
  8+xmlencode(FILE *fp, const char *s, size_t len)
  9 {
 10-	char buf[BUFSIZ];
 11-	size_t n;
 12-
 13-	while ((n = fread(buf, 1, sizeof(buf), fp1))) {
 14-		fwrite(buf, 1, n, fp2);
 15-
 16-		if (feof(fp1) || ferror(fp1) || ferror(fp2))
 17-			break;
 18+	size_t i;
 19+
 20+	for (i = 0; *s && i < len; s++, i++) {
 21+		switch(*s) {
 22+		case '<':  fputs("&lt;",   fp); break;
 23+		case '>':  fputs("&gt;",   fp); break;
 24+		case '\'': fputs("&apos;", fp); break;
 25+		case '&':  fputs("&amp;",  fp); break;
 26+		case '"':  fputs("&quot;", fp); break;
 27+		default:   fputc(*s, fp);
 28+		}
 29 	}
 30 }
 31 
 32@@ -64,7 +68,7 @@ xbasename(const char *path)
 33 }
 34 
 35 static void
 36-printtime(FILE *fp, const git_time * intime, const char *prefix)
 37+printtime(FILE *fp, const git_time *intime, const char *prefix)
 38 {
 39 	struct tm *intm;
 40 	time_t t;
 41@@ -91,7 +95,43 @@ printtime(FILE *fp, const git_time * intime, const char *prefix)
 42 }
 43 
 44 static void
 45-printcommit(FILE *fp, git_commit * commit)
 46+printcommit(FILE *fp, git_commit *commit)
 47+{
 48+	const git_signature *sig;
 49+	char buf[GIT_OID_HEXSZ + 1];
 50+	int i, count;
 51+	const char *scan, *eol;
 52+
 53+	git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
 54+	fprintf(fp, "commit <a href=\"commit/%s.html\">%s</a>\n", buf, buf);
 55+
 56+	if ((count = (int)git_commit_parentcount(commit)) > 1) {
 57+		fprintf(fp, "Merge:");
 58+		for (i = 0; i < count; ++i) {
 59+			git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
 60+			fprintf(fp, " %s", buf);
 61+		}
 62+		fprintf(fp, "\n");
 63+	}
 64+	if ((sig = git_commit_author(commit)) != NULL) {
 65+		fprintf(fp, "Author: <a href=\"author/%s.html\">%s</a> <%s>\n",
 66+			sig->name, sig->name, sig->email);
 67+		printtime(fp, &sig->when, "Date:   ");
 68+	}
 69+	fprintf(fp, "\n");
 70+
 71+	for (scan = git_commit_message(commit); scan && *scan;) {
 72+		for (eol = scan; *eol && *eol != '\n'; ++eol)	/* find eol */
 73+			;
 74+
 75+		fprintf(fp, "    %.*s\n", (int) (eol - scan), scan);
 76+		scan = *eol ? eol + 1 : NULL;
 77+	}
 78+	fprintf(fp, "\n");
 79+}
 80+
 81+static void
 82+printcommitdiff(FILE *fp, git_commit *commit)
 83 {
 84 	const git_signature *sig;
 85 	char buf[GIT_OID_HEXSZ + 1];
 86@@ -222,9 +262,16 @@ writebranches(FILE *fp)
 87 }
 88 #endif
 89 
 90+void
 91+writeblobhtml(FILE *fp, const git_blob *blob)
 92+{
 93+	xmlencode(fp, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob));
 94+}
 95+
 96 int
 97 main(int argc, char *argv[])
 98 {
 99+	git_object *obj = NULL;
100 	const git_error *e = NULL;
101 	FILE *fp, *fpread;
102 	char path[PATH_MAX], *p;
103@@ -241,7 +288,7 @@ main(int argc, char *argv[])
104 	if ((status = git_repository_open(&repo, repodir)) < 0) {
105 		e = giterr_last();
106 		fprintf(stderr, "error %d/%d: %s\n", status, e->klass, e->message);
107-		exit(status);
108+		return status;
109 	}
110 
111 	/* use directory name as name */
112@@ -264,34 +311,28 @@ main(int argc, char *argv[])
113 	}
114 
115 	/* read LICENSE */
116-	snprintf(path, sizeof(path), "%s%s%s",
117-		repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "LICENSE");
118-	if ((fpread = fopen(path, "r+b"))) {
119+	if (!git_revparse_single(&obj, repo, "HEAD:LICENSE")) {
120 		fp = efopen("license.html", "w+b");
121 		writeheader(fp);
122-		concat(fpread, fp);
123-		if (ferror(fpread) || ferror(fp))
124-			err(1, "concat");
125+		writeblobhtml(fp, (git_blob *)obj);
126+		if (ferror(fp))
127+			err(1, "fwrite");
128 		writefooter(fp);
129 
130 		fclose(fp);
131-		fclose(fpread);
132 
133 		haslicense = 1;
134 	}
135 
136 	/* read README */
137-	snprintf(path, sizeof(path), "%s%s%s",
138-		repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "README");
139-	if ((fpread = fopen(path, "r+b"))) {
140+	if (!git_revparse_single(&obj, repo, "HEAD:README")) {
141 		fp = efopen("readme.html", "w+b");
142 		writeheader(fp);
143-		concat(fpread, fp);
144-		if (ferror(fpread) || ferror(fp))
145-			err(1, "concat");
146+		writeblobhtml(fp, (git_blob *)obj);
147+		if (ferror(fp))
148+			err(1, "fwrite");
149 		writefooter(fp);
150 		fclose(fp);
151-		fclose(fpread);
152 
153 		hasreadme = 1;
154 	}