From 934980659a7c1d7ccebec69a65eef2bf96a6c7a2 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 8 Jan 2016 20:08:40 +0100 Subject: [PATCH] implify and restructure code - remove commit message and lines changed from refs page. - resolve HEAD once. - dont make filepath in writeblob() --- stagit.c | 107 +++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------- 1 file changed, 37 insertions(+), 70 deletions(-) diff --git a/stagit.c b/stagit.c index ab62c8f..3f1594d 100644 --- a/stagit.c +++ b/stagit.c @@ -329,12 +329,11 @@ printcommit(FILE *fp, struct commitinfo *ci) printtime(fp, &(ci->author->when)); fputc('\n', fp); } - fputc('\n', fp); - - if (ci->msg) + if (ci->msg) { + fputc('\n', fp); xmlencode(fp, ci->msg, strlen(ci->msg)); - - fputc('\n', fp); + fputc('\n', fp); + } } void @@ -357,19 +356,18 @@ printshowfile(struct commitinfo *ci) fp = efopen(path, "w"); writeheader(fp); - fputs("
\n", fp);
+	fputs("
", fp);
 	printcommit(fp, ci);
 
 	memset(&statsbuf, 0, sizeof(statsbuf));
 
 	/* diff stat */
-	if (ci->stats) {
-		if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
-		    GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
-			if (statsbuf.ptr && statsbuf.ptr[0]) {
-				fputs("Diffstat:\n", fp);
-				fputs(statsbuf.ptr, fp);
-			}
+	if (ci->stats &&
+	    !git_diff_stats_to_buf(&statsbuf, ci->stats,
+                                   GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
+		if (statsbuf.ptr && statsbuf.ptr[0]) {
+			fputs("Diffstat:\n", fp);
+			xmlencode(fp, statsbuf.ptr, strlen(statsbuf.ptr));
 		}
 	}
 
@@ -430,21 +428,13 @@ printshowfile(struct commitinfo *ci)
 }
 
 int
-writelog(FILE *fp, const char *branch)
+writelog(FILE *fp, const git_oid *oid)
 {
 	struct commitinfo *ci;
-	const git_oid *oid;
 	git_revwalk *w = NULL;
-	git_object *obj = NULL;
 	git_oid id;
 	size_t len;
 
-	mkdir("commit", 0755);
-
-	if (git_revparse_single(&obj, repo, branch))
-		return -1;
-	oid = git_object_id(obj);
-
 	git_revwalk_new(&w, repo);
 	git_revwalk_push(w, oid);
 	git_revwalk_sorting(w, GIT_SORT_TIME);
@@ -493,7 +483,6 @@ writelog(FILE *fp, const char *branch)
 	fputs("", fp);
 
 	git_revwalk_free(w);
-	git_object_free(obj);
 
 	relpath = "";
 
@@ -579,14 +568,13 @@ writeatom(FILE *fp)
 }
 
 int
-writeblob(git_object *obj, const char *filename, git_off_t filesize)
+writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
 {
-	char fpath[PATH_MAX];
 	char tmp[PATH_MAX] = "";
-	char *d, *p;
+	char *d;
+	const char *p;
 	FILE *fp;
 
-	snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
 	d = xdirname(fpath);
 	if (mkdirp(d)) {
 		free(d);
@@ -698,35 +686,32 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
 			git_object_free(obj);
 			continue;
 		}
-		if (path[0]) {
-			snprintf(filepath, sizeof(filepath), "%s/%s",
+		if (path[0])
+			snprintf(filepath, sizeof(filepath), "file/%s/%s.html",
 			         path, filename);
-			filename = filepath;
-		}
+		else
+			snprintf(filepath, sizeof(filepath), "file/%s.html",
+			         filename);
 		filesize = git_blob_rawsize((git_blob *)obj);
 
 		fputs("", fp);
-		fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry)));
-		fprintf(fp, "", fp);
+		fputs(filemode(git_tree_entry_filemode(entry)), fp);
+		fprintf(fp, "", relpath, filepath);
 		xmlencode(fp, filename, strlen(filename));
 		fputs("", fp);
 		fprintf(fp, "%ju", (uintmax_t)filesize);
 		fputs("\n", fp);
 
-		writeblob(obj, filename, filesize);
+		writeblob(obj, filepath, filename, filesize);
 	}
 
 	return 0;
 }
 
 int
