32
static int no_add = 0;
33
static int show_index_info = 0;
34
static int line_termination = '\n';
35
+static unsigned long p_context = -1;
36
static const char apply_usage[] =
36
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
37
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
38
39
static enum whitespace_eol {
40
nowarn_whitespace,
101
static int linenr = 1;
102
103
struct fragment {
104
+ unsigned long leading, trailing;
105
unsigned long oldpos, oldlines;
106
unsigned long newpos, newlines;
107
const char *patch;
819
int added, deleted;
820
int len = linelen(line, size), offset;
821
unsigned long oldlines, newlines;
822
+ unsigned long leading, trailing;
823
824
offset = parse_fragment_header(line, len, fragment);
825
if (offset < 0)
826
return -1;
827
oldlines = fragment->oldlines;
828
newlines = fragment->newlines;
829
+ leading = 0;
830
+ trailing = 0;
831
832
if (patch->is_new < 0) {
833
patch->is_new = !oldlines;
865
case ' ':
866
oldlines--;
867
newlines--;
868
+ if (!deleted && !added)
869
+ leading++;
870
+ trailing++;
871
break;
872
case '-':
873
deleted++;
874
oldlines--;
875
+ trailing = 0;
876
break;
877
case '+':
878
/*
896
}
897
added++;
898
newlines--;
899
+ trailing = 0;
900
break;
901
902
/* We allow "\ No newline at end of file". Depending
914
}
915
if (oldlines || newlines)
916
return -1;
917
+ fragment->leading = leading;
918
+ fragment->trailing = trailing;
919
+
920
/* If a fragment ends with an incomplete line, we failed to include
921
* it in the above loop because we hit oldlines == newlines == 0
922
* before seeing it.
1100
}
1101
}
1102
1090
-static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line)
1103
+static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1104
{
1105
int i;
1106
unsigned long start, backwards, forwards;
1161
n = (i >> 1)+1;
1162
if (i & 1)
1163
n = -n;
1164
+ *lines = n;
1165
return try;
1166
}
1167
1171
return -1;
1172
}
1173
1174
+static void remove_first_line(const char **rbuf, int *rsize)
1175
+{
1176
+ const char *buf = *rbuf;
1177
+ int size = *rsize;
1178
+ unsigned long offset;
1179
+ offset = 0;
1180
+ while (offset <= size) {
1181
+ if (buf[offset++] == '\n')
1182
+ break;
1183
+ }
1184
+ *rsize = size - offset;
1185
+ *rbuf = buf + offset;
1186
+}
1187
+
1188
+static void remove_last_line(const char **rbuf, int *rsize)
1189
+{
1190
+ const char *buf = *rbuf;
1191
+ int size = *rsize;
1192
+ unsigned long offset;
1193
+ offset = size - 1;
1194
+ while (offset > 0) {
1195
+ if (buf[--offset] == '\n')
1196
+ break;
1197
+ }
1198
+ *rsize = offset + 1;
1199
+}
1200
+
1201
struct buffer_desc {
1202
char *buffer;
1203
unsigned long size;
1233
int offset, size = frag->size;
1234
char *old = xmalloc(size);
1235
char *new = xmalloc(size);
1236
+ const char *oldlines, *newlines;
1237
int oldsize = 0, newsize = 0;
1238
+ unsigned long leading, trailing;
1239
+ int pos, lines;
1240
1241
while (size > 0) {
1242
int len = linelen(patch, size);
1285
newsize--;
1286
}
1287
#endif
1244
-
1245
- offset = find_offset(buf, desc->size, old, oldsize, frag->newpos);
1246
- if (offset >= 0) {
1247
- int diff = newsize - oldsize;
1248
- unsigned long size = desc->size + diff;
1249
- unsigned long alloc = desc->alloc;
1250
-
1251
- if (size > alloc) {
1252
- alloc = size + 8192;
1253
- desc->alloc = alloc;
1254
- buf = xrealloc(buf, alloc);
1255
- desc->buffer = buf;
1288
+
1289
+ oldlines = old;
1290
+ newlines = new;
1291
+ leading = frag->leading;
1292
+ trailing = frag->trailing;
1293
+ lines = 0;
1294
+ pos = frag->newpos;
1295
+ for (;;) {
1296
+ offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1297
+ if (offset >= 0) {
1298
+ int diff = newsize - oldsize;
1299
+ unsigned long size = desc->size + diff;
1300
+ unsigned long alloc = desc->alloc;
1301
+
1302
+ /* Warn if it was necessary to reduce the number
1303
+ * of context lines.
1304
+ */
1305
+ if ((leading != frag->leading) || (trailing != frag->trailing))
1306
+ fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1307
+ leading, trailing, pos + lines);
1308
+
1309
+ if (size > alloc) {
1310
+ alloc = size + 8192;
1311
+ desc->alloc = alloc;
1312
+ buf = xrealloc(buf, alloc);
1313
+ desc->buffer = buf;
1314
+ }
1315
+ desc->size = size;
1316
+ memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1317
+ memcpy(buf + offset, newlines, newsize);
1318
+ offset = 0;
1319
+
1320
+ break;
1321
+ }
1322
+
1323
+ /* Am I at my context limits? */
1324
+ if ((leading <= p_context) && (trailing <= p_context))
1325
+ break;
1326
+ /* Reduce the number of context lines
1327
+ * Reduce both leading and trailing if they are equal
1328
+ * otherwise just reduce the larger context.
1329
+ */
1330
+ if (leading >= trailing) {
1331
+ remove_first_line(&oldlines, &oldsize);
1332
+ remove_first_line(&newlines, &newsize);
1333
+ pos--;
1334
+ leading--;
1335
+ }
1336
+ if (trailing > leading) {
1337
+ remove_last_line(&oldlines, &oldsize);
1338
+ remove_last_line(&newlines, &newsize);
1339
+ trailing--;
1340
}
1257
- desc->size = size;
1258
- memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1259
- memcpy(buf + offset, new, newsize);
1260
- offset = 0;
1341
}
1342
1343
free(old);
1962
1963
for (i = 1; i < argc; i++) {
1964
const char *arg = argv[i];
1965
+ char *end;
1966
int fd;
1967
1968
if (!strcmp(arg, "-")) {
2026
line_termination = 0;
2027
continue;
2028
}
2029
+ if (!strncmp(arg, "-C", 2)) {
2030
+ p_context = strtoul(arg + 2, &end, 0);
2031
+ if (*end != '\0')
2032
+ die("unrecognized context count '%s'", arg + 2);
2033
+ continue;
2034
+ }
2035
if (!strncmp(arg, "--whitespace=", 13)) {
2036
whitespace_option = arg + 13;
2037
parse_whitespace_option(arg + 13);