rerere: prepare for customizable conflict marker length
This still uses the hardcoded conflict marker length of 7 but otherwise prepares the codepath to deal with customized marker length. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Jan 16, 2010 at 23:06 UTC
191f24171718e894cb5ba806bc45861ebac95e96
1 file changed
+44
-10
rerere.c
+44
-10
@@ -98,6 +98,28 @@ static void rerere_io_putstr(const char *str, struct rerere_io *io)
98
ferr_puts(str, io->output, &io->wrerror);
99
}
100
101
+static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
102
+{
103
+ char buf[64];
104
+
105
+ while (size) {
106
+ if (size < sizeof(buf) - 2) {
107
+ memset(buf, ch, size);
108
+ buf[size] = '\n';
109
+ buf[size + 1] = '\0';
110
+ size = 0;
111
+ } else {
112
+ int sz = sizeof(buf) - 1;
113
+ if (size <= sz)
114
+ sz -= (sz - size) + 1;
115
+ memset(buf, ch, sz);
116
+ buf[sz] = '\0';
117
+ size -= sz;
118
+ }
119
+ rerere_io_putstr(buf, io);
120
+ }
121
+}
122
+
123
static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
124
{
125
if (io->output)
@@ -115,7 +137,17 @@ static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
137
return strbuf_getwholeline(sb, io->input, '\n');
138
}
139
118
-static int handle_path(unsigned char *sha1, struct rerere_io *io)
140
+static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
141
+{
142
+ while (marker_size--)
143
+ if (*buf++ != marker_char)
144
+ return 0;
145
+ if (want_sp && *buf != ' ')
146
+ return 0;
147
+ return isspace(*buf);
148
+}
149
+
150
+static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
151
{
152
git_SHA_CTX ctx;
153
int hunk_no = 0;
@@ -129,30 +161,30 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io)
161
git_SHA1_Init(&ctx);
162
163
while (!io->getline(&buf, io)) {
132
- if (!prefixcmp(buf.buf, "<<<<<<< ")) {
164
+ if (is_cmarker(buf.buf, '<', marker_size, 1)) {
165
if (hunk != RR_CONTEXT)
166
goto bad;
167
hunk = RR_SIDE_1;
136
- } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
168
+ } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
169
if (hunk != RR_SIDE_1)
170
goto bad;
171
hunk = RR_ORIGINAL;
140
- } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
172
+ } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
173
if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
174
goto bad;
175
hunk = RR_SIDE_2;
144
- } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
176
+ } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
177
if (hunk != RR_SIDE_2)
178
goto bad;
179
if (strbuf_cmp(&one, &two) > 0)
180
strbuf_swap(&one, &two);
181
hunk_no++;
182
hunk = RR_CONTEXT;
151
- rerere_io_putstr("<<<<<<<\n", io);
183
+ rerere_io_putconflict('<', marker_size, io);
184
rerere_io_putmem(one.buf, one.len, io);
153
- rerere_io_putstr("=======\n", io);
185
+ rerere_io_putconflict('=', marker_size, io);
186
rerere_io_putmem(two.buf, two.len, io);
155
- rerere_io_putstr(">>>>>>>\n", io);
187
+ rerere_io_putconflict('>', marker_size, io);
188
if (sha1) {
189
git_SHA1_Update(&ctx, one.buf ? one.buf : "",
190
one.len + 1);
@@ -189,6 +221,7 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
221
{
222
int hunk_no = 0;
223
struct rerere_io_file io;
224
+ int marker_size = 7;
225
226
memset(&io, 0, sizeof(io));
227
io.io.getline = rerere_file_getline;
@@ -205,7 +238,7 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
238
}
239
}
240
208
- hunk_no = handle_path(sha1, (struct rerere_io *)&io);
241
+ hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
242
243
fclose(io.input);
244
if (io.io.wrerror)
@@ -255,6 +288,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
288
struct cache_entry *ce;
289
int pos, len, i, hunk_no;
290
struct rerere_io_mem io;
291
+ int marker_size = 7;
292
293
/*
294
* Reproduce the conflicted merge in-core
@@ -299,7 +333,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
333
strbuf_init(&io.input, 0);
334
strbuf_attach(&io.input, result.ptr, result.size, result.size);
335
302
- hunk_no = handle_path(sha1, (struct rerere_io *)&io);
336
+ hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
337
strbuf_release(&io.input);
338
if (io.io.output)
339
fclose(io.io.output);