Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Join a diff line's pieces within one call
Xdiff hands out_line a whole line each time, as the prefix, the record and any no-newline marker, and the fragment was already flushed before the call returned, so nothing ever carried over to the next call.
Diffstat (limited to '')
-rw-r--r--source/shared.c40
1 file changed, 8 insertions, 32 deletions
diff --git a/source/shared.c b/source/shared.c
index 3c61828..2add244 100644
--- a/source/shared.c
+++ b/source/shared.c
@@ -25,11 +25,6 @@ struct cgit_repolist cgit_repolist;
struct cgit_context ctx;
char cgit_default_repo_desc[] = "[no description]";
-// A diff line that arrived in pieces and is still waiting for its end, see
-// emit_line.
-static char *fragment;
-static int fragment_len;
-
static void free_refinfo(struct refinfo *ref)
{
free((char *)ref->refname);
@@ -116,39 +111,20 @@ static void release_mmfile(mmfile_t *file, const struct object_id *oid)
}
/*
- * Xdiff emits buffers that need not end on a line boundary, so a trailing
- * fragment is held back and joined with whatever arrives next.
+ * Xdiff hands over one line per call, in pieces that are the prefix, the
+ * record and, for a record with no newline, the marker saying so. They are
+ * joined here before the line reaches fn.
*/
static int emit_line(void *priv, mmbuffer_t *mb, int nbuf)
{
linediff_fn emit = *(linediff_fn *)priv;
+ struct strbuf line = STRBUF_INIT;
int i;
- for (i = 0; i < nbuf; i++) {
- if (mb[i].ptr[mb[i].size - 1] != '\n') {
- fragment = xrealloc(fragment, fragment_len + mb[i].size);
- memcpy(fragment + fragment_len, mb[i].ptr, mb[i].size);
- fragment_len += mb[i].size;
- continue;
- }
-
- if (!fragment) {
- emit(mb[i].ptr, mb[i].size);
- continue;
- }
- fragment = xrealloc(fragment, fragment_len + mb[i].size);
- memcpy(fragment + fragment_len, mb[i].ptr, mb[i].size);
- emit(fragment, fragment_len + mb[i].size);
- free(fragment);
- fragment = NULL;
- fragment_len = 0;
- }
- if (fragment) {
- emit(fragment, fragment_len);
- free(fragment);
- fragment = NULL;
- fragment_len = 0;
- }
+ for (i = 0; i < nbuf; i++)
+ strbuf_add(&line, mb[i].ptr, mb[i].size);
+ emit(line.buf, line.len);
+ strbuf_release(&line);
return 0;
}