Make builtin-rerere use of strbuf nicer and more efficient.

memory is now reused across hunks.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Pierre Habouzit 2007-09-24 11:25:04 +02:00 committed by Junio C Hamano
parent 45f66f6463
commit 8289b62095

View File

@ -72,13 +72,9 @@ static int handle_file(const char *path,
SHA_CTX ctx; SHA_CTX ctx;
char buf[1024]; char buf[1024];
int hunk = 0, hunk_no = 0; int hunk = 0, hunk_no = 0;
struct strbuf minus, plus; struct strbuf one, two;
struct strbuf *one = &minus, *two = &plus;
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
FILE *out; FILE *out = NULL;
strbuf_init(&minus, 0);
strbuf_init(&plus, 0);
if (!f) if (!f)
return error("Could not open %s", path); return error("Could not open %s", path);
@ -89,51 +85,48 @@ static int handle_file(const char *path,
fclose(f); fclose(f);
return error("Could not write %s", output); return error("Could not write %s", output);
} }
} else }
out = NULL;
if (sha1) if (sha1)
SHA1_Init(&ctx); SHA1_Init(&ctx);
strbuf_init(&one, 0);
strbuf_init(&two, 0);
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
if (!prefixcmp(buf, "<<<<<<< ")) if (!prefixcmp(buf, "<<<<<<< "))
hunk = 1; hunk = 1;
else if (!prefixcmp(buf, "=======")) else if (!prefixcmp(buf, "======="))
hunk = 2; hunk = 2;
else if (!prefixcmp(buf, ">>>>>>> ")) { else if (!prefixcmp(buf, ">>>>>>> ")) {
int one_is_longer = (one->len > two->len); int cmp = strbuf_cmp(&one, &two);
int common_len = one_is_longer ? two->len : one->len;
int cmp = memcmp(one->buf, two->buf, common_len);
hunk_no++; hunk_no++;
hunk = 0; hunk = 0;
if ((cmp > 0) || ((cmp == 0) && one_is_longer)) { if (cmp > 0) {
struct strbuf *swap = one; strbuf_swap(&one, &two);
one = two;
two = swap;
} }
if (out) { if (out) {
fputs("<<<<<<<\n", out); fputs("<<<<<<<\n", out);
fwrite(one->buf, one->len, 1, out); fwrite(one.buf, one.len, 1, out);
fputs("=======\n", out); fputs("=======\n", out);
fwrite(two->buf, two->len, 1, out); fwrite(two.buf, two.len, 1, out);
fputs(">>>>>>>\n", out); fputs(">>>>>>>\n", out);
} }
if (sha1) { if (sha1) {
SHA1_Update(&ctx, one->buf, one->len); SHA1_Update(&ctx, one.buf, one.len + 1);
SHA1_Update(&ctx, "\0", 1); SHA1_Update(&ctx, two.buf, two.len + 1);
SHA1_Update(&ctx, two->buf, two->len);
SHA1_Update(&ctx, "\0", 1);
} }
strbuf_release(one); strbuf_reset(&one);
strbuf_release(two); strbuf_reset(&two);
} else if (hunk == 1) } else if (hunk == 1)
strbuf_addstr(one, buf); strbuf_addstr(&one, buf);
else if (hunk == 2) else if (hunk == 2)
strbuf_addstr(two, buf); strbuf_addstr(&two, buf);
else if (out) else if (out)
fputs(buf, out); fputs(buf, out);
} }
strbuf_release(&one);
strbuf_release(&two);
fclose(f); fclose(f);
if (out) if (out)