stagit

Write blobs to files/

This is work in progress:

- relpath is broken under files/
- mkdirp was stolen from sbase
- strlcpy wrapper might be needed

Eivind Uggedal contact@arjunchoudhary.com

commit: b21dadf parent: 258a3e8
1 files changed, 65 insertions(+), 36 deletions(-)
Murmoms.c+65-36
M · urmoms.c +65, -36
  1@@ -1,6 +1,7 @@
  2 #include <sys/stat.h>
  3 
  4 #include <err.h>
  5+#include <errno.h>
  6 #include <inttypes.h>
  7 #include <libgen.h>
  8 #include <limits.h>
  9@@ -123,9 +124,9 @@ writeheader(FILE *fp)
 10 	fprintf(fp, "<a href=\"%slog.html\">Log</a> | ", relpath);
 11 	fprintf(fp, "<a href=\"%sfiles.html\">Files</a>", relpath);
 12 	if (hasreadme)
 13-		fprintf(fp, " | <a href=\"%sreadme.html\">README</a>", relpath);
 14+		fprintf(fp, " | <a href=\"%sfile/README.html\">README</a>", relpath);
 15 	if (haslicense)
 16-		fprintf(fp, " | <a href=\"%slicense.html\">LICENSE</a>", relpath);
 17+		fprintf(fp, " | <a href=\"%sfile/LICENSE.html\">LICENSE</a>", relpath);
 18 	fputs("</td></tr></table>\n<hr/><div id=\"content\">\n", fp);
 19 
 20 	return 0;
 21@@ -187,6 +188,25 @@ xbasename(const char *path)
 22 	return b;
 23 }
 24 
 25+int
 26+mkdirp(const char *path)
 27+{
 28+	char tmp[PATH_MAX], *p;
 29+
 30+	strlcpy(tmp, path, sizeof(tmp)); /* TODO: bring in libutil? */
 31+	for (p = tmp + (tmp[0] == '/'); *p; p++) {
 32+		if (*p != '/')
 33+			continue;
 34+		*p = '\0';
 35+		if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
 36+			return -1;
 37+		*p = '/';
 38+	}
 39+	if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST)
 40+		return -1;
 41+	return 0;
 42+}
 43+
 44 void
 45 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
 46 {
 47@@ -311,7 +331,7 @@ printshowfile(struct commitinfo *ci)
 48 		}
 49 
 50 		delta = git_patch_get_delta(patch);
 51-		fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s\">%s</a> b/<a href=\"%sfile/%s\">%s</a></b>\n",
 52+		fprintf(fp, "<b>diff --git a/<a href=\"%sfile/%s.html\">%s</a> b/<a href=\"%sfile/%s.html\">%s</a></b>\n",
 53 			relpath, delta->old_file.path, delta->old_file.path,
 54 			relpath, delta->new_file.path, delta->new_file.path);
 55 
 56@@ -508,6 +528,39 @@ writeatom(FILE *fp)
 57 	return 0;
 58 }
 59 
 60+int
 61+writeblob(const char *path)
 62+{
 63+	char htmlpath[PATH_MAX];
 64+	char refpath[PATH_MAX];
 65+	char *relp;
 66+	git_object *obj = NULL;
 67+	FILE *fp;
 68+
 69+	snprintf(htmlpath, sizeof(htmlpath), "file/%s.html", path);
 70+	snprintf(refpath, sizeof(refpath), "HEAD:%s", path);
 71+
 72+	if (git_revparse_single(&obj, repo, refpath))
 73+		return 1; /* TODO: handle error */
 74+
 75+	mkdirp(dirname(htmlpath));
 76+
 77+	relpath = "../"; /* TODO: dynamic relpath based on number of /'s */
 78+
 79+	fp = efopen(htmlpath, "w+b");
 80+	writeheader(fp);
 81+	fputs("<pre>\n", fp);
 82+	writeblobhtml(fp, (git_blob *)obj);
 83+	if (ferror(fp))
 84+		err(1, "fwrite");
 85+	git_object_free(obj);
 86+	fputs("</pre>\n", fp);
 87+	writefooter(fp);
 88+	fclose(fp);
 89+	relpath = "";
 90+	return 0;
 91+}
 92+
 93 int
 94 writefiles(FILE *fp)
 95 {
 96@@ -524,15 +577,18 @@ writefiles(FILE *fp)
 97 
 98 	for (i = 0; i < count; i++) {
 99 		entry = git_index_get_byindex(index, i);
100+
101 		fputs("<tr><td>", fp);
102 		fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
103 		fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
104 		xmlencode(fp, entry->path, strlen(entry->path));
105-		fputs("\">", fp);
106+		fputs(".html\">", fp);
107 		xmlencode(fp, entry->path, strlen(entry->path));
108 		fputs("</a></td><td>", fp);
109 		fprintf(fp, "%" PRIu64, entry->file_size);
110 		fputs("</td></tr>\n", fp);
111+
112+		writeblob(entry->path);
113 	}
114 
115 	fputs("</tbody></table>", fp);
116@@ -543,7 +599,7 @@ writefiles(FILE *fp)
117 int
118 main(int argc, char *argv[])
119 {
120-	git_object *obj_license = NULL, *obj_readme = NULL;
121+	git_object *obj = NULL;
122 	const git_error *e = NULL;
123 	FILE *fp, *fpread;
124 	char path[PATH_MAX], *p;
125@@ -584,38 +640,11 @@ main(int argc, char *argv[])
126 	}
127 
128 	/* check LICENSE */
129-	haslicense = !git_revparse_single(&obj_license, repo, "HEAD:LICENSE");
130+	haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE");
131+	git_object_free(obj);
132 	/* check README */
133-	hasreadme = !git_revparse_single(&obj_readme, repo, "HEAD:README");
134-
135-	/* read LICENSE */
136-	if (haslicense) {
137-		fp = efopen("license.html", "w+b");
138-		writeheader(fp);
139-		fputs("<pre>\n", fp);
140-		writeblobhtml(fp, (git_blob *)obj_license);
141-		git_object_free(obj_license);
142-		if (ferror(fp))
143-			err(1, "fwrite");
144-		fputs("</pre>\n", fp);
145-		writefooter(fp);
146-
147-		fclose(fp);
148-	}
149-
150-	/* read README */
151-	if (hasreadme) {
152-		fp = efopen("readme.html", "w+b");
153-		writeheader(fp);
154-		fputs("<pre>\n", fp);
155-		writeblobhtml(fp, (git_blob *)obj_readme);
156-		git_object_free(obj_readme);
157-		if (ferror(fp))
158-			err(1, "fwrite");
159-		fputs("</pre>\n", fp);
160-		writefooter(fp);
161-		fclose(fp);
162-	}
163+	hasreadme = !git_revparse_single(&obj, repo, "HEAD:README");
164+	git_object_free(obj);
165 
166 	fp = efopen("log.html", "w+b");
167 	writeheader(fp);