1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* wiggle - apply rejected patches
*
* Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
* Copyright (C) 2010-2013 Neil Brown <neilb@suse.de>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
*
* Author: Neil Brown
* Email: <neilb@suse.de>
*/
/*
* split patch or merge files.
*/
#include "wiggle.h"
#include <stdlib.h>
/* skip 'cp' past the new '\n', or all the way to 'end' */
static void skip_eol(char **cp, char *end)
{
char *c = *cp;
while (c < end && *c != '\n')
c++;
if (c < end)
c++;
*cp = c;
}
/* copy one line, or to end, from 'cp' into the stream, extending
* the stream.
*/
static void copyline(struct stream *s, char **cp, char *end)
{
char *from = *cp;
char *to = s->body+s->len;
while (from < end && *from != '\n')
*to++ = *from++;
if (from < end)
*to++ = *from++;
s->len = to-s->body;
*cp = from;
}
int split_patch(struct stream f, struct stream *f1, struct stream *f2)
{
struct stream r1, r2;
int chunks = 0;
char *cp, *end;
int state = 0;
int acnt = 0, bcnt = 0;
int a, b, c, d;
int lineno = 0;
char before[100], after[100];
char func[100];
f1->body = f2->body = NULL;
r1.body = xmalloc(f.len);
r2.body = xmalloc(f.len);
r1.len = r2.len = 0;
func[0] = 0;
cp = f.body;
end = f.body+f.len;
while (cp < end) {
/* state:
* 0 not in a patch
* 1 first half of context
* 2 second half of context
* 3 unified
*/
lineno++;
switch (state) {
case 0:
if (sscanf(cp, "@@ -%99s +%99s @@%99[^\n]", before, after, func) >= 2) {
int ok = 1;
if (sscanf(before, "%d,%d", &a, &b) == 2)
acnt = b;
else if (sscanf(before, "%d", &a) == 1)
acnt = 1;
else
ok = 0;
if (sscanf(after, "%d,%d", &c, &d) == 2)
bcnt = d;
else if (sscanf(after, "%d", &c) == 1)
bcnt = 1;
else
ok = 0;
if (ok)
state = 3;
else
state = 0;
} else if (sscanf(cp, "*** %d,%d ****", &a, &b) == 2) {
acnt = b-a+1;
state = 1;
} else if (sscanf(cp, "--- %d,%d ----", &c, &d) == 2) {
bcnt = d-c+1;
state = 2;
} else
sscanf(cp, "***************%99[^\n]", func);
skip_eol(&cp, end);
if (state == 1 || state == 3) {
char *f;
char buf[20];
buf[0] = 0;
chunks++;
sprintf(buf+1, "%5d %5d %5d", chunks, a, acnt);
memcpy(r1.body+r1.len, buf, 18);
r1.len += 18;
f = func;
while (*f == ' ')
f++;
if (*f) {
r1.body[r1.len++] = ' ';
strcpy(r1.body + r1.len, f);
r1.len += strlen(f);
}
r1.body[r1.len++] = '\n';
r1.body[r1.len++] = '\0';
}
if (state == 2 || state == 3) {
char buf[20];
buf[0] = 0;
sprintf(buf+1, "%5d %5d %5d\n", chunks, c, bcnt);
memcpy(r2.body+r2.len, buf, 20);
r2.len += 20;
}
if (state)
func[0] = 0;
break;
case 1:
if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+')
&& cp[1] == ' ') {
cp += 2;
copyline(&r1, &cp, end);
acnt--;
if (acnt == 0)
state = 0;
} else {
fprintf(stderr, "%s: bad context patch at line %d\n",
Cmd, lineno);
return 0;
}
break;
case 2:
if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+')
&& cp[1] == ' ') {
cp += 2;
copyline(&r2, &cp, end);
bcnt--;
if (bcnt == 0)
state = 0;
} else {
fprintf(stderr, "%s: bad context patch/2 at line %d\n",
Cmd, lineno);
return 0;
}
break;
case 3:
if (*cp == ' ') {
char *cp2;
cp++;
cp2 = cp;
copyline(&r1, &cp, end);
copyline(&r2, &cp2, end);
acnt--; bcnt--;
} else if (*cp == '-') {
cp++;
copyline(&r1, &cp, end);
acnt--;
} else if (*cp == '+') {
cp++;
copyline(&r2, &cp, end);
bcnt--;
} else {
fprintf(stderr, "%s: bad unified patch at line %d\n",
Cmd, lineno);
return 0;
}
if (acnt <= 0 && bcnt <= 0)
state = 0;
break;
}
}
if (r1.len > f.len || r2.len > f.len)
abort();
*f1 = r1;
*f2 = r2;
return chunks;
}
/*
* extract parts of a "diff3 -m" or "wiggle -m" output
*/
int split_merge(struct stream f, struct stream *f1, struct stream *f2, struct stream *f3)
{
int state = 0;
char *cp, *end;
struct stream r1, r2, r3;
f1->body = NULL;
f2->body = NULL;
r1.body = xmalloc(f.len);
r2.body = xmalloc(f.len);
r3.body = xmalloc(f.len);
r1.len = r2.len = r3.len = 0;
cp = f.body;
end = f.body+f.len;
while (cp < end) {
/* state:
* 0 not in conflict
* 1 in file 1 of conflict
* 2 in file 2 of conflict
* 3 in file 3 of conflict
* 4 in file 2 but expecting 1/3 next
* 5 in file 1/3
*/
int len = end-cp;
switch (state) {
case 0:
if (len >= 8 &&
strncmp(cp, "<<<<<<<", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
char *peek;
state = 1;
skip_eol(&cp, end);
/* diff3 will do something a bit strange in
* the 1st and 3rd sections are the same.
* it reports
* <<<<<<<
* 2nd
* =======
* 1st and 3rd
* >>>>>>>
* Without a ||||||| at all.
* so to know if we are in '1' or '2', skip forward
* having a peek.
*/
peek = cp;
while (peek < end) {
if (end-peek >= 8 &&
(peek[7] == ' ' || peek[7] == '\n')) {
if (strncmp(peek, "|||||||", 7) == 0 ||
strncmp(peek, ">>>>>>>", 7) == 0)
break;
else if (strncmp(peek, "=======", 7) == 0) {
state = 4;
break;
}
}
skip_eol(&peek, end);
}
} else {
char *cp2 = cp;
copyline(&r1, &cp2, end);
cp2 = cp;
copyline(&r2, &cp2, end);
copyline(&r3, &cp, end);
}
break;
case 1:
if (len >= 8 &&
strncmp(cp, "|||||||", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
state = 2;
skip_eol(&cp, end);
} else
copyline(&r1, &cp, end);
break;
case 2:
if (len >= 8 &&
strncmp(cp, "=======", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
state = 3;
skip_eol(&cp, end);
} else
copyline(&r2, &cp, end);
break;
case 3:
if (len >= 8 &&
strncmp(cp, ">>>>>>>", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
state = 0;
skip_eol(&cp, end);
} else
copyline(&r3, &cp, end);
break;
case 4:
if (len >= 8 &&
strncmp(cp, "=======", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
state = 5;
skip_eol(&cp, end);
} else
copyline(&r2, &cp, end);
break;
case 5:
if (len >= 8 &&
strncmp(cp, ">>>>>>>", 7) == 0 &&
(cp[7] == ' ' || cp[7] == '\n')
) {
state = 0;
skip_eol(&cp, end);
} else {
char *t = cp;
copyline(&r1, &t, end);
copyline(&r3, &cp, end);
}
break;
}
}
if (cp > end)
abort();
*f1 = r1;
*f2 = r2;
*f3 = r3;
return state == 0;
}
|