stagit

rewrite writefiles, now works with bare repos

dont use the index but get the tree by the last commit id (revparse HEAD).

Hiltjo Posthuma contact@arjunchoudhary.com

commit: 18585e4 parent: 08c7b14
1 files changed, 76 insertions(+), 26 deletions(-)
Murmoms.c+76-26
M · urmoms.c +76, -26
  1@@ -561,21 +561,14 @@ writeatom(FILE *fp)
  2 }
  3 
  4 int
  5-writeblob(const git_index_entry *entry)
  6+writeblob(git_object *obj, const char *filename, git_off_t filesize)
  7 {
  8 	char fpath[PATH_MAX];
  9-	char ref[PATH_MAX];
 10 	char tmp[PATH_MAX] = "";
 11 	char *p;
 12-	git_object *obj = NULL;
 13 	FILE *fp;
 14 
 15-	snprintf(fpath, sizeof(fpath), "file/%s.html", entry->path);
 16-	snprintf(ref, sizeof(ref), "HEAD:%s", entry->path);
 17-
 18-	if (git_revparse_single(&obj, repo, ref))
 19-		return 1;
 20-
 21+	snprintf(fpath, sizeof(fpath), "file/%s.html", filename);
 22 	if (mkdirp(dirname(fpath)))
 23 		return 1;
 24 
 25@@ -589,7 +582,11 @@ writeblob(const git_index_entry *entry)
 26 
 27 	fp = efopen(fpath, "w");
 28 	writeheader(fp);
 29-	fprintf(fp, "<p>%s (%" PRIu32 "b)</p><hr/>", entry->path, entry->file_size);
 30+	fputs("<p> ", fp);
 31+	xmlencode(fp, filename, strlen(filename));
 32+	fprintf(fp, " (%" PRIu32 "b)", filesize);
 33+	fputs("</p><hr/>", fp);
 34+
 35 	if (git_blob_is_binary((git_blob *)obj)) {
 36 		fprintf(fp, "<p>Binary file</p>\n");
 37 	} else {
 38@@ -597,7 +594,6 @@ writeblob(const git_index_entry *entry)
 39 		if (ferror(fp))
 40 			err(1, "fwrite");
 41 	}
 42-	git_object_free(obj);
 43 	writefooter(fp);
 44 	fclose(fp);
 45 
 46@@ -607,35 +603,89 @@ writeblob(const git_index_entry *entry)
 47 }
 48 
 49 int
 50-writefiles(FILE *fp)
 51+writefilestree(FILE *fp, git_tree *tree, const char *path)
 52 {
 53-	const git_index_entry *entry;
 54-	git_index *index;
 55+	const git_tree_entry *entry = NULL;
 56+	const char *filename;
 57+	char filepath[PATH_MAX];
 58+	git_object *obj = NULL;
 59+	git_off_t filesize;
 60 	size_t count, i;
 61+	int ret;
 62 
 63-	fputs("<table id=\"files\"><thead>\n"
 64-	      "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
 65-	      "</thead><tbody>\n", fp);
 66+	count = git_tree_entrycount(tree);
 67+	for (i = 0; i < count; i++) {
 68+		if (!(entry = git_tree_entry_byindex(tree, i)))
 69+			return -1;
 70 
 71-	git_repository_index(&index, repo);
 72-	count = git_index_entrycount(index);
 73+		filename = git_tree_entry_name(entry);
 74+		if (git_tree_entry_to_object(&obj, repo, entry))
 75+			return -1;
 76+		switch (git_object_type(obj)) {
 77+		case GIT_OBJ_BLOB:
 78+			break;
 79+		case GIT_OBJ_TREE:
 80+			ret = writefilestree(fp, (git_tree *)obj, filename);
 81+			git_object_free(obj);
 82+			if (ret)
 83+				return ret;
 84+			continue;
 85+		default:
 86+			git_object_free(obj);
 87+			continue;
 88+		}
 89+		if (path[0]) {
 90+			snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
 91+			filename = filepath;
 92+		}
 93 
 94-	for (i = 0; i < count; i++) {
 95-		entry = git_index_get_byindex(index, i);
 96+		filesize = git_blob_rawsize((git_blob *)obj);
 97 
 98 		fputs("<tr><td>", fp);
 99-		fprintf(fp, "%u", entry->mode); /* TODO: fancy print, like: "-rw-r--r--" */
100+		/* TODO: fancy print, like: "-rw-r--r--" */
101+		fprintf(fp, "%u", git_tree_entry_filemode_raw(entry));
102 		fprintf(fp, "</td><td><a href=\"%sfile/", relpath);
103-		xmlencode(fp, entry->path, strlen(entry->path));
104+		xmlencode(fp, filename, strlen(filename));
105 		fputs(".html\">", fp);
106-		xmlencode(fp, entry->path, strlen(entry->path));
107+		xmlencode(fp, filename, strlen(filename));
108 		fputs("</a></td><td class=\"num\">", fp);
109-		fprintf(fp, "%" PRIu32, entry->file_size);
110+		fprintf(fp, "%" PRIu32, filesize);
111 		fputs("</td></tr>\n", fp);
112 
113-		writeblob(entry);
114+		writeblob(obj, filename, filesize);
115 	}
116 
117+	return 0;
118+}
119+
120+int
121+writefiles(FILE *fp)
122+{
123+	const git_oid *id;
124+	git_tree *tree = NULL;
125+	git_object *obj = NULL;
126+	git_commit *commit = NULL;
127+
128+	fputs("<table id=\"files\"><thead>\n"
129+	      "<tr><td>Mode</td><td>Name</td><td>Size</td></tr>\n"
130+	      "</thead><tbody>\n", fp);
131+
132+	if (git_revparse_single(&obj, repo, "HEAD"))
133+		return -1;
134+	id = git_object_id(obj);
135+	if (git_commit_lookup(&commit, repo, id))
136+		return -1;
137+	if (git_commit_tree(&tree, commit)) {
138+		git_commit_free(commit);
139+		return -1;
140+	}
141+	git_commit_free(commit);
142+
143+	writefilestree(fp, tree, "");
144+
145+	git_commit_free(commit);
146+	git_tree_free(tree);
147+
148 	fputs("</tbody></table>", fp);
149 
150 	return 0;