commit 26d14000ec0afec9f60302b80842dc1994aeb6c4
parent c3b2788c29dd328ea34a399f891abf566c2e56ec
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Fri, 4 Sep 2026 22:04:17 -0300
If binary file is image show it in place
Diffstat:
| M | stagit.c | | | 35 | ++++++++++++++++++++++++++++++++--- |
1 file changed, 32 insertions(+), 3 deletions(-)
diff --git a/stagit.c b/stagit.c
@@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <time.h>
#include <unistd.h>
@@ -1075,6 +1076,25 @@ writeblobmd(FILE *fp, const git_blob *blob, const char *rawprefix)
"</script>\n", fp);
}
+int
+isimagefile(const char *filename)
+{
+ const char *ext;
+
+ ext = strrchr(filename, '.');
+ if (!ext)
+ return 0;
+
+ if (!strcasecmp(ext, ".png") || !strcasecmp(ext, ".jpg") ||
+ !strcasecmp(ext, ".jpeg") || !strcasecmp(ext, ".gif") ||
+ !strcasecmp(ext, ".svg") || !strcasecmp(ext, ".webp") ||
+ !strcasecmp(ext, ".bmp") || !strcasecmp(ext, ".ico") ||
+ !strcasecmp(ext, ".avif"))
+ return 1;
+
+ return 0;
+}
+
const char *
formatsize(size_t size)
{
@@ -1192,9 +1212,18 @@ writeblob(git_object *obj, const char *fpath, const char *rpath, const char *fil
fprintf(fp, "<div id=\"file-raw\"><a href=\"%s%s\">raw</a></div></div>", relpath, rpath);
/* fputs("<hr>\n", fp); */
- if (git_blob_is_binary((git_blob *)obj))
- fputs("<p id=\"binary-file\">Binary file.</p>\n", fp);
- else if (ismarkdownfile(filename)) {
+ if (isbin) {
+ if (isimagefile(filename)) {
+ fputs("<p id=\"binary-file\"><img src=\"", fp);
+ fputs(relpath, fp);
+ percentencode(fp, rpath, strlen(rpath));
+ fputs("\" alt=\"", fp);
+ xmlencode(fp, filename, strlen(filename));
+ fputs("\" loading=\"lazy\" /></p>\n", fp);
+ } else {
+ fputs("<p id=\"binary-file\">Binary file.</p>\n", fp);
+ }
+ } else if (ismarkdownfile(filename)) {
fputs("<div id=\"md-view\">", fp);
writeblobmd(fp, (git_blob *)obj, relpath);
fputs("</div>\n", fp);