@matheusbsbs / matheus-bagliano / commits / 8289b62095
Fork 0

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>

Pierre Habouzit committed Sep 24, 2007 at 11:25 UTC 8289b6209552a57c255561a2585d0edbe96d62d3
1 file changed +18 -25
builtin-rerere.c
+18 -25
@@ -72,13 +72,9 @@ static int handle_file(const char *path,
72 SHA_CTX ctx;
73 char buf[1024];
74 int hunk = 0, hunk_no = 0;
75 - struct strbuf minus, plus;
76 - struct strbuf *one = &minus, *two = &plus;
75 + struct strbuf one, two;
76 FILE *f = fopen(path, "r");
78 - FILE *out;
79 -
80 - strbuf_init(&minus, 0);
81 - strbuf_init(&plus, 0);
77 + FILE *out = NULL;
78
79 if (!f)
80 return error("Could not open %s", path);
@@ -89,51 +85,48 @@ static int handle_file(const char *path,
85 fclose(f);
86 return error("Could not write %s", output);
87 }
92 - } else
93 - out = NULL;
88 + }
89
90 if (sha1)
91 SHA1_Init(&ctx);
92
93 + strbuf_init(&one, 0);
94 + strbuf_init(&two, 0);
95 while (fgets(buf, sizeof(buf), f)) {
96 if (!prefixcmp(buf, "<<<<<<< "))
97 hunk = 1;
98 else if (!prefixcmp(buf, "======="))
99 hunk = 2;
100 else if (!prefixcmp(buf, ">>>>>>> ")) {
104 - int one_is_longer = (one->len > two->len);
105 - int common_len = one_is_longer ? two->len : one->len;
106 - int cmp = memcmp(one->buf, two->buf, common_len);
101 + int cmp = strbuf_cmp(&one, &two);
102
103 hunk_no++;
104 hunk = 0;
110 - if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
111 - struct strbuf *swap = one;
112 - one = two;
113 - two = swap;
105 + if (cmp > 0) {
106 + strbuf_swap(&one, &two);
107 }
108 if (out) {
109 fputs("<<<<<<<\n", out);
117 - fwrite(one->buf, one->len, 1, out);
110 + fwrite(one.buf, one.len, 1, out);
111 fputs("=======\n", out);
119 - fwrite(two->buf, two->len, 1, out);
112 + fwrite(two.buf, two.len, 1, out);
113 fputs(">>>>>>>\n", out);
114 }
115 if (sha1) {
123 - SHA1_Update(&ctx, one->buf, one->len);
124 - SHA1_Update(&ctx, "\0", 1);
125 - SHA1_Update(&ctx, two->buf, two->len);
126 - SHA1_Update(&ctx, "\0", 1);
116 + SHA1_Update(&ctx, one.buf, one.len + 1);
117 + SHA1_Update(&ctx, two.buf, two.len + 1);
118 }
128 - strbuf_release(one);
129 - strbuf_release(two);
119 + strbuf_reset(&one);
120 + strbuf_reset(&two);
121 } else if (hunk == 1)
131 - strbuf_addstr(one, buf);
122 + strbuf_addstr(&one, buf);
123 else if (hunk == 2)
133 - strbuf_addstr(two, buf);
124 + strbuf_addstr(&two, buf);
125 else if (out)
126 fputs(buf, out);
127 }
128 + strbuf_release(&one);
129 + strbuf_release(&two);
130
131 fclose(f);
132 if (out)