From 27397b310d2aea881f5064103bb449464329b00c Mon Sep 17 00:00:00 2001 From: Eivind Uggedal Date: Wed, 9 Dec 2015 22:46:00 +0000 Subject: [PATCH] Write blobs to files/ This is work in progress: - relpath is broken under files/ - mkdirp was stolen from sbase - strlcpy wrapper might be needed --- urmoms.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------ 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/urmoms.c b/urmoms.c index ef715e4..fd1b0e7 100644 --- a/urmoms.c +++ b/urmoms.c @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -123,9 +124,9 @@ writeheader(FILE *fp) fprintf(fp, "Log | ", relpath); fprintf(fp, "Files", relpath); if (hasreadme) - fprintf(fp, " | README", relpath); + fprintf(fp, " | README", relpath); if (haslicense) - fprintf(fp, " | LICENSE", relpath); + fprintf(fp, " | LICENSE", relpath); fputs("\n
\n", fp); return 0; @@ -187,6 +188,25 @@ xbasename(const char *path) return b; } +int +mkdirp(const char *path) +{ + char tmp[PATH_MAX], *p; + + strlcpy(tmp, path, sizeof(tmp)); /* TODO: bring in libutil? */ + for (p = tmp + (tmp[0] == '/'); *p; p++) { + if (*p != '/') + continue; + *p = '\0'; + if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) + return -1; + *p = '/'; + } + if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) + return -1; + return 0; +} + void printtimeformat(FILE *fp, const git_time *intime, const char *fmt) { @@ -311,7 +331,7 @@ printshowfile(struct commitinfo *ci) } delta = git_patch_get_delta(patch); - fprintf(fp, "diff --git a/%s b/%s\n", + fprintf(fp, "diff --git a/%s b/%s\n", relpath, delta->old_file.path, delta->old_file.path, relpath, delta->new_file.path, delta->new_file.path); @@ -509,6 +529,39 @@ writeatom(FILE *fp) } int +writeblob(const char *path) +{ + char htmlpath[PATH_MAX]; + char refpath[PATH_MAX]; + char *relp; + git_object *obj = NULL; + FILE *fp; + + snprintf(htmlpath, sizeof(htmlpath), "file/%s.html", path); + snprintf(refpath, sizeof(refpath), "HEAD:%s", path); + + if (git_revparse_single(&obj, repo, refpath)) + return 1; /* TODO: handle error */ + + mkdirp(dirname(htmlpath)); + + relpath = "../"; /* TODO: dynamic relpath based on number of /'s */ + + fp = efopen(htmlpath, "w+b"); + writeheader(fp); + fputs("
\n", fp);
+	writeblobhtml(fp, (git_blob *)obj);
+	if (ferror(fp))
+		err(1, "fwrite");
+	git_object_free(obj);
+	fputs("
\n", fp); + writefooter(fp); + fclose(fp); + relpath = ""; + return 0; +} + +int writefiles(FILE *fp) { const git_index_entry *entry; @@ -524,15 +577,18 @@ writefiles(FILE *fp) for (i = 0; i < count; i++) { entry = git_index_get_byindex(index, i); + fputs("", fp); fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */ fprintf(fp, "path, strlen(entry->path)); - fputs("\">", fp); + fputs(".html\">", fp); xmlencode(fp, entry->path, strlen(entry->path)); fputs("", fp); fprintf(fp, "%" PRIu64, entry->file_size); fputs("\n", fp); + + writeblob(entry->path); } fputs("", fp); @@ -543,7 +599,7 @@ writefiles(FILE *fp) int main(int argc, char *argv[]) { - git_object *obj_license = NULL, *obj_readme = NULL; + git_object *obj = NULL; const git_error *e = NULL; FILE *fp, *fpread; char path[PATH_MAX], *p; @@ -584,38 +640,11 @@ main(int argc, char *argv[]) } /* check LICENSE */ - haslicense = !git_revparse_single(&obj_license, repo, "HEAD:LICENSE"); + haslicense = !git_revparse_single(&obj, repo, "HEAD:LICENSE"); + git_object_free(obj); /* check README */ - hasreadme = !git_revparse_single(&obj_readme, repo, "HEAD:README"); - - /* read LICENSE */ - if (haslicense) { - fp = efopen("license.html", "w+b"); - writeheader(fp); - fputs("
\n", fp);
-		writeblobhtml(fp, (git_blob *)obj_license);
-		git_object_free(obj_license);
-		if (ferror(fp))
-			err(1, "fwrite");
-		fputs("
\n", fp); - writefooter(fp); - - fclose(fp); - } - - /* read README */ - if (hasreadme) { - fp = efopen("readme.html", "w+b"); - writeheader(fp); - fputs("
\n", fp);
-		writeblobhtml(fp, (git_blob *)obj_readme);
-		git_object_free(obj_readme);
-		if (ferror(fp))
-			err(1, "fwrite");
-		fputs("
\n", fp); - writefooter(fp); - fclose(fp); - } + hasreadme = !git_revparse_single(&obj, repo, "HEAD:README"); + git_object_free(obj); fp = efopen("log.html", "w+b"); writeheader(fp); -- libgit2 1.1.1