apply: move libified code from builtin/apply.c to apply.{c,h}

As most of the apply code in builtin/apply.c has been libified by a number of previous commits, it can now be moved to apply.{c,h}, so that more code can use it. Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Apr 22, 2016 at 20:55 UTC 13b5af22f39f5e7d952a4c98ffb7ea25053800c1
3 files changed +4751 -4732
apply.c
+4731
@@ -1,5 +1,23 @@
1 +/*
2 + * apply.c
3 + *
4 + * Copyright (C) Linus Torvalds, 2005
5 + *
6 + * This applies patches on top of some (arbitrary) version of the SCM.
7 + *
8 + */
9 +
10 #include "cache.h"
11 +#include "blob.h"
12 +#include "delta.h"
13 +#include "diff.h"
14 +#include "dir.h"
15 +#include "xdiff-interface.h"
16 +#include "ll-merge.h"
17 #include "lockfile.h"
18 +#include "parse-options.h"
19 +#include "quote.h"
20 +#include "rerere.h"
21 #include "apply.h"
22
23 static void git_apply_config(void)
@@ -125,3 +143,4716 @@ int check_apply_state(struct apply_state *state, int force_apply)
143
144 return 0;
145 }
146 +
147 +static void set_default_whitespace_mode(struct apply_state *state)
148 +{
149 + if (!state->whitespace_option && !apply_default_whitespace)
150 + state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
151 +}
152 +
153 +/*
154 + * This represents one "hunk" from a patch, starting with
155 + * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
156 + * patch text is pointed at by patch, and its byte length
157 + * is stored in size. leading and trailing are the number
158 + * of context lines.
159 + */
160 +struct fragment {
161 + unsigned long leading, trailing;
162 + unsigned long oldpos, oldlines;
163 + unsigned long newpos, newlines;
164 + /*
165 + * 'patch' is usually borrowed from buf in apply_patch(),
166 + * but some codepaths store an allocated buffer.
167 + */
168 + const char *patch;
169 + unsigned free_patch:1,
170 + rejected:1;
171 + int size;
172 + int linenr;
173 + struct fragment *next;
174 +};
175 +
176 +/*
177 + * When dealing with a binary patch, we reuse "leading" field
178 + * to store the type of the binary hunk, either deflated "delta"
179 + * or deflated "literal".
180 + */
181 +#define binary_patch_method leading
182 +#define BINARY_DELTA_DEFLATED 1
183 +#define BINARY_LITERAL_DEFLATED 2
184 +
185 +/*
186 + * This represents a "patch" to a file, both metainfo changes
187 + * such as creation/deletion, filemode and content changes represented
188 + * as a series of fragments.
189 + */
190 +struct patch {
191 + char *new_name, *old_name, *def_name;
192 + unsigned int old_mode, new_mode;
193 + int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
194 + int rejected;
195 + unsigned ws_rule;
196 + int lines_added, lines_deleted;
197 + int score;
198 + unsigned int is_toplevel_relative:1;
199 + unsigned int inaccurate_eof:1;
200 + unsigned int is_binary:1;
201 + unsigned int is_copy:1;
202 + unsigned int is_rename:1;
203 + unsigned int recount:1;
204 + unsigned int conflicted_threeway:1;
205 + unsigned int direct_to_threeway:1;
206 + struct fragment *fragments;
207 + char *result;
208 + size_t resultsize;
209 + char old_sha1_prefix[41];
210 + char new_sha1_prefix[41];
211 + struct patch *next;
212 +
213 + /* three-way fallback result */
214 + struct object_id threeway_stage[3];
215 +};
216 +
217 +static void free_fragment_list(struct fragment *list)
218 +{
219 + while (list) {
220 + struct fragment *next = list->next;
221 + if (list->free_patch)
222 + free((char *)list->patch);
223 + free(list);
224 + list = next;
225 + }
226 +}
227 +
228 +static void free_patch(struct patch *patch)
229 +{
230 + free_fragment_list(patch->fragments);
231 + free(patch->def_name);
232 + free(patch->old_name);
233 + free(patch->new_name);
234 + free(patch->result);
235 + free(patch);
236 +}
237 +
238 +static void free_patch_list(struct patch *list)
239 +{
240 + while (list) {
241 + struct patch *next = list->next;
242 + free_patch(list);
243 + list = next;
244 + }
245 +}
246 +
247 +/*
248 + * A line in a file, len-bytes long (includes the terminating LF,
249 + * except for an incomplete line at the end if the file ends with
250 + * one), and its contents hashes to 'hash'.
251 + */
252 +struct line {
253 + size_t len;
254 + unsigned hash : 24;
255 + unsigned flag : 8;
256 +#define LINE_COMMON 1
257 +#define LINE_PATCHED 2
258 +};
259 +
260 +/*
261 + * This represents a "file", which is an array of "lines".
262 + */
263 +struct image {
264 + char *buf;
265 + size_t len;
266 + size_t nr;
267 + size_t alloc;
268 + struct line *line_allocated;
269 + struct line *line;
270 +};
271 +
272 +static uint32_t hash_line(const char *cp, size_t len)
273 +{
274 + size_t i;
275 + uint32_t h;
276 + for (i = 0, h = 0; i < len; i++) {
277 + if (!isspace(cp[i])) {
278 + h = h * 3 + (cp[i] & 0xff);
279 + }
280 + }
281 + return h;
282 +}
283 +
284 +/*
285 + * Compare lines s1 of length n1 and s2 of length n2, ignoring
286 + * whitespace difference. Returns 1 if they match, 0 otherwise
287 + */
288 +static int fuzzy_matchlines(const char *s1, size_t n1,
289 + const char *s2, size_t n2)
290 +{
291 + const char *last1 = s1 + n1 - 1;
292 + const char *last2 = s2 + n2 - 1;
293 + int result = 0;
294 +
295 + /* ignore line endings */
296 + while ((*last1 == '\r') || (*last1 == '\n'))
297 + last1--;
298 + while ((*last2 == '\r') || (*last2 == '\n'))
299 + last2--;
300 +
301 + /* skip leading whitespaces, if both begin with whitespace */
302 + if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
303 + while (isspace(*s1) && (s1 <= last1))
304 + s1++;
305 + while (isspace(*s2) && (s2 <= last2))
306 + s2++;
307 + }
308 + /* early return if both lines are empty */
309 + if ((s1 > last1) && (s2 > last2))
310 + return 1;
311 + while (!result) {
312 + result = *s1++ - *s2++;
313 + /*
314 + * Skip whitespace inside. We check for whitespace on
315 + * both buffers because we don't want "a b" to match
316 + * "ab"
317 + */
318 + if (isspace(*s1) && isspace(*s2)) {
319 + while (isspace(*s1) && s1 <= last1)
320 + s1++;
321 + while (isspace(*s2) && s2 <= last2)
322 + s2++;
323 + }
324 + /*
325 + * If we reached the end on one side only,
326 + * lines don't match
327 + */
328 + if (
329 + ((s2 > last2) && (s1 <= last1)) ||
330 + ((s1 > last1) && (s2 <= last2)))
331 + return 0;
332 + if ((s1 > last1) && (s2 > last2))
333 + break;
334 + }
335 +
336 + return !result;
337 +}
338 +
339 +static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
340 +{
341 + ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
342 + img->line_allocated[img->nr].len = len;
343 + img->line_allocated[img->nr].hash = hash_line(bol, len);
344 + img->line_allocated[img->nr].flag = flag;
345 + img->nr++;
346 +}
347 +
348 +/*
349 + * "buf" has the file contents to be patched (read from various sources).
350 + * attach it to "image" and add line-based index to it.
351 + * "image" now owns the "buf".
352 + */
353 +static void prepare_image(struct image *image, char *buf, size_t len,
354 + int prepare_linetable)
355 +{
356 + const char *cp, *ep;
357 +
358 + memset(image, 0, sizeof(*image));
359 + image->buf = buf;
360 + image->len = len;
361 +
362 + if (!prepare_linetable)
363 + return;
364 +
365 + ep = image->buf + image->len;
366 + cp = image->buf;
367 + while (cp < ep) {
368 + const char *next;
369 + for (next = cp; next < ep && *next != '\n'; next++)
370 + ;
371 + if (next < ep)
372 + next++;
373 + add_line_info(image, cp, next - cp, 0);
374 + cp = next;
375 + }
376 + image->line = image->line_allocated;
377 +}
378 +
379 +static void clear_image(struct image *image)
380 +{
381 + free(image->buf);
382 + free(image->line_allocated);
383 + memset(image, 0, sizeof(*image));
384 +}
385 +
386 +/* fmt must contain _one_ %s and no other substitution */
387 +static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
388 +{
389 + struct strbuf sb = STRBUF_INIT;
390 +
391 + if (patch->old_name && patch->new_name &&
392 + strcmp(patch->old_name, patch->new_name)) {
393 + quote_c_style(patch->old_name, &sb, NULL, 0);
394 + strbuf_addstr(&sb, " => ");
395 + quote_c_style(patch->new_name, &sb, NULL, 0);
396 + } else {
397 + const char *n = patch->new_name;
398 + if (!n)
399 + n = patch->old_name;
400 + quote_c_style(n, &sb, NULL, 0);
401 + }
402 + fprintf(output, fmt, sb.buf);
403 + fputc('\n', output);
404 + strbuf_release(&sb);
405 +}
406 +
407 +#define SLOP (16)
408 +
409 +static int read_patch_file(struct strbuf *sb, int fd)
410 +{
411 + if (strbuf_read(sb, fd, 0) < 0)
412 + return error_errno("git apply: failed to read");
413 +
414 + /*
415 + * Make sure that we have some slop in the buffer
416 + * so that we can do speculative "memcmp" etc, and
417 + * see to it that it is NUL-filled.
418 + */
419 + strbuf_grow(sb, SLOP);
420 + memset(sb->buf + sb->len, 0, SLOP);
421 + return 0;
422 +}
423 +
424 +static unsigned long linelen(const char *buffer, unsigned long size)
425 +{
426 + unsigned long len = 0;
427 + while (size--) {
428 + len++;
429 + if (*buffer++ == '\n')
430 + break;
431 + }
432 + return len;
433 +}
434 +
435 +static int is_dev_null(const char *str)
436 +{
437 + return skip_prefix(str, "/dev/null", &str) && isspace(*str);
438 +}
439 +
440 +#define TERM_SPACE 1
441 +#define TERM_TAB 2
442 +
443 +static int name_terminate(int c, int terminate)
444 +{
445 + if (c == ' ' && !(terminate & TERM_SPACE))
446 + return 0;
447 + if (c == '\t' && !(terminate & TERM_TAB))
448 + return 0;
449 +
450 + return 1;
451 +}
452 +
453 +/* remove double slashes to make --index work with such filenames */
454 +static char *squash_slash(char *name)
455 +{
456 + int i = 0, j = 0;
457 +
458 + if (!name)
459 + return NULL;
460 +
461 + while (name[i]) {
462 + if ((name[j++] = name[i++]) == '/')
463 + while (name[i] == '/')
464 + i++;
465 + }
466 + name[j] = '\0';
467 + return name;
468 +}
469 +
470 +static char *find_name_gnu(struct apply_state *state,
471 + const char *line,
472 + const char *def,
473 + int p_value)
474 +{
475 + struct strbuf name = STRBUF_INIT;
476 + char *cp;
477 +
478 + /*
479 + * Proposed "new-style" GNU patch/diff format; see
480 + * http://marc.info/?l=git&m=112927316408690&w=2
481 + */
482 + if (unquote_c_style(&name, line, NULL)) {
483 + strbuf_release(&name);
484 + return NULL;
485 + }
486 +
487 + for (cp = name.buf; p_value; p_value--) {
488 + cp = strchr(cp, '/');
489 + if (!cp) {
490 + strbuf_release(&name);
491 + return NULL;
492 + }
493 + cp++;
494 + }
495 +
496 + strbuf_remove(&name, 0, cp - name.buf);
497 + if (state->root.len)
498 + strbuf_insert(&name, 0, state->root.buf, state->root.len);
499 + return squash_slash(strbuf_detach(&name, NULL));
500 +}
501 +
502 +static size_t sane_tz_len(const char *line, size_t len)
503 +{
504 + const char *tz, *p;
505 +
506 + if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
507 + return 0;
508 + tz = line + len - strlen(" +0500");
509 +
510 + if (tz[1] != '+' && tz[1] != '-')
511 + return 0;
512 +
513 + for (p = tz + 2; p != line + len; p++)
514 + if (!isdigit(*p))
515 + return 0;
516 +
517 + return line + len - tz;
518 +}
519 +
520 +static size_t tz_with_colon_len(const char *line, size_t len)
521 +{
522 + const char *tz, *p;
523 +
524 + if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
525 + return 0;
526 + tz = line + len - strlen(" +08:00");
527 +
528 + if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
529 + return 0;
530 + p = tz + 2;
531 + if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
532 + !isdigit(*p++) || !isdigit(*p++))
533 + return 0;
534 +
535 + return line + len - tz;
536 +}
537 +
538 +static size_t date_len(const char *line, size_t len)
539 +{
540 + const char *date, *p;
541 +
542 + if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
543 + return 0;
544 + p = date = line + len - strlen("72-02-05");
545 +
546 + if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
547 + !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
548 + !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
549 + return 0;
550 +
551 + if (date - line >= strlen("19") &&
552 + isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
553 + date -= strlen("19");
554 +
555 + return line + len - date;
556 +}
557 +
558 +static size_t short_time_len(const char *line, size_t len)
559 +{
560 + const char *time, *p;
561 +
562 + if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
563 + return 0;
564 + p = time = line + len - strlen(" 07:01:32");
565 +
566 + /* Permit 1-digit hours? */
567 + if (*p++ != ' ' ||
568 + !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
569 + !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
570 + !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
571 + return 0;
572 +
573 + return line + len - time;
574 +}
575 +
576 +static size_t fractional_time_len(const char *line, size_t len)
577 +{
578 + const char *p;
579 + size_t n;
580 +
581 + /* Expected format: 19:41:17.620000023 */
582 + if (!len || !isdigit(line[len - 1]))
583 + return 0;
584 + p = line + len - 1;
585 +
586 + /* Fractional seconds. */
587 + while (p > line && isdigit(*p))
588 + p--;
589 + if (*p != '.')
590 + return 0;
591 +
592 + /* Hours, minutes, and whole seconds. */
593 + n = short_time_len(line, p - line);
594 + if (!n)
595 + return 0;
596 +
597 + return line + len - p + n;
598 +}
599 +
600 +static size_t trailing_spaces_len(const char *line, size_t len)
601 +{
602 + const char *p;
603 +
604 + /* Expected format: ' ' x (1 or more) */
605 + if (!len || line[len - 1] != ' ')
606 + return 0;
607 +
608 + p = line + len;
609 + while (p != line) {
610 + p--;
611 + if (*p != ' ')
612 + return line + len - (p + 1);
613 + }
614 +
615 + /* All spaces! */
616 + return len;
617 +}
618 +
619 +static size_t diff_timestamp_len(const char *line, size_t len)
620 +{
621 + const char *end = line + len;
622 + size_t n;
623 +
624 + /*
625 + * Posix: 2010-07-05 19:41:17
626 + * GNU: 2010-07-05 19:41:17.620000023 -0500
627 + */
628 +
629 + if (!isdigit(end[-1]))
630 + return 0;
631 +
632 + n = sane_tz_len(line, end - line);
633 + if (!n)
634 + n = tz_with_colon_len(line, end - line);
635 + end -= n;
636 +
637 + n = short_time_len(line, end - line);
638 + if (!n)
639 + n = fractional_time_len(line, end - line);
640 + end -= n;
641 +
642 + n = date_len(line, end - line);
643 + if (!n) /* No date. Too bad. */
644 + return 0;
645 + end -= n;
646 +
647 + if (end == line) /* No space before date. */
648 + return 0;
649 + if (end[-1] == '\t') { /* Success! */
650 + end--;
651 + return line + len - end;
652 + }
653 + if (end[-1] != ' ') /* No space before date. */
654 + return 0;
655 +
656 + /* Whitespace damage. */
657 + end -= trailing_spaces_len(line, end - line);
658 + return line + len - end;
659 +}
660 +
661 +static char *find_name_common(struct apply_state *state,
662 + const char *line,
663 + const char *def,
664 + int p_value,
665 + const char *end,
666 + int terminate)
667 +{
668 + int len;
669 + const char *start = NULL;
670 +
671 + if (p_value == 0)
672 + start = line;
673 + while (line != end) {
674 + char c = *line;
675 +
676 + if (!end && isspace(c)) {
677 + if (c == '\n')
678 + break;
679 + if (name_terminate(c, terminate))
680 + break;
681 + }
682 + line++;
683 + if (c == '/' && !--p_value)
684 + start = line;
685 + }
686 + if (!start)
687 + return squash_slash(xstrdup_or_null(def));
688 + len = line - start;
689 + if (!len)
690 + return squash_slash(xstrdup_or_null(def));
691 +
692 + /*
693 + * Generally we prefer the shorter name, especially
694 + * if the other one is just a variation of that with
695 + * something else tacked on to the end (ie "file.orig"
696 + * or "file~").
697 + */
698 + if (def) {
699 + int deflen = strlen(def);
700 + if (deflen < len && !strncmp(start, def, deflen))
701 + return squash_slash(xstrdup(def));
702 + }
703 +
704 + if (state->root.len) {
705 + char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
706 + return squash_slash(ret);
707 + }
708 +
709 + return squash_slash(xmemdupz(start, len));
710 +}
711 +
712 +static char *find_name(struct apply_state *state,
713 + const char *line,
714 + char *def,
715 + int p_value,
716 + int terminate)
717 +{
718 + if (*line == '"') {
719 + char *name = find_name_gnu(state, line, def, p_value);
720 + if (name)
721 + return name;
722 + }
723 +
724 + return find_name_common(state, line, def, p_value, NULL, terminate);
725 +}
726 +
727 +static char *find_name_traditional(struct apply_state *state,
728 + const char *line,
729 + char *def,
730 + int p_value)
731 +{
732 + size_t len;
733 + size_t date_len;
734 +
735 + if (*line == '"') {
736 + char *name = find_name_gnu(state, line, def, p_value);
737 + if (name)
738 + return name;
739 + }
740 +
741 + len = strchrnul(line, '\n') - line;
742 + date_len = diff_timestamp_len(line, len);
743 + if (!date_len)
744 + return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
745 + len -= date_len;
746 +
747 + return find_name_common(state, line, def, p_value, line + len, 0);
748 +}
749 +
750 +static int count_slashes(const char *cp)
751 +{
752 + int cnt = 0;
753 + char ch;
754 +
755 + while ((ch = *cp++))
756 + if (ch == '/')
757 + cnt++;
758 + return cnt;
759 +}
760 +
761 +/*
762 + * Given the string after "--- " or "+++ ", guess the appropriate
763 + * p_value for the given patch.
764 + */
765 +static int guess_p_value(struct apply_state *state, const char *nameline)
766 +{
767 + char *name, *cp;
768 + int val = -1;
769 +
770 + if (is_dev_null(nameline))
771 + return -1;
772 + name = find_name_traditional(state, nameline, NULL, 0);
773 + if (!name)
774 + return -1;
775 + cp = strchr(name, '/');
776 + if (!cp)
777 + val = 0;
778 + else if (state->prefix) {
779 + /*
780 + * Does it begin with "a/$our-prefix" and such? Then this is
781 + * very likely to apply to our directory.
782 + */
783 + if (!strncmp(name, state->prefix, state->prefix_length))
784 + val = count_slashes(state->prefix);
785 + else {
786 + cp++;
787 + if (!strncmp(cp, state->prefix, state->prefix_length))
788 + val = count_slashes(state->prefix) + 1;
789 + }
790 + }
791 + free(name);
792 + return val;
793 +}
794 +
795 +/*
796 + * Does the ---/+++ line have the POSIX timestamp after the last HT?
797 + * GNU diff puts epoch there to signal a creation/deletion event. Is
798 + * this such a timestamp?
799 + */
800 +static int has_epoch_timestamp(const char *nameline)
801 +{
802 + /*
803 + * We are only interested in epoch timestamp; any non-zero
804 + * fraction cannot be one, hence "(\.0+)?" in the regexp below.
805 + * For the same reason, the date must be either 1969-12-31 or
806 + * 1970-01-01, and the seconds part must be "00".
807 + */
808 + const char stamp_regexp[] =
809 + "^(1969-12-31|1970-01-01)"
810 + " "
811 + "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
812 + " "
813 + "([-+][0-2][0-9]:?[0-5][0-9])\n";
814 + const char *timestamp = NULL, *cp, *colon;
815 + static regex_t *stamp;
816 + regmatch_t m[10];
817 + int zoneoffset;
818 + int hourminute;
819 + int status;
820 +
821 + for (cp = nameline; *cp != '\n'; cp++) {
822 + if (*cp == '\t')
823 + timestamp = cp + 1;
824 + }
825 + if (!timestamp)
826 + return 0;
827 + if (!stamp) {
828 + stamp = xmalloc(sizeof(*stamp));
829 + if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
830 + warning(_("Cannot prepare timestamp regexp %s"),
831 + stamp_regexp);
832 + return 0;
833 + }
834 + }
835 +
836 + status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
837 + if (status) {
838 + if (status != REG_NOMATCH)
839 + warning(_("regexec returned %d for input: %s"),
840 + status, timestamp);
841 + return 0;
842 + }
843 +
844 + zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
845 + if (*colon == ':')
846 + zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
847 + else
848 + zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
849 + if (timestamp[m[3].rm_so] == '-')
850 + zoneoffset = -zoneoffset;
851 +
852 + /*
853 + * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
854 + * (west of GMT) or 1970-01-01 (east of GMT)
855 + */
856 + if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
857 + (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
858 + return 0;
859 +
860 + hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
861 + strtol(timestamp + 14, NULL, 10) -
862 + zoneoffset);
863 +
864 + return ((zoneoffset < 0 && hourminute == 1440) ||
865 + (0 <= zoneoffset && !hourminute));
866 +}
867 +
868 +/*
869 + * Get the name etc info from the ---/+++ lines of a traditional patch header
870 + *
871 + * FIXME! The end-of-filename heuristics are kind of screwy. For existing
872 + * files, we can happily check the index for a match, but for creating a
873 + * new file we should try to match whatever "patch" does. I have no idea.
874 + */
875 +static int parse_traditional_patch(struct apply_state *state,
876 + const char *first,
877 + const char *second,
878 + struct patch *patch)
879 +{
880 + char *name;
881 +
882 + first += 4; /* skip "--- " */
883 + second += 4; /* skip "+++ " */
884 + if (!state->p_value_known) {
885 + int p, q;
886 + p = guess_p_value(state, first);
887 + q = guess_p_value(state, second);
888 + if (p < 0) p = q;
889 + if (0 <= p && p == q) {
890 + state->p_value = p;
891 + state->p_value_known = 1;
892 + }
893 + }
894 + if (is_dev_null(first)) {
895 + patch->is_new = 1;
896 + patch->is_delete = 0;
897 + name = find_name_traditional(state, second, NULL, state->p_value);
898 + patch->new_name = name;
899 + } else if (is_dev_null(second)) {
900 + patch->is_new = 0;
901 + patch->is_delete = 1;
902 + name = find_name_traditional(state, first, NULL, state->p_value);
903 + patch->old_name = name;
904 + } else {
905 + char *first_name;
906 + first_name = find_name_traditional(state, first, NULL, state->p_value);
907 + name = find_name_traditional(state, second, first_name, state->p_value);
908 + free(first_name);
909 + if (has_epoch_timestamp(first)) {
910 + patch->is_new = 1;
911 + patch->is_delete = 0;
912 + patch->new_name = name;
913 + } else if (has_epoch_timestamp(second)) {
914 + patch->is_new = 0;
915 + patch->is_delete = 1;
916 + patch->old_name = name;
917 + } else {
918 + patch->old_name = name;
919 + patch->new_name = xstrdup_or_null(name);
920 + }
921 + }
922 + if (!name)
923 + return error(_("unable to find filename in patch at line %d"), state->linenr);
924 +
925 + return 0;
926 +}
927 +
928 +static int gitdiff_hdrend(struct apply_state *state,
929 + const char *line,
930 + struct patch *patch)
931 +{
932 + return 1;
933 +}
934 +
935 +/*
936 + * We're anal about diff header consistency, to make
937 + * sure that we don't end up having strange ambiguous
938 + * patches floating around.
939 + *
940 + * As a result, gitdiff_{old|new}name() will check
941 + * their names against any previous information, just
942 + * to make sure..
943 + */
944 +#define DIFF_OLD_NAME 0
945 +#define DIFF_NEW_NAME 1
946 +
947 +static int gitdiff_verify_name(struct apply_state *state,
948 + const char *line,
949 + int isnull,
950 + char **name,
951 + int side)
952 +{
953 + if (!*name && !isnull) {
954 + *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
955 + return 0;
956 + }
957 +
958 + if (*name) {
959 + int len = strlen(*name);
960 + char *another;
961 + if (isnull)
962 + return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
963 + *name, state->linenr);
964 + another = find_name(state, line, NULL, state->p_value, TERM_TAB);
965 + if (!another || memcmp(another, *name, len + 1)) {
966 + free(another);
967 + return error((side == DIFF_NEW_NAME) ?
968 + _("git apply: bad git-diff - inconsistent new filename on line %d") :
969 + _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
970 + }
971 + free(another);
972 + } else {
973 + /* expect "/dev/null" */
974 + if (memcmp("/dev/null", line, 9) || line[9] != '\n')
975 + return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
976 + }
977 +
978 + return 0;
979 +}
980 +
981 +static int gitdiff_oldname(struct apply_state *state,
982 + const char *line,
983 + struct patch *patch)
984 +{
985 + return gitdiff_verify_name(state, line,
986 + patch->is_new, &patch->old_name,
987 + DIFF_OLD_NAME);
988 +}
989 +
990 +static int gitdiff_newname(struct apply_state *state,
991 + const char *line,
992 + struct patch *patch)
993 +{
994 + return gitdiff_verify_name(state, line,
995 + patch->is_delete, &patch->new_name,
996 + DIFF_NEW_NAME);
997 +}
998 +
999 +static int gitdiff_oldmode(struct apply_state *state,
1000 + const char *line,
1001 + struct patch *patch)
1002 +{
1003 + patch->old_mode = strtoul(line, NULL, 8);
1004 + return 0;
1005 +}
1006 +
1007 +static int gitdiff_newmode(struct apply_state *state,
1008 + const char *line,
1009 + struct patch *patch)
1010 +{
1011 + patch->new_mode = strtoul(line, NULL, 8);
1012 + return 0;
1013 +}
1014 +
1015 +static int gitdiff_delete(struct apply_state *state,
1016 + const char *line,
1017 + struct patch *patch)
1018 +{
1019 + patch->is_delete = 1;
1020 + free(patch->old_name);
1021 + patch->old_name = xstrdup_or_null(patch->def_name);
1022 + return gitdiff_oldmode(state, line, patch);
1023 +}
1024 +
1025 +static int gitdiff_newfile(struct apply_state *state,
1026 + const char *line,
1027 + struct patch *patch)
1028 +{
1029 + patch->is_new = 1;
1030 + free(patch->new_name);
1031 + patch->new_name = xstrdup_or_null(patch->def_name);
1032 + return gitdiff_newmode(state, line, patch);
1033 +}
1034 +
1035 +static int gitdiff_copysrc(struct apply_state *state,
1036 + const char *line,
1037 + struct patch *patch)
1038 +{
1039 + patch->is_copy = 1;
1040 + free(patch->old_name);
1041 + patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1042 + return 0;
1043 +}
1044 +
1045 +static int gitdiff_copydst(struct apply_state *state,
1046 + const char *line,
1047 + struct patch *patch)
1048 +{
1049 + patch->is_copy = 1;
1050 + free(patch->new_name);
1051 + patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1052 + return 0;
1053 +}
1054 +
1055 +static int gitdiff_renamesrc(struct apply_state *state,
1056 + const char *line,
1057 + struct patch *patch)
1058 +{
1059 + patch->is_rename = 1;
1060 + free(patch->old_name);
1061 + patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1062 + return 0;
1063 +}
1064 +
1065 +static int gitdiff_renamedst(struct apply_state *state,
1066 + const char *line,
1067 + struct patch *patch)
1068 +{
1069 + patch->is_rename = 1;
1070 + free(patch->new_name);
1071 + patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1072 + return 0;
1073 +}
1074 +
1075 +static int gitdiff_similarity(struct apply_state *state,
1076 + const char *line,
1077 + struct patch *patch)
1078 +{
1079 + unsigned long val = strtoul(line, NULL, 10);
1080 + if (val <= 100)
1081 + patch->score = val;
1082 + return 0;
1083 +}
1084 +
1085 +static int gitdiff_dissimilarity(struct apply_state *state,
1086 + const char *line,
1087 + struct patch *patch)
1088 +{
1089 + unsigned long val = strtoul(line, NULL, 10);
1090 + if (val <= 100)
1091 + patch->score = val;
1092 + return 0;
1093 +}
1094 +
1095 +static int gitdiff_index(struct apply_state *state,
1096 + const char *line,
1097 + struct patch *patch)
1098 +{
1099 + /*
1100 + * index line is N hexadecimal, "..", N hexadecimal,
1101 + * and optional space with octal mode.
1102 + */
1103 + const char *ptr, *eol;
1104 + int len;
1105 +
1106 + ptr = strchr(line, '.');
1107 + if (!ptr || ptr[1] != '.' || 40 < ptr - line)
1108 + return 0;
1109 + len = ptr - line;
1110 + memcpy(patch->old_sha1_prefix, line, len);
1111 + patch->old_sha1_prefix[len] = 0;
1112 +
1113 + line = ptr + 2;
1114 + ptr = strchr(line, ' ');
1115 + eol = strchrnul(line, '\n');
1116 +
1117 + if (!ptr || eol < ptr)
1118 + ptr = eol;
1119 + len = ptr - line;
1120 +
1121 + if (40 < len)
1122 + return 0;
1123 + memcpy(patch->new_sha1_prefix, line, len);
1124 + patch->new_sha1_prefix[len] = 0;
1125 + if (*ptr == ' ')
1126 + patch->old_mode = strtoul(ptr+1, NULL, 8);
1127 + return 0;
1128 +}
1129 +
1130 +/*
1131 + * This is normal for a diff that doesn't change anything: we'll fall through
1132 + * into the next diff. Tell the parser to break out.
1133 + */
1134 +static int gitdiff_unrecognized(struct apply_state *state,
1135 + const char *line,
1136 + struct patch *patch)
1137 +{
1138 + return 1;
1139 +}
1140 +
1141 +/*
1142 + * Skip p_value leading components from "line"; as we do not accept
1143 + * absolute paths, return NULL in that case.
1144 + */
1145 +static const char *skip_tree_prefix(struct apply_state *state,
1146 + const char *line,
1147 + int llen)
1148 +{
1149 + int nslash;
1150 + int i;
1151 +
1152 + if (!state->p_value)
1153 + return (llen && line[0] == '/') ? NULL : line;
1154 +
1155 + nslash = state->p_value;
1156 + for (i = 0; i < llen; i++) {
1157 + int ch = line[i];
1158 + if (ch == '/' && --nslash <= 0)
1159 + return (i == 0) ? NULL : &line[i + 1];
1160 + }
1161 + return NULL;
1162 +}
1163 +
1164 +/*
1165 + * This is to extract the same name that appears on "diff --git"
1166 + * line. We do not find and return anything if it is a rename
1167 + * patch, and it is OK because we will find the name elsewhere.
1168 + * We need to reliably find name only when it is mode-change only,
1169 + * creation or deletion of an empty file. In any of these cases,
1170 + * both sides are the same name under a/ and b/ respectively.
1171 + */
1172 +static char *git_header_name(struct apply_state *state,
1173 + const char *line,
1174 + int llen)
1175 +{
1176 + const char *name;
1177 + const char *second = NULL;
1178 + size_t len, line_len;
1179 +
1180 + line += strlen("diff --git ");
1181 + llen -= strlen("diff --git ");
1182 +
1183 + if (*line == '"') {
1184 + const char *cp;
1185 + struct strbuf first = STRBUF_INIT;
1186 + struct strbuf sp = STRBUF_INIT;
1187 +
1188 + if (unquote_c_style(&first, line, &second))
1189 + goto free_and_fail1;
1190 +
1191 + /* strip the a/b prefix including trailing slash */
1192 + cp = skip_tree_prefix(state, first.buf, first.len);
1193 + if (!cp)
1194 + goto free_and_fail1;
1195 + strbuf_remove(&first, 0, cp - first.buf);
1196 +
1197 + /*
1198 + * second points at one past closing dq of name.
1199 + * find the second name.
1200 + */
1201 + while ((second < line + llen) && isspace(*second))
1202 + second++;
1203 +
1204 + if (line + llen <= second)
1205 + goto free_and_fail1;
1206 + if (*second == '"') {
1207 + if (unquote_c_style(&sp, second, NULL))
1208 + goto free_and_fail1;
1209 + cp = skip_tree_prefix(state, sp.buf, sp.len);
1210 + if (!cp)
1211 + goto free_and_fail1;
1212 + /* They must match, otherwise ignore */
1213 + if (strcmp(cp, first.buf))
1214 + goto free_and_fail1;
1215 + strbuf_release(&sp);
1216 + return strbuf_detach(&first, NULL);
1217 + }
1218 +
1219 + /* unquoted second */
1220 + cp = skip_tree_prefix(state, second, line + llen - second);
1221 + if (!cp)
1222 + goto free_and_fail1;
1223 + if (line + llen - cp != first.len ||
1224 + memcmp(first.buf, cp, first.len))
1225 + goto free_and_fail1;
1226 + return strbuf_detach(&first, NULL);
1227 +
1228 + free_and_fail1:
1229 + strbuf_release(&first);
1230 + strbuf_release(&sp);
1231 + return NULL;
1232 + }
1233 +
1234 + /* unquoted first name */
1235 + name = skip_tree_prefix(state, line, llen);
1236 + if (!name)
1237 + return NULL;
1238 +
1239 + /*
1240 + * since the first name is unquoted, a dq if exists must be
1241 + * the beginning of the second name.
1242 + */
1243 + for (second = name; second < line + llen; second++) {
1244 + if (*second == '"') {
1245 + struct strbuf sp = STRBUF_INIT;
1246 + const char *np;
1247 +
1248 + if (unquote_c_style(&sp, second, NULL))
1249 + goto free_and_fail2;
1250 +
1251 + np = skip_tree_prefix(state, sp.buf, sp.len);
1252 + if (!np)
1253 + goto free_and_fail2;
1254 +
1255 + len = sp.buf + sp.len - np;
1256 + if (len < second - name &&
1257 + !strncmp(np, name, len) &&
1258 + isspace(name[len])) {
1259 + /* Good */
1260 + strbuf_remove(&sp, 0, np - sp.buf);
1261 + return strbuf_detach(&sp, NULL);
1262 + }
1263 +
1264 + free_and_fail2:
1265 + strbuf_release(&sp);
1266 + return NULL;
1267 + }
1268 + }
1269 +
1270 + /*
1271 + * Accept a name only if it shows up twice, exactly the same
1272 + * form.
1273 + */
1274 + second = strchr(name, '\n');
1275 + if (!second)
1276 + return NULL;
1277 + line_len = second - name;
1278 + for (len = 0 ; ; len++) {
1279 + switch (name[len]) {
1280 + default:
1281 + continue;
1282 + case '\n':
1283 + return NULL;
1284 + case '\t': case ' ':
1285 + /*
1286 + * Is this the separator between the preimage
1287 + * and the postimage pathname? Again, we are
1288 + * only interested in the case where there is
1289 + * no rename, as this is only to set def_name
1290 + * and a rename patch has the names elsewhere
1291 + * in an unambiguous form.
1292 + */
1293 + if (!name[len + 1])
1294 + return NULL; /* no postimage name */
1295 + second = skip_tree_prefix(state, name + len + 1,
1296 + line_len - (len + 1));
1297 + if (!second)
1298 + return NULL;
1299 + /*
1300 + * Does len bytes starting at "name" and "second"
1301 + * (that are separated by one HT or SP we just
1302 + * found) exactly match?
1303 + */
1304 + if (second[len] == '\n' && !strncmp(name, second, len))
1305 + return xmemdupz(name, len);
1306 + }
1307 + }
1308 +}
1309 +
1310 +/* Verify that we recognize the lines following a git header */
1311 +static int parse_git_header(struct apply_state *state,
1312 + const char *line,
1313 + int len,
1314 + unsigned int size,
1315 + struct patch *patch)
1316 +{
1317 + unsigned long offset;
1318 +
1319 + /* A git diff has explicit new/delete information, so we don't guess */
1320 + patch->is_new = 0;
1321 + patch->is_delete = 0;
1322 +
1323 + /*
1324 + * Some things may not have the old name in the
1325 + * rest of the headers anywhere (pure mode changes,
1326 + * or removing or adding empty files), so we get
1327 + * the default name from the header.
1328 + */
1329 + patch->def_name = git_header_name(state, line, len);
1330 + if (patch->def_name && state->root.len) {
1331 + char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1332 + free(patch->def_name);
1333 + patch->def_name = s;
1334 + }
1335 +
1336 + line += len;
1337 + size -= len;
1338 + state->linenr++;
1339 + for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1340 + static const struct opentry {
1341 + const char *str;
1342 + int (*fn)(struct apply_state *, const char *, struct patch *);
1343 + } optable[] = {
1344 + { "@@ -", gitdiff_hdrend },
1345 + { "--- ", gitdiff_oldname },
1346 + { "+++ ", gitdiff_newname },
1347 + { "old mode ", gitdiff_oldmode },
1348 + { "new mode ", gitdiff_newmode },
1349 + { "deleted file mode ", gitdiff_delete },
1350 + { "new file mode ", gitdiff_newfile },
1351 + { "copy from ", gitdiff_copysrc },
1352 + { "copy to ", gitdiff_copydst },
1353 + { "rename old ", gitdiff_renamesrc },
1354 + { "rename new ", gitdiff_renamedst },
1355 + { "rename from ", gitdiff_renamesrc },
1356 + { "rename to ", gitdiff_renamedst },
1357 + { "similarity index ", gitdiff_similarity },
1358 + { "dissimilarity index ", gitdiff_dissimilarity },
1359 + { "index ", gitdiff_index },
1360 + { "", gitdiff_unrecognized },
1361 + };
1362 + int i;
1363 +
1364 + len = linelen(line, size);
1365 + if (!len || line[len-1] != '\n')
1366 + break;
1367 + for (i = 0; i < ARRAY_SIZE(optable); i++) {
1368 + const struct opentry *p = optable + i;
1369 + int oplen = strlen(p->str);
1370 + int res;
1371 + if (len < oplen || memcmp(p->str, line, oplen))
1372 + continue;
1373 + res = p->fn(state, line + oplen, patch);
1374 + if (res < 0)
1375 + return -1;
1376 + if (res > 0)
1377 + return offset;
1378 + break;
1379 + }
1380 + }
1381 +
1382 + return offset;
1383 +}
1384 +
1385 +static int parse_num(const char *line, unsigned long *p)
1386 +{
1387 + char *ptr;
1388 +
1389 + if (!isdigit(*line))
1390 + return 0;
1391 + *p = strtoul(line, &ptr, 10);
1392 + return ptr - line;
1393 +}
1394 +
1395 +static int parse_range(const char *line, int len, int offset, const char *expect,
1396 + unsigned long *p1, unsigned long *p2)
1397 +{
1398 + int digits, ex;
1399 +
1400 + if (offset < 0 || offset >= len)
1401 + return -1;
1402 + line += offset;
1403 + len -= offset;
1404 +
1405 + digits = parse_num(line, p1);
1406 + if (!digits)
1407 + return -1;
1408 +
1409 + offset += digits;
1410 + line += digits;
1411 + len -= digits;
1412 +
1413 + *p2 = 1;
1414 + if (*line == ',') {
1415 + digits = parse_num(line+1, p2);
1416 + if (!digits)
1417 + return -1;
1418 +
1419 + offset += digits+1;
1420 + line += digits+1;
1421 + len -= digits+1;
1422 + }
1423 +
1424 + ex = strlen(expect);
1425 + if (ex > len)
1426 + return -1;
1427 + if (memcmp(line, expect, ex))
1428 + return -1;
1429 +
1430 + return offset + ex;
1431 +}
1432 +
1433 +static void recount_diff(const char *line, int size, struct fragment *fragment)
1434 +{
1435 + int oldlines = 0, newlines = 0, ret = 0;
1436 +
1437 + if (size < 1) {
1438 + warning("recount: ignore empty hunk");
1439 + return;
1440 + }
1441 +
1442 + for (;;) {
1443 + int len = linelen(line, size);
1444 + size -= len;
1445 + line += len;
1446 +
1447 + if (size < 1)
1448 + break;
1449 +
1450 + switch (*line) {
1451 + case ' ': case '\n':
1452 + newlines++;
1453 + /* fall through */
1454 + case '-':
1455 + oldlines++;
1456 + continue;
1457 + case '+':
1458 + newlines++;
1459 + continue;
1460 + case '\\':
1461 + continue;
1462 + case '@':
1463 + ret = size < 3 || !starts_with(line, "@@ ");
1464 + break;
1465 + case 'd':
1466 + ret = size < 5 || !starts_with(line, "diff ");
1467 + break;
1468 + default:
1469 + ret = -1;
1470 + break;
1471 + }
1472 + if (ret) {
1473 + warning(_("recount: unexpected line: %.*s"),
1474 + (int)linelen(line, size), line);
1475 + return;
1476 + }
1477 + break;
1478 + }
1479 + fragment->oldlines = oldlines;
1480 + fragment->newlines = newlines;
1481 +}
1482 +
1483 +/*
1484 + * Parse a unified diff fragment header of the
1485 + * form "@@ -a,b +c,d @@"
1486 + */
1487 +static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1488 +{
1489 + int offset;
1490 +
1491 + if (!len || line[len-1] != '\n')
1492 + return -1;
1493 +
1494 + /* Figure out the number of lines in a fragment */
1495 + offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1496 + offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1497 +
1498 + return offset;
1499 +}
1500 +
1501 +/*
1502 + * Find file diff header
1503 + *
1504 + * Returns:
1505 + * -1 if no header was found
1506 + * -128 in case of error
1507 + * the size of the header in bytes (called "offset") otherwise
1508 + */
1509 +static int find_header(struct apply_state *state,
1510 + const char *line,
1511 + unsigned long size,
1512 + int *hdrsize,
1513 + struct patch *patch)
1514 +{
1515 + unsigned long offset, len;
1516 +
1517 + patch->is_toplevel_relative = 0;
1518 + patch->is_rename = patch->is_copy = 0;
1519 + patch->is_new = patch->is_delete = -1;
1520 + patch->old_mode = patch->new_mode = 0;
1521 + patch->old_name = patch->new_name = NULL;
1522 + for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1523 + unsigned long nextlen;
1524 +
1525 + len = linelen(line, size);
1526 + if (!len)
1527 + break;
1528 +
1529 + /* Testing this early allows us to take a few shortcuts.. */
1530 + if (len < 6)
1531 + continue;
1532 +
1533 + /*
1534 + * Make sure we don't find any unconnected patch fragments.
1535 + * That's a sign that we didn't find a header, and that a
1536 + * patch has become corrupted/broken up.
1537 + */
1538 + if (!memcmp("@@ -", line, 4)) {
1539 + struct fragment dummy;
1540 + if (parse_fragment_header(line, len, &dummy) < 0)
1541 + continue;
1542 + error(_("patch fragment without header at line %d: %.*s"),
1543 + state->linenr, (int)len-1, line);
1544 + return -128;
1545 + }
1546 +
1547 + if (size < len + 6)
1548 + break;
1549 +
1550 + /*
1551 + * Git patch? It might not have a real patch, just a rename
1552 + * or mode change, so we handle that specially
1553 + */
1554 + if (!memcmp("diff --git ", line, 11)) {
1555 + int git_hdr_len = parse_git_header(state, line, len, size, patch);
1556 + if (git_hdr_len < 0)
1557 + return -128;
1558 + if (git_hdr_len <= len)
1559 + continue;
1560 + if (!patch->old_name && !patch->new_name) {
1561 + if (!patch->def_name) {
1562 + error(Q_("git diff header lacks filename information when removing "
1563 + "%d leading pathname component (line %d)",
1564 + "git diff header lacks filename information when removing "
1565 + "%d leading pathname components (line %d)",
1566 + state->p_value),
1567 + state->p_value, state->linenr);
1568 + return -128;
1569 + }
1570 + patch->old_name = xstrdup(patch->def_name);
1571 + patch->new_name = xstrdup(patch->def_name);
1572 + }
1573 + if (!patch->is_delete && !patch->new_name) {
1574 + error("git diff header lacks filename information "
1575 + "(line %d)", state->linenr);
1576 + return -128;
1577 + }
1578 + patch->is_toplevel_relative = 1;
1579 + *hdrsize = git_hdr_len;
1580 + return offset;
1581 + }
1582 +
1583 + /* --- followed by +++ ? */
1584 + if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1585 + continue;
1586 +
1587 + /*
1588 + * We only accept unified patches, so we want it to
1589 + * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1590 + * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1591 + */
1592 + nextlen = linelen(line + len, size - len);
1593 + if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1594 + continue;
1595 +
1596 + /* Ok, we'll consider it a patch */
1597 + if (parse_traditional_patch(state, line, line+len, patch))
1598 + return -128;
1599 + *hdrsize = len + nextlen;
1600 + state->linenr += 2;
1601 + return offset;
1602 + }
1603 + return -1;
1604 +}
1605 +
1606 +static void record_ws_error(struct apply_state *state,
1607 + unsigned result,
1608 + const char *line,
1609 + int len,
1610 + int linenr)
1611 +{
1612 + char *err;
1613 +
1614 + if (!result)
1615 + return;
1616 +
1617 + state->whitespace_error++;
1618 + if (state->squelch_whitespace_errors &&
1619 + state->squelch_whitespace_errors < state->whitespace_error)
1620 + return;
1621 +
1622 + err = whitespace_error_string(result);
1623 + fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1624 + state->patch_input_file, linenr, err, len, line);
1625 + free(err);
1626 +}
1627 +
1628 +static void check_whitespace(struct apply_state *state,
1629 + const char *line,
1630 + int len,
1631 + unsigned ws_rule)
1632 +{
1633 + unsigned result = ws_check(line + 1, len - 1, ws_rule);
1634 +
1635 + record_ws_error(state, result, line + 1, len - 2, state->linenr);
1636 +}
1637 +
1638 +/*
1639 + * Parse a unified diff. Note that this really needs to parse each
1640 + * fragment separately, since the only way to know the difference
1641 + * between a "---" that is part of a patch, and a "---" that starts
1642 + * the next patch is to look at the line counts..
1643 + */
1644 +static int parse_fragment(struct apply_state *state,
1645 + const char *line,
1646 + unsigned long size,
1647 + struct patch *patch,
1648 + struct fragment *fragment)
1649 +{
1650 + int added, deleted;
1651 + int len = linelen(line, size), offset;
1652 + unsigned long oldlines, newlines;
1653 + unsigned long leading, trailing;
1654 +
1655 + offset = parse_fragment_header(line, len, fragment);
1656 + if (offset < 0)
1657 + return -1;
1658 + if (offset > 0 && patch->recount)
1659 + recount_diff(line + offset, size - offset, fragment);
1660 + oldlines = fragment->oldlines;
1661 + newlines = fragment->newlines;
1662 + leading = 0;
1663 + trailing = 0;
1664 +
1665 + /* Parse the thing.. */
1666 + line += len;
1667 + size -= len;
1668 + state->linenr++;
1669 + added = deleted = 0;
1670 + for (offset = len;
1671 + 0 < size;
1672 + offset += len, size -= len, line += len, state->linenr++) {
1673 + if (!oldlines && !newlines)
1674 + break;
1675 + len = linelen(line, size);
1676 + if (!len || line[len-1] != '\n')
1677 + return -1;
1678 + switch (*line) {
1679 + default:
1680 + return -1;
1681 + case '\n': /* newer GNU diff, an empty context line */
1682 + case ' ':
1683 + oldlines--;
1684 + newlines--;
1685 + if (!deleted && !added)
1686 + leading++;
1687 + trailing++;
1688 + if (!state->apply_in_reverse &&
1689 + state->ws_error_action == correct_ws_error)
1690 + check_whitespace(state, line, len, patch->ws_rule);
1691 + break;
1692 + case '-':
1693 + if (state->apply_in_reverse &&
1694 + state->ws_error_action != nowarn_ws_error)
1695 + check_whitespace(state, line, len, patch->ws_rule);
1696 + deleted++;
1697 + oldlines--;
1698 + trailing = 0;
1699 + break;
1700 + case '+':
1701 + if (!state->apply_in_reverse &&
1702 + state->ws_error_action != nowarn_ws_error)
1703 + check_whitespace(state, line, len, patch->ws_rule);
1704 + added++;
1705 + newlines--;
1706 + trailing = 0;
1707 + break;
1708 +
1709 + /*
1710 + * We allow "\ No newline at end of file". Depending
1711 + * on locale settings when the patch was produced we
1712 + * don't know what this line looks like. The only
1713 + * thing we do know is that it begins with "\ ".
1714 + * Checking for 12 is just for sanity check -- any
1715 + * l10n of "\ No newline..." is at least that long.
1716 + */
1717 + case '\\':
1718 + if (len < 12 || memcmp(line, "\\ ", 2))
1719 + return -1;
1720 + break;
1721 + }
1722 + }
1723 + if (oldlines || newlines)
1724 + return -1;
1725 + if (!deleted && !added)
1726 + return -1;
1727 +
1728 + fragment->leading = leading;
1729 + fragment->trailing = trailing;
1730 +
1731 + /*
1732 + * If a fragment ends with an incomplete line, we failed to include
1733 + * it in the above loop because we hit oldlines == newlines == 0
1734 + * before seeing it.
1735 + */
1736 + if (12 < size && !memcmp(line, "\\ ", 2))
1737 + offset += linelen(line, size);
1738 +
1739 + patch->lines_added += added;
1740 + patch->lines_deleted += deleted;
1741 +
1742 + if (0 < patch->is_new && oldlines)
1743 + return error(_("new file depends on old contents"));
1744 + if (0 < patch->is_delete && newlines)
1745 + return error(_("deleted file still has contents"));
1746 + return offset;
1747 +}
1748 +
1749 +/*
1750 + * We have seen "diff --git a/... b/..." header (or a traditional patch
1751 + * header). Read hunks that belong to this patch into fragments and hang
1752 + * them to the given patch structure.
1753 + *
1754 + * The (fragment->patch, fragment->size) pair points into the memory given
1755 + * by the caller, not a copy, when we return.
1756 + *
1757 + * Returns:
1758 + * -1 in case of error,
1759 + * the number of bytes in the patch otherwise.
1760 + */
1761 +static int parse_single_patch(struct apply_state *state,
1762 + const char *line,
1763 + unsigned long size,
1764 + struct patch *patch)
1765 +{
1766 + unsigned long offset = 0;
1767 + unsigned long oldlines = 0, newlines = 0, context = 0;
1768 + struct fragment **fragp = &patch->fragments;
1769 +
1770 + while (size > 4 && !memcmp(line, "@@ -", 4)) {
1771 + struct fragment *fragment;
1772 + int len;
1773 +
1774 + fragment = xcalloc(1, sizeof(*fragment));
1775 + fragment->linenr = state->linenr;
1776 + len = parse_fragment(state, line, size, patch, fragment);
1777 + if (len <= 0) {
1778 + free(fragment);
1779 + return error(_("corrupt patch at line %d"), state->linenr);
1780 + }
1781 + fragment->patch = line;
1782 + fragment->size = len;
1783 + oldlines += fragment->oldlines;
1784 + newlines += fragment->newlines;
1785 + context += fragment->leading + fragment->trailing;
1786 +
1787 + *fragp = fragment;
1788 + fragp = &fragment->next;
1789 +
1790 + offset += len;
1791 + line += len;
1792 + size -= len;
1793 + }
1794 +
1795 + /*
1796 + * If something was removed (i.e. we have old-lines) it cannot
1797 + * be creation, and if something was added it cannot be
1798 + * deletion. However, the reverse is not true; --unified=0
1799 + * patches that only add are not necessarily creation even
1800 + * though they do not have any old lines, and ones that only
1801 + * delete are not necessarily deletion.
1802 + *
1803 + * Unfortunately, a real creation/deletion patch do _not_ have
1804 + * any context line by definition, so we cannot safely tell it
1805 + * apart with --unified=0 insanity. At least if the patch has
1806 + * more than one hunk it is not creation or deletion.
1807 + */
1808 + if (patch->is_new < 0 &&
1809 + (oldlines || (patch->fragments && patch->fragments->next)))
1810 + patch->is_new = 0;
1811 + if (patch->is_delete < 0 &&
1812 + (newlines || (patch->fragments && patch->fragments->next)))
1813 + patch->is_delete = 0;
1814 +
1815 + if (0 < patch->is_new && oldlines)
1816 + return error(_("new file %s depends on old contents"), patch->new_name);
1817 + if (0 < patch->is_delete && newlines)
1818 + return error(_("deleted file %s still has contents"), patch->old_name);
1819 + if (!patch->is_delete && !newlines && context)
1820 + fprintf_ln(stderr,
1821 + _("** warning: "
1822 + "file %s becomes empty but is not deleted"),
1823 + patch->new_name);
1824 +
1825 + return offset;
1826 +}
1827 +
1828 +static inline int metadata_changes(struct patch *patch)
1829 +{
1830 + return patch->is_rename > 0 ||
1831 + patch->is_copy > 0 ||
1832 + patch->is_new > 0 ||
1833 + patch->is_delete ||
1834 + (patch->old_mode && patch->new_mode &&
1835 + patch->old_mode != patch->new_mode);
1836 +}
1837 +
1838 +static char *inflate_it(const void *data, unsigned long size,
1839 + unsigned long inflated_size)
1840 +{
1841 + git_zstream stream;
1842 + void *out;
1843 + int st;
1844 +
1845 + memset(&stream, 0, sizeof(stream));
1846 +
1847 + stream.next_in = (unsigned char *)data;
1848 + stream.avail_in = size;
1849 + stream.next_out = out = xmalloc(inflated_size);
1850 + stream.avail_out = inflated_size;
1851 + git_inflate_init(&stream);
1852 + st = git_inflate(&stream, Z_FINISH);
1853 + git_inflate_end(&stream);
1854 + if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1855 + free(out);
1856 + return NULL;
1857 + }
1858 + return out;
1859 +}
1860 +
1861 +/*
1862 + * Read a binary hunk and return a new fragment; fragment->patch
1863 + * points at an allocated memory that the caller must free, so
1864 + * it is marked as "->free_patch = 1".
1865 + */
1866 +static struct fragment *parse_binary_hunk(struct apply_state *state,
1867 + char **buf_p,
1868 + unsigned long *sz_p,
1869 + int *status_p,
1870 + int *used_p)
1871 +{
1872 + /*
1873 + * Expect a line that begins with binary patch method ("literal"
1874 + * or "delta"), followed by the length of data before deflating.
1875 + * a sequence of 'length-byte' followed by base-85 encoded data
1876 + * should follow, terminated by a newline.
1877 + *
1878 + * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1879 + * and we would limit the patch line to 66 characters,
1880 + * so one line can fit up to 13 groups that would decode
1881 + * to 52 bytes max. The length byte 'A'-'Z' corresponds
1882 + * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1883 + */
1884 + int llen, used;
1885 + unsigned long size = *sz_p;
1886 + char *buffer = *buf_p;
1887 + int patch_method;
1888 + unsigned long origlen;
1889 + char *data = NULL;
1890 + int hunk_size = 0;
1891 + struct fragment *frag;
1892 +
1893 + llen = linelen(buffer, size);
1894 + used = llen;
1895 +
1896 + *status_p = 0;
1897 +
1898 + if (starts_with(buffer, "delta ")) {
1899 + patch_method = BINARY_DELTA_DEFLATED;
1900 + origlen = strtoul(buffer + 6, NULL, 10);
1901 + }
1902 + else if (starts_with(buffer, "literal ")) {
1903 + patch_method = BINARY_LITERAL_DEFLATED;
1904 + origlen = strtoul(buffer + 8, NULL, 10);
1905 + }
1906 + else
1907 + return NULL;
1908 +
1909 + state->linenr++;
1910 + buffer += llen;
1911 + while (1) {
1912 + int byte_length, max_byte_length, newsize;
1913 + llen = linelen(buffer, size);
1914 + used += llen;
1915 + state->linenr++;
1916 + if (llen == 1) {
1917 + /* consume the blank line */
1918 + buffer++;
1919 + size--;
1920 + break;
1921 + }
1922 + /*
1923 + * Minimum line is "A00000\n" which is 7-byte long,
1924 + * and the line length must be multiple of 5 plus 2.
1925 + */
1926 + if ((llen < 7) || (llen-2) % 5)
1927 + goto corrupt;
1928 + max_byte_length = (llen - 2) / 5 * 4;
1929 + byte_length = *buffer;
1930 + if ('A' <= byte_length && byte_length <= 'Z')
1931 + byte_length = byte_length - 'A' + 1;
1932 + else if ('a' <= byte_length && byte_length <= 'z')
1933 + byte_length = byte_length - 'a' + 27;
1934 + else
1935 + goto corrupt;
1936 + /* if the input length was not multiple of 4, we would
1937 + * have filler at the end but the filler should never
1938 + * exceed 3 bytes
1939 + */
1940 + if (max_byte_length < byte_length ||
1941 + byte_length <= max_byte_length - 4)
1942 + goto corrupt;
1943 + newsize = hunk_size + byte_length;
1944 + data = xrealloc(data, newsize);
1945 + if (decode_85(data + hunk_size, buffer + 1, byte_length))
1946 + goto corrupt;
1947 + hunk_size = newsize;
1948 + buffer += llen;
1949 + size -= llen;
1950 + }
1951 +
1952 + frag = xcalloc(1, sizeof(*frag));
1953 + frag->patch = inflate_it(data, hunk_size, origlen);
1954 + frag->free_patch = 1;
1955 + if (!frag->patch)
1956 + goto corrupt;
1957 + free(data);
1958 + frag->size = origlen;
1959 + *buf_p = buffer;
1960 + *sz_p = size;
1961 + *used_p = used;
1962 + frag->binary_patch_method = patch_method;
1963 + return frag;
1964 +
1965 + corrupt:
1966 + free(data);
1967 + *status_p = -1;
1968 + error(_("corrupt binary patch at line %d: %.*s"),
1969 + state->linenr-1, llen-1, buffer);
1970 + return NULL;
1971 +}
1972 +
1973 +/*
1974 + * Returns:
1975 + * -1 in case of error,
1976 + * the length of the parsed binary patch otherwise
1977 + */
1978 +static int parse_binary(struct apply_state *state,
1979 + char *buffer,
1980 + unsigned long size,
1981 + struct patch *patch)
1982 +{
1983 + /*
1984 + * We have read "GIT binary patch\n"; what follows is a line
1985 + * that says the patch method (currently, either "literal" or
1986 + * "delta") and the length of data before deflating; a
1987 + * sequence of 'length-byte' followed by base-85 encoded data
1988 + * follows.
1989 + *
1990 + * When a binary patch is reversible, there is another binary
1991 + * hunk in the same format, starting with patch method (either
1992 + * "literal" or "delta") with the length of data, and a sequence
1993 + * of length-byte + base-85 encoded data, terminated with another
1994 + * empty line. This data, when applied to the postimage, produces
1995 + * the preimage.
1996 + */
1997 + struct fragment *forward;
1998 + struct fragment *reverse;
1999 + int status;
2000 + int used, used_1;
2001 +
2002 + forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
2003 + if (!forward && !status)
2004 + /* there has to be one hunk (forward hunk) */
2005 + return error(_("unrecognized binary patch at line %d"), state->linenr-1);
2006 + if (status)
2007 + /* otherwise we already gave an error message */
2008 + return status;
2009 +
2010 + reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
2011 + if (reverse)
2012 + used += used_1;
2013 + else if (status) {
2014 + /*
2015 + * Not having reverse hunk is not an error, but having
2016 + * a corrupt reverse hunk is.
2017 + */
2018 + free((void*) forward->patch);
2019 + free(forward);
2020 + return status;
2021 + }
2022 + forward->next = reverse;
2023 + patch->fragments = forward;
2024 + patch->is_binary = 1;
2025 + return used;
2026 +}
2027 +
2028 +static void prefix_one(struct apply_state *state, char **name)
2029 +{
2030 + char *old_name = *name;
2031 + if (!old_name)
2032 + return;
2033 + *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
2034 + free(old_name);
2035 +}
2036 +
2037 +static void prefix_patch(struct apply_state *state, struct patch *p)
2038 +{
2039 + if (!state->prefix || p->is_toplevel_relative)
2040 + return;
2041 + prefix_one(state, &p->new_name);
2042 + prefix_one(state, &p->old_name);
2043 +}
2044 +
2045 +/*
2046 + * include/exclude
2047 + */
2048 +
2049 +static void add_name_limit(struct apply_state *state,
2050 + const char *name,
2051 + int exclude)
2052 +{
2053 + struct string_list_item *it;
2054 +
2055 + it = string_list_append(&state->limit_by_name, name);
2056 + it->util = exclude ? NULL : (void *) 1;
2057 +}
2058 +
2059 +static int use_patch(struct apply_state *state, struct patch *p)
2060 +{
2061 + const char *pathname = p->new_name ? p->new_name : p->old_name;
2062 + int i;
2063 +
2064 + /* Paths outside are not touched regardless of "--include" */
2065 + if (0 < state->prefix_length) {
2066 + int pathlen = strlen(pathname);
2067 + if (pathlen <= state->prefix_length ||
2068 + memcmp(state->prefix, pathname, state->prefix_length))
2069 + return 0;
2070 + }
2071 +
2072 + /* See if it matches any of exclude/include rule */
2073 + for (i = 0; i < state->limit_by_name.nr; i++) {
2074 + struct string_list_item *it = &state->limit_by_name.items[i];
2075 + if (!wildmatch(it->string, pathname, 0, NULL))
2076 + return (it->util != NULL);
2077 + }
2078 +
2079 + /*
2080 + * If we had any include, a path that does not match any rule is
2081 + * not used. Otherwise, we saw bunch of exclude rules (or none)
2082 + * and such a path is used.
2083 + */
2084 + return !state->has_include;
2085 +}
2086 +
2087 +/*
2088 + * Read the patch text in "buffer" that extends for "size" bytes; stop
2089 + * reading after seeing a single patch (i.e. changes to a single file).
2090 + * Create fragments (i.e. patch hunks) and hang them to the given patch.
2091 + *
2092 + * Returns:
2093 + * -1 if no header was found or parse_binary() failed,
2094 + * -128 on another error,
2095 + * the number of bytes consumed otherwise,
2096 + * so that the caller can call us again for the next patch.
2097 + */
2098 +static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2099 +{
2100 + int hdrsize, patchsize;
2101 + int offset = find_header(state, buffer, size, &hdrsize, patch);
2102 +
2103 + if (offset < 0)
2104 + return offset;
2105 +
2106 + prefix_patch(state, patch);
2107 +
2108 + if (!use_patch(state, patch))
2109 + patch->ws_rule = 0;
2110 + else
2111 + patch->ws_rule = whitespace_rule(patch->new_name
2112 + ? patch->new_name
2113 + : patch->old_name);
2114 +
2115 + patchsize = parse_single_patch(state,
2116 + buffer + offset + hdrsize,
2117 + size - offset - hdrsize,
2118 + patch);
2119 +
2120 + if (patchsize < 0)
2121 + return -128;
2122 +
2123 + if (!patchsize) {
2124 + static const char git_binary[] = "GIT binary patch\n";
2125 + int hd = hdrsize + offset;
2126 + unsigned long llen = linelen(buffer + hd, size - hd);
2127 +
2128 + if (llen == sizeof(git_binary) - 1 &&
2129 + !memcmp(git_binary, buffer + hd, llen)) {
2130 + int used;
2131 + state->linenr++;
2132 + used = parse_binary(state, buffer + hd + llen,
2133 + size - hd - llen, patch);
2134 + if (used < 0)
2135 + return -1;
2136 + if (used)
2137 + patchsize = used + llen;
2138 + else
2139 + patchsize = 0;
2140 + }
2141 + else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2142 + static const char *binhdr[] = {
2143 + "Binary files ",
2144 + "Files ",
2145 + NULL,
2146 + };
2147 + int i;
2148 + for (i = 0; binhdr[i]; i++) {
2149 + int len = strlen(binhdr[i]);
2150 + if (len < size - hd &&
2151 + !memcmp(binhdr[i], buffer + hd, len)) {
2152 + state->linenr++;
2153 + patch->is_binary = 1;
2154 + patchsize = llen;
2155 + break;
2156 + }
2157 + }
2158 + }
2159 +
2160 + /* Empty patch cannot be applied if it is a text patch
2161 + * without metadata change. A binary patch appears
2162 + * empty to us here.
2163 + */
2164 + if ((state->apply || state->check) &&
2165 + (!patch->is_binary && !metadata_changes(patch))) {
2166 + error(_("patch with only garbage at line %d"), state->linenr);
2167 + return -128;
2168 + }
2169 + }
2170 +
2171 + return offset + hdrsize + patchsize;
2172 +}
2173 +
2174 +#define swap(a,b) myswap((a),(b),sizeof(a))
2175 +
2176 +#define myswap(a, b, size) do { \
2177 + unsigned char mytmp[size]; \
2178 + memcpy(mytmp, &a, size); \
2179 + memcpy(&a, &b, size); \
2180 + memcpy(&b, mytmp, size); \
2181 +} while (0)
2182 +
2183 +static void reverse_patches(struct patch *p)
2184 +{
2185 + for (; p; p = p->next) {
2186 + struct fragment *frag = p->fragments;
2187 +
2188 + swap(p->new_name, p->old_name);
2189 + swap(p->new_mode, p->old_mode);
2190 + swap(p->is_new, p->is_delete);
2191 + swap(p->lines_added, p->lines_deleted);
2192 + swap(p->old_sha1_prefix, p->new_sha1_prefix);
2193 +
2194 + for (; frag; frag = frag->next) {
2195 + swap(frag->newpos, frag->oldpos);
2196 + swap(frag->newlines, frag->oldlines);
2197 + }
2198 + }
2199 +}
2200 +
2201 +static const char pluses[] =
2202 +"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2203 +static const char minuses[]=
2204 +"----------------------------------------------------------------------";
2205 +
2206 +static void show_stats(struct apply_state *state, struct patch *patch)
2207 +{
2208 + struct strbuf qname = STRBUF_INIT;
2209 + char *cp = patch->new_name ? patch->new_name : patch->old_name;
2210 + int max, add, del;
2211 +
2212 + quote_c_style(cp, &qname, NULL, 0);
2213 +
2214 + /*
2215 + * "scale" the filename
2216 + */
2217 + max = state->max_len;
2218 + if (max > 50)
2219 + max = 50;
2220 +
2221 + if (qname.len > max) {
2222 + cp = strchr(qname.buf + qname.len + 3 - max, '/');
2223 + if (!cp)
2224 + cp = qname.buf + qname.len + 3 - max;
2225 + strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2226 + }
2227 +
2228 + if (patch->is_binary) {
2229 + printf(" %-*s | Bin\n", max, qname.buf);
2230 + strbuf_release(&qname);
2231 + return;
2232 + }
2233 +
2234 + printf(" %-*s |", max, qname.buf);
2235 + strbuf_release(&qname);
2236 +
2237 + /*
2238 + * scale the add/delete
2239 + */
2240 + max = max + state->max_change > 70 ? 70 - max : state->max_change;
2241 + add = patch->lines_added;
2242 + del = patch->lines_deleted;
2243 +
2244 + if (state->max_change > 0) {
2245 + int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2246 + add = (add * max + state->max_change / 2) / state->max_change;
2247 + del = total - add;
2248 + }
2249 + printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2250 + add, pluses, del, minuses);
2251 +}
2252 +
2253 +static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2254 +{
2255 + switch (st->st_mode & S_IFMT) {
2256 + case S_IFLNK:
2257 + if (strbuf_readlink(buf, path, st->st_size) < 0)
2258 + return error(_("unable to read symlink %s"), path);
2259 + return 0;
2260 + case S_IFREG:
2261 + if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2262 + return error(_("unable to open or read %s"), path);
2263 + convert_to_git(path, buf->buf, buf->len, buf, 0);
2264 + return 0;
2265 + default:
2266 + return -1;
2267 + }
2268 +}
2269 +
2270 +/*
2271 + * Update the preimage, and the common lines in postimage,
2272 + * from buffer buf of length len. If postlen is 0 the postimage
2273 + * is updated in place, otherwise it's updated on a new buffer
2274 + * of length postlen
2275 + */
2276 +
2277 +static void update_pre_post_images(struct image *preimage,
2278 + struct image *postimage,
2279 + char *buf,
2280 + size_t len, size_t postlen)
2281 +{
2282 + int i, ctx, reduced;
2283 + char *new, *old, *fixed;
2284 + struct image fixed_preimage;
2285 +
2286 + /*
2287 + * Update the preimage with whitespace fixes. Note that we
2288 + * are not losing preimage->buf -- apply_one_fragment() will
2289 + * free "oldlines".
2290 + */
2291 + prepare_image(&fixed_preimage, buf, len, 1);
2292 + assert(postlen
2293 + ? fixed_preimage.nr == preimage->nr
2294 + : fixed_preimage.nr <= preimage->nr);
2295 + for (i = 0; i < fixed_preimage.nr; i++)
2296 + fixed_preimage.line[i].flag = preimage->line[i].flag;
2297 + free(preimage->line_allocated);
2298 + *preimage = fixed_preimage;
2299 +
2300 + /*
2301 + * Adjust the common context lines in postimage. This can be
2302 + * done in-place when we are shrinking it with whitespace
2303 + * fixing, but needs a new buffer when ignoring whitespace or
2304 + * expanding leading tabs to spaces.
2305 + *
2306 + * We trust the caller to tell us if the update can be done
2307 + * in place (postlen==0) or not.
2308 + */
2309 + old = postimage->buf;
2310 + if (postlen)
2311 + new = postimage->buf = xmalloc(postlen);
2312 + else
2313 + new = old;
2314 + fixed = preimage->buf;
2315 +
2316 + for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2317 + size_t l_len = postimage->line[i].len;
2318 + if (!(postimage->line[i].flag & LINE_COMMON)) {
2319 + /* an added line -- no counterparts in preimage */
2320 + memmove(new, old, l_len);
2321 + old += l_len;
2322 + new += l_len;
2323 + continue;
2324 + }
2325 +
2326 + /* a common context -- skip it in the original postimage */
2327 + old += l_len;
2328 +
2329 + /* and find the corresponding one in the fixed preimage */
2330 + while (ctx < preimage->nr &&
2331 + !(preimage->line[ctx].flag & LINE_COMMON)) {
2332 + fixed += preimage->line[ctx].len;
2333 + ctx++;
2334 + }
2335 +
2336 + /*
2337 + * preimage is expected to run out, if the caller
2338 + * fixed addition of trailing blank lines.
2339 + */
2340 + if (preimage->nr <= ctx) {
2341 + reduced++;
2342 + continue;
2343 + }
2344 +
2345 + /* and copy it in, while fixing the line length */
2346 + l_len = preimage->line[ctx].len;
2347 + memcpy(new, fixed, l_len);
2348 + new += l_len;
2349 + fixed += l_len;
2350 + postimage->line[i].len = l_len;
2351 + ctx++;
2352 + }
2353 +
2354 + if (postlen
2355 + ? postlen < new - postimage->buf
2356 + : postimage->len < new - postimage->buf)
2357 + die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2358 + (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2359 +
2360 + /* Fix the length of the whole thing */
2361 + postimage->len = new - postimage->buf;
2362 + postimage->nr -= reduced;
2363 +}
2364 +
2365 +static int line_by_line_fuzzy_match(struct image *img,
2366 + struct image *preimage,
2367 + struct image *postimage,
2368 + unsigned long try,
2369 + int try_lno,
2370 + int preimage_limit)
2371 +{
2372 + int i;
2373 + size_t imgoff = 0;
2374 + size_t preoff = 0;
2375 + size_t postlen = postimage->len;
2376 + size_t extra_chars;
2377 + char *buf;
2378 + char *preimage_eof;
2379 + char *preimage_end;
2380 + struct strbuf fixed;
2381 + char *fixed_buf;
2382 + size_t fixed_len;
2383 +
2384 + for (i = 0; i < preimage_limit; i++) {
2385 + size_t prelen = preimage->line[i].len;
2386 + size_t imglen = img->line[try_lno+i].len;
2387 +
2388 + if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2389 + preimage->buf + preoff, prelen))
2390 + return 0;
2391 + if (preimage->line[i].flag & LINE_COMMON)
2392 + postlen += imglen - prelen;
2393 + imgoff += imglen;
2394 + preoff += prelen;
2395 + }
2396 +
2397 + /*
2398 + * Ok, the preimage matches with whitespace fuzz.
2399 + *
2400 + * imgoff now holds the true length of the target that
2401 + * matches the preimage before the end of the file.
2402 + *
2403 + * Count the number of characters in the preimage that fall
2404 + * beyond the end of the file and make sure that all of them
2405 + * are whitespace characters. (This can only happen if
2406 + * we are removing blank lines at the end of the file.)
2407 + */
2408 + buf = preimage_eof = preimage->buf + preoff;
2409 + for ( ; i < preimage->nr; i++)
2410 + preoff += preimage->line[i].len;
2411 + preimage_end = preimage->buf + preoff;
2412 + for ( ; buf < preimage_end; buf++)
2413 + if (!isspace(*buf))
2414 + return 0;
2415 +
2416 + /*
2417 + * Update the preimage and the common postimage context
2418 + * lines to use the same whitespace as the target.
2419 + * If whitespace is missing in the target (i.e.
2420 + * if the preimage extends beyond the end of the file),
2421 + * use the whitespace from the preimage.
2422 + */
2423 + extra_chars = preimage_end - preimage_eof;
2424 + strbuf_init(&fixed, imgoff + extra_chars);
2425 + strbuf_add(&fixed, img->buf + try, imgoff);
2426 + strbuf_add(&fixed, preimage_eof, extra_chars);
2427 + fixed_buf = strbuf_detach(&fixed, &fixed_len);
2428 + update_pre_post_images(preimage, postimage,
2429 + fixed_buf, fixed_len, postlen);
2430 + return 1;
2431 +}
2432 +
2433 +static int match_fragment(struct apply_state *state,
2434 + struct image *img,
2435 + struct image *preimage,
2436 + struct image *postimage,
2437 + unsigned long try,
2438 + int try_lno,
2439 + unsigned ws_rule,
2440 + int match_beginning, int match_end)
2441 +{
2442 + int i;
2443 + char *fixed_buf, *buf, *orig, *target;
2444 + struct strbuf fixed;
2445 + size_t fixed_len, postlen;
2446 + int preimage_limit;
2447 +
2448 + if (preimage->nr + try_lno <= img->nr) {
2449 + /*
2450 + * The hunk falls within the boundaries of img.
2451 + */
2452 + preimage_limit = preimage->nr;
2453 + if (match_end && (preimage->nr + try_lno != img->nr))
2454 + return 0;
2455 + } else if (state->ws_error_action == correct_ws_error &&
2456 + (ws_rule & WS_BLANK_AT_EOF)) {
2457 + /*
2458 + * This hunk extends beyond the end of img, and we are
2459 + * removing blank lines at the end of the file. This
2460 + * many lines from the beginning of the preimage must
2461 + * match with img, and the remainder of the preimage
2462 + * must be blank.
2463 + */
2464 + preimage_limit = img->nr - try_lno;
2465 + } else {
2466 + /*
2467 + * The hunk extends beyond the end of the img and
2468 + * we are not removing blanks at the end, so we
2469 + * should reject the hunk at this position.
2470 + */
2471 + return 0;
2472 + }
2473 +
2474 + if (match_beginning && try_lno)
2475 + return 0;
2476 +
2477 + /* Quick hash check */
2478 + for (i = 0; i < preimage_limit; i++)
2479 + if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2480 + (preimage->line[i].hash != img->line[try_lno + i].hash))
2481 + return 0;
2482 +
2483 + if (preimage_limit == preimage->nr) {
2484 + /*
2485 + * Do we have an exact match? If we were told to match
2486 + * at the end, size must be exactly at try+fragsize,
2487 + * otherwise try+fragsize must be still within the preimage,
2488 + * and either case, the old piece should match the preimage
2489 + * exactly.
2490 + */
2491 + if ((match_end
2492 + ? (try + preimage->len == img->len)
2493 + : (try + preimage->len <= img->len)) &&
2494 + !memcmp(img->buf + try, preimage->buf, preimage->len))
2495 + return 1;
2496 + } else {
2497 + /*
2498 + * The preimage extends beyond the end of img, so
2499 + * there cannot be an exact match.
2500 + *
2501 + * There must be one non-blank context line that match
2502 + * a line before the end of img.
2503 + */
2504 + char *buf_end;
2505 +
2506 + buf = preimage->buf;
2507 + buf_end = buf;
2508 + for (i = 0; i < preimage_limit; i++)
2509 + buf_end += preimage->line[i].len;
2510 +
2511 + for ( ; buf < buf_end; buf++)
2512 + if (!isspace(*buf))
2513 + break;
2514 + if (buf == buf_end)
2515 + return 0;
2516 + }
2517 +
2518 + /*
2519 + * No exact match. If we are ignoring whitespace, run a line-by-line
2520 + * fuzzy matching. We collect all the line length information because
2521 + * we need it to adjust whitespace if we match.
2522 + */
2523 + if (state->ws_ignore_action == ignore_ws_change)
2524 + return line_by_line_fuzzy_match(img, preimage, postimage,
2525 + try, try_lno, preimage_limit);
2526 +
2527 + if (state->ws_error_action != correct_ws_error)
2528 + return 0;
2529 +
2530 + /*
2531 + * The hunk does not apply byte-by-byte, but the hash says
2532 + * it might with whitespace fuzz. We weren't asked to
2533 + * ignore whitespace, we were asked to correct whitespace
2534 + * errors, so let's try matching after whitespace correction.
2535 + *
2536 + * While checking the preimage against the target, whitespace
2537 + * errors in both fixed, we count how large the corresponding
2538 + * postimage needs to be. The postimage prepared by
2539 + * apply_one_fragment() has whitespace errors fixed on added
2540 + * lines already, but the common lines were propagated as-is,
2541 + * which may become longer when their whitespace errors are
2542 + * fixed.
2543 + */
2544 +
2545 + /* First count added lines in postimage */
2546 + postlen = 0;
2547 + for (i = 0; i < postimage->nr; i++) {
2548 + if (!(postimage->line[i].flag & LINE_COMMON))
2549 + postlen += postimage->line[i].len;
2550 + }
2551 +
2552 + /*
2553 + * The preimage may extend beyond the end of the file,
2554 + * but in this loop we will only handle the part of the
2555 + * preimage that falls within the file.
2556 + */
2557 + strbuf_init(&fixed, preimage->len + 1);
2558 + orig = preimage->buf;
2559 + target = img->buf + try;
2560 + for (i = 0; i < preimage_limit; i++) {
2561 + size_t oldlen = preimage->line[i].len;
2562 + size_t tgtlen = img->line[try_lno + i].len;
2563 + size_t fixstart = fixed.len;
2564 + struct strbuf tgtfix;
2565 + int match;
2566 +
2567 + /* Try fixing the line in the preimage */
2568 + ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2569 +
2570 + /* Try fixing the line in the target */
2571 + strbuf_init(&tgtfix, tgtlen);
2572 + ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2573 +
2574 + /*
2575 + * If they match, either the preimage was based on
2576 + * a version before our tree fixed whitespace breakage,
2577 + * or we are lacking a whitespace-fix patch the tree
2578 + * the preimage was based on already had (i.e. target
2579 + * has whitespace breakage, the preimage doesn't).
2580 + * In either case, we are fixing the whitespace breakages
2581 + * so we might as well take the fix together with their
2582 + * real change.
2583 + */
2584 + match = (tgtfix.len == fixed.len - fixstart &&
2585 + !memcmp(tgtfix.buf, fixed.buf + fixstart,
2586 + fixed.len - fixstart));
2587 +
2588 + /* Add the length if this is common with the postimage */
2589 + if (preimage->line[i].flag & LINE_COMMON)
2590 + postlen += tgtfix.len;
2591 +
2592 + strbuf_release(&tgtfix);
2593 + if (!match)
2594 + goto unmatch_exit;
2595 +
2596 + orig += oldlen;
2597 + target += tgtlen;
2598 + }
2599 +
2600 +
2601 + /*
2602 + * Now handle the lines in the preimage that falls beyond the
2603 + * end of the file (if any). They will only match if they are
2604 + * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2605 + * false).
2606 + */
2607 + for ( ; i < preimage->nr; i++) {
2608 + size_t fixstart = fixed.len; /* start of the fixed preimage */
2609 + size_t oldlen = preimage->line[i].len;
2610 + int j;
2611 +
2612 + /* Try fixing the line in the preimage */
2613 + ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2614 +
2615 + for (j = fixstart; j < fixed.len; j++)
2616 + if (!isspace(fixed.buf[j]))
2617 + goto unmatch_exit;
2618 +
2619 + orig += oldlen;
2620 + }
2621 +
2622 + /*
2623 + * Yes, the preimage is based on an older version that still
2624 + * has whitespace breakages unfixed, and fixing them makes the
2625 + * hunk match. Update the context lines in the postimage.
2626 + */
2627 + fixed_buf = strbuf_detach(&fixed, &fixed_len);
2628 + if (postlen < postimage->len)
2629 + postlen = 0;
2630 + update_pre_post_images(preimage, postimage,
2631 + fixed_buf, fixed_len, postlen);
2632 + return 1;
2633 +
2634 + unmatch_exit:
2635 + strbuf_release(&fixed);
2636 + return 0;
2637 +}
2638 +
2639 +static int find_pos(struct apply_state *state,
2640 + struct image *img,
2641 + struct image *preimage,
2642 + struct image *postimage,
2643 + int line,
2644 + unsigned ws_rule,
2645 + int match_beginning, int match_end)
2646 +{
2647 + int i;
2648 + unsigned long backwards, forwards, try;
2649 + int backwards_lno, forwards_lno, try_lno;
2650 +
2651 + /*
2652 + * If match_beginning or match_end is specified, there is no
2653 + * point starting from a wrong line that will never match and
2654 + * wander around and wait for a match at the specified end.
2655 + */
2656 + if (match_beginning)
2657 + line = 0;
2658 + else if (match_end)
2659 + line = img->nr - preimage->nr;
2660 +
2661 + /*
2662 + * Because the comparison is unsigned, the following test
2663 + * will also take care of a negative line number that can
2664 + * result when match_end and preimage is larger than the target.
2665 + */
2666 + if ((size_t) line > img->nr)
2667 + line = img->nr;
2668 +
2669 + try = 0;
2670 + for (i = 0; i < line; i++)
2671 + try += img->line[i].len;
2672 +
2673 + /*
2674 + * There's probably some smart way to do this, but I'll leave
2675 + * that to the smart and beautiful people. I'm simple and stupid.
2676 + */
2677 + backwards = try;
2678 + backwards_lno = line;
2679 + forwards = try;
2680 + forwards_lno = line;
2681 + try_lno = line;
2682 +
2683 + for (i = 0; ; i++) {
2684 + if (match_fragment(state, img, preimage, postimage,
2685 + try, try_lno, ws_rule,
2686 + match_beginning, match_end))
2687 + return try_lno;
2688 +
2689 + again:
2690 + if (backwards_lno == 0 && forwards_lno == img->nr)
2691 + break;
2692 +
2693 + if (i & 1) {
2694 + if (backwards_lno == 0) {
2695 + i++;
2696 + goto again;
2697 + }
2698 + backwards_lno--;
2699 + backwards -= img->line[backwards_lno].len;
2700 + try = backwards;
2701 + try_lno = backwards_lno;
2702 + } else {
2703 + if (forwards_lno == img->nr) {
2704 + i++;
2705 + goto again;
2706 + }
2707 + forwards += img->line[forwards_lno].len;
2708 + forwards_lno++;
2709 + try = forwards;
2710 + try_lno = forwards_lno;
2711 + }
2712 +
2713 + }
2714 + return -1;
2715 +}
2716 +
2717 +static void remove_first_line(struct image *img)
2718 +{
2719 + img->buf += img->line[0].len;
2720 + img->len -= img->line[0].len;
2721 + img->line++;
2722 + img->nr--;
2723 +}
2724 +
2725 +static void remove_last_line(struct image *img)
2726 +{
2727 + img->len -= img->line[--img->nr].len;
2728 +}
2729 +
2730 +/*
2731 + * The change from "preimage" and "postimage" has been found to
2732 + * apply at applied_pos (counts in line numbers) in "img".
2733 + * Update "img" to remove "preimage" and replace it with "postimage".
2734 + */
2735 +static void update_image(struct apply_state *state,
2736 + struct image *img,
2737 + int applied_pos,
2738 + struct image *preimage,
2739 + struct image *postimage)
2740 +{
2741 + /*
2742 + * remove the copy of preimage at offset in img
2743 + * and replace it with postimage
2744 + */
2745 + int i, nr;
2746 + size_t remove_count, insert_count, applied_at = 0;
2747 + char *result;
2748 + int preimage_limit;
2749 +
2750 + /*
2751 + * If we are removing blank lines at the end of img,
2752 + * the preimage may extend beyond the end.
2753 + * If that is the case, we must be careful only to
2754 + * remove the part of the preimage that falls within
2755 + * the boundaries of img. Initialize preimage_limit
2756 + * to the number of lines in the preimage that falls
2757 + * within the boundaries.
2758 + */
2759 + preimage_limit = preimage->nr;
2760 + if (preimage_limit > img->nr - applied_pos)
2761 + preimage_limit = img->nr - applied_pos;
2762 +
2763 + for (i = 0; i < applied_pos; i++)
2764 + applied_at += img->line[i].len;
2765 +
2766 + remove_count = 0;
2767 + for (i = 0; i < preimage_limit; i++)
2768 + remove_count += img->line[applied_pos + i].len;
2769 + insert_count = postimage->len;
2770 +
2771 + /* Adjust the contents */
2772 + result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2773 + memcpy(result, img->buf, applied_at);
2774 + memcpy(result + applied_at, postimage->buf, postimage->len);
2775 + memcpy(result + applied_at + postimage->len,
2776 + img->buf + (applied_at + remove_count),
2777 + img->len - (applied_at + remove_count));
2778 + free(img->buf);
2779 + img->buf = result;
2780 + img->len += insert_count - remove_count;
2781 + result[img->len] = '\0';
2782 +
2783 + /* Adjust the line table */
2784 + nr = img->nr + postimage->nr - preimage_limit;
2785 + if (preimage_limit < postimage->nr) {
2786 + /*
2787 + * NOTE: this knows that we never call remove_first_line()
2788 + * on anything other than pre/post image.
2789 + */
2790 + REALLOC_ARRAY(img->line, nr);
2791 + img->line_allocated = img->line;
2792 + }
2793 + if (preimage_limit != postimage->nr)
2794 + memmove(img->line + applied_pos + postimage->nr,
2795 + img->line + applied_pos + preimage_limit,
2796 + (img->nr - (applied_pos + preimage_limit)) *
2797 + sizeof(*img->line));
2798 + memcpy(img->line + applied_pos,
2799 + postimage->line,
2800 + postimage->nr * sizeof(*img->line));
2801 + if (!state->allow_overlap)
2802 + for (i = 0; i < postimage->nr; i++)
2803 + img->line[applied_pos + i].flag |= LINE_PATCHED;
2804 + img->nr = nr;
2805 +}
2806 +
2807 +/*
2808 + * Use the patch-hunk text in "frag" to prepare two images (preimage and
2809 + * postimage) for the hunk. Find lines that match "preimage" in "img" and
2810 + * replace the part of "img" with "postimage" text.
2811 + */
2812 +static int apply_one_fragment(struct apply_state *state,
2813 + struct image *img, struct fragment *frag,
2814 + int inaccurate_eof, unsigned ws_rule,
2815 + int nth_fragment)
2816 +{
2817 + int match_beginning, match_end;
2818 + const char *patch = frag->patch;
2819 + int size = frag->size;
2820 + char *old, *oldlines;
2821 + struct strbuf newlines;
2822 + int new_blank_lines_at_end = 0;
2823 + int found_new_blank_lines_at_end = 0;
2824 + int hunk_linenr = frag->linenr;
2825 + unsigned long leading, trailing;
2826 + int pos, applied_pos;
2827 + struct image preimage;
2828 + struct image postimage;
2829 +
2830 + memset(&preimage, 0, sizeof(preimage));
2831 + memset(&postimage, 0, sizeof(postimage));
2832 + oldlines = xmalloc(size);
2833 + strbuf_init(&newlines, size);
2834 +
2835 + old = oldlines;
2836 + while (size > 0) {
2837 + char first;
2838 + int len = linelen(patch, size);
2839 + int plen;
2840 + int added_blank_line = 0;
2841 + int is_blank_context = 0;
2842 + size_t start;
2843 +
2844 + if (!len)
2845 + break;
2846 +
2847 + /*
2848 + * "plen" is how much of the line we should use for
2849 + * the actual patch data. Normally we just remove the
2850 + * first character on the line, but if the line is
2851 + * followed by "\ No newline", then we also remove the
2852 + * last one (which is the newline, of course).
2853 + */
2854 + plen = len - 1;
2855 + if (len < size && patch[len] == '\\')
2856 + plen--;
2857 + first = *patch;
2858 + if (state->apply_in_reverse) {
2859 + if (first == '-')
2860 + first = '+';
2861 + else if (first == '+')
2862 + first = '-';
2863 + }
2864 +
2865 + switch (first) {
2866 + case '\n':
2867 + /* Newer GNU diff, empty context line */
2868 + if (plen < 0)
2869 + /* ... followed by '\No newline'; nothing */
2870 + break;
2871 + *old++ = '\n';
2872 + strbuf_addch(&newlines, '\n');
2873 + add_line_info(&preimage, "\n", 1, LINE_COMMON);
2874 + add_line_info(&postimage, "\n", 1, LINE_COMMON);
2875 + is_blank_context = 1;
2876 + break;
2877 + case ' ':
2878 + if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2879 + ws_blank_line(patch + 1, plen, ws_rule))
2880 + is_blank_context = 1;
2881 + case '-':
2882 + memcpy(old, patch + 1, plen);
2883 + add_line_info(&preimage, old, plen,
2884 + (first == ' ' ? LINE_COMMON : 0));
2885 + old += plen;
2886 + if (first == '-')
2887 + break;
2888 + /* Fall-through for ' ' */
2889 + case '+':
2890 + /* --no-add does not add new lines */
2891 + if (first == '+' && state->no_add)
2892 + break;
2893 +
2894 + start = newlines.len;
2895 + if (first != '+' ||
2896 + !state->whitespace_error ||
2897 + state->ws_error_action != correct_ws_error) {
2898 + strbuf_add(&newlines, patch + 1, plen);
2899 + }
2900 + else {
2901 + ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2902 + }
2903 + add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2904 + (first == '+' ? 0 : LINE_COMMON));
2905 + if (first == '+' &&
2906 + (ws_rule & WS_BLANK_AT_EOF) &&
2907 + ws_blank_line(patch + 1, plen, ws_rule))
2908 + added_blank_line = 1;
2909 + break;
2910 + case '@': case '\\':
2911 + /* Ignore it, we already handled it */
2912 + break;
2913 + default:
2914 + if (state->apply_verbosely)
2915 + error(_("invalid start of line: '%c'"), first);
2916 + applied_pos = -1;
2917 + goto out;
2918 + }
2919 + if (added_blank_line) {
2920 + if (!new_blank_lines_at_end)
2921 + found_new_blank_lines_at_end = hunk_linenr;
2922 + new_blank_lines_at_end++;
2923 + }
2924 + else if (is_blank_context)
2925 + ;
2926 + else
2927 + new_blank_lines_at_end = 0;
2928 + patch += len;
2929 + size -= len;
2930 + hunk_linenr++;
2931 + }
2932 + if (inaccurate_eof &&
2933 + old > oldlines && old[-1] == '\n' &&
2934 + newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2935 + old--;
2936 + strbuf_setlen(&newlines, newlines.len - 1);
2937 + }
2938 +
2939 + leading = frag->leading;
2940 + trailing = frag->trailing;
2941 +
2942 + /*
2943 + * A hunk to change lines at the beginning would begin with
2944 + * @@ -1,L +N,M @@
2945 + * but we need to be careful. -U0 that inserts before the second
2946 + * line also has this pattern.
2947 + *
2948 + * And a hunk to add to an empty file would begin with
2949 + * @@ -0,0 +N,M @@
2950 + *
2951 + * In other words, a hunk that is (frag->oldpos <= 1) with or
2952 + * without leading context must match at the beginning.
2953 + */
2954 + match_beginning = (!frag->oldpos ||
2955 + (frag->oldpos == 1 && !state->unidiff_zero));
2956 +
2957 + /*
2958 + * A hunk without trailing lines must match at the end.
2959 + * However, we simply cannot tell if a hunk must match end
2960 + * from the lack of trailing lines if the patch was generated
2961 + * with unidiff without any context.
2962 + */
2963 + match_end = !state->unidiff_zero && !trailing;
2964 +
2965 + pos = frag->newpos ? (frag->newpos - 1) : 0;
2966 + preimage.buf = oldlines;
2967 + preimage.len = old - oldlines;
2968 + postimage.buf = newlines.buf;
2969 + postimage.len = newlines.len;
2970 + preimage.line = preimage.line_allocated;
2971 + postimage.line = postimage.line_allocated;
2972 +
2973 + for (;;) {
2974 +
2975 + applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2976 + ws_rule, match_beginning, match_end);
2977 +
2978 + if (applied_pos >= 0)
2979 + break;
2980 +
2981 + /* Am I at my context limits? */
2982 + if ((leading <= state->p_context) && (trailing <= state->p_context))
2983 + break;
2984 + if (match_beginning || match_end) {
2985 + match_beginning = match_end = 0;
2986 + continue;
2987 + }
2988 +
2989 + /*
2990 + * Reduce the number of context lines; reduce both
2991 + * leading and trailing if they are equal otherwise
2992 + * just reduce the larger context.
2993 + */
2994 + if (leading >= trailing) {
2995 + remove_first_line(&preimage);
2996 + remove_first_line(&postimage);
2997 + pos--;
2998 + leading--;
2999 + }
3000 + if (trailing > leading) {
3001 + remove_last_line(&preimage);
3002 + remove_last_line(&postimage);
3003 + trailing--;
3004 + }
3005 + }
3006 +
3007 + if (applied_pos >= 0) {
3008 + if (new_blank_lines_at_end &&
3009 + preimage.nr + applied_pos >= img->nr &&
3010 + (ws_rule & WS_BLANK_AT_EOF) &&
3011 + state->ws_error_action != nowarn_ws_error) {
3012 + record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
3013 + found_new_blank_lines_at_end);
3014 + if (state->ws_error_action == correct_ws_error) {
3015 + while (new_blank_lines_at_end--)
3016 + remove_last_line(&postimage);
3017 + }
3018 + /*
3019 + * We would want to prevent write_out_results()
3020 + * from taking place in apply_patch() that follows
3021 + * the callchain led us here, which is:
3022 + * apply_patch->check_patch_list->check_patch->
3023 + * apply_data->apply_fragments->apply_one_fragment
3024 + */
3025 + if (state->ws_error_action == die_on_ws_error)
3026 + state->apply = 0;
3027 + }
3028 +
3029 + if (state->apply_verbosely && applied_pos != pos) {
3030 + int offset = applied_pos - pos;
3031 + if (state->apply_in_reverse)
3032 + offset = 0 - offset;
3033 + fprintf_ln(stderr,
3034 + Q_("Hunk #%d succeeded at %d (offset %d line).",
3035 + "Hunk #%d succeeded at %d (offset %d lines).",
3036 + offset),
3037 + nth_fragment, applied_pos + 1, offset);
3038 + }
3039 +
3040 + /*
3041 + * Warn if it was necessary to reduce the number
3042 + * of context lines.
3043 + */
3044 + if ((leading != frag->leading) ||
3045 + (trailing != frag->trailing))
3046 + fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
3047 + " to apply fragment at %d"),
3048 + leading, trailing, applied_pos+1);
3049 + update_image(state, img, applied_pos, &preimage, &postimage);
3050 + } else {
3051 + if (state->apply_verbosely)
3052 + error(_("while searching for:\n%.*s"),
3053 + (int)(old - oldlines), oldlines);
3054 + }
3055 +
3056 +out:
3057 + free(oldlines);
3058 + strbuf_release(&newlines);
3059 + free(preimage.line_allocated);
3060 + free(postimage.line_allocated);
3061 +
3062 + return (applied_pos < 0);
3063 +}
3064 +
3065 +static int apply_binary_fragment(struct apply_state *state,
3066 + struct image *img,
3067 + struct patch *patch)
3068 +{
3069 + struct fragment *fragment = patch->fragments;
3070 + unsigned long len;
3071 + void *dst;
3072 +
3073 + if (!fragment)
3074 + return error(_("missing binary patch data for '%s'"),
3075 + patch->new_name ?
3076 + patch->new_name :
3077 + patch->old_name);
3078 +
3079 + /* Binary patch is irreversible without the optional second hunk */
3080 + if (state->apply_in_reverse) {
3081 + if (!fragment->next)
3082 + return error("cannot reverse-apply a binary patch "
3083 + "without the reverse hunk to '%s'",
3084 + patch->new_name
3085 + ? patch->new_name : patch->old_name);
3086 + fragment = fragment->next;
3087 + }
3088 + switch (fragment->binary_patch_method) {
3089 + case BINARY_DELTA_DEFLATED:
3090 + dst = patch_delta(img->buf, img->len, fragment->patch,
3091 + fragment->size, &len);
3092 + if (!dst)
3093 + return -1;
3094 + clear_image(img);
3095 + img->buf = dst;
3096 + img->len = len;
3097 + return 0;
3098 + case BINARY_LITERAL_DEFLATED:
3099 + clear_image(img);
3100 + img->len = fragment->size;
3101 + img->buf = xmemdupz(fragment->patch, img->len);
3102 + return 0;
3103 + }
3104 + return -1;
3105 +}
3106 +
3107 +/*
3108 + * Replace "img" with the result of applying the binary patch.
3109 + * The binary patch data itself in patch->fragment is still kept
3110 + * but the preimage prepared by the caller in "img" is freed here
3111 + * or in the helper function apply_binary_fragment() this calls.
3112 + */
3113 +static int apply_binary(struct apply_state *state,
3114 + struct image *img,
3115 + struct patch *patch)
3116 +{
3117 + const char *name = patch->old_name ? patch->old_name : patch->new_name;
3118 + unsigned char sha1[20];
3119 +
3120 + /*
3121 + * For safety, we require patch index line to contain
3122 + * full 40-byte textual SHA1 for old and new, at least for now.
3123 + */
3124 + if (strlen(patch->old_sha1_prefix) != 40 ||
3125 + strlen(patch->new_sha1_prefix) != 40 ||
3126 + get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3127 + get_sha1_hex(patch->new_sha1_prefix, sha1))
3128 + return error("cannot apply binary patch to '%s' "
3129 + "without full index line", name);
3130 +
3131 + if (patch->old_name) {
3132 + /*
3133 + * See if the old one matches what the patch
3134 + * applies to.
3135 + */
3136 + hash_sha1_file(img->buf, img->len, blob_type, sha1);
3137 + if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3138 + return error("the patch applies to '%s' (%s), "
3139 + "which does not match the "
3140 + "current contents.",
3141 + name, sha1_to_hex(sha1));
3142 + }
3143 + else {
3144 + /* Otherwise, the old one must be empty. */
3145 + if (img->len)
3146 + return error("the patch applies to an empty "
3147 + "'%s' but it is not empty", name);
3148 + }
3149 +
3150 + get_sha1_hex(patch->new_sha1_prefix, sha1);
3151 + if (is_null_sha1(sha1)) {
3152 + clear_image(img);
3153 + return 0; /* deletion patch */
3154 + }
3155 +
3156 + if (has_sha1_file(sha1)) {
3157 + /* We already have the postimage */
3158 + enum object_type type;
3159 + unsigned long size;
3160 + char *result;
3161 +
3162 + result = read_sha1_file(sha1, &type, &size);
3163 + if (!result)
3164 + return error("the necessary postimage %s for "
3165 + "'%s' cannot be read",
3166 + patch->new_sha1_prefix, name);
3167 + clear_image(img);
3168 + img->buf = result;
3169 + img->len = size;
3170 + } else {
3171 + /*
3172 + * We have verified buf matches the preimage;
3173 + * apply the patch data to it, which is stored
3174 + * in the patch->fragments->{patch,size}.
3175 + */
3176 + if (apply_binary_fragment(state, img, patch))
3177 + return error(_("binary patch does not apply to '%s'"),
3178 + name);
3179 +
3180 + /* verify that the result matches */
3181 + hash_sha1_file(img->buf, img->len, blob_type, sha1);
3182 + if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3183 + return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3184 + name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3185 + }
3186 +
3187 + return 0;
3188 +}
3189 +
3190 +static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3191 +{
3192 + struct fragment *frag = patch->fragments;
3193 + const char *name = patch->old_name ? patch->old_name : patch->new_name;
3194 + unsigned ws_rule = patch->ws_rule;
3195 + unsigned inaccurate_eof = patch->inaccurate_eof;
3196 + int nth = 0;
3197 +
3198 + if (patch->is_binary)
3199 + return apply_binary(state, img, patch);
3200 +
3201 + while (frag) {
3202 + nth++;
3203 + if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3204 + error(_("patch failed: %s:%ld"), name, frag->oldpos);
3205 + if (!state->apply_with_reject)
3206 + return -1;
3207 + frag->rejected = 1;
3208 + }
3209 + frag = frag->next;
3210 + }
3211 + return 0;
3212 +}
3213 +
3214 +static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3215 +{
3216 + if (S_ISGITLINK(mode)) {
3217 + strbuf_grow(buf, 100);
3218 + strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3219 + } else {
3220 + enum object_type type;
3221 + unsigned long sz;
3222 + char *result;
3223 +
3224 + result = read_sha1_file(sha1, &type, &sz);
3225 + if (!result)
3226 + return -1;
3227 + /* XXX read_sha1_file NUL-terminates */
3228 + strbuf_attach(buf, result, sz, sz + 1);
3229 + }
3230 + return 0;
3231 +}
3232 +
3233 +static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3234 +{
3235 + if (!ce)
3236 + return 0;
3237 + return read_blob_object(buf, ce->sha1, ce->ce_mode);
3238 +}
3239 +
3240 +static struct patch *in_fn_table(struct apply_state *state, const char *name)
3241 +{
3242 + struct string_list_item *item;
3243 +
3244 + if (name == NULL)
3245 + return NULL;
3246 +
3247 + item = string_list_lookup(&state->fn_table, name);
3248 + if (item != NULL)
3249 + return (struct patch *)item->util;
3250 +
3251 + return NULL;
3252 +}
3253 +
3254 +/*
3255 + * item->util in the filename table records the status of the path.
3256 + * Usually it points at a patch (whose result records the contents
3257 + * of it after applying it), but it could be PATH_WAS_DELETED for a
3258 + * path that a previously applied patch has already removed, or
3259 + * PATH_TO_BE_DELETED for a path that a later patch would remove.
3260 + *
3261 + * The latter is needed to deal with a case where two paths A and B
3262 + * are swapped by first renaming A to B and then renaming B to A;
3263 + * moving A to B should not be prevented due to presence of B as we
3264 + * will remove it in a later patch.
3265 + */
3266 +#define PATH_TO_BE_DELETED ((struct patch *) -2)
3267 +#define PATH_WAS_DELETED ((struct patch *) -1)
3268 +
3269 +static int to_be_deleted(struct patch *patch)
3270 +{
3271 + return patch == PATH_TO_BE_DELETED;
3272 +}
3273 +
3274 +static int was_deleted(struct patch *patch)
3275 +{
3276 + return patch == PATH_WAS_DELETED;
3277 +}
3278 +
3279 +static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3280 +{
3281 + struct string_list_item *item;
3282 +
3283 + /*
3284 + * Always add new_name unless patch is a deletion
3285 + * This should cover the cases for normal diffs,
3286 + * file creations and copies
3287 + */
3288 + if (patch->new_name != NULL) {
3289 + item = string_list_insert(&state->fn_table, patch->new_name);
3290 + item->util = patch;
3291 + }
3292 +
3293 + /*
3294 + * store a failure on rename/deletion cases because
3295 + * later chunks shouldn't patch old names
3296 + */
3297 + if ((patch->new_name == NULL) || (patch->is_rename)) {
3298 + item = string_list_insert(&state->fn_table, patch->old_name);
3299 + item->util = PATH_WAS_DELETED;
3300 + }
3301 +}
3302 +
3303 +static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3304 +{
3305 + /*
3306 + * store information about incoming file deletion
3307 + */
3308 + while (patch) {
3309 + if ((patch->new_name == NULL) || (patch->is_rename)) {
3310 + struct string_list_item *item;
3311 + item = string_list_insert(&state->fn_table, patch->old_name);
3312 + item->util = PATH_TO_BE_DELETED;
3313 + }
3314 + patch = patch->next;
3315 + }
3316 +}
3317 +
3318 +static int checkout_target(struct index_state *istate,
3319 + struct cache_entry *ce, struct stat *st)
3320 +{
3321 + struct checkout costate;
3322 +
3323 + memset(&costate, 0, sizeof(costate));
3324 + costate.base_dir = "";
3325 + costate.refresh_cache = 1;
3326 + costate.istate = istate;
3327 + if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3328 + return error(_("cannot checkout %s"), ce->name);
3329 + return 0;
3330 +}
3331 +
3332 +static struct patch *previous_patch(struct apply_state *state,
3333 + struct patch *patch,
3334 + int *gone)
3335 +{
3336 + struct patch *previous;
3337 +
3338 + *gone = 0;
3339 + if (patch->is_copy || patch->is_rename)
3340 + return NULL; /* "git" patches do not depend on the order */
3341 +
3342 + previous = in_fn_table(state, patch->old_name);
3343 + if (!previous)
3344 + return NULL;
3345 +
3346 + if (to_be_deleted(previous))
3347 + return NULL; /* the deletion hasn't happened yet */
3348 +
3349 + if (was_deleted(previous))
3350 + *gone = 1;
3351 +
3352 + return previous;
3353 +}
3354 +
3355 +static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3356 +{
3357 + if (S_ISGITLINK(ce->ce_mode)) {
3358 + if (!S_ISDIR(st->st_mode))
3359 + return -1;
3360 + return 0;
3361 + }
3362 + return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3363 +}
3364 +
3365 +#define SUBMODULE_PATCH_WITHOUT_INDEX 1
3366 +
3367 +static int load_patch_target(struct apply_state *state,
3368 + struct strbuf *buf,
3369 + const struct cache_entry *ce,
3370 + struct stat *st,
3371 + const char *name,
3372 + unsigned expected_mode)
3373 +{
3374 + if (state->cached || state->check_index) {
3375 + if (read_file_or_gitlink(ce, buf))
3376 + return error(_("failed to read %s"), name);
3377 + } else if (name) {
3378 + if (S_ISGITLINK(expected_mode)) {
3379 + if (ce)
3380 + return read_file_or_gitlink(ce, buf);
3381 + else
3382 + return SUBMODULE_PATCH_WITHOUT_INDEX;
3383 + } else if (has_symlink_leading_path(name, strlen(name))) {
3384 + return error(_("reading from '%s' beyond a symbolic link"), name);
3385 + } else {
3386 + if (read_old_data(st, name, buf))
3387 + return error(_("failed to read %s"), name);
3388 + }
3389 + }
3390 + return 0;
3391 +}
3392 +
3393 +/*
3394 + * We are about to apply "patch"; populate the "image" with the
3395 + * current version we have, from the working tree or from the index,
3396 + * depending on the situation e.g. --cached/--index. If we are
3397 + * applying a non-git patch that incrementally updates the tree,
3398 + * we read from the result of a previous diff.
3399 + */
3400 +static int load_preimage(struct apply_state *state,
3401 + struct image *image,
3402 + struct patch *patch, struct stat *st,
3403 + const struct cache_entry *ce)
3404 +{
3405 + struct strbuf buf = STRBUF_INIT;
3406 + size_t len;
3407 + char *img;
3408 + struct patch *previous;
3409 + int status;
3410 +
3411 + previous = previous_patch(state, patch, &status);
3412 + if (status)
3413 + return error(_("path %s has been renamed/deleted"),
3414 + patch->old_name);
3415 + if (previous) {
3416 + /* We have a patched copy in memory; use that. */
3417 + strbuf_add(&buf, previous->result, previous->resultsize);
3418 + } else {
3419 + status = load_patch_target(state, &buf, ce, st,
3420 + patch->old_name, patch->old_mode);
3421 + if (status < 0)
3422 + return status;
3423 + else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3424 + /*
3425 + * There is no way to apply subproject
3426 + * patch without looking at the index.
3427 + * NEEDSWORK: shouldn't this be flagged
3428 + * as an error???
3429 + */
3430 + free_fragment_list(patch->fragments);
3431 + patch->fragments = NULL;
3432 + } else if (status) {
3433 + return error(_("failed to read %s"), patch->old_name);
3434 + }
3435 + }
3436 +
3437 + img = strbuf_detach(&buf, &len);
3438 + prepare_image(image, img, len, !patch->is_binary);
3439 + return 0;
3440 +}
3441 +
3442 +static int three_way_merge(struct image *image,
3443 + char *path,
3444 + const unsigned char *base,
3445 + const unsigned char *ours,
3446 + const unsigned char *theirs)
3447 +{
3448 + mmfile_t base_file, our_file, their_file;
3449 + mmbuffer_t result = { NULL };
3450 + int status;
3451 +
3452 + read_mmblob(&base_file, base);
3453 + read_mmblob(&our_file, ours);
3454 + read_mmblob(&their_file, theirs);
3455 + status = ll_merge(&result, path,
3456 + &base_file, "base",
3457 + &our_file, "ours",
3458 + &their_file, "theirs", NULL);
3459 + free(base_file.ptr);
3460 + free(our_file.ptr);
3461 + free(their_file.ptr);
3462 + if (status < 0 || !result.ptr) {
3463 + free(result.ptr);
3464 + return -1;
3465 + }
3466 + clear_image(image);
3467 + image->buf = result.ptr;
3468 + image->len = result.size;
3469 +
3470 + return status;
3471 +}
3472 +
3473 +/*
3474 + * When directly falling back to add/add three-way merge, we read from
3475 + * the current contents of the new_name. In no cases other than that
3476 + * this function will be called.
3477 + */
3478 +static int load_current(struct apply_state *state,
3479 + struct image *image,
3480 + struct patch *patch)
3481 +{
3482 + struct strbuf buf = STRBUF_INIT;
3483 + int status, pos;
3484 + size_t len;
3485 + char *img;
3486 + struct stat st;
3487 + struct cache_entry *ce;
3488 + char *name = patch->new_name;
3489 + unsigned mode = patch->new_mode;
3490 +
3491 + if (!patch->is_new)
3492 + die("BUG: patch to %s is not a creation", patch->old_name);
3493 +
3494 + pos = cache_name_pos(name, strlen(name));
3495 + if (pos < 0)
3496 + return error(_("%s: does not exist in index"), name);
3497 + ce = active_cache[pos];
3498 + if (lstat(name, &st)) {
3499 + if (errno != ENOENT)
3500 + return error(_("%s: %s"), name, strerror(errno));
3501 + if (checkout_target(&the_index, ce, &st))
3502 + return -1;
3503 + }
3504 + if (verify_index_match(ce, &st))
3505 + return error(_("%s: does not match index"), name);
3506 +
3507 + status = load_patch_target(state, &buf, ce, &st, name, mode);
3508 + if (status < 0)
3509 + return status;
3510 + else if (status)
3511 + return -1;
3512 + img = strbuf_detach(&buf, &len);
3513 + prepare_image(image, img, len, !patch->is_binary);
3514 + return 0;
3515 +}
3516 +
3517 +static int try_threeway(struct apply_state *state,
3518 + struct image *image,
3519 + struct patch *patch,
3520 + struct stat *st,
3521 + const struct cache_entry *ce)
3522 +{
3523 + unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3524 + struct strbuf buf = STRBUF_INIT;
3525 + size_t len;
3526 + int status;
3527 + char *img;
3528 + struct image tmp_image;
3529 +
3530 + /* No point falling back to 3-way merge in these cases */
3531 + if (patch->is_delete ||
3532 + S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3533 + return -1;
3534 +
3535 + /* Preimage the patch was prepared for */
3536 + if (patch->is_new)
3537 + write_sha1_file("", 0, blob_type, pre_sha1);
3538 + else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3539 + read_blob_object(&buf, pre_sha1, patch->old_mode))
3540 + return error("repository lacks the necessary blob to fall back on 3-way merge.");
3541 +
3542 + fprintf(stderr, "Falling back to three-way merge...\n");
3543 +
3544 + img = strbuf_detach(&buf, &len);
3545 + prepare_image(&tmp_image, img, len, 1);
3546 + /* Apply the patch to get the post image */
3547 + if (apply_fragments(state, &tmp_image, patch) < 0) {
3548 + clear_image(&tmp_image);
3549 + return -1;
3550 + }
3551 + /* post_sha1[] is theirs */
3552 + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3553 + clear_image(&tmp_image);
3554 +
3555 + /* our_sha1[] is ours */
3556 + if (patch->is_new) {
3557 + if (load_current(state, &tmp_image, patch))
3558 + return error("cannot read the current contents of '%s'",
3559 + patch->new_name);
3560 + } else {
3561 + if (load_preimage(state, &tmp_image, patch, st, ce))
3562 + return error("cannot read the current contents of '%s'",
3563 + patch->old_name);
3564 + }
3565 + write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3566 + clear_image(&tmp_image);
3567 +
3568 + /* in-core three-way merge between post and our using pre as base */
3569 + status = three_way_merge(image, patch->new_name,
3570 + pre_sha1, our_sha1, post_sha1);
3571 + if (status < 0) {
3572 + fprintf(stderr, "Failed to fall back on three-way merge...\n");
3573 + return status;
3574 + }
3575 +
3576 + if (status) {
3577 + patch->conflicted_threeway = 1;
3578 + if (patch->is_new)
3579 + oidclr(&patch->threeway_stage[0]);
3580 + else
3581 + hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3582 + hashcpy(patch->threeway_stage[1].hash, our_sha1);
3583 + hashcpy(patch->threeway_stage[2].hash, post_sha1);
3584 + fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3585 + } else {
3586 + fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3587 + }
3588 + return 0;
3589 +}
3590 +
3591 +static int apply_data(struct apply_state *state, struct patch *patch,
3592 + struct stat *st, const struct cache_entry *ce)
3593 +{
3594 + struct image image;
3595 +
3596 + if (load_preimage(state, &image, patch, st, ce) < 0)
3597 + return -1;
3598 +
3599 + if (patch->direct_to_threeway ||
3600 + apply_fragments(state, &image, patch) < 0) {
3601 + /* Note: with --reject, apply_fragments() returns 0 */
3602 + if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3603 + return -1;
3604 + }
3605 + patch->result = image.buf;
3606 + patch->resultsize = image.len;
3607 + add_to_fn_table(state, patch);
3608 + free(image.line_allocated);
3609 +
3610 + if (0 < patch->is_delete && patch->resultsize)
3611 + return error(_("removal patch leaves file contents"));
3612 +
3613 + return 0;
3614 +}
3615 +
3616 +/*
3617 + * If "patch" that we are looking at modifies or deletes what we have,
3618 + * we would want it not to lose any local modification we have, either
3619 + * in the working tree or in the index.
3620 + *
3621 + * This also decides if a non-git patch is a creation patch or a
3622 + * modification to an existing empty file. We do not check the state
3623 + * of the current tree for a creation patch in this function; the caller
3624 + * check_patch() separately makes sure (and errors out otherwise) that
3625 + * the path the patch creates does not exist in the current tree.
3626 + */
3627 +static int check_preimage(struct apply_state *state,
3628 + struct patch *patch,
3629 + struct cache_entry **ce,
3630 + struct stat *st)
3631 +{
3632 + const char *old_name = patch->old_name;
3633 + struct patch *previous = NULL;
3634 + int stat_ret = 0, status;
3635 + unsigned st_mode = 0;
3636 +
3637 + if (!old_name)
3638 + return 0;
3639 +
3640 + assert(patch->is_new <= 0);
3641 + previous = previous_patch(state, patch, &status);
3642 +
3643 + if (status)
3644 + return error(_("path %s has been renamed/deleted"), old_name);
3645 + if (previous) {
3646 + st_mode = previous->new_mode;
3647 + } else if (!state->cached) {
3648 + stat_ret = lstat(old_name, st);
3649 + if (stat_ret && errno != ENOENT)
3650 + return error(_("%s: %s"), old_name, strerror(errno));
3651 + }
3652 +
3653 + if (state->check_index && !previous) {
3654 + int pos = cache_name_pos(old_name, strlen(old_name));
3655 + if (pos < 0) {
3656 + if (patch->is_new < 0)
3657 + goto is_new;
3658 + return error(_("%s: does not exist in index"), old_name);
3659 + }
3660 + *ce = active_cache[pos];
3661 + if (stat_ret < 0) {
3662 + if (checkout_target(&the_index, *ce, st))
3663 + return -1;
3664 + }
3665 + if (!state->cached && verify_index_match(*ce, st))
3666 + return error(_("%s: does not match index"), old_name);
3667 + if (state->cached)
3668 + st_mode = (*ce)->ce_mode;
3669 + } else if (stat_ret < 0) {
3670 + if (patch->is_new < 0)
3671 + goto is_new;
3672 + return error(_("%s: %s"), old_name, strerror(errno));
3673 + }
3674 +
3675 + if (!state->cached && !previous)
3676 + st_mode = ce_mode_from_stat(*ce, st->st_mode);
3677 +
3678 + if (patch->is_new < 0)
3679 + patch->is_new = 0;
3680 + if (!patch->old_mode)
3681 + patch->old_mode = st_mode;
3682 + if ((st_mode ^ patch->old_mode) & S_IFMT)
3683 + return error(_("%s: wrong type"), old_name);
3684 + if (st_mode != patch->old_mode)
3685 + warning(_("%s has type %o, expected %o"),
3686 + old_name, st_mode, patch->old_mode);
3687 + if (!patch->new_mode && !patch->is_delete)
3688 + patch->new_mode = st_mode;
3689 + return 0;
3690 +
3691 + is_new:
3692 + patch->is_new = 1;
3693 + patch->is_delete = 0;
3694 + free(patch->old_name);
3695 + patch->old_name = NULL;
3696 + return 0;
3697 +}
3698 +
3699 +
3700 +#define EXISTS_IN_INDEX 1
3701 +#define EXISTS_IN_WORKTREE 2
3702 +
3703 +static int check_to_create(struct apply_state *state,
3704 + const char *new_name,
3705 + int ok_if_exists)
3706 +{
3707 + struct stat nst;
3708 +
3709 + if (state->check_index &&
3710 + cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3711 + !ok_if_exists)
3712 + return EXISTS_IN_INDEX;
3713 + if (state->cached)
3714 + return 0;
3715 +
3716 + if (!lstat(new_name, &nst)) {
3717 + if (S_ISDIR(nst.st_mode) || ok_if_exists)
3718 + return 0;
3719 + /*
3720 + * A leading component of new_name might be a symlink
3721 + * that is going to be removed with this patch, but
3722 + * still pointing at somewhere that has the path.
3723 + * In such a case, path "new_name" does not exist as
3724 + * far as git is concerned.
3725 + */
3726 + if (has_symlink_leading_path(new_name, strlen(new_name)))
3727 + return 0;
3728 +
3729 + return EXISTS_IN_WORKTREE;
3730 + } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3731 + return error("%s: %s", new_name, strerror(errno));
3732 + }
3733 + return 0;
3734 +}
3735 +
3736 +static uintptr_t register_symlink_changes(struct apply_state *state,
3737 + const char *path,
3738 + uintptr_t what)
3739 +{
3740 + struct string_list_item *ent;
3741 +
3742 + ent = string_list_lookup(&state->symlink_changes, path);
3743 + if (!ent) {
3744 + ent = string_list_insert(&state->symlink_changes, path);
3745 + ent->util = (void *)0;
3746 + }
3747 + ent->util = (void *)(what | ((uintptr_t)ent->util));
3748 + return (uintptr_t)ent->util;
3749 +}
3750 +
3751 +static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3752 +{
3753 + struct string_list_item *ent;
3754 +
3755 + ent = string_list_lookup(&state->symlink_changes, path);
3756 + if (!ent)
3757 + return 0;
3758 + return (uintptr_t)ent->util;
3759 +}
3760 +
3761 +static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3762 +{
3763 + for ( ; patch; patch = patch->next) {
3764 + if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3765 + (patch->is_rename || patch->is_delete))
3766 + /* the symlink at patch->old_name is removed */
3767 + register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3768 +
3769 + if (patch->new_name && S_ISLNK(patch->new_mode))
3770 + /* the symlink at patch->new_name is created or remains */
3771 + register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3772 + }
3773 +}
3774 +
3775 +static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3776 +{
3777 + do {
3778 + unsigned int change;
3779 +
3780 + while (--name->len && name->buf[name->len] != '/')
3781 + ; /* scan backwards */
3782 + if (!name->len)
3783 + break;
3784 + name->buf[name->len] = '\0';
3785 + change = check_symlink_changes(state, name->buf);
3786 + if (change & APPLY_SYMLINK_IN_RESULT)
3787 + return 1;
3788 + if (change & APPLY_SYMLINK_GOES_AWAY)
3789 + /*
3790 + * This cannot be "return 0", because we may
3791 + * see a new one created at a higher level.
3792 + */
3793 + continue;
3794 +
3795 + /* otherwise, check the preimage */
3796 + if (state->check_index) {
3797 + struct cache_entry *ce;
3798 +
3799 + ce = cache_file_exists(name->buf, name->len, ignore_case);
3800 + if (ce && S_ISLNK(ce->ce_mode))
3801 + return 1;
3802 + } else {
3803 + struct stat st;
3804 + if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3805 + return 1;
3806 + }
3807 + } while (1);
3808 + return 0;
3809 +}
3810 +
3811 +static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3812 +{
3813 + int ret;
3814 + struct strbuf name = STRBUF_INIT;
3815 +
3816 + assert(*name_ != '\0');
3817 + strbuf_addstr(&name, name_);
3818 + ret = path_is_beyond_symlink_1(state, &name);
3819 + strbuf_release(&name);
3820 +
3821 + return ret;
3822 +}
3823 +
3824 +static int check_unsafe_path(struct patch *patch)
3825 +{
3826 + const char *old_name = NULL;
3827 + const char *new_name = NULL;
3828 + if (patch->is_delete)
3829 + old_name = patch->old_name;
3830 + else if (!patch->is_new && !patch->is_copy)
3831 + old_name = patch->old_name;
3832 + if (!patch->is_delete)
3833 + new_name = patch->new_name;
3834 +
3835 + if (old_name && !verify_path(old_name))
3836 + return error(_("invalid path '%s'"), old_name);
3837 + if (new_name && !verify_path(new_name))
3838 + return error(_("invalid path '%s'"), new_name);
3839 + return 0;
3840 +}
3841 +
3842 +/*
3843 + * Check and apply the patch in-core; leave the result in patch->result
3844 + * for the caller to write it out to the final destination.
3845 + */
3846 +static int check_patch(struct apply_state *state, struct patch *patch)
3847 +{
3848 + struct stat st;
3849 + const char *old_name = patch->old_name;
3850 + const char *new_name = patch->new_name;
3851 + const char *name = old_name ? old_name : new_name;
3852 + struct cache_entry *ce = NULL;
3853 + struct patch *tpatch;
3854 + int ok_if_exists;
3855 + int status;
3856 +
3857 + patch->rejected = 1; /* we will drop this after we succeed */
3858 +
3859 + status = check_preimage(state, patch, &ce, &st);
3860 + if (status)
3861 + return status;
3862 + old_name = patch->old_name;
3863 +
3864 + /*
3865 + * A type-change diff is always split into a patch to delete
3866 + * old, immediately followed by a patch to create new (see
3867 + * diff.c::run_diff()); in such a case it is Ok that the entry
3868 + * to be deleted by the previous patch is still in the working
3869 + * tree and in the index.
3870 + *
3871 + * A patch to swap-rename between A and B would first rename A
3872 + * to B and then rename B to A. While applying the first one,
3873 + * the presence of B should not stop A from getting renamed to
3874 + * B; ask to_be_deleted() about the later rename. Removal of
3875 + * B and rename from A to B is handled the same way by asking
3876 + * was_deleted().
3877 + */
3878 + if ((tpatch = in_fn_table(state, new_name)) &&
3879 + (was_deleted(tpatch) || to_be_deleted(tpatch)))
3880 + ok_if_exists = 1;
3881 + else
3882 + ok_if_exists = 0;
3883 +
3884 + if (new_name &&
3885 + ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3886 + int err = check_to_create(state, new_name, ok_if_exists);
3887 +
3888 + if (err && state->threeway) {
3889 + patch->direct_to_threeway = 1;
3890 + } else switch (err) {
3891 + case 0:
3892 + break; /* happy */
3893 + case EXISTS_IN_INDEX:
3894 + return error(_("%s: already exists in index"), new_name);
3895 + break;
3896 + case EXISTS_IN_WORKTREE:
3897 + return error(_("%s: already exists in working directory"),
3898 + new_name);
3899 + default:
3900 + return err;
3901 + }
3902 +
3903 + if (!patch->new_mode) {
3904 + if (0 < patch->is_new)
3905 + patch->new_mode = S_IFREG | 0644;
3906 + else
3907 + patch->new_mode = patch->old_mode;
3908 + }
3909 + }
3910 +
3911 + if (new_name && old_name) {
3912 + int same = !strcmp(old_name, new_name);
3913 + if (!patch->new_mode)
3914 + patch->new_mode = patch->old_mode;
3915 + if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3916 + if (same)
3917 + return error(_("new mode (%o) of %s does not "
3918 + "match old mode (%o)"),
3919 + patch->new_mode, new_name,
3920 + patch->old_mode);
3921 + else
3922 + return error(_("new mode (%o) of %s does not "
3923 + "match old mode (%o) of %s"),
3924 + patch->new_mode, new_name,
3925 + patch->old_mode, old_name);
3926 + }
3927 + }
3928 +
3929 + if (!state->unsafe_paths && check_unsafe_path(patch))
3930 + return -128;
3931 +
3932 + /*
3933 + * An attempt to read from or delete a path that is beyond a
3934 + * symbolic link will be prevented by load_patch_target() that
3935 + * is called at the beginning of apply_data() so we do not
3936 + * have to worry about a patch marked with "is_delete" bit
3937 + * here. We however need to make sure that the patch result
3938 + * is not deposited to a path that is beyond a symbolic link
3939 + * here.
3940 + */
3941 + if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3942 + return error(_("affected file '%s' is beyond a symbolic link"),
3943 + patch->new_name);
3944 +
3945 + if (apply_data(state, patch, &st, ce) < 0)
3946 + return error(_("%s: patch does not apply"), name);
3947 + patch->rejected = 0;
3948 + return 0;
3949 +}
3950 +
3951 +static int check_patch_list(struct apply_state *state, struct patch *patch)
3952 +{
3953 + int err = 0;
3954 +
3955 + prepare_symlink_changes(state, patch);
3956 + prepare_fn_table(state, patch);
3957 + while (patch) {
3958 + int res;
3959 + if (state->apply_verbosely)
3960 + say_patch_name(stderr,
3961 + _("Checking patch %s..."), patch);
3962 + res = check_patch(state, patch);
3963 + if (res == -128)
3964 + return -128;
3965 + err |= res;
3966 + patch = patch->next;
3967 + }
3968 + return err;
3969 +}
3970 +
3971 +/* This function tries to read the sha1 from the current index */
3972 +static int get_current_sha1(const char *path, unsigned char *sha1)
3973 +{
3974 + int pos;
3975 +
3976 + if (read_cache() < 0)
3977 + return -1;
3978 + pos = cache_name_pos(path, strlen(path));
3979 + if (pos < 0)
3980 + return -1;
3981 + hashcpy(sha1, active_cache[pos]->sha1);
3982 + return 0;
3983 +}
3984 +
3985 +static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3986 +{
3987 + /*
3988 + * A usable gitlink patch has only one fragment (hunk) that looks like:
3989 + * @@ -1 +1 @@
3990 + * -Subproject commit <old sha1>
3991 + * +Subproject commit <new sha1>
3992 + * or
3993 + * @@ -1 +0,0 @@
3994 + * -Subproject commit <old sha1>
3995 + * for a removal patch.
3996 + */
3997 + struct fragment *hunk = p->fragments;
3998 + static const char heading[] = "-Subproject commit ";
3999 + char *preimage;
4000 +
4001 + if (/* does the patch have only one hunk? */
4002 + hunk && !hunk->next &&
4003 + /* is its preimage one line? */
4004 + hunk->oldpos == 1 && hunk->oldlines == 1 &&
4005 + /* does preimage begin with the heading? */
4006 + (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
4007 + starts_with(++preimage, heading) &&
4008 + /* does it record full SHA-1? */
4009 + !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
4010 + preimage[sizeof(heading) + 40 - 1] == '\n' &&
4011 + /* does the abbreviated name on the index line agree with it? */
4012 + starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
4013 + return 0; /* it all looks fine */
4014 +
4015 + /* we may have full object name on the index line */
4016 + return get_sha1_hex(p->old_sha1_prefix, sha1);
4017 +}
4018 +
4019 +/* Build an index that contains the just the files needed for a 3way merge */
4020 +static int build_fake_ancestor(struct patch *list, const char *filename)
4021 +{
4022 + struct patch *patch;
4023 + struct index_state result = { NULL };
4024 + static struct lock_file lock;
4025 + int res;
4026 +
4027 + /* Once we start supporting the reverse patch, it may be
4028 + * worth showing the new sha1 prefix, but until then...
4029 + */
4030 + for (patch = list; patch; patch = patch->next) {
4031 + unsigned char sha1[20];
4032 + struct cache_entry *ce;
4033 + const char *name;
4034 +
4035 + name = patch->old_name ? patch->old_name : patch->new_name;
4036 + if (0 < patch->is_new)
4037 + continue;
4038 +
4039 + if (S_ISGITLINK(patch->old_mode)) {
4040 + if (!preimage_sha1_in_gitlink_patch(patch, sha1))
4041 + ; /* ok, the textual part looks sane */
4042 + else
4043 + return error("sha1 information is lacking or "
4044 + "useless for submodule %s", name);
4045 + } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
4046 + ; /* ok */
4047 + } else if (!patch->lines_added && !patch->lines_deleted) {
4048 + /* mode-only change: update the current */
4049 + if (get_current_sha1(patch->old_name, sha1))
4050 + return error("mode change for %s, which is not "
4051 + "in current HEAD", name);
4052 + } else
4053 + return error("sha1 information is lacking or useless "
4054 + "(%s).", name);
4055 +
4056 + ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
4057 + if (!ce)
4058 + return error(_("make_cache_entry failed for path '%s'"),
4059 + name);
4060 + if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
4061 + free(ce);
4062 + return error("Could not add %s to temporary index",
4063 + name);
4064 + }
4065 + }
4066 +
4067 + hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
4068 + res = write_locked_index(&result, &lock, COMMIT_LOCK);
4069 + discard_index(&result);
4070 +
4071 + if (res)
4072 + return error("Could not write temporary index to %s", filename);
4073 +
4074 + return 0;
4075 +}
4076 +
4077 +static void stat_patch_list(struct apply_state *state, struct patch *patch)
4078 +{
4079 + int files, adds, dels;
4080 +
4081 + for (files = adds = dels = 0 ; patch ; patch = patch->next) {
4082 + files++;
4083 + adds += patch->lines_added;
4084 + dels += patch->lines_deleted;
4085 + show_stats(state, patch);
4086 + }
4087 +
4088 + print_stat_summary(stdout, files, adds, dels);
4089 +}
4090 +
4091 +static void numstat_patch_list(struct apply_state *state,
4092 + struct patch *patch)
4093 +{
4094 + for ( ; patch; patch = patch->next) {
4095 + const char *name;
4096 + name = patch->new_name ? patch->new_name : patch->old_name;
4097 + if (patch->is_binary)
4098 + printf("-\t-\t");
4099 + else
4100 + printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
4101 + write_name_quoted(name, stdout, state->line_termination);
4102 + }
4103 +}
4104 +
4105 +static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
4106 +{
4107 + if (mode)
4108 + printf(" %s mode %06o %s\n", newdelete, mode, name);
4109 + else
4110 + printf(" %s %s\n", newdelete, name);
4111 +}
4112 +
4113 +static void show_mode_change(struct patch *p, int show_name)
4114 +{
4115 + if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
4116 + if (show_name)
4117 + printf(" mode change %06o => %06o %s\n",
4118 + p->old_mode, p->new_mode, p->new_name);
4119 + else
4120 + printf(" mode change %06o => %06o\n",
4121 + p->old_mode, p->new_mode);
4122 + }
4123 +}
4124 +
4125 +static void show_rename_copy(struct patch *p)
4126 +{
4127 + const char *renamecopy = p->is_rename ? "rename" : "copy";
4128 + const char *old, *new;
4129 +
4130 + /* Find common prefix */
4131 + old = p->old_name;
4132 + new = p->new_name;
4133 + while (1) {
4134 + const char *slash_old, *slash_new;
4135 + slash_old = strchr(old, '/');
4136 + slash_new = strchr(new, '/');
4137 + if (!slash_old ||
4138 + !slash_new ||
4139 + slash_old - old != slash_new - new ||
4140 + memcmp(old, new, slash_new - new))
4141 + break;
4142 + old = slash_old + 1;
4143 + new = slash_new + 1;
4144 + }
4145 + /* p->old_name thru old is the common prefix, and old and new
4146 + * through the end of names are renames
4147 + */
4148 + if (old != p->old_name)
4149 + printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4150 + (int)(old - p->old_name), p->old_name,
4151 + old, new, p->score);
4152 + else
4153 + printf(" %s %s => %s (%d%%)\n", renamecopy,
4154 + p->old_name, p->new_name, p->score);
4155 + show_mode_change(p, 0);
4156 +}
4157 +
4158 +static void summary_patch_list(struct patch *patch)
4159 +{
4160 + struct patch *p;
4161 +
4162 + for (p = patch; p; p = p->next) {
4163 + if (p->is_new)
4164 + show_file_mode_name("create", p->new_mode, p->new_name);
4165 + else if (p->is_delete)
4166 + show_file_mode_name("delete", p->old_mode, p->old_name);
4167 + else {
4168 + if (p->is_rename || p->is_copy)
4169 + show_rename_copy(p);
4170 + else {
4171 + if (p->score) {
4172 + printf(" rewrite %s (%d%%)\n",
4173 + p->new_name, p->score);
4174 + show_mode_change(p, 0);
4175 + }
4176 + else
4177 + show_mode_change(p, 1);
4178 + }
4179 + }
4180 + }
4181 +}
4182 +
4183 +static void patch_stats(struct apply_state *state, struct patch *patch)
4184 +{
4185 + int lines = patch->lines_added + patch->lines_deleted;
4186 +
4187 + if (lines > state->max_change)
4188 + state->max_change = lines;
4189 + if (patch->old_name) {
4190 + int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4191 + if (!len)
4192 + len = strlen(patch->old_name);
4193 + if (len > state->max_len)
4194 + state->max_len = len;
4195 + }
4196 + if (patch->new_name) {
4197 + int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4198 + if (!len)
4199 + len = strlen(patch->new_name);
4200 + if (len > state->max_len)
4201 + state->max_len = len;
4202 + }
4203 +}
4204 +
4205 +static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4206 +{
4207 + if (state->update_index) {
4208 + if (remove_file_from_cache(patch->old_name) < 0)
4209 + return error(_("unable to remove %s from index"), patch->old_name);
4210 + }
4211 + if (!state->cached) {
4212 + if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4213 + remove_path(patch->old_name);
4214 + }
4215 + }
4216 + return 0;
4217 +}
4218 +
4219 +static int add_index_file(struct apply_state *state,
4220 + const char *path,
4221 + unsigned mode,
4222 + void *buf,
4223 + unsigned long size)
4224 +{
4225 + struct stat st;
4226 + struct cache_entry *ce;
4227 + int namelen = strlen(path);
4228 + unsigned ce_size = cache_entry_size(namelen);
4229 +
4230 + if (!state->update_index)
4231 + return 0;
4232 +
4233 + ce = xcalloc(1, ce_size);
4234 + memcpy(ce->name, path, namelen);
4235 + ce->ce_mode = create_ce_mode(mode);
4236 + ce->ce_flags = create_ce_flags(0);
4237 + ce->ce_namelen = namelen;
4238 + if (S_ISGITLINK(mode)) {
4239 + const char *s;
4240 +
4241 + if (!skip_prefix(buf, "Subproject commit ", &s) ||
4242 + get_sha1_hex(s, ce->sha1)) {
4243 + free(ce);
4244 + return error(_("corrupt patch for submodule %s"), path);
4245 + }
4246 + } else {
4247 + if (!state->cached) {
4248 + if (lstat(path, &st) < 0) {
4249 + free(ce);
4250 + return error(_("unable to stat newly "
4251 + "created file '%s': %s"),
4252 + path, strerror(errno));
4253 + }
4254 + fill_stat_cache_info(ce, &st);
4255 + }
4256 + if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
4257 + free(ce);
4258 + return error(_("unable to create backing store "
4259 + "for newly created file %s"), path);
4260 + }
4261 + }
4262 + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4263 + free(ce);
4264 + return error(_("unable to add cache entry for %s"), path);
4265 + }
4266 +
4267 + return 0;
4268 +}
4269 +
4270 +/*
4271 + * Returns:
4272 + * -1 if an unrecoverable error happened
4273 + * 0 if everything went well
4274 + * 1 if a recoverable error happened
4275 + */
4276 +static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4277 +{
4278 + int fd, res;
4279 + struct strbuf nbuf = STRBUF_INIT;
4280 +
4281 + if (S_ISGITLINK(mode)) {
4282 + struct stat st;
4283 + if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4284 + return 0;
4285 + return !!mkdir(path, 0777);
4286 + }
4287 +
4288 + if (has_symlinks && S_ISLNK(mode))
4289 + /* Although buf:size is counted string, it also is NUL
4290 + * terminated.
4291 + */
4292 + return !!symlink(buf, path);
4293 +
4294 + fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4295 + if (fd < 0)
4296 + return 1;
4297 +
4298 + if (convert_to_working_tree(path, buf, size, &nbuf)) {
4299 + size = nbuf.len;
4300 + buf = nbuf.buf;
4301 + }
4302 +
4303 + res = write_in_full(fd, buf, size) < 0;
4304 + if (res)
4305 + error_errno(_("failed to write to '%s'"), path);
4306 + strbuf_release(&nbuf);
4307 +
4308 + if (close(fd) < 0 && !res)
4309 + return error_errno(_("closing file '%s'"), path);
4310 +
4311 + return res ? -1 : 0;
4312 +}
4313 +
4314 +/*
4315 + * We optimistically assume that the directories exist,
4316 + * which is true 99% of the time anyway. If they don't,
4317 + * we create them and try again.
4318 + *
4319 + * Returns:
4320 + * -1 on error
4321 + * 0 otherwise
4322 + */
4323 +static int create_one_file(struct apply_state *state,
4324 + char *path,
4325 + unsigned mode,
4326 + const char *buf,
4327 + unsigned long size)
4328 +{
4329 + int res;
4330 +
4331 + if (state->cached)
4332 + return 0;
4333 +
4334 + res = try_create_file(path, mode, buf, size);
4335 + if (res < 0)
4336 + return -1;
4337 + if (!res)
4338 + return 0;
4339 +
4340 + if (errno == ENOENT) {
4341 + if (safe_create_leading_directories(path))
4342 + return 0;
4343 + res = try_create_file(path, mode, buf, size);
4344 + if (res < 0)
4345 + return -1;
4346 + if (!res)
4347 + return 0;
4348 + }
4349 +
4350 + if (errno == EEXIST || errno == EACCES) {
4351 + /* We may be trying to create a file where a directory
4352 + * used to be.
4353 + */
4354 + struct stat st;
4355 + if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4356 + errno = EEXIST;
4357 + }
4358 +
4359 + if (errno == EEXIST) {
4360 + unsigned int nr = getpid();
4361 +
4362 + for (;;) {
4363 + char newpath[PATH_MAX];
4364 + mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4365 + res = try_create_file(newpath, mode, buf, size);
4366 + if (res < 0)
4367 + return -1;
4368 + if (!res) {
4369 + if (!rename(newpath, path))
4370 + return 0;
4371 + unlink_or_warn(newpath);
4372 + break;
4373 + }
4374 + if (errno != EEXIST)
4375 + break;
4376 + ++nr;
4377 + }
4378 + }
4379 + return error_errno(_("unable to write file '%s' mode %o"),
4380 + path, mode);
4381 +}
4382 +
4383 +static int add_conflicted_stages_file(struct apply_state *state,
4384 + struct patch *patch)
4385 +{
4386 + int stage, namelen;
4387 + unsigned ce_size, mode;
4388 + struct cache_entry *ce;
4389 +
4390 + if (!state->update_index)
4391 + return 0;
4392 + namelen = strlen(patch->new_name);
4393 + ce_size = cache_entry_size(namelen);
4394 + mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4395 +
4396 + remove_file_from_cache(patch->new_name);
4397 + for (stage = 1; stage < 4; stage++) {
4398 + if (is_null_oid(&patch->threeway_stage[stage - 1]))
4399 + continue;
4400 + ce = xcalloc(1, ce_size);
4401 + memcpy(ce->name, patch->new_name, namelen);
4402 + ce->ce_mode = create_ce_mode(mode);
4403 + ce->ce_flags = create_ce_flags(stage);
4404 + ce->ce_namelen = namelen;
4405 + hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4406 + if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4407 + free(ce);
4408 + return error(_("unable to add cache entry for %s"),
4409 + patch->new_name);
4410 + }
4411 + }
4412 +
4413 + return 0;
4414 +}
4415 +
4416 +static int create_file(struct apply_state *state, struct patch *patch)
4417 +{
4418 + char *path = patch->new_name;
4419 + unsigned mode = patch->new_mode;
4420 + unsigned long size = patch->resultsize;
4421 + char *buf = patch->result;
4422 +
4423 + if (!mode)
4424 + mode = S_IFREG | 0644;
4425 + if (create_one_file(state, path, mode, buf, size))
4426 + return -1;
4427 +
4428 + if (patch->conflicted_threeway)
4429 + return add_conflicted_stages_file(state, patch);
4430 + else
4431 + return add_index_file(state, path, mode, buf, size);
4432 +}
4433 +
4434 +/* phase zero is to remove, phase one is to create */
4435 +static int write_out_one_result(struct apply_state *state,
4436 + struct patch *patch,
4437 + int phase)
4438 +{
4439 + if (patch->is_delete > 0) {
4440 + if (phase == 0)
4441 + return remove_file(state, patch, 1);
4442 + return 0;
4443 + }
4444 + if (patch->is_new > 0 || patch->is_copy) {
4445 + if (phase == 1)
4446 + return create_file(state, patch);
4447 + return 0;
4448 + }
4449 + /*
4450 + * Rename or modification boils down to the same
4451 + * thing: remove the old, write the new
4452 + */
4453 + if (phase == 0)
4454 + return remove_file(state, patch, patch->is_rename);
4455 + if (phase == 1)
4456 + return create_file(state, patch);
4457 + return 0;
4458 +}
4459 +
4460 +static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4461 +{
4462 + FILE *rej;
4463 + char namebuf[PATH_MAX];
4464 + struct fragment *frag;
4465 + int cnt = 0;
4466 + struct strbuf sb = STRBUF_INIT;
4467 +
4468 + for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4469 + if (!frag->rejected)
4470 + continue;
4471 + cnt++;
4472 + }
4473 +
4474 + if (!cnt) {
4475 + if (state->apply_verbosely)
4476 + say_patch_name(stderr,
4477 + _("Applied patch %s cleanly."), patch);
4478 + return 0;
4479 + }
4480 +
4481 + /* This should not happen, because a removal patch that leaves
4482 + * contents are marked "rejected" at the patch level.
4483 + */
4484 + if (!patch->new_name)
4485 + die(_("internal error"));
4486 +
4487 + /* Say this even without --verbose */
4488 + strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4489 + "Applying patch %%s with %d rejects...",
4490 + cnt),
4491 + cnt);
4492 + say_patch_name(stderr, sb.buf, patch);
4493 + strbuf_release(&sb);
4494 +
4495 + cnt = strlen(patch->new_name);
4496 + if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4497 + cnt = ARRAY_SIZE(namebuf) - 5;
4498 + warning(_("truncating .rej filename to %.*s.rej"),
4499 + cnt - 1, patch->new_name);
4500 + }
4501 + memcpy(namebuf, patch->new_name, cnt);
4502 + memcpy(namebuf + cnt, ".rej", 5);
4503 +
4504 + rej = fopen(namebuf, "w");
4505 + if (!rej)
4506 + return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4507 +
4508 + /* Normal git tools never deal with .rej, so do not pretend
4509 + * this is a git patch by saying --git or giving extended
4510 + * headers. While at it, maybe please "kompare" that wants
4511 + * the trailing TAB and some garbage at the end of line ;-).
4512 + */
4513 + fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4514 + patch->new_name, patch->new_name);
4515 + for (cnt = 1, frag = patch->fragments;
4516 + frag;
4517 + cnt++, frag = frag->next) {
4518 + if (!frag->rejected) {
4519 + fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4520 + continue;
4521 + }
4522 + fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4523 + fprintf(rej, "%.*s", frag->size, frag->patch);
4524 + if (frag->patch[frag->size-1] != '\n')
4525 + fputc('\n', rej);
4526 + }
4527 + fclose(rej);
4528 + return -1;
4529 +}
4530 +
4531 +/*
4532 + * Returns:
4533 + * -1 if an error happened
4534 + * 0 if the patch applied cleanly
4535 + * 1 if the patch did not apply cleanly
4536 + */
4537 +static int write_out_results(struct apply_state *state, struct patch *list)
4538 +{
4539 + int phase;
4540 + int errs = 0;
4541 + struct patch *l;
4542 + struct string_list cpath = STRING_LIST_INIT_DUP;
4543 +
4544 + for (phase = 0; phase < 2; phase++) {
4545 + l = list;
4546 + while (l) {
4547 + if (l->rejected)
4548 + errs = 1;
4549 + else {
4550 + if (write_out_one_result(state, l, phase)) {
4551 + string_list_clear(&cpath, 0);
4552 + return -1;
4553 + }
4554 + if (phase == 1) {
4555 + if (write_out_one_reject(state, l))
4556 + errs = 1;
4557 + if (l->conflicted_threeway) {
4558 + string_list_append(&cpath, l->new_name);
4559 + errs = 1;
4560 + }
4561 + }
4562 + }
4563 + l = l->next;
4564 + }
4565 + }
4566 +
4567 + if (cpath.nr) {
4568 + struct string_list_item *item;
4569 +
4570 + string_list_sort(&cpath);
4571 + for_each_string_list_item(item, &cpath)
4572 + fprintf(stderr, "U %s\n", item->string);
4573 + string_list_clear(&cpath, 0);
4574 +
4575 + rerere(0);
4576 + }
4577 +
4578 + return errs;
4579 +}
4580 +
4581 +/*
4582 + * Try to apply a patch.
4583 + *
4584 + * Returns:
4585 + * -128 if a bad error happened (like patch unreadable)
4586 + * -1 if patch did not apply and user cannot deal with it
4587 + * 0 if the patch applied
4588 + * 1 if the patch did not apply but user might fix it
4589 + */
4590 +static int apply_patch(struct apply_state *state,
4591 + int fd,
4592 + const char *filename,
4593 + int options)
4594 +{
4595 + size_t offset;
4596 + struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4597 + struct patch *list = NULL, **listp = &list;
4598 + int skipped_patch = 0;
4599 + int res = 0;
4600 +
4601 + state->patch_input_file = filename;
4602 + if (read_patch_file(&buf, fd) < 0)
4603 + return -128;
4604 + offset = 0;
4605 + while (offset < buf.len) {
4606 + struct patch *patch;
4607 + int nr;
4608 +
4609 + patch = xcalloc(1, sizeof(*patch));
4610 + patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
4611 + patch->recount = !!(options & APPLY_OPT_RECOUNT);
4612 + nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4613 + if (nr < 0) {
4614 + free_patch(patch);
4615 + if (nr == -128) {
4616 + res = -128;
4617 + goto end;
4618 + }
4619 + break;
4620 + }
4621 + if (state->apply_in_reverse)
4622 + reverse_patches(patch);
4623 + if (use_patch(state, patch)) {
4624 + patch_stats(state, patch);
4625 + *listp = patch;
4626 + listp = &patch->next;
4627 + }
4628 + else {
4629 + if (state->apply_verbosely)
4630 + say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4631 + free_patch(patch);
4632 + skipped_patch++;
4633 + }
4634 + offset += nr;
4635 + }
4636 +
4637 + if (!list && !skipped_patch) {
4638 + error(_("unrecognized input"));
4639 + res = -128;
4640 + goto end;
4641 + }
4642 +
4643 + if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4644 + state->apply = 0;
4645 +
4646 + state->update_index = state->check_index && state->apply;
4647 + if (state->update_index && state->newfd < 0)
4648 + state->newfd = hold_locked_index(state->lock_file, 1);
4649 +
4650 + if (state->check_index && read_cache() < 0) {
4651 + error(_("unable to read index file"));
4652 + res = -128;
4653 + goto end;
4654 + }
4655 +
4656 + if (state->check || state->apply) {
4657 + int r = check_patch_list(state, list);
4658 + if (r == -128) {
4659 + res = -128;
4660 + goto end;
4661 + }
4662 + if (r < 0 && !state->apply_with_reject) {
4663 + res = -1;
4664 + goto end;
4665 + }
4666 + }
4667 +
4668 + if (state->apply) {
4669 + int write_res = write_out_results(state, list);
4670 + if (write_res < 0) {
4671 + res = -128;
4672 + goto end;
4673 + }
4674 + if (write_res > 0) {
4675 + /* with --3way, we still need to write the index out */
4676 + res = state->apply_with_reject ? -1 : 1;
4677 + goto end;
4678 + }
4679 + }
4680 +
4681 + if (state->fake_ancestor &&
4682 + build_fake_ancestor(list, state->fake_ancestor)) {
4683 + res = -128;
4684 + goto end;
4685 + }
4686 +
4687 + if (state->diffstat)
4688 + stat_patch_list(state, list);
4689 +
4690 + if (state->numstat)
4691 + numstat_patch_list(state, list);
4692 +
4693 + if (state->summary)
4694 + summary_patch_list(list);
4695 +
4696 +end:
4697 + free_patch_list(list);
4698 + strbuf_release(&buf);
4699 + string_list_clear(&state->fn_table, 0);
4700 + return res;
4701 +}
4702 +
4703 +int apply_option_parse_exclude(const struct option *opt,
4704 + const char *arg, int unset)
4705 +{
4706 + struct apply_state *state = opt->value;
4707 + add_name_limit(state, arg, 1);
4708 + return 0;
4709 +}
4710 +
4711 +int apply_option_parse_include(const struct option *opt,
4712 + const char *arg, int unset)
4713 +{
4714 + struct apply_state *state = opt->value;
4715 + add_name_limit(state, arg, 0);
4716 + state->has_include = 1;
4717 + return 0;
4718 +}
4719 +
4720 +int apply_option_parse_p(const struct option *opt,
4721 + const char *arg,
4722 + int unset)
4723 +{
4724 + struct apply_state *state = opt->value;
4725 + state->p_value = atoi(arg);
4726 + state->p_value_known = 1;
4727 + return 0;
4728 +}
4729 +
4730 +int apply_option_parse_space_change(const struct option *opt,
4731 + const char *arg, int unset)
4732 +{
4733 + struct apply_state *state = opt->value;
4734 + if (unset)
4735 + state->ws_ignore_action = ignore_ws_none;
4736 + else
4737 + state->ws_ignore_action = ignore_ws_change;
4738 + return 0;
4739 +}
4740 +
4741 +int apply_option_parse_whitespace(const struct option *opt,
4742 + const char *arg, int unset)
4743 +{
4744 + struct apply_state *state = opt->value;
4745 + state->whitespace_option = arg;
4746 + if (parse_whitespace_option(state, arg))
4747 + exit(1);
4748 + return 0;
4749 +}
4750 +
4751 +int apply_option_parse_directory(const struct option *opt,
4752 + const char *arg, int unset)
4753 +{
4754 + struct apply_state *state = opt->value;
4755 + strbuf_reset(&state->root);
4756 + strbuf_addstr(&state->root, arg);
4757 + strbuf_complete(&state->root, '/');
4758 + return 0;
4759 +}
4760 +
4761 +int apply_all_patches(struct apply_state *state,
4762 + int argc,
4763 + const char **argv,
4764 + int options)
4765 +{
4766 + int i;
4767 + int res;
4768 + int errs = 0;
4769 + int read_stdin = 1;
4770 +
4771 + for (i = 0; i < argc; i++) {
4772 + const char *arg = argv[i];
4773 + int fd;
4774 +
4775 + if (!strcmp(arg, "-")) {
4776 + res = apply_patch(state, 0, "<stdin>", options);
4777 + if (res < 0)
4778 + goto end;
4779 + errs |= res;
4780 + read_stdin = 0;
4781 + continue;
4782 + } else if (0 < state->prefix_length)
4783 + arg = prefix_filename(state->prefix,
4784 + state->prefix_length,
4785 + arg);
4786 +
4787 + fd = open(arg, O_RDONLY);
4788 + if (fd < 0) {
4789 + error(_("can't open patch '%s': %s"), arg, strerror(errno));
4790 + res = -128;
4791 + goto end;
4792 + }
4793 + read_stdin = 0;
4794 + set_default_whitespace_mode(state);
4795 + res = apply_patch(state, fd, arg, options);
4796 + close(fd);
4797 + if (res < 0)
4798 + goto end;
4799 + errs |= res;
4800 + }
4801 + set_default_whitespace_mode(state);
4802 + if (read_stdin) {
4803 + res = apply_patch(state, 0, "<stdin>", options);
4804 + if (res < 0)
4805 + goto end;
4806 + errs |= res;
4807 + }
4808 +
4809 + if (state->whitespace_error) {
4810 + if (state->squelch_whitespace_errors &&
4811 + state->squelch_whitespace_errors < state->whitespace_error) {
4812 + int squelched =
4813 + state->whitespace_error - state->squelch_whitespace_errors;
4814 + warning(Q_("squelched %d whitespace error",
4815 + "squelched %d whitespace errors",
4816 + squelched),
4817 + squelched);
4818 + }
4819 + if (state->ws_error_action == die_on_ws_error) {
4820 + error(Q_("%d line adds whitespace errors.",
4821 + "%d lines add whitespace errors.",
4822 + state->whitespace_error),
4823 + state->whitespace_error);
4824 + res = -128;
4825 + goto end;
4826 + }
4827 + if (state->applied_after_fixing_ws && state->apply)
4828 + warning("%d line%s applied after"
4829 + " fixing whitespace errors.",
4830 + state->applied_after_fixing_ws,
4831 + state->applied_after_fixing_ws == 1 ? "" : "s");
4832 + else if (state->whitespace_error)
4833 + warning(Q_("%d line adds whitespace errors.",
4834 + "%d lines add whitespace errors.",
4835 + state->whitespace_error),
4836 + state->whitespace_error);
4837 + }
4838 +
4839 + if (state->update_index) {
4840 + res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4841 + if (res) {
4842 + error(_("Unable to write new index file"));
4843 + res = -128;
4844 + goto end;
4845 + }
4846 + state->newfd = -1;
4847 + }
4848 +
4849 + return !!errs;
4850 +
4851 +end:
4852 + if (state->newfd >= 0) {
4853 + rollback_lock_file(state->lock_file);
4854 + state->newfd = -1;
4855 + }
4856 +
4857 + return (res == -1 ? 1 : 128);
4858 +}
apply.h
+19
@@ -102,6 +102,20 @@ extern int parse_whitespace_option(struct apply_state *state,
102 extern int parse_ignorewhitespace_option(struct apply_state *state,
103 const char *option);
104
105 +extern int apply_option_parse_exclude(const struct option *opt,
106 + const char *arg, int unset);
107 +extern int apply_option_parse_include(const struct option *opt,
108 + const char *arg, int unset);
109 +extern int apply_option_parse_p(const struct option *opt,
110 + const char *arg,
111 + int unset);
112 +extern int apply_option_parse_whitespace(const struct option *opt,
113 + const char *arg, int unset);
114 +extern int apply_option_parse_directory(const struct option *opt,
115 + const char *arg, int unset);
116 +extern int apply_option_parse_space_change(const struct option *opt,
117 + const char *arg, int unset);
118 +
119 extern int init_apply_state(struct apply_state *state,
120 const char *prefix,
121 struct lock_file *lock_file);
@@ -115,4 +129,9 @@ extern int check_apply_state(struct apply_state *state, int force_apply);
129 #define APPLY_OPT_INACCURATE_EOF (1<<0) /* accept inaccurate eof */
130 #define APPLY_OPT_RECOUNT (1<<1) /* accept inaccurate line count */
131
132 +extern int apply_all_patches(struct apply_state *state,
133 + int argc,
134 + const char **argv,
135 + int options);
136 +
137 #endif
builtin/apply.c
+1 -4732
@@ -1,25 +1,7 @@
1 -/*
2 - * apply.c
3 - *
4 - * Copyright (C) Linus Torvalds, 2005
5 - *
6 - * This applies patches on top of some (arbitrary) version of the SCM.
7 - *
8 - */
1 #include "cache.h"
10 -#include "lockfile.h"
11 -#include "cache-tree.h"
12 -#include "quote.h"
13 -#include "blob.h"
14 -#include "delta.h"
2 #include "builtin.h"
16 -#include "string-list.h"
17 -#include "dir.h"
18 -#include "diff.h"
3 #include "parse-options.h"
20 -#include "xdiff-interface.h"
21 -#include "ll-merge.h"
22 -#include "rerere.h"
4 +#include "lockfile.h"
5 #include "apply.h"
6
7 static const char * const apply_usage[] = {
@@ -27,4721 +9,8 @@ static const char * const apply_usage[] = {
9 NULL
10 };
11
30 -static void set_default_whitespace_mode(struct apply_state *state)
31 -{
32 - if (!state->whitespace_option && !apply_default_whitespace)
33 - state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
34 -}
35 -
36 -/*
37 - * This represents one "hunk" from a patch, starting with
38 - * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
39 - * patch text is pointed at by patch, and its byte length
40 - * is stored in size. leading and trailing are the number
41 - * of context lines.
42 - */
43 -struct fragment {
44 - unsigned long leading, trailing;
45 - unsigned long oldpos, oldlines;
46 - unsigned long newpos, newlines;
47 - /*
48 - * 'patch' is usually borrowed from buf in apply_patch(),
49 - * but some codepaths store an allocated buffer.
50 - */
51 - const char *patch;
52 - unsigned free_patch:1,
53 - rejected:1;
54 - int size;
55 - int linenr;
56 - struct fragment *next;
57 -};
58 -
59 -/*
60 - * When dealing with a binary patch, we reuse "leading" field
61 - * to store the type of the binary hunk, either deflated "delta"
62 - * or deflated "literal".
63 - */
64 -#define binary_patch_method leading
65 -#define BINARY_DELTA_DEFLATED 1
66 -#define BINARY_LITERAL_DEFLATED 2
67 -
68 -/*
69 - * This represents a "patch" to a file, both metainfo changes
70 - * such as creation/deletion, filemode and content changes represented
71 - * as a series of fragments.
72 - */
73 -struct patch {
74 - char *new_name, *old_name, *def_name;
75 - unsigned int old_mode, new_mode;
76 - int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
77 - int rejected;
78 - unsigned ws_rule;
79 - int lines_added, lines_deleted;
80 - int score;
81 - unsigned int is_toplevel_relative:1;
82 - unsigned int inaccurate_eof:1;
83 - unsigned int is_binary:1;
84 - unsigned int is_copy:1;
85 - unsigned int is_rename:1;
86 - unsigned int recount:1;
87 - unsigned int conflicted_threeway:1;
88 - unsigned int direct_to_threeway:1;
89 - struct fragment *fragments;
90 - char *result;
91 - size_t resultsize;
92 - char old_sha1_prefix[41];
93 - char new_sha1_prefix[41];
94 - struct patch *next;
95 -
96 - /* three-way fallback result */
97 - struct object_id threeway_stage[3];
98 -};
99 -
100 -static void free_fragment_list(struct fragment *list)
101 -{
102 - while (list) {
103 - struct fragment *next = list->next;
104 - if (list->free_patch)
105 - free((char *)list->patch);
106 - free(list);
107 - list = next;
108 - }
109 -}
110 -
111 -static void free_patch(struct patch *patch)
112 -{
113 - free_fragment_list(patch->fragments);
114 - free(patch->def_name);
115 - free(patch->old_name);
116 - free(patch->new_name);
117 - free(patch->result);
118 - free(patch);
119 -}
120 -
121 -static void free_patch_list(struct patch *list)
122 -{
123 - while (list) {
124 - struct patch *next = list->next;
125 - free_patch(list);
126 - list = next;
127 - }
128 -}
129 -
130 -/*
131 - * A line in a file, len-bytes long (includes the terminating LF,
132 - * except for an incomplete line at the end if the file ends with
133 - * one), and its contents hashes to 'hash'.
134 - */
135 -struct line {
136 - size_t len;
137 - unsigned hash : 24;
138 - unsigned flag : 8;
139 -#define LINE_COMMON 1
140 -#define LINE_PATCHED 2
141 -};
142 -
143 -/*
144 - * This represents a "file", which is an array of "lines".
145 - */
146 -struct image {
147 - char *buf;
148 - size_t len;
149 - size_t nr;
150 - size_t alloc;
151 - struct line *line_allocated;
152 - struct line *line;
153 -};
154 -
155 -static uint32_t hash_line(const char *cp, size_t len)
156 -{
157 - size_t i;
158 - uint32_t h;
159 - for (i = 0, h = 0; i < len; i++) {
160 - if (!isspace(cp[i])) {
161 - h = h * 3 + (cp[i] & 0xff);
162 - }
163 - }
164 - return h;
165 -}
166 -
167 -/*
168 - * Compare lines s1 of length n1 and s2 of length n2, ignoring
169 - * whitespace difference. Returns 1 if they match, 0 otherwise
170 - */
171 -static int fuzzy_matchlines(const char *s1, size_t n1,
172 - const char *s2, size_t n2)
173 -{
174 - const char *last1 = s1 + n1 - 1;
175 - const char *last2 = s2 + n2 - 1;
176 - int result = 0;
177 -
178 - /* ignore line endings */
179 - while ((*last1 == '\r') || (*last1 == '\n'))
180 - last1--;
181 - while ((*last2 == '\r') || (*last2 == '\n'))
182 - last2--;
183 -
184 - /* skip leading whitespaces, if both begin with whitespace */
185 - if (s1 <= last1 && s2 <= last2 && isspace(*s1) && isspace(*s2)) {
186 - while (isspace(*s1) && (s1 <= last1))
187 - s1++;
188 - while (isspace(*s2) && (s2 <= last2))
189 - s2++;
190 - }
191 - /* early return if both lines are empty */
192 - if ((s1 > last1) && (s2 > last2))
193 - return 1;
194 - while (!result) {
195 - result = *s1++ - *s2++;
196 - /*
197 - * Skip whitespace inside. We check for whitespace on
198 - * both buffers because we don't want "a b" to match
199 - * "ab"
200 - */
201 - if (isspace(*s1) && isspace(*s2)) {
202 - while (isspace(*s1) && s1 <= last1)
203 - s1++;
204 - while (isspace(*s2) && s2 <= last2)
205 - s2++;
206 - }
207 - /*
208 - * If we reached the end on one side only,
209 - * lines don't match
210 - */
211 - if (
212 - ((s2 > last2) && (s1 <= last1)) ||
213 - ((s1 > last1) && (s2 <= last2)))
214 - return 0;
215 - if ((s1 > last1) && (s2 > last2))
216 - break;
217 - }
218 -
219 - return !result;
220 -}
221 -
222 -static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag)
223 -{
224 - ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc);
225 - img->line_allocated[img->nr].len = len;
226 - img->line_allocated[img->nr].hash = hash_line(bol, len);
227 - img->line_allocated[img->nr].flag = flag;
228 - img->nr++;
229 -}
230 -
231 -/*
232 - * "buf" has the file contents to be patched (read from various sources).
233 - * attach it to "image" and add line-based index to it.
234 - * "image" now owns the "buf".
235 - */
236 -static void prepare_image(struct image *image, char *buf, size_t len,
237 - int prepare_linetable)
238 -{
239 - const char *cp, *ep;
240 -
241 - memset(image, 0, sizeof(*image));
242 - image->buf = buf;
243 - image->len = len;
244 -
245 - if (!prepare_linetable)
246 - return;
247 -
248 - ep = image->buf + image->len;
249 - cp = image->buf;
250 - while (cp < ep) {
251 - const char *next;
252 - for (next = cp; next < ep && *next != '\n'; next++)
253 - ;
254 - if (next < ep)
255 - next++;
256 - add_line_info(image, cp, next - cp, 0);
257 - cp = next;
258 - }
259 - image->line = image->line_allocated;
260 -}
261 -
262 -static void clear_image(struct image *image)
263 -{
264 - free(image->buf);
265 - free(image->line_allocated);
266 - memset(image, 0, sizeof(*image));
267 -}
268 -
269 -/* fmt must contain _one_ %s and no other substitution */
270 -static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
271 -{
272 - struct strbuf sb = STRBUF_INIT;
273 -
274 - if (patch->old_name && patch->new_name &&
275 - strcmp(patch->old_name, patch->new_name)) {
276 - quote_c_style(patch->old_name, &sb, NULL, 0);
277 - strbuf_addstr(&sb, " => ");
278 - quote_c_style(patch->new_name, &sb, NULL, 0);
279 - } else {
280 - const char *n = patch->new_name;
281 - if (!n)
282 - n = patch->old_name;
283 - quote_c_style(n, &sb, NULL, 0);
284 - }
285 - fprintf(output, fmt, sb.buf);
286 - fputc('\n', output);
287 - strbuf_release(&sb);
288 -}
289 -
290 -#define SLOP (16)
291 -
292 -static int read_patch_file(struct strbuf *sb, int fd)
293 -{
294 - if (strbuf_read(sb, fd, 0) < 0)
295 - return error_errno("git apply: failed to read");
296 -
297 - /*
298 - * Make sure that we have some slop in the buffer
299 - * so that we can do speculative "memcmp" etc, and
300 - * see to it that it is NUL-filled.
301 - */
302 - strbuf_grow(sb, SLOP);
303 - memset(sb->buf + sb->len, 0, SLOP);
304 - return 0;
305 -}
306 -
307 -static unsigned long linelen(const char *buffer, unsigned long size)
308 -{
309 - unsigned long len = 0;
310 - while (size--) {
311 - len++;
312 - if (*buffer++ == '\n')
313 - break;
314 - }
315 - return len;
316 -}
317 -
318 -static int is_dev_null(const char *str)
319 -{
320 - return skip_prefix(str, "/dev/null", &str) && isspace(*str);
321 -}
322 -
323 -#define TERM_SPACE 1
324 -#define TERM_TAB 2
325 -
326 -static int name_terminate(int c, int terminate)
327 -{
328 - if (c == ' ' && !(terminate & TERM_SPACE))
329 - return 0;
330 - if (c == '\t' && !(terminate & TERM_TAB))
331 - return 0;
332 -
333 - return 1;
334 -}
335 -
336 -/* remove double slashes to make --index work with such filenames */
337 -static char *squash_slash(char *name)
338 -{
339 - int i = 0, j = 0;
340 -
341 - if (!name)
342 - return NULL;
343 -
344 - while (name[i]) {
345 - if ((name[j++] = name[i++]) == '/')
346 - while (name[i] == '/')
347 - i++;
348 - }
349 - name[j] = '\0';
350 - return name;
351 -}
352 -
353 -static char *find_name_gnu(struct apply_state *state,
354 - const char *line,
355 - const char *def,
356 - int p_value)
357 -{
358 - struct strbuf name = STRBUF_INIT;
359 - char *cp;
360 -
361 - /*
362 - * Proposed "new-style" GNU patch/diff format; see
363 - * http://marc.info/?l=git&m=112927316408690&w=2
364 - */
365 - if (unquote_c_style(&name, line, NULL)) {
366 - strbuf_release(&name);
367 - return NULL;
368 - }
369 -
370 - for (cp = name.buf; p_value; p_value--) {
371 - cp = strchr(cp, '/');
372 - if (!cp) {
373 - strbuf_release(&name);
374 - return NULL;
375 - }
376 - cp++;
377 - }
378 -
379 - strbuf_remove(&name, 0, cp - name.buf);
380 - if (state->root.len)
381 - strbuf_insert(&name, 0, state->root.buf, state->root.len);
382 - return squash_slash(strbuf_detach(&name, NULL));
383 -}
384 -
385 -static size_t sane_tz_len(const char *line, size_t len)
386 -{
387 - const char *tz, *p;
388 -
389 - if (len < strlen(" +0500") || line[len-strlen(" +0500")] != ' ')
390 - return 0;
391 - tz = line + len - strlen(" +0500");
392 -
393 - if (tz[1] != '+' && tz[1] != '-')
394 - return 0;
395 -
396 - for (p = tz + 2; p != line + len; p++)
397 - if (!isdigit(*p))
398 - return 0;
399 -
400 - return line + len - tz;
401 -}
402 -
403 -static size_t tz_with_colon_len(const char *line, size_t len)
404 -{
405 - const char *tz, *p;
406 -
407 - if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
408 - return 0;
409 - tz = line + len - strlen(" +08:00");
410 -
411 - if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
412 - return 0;
413 - p = tz + 2;
414 - if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
415 - !isdigit(*p++) || !isdigit(*p++))
416 - return 0;
417 -
418 - return line + len - tz;
419 -}
420 -
421 -static size_t date_len(const char *line, size_t len)
422 -{
423 - const char *date, *p;
424 -
425 - if (len < strlen("72-02-05") || line[len-strlen("-05")] != '-')
426 - return 0;
427 - p = date = line + len - strlen("72-02-05");
428 -
429 - if (!isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
430 - !isdigit(*p++) || !isdigit(*p++) || *p++ != '-' ||
431 - !isdigit(*p++) || !isdigit(*p++)) /* Not a date. */
432 - return 0;
433 -
434 - if (date - line >= strlen("19") &&
435 - isdigit(date[-1]) && isdigit(date[-2])) /* 4-digit year */
436 - date -= strlen("19");
437 -
438 - return line + len - date;
439 -}
440 -
441 -static size_t short_time_len(const char *line, size_t len)
442 -{
443 - const char *time, *p;
444 -
445 - if (len < strlen(" 07:01:32") || line[len-strlen(":32")] != ':')
446 - return 0;
447 - p = time = line + len - strlen(" 07:01:32");
448 -
449 - /* Permit 1-digit hours? */
450 - if (*p++ != ' ' ||
451 - !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
452 - !isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
453 - !isdigit(*p++) || !isdigit(*p++)) /* Not a time. */
454 - return 0;
455 -
456 - return line + len - time;
457 -}
458 -
459 -static size_t fractional_time_len(const char *line, size_t len)
460 -{
461 - const char *p;
462 - size_t n;
463 -
464 - /* Expected format: 19:41:17.620000023 */
465 - if (!len || !isdigit(line[len - 1]))
466 - return 0;
467 - p = line + len - 1;
468 -
469 - /* Fractional seconds. */
470 - while (p > line && isdigit(*p))
471 - p--;
472 - if (*p != '.')
473 - return 0;
474 -
475 - /* Hours, minutes, and whole seconds. */
476 - n = short_time_len(line, p - line);
477 - if (!n)
478 - return 0;
479 -
480 - return line + len - p + n;
481 -}
482 -
483 -static size_t trailing_spaces_len(const char *line, size_t len)
484 -{
485 - const char *p;
486 -
487 - /* Expected format: ' ' x (1 or more) */
488 - if (!len || line[len - 1] != ' ')
489 - return 0;
490 -
491 - p = line + len;
492 - while (p != line) {
493 - p--;
494 - if (*p != ' ')
495 - return line + len - (p + 1);
496 - }
497 -
498 - /* All spaces! */
499 - return len;
500 -}
501 -
502 -static size_t diff_timestamp_len(const char *line, size_t len)
503 -{
504 - const char *end = line + len;
505 - size_t n;
506 -
507 - /*
508 - * Posix: 2010-07-05 19:41:17
509 - * GNU: 2010-07-05 19:41:17.620000023 -0500
510 - */
511 -
512 - if (!isdigit(end[-1]))
513 - return 0;
514 -
515 - n = sane_tz_len(line, end - line);
516 - if (!n)
517 - n = tz_with_colon_len(line, end - line);
518 - end -= n;
519 -
520 - n = short_time_len(line, end - line);
521 - if (!n)
522 - n = fractional_time_len(line, end - line);
523 - end -= n;
524 -
525 - n = date_len(line, end - line);
526 - if (!n) /* No date. Too bad. */
527 - return 0;
528 - end -= n;
529 -
530 - if (end == line) /* No space before date. */
531 - return 0;
532 - if (end[-1] == '\t') { /* Success! */
533 - end--;
534 - return line + len - end;
535 - }
536 - if (end[-1] != ' ') /* No space before date. */
537 - return 0;
538 -
539 - /* Whitespace damage. */
540 - end -= trailing_spaces_len(line, end - line);
541 - return line + len - end;
542 -}
543 -
544 -static char *find_name_common(struct apply_state *state,
545 - const char *line,
546 - const char *def,
547 - int p_value,
548 - const char *end,
549 - int terminate)
550 -{
551 - int len;
552 - const char *start = NULL;
553 -
554 - if (p_value == 0)
555 - start = line;
556 - while (line != end) {
557 - char c = *line;
558 -
559 - if (!end && isspace(c)) {
560 - if (c == '\n')
561 - break;
562 - if (name_terminate(c, terminate))
563 - break;
564 - }
565 - line++;
566 - if (c == '/' && !--p_value)
567 - start = line;
568 - }
569 - if (!start)
570 - return squash_slash(xstrdup_or_null(def));
571 - len = line - start;
572 - if (!len)
573 - return squash_slash(xstrdup_or_null(def));
574 -
575 - /*
576 - * Generally we prefer the shorter name, especially
577 - * if the other one is just a variation of that with
578 - * something else tacked on to the end (ie "file.orig"
579 - * or "file~").
580 - */
581 - if (def) {
582 - int deflen = strlen(def);
583 - if (deflen < len && !strncmp(start, def, deflen))
584 - return squash_slash(xstrdup(def));
585 - }
586 -
587 - if (state->root.len) {
588 - char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
589 - return squash_slash(ret);
590 - }
591 -
592 - return squash_slash(xmemdupz(start, len));
593 -}
594 -
595 -static char *find_name(struct apply_state *state,
596 - const char *line,
597 - char *def,
598 - int p_value,
599 - int terminate)
600 -{
601 - if (*line == '"') {
602 - char *name = find_name_gnu(state, line, def, p_value);
603 - if (name)
604 - return name;
605 - }
606 -
607 - return find_name_common(state, line, def, p_value, NULL, terminate);
608 -}
609 -
610 -static char *find_name_traditional(struct apply_state *state,
611 - const char *line,
612 - char *def,
613 - int p_value)
614 -{
615 - size_t len;
616 - size_t date_len;
617 -
618 - if (*line == '"') {
619 - char *name = find_name_gnu(state, line, def, p_value);
620 - if (name)
621 - return name;
622 - }
623 -
624 - len = strchrnul(line, '\n') - line;
625 - date_len = diff_timestamp_len(line, len);
626 - if (!date_len)
627 - return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
628 - len -= date_len;
629 -
630 - return find_name_common(state, line, def, p_value, line + len, 0);
631 -}
632 -
633 -static int count_slashes(const char *cp)
634 -{
635 - int cnt = 0;
636 - char ch;
637 -
638 - while ((ch = *cp++))
639 - if (ch == '/')
640 - cnt++;
641 - return cnt;
642 -}
643 -
644 -/*
645 - * Given the string after "--- " or "+++ ", guess the appropriate
646 - * p_value for the given patch.
647 - */
648 -static int guess_p_value(struct apply_state *state, const char *nameline)
649 -{
650 - char *name, *cp;
651 - int val = -1;
652 -
653 - if (is_dev_null(nameline))
654 - return -1;
655 - name = find_name_traditional(state, nameline, NULL, 0);
656 - if (!name)
657 - return -1;
658 - cp = strchr(name, '/');
659 - if (!cp)
660 - val = 0;
661 - else if (state->prefix) {
662 - /*
663 - * Does it begin with "a/$our-prefix" and such? Then this is
664 - * very likely to apply to our directory.
665 - */
666 - if (!strncmp(name, state->prefix, state->prefix_length))
667 - val = count_slashes(state->prefix);
668 - else {
669 - cp++;
670 - if (!strncmp(cp, state->prefix, state->prefix_length))
671 - val = count_slashes(state->prefix) + 1;
672 - }
673 - }
674 - free(name);
675 - return val;
676 -}
677 -
678 -/*
679 - * Does the ---/+++ line have the POSIX timestamp after the last HT?
680 - * GNU diff puts epoch there to signal a creation/deletion event. Is
681 - * this such a timestamp?
682 - */
683 -static int has_epoch_timestamp(const char *nameline)
684 -{
685 - /*
686 - * We are only interested in epoch timestamp; any non-zero
687 - * fraction cannot be one, hence "(\.0+)?" in the regexp below.
688 - * For the same reason, the date must be either 1969-12-31 or
689 - * 1970-01-01, and the seconds part must be "00".
690 - */
691 - const char stamp_regexp[] =
692 - "^(1969-12-31|1970-01-01)"
693 - " "
694 - "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
695 - " "
696 - "([-+][0-2][0-9]:?[0-5][0-9])\n";
697 - const char *timestamp = NULL, *cp, *colon;
698 - static regex_t *stamp;
699 - regmatch_t m[10];
700 - int zoneoffset;
701 - int hourminute;
702 - int status;
703 -
704 - for (cp = nameline; *cp != '\n'; cp++) {
705 - if (*cp == '\t')
706 - timestamp = cp + 1;
707 - }
708 - if (!timestamp)
709 - return 0;
710 - if (!stamp) {
711 - stamp = xmalloc(sizeof(*stamp));
712 - if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
713 - warning(_("Cannot prepare timestamp regexp %s"),
714 - stamp_regexp);
715 - return 0;
716 - }
717 - }
718 -
719 - status = regexec(stamp, timestamp, ARRAY_SIZE(m), m, 0);
720 - if (status) {
721 - if (status != REG_NOMATCH)
722 - warning(_("regexec returned %d for input: %s"),
723 - status, timestamp);
724 - return 0;
725 - }
726 -
727 - zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
728 - if (*colon == ':')
729 - zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
730 - else
731 - zoneoffset = (zoneoffset / 100) * 60 + (zoneoffset % 100);
732 - if (timestamp[m[3].rm_so] == '-')
733 - zoneoffset = -zoneoffset;
734 -
735 - /*
736 - * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
737 - * (west of GMT) or 1970-01-01 (east of GMT)
738 - */
739 - if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
740 - (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
741 - return 0;
742 -
743 - hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
744 - strtol(timestamp + 14, NULL, 10) -
745 - zoneoffset);
746 -
747 - return ((zoneoffset < 0 && hourminute == 1440) ||
748 - (0 <= zoneoffset && !hourminute));
749 -}
750 -
751 -/*
752 - * Get the name etc info from the ---/+++ lines of a traditional patch header
753 - *
754 - * FIXME! The end-of-filename heuristics are kind of screwy. For existing
755 - * files, we can happily check the index for a match, but for creating a
756 - * new file we should try to match whatever "patch" does. I have no idea.
757 - */
758 -static int parse_traditional_patch(struct apply_state *state,
759 - const char *first,
760 - const char *second,
761 - struct patch *patch)
762 -{
763 - char *name;
764 -
765 - first += 4; /* skip "--- " */
766 - second += 4; /* skip "+++ " */
767 - if (!state->p_value_known) {
768 - int p, q;
769 - p = guess_p_value(state, first);
770 - q = guess_p_value(state, second);
771 - if (p < 0) p = q;
772 - if (0 <= p && p == q) {
773 - state->p_value = p;
774 - state->p_value_known = 1;
775 - }
776 - }
777 - if (is_dev_null(first)) {
778 - patch->is_new = 1;
779 - patch->is_delete = 0;
780 - name = find_name_traditional(state, second, NULL, state->p_value);
781 - patch->new_name = name;
782 - } else if (is_dev_null(second)) {
783 - patch->is_new = 0;
784 - patch->is_delete = 1;
785 - name = find_name_traditional(state, first, NULL, state->p_value);
786 - patch->old_name = name;
787 - } else {
788 - char *first_name;
789 - first_name = find_name_traditional(state, first, NULL, state->p_value);
790 - name = find_name_traditional(state, second, first_name, state->p_value);
791 - free(first_name);
792 - if (has_epoch_timestamp(first)) {
793 - patch->is_new = 1;
794 - patch->is_delete = 0;
795 - patch->new_name = name;
796 - } else if (has_epoch_timestamp(second)) {
797 - patch->is_new = 0;
798 - patch->is_delete = 1;
799 - patch->old_name = name;
800 - } else {
801 - patch->old_name = name;
802 - patch->new_name = xstrdup_or_null(name);
803 - }
804 - }
805 - if (!name)
806 - return error(_("unable to find filename in patch at line %d"), state->linenr);
807 -
808 - return 0;
809 -}
810 -
811 -static int gitdiff_hdrend(struct apply_state *state,
812 - const char *line,
813 - struct patch *patch)
814 -{
815 - return 1;
816 -}
817 -
818 -/*
819 - * We're anal about diff header consistency, to make
820 - * sure that we don't end up having strange ambiguous
821 - * patches floating around.
822 - *
823 - * As a result, gitdiff_{old|new}name() will check
824 - * their names against any previous information, just
825 - * to make sure..
826 - */
827 -#define DIFF_OLD_NAME 0
828 -#define DIFF_NEW_NAME 1
829 -
830 -static int gitdiff_verify_name(struct apply_state *state,
831 - const char *line,
832 - int isnull,
833 - char **name,
834 - int side)
835 -{
836 - if (!*name && !isnull) {
837 - *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
838 - return 0;
839 - }
840 -
841 - if (*name) {
842 - int len = strlen(*name);
843 - char *another;
844 - if (isnull)
845 - return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
846 - *name, state->linenr);
847 - another = find_name(state, line, NULL, state->p_value, TERM_TAB);
848 - if (!another || memcmp(another, *name, len + 1)) {
849 - free(another);
850 - return error((side == DIFF_NEW_NAME) ?
851 - _("git apply: bad git-diff - inconsistent new filename on line %d") :
852 - _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
853 - }
854 - free(another);
855 - } else {
856 - /* expect "/dev/null" */
857 - if (memcmp("/dev/null", line, 9) || line[9] != '\n')
858 - return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
859 - }
860 -
861 - return 0;
862 -}
863 -
864 -static int gitdiff_oldname(struct apply_state *state,
865 - const char *line,
866 - struct patch *patch)
867 -{
868 - return gitdiff_verify_name(state, line,
869 - patch->is_new, &patch->old_name,
870 - DIFF_OLD_NAME);
871 -}
872 -
873 -static int gitdiff_newname(struct apply_state *state,
874 - const char *line,
875 - struct patch *patch)
876 -{
877 - return gitdiff_verify_name(state, line,
878 - patch->is_delete, &patch->new_name,
879 - DIFF_NEW_NAME);
880 -}
881 -
882 -static int gitdiff_oldmode(struct apply_state *state,
883 - const char *line,
884 - struct patch *patch)
885 -{
886 - patch->old_mode = strtoul(line, NULL, 8);
887 - return 0;
888 -}
889 -
890 -static int gitdiff_newmode(struct apply_state *state,
891 - const char *line,
892 - struct patch *patch)
893 -{
894 - patch->new_mode = strtoul(line, NULL, 8);
895 - return 0;
896 -}
897 -
898 -static int gitdiff_delete(struct apply_state *state,
899 - const char *line,
900 - struct patch *patch)
901 -{
902 - patch->is_delete = 1;
903 - free(patch->old_name);
904 - patch->old_name = xstrdup_or_null(patch->def_name);
905 - return gitdiff_oldmode(state, line, patch);
906 -}
907 -
908 -static int gitdiff_newfile(struct apply_state *state,
909 - const char *line,
910 - struct patch *patch)
911 -{
912 - patch->is_new = 1;
913 - free(patch->new_name);
914 - patch->new_name = xstrdup_or_null(patch->def_name);
915 - return gitdiff_newmode(state, line, patch);
916 -}
917 -
918 -static int gitdiff_copysrc(struct apply_state *state,
919 - const char *line,
920 - struct patch *patch)
921 -{
922 - patch->is_copy = 1;
923 - free(patch->old_name);
924 - patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
925 - return 0;
926 -}
927 -
928 -static int gitdiff_copydst(struct apply_state *state,
929 - const char *line,
930 - struct patch *patch)
931 -{
932 - patch->is_copy = 1;
933 - free(patch->new_name);
934 - patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
935 - return 0;
936 -}
937 -
938 -static int gitdiff_renamesrc(struct apply_state *state,
939 - const char *line,
940 - struct patch *patch)
941 -{
942 - patch->is_rename = 1;
943 - free(patch->old_name);
944 - patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
945 - return 0;
946 -}
947 -
948 -static int gitdiff_renamedst(struct apply_state *state,
949 - const char *line,
950 - struct patch *patch)
951 -{
952 - patch->is_rename = 1;
953 - free(patch->new_name);
954 - patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
955 - return 0;
956 -}
957 -
958 -static int gitdiff_similarity(struct apply_state *state,
959 - const char *line,
960 - struct patch *patch)
961 -{
962 - unsigned long val = strtoul(line, NULL, 10);
963 - if (val <= 100)
964 - patch->score = val;
965 - return 0;
966 -}
967 -
968 -static int gitdiff_dissimilarity(struct apply_state *state,
969 - const char *line,
970 - struct patch *patch)
971 -{
972 - unsigned long val = strtoul(line, NULL, 10);
973 - if (val <= 100)
974 - patch->score = val;
975 - return 0;
976 -}
977 -
978 -static int gitdiff_index(struct apply_state *state,
979 - const char *line,
980 - struct patch *patch)
981 -{
982 - /*
983 - * index line is N hexadecimal, "..", N hexadecimal,
984 - * and optional space with octal mode.
985 - */
986 - const char *ptr, *eol;
987 - int len;
988 -
989 - ptr = strchr(line, '.');
990 - if (!ptr || ptr[1] != '.' || 40 < ptr - line)
991 - return 0;
992 - len = ptr - line;
993 - memcpy(patch->old_sha1_prefix, line, len);
994 - patch->old_sha1_prefix[len] = 0;
995 -
996 - line = ptr + 2;
997 - ptr = strchr(line, ' ');
998 - eol = strchrnul(line, '\n');
999 -
1000 - if (!ptr || eol < ptr)
1001 - ptr = eol;
1002 - len = ptr - line;
1003 -
1004 - if (40 < len)
1005 - return 0;
1006 - memcpy(patch->new_sha1_prefix, line, len);
1007 - patch->new_sha1_prefix[len] = 0;
1008 - if (*ptr == ' ')
1009 - patch->old_mode = strtoul(ptr+1, NULL, 8);
1010 - return 0;
1011 -}
1012 -
1013 -/*
1014 - * This is normal for a diff that doesn't change anything: we'll fall through
1015 - * into the next diff. Tell the parser to break out.
1016 - */
1017 -static int gitdiff_unrecognized(struct apply_state *state,
1018 - const char *line,
1019 - struct patch *patch)
1020 -{
1021 - return 1;
1022 -}
1023 -
1024 -/*
1025 - * Skip p_value leading components from "line"; as we do not accept
1026 - * absolute paths, return NULL in that case.
1027 - */
1028 -static const char *skip_tree_prefix(struct apply_state *state,
1029 - const char *line,
1030 - int llen)
1031 -{
1032 - int nslash;
1033 - int i;
1034 -
1035 - if (!state->p_value)
1036 - return (llen && line[0] == '/') ? NULL : line;
1037 -
1038 - nslash = state->p_value;
1039 - for (i = 0; i < llen; i++) {
1040 - int ch = line[i];
1041 - if (ch == '/' && --nslash <= 0)
1042 - return (i == 0) ? NULL : &line[i + 1];
1043 - }
1044 - return NULL;
1045 -}
1046 -
1047 -/*
1048 - * This is to extract the same name that appears on "diff --git"
1049 - * line. We do not find and return anything if it is a rename
1050 - * patch, and it is OK because we will find the name elsewhere.
1051 - * We need to reliably find name only when it is mode-change only,
1052 - * creation or deletion of an empty file. In any of these cases,
1053 - * both sides are the same name under a/ and b/ respectively.
1054 - */
1055 -static char *git_header_name(struct apply_state *state,
1056 - const char *line,
1057 - int llen)
1058 -{
1059 - const char *name;
1060 - const char *second = NULL;
1061 - size_t len, line_len;
1062 -
1063 - line += strlen("diff --git ");
1064 - llen -= strlen("diff --git ");
1065 -
1066 - if (*line == '"') {
1067 - const char *cp;
1068 - struct strbuf first = STRBUF_INIT;
1069 - struct strbuf sp = STRBUF_INIT;
1070 -
1071 - if (unquote_c_style(&first, line, &second))
1072 - goto free_and_fail1;
1073 -
1074 - /* strip the a/b prefix including trailing slash */
1075 - cp = skip_tree_prefix(state, first.buf, first.len);
1076 - if (!cp)
1077 - goto free_and_fail1;
1078 - strbuf_remove(&first, 0, cp - first.buf);
1079 -
1080 - /*
1081 - * second points at one past closing dq of name.
1082 - * find the second name.
1083 - */
1084 - while ((second < line + llen) && isspace(*second))
1085 - second++;
1086 -
1087 - if (line + llen <= second)
1088 - goto free_and_fail1;
1089 - if (*second == '"') {
1090 - if (unquote_c_style(&sp, second, NULL))
1091 - goto free_and_fail1;
1092 - cp = skip_tree_prefix(state, sp.buf, sp.len);
1093 - if (!cp)
1094 - goto free_and_fail1;
1095 - /* They must match, otherwise ignore */
1096 - if (strcmp(cp, first.buf))
1097 - goto free_and_fail1;
1098 - strbuf_release(&sp);
1099 - return strbuf_detach(&first, NULL);
1100 - }
1101 -
1102 - /* unquoted second */
1103 - cp = skip_tree_prefix(state, second, line + llen - second);
1104 - if (!cp)
1105 - goto free_and_fail1;
1106 - if (line + llen - cp != first.len ||
1107 - memcmp(first.buf, cp, first.len))
1108 - goto free_and_fail1;
1109 - return strbuf_detach(&first, NULL);
1110 -
1111 - free_and_fail1:
1112 - strbuf_release(&first);
1113 - strbuf_release(&sp);
1114 - return NULL;
1115 - }
1116 -
1117 - /* unquoted first name */
1118 - name = skip_tree_prefix(state, line, llen);
1119 - if (!name)
1120 - return NULL;
1121 -
1122 - /*
1123 - * since the first name is unquoted, a dq if exists must be
1124 - * the beginning of the second name.
1125 - */
1126 - for (second = name; second < line + llen; second++) {
1127 - if (*second == '"') {
1128 - struct strbuf sp = STRBUF_INIT;
1129 - const char *np;
1130 -
1131 - if (unquote_c_style(&sp, second, NULL))
1132 - goto free_and_fail2;
1133 -
1134 - np = skip_tree_prefix(state, sp.buf, sp.len);
1135 - if (!np)
1136 - goto free_and_fail2;
1137 -
1138 - len = sp.buf + sp.len - np;
1139 - if (len < second - name &&
1140 - !strncmp(np, name, len) &&
1141 - isspace(name[len])) {
1142 - /* Good */
1143 - strbuf_remove(&sp, 0, np - sp.buf);
1144 - return strbuf_detach(&sp, NULL);
1145 - }
1146 -
1147 - free_and_fail2:
1148 - strbuf_release(&sp);
1149 - return NULL;
1150 - }
1151 - }
1152 -
1153 - /*
1154 - * Accept a name only if it shows up twice, exactly the same
1155 - * form.
1156 - */
1157 - second = strchr(name, '\n');
1158 - if (!second)
1159 - return NULL;
1160 - line_len = second - name;
1161 - for (len = 0 ; ; len++) {
1162 - switch (name[len]) {
1163 - default:
1164 - continue;
1165 - case '\n':
1166 - return NULL;
1167 - case '\t': case ' ':
1168 - /*
1169 - * Is this the separator between the preimage
1170 - * and the postimage pathname? Again, we are
1171 - * only interested in the case where there is
1172 - * no rename, as this is only to set def_name
1173 - * and a rename patch has the names elsewhere
1174 - * in an unambiguous form.
1175 - */
1176 - if (!name[len + 1])
1177 - return NULL; /* no postimage name */
1178 - second = skip_tree_prefix(state, name + len + 1,
1179 - line_len - (len + 1));
1180 - if (!second)
1181 - return NULL;
1182 - /*
1183 - * Does len bytes starting at "name" and "second"
1184 - * (that are separated by one HT or SP we just
1185 - * found) exactly match?
1186 - */
1187 - if (second[len] == '\n' && !strncmp(name, second, len))
1188 - return xmemdupz(name, len);
1189 - }
1190 - }
1191 -}
1192 -
1193 -/* Verify that we recognize the lines following a git header */
1194 -static int parse_git_header(struct apply_state *state,
1195 - const char *line,
1196 - int len,
1197 - unsigned int size,
1198 - struct patch *patch)
1199 -{
1200 - unsigned long offset;
1201 -
1202 - /* A git diff has explicit new/delete information, so we don't guess */
1203 - patch->is_new = 0;
1204 - patch->is_delete = 0;
1205 -
1206 - /*
1207 - * Some things may not have the old name in the
1208 - * rest of the headers anywhere (pure mode changes,
1209 - * or removing or adding empty files), so we get
1210 - * the default name from the header.
1211 - */
1212 - patch->def_name = git_header_name(state, line, len);
1213 - if (patch->def_name && state->root.len) {
1214 - char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1215 - free(patch->def_name);
1216 - patch->def_name = s;
1217 - }
1218 -
1219 - line += len;
1220 - size -= len;
1221 - state->linenr++;
1222 - for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1223 - static const struct opentry {
1224 - const char *str;
1225 - int (*fn)(struct apply_state *, const char *, struct patch *);
1226 - } optable[] = {
1227 - { "@@ -", gitdiff_hdrend },
1228 - { "--- ", gitdiff_oldname },
1229 - { "+++ ", gitdiff_newname },
1230 - { "old mode ", gitdiff_oldmode },
1231 - { "new mode ", gitdiff_newmode },
1232 - { "deleted file mode ", gitdiff_delete },
1233 - { "new file mode ", gitdiff_newfile },
1234 - { "copy from ", gitdiff_copysrc },
1235 - { "copy to ", gitdiff_copydst },
1236 - { "rename old ", gitdiff_renamesrc },
1237 - { "rename new ", gitdiff_renamedst },
1238 - { "rename from ", gitdiff_renamesrc },
1239 - { "rename to ", gitdiff_renamedst },
1240 - { "similarity index ", gitdiff_similarity },
1241 - { "dissimilarity index ", gitdiff_dissimilarity },
1242 - { "index ", gitdiff_index },
1243 - { "", gitdiff_unrecognized },
1244 - };
1245 - int i;
1246 -
1247 - len = linelen(line, size);
1248 - if (!len || line[len-1] != '\n')
1249 - break;
1250 - for (i = 0; i < ARRAY_SIZE(optable); i++) {
1251 - const struct opentry *p = optable + i;
1252 - int oplen = strlen(p->str);
1253 - int res;
1254 - if (len < oplen || memcmp(p->str, line, oplen))
1255 - continue;
1256 - res = p->fn(state, line + oplen, patch);
1257 - if (res < 0)
1258 - return -1;
1259 - if (res > 0)
1260 - return offset;
1261 - break;
1262 - }
1263 - }
1264 -
1265 - return offset;
1266 -}
1267 -
1268 -static int parse_num(const char *line, unsigned long *p)
1269 -{
1270 - char *ptr;
1271 -
1272 - if (!isdigit(*line))
1273 - return 0;
1274 - *p = strtoul(line, &ptr, 10);
1275 - return ptr - line;
1276 -}
1277 -
1278 -static int parse_range(const char *line, int len, int offset, const char *expect,
1279 - unsigned long *p1, unsigned long *p2)
1280 -{
1281 - int digits, ex;
1282 -
1283 - if (offset < 0 || offset >= len)
1284 - return -1;
1285 - line += offset;
1286 - len -= offset;
1287 -
1288 - digits = parse_num(line, p1);
1289 - if (!digits)
1290 - return -1;
1291 -
1292 - offset += digits;
1293 - line += digits;
1294 - len -= digits;
1295 -
1296 - *p2 = 1;
1297 - if (*line == ',') {
1298 - digits = parse_num(line+1, p2);
1299 - if (!digits)
1300 - return -1;
1301 -
1302 - offset += digits+1;
1303 - line += digits+1;
1304 - len -= digits+1;
1305 - }
1306 -
1307 - ex = strlen(expect);
1308 - if (ex > len)
1309 - return -1;
1310 - if (memcmp(line, expect, ex))
1311 - return -1;
1312 -
1313 - return offset + ex;
1314 -}
1315 -
1316 -static void recount_diff(const char *line, int size, struct fragment *fragment)
1317 -{
1318 - int oldlines = 0, newlines = 0, ret = 0;
1319 -
1320 - if (size < 1) {
1321 - warning("recount: ignore empty hunk");
1322 - return;
1323 - }
1324 -
1325 - for (;;) {
1326 - int len = linelen(line, size);
1327 - size -= len;
1328 - line += len;
1329 -
1330 - if (size < 1)
1331 - break;
1332 -
1333 - switch (*line) {
1334 - case ' ': case '\n':
1335 - newlines++;
1336 - /* fall through */
1337 - case '-':
1338 - oldlines++;
1339 - continue;
1340 - case '+':
1341 - newlines++;
1342 - continue;
1343 - case '\\':
1344 - continue;
1345 - case '@':
1346 - ret = size < 3 || !starts_with(line, "@@ ");
1347 - break;
1348 - case 'd':
1349 - ret = size < 5 || !starts_with(line, "diff ");
1350 - break;
1351 - default:
1352 - ret = -1;
1353 - break;
1354 - }
1355 - if (ret) {
1356 - warning(_("recount: unexpected line: %.*s"),
1357 - (int)linelen(line, size), line);
1358 - return;
1359 - }
1360 - break;
1361 - }
1362 - fragment->oldlines = oldlines;
1363 - fragment->newlines = newlines;
1364 -}
1365 -
1366 -/*
1367 - * Parse a unified diff fragment header of the
1368 - * form "@@ -a,b +c,d @@"
1369 - */
1370 -static int parse_fragment_header(const char *line, int len, struct fragment *fragment)
1371 -{
1372 - int offset;
1373 -
1374 - if (!len || line[len-1] != '\n')
1375 - return -1;
1376 -
1377 - /* Figure out the number of lines in a fragment */
1378 - offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines);
1379 - offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines);
1380 -
1381 - return offset;
1382 -}
1383 -
1384 -/*
1385 - * Find file diff header
1386 - *
1387 - * Returns:
1388 - * -1 if no header was found
1389 - * -128 in case of error
1390 - * the size of the header in bytes (called "offset") otherwise
1391 - */
1392 -static int find_header(struct apply_state *state,
1393 - const char *line,
1394 - unsigned long size,
1395 - int *hdrsize,
1396 - struct patch *patch)
1397 -{
1398 - unsigned long offset, len;
1399 -
1400 - patch->is_toplevel_relative = 0;
1401 - patch->is_rename = patch->is_copy = 0;
1402 - patch->is_new = patch->is_delete = -1;
1403 - patch->old_mode = patch->new_mode = 0;
1404 - patch->old_name = patch->new_name = NULL;
1405 - for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1406 - unsigned long nextlen;
1407 -
1408 - len = linelen(line, size);
1409 - if (!len)
1410 - break;
1411 -
1412 - /* Testing this early allows us to take a few shortcuts.. */
1413 - if (len < 6)
1414 - continue;
1415 -
1416 - /*
1417 - * Make sure we don't find any unconnected patch fragments.
1418 - * That's a sign that we didn't find a header, and that a
1419 - * patch has become corrupted/broken up.
1420 - */
1421 - if (!memcmp("@@ -", line, 4)) {
1422 - struct fragment dummy;
1423 - if (parse_fragment_header(line, len, &dummy) < 0)
1424 - continue;
1425 - error(_("patch fragment without header at line %d: %.*s"),
1426 - state->linenr, (int)len-1, line);
1427 - return -128;
1428 - }
1429 -
1430 - if (size < len + 6)
1431 - break;
1432 -
1433 - /*
1434 - * Git patch? It might not have a real patch, just a rename
1435 - * or mode change, so we handle that specially
1436 - */
1437 - if (!memcmp("diff --git ", line, 11)) {
1438 - int git_hdr_len = parse_git_header(state, line, len, size, patch);
1439 - if (git_hdr_len < 0)
1440 - return -128;
1441 - if (git_hdr_len <= len)
1442 - continue;
1443 - if (!patch->old_name && !patch->new_name) {
1444 - if (!patch->def_name) {
1445 - error(Q_("git diff header lacks filename information when removing "
1446 - "%d leading pathname component (line %d)",
1447 - "git diff header lacks filename information when removing "
1448 - "%d leading pathname components (line %d)",
1449 - state->p_value),
1450 - state->p_value, state->linenr);
1451 - return -128;
1452 - }
1453 - patch->old_name = xstrdup(patch->def_name);
1454 - patch->new_name = xstrdup(patch->def_name);
1455 - }
1456 - if (!patch->is_delete && !patch->new_name) {
1457 - error("git diff header lacks filename information "
1458 - "(line %d)", state->linenr);
1459 - return -128;
1460 - }
1461 - patch->is_toplevel_relative = 1;
1462 - *hdrsize = git_hdr_len;
1463 - return offset;
1464 - }
1465 -
1466 - /* --- followed by +++ ? */
1467 - if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
1468 - continue;
1469 -
1470 - /*
1471 - * We only accept unified patches, so we want it to
1472 - * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1473 - * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1474 - */
1475 - nextlen = linelen(line + len, size - len);
1476 - if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
1477 - continue;
1478 -
1479 - /* Ok, we'll consider it a patch */
1480 - if (parse_traditional_patch(state, line, line+len, patch))
1481 - return -128;
1482 - *hdrsize = len + nextlen;
1483 - state->linenr += 2;
1484 - return offset;
1485 - }
1486 - return -1;
1487 -}
1488 -
1489 -static void record_ws_error(struct apply_state *state,
1490 - unsigned result,
1491 - const char *line,
1492 - int len,
1493 - int linenr)
1494 -{
1495 - char *err;
1496 -
1497 - if (!result)
1498 - return;
1499 -
1500 - state->whitespace_error++;
1501 - if (state->squelch_whitespace_errors &&
1502 - state->squelch_whitespace_errors < state->whitespace_error)
1503 - return;
1504 -
1505 - err = whitespace_error_string(result);
1506 - fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1507 - state->patch_input_file, linenr, err, len, line);
1508 - free(err);
1509 -}
1510 -
1511 -static void check_whitespace(struct apply_state *state,
1512 - const char *line,
1513 - int len,
1514 - unsigned ws_rule)
1515 -{
1516 - unsigned result = ws_check(line + 1, len - 1, ws_rule);
1517 -
1518 - record_ws_error(state, result, line + 1, len - 2, state->linenr);
1519 -}
1520 -
1521 -/*
1522 - * Parse a unified diff. Note that this really needs to parse each
1523 - * fragment separately, since the only way to know the difference
1524 - * between a "---" that is part of a patch, and a "---" that starts
1525 - * the next patch is to look at the line counts..
1526 - */
1527 -static int parse_fragment(struct apply_state *state,
1528 - const char *line,
1529 - unsigned long size,
1530 - struct patch *patch,
1531 - struct fragment *fragment)
1532 -{
1533 - int added, deleted;
1534 - int len = linelen(line, size), offset;
1535 - unsigned long oldlines, newlines;
1536 - unsigned long leading, trailing;
1537 -
1538 - offset = parse_fragment_header(line, len, fragment);
1539 - if (offset < 0)
1540 - return -1;
1541 - if (offset > 0 && patch->recount)
1542 - recount_diff(line + offset, size - offset, fragment);
1543 - oldlines = fragment->oldlines;
1544 - newlines = fragment->newlines;
1545 - leading = 0;
1546 - trailing = 0;
1547 -
1548 - /* Parse the thing.. */
1549 - line += len;
1550 - size -= len;
1551 - state->linenr++;
1552 - added = deleted = 0;
1553 - for (offset = len;
1554 - 0 < size;
1555 - offset += len, size -= len, line += len, state->linenr++) {
1556 - if (!oldlines && !newlines)
1557 - break;
1558 - len = linelen(line, size);
1559 - if (!len || line[len-1] != '\n')
1560 - return -1;
1561 - switch (*line) {
1562 - default:
1563 - return -1;
1564 - case '\n': /* newer GNU diff, an empty context line */
1565 - case ' ':
1566 - oldlines--;
1567 - newlines--;
1568 - if (!deleted && !added)
1569 - leading++;
1570 - trailing++;
1571 - if (!state->apply_in_reverse &&
1572 - state->ws_error_action == correct_ws_error)
1573 - check_whitespace(state, line, len, patch->ws_rule);
1574 - break;
1575 - case '-':
1576 - if (state->apply_in_reverse &&
1577 - state->ws_error_action != nowarn_ws_error)
1578 - check_whitespace(state, line, len, patch->ws_rule);
1579 - deleted++;
1580 - oldlines--;
1581 - trailing = 0;
1582 - break;
1583 - case '+':
1584 - if (!state->apply_in_reverse &&
1585 - state->ws_error_action != nowarn_ws_error)
1586 - check_whitespace(state, line, len, patch->ws_rule);
1587 - added++;
1588 - newlines--;
1589 - trailing = 0;
1590 - break;
1591 -
1592 - /*
1593 - * We allow "\ No newline at end of file". Depending
1594 - * on locale settings when the patch was produced we
1595 - * don't know what this line looks like. The only
1596 - * thing we do know is that it begins with "\ ".
1597 - * Checking for 12 is just for sanity check -- any
1598 - * l10n of "\ No newline..." is at least that long.
1599 - */
1600 - case '\\':
1601 - if (len < 12 || memcmp(line, "\\ ", 2))
1602 - return -1;
1603 - break;
1604 - }
1605 - }
1606 - if (oldlines || newlines)
1607 - return -1;
1608 - if (!deleted && !added)
1609 - return -1;
1610 -
1611 - fragment->leading = leading;
1612 - fragment->trailing = trailing;
1613 -
1614 - /*
1615 - * If a fragment ends with an incomplete line, we failed to include
1616 - * it in the above loop because we hit oldlines == newlines == 0
1617 - * before seeing it.
1618 - */
1619 - if (12 < size && !memcmp(line, "\\ ", 2))
1620 - offset += linelen(line, size);
1621 -
1622 - patch->lines_added += added;
1623 - patch->lines_deleted += deleted;
1624 -
1625 - if (0 < patch->is_new && oldlines)
1626 - return error(_("new file depends on old contents"));
1627 - if (0 < patch->is_delete && newlines)
1628 - return error(_("deleted file still has contents"));
1629 - return offset;
1630 -}
1631 -
1632 -/*
1633 - * We have seen "diff --git a/... b/..." header (or a traditional patch
1634 - * header). Read hunks that belong to this patch into fragments and hang
1635 - * them to the given patch structure.
1636 - *
1637 - * The (fragment->patch, fragment->size) pair points into the memory given
1638 - * by the caller, not a copy, when we return.
1639 - *
1640 - * Returns:
1641 - * -1 in case of error,
1642 - * the number of bytes in the patch otherwise.
1643 - */
1644 -static int parse_single_patch(struct apply_state *state,
1645 - const char *line,
1646 - unsigned long size,
1647 - struct patch *patch)
1648 -{
1649 - unsigned long offset = 0;
1650 - unsigned long oldlines = 0, newlines = 0, context = 0;
1651 - struct fragment **fragp = &patch->fragments;
1652 -
1653 - while (size > 4 && !memcmp(line, "@@ -", 4)) {
1654 - struct fragment *fragment;
1655 - int len;
1656 -
1657 - fragment = xcalloc(1, sizeof(*fragment));
1658 - fragment->linenr = state->linenr;
1659 - len = parse_fragment(state, line, size, patch, fragment);
1660 - if (len <= 0) {
1661 - free(fragment);
1662 - return error(_("corrupt patch at line %d"), state->linenr);
1663 - }
1664 - fragment->patch = line;
1665 - fragment->size = len;
1666 - oldlines += fragment->oldlines;
1667 - newlines += fragment->newlines;
1668 - context += fragment->leading + fragment->trailing;
1669 -
1670 - *fragp = fragment;
1671 - fragp = &fragment->next;
1672 -
1673 - offset += len;
1674 - line += len;
1675 - size -= len;
1676 - }
1677 -
1678 - /*
1679 - * If something was removed (i.e. we have old-lines) it cannot
1680 - * be creation, and if something was added it cannot be
1681 - * deletion. However, the reverse is not true; --unified=0
1682 - * patches that only add are not necessarily creation even
1683 - * though they do not have any old lines, and ones that only
1684 - * delete are not necessarily deletion.
1685 - *
1686 - * Unfortunately, a real creation/deletion patch do _not_ have
1687 - * any context line by definition, so we cannot safely tell it
1688 - * apart with --unified=0 insanity. At least if the patch has
1689 - * more than one hunk it is not creation or deletion.
1690 - */
1691 - if (patch->is_new < 0 &&
1692 - (oldlines || (patch->fragments && patch->fragments->next)))
1693 - patch->is_new = 0;
1694 - if (patch->is_delete < 0 &&
1695 - (newlines || (patch->fragments && patch->fragments->next)))
1696 - patch->is_delete = 0;
1697 -
1698 - if (0 < patch->is_new && oldlines)
1699 - return error(_("new file %s depends on old contents"), patch->new_name);
1700 - if (0 < patch->is_delete && newlines)
1701 - return error(_("deleted file %s still has contents"), patch->old_name);
1702 - if (!patch->is_delete && !newlines && context)
1703 - fprintf_ln(stderr,
1704 - _("** warning: "
1705 - "file %s becomes empty but is not deleted"),
1706 - patch->new_name);
1707 -
1708 - return offset;
1709 -}
1710 -
1711 -static inline int metadata_changes(struct patch *patch)
1712 -{
1713 - return patch->is_rename > 0 ||
1714 - patch->is_copy > 0 ||
1715 - patch->is_new > 0 ||
1716 - patch->is_delete ||
1717 - (patch->old_mode && patch->new_mode &&
1718 - patch->old_mode != patch->new_mode);
1719 -}
1720 -
1721 -static char *inflate_it(const void *data, unsigned long size,
1722 - unsigned long inflated_size)
1723 -{
1724 - git_zstream stream;
1725 - void *out;
1726 - int st;
1727 -
1728 - memset(&stream, 0, sizeof(stream));
1729 -
1730 - stream.next_in = (unsigned char *)data;
1731 - stream.avail_in = size;
1732 - stream.next_out = out = xmalloc(inflated_size);
1733 - stream.avail_out = inflated_size;
1734 - git_inflate_init(&stream);
1735 - st = git_inflate(&stream, Z_FINISH);
1736 - git_inflate_end(&stream);
1737 - if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1738 - free(out);
1739 - return NULL;
1740 - }
1741 - return out;
1742 -}
1743 -
1744 -/*
1745 - * Read a binary hunk and return a new fragment; fragment->patch
1746 - * points at an allocated memory that the caller must free, so
1747 - * it is marked as "->free_patch = 1".
1748 - */
1749 -static struct fragment *parse_binary_hunk(struct apply_state *state,
1750 - char **buf_p,
1751 - unsigned long *sz_p,
1752 - int *status_p,
1753 - int *used_p)
1754 -{
1755 - /*
1756 - * Expect a line that begins with binary patch method ("literal"
1757 - * or "delta"), followed by the length of data before deflating.
1758 - * a sequence of 'length-byte' followed by base-85 encoded data
1759 - * should follow, terminated by a newline.
1760 - *
1761 - * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1762 - * and we would limit the patch line to 66 characters,
1763 - * so one line can fit up to 13 groups that would decode
1764 - * to 52 bytes max. The length byte 'A'-'Z' corresponds
1765 - * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1766 - */
1767 - int llen, used;
1768 - unsigned long size = *sz_p;
1769 - char *buffer = *buf_p;
1770 - int patch_method;
1771 - unsigned long origlen;
1772 - char *data = NULL;
1773 - int hunk_size = 0;
1774 - struct fragment *frag;
1775 -
1776 - llen = linelen(buffer, size);
1777 - used = llen;
1778 -
1779 - *status_p = 0;
1780 -
1781 - if (starts_with(buffer, "delta ")) {
1782 - patch_method = BINARY_DELTA_DEFLATED;
1783 - origlen = strtoul(buffer + 6, NULL, 10);
1784 - }
1785 - else if (starts_with(buffer, "literal ")) {
1786 - patch_method = BINARY_LITERAL_DEFLATED;
1787 - origlen = strtoul(buffer + 8, NULL, 10);
1788 - }
1789 - else
1790 - return NULL;
1791 -
1792 - state->linenr++;
1793 - buffer += llen;
1794 - while (1) {
1795 - int byte_length, max_byte_length, newsize;
1796 - llen = linelen(buffer, size);
1797 - used += llen;
1798 - state->linenr++;
1799 - if (llen == 1) {
1800 - /* consume the blank line */
1801 - buffer++;
1802 - size--;
1803 - break;
1804 - }
1805 - /*
1806 - * Minimum line is "A00000\n" which is 7-byte long,
1807 - * and the line length must be multiple of 5 plus 2.
1808 - */
1809 - if ((llen < 7) || (llen-2) % 5)
1810 - goto corrupt;
1811 - max_byte_length = (llen - 2) / 5 * 4;
1812 - byte_length = *buffer;
1813 - if ('A' <= byte_length && byte_length <= 'Z')
1814 - byte_length = byte_length - 'A' + 1;
1815 - else if ('a' <= byte_length && byte_length <= 'z')
1816 - byte_length = byte_length - 'a' + 27;
1817 - else
1818 - goto corrupt;
1819 - /* if the input length was not multiple of 4, we would
1820 - * have filler at the end but the filler should never
1821 - * exceed 3 bytes
1822 - */
1823 - if (max_byte_length < byte_length ||
1824 - byte_length <= max_byte_length - 4)
1825 - goto corrupt;
1826 - newsize = hunk_size + byte_length;
1827 - data = xrealloc(data, newsize);
1828 - if (decode_85(data + hunk_size, buffer + 1, byte_length))
1829 - goto corrupt;
1830 - hunk_size = newsize;
1831 - buffer += llen;
1832 - size -= llen;
1833 - }
1834 -
1835 - frag = xcalloc(1, sizeof(*frag));
1836 - frag->patch = inflate_it(data, hunk_size, origlen);
1837 - frag->free_patch = 1;
1838 - if (!frag->patch)
1839 - goto corrupt;
1840 - free(data);
1841 - frag->size = origlen;
1842 - *buf_p = buffer;
1843 - *sz_p = size;
1844 - *used_p = used;
1845 - frag->binary_patch_method = patch_method;
1846 - return frag;
1847 -
1848 - corrupt:
1849 - free(data);
1850 - *status_p = -1;
1851 - error(_("corrupt binary patch at line %d: %.*s"),
1852 - state->linenr-1, llen-1, buffer);
1853 - return NULL;
1854 -}
1855 -
1856 -/*
1857 - * Returns:
1858 - * -1 in case of error,
1859 - * the length of the parsed binary patch otherwise
1860 - */
1861 -static int parse_binary(struct apply_state *state,
1862 - char *buffer,
1863 - unsigned long size,
1864 - struct patch *patch)
1865 -{
1866 - /*
1867 - * We have read "GIT binary patch\n"; what follows is a line
1868 - * that says the patch method (currently, either "literal" or
1869 - * "delta") and the length of data before deflating; a
1870 - * sequence of 'length-byte' followed by base-85 encoded data
1871 - * follows.
1872 - *
1873 - * When a binary patch is reversible, there is another binary
1874 - * hunk in the same format, starting with patch method (either
1875 - * "literal" or "delta") with the length of data, and a sequence
1876 - * of length-byte + base-85 encoded data, terminated with another
1877 - * empty line. This data, when applied to the postimage, produces
1878 - * the preimage.
1879 - */
1880 - struct fragment *forward;
1881 - struct fragment *reverse;
1882 - int status;
1883 - int used, used_1;
1884 -
1885 - forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
1886 - if (!forward && !status)
1887 - /* there has to be one hunk (forward hunk) */
1888 - return error(_("unrecognized binary patch at line %d"), state->linenr-1);
1889 - if (status)
1890 - /* otherwise we already gave an error message */
1891 - return status;
1892 -
1893 - reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
1894 - if (reverse)
1895 - used += used_1;
1896 - else if (status) {
1897 - /*
1898 - * Not having reverse hunk is not an error, but having
1899 - * a corrupt reverse hunk is.
1900 - */
1901 - free((void*) forward->patch);
1902 - free(forward);
1903 - return status;
1904 - }
1905 - forward->next = reverse;
1906 - patch->fragments = forward;
1907 - patch->is_binary = 1;
1908 - return used;
1909 -}
1910 -
1911 -static void prefix_one(struct apply_state *state, char **name)
1912 -{
1913 - char *old_name = *name;
1914 - if (!old_name)
1915 - return;
1916 - *name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
1917 - free(old_name);
1918 -}
1919 -
1920 -static void prefix_patch(struct apply_state *state, struct patch *p)
1921 -{
1922 - if (!state->prefix || p->is_toplevel_relative)
1923 - return;
1924 - prefix_one(state, &p->new_name);
1925 - prefix_one(state, &p->old_name);
1926 -}
1927 -
1928 -/*
1929 - * include/exclude
1930 - */
1931 -
1932 -static void add_name_limit(struct apply_state *state,
1933 - const char *name,
1934 - int exclude)
1935 -{
1936 - struct string_list_item *it;
1937 -
1938 - it = string_list_append(&state->limit_by_name, name);
1939 - it->util = exclude ? NULL : (void *) 1;
1940 -}
1941 -
1942 -static int use_patch(struct apply_state *state, struct patch *p)
1943 -{
1944 - const char *pathname = p->new_name ? p->new_name : p->old_name;
1945 - int i;
1946 -
1947 - /* Paths outside are not touched regardless of "--include" */
1948 - if (0 < state->prefix_length) {
1949 - int pathlen = strlen(pathname);
1950 - if (pathlen <= state->prefix_length ||
1951 - memcmp(state->prefix, pathname, state->prefix_length))
1952 - return 0;
1953 - }
1954 -
1955 - /* See if it matches any of exclude/include rule */
1956 - for (i = 0; i < state->limit_by_name.nr; i++) {
1957 - struct string_list_item *it = &state->limit_by_name.items[i];
1958 - if (!wildmatch(it->string, pathname, 0, NULL))
1959 - return (it->util != NULL);
1960 - }
1961 -
1962 - /*
1963 - * If we had any include, a path that does not match any rule is
1964 - * not used. Otherwise, we saw bunch of exclude rules (or none)
1965 - * and such a path is used.
1966 - */
1967 - return !state->has_include;
1968 -}
1969 -
1970 -/*
1971 - * Read the patch text in "buffer" that extends for "size" bytes; stop
1972 - * reading after seeing a single patch (i.e. changes to a single file).
1973 - * Create fragments (i.e. patch hunks) and hang them to the given patch.
1974 - *
1975 - * Returns:
1976 - * -1 if no header was found or parse_binary() failed,
1977 - * -128 on another error,
1978 - * the number of bytes consumed otherwise,
1979 - * so that the caller can call us again for the next patch.
1980 - */
1981 -static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
1982 -{
1983 - int hdrsize, patchsize;
1984 - int offset = find_header(state, buffer, size, &hdrsize, patch);
1985 -
1986 - if (offset < 0)
1987 - return offset;
1988 -
1989 - prefix_patch(state, patch);
1990 -
1991 - if (!use_patch(state, patch))
1992 - patch->ws_rule = 0;
1993 - else
1994 - patch->ws_rule = whitespace_rule(patch->new_name
1995 - ? patch->new_name
1996 - : patch->old_name);
1997 -
1998 - patchsize = parse_single_patch(state,
1999 - buffer + offset + hdrsize,
2000 - size - offset - hdrsize,
2001 - patch);
2002 -
2003 - if (patchsize < 0)
2004 - return -128;
2005 -
2006 - if (!patchsize) {
2007 - static const char git_binary[] = "GIT binary patch\n";
2008 - int hd = hdrsize + offset;
2009 - unsigned long llen = linelen(buffer + hd, size - hd);
2010 -
2011 - if (llen == sizeof(git_binary) - 1 &&
2012 - !memcmp(git_binary, buffer + hd, llen)) {
2013 - int used;
2014 - state->linenr++;
2015 - used = parse_binary(state, buffer + hd + llen,
2016 - size - hd - llen, patch);
2017 - if (used < 0)
2018 - return -1;
2019 - if (used)
2020 - patchsize = used + llen;
2021 - else
2022 - patchsize = 0;
2023 - }
2024 - else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
2025 - static const char *binhdr[] = {
2026 - "Binary files ",
2027 - "Files ",
2028 - NULL,
2029 - };
2030 - int i;
2031 - for (i = 0; binhdr[i]; i++) {
2032 - int len = strlen(binhdr[i]);
2033 - if (len < size - hd &&
2034 - !memcmp(binhdr[i], buffer + hd, len)) {
2035 - state->linenr++;
2036 - patch->is_binary = 1;
2037 - patchsize = llen;
2038 - break;
2039 - }
2040 - }
2041 - }
2042 -
2043 - /* Empty patch cannot be applied if it is a text patch
2044 - * without metadata change. A binary patch appears
2045 - * empty to us here.
2046 - */
2047 - if ((state->apply || state->check) &&
2048 - (!patch->is_binary && !metadata_changes(patch))) {
2049 - error(_("patch with only garbage at line %d"), state->linenr);
2050 - return -128;
2051 - }
2052 - }
2053 -
2054 - return offset + hdrsize + patchsize;
2055 -}
2056 -
2057 -#define swap(a,b) myswap((a),(b),sizeof(a))
2058 -
2059 -#define myswap(a, b, size) do { \
2060 - unsigned char mytmp[size]; \
2061 - memcpy(mytmp, &a, size); \
2062 - memcpy(&a, &b, size); \
2063 - memcpy(&b, mytmp, size); \
2064 -} while (0)
2065 -
2066 -static void reverse_patches(struct patch *p)
2067 -{
2068 - for (; p; p = p->next) {
2069 - struct fragment *frag = p->fragments;
2070 -
2071 - swap(p->new_name, p->old_name);
2072 - swap(p->new_mode, p->old_mode);
2073 - swap(p->is_new, p->is_delete);
2074 - swap(p->lines_added, p->lines_deleted);
2075 - swap(p->old_sha1_prefix, p->new_sha1_prefix);
2076 -
2077 - for (; frag; frag = frag->next) {
2078 - swap(frag->newpos, frag->oldpos);
2079 - swap(frag->newlines, frag->oldlines);
2080 - }
2081 - }
2082 -}
2083 -
2084 -static const char pluses[] =
2085 -"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2086 -static const char minuses[]=
2087 -"----------------------------------------------------------------------";
2088 -
2089 -static void show_stats(struct apply_state *state, struct patch *patch)
2090 -{
2091 - struct strbuf qname = STRBUF_INIT;
2092 - char *cp = patch->new_name ? patch->new_name : patch->old_name;
2093 - int max, add, del;
2094 -
2095 - quote_c_style(cp, &qname, NULL, 0);
2096 -
2097 - /*
2098 - * "scale" the filename
2099 - */
2100 - max = state->max_len;
2101 - if (max > 50)
2102 - max = 50;
2103 -
2104 - if (qname.len > max) {
2105 - cp = strchr(qname.buf + qname.len + 3 - max, '/');
2106 - if (!cp)
2107 - cp = qname.buf + qname.len + 3 - max;
2108 - strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
2109 - }
2110 -
2111 - if (patch->is_binary) {
2112 - printf(" %-*s | Bin\n", max, qname.buf);
2113 - strbuf_release(&qname);
2114 - return;
2115 - }
2116 -
2117 - printf(" %-*s |", max, qname.buf);
2118 - strbuf_release(&qname);
2119 -
2120 - /*
2121 - * scale the add/delete
2122 - */
2123 - max = max + state->max_change > 70 ? 70 - max : state->max_change;
2124 - add = patch->lines_added;
2125 - del = patch->lines_deleted;
2126 -
2127 - if (state->max_change > 0) {
2128 - int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2129 - add = (add * max + state->max_change / 2) / state->max_change;
2130 - del = total - add;
2131 - }
2132 - printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
2133 - add, pluses, del, minuses);
2134 -}
2135 -
2136 -static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
2137 -{
2138 - switch (st->st_mode & S_IFMT) {
2139 - case S_IFLNK:
2140 - if (strbuf_readlink(buf, path, st->st_size) < 0)
2141 - return error(_("unable to read symlink %s"), path);
2142 - return 0;
2143 - case S_IFREG:
2144 - if (strbuf_read_file(buf, path, st->st_size) != st->st_size)
2145 - return error(_("unable to open or read %s"), path);
2146 - convert_to_git(path, buf->buf, buf->len, buf, 0);
2147 - return 0;
2148 - default:
2149 - return -1;
2150 - }
2151 -}
2152 -
2153 -/*
2154 - * Update the preimage, and the common lines in postimage,
2155 - * from buffer buf of length len. If postlen is 0 the postimage
2156 - * is updated in place, otherwise it's updated on a new buffer
2157 - * of length postlen
2158 - */
2159 -
2160 -static void update_pre_post_images(struct image *preimage,
2161 - struct image *postimage,
2162 - char *buf,
2163 - size_t len, size_t postlen)
2164 -{
2165 - int i, ctx, reduced;
2166 - char *new, *old, *fixed;
2167 - struct image fixed_preimage;
2168 -
2169 - /*
2170 - * Update the preimage with whitespace fixes. Note that we
2171 - * are not losing preimage->buf -- apply_one_fragment() will
2172 - * free "oldlines".
2173 - */
2174 - prepare_image(&fixed_preimage, buf, len, 1);
2175 - assert(postlen
2176 - ? fixed_preimage.nr == preimage->nr
2177 - : fixed_preimage.nr <= preimage->nr);
2178 - for (i = 0; i < fixed_preimage.nr; i++)
2179 - fixed_preimage.line[i].flag = preimage->line[i].flag;
2180 - free(preimage->line_allocated);
2181 - *preimage = fixed_preimage;
2182 -
2183 - /*
2184 - * Adjust the common context lines in postimage. This can be
2185 - * done in-place when we are shrinking it with whitespace
2186 - * fixing, but needs a new buffer when ignoring whitespace or
2187 - * expanding leading tabs to spaces.
2188 - *
2189 - * We trust the caller to tell us if the update can be done
2190 - * in place (postlen==0) or not.
2191 - */
2192 - old = postimage->buf;
2193 - if (postlen)
2194 - new = postimage->buf = xmalloc(postlen);
2195 - else
2196 - new = old;
2197 - fixed = preimage->buf;
2198 -
2199 - for (i = reduced = ctx = 0; i < postimage->nr; i++) {
2200 - size_t l_len = postimage->line[i].len;
2201 - if (!(postimage->line[i].flag & LINE_COMMON)) {
2202 - /* an added line -- no counterparts in preimage */
2203 - memmove(new, old, l_len);
2204 - old += l_len;
2205 - new += l_len;
2206 - continue;
2207 - }
2208 -
2209 - /* a common context -- skip it in the original postimage */
2210 - old += l_len;
2211 -
2212 - /* and find the corresponding one in the fixed preimage */
2213 - while (ctx < preimage->nr &&
2214 - !(preimage->line[ctx].flag & LINE_COMMON)) {
2215 - fixed += preimage->line[ctx].len;
2216 - ctx++;
2217 - }
2218 -
2219 - /*
2220 - * preimage is expected to run out, if the caller
2221 - * fixed addition of trailing blank lines.
2222 - */
2223 - if (preimage->nr <= ctx) {
2224 - reduced++;
2225 - continue;
2226 - }
2227 -
2228 - /* and copy it in, while fixing the line length */
2229 - l_len = preimage->line[ctx].len;
2230 - memcpy(new, fixed, l_len);
2231 - new += l_len;
2232 - fixed += l_len;
2233 - postimage->line[i].len = l_len;
2234 - ctx++;
2235 - }
2236 -
2237 - if (postlen
2238 - ? postlen < new - postimage->buf
2239 - : postimage->len < new - postimage->buf)
2240 - die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2241 - (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
2242 -
2243 - /* Fix the length of the whole thing */
2244 - postimage->len = new - postimage->buf;
2245 - postimage->nr -= reduced;
2246 -}
2247 -
2248 -static int line_by_line_fuzzy_match(struct image *img,
2249 - struct image *preimage,
2250 - struct image *postimage,
2251 - unsigned long try,
2252 - int try_lno,
2253 - int preimage_limit)
2254 -{
2255 - int i;
2256 - size_t imgoff = 0;
2257 - size_t preoff = 0;
2258 - size_t postlen = postimage->len;
2259 - size_t extra_chars;
2260 - char *buf;
2261 - char *preimage_eof;
2262 - char *preimage_end;
2263 - struct strbuf fixed;
2264 - char *fixed_buf;
2265 - size_t fixed_len;
2266 -
2267 - for (i = 0; i < preimage_limit; i++) {
2268 - size_t prelen = preimage->line[i].len;
2269 - size_t imglen = img->line[try_lno+i].len;
2270 -
2271 - if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
2272 - preimage->buf + preoff, prelen))
2273 - return 0;
2274 - if (preimage->line[i].flag & LINE_COMMON)
2275 - postlen += imglen - prelen;
2276 - imgoff += imglen;
2277 - preoff += prelen;
2278 - }
2279 -
2280 - /*
2281 - * Ok, the preimage matches with whitespace fuzz.
2282 - *
2283 - * imgoff now holds the true length of the target that
2284 - * matches the preimage before the end of the file.
2285 - *
2286 - * Count the number of characters in the preimage that fall
2287 - * beyond the end of the file and make sure that all of them
2288 - * are whitespace characters. (This can only happen if
2289 - * we are removing blank lines at the end of the file.)
2290 - */
2291 - buf = preimage_eof = preimage->buf + preoff;
2292 - for ( ; i < preimage->nr; i++)
2293 - preoff += preimage->line[i].len;
2294 - preimage_end = preimage->buf + preoff;
2295 - for ( ; buf < preimage_end; buf++)
2296 - if (!isspace(*buf))
2297 - return 0;
2298 -
2299 - /*
2300 - * Update the preimage and the common postimage context
2301 - * lines to use the same whitespace as the target.
2302 - * If whitespace is missing in the target (i.e.
2303 - * if the preimage extends beyond the end of the file),
2304 - * use the whitespace from the preimage.
2305 - */
2306 - extra_chars = preimage_end - preimage_eof;
2307 - strbuf_init(&fixed, imgoff + extra_chars);
2308 - strbuf_add(&fixed, img->buf + try, imgoff);
2309 - strbuf_add(&fixed, preimage_eof, extra_chars);
2310 - fixed_buf = strbuf_detach(&fixed, &fixed_len);
2311 - update_pre_post_images(preimage, postimage,
2312 - fixed_buf, fixed_len, postlen);
2313 - return 1;
2314 -}
2315 -
2316 -static int match_fragment(struct apply_state *state,
2317 - struct image *img,
2318 - struct image *preimage,
2319 - struct image *postimage,
2320 - unsigned long try,
2321 - int try_lno,
2322 - unsigned ws_rule,
2323 - int match_beginning, int match_end)
2324 -{
2325 - int i;
2326 - char *fixed_buf, *buf, *orig, *target;
2327 - struct strbuf fixed;
2328 - size_t fixed_len, postlen;
2329 - int preimage_limit;
2330 -
2331 - if (preimage->nr + try_lno <= img->nr) {
2332 - /*
2333 - * The hunk falls within the boundaries of img.
2334 - */
2335 - preimage_limit = preimage->nr;
2336 - if (match_end && (preimage->nr + try_lno != img->nr))
2337 - return 0;
2338 - } else if (state->ws_error_action == correct_ws_error &&
2339 - (ws_rule & WS_BLANK_AT_EOF)) {
2340 - /*
2341 - * This hunk extends beyond the end of img, and we are
2342 - * removing blank lines at the end of the file. This
2343 - * many lines from the beginning of the preimage must
2344 - * match with img, and the remainder of the preimage
2345 - * must be blank.
2346 - */
2347 - preimage_limit = img->nr - try_lno;
2348 - } else {
2349 - /*
2350 - * The hunk extends beyond the end of the img and
2351 - * we are not removing blanks at the end, so we
2352 - * should reject the hunk at this position.
2353 - */
2354 - return 0;
2355 - }
2356 -
2357 - if (match_beginning && try_lno)
2358 - return 0;
2359 -
2360 - /* Quick hash check */
2361 - for (i = 0; i < preimage_limit; i++)
2362 - if ((img->line[try_lno + i].flag & LINE_PATCHED) ||
2363 - (preimage->line[i].hash != img->line[try_lno + i].hash))
2364 - return 0;
2365 -
2366 - if (preimage_limit == preimage->nr) {
2367 - /*
2368 - * Do we have an exact match? If we were told to match
2369 - * at the end, size must be exactly at try+fragsize,
2370 - * otherwise try+fragsize must be still within the preimage,
2371 - * and either case, the old piece should match the preimage
2372 - * exactly.
2373 - */
2374 - if ((match_end
2375 - ? (try + preimage->len == img->len)
2376 - : (try + preimage->len <= img->len)) &&
2377 - !memcmp(img->buf + try, preimage->buf, preimage->len))
2378 - return 1;
2379 - } else {
2380 - /*
2381 - * The preimage extends beyond the end of img, so
2382 - * there cannot be an exact match.
2383 - *
2384 - * There must be one non-blank context line that match
2385 - * a line before the end of img.
2386 - */
2387 - char *buf_end;
2388 -
2389 - buf = preimage->buf;
2390 - buf_end = buf;
2391 - for (i = 0; i < preimage_limit; i++)
2392 - buf_end += preimage->line[i].len;
2393 -
2394 - for ( ; buf < buf_end; buf++)
2395 - if (!isspace(*buf))
2396 - break;
2397 - if (buf == buf_end)
2398 - return 0;
2399 - }
2400 -
2401 - /*
2402 - * No exact match. If we are ignoring whitespace, run a line-by-line
2403 - * fuzzy matching. We collect all the line length information because
2404 - * we need it to adjust whitespace if we match.
2405 - */
2406 - if (state->ws_ignore_action == ignore_ws_change)
2407 - return line_by_line_fuzzy_match(img, preimage, postimage,
2408 - try, try_lno, preimage_limit);
2409 -
2410 - if (state->ws_error_action != correct_ws_error)
2411 - return 0;
2412 -
2413 - /*
2414 - * The hunk does not apply byte-by-byte, but the hash says
2415 - * it might with whitespace fuzz. We weren't asked to
2416 - * ignore whitespace, we were asked to correct whitespace
2417 - * errors, so let's try matching after whitespace correction.
2418 - *
2419 - * While checking the preimage against the target, whitespace
2420 - * errors in both fixed, we count how large the corresponding
2421 - * postimage needs to be. The postimage prepared by
2422 - * apply_one_fragment() has whitespace errors fixed on added
2423 - * lines already, but the common lines were propagated as-is,
2424 - * which may become longer when their whitespace errors are
2425 - * fixed.
2426 - */
2427 -
2428 - /* First count added lines in postimage */
2429 - postlen = 0;
2430 - for (i = 0; i < postimage->nr; i++) {
2431 - if (!(postimage->line[i].flag & LINE_COMMON))
2432 - postlen += postimage->line[i].len;
2433 - }
2434 -
2435 - /*
2436 - * The preimage may extend beyond the end of the file,
2437 - * but in this loop we will only handle the part of the
2438 - * preimage that falls within the file.
2439 - */
2440 - strbuf_init(&fixed, preimage->len + 1);
2441 - orig = preimage->buf;
2442 - target = img->buf + try;
2443 - for (i = 0; i < preimage_limit; i++) {
2444 - size_t oldlen = preimage->line[i].len;
2445 - size_t tgtlen = img->line[try_lno + i].len;
2446 - size_t fixstart = fixed.len;
2447 - struct strbuf tgtfix;
2448 - int match;
2449 -
2450 - /* Try fixing the line in the preimage */
2451 - ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2452 -
2453 - /* Try fixing the line in the target */
2454 - strbuf_init(&tgtfix, tgtlen);
2455 - ws_fix_copy(&tgtfix, target, tgtlen, ws_rule, NULL);
2456 -
2457 - /*
2458 - * If they match, either the preimage was based on
2459 - * a version before our tree fixed whitespace breakage,
2460 - * or we are lacking a whitespace-fix patch the tree
2461 - * the preimage was based on already had (i.e. target
2462 - * has whitespace breakage, the preimage doesn't).
2463 - * In either case, we are fixing the whitespace breakages
2464 - * so we might as well take the fix together with their
2465 - * real change.
2466 - */
2467 - match = (tgtfix.len == fixed.len - fixstart &&
2468 - !memcmp(tgtfix.buf, fixed.buf + fixstart,
2469 - fixed.len - fixstart));
2470 -
2471 - /* Add the length if this is common with the postimage */
2472 - if (preimage->line[i].flag & LINE_COMMON)
2473 - postlen += tgtfix.len;
2474 -
2475 - strbuf_release(&tgtfix);
2476 - if (!match)
2477 - goto unmatch_exit;
2478 -
2479 - orig += oldlen;
2480 - target += tgtlen;
2481 - }
2482 -
2483 -
2484 - /*
2485 - * Now handle the lines in the preimage that falls beyond the
2486 - * end of the file (if any). They will only match if they are
2487 - * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2488 - * false).
2489 - */
2490 - for ( ; i < preimage->nr; i++) {
2491 - size_t fixstart = fixed.len; /* start of the fixed preimage */
2492 - size_t oldlen = preimage->line[i].len;
2493 - int j;
2494 -
2495 - /* Try fixing the line in the preimage */
2496 - ws_fix_copy(&fixed, orig, oldlen, ws_rule, NULL);
2497 -
2498 - for (j = fixstart; j < fixed.len; j++)
2499 - if (!isspace(fixed.buf[j]))
2500 - goto unmatch_exit;
2501 -
2502 - orig += oldlen;
2503 - }
2504 -
2505 - /*
2506 - * Yes, the preimage is based on an older version that still
2507 - * has whitespace breakages unfixed, and fixing them makes the
2508 - * hunk match. Update the context lines in the postimage.
2509 - */
2510 - fixed_buf = strbuf_detach(&fixed, &fixed_len);
2511 - if (postlen < postimage->len)
2512 - postlen = 0;
2513 - update_pre_post_images(preimage, postimage,
2514 - fixed_buf, fixed_len, postlen);
2515 - return 1;
2516 -
2517 - unmatch_exit:
2518 - strbuf_release(&fixed);
2519 - return 0;
2520 -}
2521 -
2522 -static int find_pos(struct apply_state *state,
2523 - struct image *img,
2524 - struct image *preimage,
2525 - struct image *postimage,
2526 - int line,
2527 - unsigned ws_rule,
2528 - int match_beginning, int match_end)
2529 -{
2530 - int i;
2531 - unsigned long backwards, forwards, try;
2532 - int backwards_lno, forwards_lno, try_lno;
2533 -
2534 - /*
2535 - * If match_beginning or match_end is specified, there is no
2536 - * point starting from a wrong line that will never match and
2537 - * wander around and wait for a match at the specified end.
2538 - */
2539 - if (match_beginning)
2540 - line = 0;
2541 - else if (match_end)
2542 - line = img->nr - preimage->nr;
2543 -
2544 - /*
2545 - * Because the comparison is unsigned, the following test
2546 - * will also take care of a negative line number that can
2547 - * result when match_end and preimage is larger than the target.
2548 - */
2549 - if ((size_t) line > img->nr)
2550 - line = img->nr;
2551 -
2552 - try = 0;
2553 - for (i = 0; i < line; i++)
2554 - try += img->line[i].len;
2555 -
2556 - /*
2557 - * There's probably some smart way to do this, but I'll leave
2558 - * that to the smart and beautiful people. I'm simple and stupid.
2559 - */
2560 - backwards = try;
2561 - backwards_lno = line;
2562 - forwards = try;
2563 - forwards_lno = line;
2564 - try_lno = line;
2565 -
2566 - for (i = 0; ; i++) {
2567 - if (match_fragment(state, img, preimage, postimage,
2568 - try, try_lno, ws_rule,
2569 - match_beginning, match_end))
2570 - return try_lno;
2571 -
2572 - again:
2573 - if (backwards_lno == 0 && forwards_lno == img->nr)
2574 - break;
2575 -
2576 - if (i & 1) {
2577 - if (backwards_lno == 0) {
2578 - i++;
2579 - goto again;
2580 - }
2581 - backwards_lno--;
2582 - backwards -= img->line[backwards_lno].len;
2583 - try = backwards;
2584 - try_lno = backwards_lno;
2585 - } else {
2586 - if (forwards_lno == img->nr) {
2587 - i++;
2588 - goto again;
2589 - }
2590 - forwards += img->line[forwards_lno].len;
2591 - forwards_lno++;
2592 - try = forwards;
2593 - try_lno = forwards_lno;
2594 - }
2595 -
2596 - }
2597 - return -1;
2598 -}
2599 -
2600 -static void remove_first_line(struct image *img)
2601 -{
2602 - img->buf += img->line[0].len;
2603 - img->len -= img->line[0].len;
2604 - img->line++;
2605 - img->nr--;
2606 -}
2607 -
2608 -static void remove_last_line(struct image *img)
2609 -{
2610 - img->len -= img->line[--img->nr].len;
2611 -}
2612 -
2613 -/*
2614 - * The change from "preimage" and "postimage" has been found to
2615 - * apply at applied_pos (counts in line numbers) in "img".
2616 - * Update "img" to remove "preimage" and replace it with "postimage".
2617 - */
2618 -static void update_image(struct apply_state *state,
2619 - struct image *img,
2620 - int applied_pos,
2621 - struct image *preimage,
2622 - struct image *postimage)
2623 -{
2624 - /*
2625 - * remove the copy of preimage at offset in img
2626 - * and replace it with postimage
2627 - */
2628 - int i, nr;
2629 - size_t remove_count, insert_count, applied_at = 0;
2630 - char *result;
2631 - int preimage_limit;
2632 -
2633 - /*
2634 - * If we are removing blank lines at the end of img,
2635 - * the preimage may extend beyond the end.
2636 - * If that is the case, we must be careful only to
2637 - * remove the part of the preimage that falls within
2638 - * the boundaries of img. Initialize preimage_limit
2639 - * to the number of lines in the preimage that falls
2640 - * within the boundaries.
2641 - */
2642 - preimage_limit = preimage->nr;
2643 - if (preimage_limit > img->nr - applied_pos)
2644 - preimage_limit = img->nr - applied_pos;
2645 -
2646 - for (i = 0; i < applied_pos; i++)
2647 - applied_at += img->line[i].len;
2648 -
2649 - remove_count = 0;
2650 - for (i = 0; i < preimage_limit; i++)
2651 - remove_count += img->line[applied_pos + i].len;
2652 - insert_count = postimage->len;
2653 -
2654 - /* Adjust the contents */
2655 - result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
2656 - memcpy(result, img->buf, applied_at);
2657 - memcpy(result + applied_at, postimage->buf, postimage->len);
2658 - memcpy(result + applied_at + postimage->len,
2659 - img->buf + (applied_at + remove_count),
2660 - img->len - (applied_at + remove_count));
2661 - free(img->buf);
2662 - img->buf = result;
2663 - img->len += insert_count - remove_count;
2664 - result[img->len] = '\0';
2665 -
2666 - /* Adjust the line table */
2667 - nr = img->nr + postimage->nr - preimage_limit;
2668 - if (preimage_limit < postimage->nr) {
2669 - /*
2670 - * NOTE: this knows that we never call remove_first_line()
2671 - * on anything other than pre/post image.
2672 - */
2673 - REALLOC_ARRAY(img->line, nr);
2674 - img->line_allocated = img->line;
2675 - }
2676 - if (preimage_limit != postimage->nr)
2677 - memmove(img->line + applied_pos + postimage->nr,
2678 - img->line + applied_pos + preimage_limit,
2679 - (img->nr - (applied_pos + preimage_limit)) *
2680 - sizeof(*img->line));
2681 - memcpy(img->line + applied_pos,
2682 - postimage->line,
2683 - postimage->nr * sizeof(*img->line));
2684 - if (!state->allow_overlap)
2685 - for (i = 0; i < postimage->nr; i++)
2686 - img->line[applied_pos + i].flag |= LINE_PATCHED;
2687 - img->nr = nr;
2688 -}
2689 -
2690 -/*
2691 - * Use the patch-hunk text in "frag" to prepare two images (preimage and
2692 - * postimage) for the hunk. Find lines that match "preimage" in "img" and
2693 - * replace the part of "img" with "postimage" text.
2694 - */
2695 -static int apply_one_fragment(struct apply_state *state,
2696 - struct image *img, struct fragment *frag,
2697 - int inaccurate_eof, unsigned ws_rule,
2698 - int nth_fragment)
2699 -{
2700 - int match_beginning, match_end;
2701 - const char *patch = frag->patch;
2702 - int size = frag->size;
2703 - char *old, *oldlines;
2704 - struct strbuf newlines;
2705 - int new_blank_lines_at_end = 0;
2706 - int found_new_blank_lines_at_end = 0;
2707 - int hunk_linenr = frag->linenr;
2708 - unsigned long leading, trailing;
2709 - int pos, applied_pos;
2710 - struct image preimage;
2711 - struct image postimage;
2712 -
2713 - memset(&preimage, 0, sizeof(preimage));
2714 - memset(&postimage, 0, sizeof(postimage));
2715 - oldlines = xmalloc(size);
2716 - strbuf_init(&newlines, size);
2717 -
2718 - old = oldlines;
2719 - while (size > 0) {
2720 - char first;
2721 - int len = linelen(patch, size);
2722 - int plen;
2723 - int added_blank_line = 0;
2724 - int is_blank_context = 0;
2725 - size_t start;
2726 -
2727 - if (!len)
2728 - break;
2729 -
2730 - /*
2731 - * "plen" is how much of the line we should use for
2732 - * the actual patch data. Normally we just remove the
2733 - * first character on the line, but if the line is
2734 - * followed by "\ No newline", then we also remove the
2735 - * last one (which is the newline, of course).
2736 - */
2737 - plen = len - 1;
2738 - if (len < size && patch[len] == '\\')
2739 - plen--;
2740 - first = *patch;
2741 - if (state->apply_in_reverse) {
2742 - if (first == '-')
2743 - first = '+';
2744 - else if (first == '+')
2745 - first = '-';
2746 - }
2747 -
2748 - switch (first) {
2749 - case '\n':
2750 - /* Newer GNU diff, empty context line */
2751 - if (plen < 0)
2752 - /* ... followed by '\No newline'; nothing */
2753 - break;
2754 - *old++ = '\n';
2755 - strbuf_addch(&newlines, '\n');
2756 - add_line_info(&preimage, "\n", 1, LINE_COMMON);
2757 - add_line_info(&postimage, "\n", 1, LINE_COMMON);
2758 - is_blank_context = 1;
2759 - break;
2760 - case ' ':
2761 - if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2762 - ws_blank_line(patch + 1, plen, ws_rule))
2763 - is_blank_context = 1;
2764 - case '-':
2765 - memcpy(old, patch + 1, plen);
2766 - add_line_info(&preimage, old, plen,
2767 - (first == ' ' ? LINE_COMMON : 0));
2768 - old += plen;
2769 - if (first == '-')
2770 - break;
2771 - /* Fall-through for ' ' */
2772 - case '+':
2773 - /* --no-add does not add new lines */
2774 - if (first == '+' && state->no_add)
2775 - break;
2776 -
2777 - start = newlines.len;
2778 - if (first != '+' ||
2779 - !state->whitespace_error ||
2780 - state->ws_error_action != correct_ws_error) {
2781 - strbuf_add(&newlines, patch + 1, plen);
2782 - }
2783 - else {
2784 - ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
2785 - }
2786 - add_line_info(&postimage, newlines.buf + start, newlines.len - start,
2787 - (first == '+' ? 0 : LINE_COMMON));
2788 - if (first == '+' &&
2789 - (ws_rule & WS_BLANK_AT_EOF) &&
2790 - ws_blank_line(patch + 1, plen, ws_rule))
2791 - added_blank_line = 1;
2792 - break;
2793 - case '@': case '\\':
2794 - /* Ignore it, we already handled it */
2795 - break;
2796 - default:
2797 - if (state->apply_verbosely)
2798 - error(_("invalid start of line: '%c'"), first);
2799 - applied_pos = -1;
2800 - goto out;
2801 - }
2802 - if (added_blank_line) {
2803 - if (!new_blank_lines_at_end)
2804 - found_new_blank_lines_at_end = hunk_linenr;
2805 - new_blank_lines_at_end++;
2806 - }
2807 - else if (is_blank_context)
2808 - ;
2809 - else
2810 - new_blank_lines_at_end = 0;
2811 - patch += len;
2812 - size -= len;
2813 - hunk_linenr++;
2814 - }
2815 - if (inaccurate_eof &&
2816 - old > oldlines && old[-1] == '\n' &&
2817 - newlines.len > 0 && newlines.buf[newlines.len - 1] == '\n') {
2818 - old--;
2819 - strbuf_setlen(&newlines, newlines.len - 1);
2820 - }
2821 -
2822 - leading = frag->leading;
2823 - trailing = frag->trailing;
2824 -
2825 - /*
2826 - * A hunk to change lines at the beginning would begin with
2827 - * @@ -1,L +N,M @@
2828 - * but we need to be careful. -U0 that inserts before the second
2829 - * line also has this pattern.
2830 - *
2831 - * And a hunk to add to an empty file would begin with
2832 - * @@ -0,0 +N,M @@
2833 - *
2834 - * In other words, a hunk that is (frag->oldpos <= 1) with or
2835 - * without leading context must match at the beginning.
2836 - */
2837 - match_beginning = (!frag->oldpos ||
2838 - (frag->oldpos == 1 && !state->unidiff_zero));
2839 -
2840 - /*
2841 - * A hunk without trailing lines must match at the end.
2842 - * However, we simply cannot tell if a hunk must match end
2843 - * from the lack of trailing lines if the patch was generated
2844 - * with unidiff without any context.
2845 - */
2846 - match_end = !state->unidiff_zero && !trailing;
2847 -
2848 - pos = frag->newpos ? (frag->newpos - 1) : 0;
2849 - preimage.buf = oldlines;
2850 - preimage.len = old - oldlines;
2851 - postimage.buf = newlines.buf;
2852 - postimage.len = newlines.len;
2853 - preimage.line = preimage.line_allocated;
2854 - postimage.line = postimage.line_allocated;
2855 -
2856 - for (;;) {
2857 -
2858 - applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2859 - ws_rule, match_beginning, match_end);
2860 -
2861 - if (applied_pos >= 0)
2862 - break;
2863 -
2864 - /* Am I at my context limits? */
2865 - if ((leading <= state->p_context) && (trailing <= state->p_context))
2866 - break;
2867 - if (match_beginning || match_end) {
2868 - match_beginning = match_end = 0;
2869 - continue;
2870 - }
2871 -
2872 - /*
2873 - * Reduce the number of context lines; reduce both
2874 - * leading and trailing if they are equal otherwise
2875 - * just reduce the larger context.
2876 - */
2877 - if (leading >= trailing) {
2878 - remove_first_line(&preimage);
2879 - remove_first_line(&postimage);
2880 - pos--;
2881 - leading--;
2882 - }
2883 - if (trailing > leading) {
2884 - remove_last_line(&preimage);
2885 - remove_last_line(&postimage);
2886 - trailing--;
2887 - }
2888 - }
2889 -
2890 - if (applied_pos >= 0) {
2891 - if (new_blank_lines_at_end &&
2892 - preimage.nr + applied_pos >= img->nr &&
2893 - (ws_rule & WS_BLANK_AT_EOF) &&
2894 - state->ws_error_action != nowarn_ws_error) {
2895 - record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2896 - found_new_blank_lines_at_end);
2897 - if (state->ws_error_action == correct_ws_error) {
2898 - while (new_blank_lines_at_end--)
2899 - remove_last_line(&postimage);
2900 - }
2901 - /*
2902 - * We would want to prevent write_out_results()
2903 - * from taking place in apply_patch() that follows
2904 - * the callchain led us here, which is:
2905 - * apply_patch->check_patch_list->check_patch->
2906 - * apply_data->apply_fragments->apply_one_fragment
2907 - */
2908 - if (state->ws_error_action == die_on_ws_error)
2909 - state->apply = 0;
2910 - }
2911 -
2912 - if (state->apply_verbosely && applied_pos != pos) {
2913 - int offset = applied_pos - pos;
2914 - if (state->apply_in_reverse)
2915 - offset = 0 - offset;
2916 - fprintf_ln(stderr,
2917 - Q_("Hunk #%d succeeded at %d (offset %d line).",
2918 - "Hunk #%d succeeded at %d (offset %d lines).",
2919 - offset),
2920 - nth_fragment, applied_pos + 1, offset);
2921 - }
2922 -
2923 - /*
2924 - * Warn if it was necessary to reduce the number
2925 - * of context lines.
2926 - */
2927 - if ((leading != frag->leading) ||
2928 - (trailing != frag->trailing))
2929 - fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
2930 - " to apply fragment at %d"),
2931 - leading, trailing, applied_pos+1);
2932 - update_image(state, img, applied_pos, &preimage, &postimage);
2933 - } else {
2934 - if (state->apply_verbosely)
2935 - error(_("while searching for:\n%.*s"),
2936 - (int)(old - oldlines), oldlines);
2937 - }
2938 -
2939 -out:
2940 - free(oldlines);
2941 - strbuf_release(&newlines);
2942 - free(preimage.line_allocated);
2943 - free(postimage.line_allocated);
2944 -
2945 - return (applied_pos < 0);
2946 -}
2947 -
2948 -static int apply_binary_fragment(struct apply_state *state,
2949 - struct image *img,
2950 - struct patch *patch)
2951 -{
2952 - struct fragment *fragment = patch->fragments;
2953 - unsigned long len;
2954 - void *dst;
2955 -
2956 - if (!fragment)
2957 - return error(_("missing binary patch data for '%s'"),
2958 - patch->new_name ?
2959 - patch->new_name :
2960 - patch->old_name);
2961 -
2962 - /* Binary patch is irreversible without the optional second hunk */
2963 - if (state->apply_in_reverse) {
2964 - if (!fragment->next)
2965 - return error("cannot reverse-apply a binary patch "
2966 - "without the reverse hunk to '%s'",
2967 - patch->new_name
2968 - ? patch->new_name : patch->old_name);
2969 - fragment = fragment->next;
2970 - }
2971 - switch (fragment->binary_patch_method) {
2972 - case BINARY_DELTA_DEFLATED:
2973 - dst = patch_delta(img->buf, img->len, fragment->patch,
2974 - fragment->size, &len);
2975 - if (!dst)
2976 - return -1;
2977 - clear_image(img);
2978 - img->buf = dst;
2979 - img->len = len;
2980 - return 0;
2981 - case BINARY_LITERAL_DEFLATED:
2982 - clear_image(img);
2983 - img->len = fragment->size;
2984 - img->buf = xmemdupz(fragment->patch, img->len);
2985 - return 0;
2986 - }
2987 - return -1;
2988 -}
2989 -
2990 -/*
2991 - * Replace "img" with the result of applying the binary patch.
2992 - * The binary patch data itself in patch->fragment is still kept
2993 - * but the preimage prepared by the caller in "img" is freed here
2994 - * or in the helper function apply_binary_fragment() this calls.
2995 - */
2996 -static int apply_binary(struct apply_state *state,
2997 - struct image *img,
2998 - struct patch *patch)
2999 -{
3000 - const char *name = patch->old_name ? patch->old_name : patch->new_name;
3001 - unsigned char sha1[20];
3002 -
3003 - /*
3004 - * For safety, we require patch index line to contain
3005 - * full 40-byte textual SHA1 for old and new, at least for now.
3006 - */
3007 - if (strlen(patch->old_sha1_prefix) != 40 ||
3008 - strlen(patch->new_sha1_prefix) != 40 ||
3009 - get_sha1_hex(patch->old_sha1_prefix, sha1) ||
3010 - get_sha1_hex(patch->new_sha1_prefix, sha1))
3011 - return error("cannot apply binary patch to '%s' "
3012 - "without full index line", name);
3013 -
3014 - if (patch->old_name) {
3015 - /*
3016 - * See if the old one matches what the patch
3017 - * applies to.
3018 - */
3019 - hash_sha1_file(img->buf, img->len, blob_type, sha1);
3020 - if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
3021 - return error("the patch applies to '%s' (%s), "
3022 - "which does not match the "
3023 - "current contents.",
3024 - name, sha1_to_hex(sha1));
3025 - }
3026 - else {
3027 - /* Otherwise, the old one must be empty. */
3028 - if (img->len)
3029 - return error("the patch applies to an empty "
3030 - "'%s' but it is not empty", name);
3031 - }
3032 -
3033 - get_sha1_hex(patch->new_sha1_prefix, sha1);
3034 - if (is_null_sha1(sha1)) {
3035 - clear_image(img);
3036 - return 0; /* deletion patch */
3037 - }
3038 -
3039 - if (has_sha1_file(sha1)) {
3040 - /* We already have the postimage */
3041 - enum object_type type;
3042 - unsigned long size;
3043 - char *result;
3044 -
3045 - result = read_sha1_file(sha1, &type, &size);
3046 - if (!result)
3047 - return error("the necessary postimage %s for "
3048 - "'%s' cannot be read",
3049 - patch->new_sha1_prefix, name);
3050 - clear_image(img);
3051 - img->buf = result;
3052 - img->len = size;
3053 - } else {
3054 - /*
3055 - * We have verified buf matches the preimage;
3056 - * apply the patch data to it, which is stored
3057 - * in the patch->fragments->{patch,size}.
3058 - */
3059 - if (apply_binary_fragment(state, img, patch))
3060 - return error(_("binary patch does not apply to '%s'"),
3061 - name);
3062 -
3063 - /* verify that the result matches */
3064 - hash_sha1_file(img->buf, img->len, blob_type, sha1);
3065 - if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
3066 - return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3067 - name, patch->new_sha1_prefix, sha1_to_hex(sha1));
3068 - }
3069 -
3070 - return 0;
3071 -}
3072 -
3073 -static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3074 -{
3075 - struct fragment *frag = patch->fragments;
3076 - const char *name = patch->old_name ? patch->old_name : patch->new_name;
3077 - unsigned ws_rule = patch->ws_rule;
3078 - unsigned inaccurate_eof = patch->inaccurate_eof;
3079 - int nth = 0;
3080 -
3081 - if (patch->is_binary)
3082 - return apply_binary(state, img, patch);
3083 -
3084 - while (frag) {
3085 - nth++;
3086 - if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3087 - error(_("patch failed: %s:%ld"), name, frag->oldpos);
3088 - if (!state->apply_with_reject)
3089 - return -1;
3090 - frag->rejected = 1;
3091 - }
3092 - frag = frag->next;
3093 - }
3094 - return 0;
3095 -}
3096 -
3097 -static int read_blob_object(struct strbuf *buf, const unsigned char *sha1, unsigned mode)
3098 -{
3099 - if (S_ISGITLINK(mode)) {
3100 - strbuf_grow(buf, 100);
3101 - strbuf_addf(buf, "Subproject commit %s\n", sha1_to_hex(sha1));
3102 - } else {
3103 - enum object_type type;
3104 - unsigned long sz;
3105 - char *result;
3106 -
3107 - result = read_sha1_file(sha1, &type, &sz);
3108 - if (!result)
3109 - return -1;
3110 - /* XXX read_sha1_file NUL-terminates */
3111 - strbuf_attach(buf, result, sz, sz + 1);
3112 - }
3113 - return 0;
3114 -}
3115 -
3116 -static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf)
3117 -{
3118 - if (!ce)
3119 - return 0;
3120 - return read_blob_object(buf, ce->sha1, ce->ce_mode);
3121 -}
3122 -
3123 -static struct patch *in_fn_table(struct apply_state *state, const char *name)
3124 -{
3125 - struct string_list_item *item;
3126 -
3127 - if (name == NULL)
3128 - return NULL;
3129 -
3130 - item = string_list_lookup(&state->fn_table, name);
3131 - if (item != NULL)
3132 - return (struct patch *)item->util;
3133 -
3134 - return NULL;
3135 -}
3136 -
3137 -/*
3138 - * item->util in the filename table records the status of the path.
3139 - * Usually it points at a patch (whose result records the contents
3140 - * of it after applying it), but it could be PATH_WAS_DELETED for a
3141 - * path that a previously applied patch has already removed, or
3142 - * PATH_TO_BE_DELETED for a path that a later patch would remove.
3143 - *
3144 - * The latter is needed to deal with a case where two paths A and B
3145 - * are swapped by first renaming A to B and then renaming B to A;
3146 - * moving A to B should not be prevented due to presence of B as we
3147 - * will remove it in a later patch.
3148 - */
3149 -#define PATH_TO_BE_DELETED ((struct patch *) -2)
3150 -#define PATH_WAS_DELETED ((struct patch *) -1)
3151 -
3152 -static int to_be_deleted(struct patch *patch)
3153 -{
3154 - return patch == PATH_TO_BE_DELETED;
3155 -}
3156 -
3157 -static int was_deleted(struct patch *patch)
3158 -{
3159 - return patch == PATH_WAS_DELETED;
3160 -}
3161 -
3162 -static void add_to_fn_table(struct apply_state *state, struct patch *patch)
3163 -{
3164 - struct string_list_item *item;
3165 -
3166 - /*
3167 - * Always add new_name unless patch is a deletion
3168 - * This should cover the cases for normal diffs,
3169 - * file creations and copies
3170 - */
3171 - if (patch->new_name != NULL) {
3172 - item = string_list_insert(&state->fn_table, patch->new_name);
3173 - item->util = patch;
3174 - }
3175 -
3176 - /*
3177 - * store a failure on rename/deletion cases because
3178 - * later chunks shouldn't patch old names
3179 - */
3180 - if ((patch->new_name == NULL) || (patch->is_rename)) {
3181 - item = string_list_insert(&state->fn_table, patch->old_name);
3182 - item->util = PATH_WAS_DELETED;
3183 - }
3184 -}
3185 -
3186 -static void prepare_fn_table(struct apply_state *state, struct patch *patch)
3187 -{
3188 - /*
3189 - * store information about incoming file deletion
3190 - */
3191 - while (patch) {
3192 - if ((patch->new_name == NULL) || (patch->is_rename)) {
3193 - struct string_list_item *item;
3194 - item = string_list_insert(&state->fn_table, patch->old_name);
3195 - item->util = PATH_TO_BE_DELETED;
3196 - }
3197 - patch = patch->next;
3198 - }
3199 -}
3200 -
3201 -static int checkout_target(struct index_state *istate,
3202 - struct cache_entry *ce, struct stat *st)
3203 -{
3204 - struct checkout costate;
3205 -
3206 - memset(&costate, 0, sizeof(costate));
3207 - costate.base_dir = "";
3208 - costate.refresh_cache = 1;
3209 - costate.istate = istate;
3210 - if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3211 - return error(_("cannot checkout %s"), ce->name);
3212 - return 0;
3213 -}
3214 -
3215 -static struct patch *previous_patch(struct apply_state *state,
3216 - struct patch *patch,
3217 - int *gone)
3218 -{
3219 - struct patch *previous;
3220 -
3221 - *gone = 0;
3222 - if (patch->is_copy || patch->is_rename)
3223 - return NULL; /* "git" patches do not depend on the order */
3224 -
3225 - previous = in_fn_table(state, patch->old_name);
3226 - if (!previous)
3227 - return NULL;
3228 -
3229 - if (to_be_deleted(previous))
3230 - return NULL; /* the deletion hasn't happened yet */
3231 -
3232 - if (was_deleted(previous))
3233 - *gone = 1;
3234 -
3235 - return previous;
3236 -}
3237 -
3238 -static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3239 -{
3240 - if (S_ISGITLINK(ce->ce_mode)) {
3241 - if (!S_ISDIR(st->st_mode))
3242 - return -1;
3243 - return 0;
3244 - }
3245 - return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3246 -}
3247 -
3248 -#define SUBMODULE_PATCH_WITHOUT_INDEX 1
3249 -
3250 -static int load_patch_target(struct apply_state *state,
3251 - struct strbuf *buf,
3252 - const struct cache_entry *ce,
3253 - struct stat *st,
3254 - const char *name,
3255 - unsigned expected_mode)
3256 -{
3257 - if (state->cached || state->check_index) {
3258 - if (read_file_or_gitlink(ce, buf))
3259 - return error(_("failed to read %s"), name);
3260 - } else if (name) {
3261 - if (S_ISGITLINK(expected_mode)) {
3262 - if (ce)
3263 - return read_file_or_gitlink(ce, buf);
3264 - else
3265 - return SUBMODULE_PATCH_WITHOUT_INDEX;
3266 - } else if (has_symlink_leading_path(name, strlen(name))) {
3267 - return error(_("reading from '%s' beyond a symbolic link"), name);
3268 - } else {
3269 - if (read_old_data(st, name, buf))
3270 - return error(_("failed to read %s"), name);
3271 - }
3272 - }
3273 - return 0;
3274 -}
3275 -
3276 -/*
3277 - * We are about to apply "patch"; populate the "image" with the
3278 - * current version we have, from the working tree or from the index,
3279 - * depending on the situation e.g. --cached/--index. If we are
3280 - * applying a non-git patch that incrementally updates the tree,
3281 - * we read from the result of a previous diff.
3282 - */
3283 -static int load_preimage(struct apply_state *state,
3284 - struct image *image,
3285 - struct patch *patch, struct stat *st,
3286 - const struct cache_entry *ce)
3287 -{
3288 - struct strbuf buf = STRBUF_INIT;
3289 - size_t len;
3290 - char *img;
3291 - struct patch *previous;
3292 - int status;
3293 -
3294 - previous = previous_patch(state, patch, &status);
3295 - if (status)
3296 - return error(_("path %s has been renamed/deleted"),
3297 - patch->old_name);
3298 - if (previous) {
3299 - /* We have a patched copy in memory; use that. */
3300 - strbuf_add(&buf, previous->result, previous->resultsize);
3301 - } else {
3302 - status = load_patch_target(state, &buf, ce, st,
3303 - patch->old_name, patch->old_mode);
3304 - if (status < 0)
3305 - return status;
3306 - else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3307 - /*
3308 - * There is no way to apply subproject
3309 - * patch without looking at the index.
3310 - * NEEDSWORK: shouldn't this be flagged
3311 - * as an error???
3312 - */
3313 - free_fragment_list(patch->fragments);
3314 - patch->fragments = NULL;
3315 - } else if (status) {
3316 - return error(_("failed to read %s"), patch->old_name);
3317 - }
3318 - }
3319 -
3320 - img = strbuf_detach(&buf, &len);
3321 - prepare_image(image, img, len, !patch->is_binary);
3322 - return 0;
3323 -}
3324 -
3325 -static int three_way_merge(struct image *image,
3326 - char *path,
3327 - const unsigned char *base,
3328 - const unsigned char *ours,
3329 - const unsigned char *theirs)
3330 -{
3331 - mmfile_t base_file, our_file, their_file;
3332 - mmbuffer_t result = { NULL };
3333 - int status;
3334 -
3335 - read_mmblob(&base_file, base);
3336 - read_mmblob(&our_file, ours);
3337 - read_mmblob(&their_file, theirs);
3338 - status = ll_merge(&result, path,
3339 - &base_file, "base",
3340 - &our_file, "ours",
3341 - &their_file, "theirs", NULL);
3342 - free(base_file.ptr);
3343 - free(our_file.ptr);
3344 - free(their_file.ptr);
3345 - if (status < 0 || !result.ptr) {
3346 - free(result.ptr);
3347 - return -1;
3348 - }
3349 - clear_image(image);
3350 - image->buf = result.ptr;
3351 - image->len = result.size;
3352 -
3353 - return status;
3354 -}
3355 -
3356 -/*
3357 - * When directly falling back to add/add three-way merge, we read from
3358 - * the current contents of the new_name. In no cases other than that
3359 - * this function will be called.
3360 - */
3361 -static int load_current(struct apply_state *state,
3362 - struct image *image,
3363 - struct patch *patch)
3364 -{
3365 - struct strbuf buf = STRBUF_INIT;
3366 - int status, pos;
3367 - size_t len;
3368 - char *img;
3369 - struct stat st;
3370 - struct cache_entry *ce;
3371 - char *name = patch->new_name;
3372 - unsigned mode = patch->new_mode;
3373 -
3374 - if (!patch->is_new)
3375 - die("BUG: patch to %s is not a creation", patch->old_name);
3376 -
3377 - pos = cache_name_pos(name, strlen(name));
3378 - if (pos < 0)
3379 - return error(_("%s: does not exist in index"), name);
3380 - ce = active_cache[pos];
3381 - if (lstat(name, &st)) {
3382 - if (errno != ENOENT)
3383 - return error(_("%s: %s"), name, strerror(errno));
3384 - if (checkout_target(&the_index, ce, &st))
3385 - return -1;
3386 - }
3387 - if (verify_index_match(ce, &st))
3388 - return error(_("%s: does not match index"), name);
3389 -
3390 - status = load_patch_target(state, &buf, ce, &st, name, mode);
3391 - if (status < 0)
3392 - return status;
3393 - else if (status)
3394 - return -1;
3395 - img = strbuf_detach(&buf, &len);
3396 - prepare_image(image, img, len, !patch->is_binary);
3397 - return 0;
3398 -}
3399 -
3400 -static int try_threeway(struct apply_state *state,
3401 - struct image *image,
3402 - struct patch *patch,
3403 - struct stat *st,
3404 - const struct cache_entry *ce)
3405 -{
3406 - unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3407 - struct strbuf buf = STRBUF_INIT;
3408 - size_t len;
3409 - int status;
3410 - char *img;
3411 - struct image tmp_image;
3412 -
3413 - /* No point falling back to 3-way merge in these cases */
3414 - if (patch->is_delete ||
3415 - S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3416 - return -1;
3417 -
3418 - /* Preimage the patch was prepared for */
3419 - if (patch->is_new)
3420 - write_sha1_file("", 0, blob_type, pre_sha1);
3421 - else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3422 - read_blob_object(&buf, pre_sha1, patch->old_mode))
3423 - return error("repository lacks the necessary blob to fall back on 3-way merge.");
3424 -
3425 - fprintf(stderr, "Falling back to three-way merge...\n");
3426 -
3427 - img = strbuf_detach(&buf, &len);
3428 - prepare_image(&tmp_image, img, len, 1);
3429 - /* Apply the patch to get the post image */
3430 - if (apply_fragments(state, &tmp_image, patch) < 0) {
3431 - clear_image(&tmp_image);
3432 - return -1;
3433 - }
3434 - /* post_sha1[] is theirs */
3435 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, post_sha1);
3436 - clear_image(&tmp_image);
3437 -
3438 - /* our_sha1[] is ours */
3439 - if (patch->is_new) {
3440 - if (load_current(state, &tmp_image, patch))
3441 - return error("cannot read the current contents of '%s'",
3442 - patch->new_name);
3443 - } else {
3444 - if (load_preimage(state, &tmp_image, patch, st, ce))
3445 - return error("cannot read the current contents of '%s'",
3446 - patch->old_name);
3447 - }
3448 - write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3449 - clear_image(&tmp_image);
3450 -
3451 - /* in-core three-way merge between post and our using pre as base */
3452 - status = three_way_merge(image, patch->new_name,
3453 - pre_sha1, our_sha1, post_sha1);
3454 - if (status < 0) {
3455 - fprintf(stderr, "Failed to fall back on three-way merge...\n");
3456 - return status;
3457 - }
3458 -
3459 - if (status) {
3460 - patch->conflicted_threeway = 1;
3461 - if (patch->is_new)
3462 - oidclr(&patch->threeway_stage[0]);
3463 - else
3464 - hashcpy(patch->threeway_stage[0].hash, pre_sha1);
3465 - hashcpy(patch->threeway_stage[1].hash, our_sha1);
3466 - hashcpy(patch->threeway_stage[2].hash, post_sha1);
3467 - fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
3468 - } else {
3469 - fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
3470 - }
3471 - return 0;
3472 -}
3473 -
3474 -static int apply_data(struct apply_state *state, struct patch *patch,
3475 - struct stat *st, const struct cache_entry *ce)
3476 -{
3477 - struct image image;
3478 -
3479 - if (load_preimage(state, &image, patch, st, ce) < 0)
3480 - return -1;
3481 -
3482 - if (patch->direct_to_threeway ||
3483 - apply_fragments(state, &image, patch) < 0) {
3484 - /* Note: with --reject, apply_fragments() returns 0 */
3485 - if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
3486 - return -1;
3487 - }
3488 - patch->result = image.buf;
3489 - patch->resultsize = image.len;
3490 - add_to_fn_table(state, patch);
3491 - free(image.line_allocated);
3492 -
3493 - if (0 < patch->is_delete && patch->resultsize)
3494 - return error(_("removal patch leaves file contents"));
3495 -
3496 - return 0;
3497 -}
3498 -
3499 -/*
3500 - * If "patch" that we are looking at modifies or deletes what we have,
3501 - * we would want it not to lose any local modification we have, either
3502 - * in the working tree or in the index.
3503 - *
3504 - * This also decides if a non-git patch is a creation patch or a
3505 - * modification to an existing empty file. We do not check the state
3506 - * of the current tree for a creation patch in this function; the caller
3507 - * check_patch() separately makes sure (and errors out otherwise) that
3508 - * the path the patch creates does not exist in the current tree.
3509 - */
3510 -static int check_preimage(struct apply_state *state,
3511 - struct patch *patch,
3512 - struct cache_entry **ce,
3513 - struct stat *st)
3514 -{
3515 - const char *old_name = patch->old_name;
3516 - struct patch *previous = NULL;
3517 - int stat_ret = 0, status;
3518 - unsigned st_mode = 0;
3519 -
3520 - if (!old_name)
3521 - return 0;
3522 -
3523 - assert(patch->is_new <= 0);
3524 - previous = previous_patch(state, patch, &status);
3525 -
3526 - if (status)
3527 - return error(_("path %s has been renamed/deleted"), old_name);
3528 - if (previous) {
3529 - st_mode = previous->new_mode;
3530 - } else if (!state->cached) {
3531 - stat_ret = lstat(old_name, st);
3532 - if (stat_ret && errno != ENOENT)
3533 - return error(_("%s: %s"), old_name, strerror(errno));
3534 - }
3535 -
3536 - if (state->check_index && !previous) {
3537 - int pos = cache_name_pos(old_name, strlen(old_name));
3538 - if (pos < 0) {
3539 - if (patch->is_new < 0)
3540 - goto is_new;
3541 - return error(_("%s: does not exist in index"), old_name);
3542 - }
3543 - *ce = active_cache[pos];
3544 - if (stat_ret < 0) {
3545 - if (checkout_target(&the_index, *ce, st))
3546 - return -1;
3547 - }
3548 - if (!state->cached && verify_index_match(*ce, st))
3549 - return error(_("%s: does not match index"), old_name);
3550 - if (state->cached)
3551 - st_mode = (*ce)->ce_mode;
3552 - } else if (stat_ret < 0) {
3553 - if (patch->is_new < 0)
3554 - goto is_new;
3555 - return error(_("%s: %s"), old_name, strerror(errno));
3556 - }
3557 -
3558 - if (!state->cached && !previous)
3559 - st_mode = ce_mode_from_stat(*ce, st->st_mode);
3560 -
3561 - if (patch->is_new < 0)
3562 - patch->is_new = 0;
3563 - if (!patch->old_mode)
3564 - patch->old_mode = st_mode;
3565 - if ((st_mode ^ patch->old_mode) & S_IFMT)
3566 - return error(_("%s: wrong type"), old_name);
3567 - if (st_mode != patch->old_mode)
3568 - warning(_("%s has type %o, expected %o"),
3569 - old_name, st_mode, patch->old_mode);
3570 - if (!patch->new_mode && !patch->is_delete)
3571 - patch->new_mode = st_mode;
3572 - return 0;
3573 -
3574 - is_new:
3575 - patch->is_new = 1;
3576 - patch->is_delete = 0;
3577 - free(patch->old_name);
3578 - patch->old_name = NULL;
3579 - return 0;
3580 -}
3581 -
3582 -
3583 -#define EXISTS_IN_INDEX 1
3584 -#define EXISTS_IN_WORKTREE 2
3585 -
3586 -static int check_to_create(struct apply_state *state,
3587 - const char *new_name,
3588 - int ok_if_exists)
3589 -{
3590 - struct stat nst;
3591 -
3592 - if (state->check_index &&
3593 - cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3594 - !ok_if_exists)
3595 - return EXISTS_IN_INDEX;
3596 - if (state->cached)
3597 - return 0;
3598 -
3599 - if (!lstat(new_name, &nst)) {
3600 - if (S_ISDIR(nst.st_mode) || ok_if_exists)
3601 - return 0;
3602 - /*
3603 - * A leading component of new_name might be a symlink
3604 - * that is going to be removed with this patch, but
3605 - * still pointing at somewhere that has the path.
3606 - * In such a case, path "new_name" does not exist as
3607 - * far as git is concerned.
3608 - */
3609 - if (has_symlink_leading_path(new_name, strlen(new_name)))
3610 - return 0;
3611 -
3612 - return EXISTS_IN_WORKTREE;
3613 - } else if ((errno != ENOENT) && (errno != ENOTDIR)) {
3614 - return error("%s: %s", new_name, strerror(errno));
3615 - }
3616 - return 0;
3617 -}
3618 -
3619 -static uintptr_t register_symlink_changes(struct apply_state *state,
3620 - const char *path,
3621 - uintptr_t what)
3622 -{
3623 - struct string_list_item *ent;
3624 -
3625 - ent = string_list_lookup(&state->symlink_changes, path);
3626 - if (!ent) {
3627 - ent = string_list_insert(&state->symlink_changes, path);
3628 - ent->util = (void *)0;
3629 - }
3630 - ent->util = (void *)(what | ((uintptr_t)ent->util));
3631 - return (uintptr_t)ent->util;
3632 -}
3633 -
3634 -static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3635 -{
3636 - struct string_list_item *ent;
3637 -
3638 - ent = string_list_lookup(&state->symlink_changes, path);
3639 - if (!ent)
3640 - return 0;
3641 - return (uintptr_t)ent->util;
3642 -}
3643 -
3644 -static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3645 -{
3646 - for ( ; patch; patch = patch->next) {
3647 - if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3648 - (patch->is_rename || patch->is_delete))
3649 - /* the symlink at patch->old_name is removed */
3650 - register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3651 -
3652 - if (patch->new_name && S_ISLNK(patch->new_mode))
3653 - /* the symlink at patch->new_name is created or remains */
3654 - register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3655 - }
3656 -}
3657 -
3658 -static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3659 -{
3660 - do {
3661 - unsigned int change;
3662 -
3663 - while (--name->len && name->buf[name->len] != '/')
3664 - ; /* scan backwards */
3665 - if (!name->len)
3666 - break;
3667 - name->buf[name->len] = '\0';
3668 - change = check_symlink_changes(state, name->buf);
3669 - if (change & APPLY_SYMLINK_IN_RESULT)
3670 - return 1;
3671 - if (change & APPLY_SYMLINK_GOES_AWAY)
3672 - /*
3673 - * This cannot be "return 0", because we may
3674 - * see a new one created at a higher level.
3675 - */
3676 - continue;
3677 -
3678 - /* otherwise, check the preimage */
3679 - if (state->check_index) {
3680 - struct cache_entry *ce;
3681 -
3682 - ce = cache_file_exists(name->buf, name->len, ignore_case);
3683 - if (ce && S_ISLNK(ce->ce_mode))
3684 - return 1;
3685 - } else {
3686 - struct stat st;
3687 - if (!lstat(name->buf, &st) && S_ISLNK(st.st_mode))
3688 - return 1;
3689 - }
3690 - } while (1);
3691 - return 0;
3692 -}
3693 -
3694 -static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3695 -{
3696 - int ret;
3697 - struct strbuf name = STRBUF_INIT;
3698 -
3699 - assert(*name_ != '\0');
3700 - strbuf_addstr(&name, name_);
3701 - ret = path_is_beyond_symlink_1(state, &name);
3702 - strbuf_release(&name);
3703 -
3704 - return ret;
3705 -}
3706 -
3707 -static int check_unsafe_path(struct patch *patch)
3708 -{
3709 - const char *old_name = NULL;
3710 - const char *new_name = NULL;
3711 - if (patch->is_delete)
3712 - old_name = patch->old_name;
3713 - else if (!patch->is_new && !patch->is_copy)
3714 - old_name = patch->old_name;
3715 - if (!patch->is_delete)
3716 - new_name = patch->new_name;
3717 -
3718 - if (old_name && !verify_path(old_name))
3719 - return error(_("invalid path '%s'"), old_name);
3720 - if (new_name && !verify_path(new_name))
3721 - return error(_("invalid path '%s'"), new_name);
3722 - return 0;
3723 -}
3724 -
3725 -/*
3726 - * Check and apply the patch in-core; leave the result in patch->result
3727 - * for the caller to write it out to the final destination.
3728 - */
3729 -static int check_patch(struct apply_state *state, struct patch *patch)
3730 -{
3731 - struct stat st;
3732 - const char *old_name = patch->old_name;
3733 - const char *new_name = patch->new_name;
3734 - const char *name = old_name ? old_name : new_name;
3735 - struct cache_entry *ce = NULL;
3736 - struct patch *tpatch;
3737 - int ok_if_exists;
3738 - int status;
3739 -
3740 - patch->rejected = 1; /* we will drop this after we succeed */
3741 -
3742 - status = check_preimage(state, patch, &ce, &st);
3743 - if (status)
3744 - return status;
3745 - old_name = patch->old_name;
3746 -
3747 - /*
3748 - * A type-change diff is always split into a patch to delete
3749 - * old, immediately followed by a patch to create new (see
3750 - * diff.c::run_diff()); in such a case it is Ok that the entry
3751 - * to be deleted by the previous patch is still in the working
3752 - * tree and in the index.
3753 - *
3754 - * A patch to swap-rename between A and B would first rename A
3755 - * to B and then rename B to A. While applying the first one,
3756 - * the presence of B should not stop A from getting renamed to
3757 - * B; ask to_be_deleted() about the later rename. Removal of
3758 - * B and rename from A to B is handled the same way by asking
3759 - * was_deleted().
3760 - */
3761 - if ((tpatch = in_fn_table(state, new_name)) &&
3762 - (was_deleted(tpatch) || to_be_deleted(tpatch)))
3763 - ok_if_exists = 1;
3764 - else
3765 - ok_if_exists = 0;
3766 -
3767 - if (new_name &&
3768 - ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3769 - int err = check_to_create(state, new_name, ok_if_exists);
3770 -
3771 - if (err && state->threeway) {
3772 - patch->direct_to_threeway = 1;
3773 - } else switch (err) {
3774 - case 0:
3775 - break; /* happy */
3776 - case EXISTS_IN_INDEX:
3777 - return error(_("%s: already exists in index"), new_name);
3778 - break;
3779 - case EXISTS_IN_WORKTREE:
3780 - return error(_("%s: already exists in working directory"),
3781 - new_name);
3782 - default:
3783 - return err;
3784 - }
3785 -
3786 - if (!patch->new_mode) {
3787 - if (0 < patch->is_new)
3788 - patch->new_mode = S_IFREG | 0644;
3789 - else
3790 - patch->new_mode = patch->old_mode;
3791 - }
3792 - }
3793 -
3794 - if (new_name && old_name) {
3795 - int same = !strcmp(old_name, new_name);
3796 - if (!patch->new_mode)
3797 - patch->new_mode = patch->old_mode;
3798 - if ((patch->old_mode ^ patch->new_mode) & S_IFMT) {
3799 - if (same)
3800 - return error(_("new mode (%o) of %s does not "
3801 - "match old mode (%o)"),
3802 - patch->new_mode, new_name,
3803 - patch->old_mode);
3804 - else
3805 - return error(_("new mode (%o) of %s does not "
3806 - "match old mode (%o) of %s"),
3807 - patch->new_mode, new_name,
3808 - patch->old_mode, old_name);
3809 - }
3810 - }
3811 -
3812 - if (!state->unsafe_paths && check_unsafe_path(patch))
3813 - return -128;
3814 -
3815 - /*
3816 - * An attempt to read from or delete a path that is beyond a
3817 - * symbolic link will be prevented by load_patch_target() that
3818 - * is called at the beginning of apply_data() so we do not
3819 - * have to worry about a patch marked with "is_delete" bit
3820 - * here. We however need to make sure that the patch result
3821 - * is not deposited to a path that is beyond a symbolic link
3822 - * here.
3823 - */
3824 - if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3825 - return error(_("affected file '%s' is beyond a symbolic link"),
3826 - patch->new_name);
3827 -
3828 - if (apply_data(state, patch, &st, ce) < 0)
3829 - return error(_("%s: patch does not apply"), name);
3830 - patch->rejected = 0;
3831 - return 0;
3832 -}
3833 -
3834 -static int check_patch_list(struct apply_state *state, struct patch *patch)
3835 -{
3836 - int err = 0;
3837 -
3838 - prepare_symlink_changes(state, patch);
3839 - prepare_fn_table(state, patch);
3840 - while (patch) {
3841 - int res;
3842 - if (state->apply_verbosely)
3843 - say_patch_name(stderr,
3844 - _("Checking patch %s..."), patch);
3845 - res = check_patch(state, patch);
3846 - if (res == -128)
3847 - return -128;
3848 - err |= res;
3849 - patch = patch->next;
3850 - }
3851 - return err;
3852 -}
3853 -
3854 -/* This function tries to read the sha1 from the current index */
3855 -static int get_current_sha1(const char *path, unsigned char *sha1)
3856 -{
3857 - int pos;
3858 -
3859 - if (read_cache() < 0)
3860 - return -1;
3861 - pos = cache_name_pos(path, strlen(path));
3862 - if (pos < 0)
3863 - return -1;
3864 - hashcpy(sha1, active_cache[pos]->sha1);
3865 - return 0;
3866 -}
3867 -
3868 -static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20])
3869 -{
3870 - /*
3871 - * A usable gitlink patch has only one fragment (hunk) that looks like:
3872 - * @@ -1 +1 @@
3873 - * -Subproject commit <old sha1>
3874 - * +Subproject commit <new sha1>
3875 - * or
3876 - * @@ -1 +0,0 @@
3877 - * -Subproject commit <old sha1>
3878 - * for a removal patch.
3879 - */
3880 - struct fragment *hunk = p->fragments;
3881 - static const char heading[] = "-Subproject commit ";
3882 - char *preimage;
3883 -
3884 - if (/* does the patch have only one hunk? */
3885 - hunk && !hunk->next &&
3886 - /* is its preimage one line? */
3887 - hunk->oldpos == 1 && hunk->oldlines == 1 &&
3888 - /* does preimage begin with the heading? */
3889 - (preimage = memchr(hunk->patch, '\n', hunk->size)) != NULL &&
3890 - starts_with(++preimage, heading) &&
3891 - /* does it record full SHA-1? */
3892 - !get_sha1_hex(preimage + sizeof(heading) - 1, sha1) &&
3893 - preimage[sizeof(heading) + 40 - 1] == '\n' &&
3894 - /* does the abbreviated name on the index line agree with it? */
3895 - starts_with(preimage + sizeof(heading) - 1, p->old_sha1_prefix))
3896 - return 0; /* it all looks fine */
3897 -
3898 - /* we may have full object name on the index line */
3899 - return get_sha1_hex(p->old_sha1_prefix, sha1);
3900 -}
3901 -
3902 -/* Build an index that contains the just the files needed for a 3way merge */
3903 -static int build_fake_ancestor(struct patch *list, const char *filename)
3904 -{
3905 - struct patch *patch;
3906 - struct index_state result = { NULL };
3907 - static struct lock_file lock;
3908 - int res;
3909 -
3910 - /* Once we start supporting the reverse patch, it may be
3911 - * worth showing the new sha1 prefix, but until then...
3912 - */
3913 - for (patch = list; patch; patch = patch->next) {
3914 - unsigned char sha1[20];
3915 - struct cache_entry *ce;
3916 - const char *name;
3917 -
3918 - name = patch->old_name ? patch->old_name : patch->new_name;
3919 - if (0 < patch->is_new)
3920 - continue;
3921 -
3922 - if (S_ISGITLINK(patch->old_mode)) {
3923 - if (!preimage_sha1_in_gitlink_patch(patch, sha1))
3924 - ; /* ok, the textual part looks sane */
3925 - else
3926 - return error("sha1 information is lacking or "
3927 - "useless for submodule %s", name);
3928 - } else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
3929 - ; /* ok */
3930 - } else if (!patch->lines_added && !patch->lines_deleted) {
3931 - /* mode-only change: update the current */
3932 - if (get_current_sha1(patch->old_name, sha1))
3933 - return error("mode change for %s, which is not "
3934 - "in current HEAD", name);
3935 - } else
3936 - return error("sha1 information is lacking or useless "
3937 - "(%s).", name);
3938 -
3939 - ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
3940 - if (!ce)
3941 - return error(_("make_cache_entry failed for path '%s'"),
3942 - name);
3943 - if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD)) {
3944 - free(ce);
3945 - return error("Could not add %s to temporary index",
3946 - name);
3947 - }
3948 - }
3949 -
3950 - hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
3951 - res = write_locked_index(&result, &lock, COMMIT_LOCK);
3952 - discard_index(&result);
3953 -
3954 - if (res)
3955 - return error("Could not write temporary index to %s", filename);
3956 -
3957 - return 0;
3958 -}
3959 -
3960 -static void stat_patch_list(struct apply_state *state, struct patch *patch)
3961 -{
3962 - int files, adds, dels;
3963 -
3964 - for (files = adds = dels = 0 ; patch ; patch = patch->next) {
3965 - files++;
3966 - adds += patch->lines_added;
3967 - dels += patch->lines_deleted;
3968 - show_stats(state, patch);
3969 - }
3970 -
3971 - print_stat_summary(stdout, files, adds, dels);
3972 -}
3973 -
3974 -static void numstat_patch_list(struct apply_state *state,
3975 - struct patch *patch)
3976 -{
3977 - for ( ; patch; patch = patch->next) {
3978 - const char *name;
3979 - name = patch->new_name ? patch->new_name : patch->old_name;
3980 - if (patch->is_binary)
3981 - printf("-\t-\t");
3982 - else
3983 - printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
3984 - write_name_quoted(name, stdout, state->line_termination);
3985 - }
3986 -}
3987 -
3988 -static void show_file_mode_name(const char *newdelete, unsigned int mode, const char *name)
3989 -{
3990 - if (mode)
3991 - printf(" %s mode %06o %s\n", newdelete, mode, name);
3992 - else
3993 - printf(" %s %s\n", newdelete, name);
3994 -}
3995 -
3996 -static void show_mode_change(struct patch *p, int show_name)
3997 -{
3998 - if (p->old_mode && p->new_mode && p->old_mode != p->new_mode) {
3999 - if (show_name)
4000 - printf(" mode change %06o => %06o %s\n",
4001 - p->old_mode, p->new_mode, p->new_name);
4002 - else
4003 - printf(" mode change %06o => %06o\n",
4004 - p->old_mode, p->new_mode);
4005 - }
4006 -}
4007 -
4008 -static void show_rename_copy(struct patch *p)
4009 -{
4010 - const char *renamecopy = p->is_rename ? "rename" : "copy";
4011 - const char *old, *new;
4012 -
4013 - /* Find common prefix */
4014 - old = p->old_name;
4015 - new = p->new_name;
4016 - while (1) {
4017 - const char *slash_old, *slash_new;
4018 - slash_old = strchr(old, '/');
4019 - slash_new = strchr(new, '/');
4020 - if (!slash_old ||
4021 - !slash_new ||
4022 - slash_old - old != slash_new - new ||
4023 - memcmp(old, new, slash_new - new))
4024 - break;
4025 - old = slash_old + 1;
4026 - new = slash_new + 1;
4027 - }
4028 - /* p->old_name thru old is the common prefix, and old and new
4029 - * through the end of names are renames
4030 - */
4031 - if (old != p->old_name)
4032 - printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
4033 - (int)(old - p->old_name), p->old_name,
4034 - old, new, p->score);
4035 - else
4036 - printf(" %s %s => %s (%d%%)\n", renamecopy,
4037 - p->old_name, p->new_name, p->score);
4038 - show_mode_change(p, 0);
4039 -}
4040 -
4041 -static void summary_patch_list(struct patch *patch)
4042 -{
4043 - struct patch *p;
4044 -
4045 - for (p = patch; p; p = p->next) {
4046 - if (p->is_new)
4047 - show_file_mode_name("create", p->new_mode, p->new_name);
4048 - else if (p->is_delete)
4049 - show_file_mode_name("delete", p->old_mode, p->old_name);
4050 - else {
4051 - if (p->is_rename || p->is_copy)
4052 - show_rename_copy(p);
4053 - else {
4054 - if (p->score) {
4055 - printf(" rewrite %s (%d%%)\n",
4056 - p->new_name, p->score);
4057 - show_mode_change(p, 0);
4058 - }
4059 - else
4060 - show_mode_change(p, 1);
4061 - }
4062 - }
4063 - }
4064 -}
4065 -
4066 -static void patch_stats(struct apply_state *state, struct patch *patch)
4067 -{
4068 - int lines = patch->lines_added + patch->lines_deleted;
4069 -
4070 - if (lines > state->max_change)
4071 - state->max_change = lines;
4072 - if (patch->old_name) {
4073 - int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4074 - if (!len)
4075 - len = strlen(patch->old_name);
4076 - if (len > state->max_len)
4077 - state->max_len = len;
4078 - }
4079 - if (patch->new_name) {
4080 - int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4081 - if (!len)
4082 - len = strlen(patch->new_name);
4083 - if (len > state->max_len)
4084 - state->max_len = len;
4085 - }
4086 -}
4087 -
4088 -static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
4089 -{
4090 - if (state->update_index) {
4091 - if (remove_file_from_cache(patch->old_name) < 0)
4092 - return error(_("unable to remove %s from index"), patch->old_name);
4093 - }
4094 - if (!state->cached) {
4095 - if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4096 - remove_path(patch->old_name);
4097 - }
4098 - }
4099 - return 0;
4100 -}
4101 -
4102 -static int add_index_file(struct apply_state *state,
4103 - const char *path,
4104 - unsigned mode,
4105 - void *buf,
4106 - unsigned long size)
4107 -{
4108 - struct stat st;
4109 - struct cache_entry *ce;
4110 - int namelen = strlen(path);
4111 - unsigned ce_size = cache_entry_size(namelen);
4112 -
4113 - if (!state->update_index)
4114 - return 0;
4115 -
4116 - ce = xcalloc(1, ce_size);
4117 - memcpy(ce->name, path, namelen);
4118 - ce->ce_mode = create_ce_mode(mode);
4119 - ce->ce_flags = create_ce_flags(0);
4120 - ce->ce_namelen = namelen;
4121 - if (S_ISGITLINK(mode)) {
4122 - const char *s;
4123 -
4124 - if (!skip_prefix(buf, "Subproject commit ", &s) ||
4125 - get_sha1_hex(s, ce->sha1)) {
4126 - free(ce);
4127 - return error(_("corrupt patch for submodule %s"), path);
4128 - }
4129 - } else {
4130 - if (!state->cached) {
4131 - if (lstat(path, &st) < 0) {
4132 - free(ce);
4133 - return error(_("unable to stat newly "
4134 - "created file '%s': %s"),
4135 - path, strerror(errno));
4136 - }
4137 - fill_stat_cache_info(ce, &st);
4138 - }
4139 - if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
4140 - free(ce);
4141 - return error(_("unable to create backing store "
4142 - "for newly created file %s"), path);
4143 - }
4144 - }
4145 - if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4146 - free(ce);
4147 - return error(_("unable to add cache entry for %s"), path);
4148 - }
4149 -
4150 - return 0;
4151 -}
4152 -
4153 -/*
4154 - * Returns:
4155 - * -1 if an unrecoverable error happened
4156 - * 0 if everything went well
4157 - * 1 if a recoverable error happened
4158 - */
4159 -static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
4160 -{
4161 - int fd, res;
4162 - struct strbuf nbuf = STRBUF_INIT;
4163 -
4164 - if (S_ISGITLINK(mode)) {
4165 - struct stat st;
4166 - if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4167 - return 0;
4168 - return !!mkdir(path, 0777);
4169 - }
4170 -
4171 - if (has_symlinks && S_ISLNK(mode))
4172 - /* Although buf:size is counted string, it also is NUL
4173 - * terminated.
4174 - */
4175 - return !!symlink(buf, path);
4176 -
4177 - fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4178 - if (fd < 0)
4179 - return 1;
4180 -
4181 - if (convert_to_working_tree(path, buf, size, &nbuf)) {
4182 - size = nbuf.len;
4183 - buf = nbuf.buf;
4184 - }
4185 -
4186 - res = write_in_full(fd, buf, size) < 0;
4187 - if (res)
4188 - error_errno(_("failed to write to '%s'"), path);
4189 - strbuf_release(&nbuf);
4190 -
4191 - if (close(fd) < 0 && !res)
4192 - return error_errno(_("closing file '%s'"), path);
4193 -
4194 - return res ? -1 : 0;
4195 -}
4196 -
4197 -/*
4198 - * We optimistically assume that the directories exist,
4199 - * which is true 99% of the time anyway. If they don't,
4200 - * we create them and try again.
4201 - *
4202 - * Returns:
4203 - * -1 on error
4204 - * 0 otherwise
4205 - */
4206 -static int create_one_file(struct apply_state *state,
4207 - char *path,
4208 - unsigned mode,
4209 - const char *buf,
4210 - unsigned long size)
4211 -{
4212 - int res;
4213 -
4214 - if (state->cached)
4215 - return 0;
4216 -
4217 - res = try_create_file(path, mode, buf, size);
4218 - if (res < 0)
4219 - return -1;
4220 - if (!res)
4221 - return 0;
4222 -
4223 - if (errno == ENOENT) {
4224 - if (safe_create_leading_directories(path))
4225 - return 0;
4226 - res = try_create_file(path, mode, buf, size);
4227 - if (res < 0)
4228 - return -1;
4229 - if (!res)
4230 - return 0;
4231 - }
4232 -
4233 - if (errno == EEXIST || errno == EACCES) {
4234 - /* We may be trying to create a file where a directory
4235 - * used to be.
4236 - */
4237 - struct stat st;
4238 - if (!lstat(path, &st) && (!S_ISDIR(st.st_mode) || !rmdir(path)))
4239 - errno = EEXIST;
4240 - }
4241 -
4242 - if (errno == EEXIST) {
4243 - unsigned int nr = getpid();
4244 -
4245 - for (;;) {
4246 - char newpath[PATH_MAX];
4247 - mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
4248 - res = try_create_file(newpath, mode, buf, size);
4249 - if (res < 0)
4250 - return -1;
4251 - if (!res) {
4252 - if (!rename(newpath, path))
4253 - return 0;
4254 - unlink_or_warn(newpath);
4255 - break;
4256 - }
4257 - if (errno != EEXIST)
4258 - break;
4259 - ++nr;
4260 - }
4261 - }
4262 - return error_errno(_("unable to write file '%s' mode %o"),
4263 - path, mode);
4264 -}
4265 -
4266 -static int add_conflicted_stages_file(struct apply_state *state,
4267 - struct patch *patch)
4268 -{
4269 - int stage, namelen;
4270 - unsigned ce_size, mode;
4271 - struct cache_entry *ce;
4272 -
4273 - if (!state->update_index)
4274 - return 0;
4275 - namelen = strlen(patch->new_name);
4276 - ce_size = cache_entry_size(namelen);
4277 - mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
4278 -
4279 - remove_file_from_cache(patch->new_name);
4280 - for (stage = 1; stage < 4; stage++) {
4281 - if (is_null_oid(&patch->threeway_stage[stage - 1]))
4282 - continue;
4283 - ce = xcalloc(1, ce_size);
4284 - memcpy(ce->name, patch->new_name, namelen);
4285 - ce->ce_mode = create_ce_mode(mode);
4286 - ce->ce_flags = create_ce_flags(stage);
4287 - ce->ce_namelen = namelen;
4288 - hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
4289 - if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
4290 - free(ce);
4291 - return error(_("unable to add cache entry for %s"),
4292 - patch->new_name);
4293 - }
4294 - }
4295 -
4296 - return 0;
4297 -}
4298 -
4299 -static int create_file(struct apply_state *state, struct patch *patch)
4300 -{
4301 - char *path = patch->new_name;
4302 - unsigned mode = patch->new_mode;
4303 - unsigned long size = patch->resultsize;
4304 - char *buf = patch->result;
4305 -
4306 - if (!mode)
4307 - mode = S_IFREG | 0644;
4308 - if (create_one_file(state, path, mode, buf, size))
4309 - return -1;
4310 -
4311 - if (patch->conflicted_threeway)
4312 - return add_conflicted_stages_file(state, patch);
4313 - else
4314 - return add_index_file(state, path, mode, buf, size);
4315 -}
4316 -
4317 -/* phase zero is to remove, phase one is to create */
4318 -static int write_out_one_result(struct apply_state *state,
4319 - struct patch *patch,
4320 - int phase)
4321 -{
4322 - if (patch->is_delete > 0) {
4323 - if (phase == 0)
4324 - return remove_file(state, patch, 1);
4325 - return 0;
4326 - }
4327 - if (patch->is_new > 0 || patch->is_copy) {
4328 - if (phase == 1)
4329 - return create_file(state, patch);
4330 - return 0;
4331 - }
4332 - /*
4333 - * Rename or modification boils down to the same
4334 - * thing: remove the old, write the new
4335 - */
4336 - if (phase == 0)
4337 - return remove_file(state, patch, patch->is_rename);
4338 - if (phase == 1)
4339 - return create_file(state, patch);
4340 - return 0;
4341 -}
4342 -
4343 -static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4344 -{
4345 - FILE *rej;
4346 - char namebuf[PATH_MAX];
4347 - struct fragment *frag;
4348 - int cnt = 0;
4349 - struct strbuf sb = STRBUF_INIT;
4350 -
4351 - for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
4352 - if (!frag->rejected)
4353 - continue;
4354 - cnt++;
4355 - }
4356 -
4357 - if (!cnt) {
4358 - if (state->apply_verbosely)
4359 - say_patch_name(stderr,
4360 - _("Applied patch %s cleanly."), patch);
4361 - return 0;
4362 - }
4363 -
4364 - /* This should not happen, because a removal patch that leaves
4365 - * contents are marked "rejected" at the patch level.
4366 - */
4367 - if (!patch->new_name)
4368 - die(_("internal error"));
4369 -
4370 - /* Say this even without --verbose */
4371 - strbuf_addf(&sb, Q_("Applying patch %%s with %d reject...",
4372 - "Applying patch %%s with %d rejects...",
4373 - cnt),
4374 - cnt);
4375 - say_patch_name(stderr, sb.buf, patch);
4376 - strbuf_release(&sb);
4377 -
4378 - cnt = strlen(patch->new_name);
4379 - if (ARRAY_SIZE(namebuf) <= cnt + 5) {
4380 - cnt = ARRAY_SIZE(namebuf) - 5;
4381 - warning(_("truncating .rej filename to %.*s.rej"),
4382 - cnt - 1, patch->new_name);
4383 - }
4384 - memcpy(namebuf, patch->new_name, cnt);
4385 - memcpy(namebuf + cnt, ".rej", 5);
4386 -
4387 - rej = fopen(namebuf, "w");
4388 - if (!rej)
4389 - return error(_("cannot open %s: %s"), namebuf, strerror(errno));
4390 -
4391 - /* Normal git tools never deal with .rej, so do not pretend
4392 - * this is a git patch by saying --git or giving extended
4393 - * headers. While at it, maybe please "kompare" that wants
4394 - * the trailing TAB and some garbage at the end of line ;-).
4395 - */
4396 - fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
4397 - patch->new_name, patch->new_name);
4398 - for (cnt = 1, frag = patch->fragments;
4399 - frag;
4400 - cnt++, frag = frag->next) {
4401 - if (!frag->rejected) {
4402 - fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
4403 - continue;
4404 - }
4405 - fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
4406 - fprintf(rej, "%.*s", frag->size, frag->patch);
4407 - if (frag->patch[frag->size-1] != '\n')
4408 - fputc('\n', rej);
4409 - }
4410 - fclose(rej);
4411 - return -1;
4412 -}
4413 -
4414 -/*
4415 - * Returns:
4416 - * -1 if an error happened
4417 - * 0 if the patch applied cleanly
4418 - * 1 if the patch did not apply cleanly
4419 - */
4420 -static int write_out_results(struct apply_state *state, struct patch *list)
4421 -{
4422 - int phase;
4423 - int errs = 0;
4424 - struct patch *l;
4425 - struct string_list cpath = STRING_LIST_INIT_DUP;
4426 -
4427 - for (phase = 0; phase < 2; phase++) {
4428 - l = list;
4429 - while (l) {
4430 - if (l->rejected)
4431 - errs = 1;
4432 - else {
4433 - if (write_out_one_result(state, l, phase)) {
4434 - string_list_clear(&cpath, 0);
4435 - return -1;
4436 - }
4437 - if (phase == 1) {
4438 - if (write_out_one_reject(state, l))
4439 - errs = 1;
4440 - if (l->conflicted_threeway) {
4441 - string_list_append(&cpath, l->new_name);
4442 - errs = 1;
4443 - }
4444 - }
4445 - }
4446 - l = l->next;
4447 - }
4448 - }
4449 -
4450 - if (cpath.nr) {
4451 - struct string_list_item *item;
4452 -
4453 - string_list_sort(&cpath);
4454 - for_each_string_list_item(item, &cpath)
4455 - fprintf(stderr, "U %s\n", item->string);
4456 - string_list_clear(&cpath, 0);
4457 -
4458 - rerere(0);
4459 - }
4460 -
4461 - return errs;
4462 -}
4463 -
12 static struct lock_file lock_file;
13
4466 -/*
4467 - * Try to apply a patch.
4468 - *
4469 - * Returns:
4470 - * -128 if a bad error happened (like patch unreadable)
4471 - * -1 if patch did not apply and user cannot deal with it
4472 - * 0 if the patch applied
4473 - * 1 if the patch did not apply but user might fix it
4474 - */
4475 -static int apply_patch(struct apply_state *state,
4476 - int fd,
4477 - const char *filename,
4478 - int options)
4479 -{
4480 - size_t offset;
4481 - struct strbuf buf = STRBUF_INIT; /* owns the patch text */
4482 - struct patch *list = NULL, **listp = &list;
4483 - int skipped_patch = 0;
4484 - int res = 0;
4485 -
4486 - state->patch_input_file = filename;
4487 - if (read_patch_file(&buf, fd) < 0)
4488 - return -128;
4489 - offset = 0;
4490 - while (offset < buf.len) {
4491 - struct patch *patch;
4492 - int nr;
4493 -
4494 - patch = xcalloc(1, sizeof(*patch));
4495 - patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
4496 - patch->recount = !!(options & APPLY_OPT_RECOUNT);
4497 - nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4498 - if (nr < 0) {
4499 - free_patch(patch);
4500 - if (nr == -128) {
4501 - res = -128;
4502 - goto end;
4503 - }
4504 - break;
4505 - }
4506 - if (state->apply_in_reverse)
4507 - reverse_patches(patch);
4508 - if (use_patch(state, patch)) {
4509 - patch_stats(state, patch);
4510 - *listp = patch;
4511 - listp = &patch->next;
4512 - }
4513 - else {
4514 - if (state->apply_verbosely)
4515 - say_patch_name(stderr, _("Skipped patch '%s'."), patch);
4516 - free_patch(patch);
4517 - skipped_patch++;
4518 - }
4519 - offset += nr;
4520 - }
4521 -
4522 - if (!list && !skipped_patch) {
4523 - error(_("unrecognized input"));
4524 - res = -128;
4525 - goto end;
4526 - }
4527 -
4528 - if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4529 - state->apply = 0;
4530 -
4531 - state->update_index = state->check_index && state->apply;
4532 - if (state->update_index && state->newfd < 0)
4533 - state->newfd = hold_locked_index(state->lock_file, 1);
4534 -
4535 - if (state->check_index && read_cache() < 0) {
4536 - error(_("unable to read index file"));
4537 - res = -128;
4538 - goto end;
4539 - }
4540 -
4541 - if (state->check || state->apply) {
4542 - int r = check_patch_list(state, list);
4543 - if (r == -128) {
4544 - res = -128;
4545 - goto end;
4546 - }
4547 - if (r < 0 && !state->apply_with_reject) {
4548 - res = -1;
4549 - goto end;
4550 - }
4551 - }
4552 -
4553 - if (state->apply) {
4554 - int write_res = write_out_results(state, list);
4555 - if (write_res < 0) {
4556 - res = -128;
4557 - goto end;
4558 - }
4559 - if (write_res > 0) {
4560 - /* with --3way, we still need to write the index out */
4561 - res = state->apply_with_reject ? -1 : 1;
4562 - goto end;
4563 - }
4564 - }
4565 -
4566 - if (state->fake_ancestor &&
4567 - build_fake_ancestor(list, state->fake_ancestor)) {
4568 - res = -128;
4569 - goto end;
4570 - }
4571 -
4572 - if (state->diffstat)
4573 - stat_patch_list(state, list);
4574 -
4575 - if (state->numstat)
4576 - numstat_patch_list(state, list);
4577 -
4578 - if (state->summary)
4579 - summary_patch_list(list);
4580 -
4581 -end:
4582 - free_patch_list(list);
4583 - strbuf_release(&buf);
4584 - string_list_clear(&state->fn_table, 0);
4585 - return res;
4586 -}
4587 -
4588 -static int apply_option_parse_exclude(const struct option *opt,
4589 - const char *arg, int unset)
4590 -{
4591 - struct apply_state *state = opt->value;
4592 - add_name_limit(state, arg, 1);
4593 - return 0;
4594 -}
4595 -
4596 -static int apply_option_parse_include(const struct option *opt,
4597 - const char *arg, int unset)
4598 -{
4599 - struct apply_state *state = opt->value;
4600 - add_name_limit(state, arg, 0);
4601 - state->has_include = 1;
4602 - return 0;
4603 -}
4604 -
4605 -static int apply_option_parse_p(const struct option *opt,
4606 - const char *arg,
4607 - int unset)
4608 -{
4609 - struct apply_state *state = opt->value;
4610 - state->p_value = atoi(arg);
4611 - state->p_value_known = 1;
4612 - return 0;
4613 -}
4614 -
4615 -static int apply_option_parse_space_change(const struct option *opt,
4616 - const char *arg, int unset)
4617 -{
4618 - struct apply_state *state = opt->value;
4619 - if (unset)
4620 - state->ws_ignore_action = ignore_ws_none;
4621 - else
4622 - state->ws_ignore_action = ignore_ws_change;
4623 - return 0;
4624 -}
4625 -
4626 -static int apply_option_parse_whitespace(const struct option *opt,
4627 - const char *arg, int unset)
4628 -{
4629 - struct apply_state *state = opt->value;
4630 - state->whitespace_option = arg;
4631 - if (parse_whitespace_option(state, arg))
4632 - exit(1);
4633 - return 0;
4634 -}
4635 -
4636 -static int apply_option_parse_directory(const struct option *opt,
4637 - const char *arg, int unset)
4638 -{
4639 - struct apply_state *state = opt->value;
4640 - strbuf_reset(&state->root);
4641 - strbuf_addstr(&state->root, arg);
4642 - strbuf_complete(&state->root, '/');
4643 - return 0;
4644 -}
4645 -
4646 -static int apply_all_patches(struct apply_state *state,
4647 - int argc,
4648 - const char **argv,
4649 - int options)
4650 -{
4651 - int i;
4652 - int res;
4653 - int errs = 0;
4654 - int read_stdin = 1;
4655 -
4656 - for (i = 0; i < argc; i++) {
4657 - const char *arg = argv[i];
4658 - int fd;
4659 -
4660 - if (!strcmp(arg, "-")) {
4661 - res = apply_patch(state, 0, "<stdin>", options);
4662 - if (res < 0)
4663 - goto end;
4664 - errs |= res;
4665 - read_stdin = 0;
4666 - continue;
4667 - } else if (0 < state->prefix_length)
4668 - arg = prefix_filename(state->prefix,
4669 - state->prefix_length,
4670 - arg);
4671 -
4672 - fd = open(arg, O_RDONLY);
4673 - if (fd < 0) {
4674 - error(_("can't open patch '%s': %s"), arg, strerror(errno));
4675 - res = -128;
4676 - goto end;
4677 - }
4678 - read_stdin = 0;
4679 - set_default_whitespace_mode(state);
4680 - res = apply_patch(state, fd, arg, options);
4681 - close(fd);
4682 - if (res < 0)
4683 - goto end;
4684 - errs |= res;
4685 - }
4686 - set_default_whitespace_mode(state);
4687 - if (read_stdin) {
4688 - res = apply_patch(state, 0, "<stdin>", options);
4689 - if (res < 0)
4690 - goto end;
4691 - errs |= res;
4692 - }
4693 -
4694 - if (state->whitespace_error) {
4695 - if (state->squelch_whitespace_errors &&
4696 - state->squelch_whitespace_errors < state->whitespace_error) {
4697 - int squelched =
4698 - state->whitespace_error - state->squelch_whitespace_errors;
4699 - warning(Q_("squelched %d whitespace error",
4700 - "squelched %d whitespace errors",
4701 - squelched),
4702 - squelched);
4703 - }
4704 - if (state->ws_error_action == die_on_ws_error) {
4705 - error(Q_("%d line adds whitespace errors.",
4706 - "%d lines add whitespace errors.",
4707 - state->whitespace_error),
4708 - state->whitespace_error);
4709 - res = -128;
4710 - goto end;
4711 - }
4712 - if (state->applied_after_fixing_ws && state->apply)
4713 - warning("%d line%s applied after"
4714 - " fixing whitespace errors.",
4715 - state->applied_after_fixing_ws,
4716 - state->applied_after_fixing_ws == 1 ? "" : "s");
4717 - else if (state->whitespace_error)
4718 - warning(Q_("%d line adds whitespace errors.",
4719 - "%d lines add whitespace errors.",
4720 - state->whitespace_error),
4721 - state->whitespace_error);
4722 - }
4723 -
4724 - if (state->update_index) {
4725 - res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4726 - if (res) {
4727 - error(_("Unable to write new index file"));
4728 - res = -128;
4729 - goto end;
4730 - }
4731 - state->newfd = -1;
4732 - }
4733 -
4734 - return !!errs;
4735 -
4736 -end:
4737 - if (state->newfd >= 0) {
4738 - rollback_lock_file(state->lock_file);
4739 - state->newfd = -1;
4740 - }
4741 -
4742 - return (res == -1 ? 1 : 128);
4743 -}
4744 -
14 int cmd_apply(int argc, const char **argv, const char *prefix)
15 {
16 int force_apply = 0;