git.strcat.st

/strcat/sgit.git/ - summarytreelogarchive

subject
added <span class=diff-add> & <span class=diff-del> for optional styling
commit
07c9b9ad6caeacedd141f906848c554a418367a0
date
2026-05-28T13:15:19Z
message
diff
 sgit.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/sgit.c b/sgit.c
index 06096ed..1ec46c7 100644
--- a/sgit.c
+++ b/sgit.c
@@ -105,6 +105,7 @@ static void	 usage(void);
 static void	 write_cache_head(const char *, const char *);
 static void	 write_html_escaped(FILE *, const char *);
 static void	 write_html_escaped_len(FILE *, const char *, size_t);
+static void	 write_diff_html(FILE *, const char *);
 static char	*write_shell_quoted(const char *);
 
 int
@@ -867,6 +868,38 @@ write_html_escaped_len(FILE *fp, const char *s, size_t len)
   }
 }
 
+static void
+write_diff_html(FILE *fp, const char *patch)
+{
+  const char *line, *nl;
+  size_t len;
+
+  line = patch;
+  while (*line != '\0') {
+      nl = strchr(line, '\n');
+      if (nl != NULL)
+          len = (size_t)(nl - line);
+      else
+          len = strlen(line);
+
+      if (len > 0 && line[0] == '+' && strncmp(line, "+++", 3) != 0)
+          fputs("<span class=\"diff-add\">", fp);
+      else if (len > 0 && line[0] == '-' && strncmp(line, "---", 3) != 0)
+          fputs("<span class=\"diff-del\">", fp);
+      else
+          fputs("<span>", fp);
+
+      write_html_escaped_len(fp, line, len);
+      fputs("</span>", fp);
+      if (nl != NULL)
+          fputc('\n', fp);
+
+      if (nl == NULL)
+          break;
+      line = nl + 1;
+  }
+}
+
 static void
 make_commit_page(const struct repo *r, const char *hash, const char *short_hash,
     const char *date, const char *author, const char *subject)
@@ -926,7 +959,7 @@ make_commit_page(const struct repo *r, const char *hash, const char *short_hash,
   write_html_escaped(fp, subject);
   fputs("</p>\n", fp);
   fputs("<h2>Diff</h2>\n<pre>", fp);
-  write_html_escaped(fp, patch);
+  write_diff_html(fp, patch);
   fputs("</pre>\n</body>\n</html>\n", fp);
 
   if (fclose(fp) == EOF)