stagit

implify and restructure code

- remove commit message and lines changed from refs page.
- resolve HEAD once.
- dont make filepath in writeblob()

Hiltjo Posthuma contact@arjunchoudhary.com

commit: a55783c parent: d9acaf2
1 files changed, 37 insertions(+), 70 deletions(-)
Mstagit.c+37-70
M · stagit.c +37, -70
  1@@ -329,12 +329,11 @@ printcommit(FILE *fp, struct commitinfo *ci)
  2 		printtime(fp, &(ci->author->when));
  3 		fputc('\n', fp);
  4 	}
  5-	fputc('\n', fp);
  6-
  7-	if (ci->msg)
  8+	if (ci->msg) {
  9+		fputc('\n', fp);
 10 		xmlencode(fp, ci->msg, strlen(ci->msg));
 11-
 12-	fputc('\n', fp);
 13+		fputc('\n', fp);
 14+	}
 15 }
 16 
 17 void
 18@@ -357,19 +356,18 @@ printshowfile(struct commitinfo *ci)
 19 
 20 	fp = efopen(path, "w");
 21 	writeheader(fp);
 22-	fputs("<pre>\n", fp);
 23+	fputs("<pre>", fp);
 24 	printcommit(fp, ci);
 25 
 26 	memset(&statsbuf, 0, sizeof(statsbuf));
 27 
 28 	/* diff stat */
 29-	if (ci->stats) {
 30-		if (!git_diff_stats_to_buf(&statsbuf, ci->stats,
 31-		    GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
 32-			if (statsbuf.ptr && statsbuf.ptr[0]) {
 33-				fputs("<b>Diffstat:</b>\n", fp);
 34-				fputs(statsbuf.ptr, fp);
 35-			}
 36+	if (ci->stats &&
 37+	    !git_diff_stats_to_buf(&statsbuf, ci->stats,
 38+                                   GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_SHORT, 80)) {
 39+		if (statsbuf.ptr && statsbuf.ptr[0]) {
 40+			fputs("<b>Diffstat:</b>\n", fp);
 41+			xmlencode(fp, statsbuf.ptr, strlen(statsbuf.ptr));
 42 		}
 43 	}
 44 
 45@@ -430,21 +428,13 @@ printshowfile(struct commitinfo *ci)
 46 }
 47 
 48 int
 49-writelog(FILE *fp, const char *branch)
 50+writelog(FILE *fp, const git_oid *oid)
 51 {
 52 	struct commitinfo *ci;
 53-	const git_oid *oid;
 54 	git_revwalk *w = NULL;
 55-	git_object *obj = NULL;
 56 	git_oid id;
 57 	size_t len;
 58 
 59-	mkdir("commit", 0755);
 60-
 61-	if (git_revparse_single(&obj, repo, branch))
 62-		return -1;
 63-	oid = git_object_id(obj);
 64-
 65 	git_revwalk_new(&w, repo);
 66 	git_revwalk_push(w, oid);
 67 	git_revwalk_sorting(w, GIT_SORT_TIME);
 68@@ -493,7 +483,6 @@ writelog(FILE *fp, const char *branch)
 69 	fputs("</tbody></table>", fp);
 70 
 71 	git_revwalk_free(w);
 72-	git_object_free(obj);
 73 
 74 	relpath = "";
 75 
 76@@ -579,14 +568,13 @@ writeatom(FILE *fp)
 77 }
 78 
 79 int
 80-writeblob(git_object *obj, const char *filename, git_off_t filesize)
 81+writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize)
 82 {
 83-	char fpath[PATH_MAX];
 84 	char tmp[PATH_MAX] = "";
 85-	char *d, *p;
 86+	char *d;
 87+	const char *p;
 88 	FILE *fp;
 89 
 90-	snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
 91 	d = xdirname(fpath);
 92 	if (mkdirp(d)) {
 93 		free(d);
 94@@ -698,35 +686,32 @@ writefilestree(FILE *fp, git_tree *tree, const char *branch, const char *path)
 95 			git_object_free(obj);
 96 			continue;
 97 		}
 98-		if (path[0]) {
 99-			snprintf(filepath, sizeof(filepath), "%s/%s",
100+		if (path[0])
101+			snprintf(filepath, sizeof(filepath), "file/%s/%s.html",
102 			         path, filename);
103-			filename = filepath;
104-		}
105+		else
106+			snprintf(filepath, sizeof(filepath), "file/%s.html",
107+			         filename);
108 		filesize = git_blob_rawsize((git_blob *)obj);
109 
110 		fputs("<tr><td>", fp);
111-		fprintf(fp, "%s", filemode(git_tree_entry_filemode(entry)));
112-		fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
113-		xmlencode(fp, filename, strlen(filename));
114-		fputs(".html\">", fp);
115+		fputs(filemode(git_tree_entry_filemode(entry)), fp);
116+		fprintf(fp, "</td><td><a href=\"%s%s\">", relpath, filepath);
117 		xmlencode(fp, filename, strlen(filename));
118 		fputs("</a></td><td class=\"num\">", fp);
119 		fprintf(fp, "%ju", (uintmax_t)filesize);
120 		fputs("</td></tr>\n", fp);
121 
122-		writeblob(obj, filename, filesize);
123+		writeblob(obj, filepath, filename, filesize);
124 	}
125 
126 	return 0;
127 }
128 
129 int
130-writefiles(FILE *fp, const char *branch)
131+writefiles(FILE *fp, const git_oid *id, const char *branch)
132 {
133-	const git_oid *id;
134 	git_tree *tree = NULL;
135-	git_object *obj = NULL;
136 	git_commit *commit = NULL;
137 	int ret = -1;
138 
139@@ -734,9 +719,6 @@ writefiles(FILE *fp, const char *branch)
140 	      "<td>Mode</td><td>Name</td><td class=\"num\">Size</td>"
141 	      "</tr>\n</thead><tbody>\n", fp);
142 
143-	if (git_revparse_single(&obj, repo, branch))
144-		goto err;
145-	id = git_object_id(obj);
146 	if (git_commit_lookup(&commit, repo, id) ||
147 	    git_commit_tree(&tree, commit))
148 		goto err;
149@@ -745,7 +727,6 @@ writefiles(FILE *fp, const char *branch)
150 err:
151 	fputs("</tbody></table>", fp);
152 
153-	git_object_free(obj);
154 	git_commit_free(commit);
155 	git_tree_free(tree);
156 
157@@ -778,7 +759,7 @@ writerefs(FILE *fp)
158 	git_reference *dref = NULL, *r, *ref = NULL;
159 	git_reference_iterator *it = NULL;
160 	git_reference **refs = NULL;
161-	size_t count, i, j, len, refcount = 0;
162+	size_t count, i, j, refcount = 0;
163 	const char *cols[] = { "Branch", "Tag" }; /* first column title */
164 	const char *titles[] = { "Branches", "Tags" };
165 	const char *ids[] = { "branches", "tags" };
166@@ -827,9 +808,7 @@ writerefs(FILE *fp)
167 			/* print header if it has an entry (first). */
168 			if (++count == 1) {
169 				fprintf(fp, "<h2>%s</h2><table id=\"%s\"><thead>\n<tr><td>%s</td>"
170-				      "<td>Age</td><td>Commit message</td>"
171-				      "<td>Author</td><td>Files</td><td class=\"num\">+</td>"
172-				      "<td class=\"num\">-</td></tr>\n</thead><tbody>\n",
173+				      "<td>Age</td><td>Author</td>\n</tr>\n</thead><tbody>\n",
174 				      titles[j], ids[j], cols[j]);
175 			}
176 
177@@ -842,28 +821,8 @@ writerefs(FILE *fp)
178 			if (ci->author)
179 				printtimeshort(fp, &(ci->author->when));
180 			fputs("</td><td>", fp);
181-			if (ci->summary) {
182-				if (j)
183-					fprintf(fp, "<a href=\"%scommit/%s.html\">",
184-					        relpath, ci->oid);
185-				if ((len = strlen(ci->summary)) > summarylen) {
186-					xmlencode(fp, ci->summary, summarylen - 1);
187-					fputs("…", fp);
188-				} else {
189-					xmlencode(fp, ci->summary, len);
190-				}
191-				if (j)
192-					fputs("</a>", fp);
193-			}
194-			fputs("</td><td>", fp);
195 			if (ci->author)
196 				xmlencode(fp, ci->author->name, strlen(ci->author->name));
197-			fputs("</td><td class=\"num\">", fp);
198-			fprintf(fp, "%zu", ci->filecount);
199-			fputs("</td><td class=\"num\">", fp);
200-			fprintf(fp, "+%zu", ci->addcount);
201-			fputs("</td><td class=\"num\">", fp);
202-			fprintf(fp, "-%zu", ci->delcount);
203 			fputs("</td></tr>\n", fp);
204 
205 			relpath = "../";
206@@ -876,7 +835,7 @@ writerefs(FILE *fp)
207 		}
208 		/* table footer */
209 		if (count)
210-			fputs("</tbody></table>", fp);
211+			fputs("</tbody></table><br/>", fp);
212 	}
213 
214 err:
215@@ -894,6 +853,7 @@ int
216 main(int argc, char *argv[])
217 {
218 	git_object *obj = NULL;
219+	const git_oid *head = NULL;
220 	const git_error *e = NULL;
221 	FILE *fp, *fpread;
222 	char path[PATH_MAX], *p;
223@@ -914,6 +874,12 @@ main(int argc, char *argv[])
224 		return status;
225 	}
226 
227+	/* find HEAD */
228+	if (git_revparse_single(&obj, repo, "HEAD"))
229+		return 1;
230+	head = git_object_id(obj);
231+	git_object_free(obj);
232+
233 	/* use directory name as name */
234 	name = xbasename(repodir);
235 
236@@ -964,14 +930,15 @@ main(int argc, char *argv[])
237 	fp = efopen("log.html", "w");
238 	relpath = "";
239 	writeheader(fp);
240-	writelog(fp, "HEAD");
241+	mkdir("commit", 0755);
242+	writelog(fp, head);
243 	writefooter(fp);
244 	fclose(fp);
245 
246 	/* files for HEAD */
247 	fp = efopen("files.html", "w");
248 	writeheader(fp);
249-	writefiles(fp, "HEAD");
250+	writefiles(fp, head, "HEAD");
251 	writefooter(fp);
252 	fclose(fp);
253