@matheusbsbs / matheus-bagliano / commits / cc58d7dfdd
Fork 0

rerere.c: use symbolic constants to keep track of parsing states

These hardcoded integers make the code harder to follow than necessary; replace them with enums to make it easier to read, before adding support for optionally parsing "diff3 -m" style conflict markers. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 29, 2008 at 10:12 UTC cc58d7dfddc842360f6aade1507e1063d8cd0f40
1 file changed +13 -10
rerere.c
+13 -10
@@ -75,7 +75,10 @@ static int handle_file(const char *path,
75 {
76 SHA_CTX ctx;
77 char buf[1024];
78 - int hunk = 0, hunk_no = 0;
78 + int hunk_no = 0;
79 + enum {
80 + RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2,
81 + } hunk = RR_CONTEXT;
82 struct strbuf one, two;
83 FILE *f = fopen(path, "r");
84 FILE *out = NULL;
@@ -98,20 +101,20 @@ static int handle_file(const char *path,
101 strbuf_init(&two, 0);
102 while (fgets(buf, sizeof(buf), f)) {
103 if (!prefixcmp(buf, "<<<<<<< ")) {
101 - if (hunk)
104 + if (hunk != RR_CONTEXT)
105 goto bad;
103 - hunk = 1;
106 + hunk = RR_SIDE_1;
107 } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
105 - if (hunk != 1)
108 + if (hunk != RR_SIDE_1)
109 goto bad;
107 - hunk = 2;
110 + hunk = RR_SIDE_2;
111 } else if (!prefixcmp(buf, ">>>>>>> ")) {
109 - if (hunk != 2)
112 + if (hunk != RR_SIDE_2)
113 goto bad;
114 if (strbuf_cmp(&one, &two) > 0)
115 strbuf_swap(&one, &two);
116 hunk_no++;
114 - hunk = 0;
117 + hunk = RR_CONTEXT;
118 if (out) {
119 fputs("<<<<<<<\n", out);
120 fwrite(one.buf, one.len, 1, out);
@@ -127,9 +130,9 @@ static int handle_file(const char *path,
130 }
131 strbuf_reset(&one);
132 strbuf_reset(&two);
130 - } else if (hunk == 1)
133 + } else if (hunk == RR_SIDE_1)
134 strbuf_addstr(&one, buf);
132 - else if (hunk == 2)
135 + else if (hunk == RR_SIDE_2)
136 strbuf_addstr(&two, buf);
137 else if (out)
138 fputs(buf, out);
@@ -146,7 +149,7 @@ static int handle_file(const char *path,
149 fclose(out);
150 if (sha1)
151 SHA1_Final(sha1, &ctx);
149 - if (hunk) {
152 + if (hunk != RR_CONTEXT) {
153 if (output)
154 unlink(output);
155 return error("Could not parse conflict hunks in %s", path);