stagit

Personal stagit fork
git clone git://git.alexwennerberg.com/stagit
Log | Files | Refs | README | LICENSE

commit 62ef4529659f69f73f23e118f7f5644b57b57866
parent 76d180b3de8eb5889cb203209cb0de6c53476197
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 19 Feb 2022 16:19:22 -0800

Add raw file link

Basically copied
https://git.oscarbenedito.com/stagit/commit/1e35b0fa08a98e532ede60f4add7a91f8030deb6.html#h0-2-4

Diffstat:
Mstagit.c | 38+++++++++++++++++++++++++++++++++-----
1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/stagit.c b/stagit.c @@ -590,6 +590,30 @@ writeblobhtml(FILE *fp, const git_blob *blob) } void +writeblobraw(const git_blob *blob, const char *fpath, const char *filename, git_off_t filesize) +{ + char tmp[PATH_MAX] = "", *d; + const char *p; + int lc = 0; + FILE *fp; + + if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp)) + errx(1, "path truncated: '%s'", fpath); + if (!(d = dirname(tmp))) + err(1, "dirname"); + mkdirp(d); + + for (p = fpath, tmp[0] = '\0'; *p; p++) { + if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp)) + errx(1, "path truncated: '../%s'", tmp); + } + + fp = efopen(fpath, "w"); + fwrite(git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob), 1, fp); + fclose(fp); +} + +void printcommit(FILE *fp, struct commitinfo *ci) { fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n", @@ -947,7 +971,7 @@ writeatom(FILE *fp, int all) } size_t -writeblob(git_object *obj, const char *fpath, const char *filename, size_t filesize) +writeblob(git_object *obj, const char *fpath, const char *rpath, const char *filename, size_t filesize) { char tmp[PATH_MAX] = "", *d; const char *p; @@ -972,7 +996,7 @@ writeblob(git_object *obj, const char *fpath, const char *filename, size_t files fputs("<p> ", fp); xmlencode(fp, filename, strlen(filename)); fprintf(fp, " (%zuB)", filesize); - fputs("</p><hr/>", fp); + fprintf(fp, " - <a href=\"%s%s\">raw</a></p><hr/>", relpath, rpath); if (git_blob_is_binary((git_blob *)obj)) fputs("<p>Binary file.</p>\n", fp); @@ -1036,9 +1060,9 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) const git_tree_entry *entry = NULL; git_object *obj = NULL; const char *entryname; - char filepath[PATH_MAX], entrypath[PATH_MAX], oid[8]; + char filepath[PATH_MAX], rawpath[PATH_MAX], entrypath[PATH_MAX], oid[8]; size_t count, i, lc, filesize; - int r, ret; + int r, rf, ret; count = git_tree_entrycount(tree); for (i = 0; i < count; i++) { @@ -1052,6 +1076,9 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) 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: @@ -1070,7 +1097,8 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) } filesize = git_blob_rawsize((git_blob *)obj); - lc = writeblob(obj, filepath, entryname, filesize); + lc = writeblob(obj, filepath, rawpath, entryname, filesize); + writeblobraw((git_blob *)obj, rawpath, entryname, filesize); fputs("<tr><td>", fp); fputs(filemode(git_tree_entry_filemode(entry)), fp);