commit 580956c405a2ceba28271090cde3cd19dfe1f037
parent 96d88a5947be059369d206d51e6288fab37a1ad0
Author: gearsix <gearsix@tuta.io>
Date: Tue, 18 Feb 2025 23:33:57 +0000
write a raw source file next to the html source file
writeblob now takes an ftype arg, 0 for html, 1 for raw.
The function acts as before with 0 and with 1 will just
write the git_blob_rawcontent to the file and return -1.
errx is called if ftype is not 0 or 1.
param fix to a mistake made in stagit-index during merge.
Diffstat:
2 files changed, 32 insertions(+), 17 deletions(-)
diff --git a/stagit-index.c b/stagit-index.c
@@ -298,7 +298,7 @@ main(int argc, char *argv[])
fputs("<details open><summary><b>forks</b></summary>", stdout);
writebodyhead(stdout);
for (i = 1; i < argc; i++) {
- if ( writebody(stdout, argv[i], 0) != 0 ) {
+ if ( writebody(stdout, argv[i], 1) != 0 ) {
ret = 1;
continue;
}
diff --git a/stagit.c b/stagit.c
@@ -561,12 +561,24 @@ writefooter(FILE *fp)
}
size_t
-writeblobhtml(FILE *fp, const git_blob *blob)
+writeblobhtml(FILE *fp, const char *filename, size_t filesize, const git_blob *blob)
{
size_t n = 0, i, len, prev;
const char *nfmt = "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">%7zu</a> ";
const char *s = git_blob_rawcontent(blob);
+ writeheader(fp, filename);
+ fputs("<p>", fp);
+ xmlencode(fp, filename, strlen(filename));
+ fputs(" (<a href=\"./", fp);
+ xmlencode(fp, filename, strlen(filename));
+ fputs("\">raw</a>)", fp);
+ fprintf(fp, " (%zuB)", filesize);
+ fputs("</p><hr/>", fp);
+
+ if (git_blob_is_binary(blob))
+ fputs("<p>Binary file.</p>\n", fp);
+
len = git_blob_rawsize(blob);
fputs("<pre id=\"blob\">\n", fp);
@@ -589,6 +601,7 @@ writeblobhtml(FILE *fp, const git_blob *blob)
}
fputs("</pre>\n", fp);
+ writefooter(fp);
return n;
}
@@ -952,7 +965,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, int ftype, const char *filename, size_t filesize)
{
char tmp[PATH_MAX] = "", *d;
const char *p;
@@ -973,18 +986,14 @@ writeblob(git_object *obj, const char *fpath, const char *filename, size_t files
relpath = tmp;
fp = efopen(fpath, "w");
- writeheader(fp, filename);
- fputs("<p> ", fp);
- xmlencode(fp, filename, strlen(filename));
- fprintf(fp, " (%zuB)", filesize);
- fputs("</p><hr/>", fp);
-
- if (git_blob_is_binary((git_blob *)obj))
- fputs("<p>Binary file.</p>\n", fp);
- else
- lc = writeblobhtml(fp, (git_blob *)obj);
-
- writefooter(fp);
+ switch (ftype) {
+ case 0: lc = writeblobhtml(fp, filename, filesize, (git_blob *)obj); break;
+ case 1:
+ fputs(git_blob_rawcontent((git_blob *)obj), fp);
+ lc = -1;
+ break;
+ default: errx(1, "invalid ftype passed to writeblob: %d", ftype); break;
+ }
checkfileerror(fp, fpath, 'w');
fclose(fp);
@@ -1041,7 +1050,7 @@ 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], filepath_raw[PATH_MAX], entrypath[PATH_MAX], oid[8];
size_t count, i, lc, filesize;
int r, ret;
@@ -1057,6 +1066,11 @@ 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);
+ r = snprintf(filepath_raw, sizeof(filepath_raw), "file/%s",
+ entrypath);
+ if (r < 0 || (size_t)r >= sizeof(filepath_raw))
+ errx(1, "path truncated: 'file/%s'", entrypath);
+
if (!git_tree_entry_to_object(&obj, repo, entry)) {
switch (git_object_type(obj)) {
case GIT_OBJ_BLOB:
@@ -1075,7 +1089,8 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
}
filesize = git_blob_rawsize((git_blob *)obj);
- lc = writeblob(obj, filepath, entryname, filesize);
+ writeblob(obj, filepath_raw, 1, entryname, filesize);
+ lc = writeblob(obj, filepath, 0, entryname, filesize);
fputs("<tr><td>", fp);
fputs(filemode(git_tree_entry_filemode(entry)), fp);