add function to print a single line, ignoring \r and \n
This can happen when there is no newline at end of file in the diff which is served by libgit2 as: "\n\ No newline at end of file\n".
1 files changed, 22 insertions(+), 1 deletions(-) | |||
---|---|---|---|
M | stagit.c | +22 | -1 |
1@@ -377,6 +377,26 @@ xmlencode(FILE *fp, const char *s, size_t len)
2 }
3 }
4
5+/* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */
6+void
7+xmlencodeline(FILE *fp, const char *s, size_t len)
8+{
9+ size_t i;
10+
11+ for (i = 0; *s && i < len; s++, i++) {
12+ switch(*s) {
13+ case '<': fputs("<", fp); break;
14+ case '>': fputs(">", fp); break;
15+ case '\'': fputs("'", fp); break;
16+ case '&': fputs("&", fp); break;
17+ case '"': fputs(""", fp); break;
18+ case '\r': break; /* ignore CR */
19+ case '\n': break; /* ignore LF */
20+ default: putc(*s, fp);
21+ }
22+ }
23+}
24+
25 int
26 mkdirp(const char *path)
27 {
28@@ -678,7 +698,8 @@ printshowfile(FILE *fp, struct commitinfo *ci)
29 i, j, k, i, j, k);
30 else
31 putc(' ', fp);
32- xmlencode(fp, line->content, line->content_len);
33+ xmlencodeline(fp, line->content, line->content_len);
34+ putc('\n', fp);
35 if (line->old_lineno == -1 || line->new_lineno == -1)
36 fputs("</a>", fp);
37 }