-writefiles(FILE *fp, const char *branch)
+writefiles(FILE *fp, const git_oid *id, const char *branch)
 {
-	const git_oid *id;
 	git_tree *tree = NULL;
-	git_object *obj = NULL;
 	git_commit *commit = NULL;
 	int ret = -1;
 
@@ -734,9 +719,6 @@ writefiles(FILE *fp, const char *branch)
 	      "ModeNameSize"
 	      "\n\n", fp);
 
-	if (git_revparse_single(&obj, repo, branch))
-		goto err;
-	id = git_object_id(obj);
 	if (git_commit_lookup(&commit, repo, id) ||
 	    git_commit_tree(&tree, commit))
 		goto err;
@@ -745,7 +727,6 @@ writefiles(FILE *fp, const char *branch)
 err:
 	fputs("", fp);
 
-	git_object_free(obj);
 	git_commit_free(commit);
 	git_tree_free(tree);
 
@@ -778,7 +759,7 @@ writerefs(FILE *fp)
 	git_reference *dref = NULL, *r, *ref = NULL;
 	git_reference_iterator *it = NULL;
 	git_reference **refs = NULL;
-	size_t count, i, j, len, refcount = 0;
+	size_t count, i, j, refcount = 0;
 	const char *cols[] = { "Branch", "Tag" }; /* first column title */
 	const char *titles[] = { "Branches", "Tags" };
 	const char *ids[] = { "branches", "tags" };
@@ -827,9 +808,7 @@ writerefs(FILE *fp)
 			/* print header if it has an entry (first). */
 			if (++count == 1) {
 				fprintf(fp, "

%s

\n" - "" - "" - "\n\n", + "\n\n\n", titles[j], ids[j], cols[j]); } @@ -842,28 +821,8 @@ writerefs(FILE *fp) if (ci->author) printtimeshort(fp, &(ci->author->when)); fputs("\n", fp); relpath = "../"; @@ -876,7 +835,7 @@ writerefs(FILE *fp) } /* table footer */ if (count) - fputs("
%sAgeCommit messageAuthorFiles+-
AgeAuthor
", fp); - if (ci->summary) { - if (j) - fprintf(fp, "", - relpath, ci->oid); - if ((len = strlen(ci->summary)) > summarylen) { - xmlencode(fp, ci->summary, summarylen - 1); - fputs("…", fp); - } else { - xmlencode(fp, ci->summary, len); - } - if (j) - fputs("", fp); - } - fputs("", fp); if (ci->author) xmlencode(fp, ci->author->name, strlen(ci->author->name)); - fputs("", fp); - fprintf(fp, "%zu", ci->filecount); - fputs("", fp); - fprintf(fp, "+%zu", ci->addcount); - fputs("", fp); - fprintf(fp, "-%zu", ci->delcount); fputs("
", fp); + fputs("
", fp); } err: @@ -894,6 +853,7 @@ int main(int argc, char *argv[]) { git_object *obj = NULL; + const git_oid *head = NULL; const git_error *e = NULL; FILE *fp, *fpread; char path[PATH_MAX], *p; @@ -914,6 +874,12 @@ main(int argc, char *argv[]) return status; } + /* find HEAD */ + if (git_revparse_single(&obj, repo, "HEAD")) + return 1; + head = git_object_id(obj); + git_object_free(obj); + /* use directory name as name */ name = xbasename(repodir); @@ -964,14 +930,15 @@ main(int argc, char *argv[]) fp = efopen("log.html", "w"); relpath = ""; writeheader(fp); - writelog(fp, "HEAD"); + mkdir("commit", 0755); + writelog(fp, head); writefooter(fp); fclose(fp); /* files for HEAD */ fp = efopen("files.html", "w"); writeheader(fp); - writefiles(fp, "HEAD"); + writefiles(fp, head, "HEAD"); writefooter(fp); fclose(fp); -- libgit2 1.1.1