stagit

static git page generator
git clone https://git.awy.one/stagit
Log | Files | Refs | README | LICENSE

commit 3d5531f6c10ae36ca34a69f78a8974f3adfafcc3
parent 60f688cb8923c79ad682d6cf47bcc6807bceeebf
Author: awy <awy@awy.one>
Date:   Fri, 10 Apr 2026 22:11:28 +0300

show folders first in files tab

Diffstat:
Mstagit.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 60 insertions(+), 23 deletions(-)

diff --git a/stagit.c b/stagit.c @@ -1120,25 +1120,22 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) else ++parent; } - /* fputs("<tr><td>d---------</td><td><a class=\"dir\" href=\"../", fp); */ - /* xmlencode(fp, parent, strlen(parent)); */ - - fprintf(fp, "<tr style=\"cursor: pointer; cursor: hand;\" onclick=\"window.location.href=\'../"); - percentencode(fp, parent, strlen(parent)); - fputs(".html\'\">", fp); - - fputs("<td id=\"file-mode\">d---------</td><td><a class=\"dir\" href=\"../", fp); + fprintf(fp, "<tr>"); + // percentencode(fp, parent, strlen(parent)); + fputs("<td id=\"file-mode\">d---------</td>", fp); + fputs("<td><a class=\"dir\" href=\"../", fp); xmlencode(fp, parent, strlen(parent)); - - /* fputs(".html\">..</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp); */ - fputs(".html\">..</a></td><td class=\"num\" align=\"right\"></td></tr>\n", fp); + fputs(".html\">..</a></td>", fp); + fputs("<td class=\"num\" align=\"right\"></td></tr>\n", fp); } count = git_tree_entrycount(tree); + for (i = 0; i < count; i++) { if (!(entry = git_tree_entry_byindex(tree, i)) || !(entryname = git_tree_entry_name(entry))) return -1; + joinpath(entrypath, sizeof(entrypath), path, entryname); r = snprintf(filepath, sizeof(filepath), "file/%s.html", @@ -1150,15 +1147,8 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) if (rf < 0 || (size_t)rf >= sizeof(rawpath)) errx(1, "path truncated: 'raw/%s'", entrypath); - if (!git_tree_entry_to_object(&obj, repo, entry)) { - switch (git_object_type(obj)) { - case GIT_OBJ_BLOB: - is_obj_tree = 0; - filesize = git_blob_rawsize((git_blob *)obj); - lc = writeblob(obj, filepath, rawpath, entryname, filesize); - writeblobraw((git_blob *)obj, rawpath, entryname, filesize); - break; - case GIT_OBJ_TREE: + if (!git_tree_entry_to_object(&obj, repo, entry)) + if (git_object_type(obj) == GIT_OBJ_TREE) { mkdirfile(filepath); if (strlcpy(tmp, relpath, sizeof(tmp)) >= sizeof(tmp)) @@ -1182,7 +1172,57 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) is_obj_tree = 1; if (ret) return ret; + + fputs("<tr><td>", fp); + fputs(filemode(git_tree_entry_filemode(entry)), fp); + fprintf(fp, "</td>"); + + if (git_object_type(obj) == GIT_OBJ_TREE) + fprintf(fp, "<td id=\"dir-name\"><a href=\"%s", relpath); + else + fprintf(fp, "<td id=\"file-name\"><a href=\"%s", relpath); + + percentencode(fp, filepath, strlen(filepath)); + fputs("\">", fp); + + xmlencode(fp, entryname, strlen(entryname)); + + fputs("</a></td><td id=\"file-size\" class=\"num\" align=\"right\">", fp); + + if (lc > 0) + fprintf(fp, "%zuL", lc); + else if (!is_obj_tree) + fprintf(fp, "%zuB", filesize); + fputs("</td></tr>\n", fp); + git_object_free(obj); + } + } + + for (i = 0; i < count; i++) { + if (!(entry = git_tree_entry_byindex(tree, i)) || + !(entryname = git_tree_entry_name(entry))) + return -1; + joinpath(entrypath, sizeof(entrypath), path, entryname); + + r = snprintf(filepath, sizeof(filepath), "file/%s.html", + entrypath); + if (r < 0 || (size_t)r >= sizeof(filepath)) + errx(1, "path truncated: 'file/%s.html'", entrypath); + rf = snprintf(rawpath, sizeof(rawpath), "raw/%s", + entrypath); + if (rf < 0 || (size_t)rf >= sizeof(rawpath)) + errx(1, "path truncated: 'raw/%s'", entrypath); + + if (!git_tree_entry_to_object(&obj, repo, entry)) { + switch (git_object_type(obj)) { + case GIT_OBJ_BLOB: + is_obj_tree = 0; + filesize = git_blob_rawsize((git_blob *)obj); + lc = writeblob(obj, filepath, rawpath, entryname, filesize); + writeblobraw((git_blob *)obj, rawpath, entryname, filesize); break; + case GIT_OBJ_TREE: + continue; default: git_object_free(obj); continue; @@ -1196,9 +1236,6 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) fprintf(fp, "<td id=\"dir-name\"><a href=\"%s", relpath); else fprintf(fp, "<td id=\"file-name\"><a href=\"%s", relpath); - /* fputs("id=\"files-dir\" ", fp); */ - - /* fprintf(fp, "href=\"%s", relpath); */ percentencode(fp, filepath, strlen(filepath)); fputs("\">", fp);