4 * Copyright (C) Linus Torvalds, 2005
6 * This applies patches on top of some (arbitrary) version of the SCM.
11 #include "cache-tree.h"
16 #include "string-list.h"
19 #include "parse-options.h"
20 #include "xdiff-interface.h"
25 static const char * const apply_usage
[] = {
26 N_("git apply [<options>] [<patch>...]"),
30 static void set_default_whitespace_mode(struct apply_state
*state
)
32 if (!state
->whitespace_option
&& !apply_default_whitespace
)
33 state
->ws_error_action
= (state
->apply
? warn_on_ws_error
: nowarn_ws_error
);
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
44 unsigned long leading
, trailing
;
45 unsigned long oldpos
, oldlines
;
46 unsigned long newpos
, newlines
;
48 * 'patch' is usually borrowed from buf in apply_patch(),
49 * but some codepaths store an allocated buffer.
52 unsigned free_patch
:1,
56 struct fragment
*next
;
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".
64 #define binary_patch_method leading
65 #define BINARY_DELTA_DEFLATED 1
66 #define BINARY_LITERAL_DEFLATED 2
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.
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 */
79 int lines_added
, lines_deleted
;
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
;
92 char old_sha1_prefix
[41];
93 char new_sha1_prefix
[41];
96 /* three-way fallback result */
97 struct object_id threeway_stage
[3];
100 static void free_fragment_list(struct fragment
*list
)
103 struct fragment
*next
= list
->next
;
104 if (list
->free_patch
)
105 free((char *)list
->patch
);
111 static void free_patch(struct patch
*patch
)
113 free_fragment_list(patch
->fragments
);
114 free(patch
->def_name
);
115 free(patch
->old_name
);
116 free(patch
->new_name
);
121 static void free_patch_list(struct patch
*list
)
124 struct patch
*next
= list
->next
;
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'.
139 #define LINE_COMMON 1
140 #define LINE_PATCHED 2
144 * This represents a "file", which is an array of "lines".
151 struct line
*line_allocated
;
155 static uint32_t hash_line(const char *cp
, size_t len
)
159 for (i
= 0, h
= 0; i
< len
; i
++) {
160 if (!isspace(cp
[i
])) {
161 h
= h
* 3 + (cp
[i
] & 0xff);
168 * Compare lines s1 of length n1 and s2 of length n2, ignoring
169 * whitespace difference. Returns 1 if they match, 0 otherwise
171 static int fuzzy_matchlines(const char *s1
, size_t n1
,
172 const char *s2
, size_t n2
)
174 const char *last1
= s1
+ n1
- 1;
175 const char *last2
= s2
+ n2
- 1;
178 /* ignore line endings */
179 while ((*last1
== '\r') || (*last1
== '\n'))
181 while ((*last2
== '\r') || (*last2
== '\n'))
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
))
188 while (isspace(*s2
) && (s2
<= last2
))
191 /* early return if both lines are empty */
192 if ((s1
> last1
) && (s2
> last2
))
195 result
= *s1
++ - *s2
++;
197 * Skip whitespace inside. We check for whitespace on
198 * both buffers because we don't want "a b" to match
201 if (isspace(*s1
) && isspace(*s2
)) {
202 while (isspace(*s1
) && s1
<= last1
)
204 while (isspace(*s2
) && s2
<= last2
)
208 * If we reached the end on one side only,
212 ((s2
> last2
) && (s1
<= last1
)) ||
213 ((s1
> last1
) && (s2
<= last2
)))
215 if ((s1
> last1
) && (s2
> last2
))
222 static void add_line_info(struct image
*img
, const char *bol
, size_t len
, unsigned flag
)
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
;
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".
236 static void prepare_image(struct image
*image
, char *buf
, size_t len
,
237 int prepare_linetable
)
241 memset(image
, 0, sizeof(*image
));
245 if (!prepare_linetable
)
248 ep
= image
->buf
+ image
->len
;
252 for (next
= cp
; next
< ep
&& *next
!= '\n'; next
++)
256 add_line_info(image
, cp
, next
- cp
, 0);
259 image
->line
= image
->line_allocated
;
262 static void clear_image(struct image
*image
)
265 free(image
->line_allocated
);
266 memset(image
, 0, sizeof(*image
));
269 /* fmt must contain _one_ %s and no other substitution */
270 static void say_patch_name(FILE *output
, const char *fmt
, struct patch
*patch
)
272 struct strbuf sb
= STRBUF_INIT
;
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);
280 const char *n
= patch
->new_name
;
283 quote_c_style(n
, &sb
, NULL
, 0);
285 fprintf(output
, fmt
, sb
.buf
);
292 static int read_patch_file(struct strbuf
*sb
, int fd
)
294 if (strbuf_read(sb
, fd
, 0) < 0)
295 return error_errno("git apply: failed to read");
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.
302 strbuf_grow(sb
, SLOP
);
303 memset(sb
->buf
+ sb
->len
, 0, SLOP
);
307 static unsigned long linelen(const char *buffer
, unsigned long size
)
309 unsigned long len
= 0;
312 if (*buffer
++ == '\n')
318 static int is_dev_null(const char *str
)
320 return skip_prefix(str
, "/dev/null", &str
) && isspace(*str
);
326 static int name_terminate(int c
, int terminate
)
328 if (c
== ' ' && !(terminate
& TERM_SPACE
))
330 if (c
== '\t' && !(terminate
& TERM_TAB
))
336 /* remove double slashes to make --index work with such filenames */
337 static char *squash_slash(char *name
)
345 if ((name
[j
++] = name
[i
++]) == '/')
346 while (name
[i
] == '/')
353 static char *find_name_gnu(struct apply_state
*state
,
358 struct strbuf name
= STRBUF_INIT
;
362 * Proposed "new-style" GNU patch/diff format; see
363 * http://marc.info/?l=git&m=112927316408690&w=2
365 if (unquote_c_style(&name
, line
, NULL
)) {
366 strbuf_release(&name
);
370 for (cp
= name
.buf
; p_value
; p_value
--) {
371 cp
= strchr(cp
, '/');
373 strbuf_release(&name
);
379 strbuf_remove(&name
, 0, cp
- name
.buf
);
381 strbuf_insert(&name
, 0, state
->root
.buf
, state
->root
.len
);
382 return squash_slash(strbuf_detach(&name
, NULL
));
385 static size_t sane_tz_len(const char *line
, size_t len
)
389 if (len
< strlen(" +0500") || line
[len
-strlen(" +0500")] != ' ')
391 tz
= line
+ len
- strlen(" +0500");
393 if (tz
[1] != '+' && tz
[1] != '-')
396 for (p
= tz
+ 2; p
!= line
+ len
; p
++)
400 return line
+ len
- tz
;
403 static size_t tz_with_colon_len(const char *line
, size_t len
)
407 if (len
< strlen(" +08:00") || line
[len
- strlen(":00")] != ':')
409 tz
= line
+ len
- strlen(" +08:00");
411 if (tz
[0] != ' ' || (tz
[1] != '+' && tz
[1] != '-'))
414 if (!isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
415 !isdigit(*p
++) || !isdigit(*p
++))
418 return line
+ len
- tz
;
421 static size_t date_len(const char *line
, size_t len
)
423 const char *date
, *p
;
425 if (len
< strlen("72-02-05") || line
[len
-strlen("-05")] != '-')
427 p
= date
= line
+ len
- strlen("72-02-05");
429 if (!isdigit(*p
++) || !isdigit(*p
++) || *p
++ != '-' ||
430 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != '-' ||
431 !isdigit(*p
++) || !isdigit(*p
++)) /* Not a date. */
434 if (date
- line
>= strlen("19") &&
435 isdigit(date
[-1]) && isdigit(date
[-2])) /* 4-digit year */
436 date
-= strlen("19");
438 return line
+ len
- date
;
441 static size_t short_time_len(const char *line
, size_t len
)
443 const char *time
, *p
;
445 if (len
< strlen(" 07:01:32") || line
[len
-strlen(":32")] != ':')
447 p
= time
= line
+ len
- strlen(" 07:01:32");
449 /* Permit 1-digit hours? */
451 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
452 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
453 !isdigit(*p
++) || !isdigit(*p
++)) /* Not a time. */
456 return line
+ len
- time
;
459 static size_t fractional_time_len(const char *line
, size_t len
)
464 /* Expected format: 19:41:17.620000023 */
465 if (!len
|| !isdigit(line
[len
- 1]))
469 /* Fractional seconds. */
470 while (p
> line
&& isdigit(*p
))
475 /* Hours, minutes, and whole seconds. */
476 n
= short_time_len(line
, p
- line
);
480 return line
+ len
- p
+ n
;
483 static size_t trailing_spaces_len(const char *line
, size_t len
)
487 /* Expected format: ' ' x (1 or more) */
488 if (!len
|| line
[len
- 1] != ' ')
495 return line
+ len
- (p
+ 1);
502 static size_t diff_timestamp_len(const char *line
, size_t len
)
504 const char *end
= line
+ len
;
508 * Posix: 2010-07-05 19:41:17
509 * GNU: 2010-07-05 19:41:17.620000023 -0500
512 if (!isdigit(end
[-1]))
515 n
= sane_tz_len(line
, end
- line
);
517 n
= tz_with_colon_len(line
, end
- line
);
520 n
= short_time_len(line
, end
- line
);
522 n
= fractional_time_len(line
, end
- line
);
525 n
= date_len(line
, end
- line
);
526 if (!n
) /* No date. Too bad. */
530 if (end
== line
) /* No space before date. */
532 if (end
[-1] == '\t') { /* Success! */
534 return line
+ len
- end
;
536 if (end
[-1] != ' ') /* No space before date. */
539 /* Whitespace damage. */
540 end
-= trailing_spaces_len(line
, end
- line
);
541 return line
+ len
- end
;
544 static char *find_name_common(struct apply_state
*state
,
552 const char *start
= NULL
;
556 while (line
!= end
) {
559 if (!end
&& isspace(c
)) {
562 if (name_terminate(c
, terminate
))
566 if (c
== '/' && !--p_value
)
570 return squash_slash(xstrdup_or_null(def
));
573 return squash_slash(xstrdup_or_null(def
));
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"
582 int deflen
= strlen(def
);
583 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
584 return squash_slash(xstrdup(def
));
587 if (state
->root
.len
) {
588 char *ret
= xstrfmt("%s%.*s", state
->root
.buf
, len
, start
);
589 return squash_slash(ret
);
592 return squash_slash(xmemdupz(start
, len
));
595 static char *find_name(struct apply_state
*state
,
602 char *name
= find_name_gnu(state
, line
, def
, p_value
);
607 return find_name_common(state
, line
, def
, p_value
, NULL
, terminate
);
610 static char *find_name_traditional(struct apply_state
*state
,
619 char *name
= find_name_gnu(state
, line
, def
, p_value
);
624 len
= strchrnul(line
, '\n') - line
;
625 date_len
= diff_timestamp_len(line
, len
);
627 return find_name_common(state
, line
, def
, p_value
, NULL
, TERM_TAB
);
630 return find_name_common(state
, line
, def
, p_value
, line
+ len
, 0);
633 static int count_slashes(const char *cp
)
645 * Given the string after "--- " or "+++ ", guess the appropriate
646 * p_value for the given patch.
648 static int guess_p_value(struct apply_state
*state
, const char *nameline
)
653 if (is_dev_null(nameline
))
655 name
= find_name_traditional(state
, nameline
, NULL
, 0);
658 cp
= strchr(name
, '/');
661 else if (state
->prefix
) {
663 * Does it begin with "a/$our-prefix" and such? Then this is
664 * very likely to apply to our directory.
666 if (!strncmp(name
, state
->prefix
, state
->prefix_length
))
667 val
= count_slashes(state
->prefix
);
670 if (!strncmp(cp
, state
->prefix
, state
->prefix_length
))
671 val
= count_slashes(state
->prefix
) + 1;
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?
683 static int has_epoch_timestamp(const char *nameline
)
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".
691 const char stamp_regexp
[] =
692 "^(1969-12-31|1970-01-01)"
694 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
696 "([-+][0-2][0-9]:?[0-5][0-9])\n";
697 const char *timestamp
= NULL
, *cp
, *colon
;
698 static regex_t
*stamp
;
704 for (cp
= nameline
; *cp
!= '\n'; cp
++) {
711 stamp
= xmalloc(sizeof(*stamp
));
712 if (regcomp(stamp
, stamp_regexp
, REG_EXTENDED
)) {
713 warning(_("Cannot prepare timestamp regexp %s"),
719 status
= regexec(stamp
, timestamp
, ARRAY_SIZE(m
), m
, 0);
721 if (status
!= REG_NOMATCH
)
722 warning(_("regexec returned %d for input: %s"),
727 zoneoffset
= strtol(timestamp
+ m
[3].rm_so
+ 1, (char **) &colon
, 10);
729 zoneoffset
= zoneoffset
* 60 + strtol(colon
+ 1, NULL
, 10);
731 zoneoffset
= (zoneoffset
/ 100) * 60 + (zoneoffset
% 100);
732 if (timestamp
[m
[3].rm_so
] == '-')
733 zoneoffset
= -zoneoffset
;
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)
739 if ((zoneoffset
< 0 && memcmp(timestamp
, "1969-12-31", 10)) ||
740 (0 <= zoneoffset
&& memcmp(timestamp
, "1970-01-01", 10)))
743 hourminute
= (strtol(timestamp
+ 11, NULL
, 10) * 60 +
744 strtol(timestamp
+ 14, NULL
, 10) -
747 return ((zoneoffset
< 0 && hourminute
== 1440) ||
748 (0 <= zoneoffset
&& !hourminute
));
752 * Get the name etc info from the ---/+++ lines of a traditional patch header
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.
758 static int parse_traditional_patch(struct apply_state
*state
,
765 first
+= 4; /* skip "--- " */
766 second
+= 4; /* skip "+++ " */
767 if (!state
->p_value_known
) {
769 p
= guess_p_value(state
, first
);
770 q
= guess_p_value(state
, second
);
772 if (0 <= p
&& p
== q
) {
774 state
->p_value_known
= 1;
777 if (is_dev_null(first
)) {
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
)) {
784 patch
->is_delete
= 1;
785 name
= find_name_traditional(state
, first
, NULL
, state
->p_value
);
786 patch
->old_name
= 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
);
792 if (has_epoch_timestamp(first
)) {
794 patch
->is_delete
= 0;
795 patch
->new_name
= name
;
796 } else if (has_epoch_timestamp(second
)) {
798 patch
->is_delete
= 1;
799 patch
->old_name
= name
;
801 patch
->old_name
= name
;
802 patch
->new_name
= xstrdup_or_null(name
);
806 return error(_("unable to find filename in patch at line %d"), state
->linenr
);
811 static int gitdiff_hdrend(struct apply_state
*state
,
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.
823 * As a result, gitdiff_{old|new}name() will check
824 * their names against any previous information, just
827 #define DIFF_OLD_NAME 0
828 #define DIFF_NEW_NAME 1
830 static int gitdiff_verify_name(struct apply_state
*state
,
836 if (!*name
&& !isnull
) {
837 *name
= find_name(state
, line
, NULL
, state
->p_value
, TERM_TAB
);
842 int len
= strlen(*name
);
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)) {
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
);
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
);
864 static int gitdiff_oldname(struct apply_state
*state
,
868 return gitdiff_verify_name(state
, line
,
869 patch
->is_new
, &patch
->old_name
,
873 static int gitdiff_newname(struct apply_state
*state
,
877 return gitdiff_verify_name(state
, line
,
878 patch
->is_delete
, &patch
->new_name
,
882 static int gitdiff_oldmode(struct apply_state
*state
,
886 patch
->old_mode
= strtoul(line
, NULL
, 8);
890 static int gitdiff_newmode(struct apply_state
*state
,
894 patch
->new_mode
= strtoul(line
, NULL
, 8);
898 static int gitdiff_delete(struct apply_state
*state
,
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
);
908 static int gitdiff_newfile(struct apply_state
*state
,
913 free(patch
->new_name
);
914 patch
->new_name
= xstrdup_or_null(patch
->def_name
);
915 return gitdiff_newmode(state
, line
, patch
);
918 static int gitdiff_copysrc(struct apply_state
*state
,
923 free(patch
->old_name
);
924 patch
->old_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
928 static int gitdiff_copydst(struct apply_state
*state
,
933 free(patch
->new_name
);
934 patch
->new_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
938 static int gitdiff_renamesrc(struct apply_state
*state
,
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);
948 static int gitdiff_renamedst(struct apply_state
*state
,
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);
958 static int gitdiff_similarity(struct apply_state
*state
,
962 unsigned long val
= strtoul(line
, NULL
, 10);
968 static int gitdiff_dissimilarity(struct apply_state
*state
,
972 unsigned long val
= strtoul(line
, NULL
, 10);
978 static int gitdiff_index(struct apply_state
*state
,
983 * index line is N hexadecimal, "..", N hexadecimal,
984 * and optional space with octal mode.
986 const char *ptr
, *eol
;
989 ptr
= strchr(line
, '.');
990 if (!ptr
|| ptr
[1] != '.' || 40 < ptr
- line
)
993 memcpy(patch
->old_sha1_prefix
, line
, len
);
994 patch
->old_sha1_prefix
[len
] = 0;
997 ptr
= strchr(line
, ' ');
998 eol
= strchrnul(line
, '\n');
1000 if (!ptr
|| eol
< ptr
)
1006 memcpy(patch
->new_sha1_prefix
, line
, len
);
1007 patch
->new_sha1_prefix
[len
] = 0;
1009 patch
->old_mode
= strtoul(ptr
+1, NULL
, 8);
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.
1017 static int gitdiff_unrecognized(struct apply_state
*state
,
1019 struct patch
*patch
)
1025 * Skip p_value leading components from "line"; as we do not accept
1026 * absolute paths, return NULL in that case.
1028 static const char *skip_tree_prefix(struct apply_state
*state
,
1035 if (!state
->p_value
)
1036 return (llen
&& line
[0] == '/') ? NULL
: line
;
1038 nslash
= state
->p_value
;
1039 for (i
= 0; i
< llen
; i
++) {
1041 if (ch
== '/' && --nslash
<= 0)
1042 return (i
== 0) ? NULL
: &line
[i
+ 1];
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.
1055 static char *git_header_name(struct apply_state
*state
,
1060 const char *second
= NULL
;
1061 size_t len
, line_len
;
1063 line
+= strlen("diff --git ");
1064 llen
-= strlen("diff --git ");
1068 struct strbuf first
= STRBUF_INIT
;
1069 struct strbuf sp
= STRBUF_INIT
;
1071 if (unquote_c_style(&first
, line
, &second
))
1072 goto free_and_fail1
;
1074 /* strip the a/b prefix including trailing slash */
1075 cp
= skip_tree_prefix(state
, first
.buf
, first
.len
);
1077 goto free_and_fail1
;
1078 strbuf_remove(&first
, 0, cp
- first
.buf
);
1081 * second points at one past closing dq of name.
1082 * find the second name.
1084 while ((second
< line
+ llen
) && isspace(*second
))
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
);
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
);
1102 /* unquoted second */
1103 cp
= skip_tree_prefix(state
, second
, line
+ llen
- second
);
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
);
1112 strbuf_release(&first
);
1113 strbuf_release(&sp
);
1117 /* unquoted first name */
1118 name
= skip_tree_prefix(state
, line
, llen
);
1123 * since the first name is unquoted, a dq if exists must be
1124 * the beginning of the second name.
1126 for (second
= name
; second
< line
+ llen
; second
++) {
1127 if (*second
== '"') {
1128 struct strbuf sp
= STRBUF_INIT
;
1131 if (unquote_c_style(&sp
, second
, NULL
))
1132 goto free_and_fail2
;
1134 np
= skip_tree_prefix(state
, sp
.buf
, sp
.len
);
1136 goto free_and_fail2
;
1138 len
= sp
.buf
+ sp
.len
- np
;
1139 if (len
< second
- name
&&
1140 !strncmp(np
, name
, len
) &&
1141 isspace(name
[len
])) {
1143 strbuf_remove(&sp
, 0, np
- sp
.buf
);
1144 return strbuf_detach(&sp
, NULL
);
1148 strbuf_release(&sp
);
1154 * Accept a name only if it shows up twice, exactly the same
1157 second
= strchr(name
, '\n');
1160 line_len
= second
- name
;
1161 for (len
= 0 ; ; len
++) {
1162 switch (name
[len
]) {
1167 case '\t': case ' ':
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.
1177 return NULL
; /* no postimage name */
1178 second
= skip_tree_prefix(state
, name
+ len
+ 1,
1179 line_len
- (len
+ 1));
1183 * Does len bytes starting at "name" and "second"
1184 * (that are separated by one HT or SP we just
1185 * found) exactly match?
1187 if (second
[len
] == '\n' && !strncmp(name
, second
, len
))
1188 return xmemdupz(name
, len
);
1193 /* Verify that we recognize the lines following a git header */
1194 static int parse_git_header(struct apply_state
*state
,
1198 struct patch
*patch
)
1200 unsigned long offset
;
1202 /* A git diff has explicit new/delete information, so we don't guess */
1204 patch
->is_delete
= 0;
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.
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
;
1222 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, state
->linenr
++) {
1223 static const struct opentry
{
1225 int (*fn
)(struct apply_state
*, const char *, struct patch
*);
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
},
1247 len
= linelen(line
, size
);
1248 if (!len
|| line
[len
-1] != '\n')
1250 for (i
= 0; i
< ARRAY_SIZE(optable
); i
++) {
1251 const struct opentry
*p
= optable
+ i
;
1252 int oplen
= strlen(p
->str
);
1254 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
1256 res
= p
->fn(state
, line
+ oplen
, patch
);
1268 static int parse_num(const char *line
, unsigned long *p
)
1272 if (!isdigit(*line
))
1274 *p
= strtoul(line
, &ptr
, 10);
1278 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
1279 unsigned long *p1
, unsigned long *p2
)
1283 if (offset
< 0 || offset
>= len
)
1288 digits
= parse_num(line
, p1
);
1298 digits
= parse_num(line
+1, p2
);
1307 ex
= strlen(expect
);
1310 if (memcmp(line
, expect
, ex
))
1316 static void recount_diff(const char *line
, int size
, struct fragment
*fragment
)
1318 int oldlines
= 0, newlines
= 0, ret
= 0;
1321 warning("recount: ignore empty hunk");
1326 int len
= linelen(line
, size
);
1334 case ' ': case '\n':
1346 ret
= size
< 3 || !starts_with(line
, "@@ ");
1349 ret
= size
< 5 || !starts_with(line
, "diff ");
1356 warning(_("recount: unexpected line: %.*s"),
1357 (int)linelen(line
, size
), line
);
1362 fragment
->oldlines
= oldlines
;
1363 fragment
->newlines
= newlines
;
1367 * Parse a unified diff fragment header of the
1368 * form "@@ -a,b +c,d @@"
1370 static int parse_fragment_header(const char *line
, int len
, struct fragment
*fragment
)
1374 if (!len
|| line
[len
-1] != '\n')
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
);
1385 * Find file diff header
1388 * -1 if no header was found
1389 * -128 in case of error
1390 * the size of the header in bytes (called "offset") otherwise
1392 static int find_header(struct apply_state
*state
,
1396 struct patch
*patch
)
1398 unsigned long offset
, len
;
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
;
1408 len
= linelen(line
, size
);
1412 /* Testing this early allows us to take a few shortcuts.. */
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.
1421 if (!memcmp("@@ -", line
, 4)) {
1422 struct fragment dummy
;
1423 if (parse_fragment_header(line
, len
, &dummy
) < 0)
1425 error(_("patch fragment without header at line %d: %.*s"),
1426 state
->linenr
, (int)len
-1, line
);
1434 * Git patch? It might not have a real patch, just a rename
1435 * or mode change, so we handle that specially
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)
1441 if (git_hdr_len
<= len
)
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)",
1450 state
->p_value
, state
->linenr
);
1453 patch
->old_name
= xstrdup(patch
->def_name
);
1454 patch
->new_name
= xstrdup(patch
->def_name
);
1456 if (!patch
->is_delete
&& !patch
->new_name
) {
1457 error("git diff header lacks filename information "
1458 "(line %d)", state
->linenr
);
1461 patch
->is_toplevel_relative
= 1;
1462 *hdrsize
= git_hdr_len
;
1466 /* --- followed by +++ ? */
1467 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
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).
1475 nextlen
= linelen(line
+ len
, size
- len
);
1476 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
1479 /* Ok, we'll consider it a patch */
1480 if (parse_traditional_patch(state
, line
, line
+len
, patch
))
1482 *hdrsize
= len
+ nextlen
;
1489 static void record_ws_error(struct apply_state
*state
,
1500 state
->whitespace_error
++;
1501 if (state
->squelch_whitespace_errors
&&
1502 state
->squelch_whitespace_errors
< state
->whitespace_error
)
1505 err
= whitespace_error_string(result
);
1506 fprintf(stderr
, "%s:%d: %s.\n%.*s\n",
1507 state
->patch_input_file
, linenr
, err
, len
, line
);
1511 static void check_whitespace(struct apply_state
*state
,
1516 unsigned result
= ws_check(line
+ 1, len
- 1, ws_rule
);
1518 record_ws_error(state
, result
, line
+ 1, len
- 2, state
->linenr
);
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..
1527 static int parse_fragment(struct apply_state
*state
,
1530 struct patch
*patch
,
1531 struct fragment
*fragment
)
1534 int len
= linelen(line
, size
), offset
;
1535 unsigned long oldlines
, newlines
;
1536 unsigned long leading
, trailing
;
1538 offset
= parse_fragment_header(line
, len
, fragment
);
1541 if (offset
> 0 && patch
->recount
)
1542 recount_diff(line
+ offset
, size
- offset
, fragment
);
1543 oldlines
= fragment
->oldlines
;
1544 newlines
= fragment
->newlines
;
1548 /* Parse the thing.. */
1552 added
= deleted
= 0;
1555 offset
+= len
, size
-= len
, line
+= len
, state
->linenr
++) {
1556 if (!oldlines
&& !newlines
)
1558 len
= linelen(line
, size
);
1559 if (!len
|| line
[len
-1] != '\n')
1564 case '\n': /* newer GNU diff, an empty context line */
1568 if (!deleted
&& !added
)
1571 if (!state
->apply_in_reverse
&&
1572 state
->ws_error_action
== correct_ws_error
)
1573 check_whitespace(state
, line
, len
, patch
->ws_rule
);
1576 if (state
->apply_in_reverse
&&
1577 state
->ws_error_action
!= nowarn_ws_error
)
1578 check_whitespace(state
, line
, len
, patch
->ws_rule
);
1584 if (!state
->apply_in_reverse
&&
1585 state
->ws_error_action
!= nowarn_ws_error
)
1586 check_whitespace(state
, line
, len
, patch
->ws_rule
);
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.
1601 if (len
< 12 || memcmp(line
, "\\ ", 2))
1606 if (oldlines
|| newlines
)
1608 if (!deleted
&& !added
)
1611 fragment
->leading
= leading
;
1612 fragment
->trailing
= trailing
;
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
1619 if (12 < size
&& !memcmp(line
, "\\ ", 2))
1620 offset
+= linelen(line
, size
);
1622 patch
->lines_added
+= added
;
1623 patch
->lines_deleted
+= deleted
;
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"));
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.
1637 * The (fragment->patch, fragment->size) pair points into the memory given
1638 * by the caller, not a copy, when we return.
1641 * -1 in case of error,
1642 * the number of bytes in the patch otherwise.
1644 static int parse_single_patch(struct apply_state
*state
,
1647 struct patch
*patch
)
1649 unsigned long offset
= 0;
1650 unsigned long oldlines
= 0, newlines
= 0, context
= 0;
1651 struct fragment
**fragp
= &patch
->fragments
;
1653 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
1654 struct fragment
*fragment
;
1657 fragment
= xcalloc(1, sizeof(*fragment
));
1658 fragment
->linenr
= state
->linenr
;
1659 len
= parse_fragment(state
, line
, size
, patch
, fragment
);
1662 return error(_("corrupt patch at line %d"), state
->linenr
);
1664 fragment
->patch
= line
;
1665 fragment
->size
= len
;
1666 oldlines
+= fragment
->oldlines
;
1667 newlines
+= fragment
->newlines
;
1668 context
+= fragment
->leading
+ fragment
->trailing
;
1671 fragp
= &fragment
->next
;
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.
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.
1691 if (patch
->is_new
< 0 &&
1692 (oldlines
|| (patch
->fragments
&& patch
->fragments
->next
)))
1694 if (patch
->is_delete
< 0 &&
1695 (newlines
|| (patch
->fragments
&& patch
->fragments
->next
)))
1696 patch
->is_delete
= 0;
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
)
1705 "file %s becomes empty but is not deleted"),
1711 static inline int metadata_changes(struct patch
*patch
)
1713 return patch
->is_rename
> 0 ||
1714 patch
->is_copy
> 0 ||
1715 patch
->is_new
> 0 ||
1717 (patch
->old_mode
&& patch
->new_mode
&&
1718 patch
->old_mode
!= patch
->new_mode
);
1721 static char *inflate_it(const void *data
, unsigned long size
,
1722 unsigned long inflated_size
)
1728 memset(&stream
, 0, sizeof(stream
));
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
) {
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".
1749 static struct fragment
*parse_binary_hunk(struct apply_state
*state
,
1751 unsigned long *sz_p
,
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.
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.
1768 unsigned long size
= *sz_p
;
1769 char *buffer
= *buf_p
;
1771 unsigned long origlen
;
1774 struct fragment
*frag
;
1776 llen
= linelen(buffer
, size
);
1781 if (starts_with(buffer
, "delta ")) {
1782 patch_method
= BINARY_DELTA_DEFLATED
;
1783 origlen
= strtoul(buffer
+ 6, NULL
, 10);
1785 else if (starts_with(buffer
, "literal ")) {
1786 patch_method
= BINARY_LITERAL_DEFLATED
;
1787 origlen
= strtoul(buffer
+ 8, NULL
, 10);
1795 int byte_length
, max_byte_length
, newsize
;
1796 llen
= linelen(buffer
, size
);
1800 /* consume the blank line */
1806 * Minimum line is "A00000\n" which is 7-byte long,
1807 * and the line length must be multiple of 5 plus 2.
1809 if ((llen
< 7) || (llen
-2) % 5)
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;
1819 /* if the input length was not multiple of 4, we would
1820 * have filler at the end but the filler should never
1823 if (max_byte_length
< byte_length
||
1824 byte_length
<= max_byte_length
- 4)
1826 newsize
= hunk_size
+ byte_length
;
1827 data
= xrealloc(data
, newsize
);
1828 if (decode_85(data
+ hunk_size
, buffer
+ 1, byte_length
))
1830 hunk_size
= newsize
;
1835 frag
= xcalloc(1, sizeof(*frag
));
1836 frag
->patch
= inflate_it(data
, hunk_size
, origlen
);
1837 frag
->free_patch
= 1;
1841 frag
->size
= origlen
;
1845 frag
->binary_patch_method
= patch_method
;
1851 error(_("corrupt binary patch at line %d: %.*s"),
1852 state
->linenr
-1, llen
-1, buffer
);
1858 * -1 in case of error,
1859 * the length of the parsed binary patch otherwise
1861 static int parse_binary(struct apply_state
*state
,
1864 struct patch
*patch
)
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
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
1880 struct fragment
*forward
;
1881 struct fragment
*reverse
;
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);
1890 /* otherwise we already gave an error message */
1893 reverse
= parse_binary_hunk(state
, &buffer
, &size
, &status
, &used_1
);
1898 * Not having reverse hunk is not an error, but having
1899 * a corrupt reverse hunk is.
1901 free((void*) forward
->patch
);
1905 forward
->next
= reverse
;
1906 patch
->fragments
= forward
;
1907 patch
->is_binary
= 1;
1911 static void prefix_one(struct apply_state
*state
, char **name
)
1913 char *old_name
= *name
;
1916 *name
= xstrdup(prefix_filename(state
->prefix
, state
->prefix_length
, *name
));
1920 static void prefix_patch(struct apply_state
*state
, struct patch
*p
)
1922 if (!state
->prefix
|| p
->is_toplevel_relative
)
1924 prefix_one(state
, &p
->new_name
);
1925 prefix_one(state
, &p
->old_name
);
1932 static void add_name_limit(struct apply_state
*state
,
1936 struct string_list_item
*it
;
1938 it
= string_list_append(&state
->limit_by_name
, name
);
1939 it
->util
= exclude
? NULL
: (void *) 1;
1942 static int use_patch(struct apply_state
*state
, struct patch
*p
)
1944 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
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
))
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
);
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.
1967 return !state
->has_include
;
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.
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.
1981 static int parse_chunk(struct apply_state
*state
, char *buffer
, unsigned long size
, struct patch
*patch
)
1983 int hdrsize
, patchsize
;
1984 int offset
= find_header(state
, buffer
, size
, &hdrsize
, patch
);
1989 prefix_patch(state
, patch
);
1991 if (!use_patch(state
, patch
))
1994 patch
->ws_rule
= whitespace_rule(patch
->new_name
1998 patchsize
= parse_single_patch(state
,
1999 buffer
+ offset
+ hdrsize
,
2000 size
- offset
- hdrsize
,
2007 static const char git_binary
[] = "GIT binary patch\n";
2008 int hd
= hdrsize
+ offset
;
2009 unsigned long llen
= linelen(buffer
+ hd
, size
- hd
);
2011 if (llen
== sizeof(git_binary
) - 1 &&
2012 !memcmp(git_binary
, buffer
+ hd
, llen
)) {
2015 used
= parse_binary(state
, buffer
+ hd
+ llen
,
2016 size
- hd
- llen
, patch
);
2020 patchsize
= used
+ llen
;
2024 else if (!memcmp(" differ\n", buffer
+ hd
+ llen
- 8, 8)) {
2025 static const char *binhdr
[] = {
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
)) {
2036 patch
->is_binary
= 1;
2043 /* Empty patch cannot be applied if it is a text patch
2044 * without metadata change. A binary patch appears
2047 if ((state
->apply
|| state
->check
) &&
2048 (!patch
->is_binary
&& !metadata_changes(patch
))) {
2049 error(_("patch with only garbage at line %d"), state
->linenr
);
2054 return offset
+ hdrsize
+ patchsize
;
2057 #define swap(a,b) myswap((a),(b),sizeof(a))
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); \
2066 static void reverse_patches(struct patch
*p
)
2068 for (; p
; p
= p
->next
) {
2069 struct fragment
*frag
= p
->fragments
;
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
);
2077 for (; frag
; frag
= frag
->next
) {
2078 swap(frag
->newpos
, frag
->oldpos
);
2079 swap(frag
->newlines
, frag
->oldlines
);
2084 static const char pluses
[] =
2085 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2086 static const char minuses
[]=
2087 "----------------------------------------------------------------------";
2089 static void show_stats(struct apply_state
*state
, struct patch
*patch
)
2091 struct strbuf qname
= STRBUF_INIT
;
2092 char *cp
= patch
->new_name
? patch
->new_name
: patch
->old_name
;
2095 quote_c_style(cp
, &qname
, NULL
, 0);
2098 * "scale" the filename
2100 max
= state
->max_len
;
2104 if (qname
.len
> max
) {
2105 cp
= strchr(qname
.buf
+ qname
.len
+ 3 - max
, '/');
2107 cp
= qname
.buf
+ qname
.len
+ 3 - max
;
2108 strbuf_splice(&qname
, 0, cp
- qname
.buf
, "...", 3);
2111 if (patch
->is_binary
) {
2112 printf(" %-*s | Bin\n", max
, qname
.buf
);
2113 strbuf_release(&qname
);
2117 printf(" %-*s |", max
, qname
.buf
);
2118 strbuf_release(&qname
);
2121 * scale the add/delete
2123 max
= max
+ state
->max_change
> 70 ? 70 - max
: state
->max_change
;
2124 add
= patch
->lines_added
;
2125 del
= patch
->lines_deleted
;
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
;
2132 printf("%5d %.*s%.*s\n", patch
->lines_added
+ patch
->lines_deleted
,
2133 add
, pluses
, del
, minuses
);
2136 static int read_old_data(struct stat
*st
, const char *path
, struct strbuf
*buf
)
2138 switch (st
->st_mode
& S_IFMT
) {
2140 if (strbuf_readlink(buf
, path
, st
->st_size
) < 0)
2141 return error(_("unable to read symlink %s"), path
);
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);
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
2160 static void update_pre_post_images(struct image
*preimage
,
2161 struct image
*postimage
,
2163 size_t len
, size_t postlen
)
2165 int i
, ctx
, reduced
;
2166 char *new, *old
, *fixed
;
2167 struct image fixed_preimage
;
2170 * Update the preimage with whitespace fixes. Note that we
2171 * are not losing preimage->buf -- apply_one_fragment() will
2174 prepare_image(&fixed_preimage
, buf
, len
, 1);
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
;
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.
2189 * We trust the caller to tell us if the update can be done
2190 * in place (postlen==0) or not.
2192 old
= postimage
->buf
;
2194 new = postimage
->buf
= xmalloc(postlen
);
2197 fixed
= preimage
->buf
;
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
);
2209 /* a common context -- skip it in the original postimage */
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
;
2220 * preimage is expected to run out, if the caller
2221 * fixed addition of trailing blank lines.
2223 if (preimage
->nr
<= ctx
) {
2228 /* and copy it in, while fixing the line length */
2229 l_len
= preimage
->line
[ctx
].len
;
2230 memcpy(new, fixed
, l_len
);
2233 postimage
->line
[i
].len
= l_len
;
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
));
2243 /* Fix the length of the whole thing */
2244 postimage
->len
= new - postimage
->buf
;
2245 postimage
->nr
-= reduced
;
2248 static int line_by_line_fuzzy_match(struct image
*img
,
2249 struct image
*preimage
,
2250 struct image
*postimage
,
2258 size_t postlen
= postimage
->len
;
2263 struct strbuf fixed
;
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
;
2271 if (!fuzzy_matchlines(img
->buf
+ try + imgoff
, imglen
,
2272 preimage
->buf
+ preoff
, prelen
))
2274 if (preimage
->line
[i
].flag
& LINE_COMMON
)
2275 postlen
+= imglen
- prelen
;
2281 * Ok, the preimage matches with whitespace fuzz.
2283 * imgoff now holds the true length of the target that
2284 * matches the preimage before the end of the file.
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.)
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
++)
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.
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
);
2316 static int match_fragment(struct apply_state
*state
,
2318 struct image
*preimage
,
2319 struct image
*postimage
,
2323 int match_beginning
, int match_end
)
2326 char *fixed_buf
, *buf
, *orig
, *target
;
2327 struct strbuf fixed
;
2328 size_t fixed_len
, postlen
;
2331 if (preimage
->nr
+ try_lno
<= img
->nr
) {
2333 * The hunk falls within the boundaries of img.
2335 preimage_limit
= preimage
->nr
;
2336 if (match_end
&& (preimage
->nr
+ try_lno
!= img
->nr
))
2338 } else if (state
->ws_error_action
== correct_ws_error
&&
2339 (ws_rule
& WS_BLANK_AT_EOF
)) {
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
2347 preimage_limit
= img
->nr
- try_lno
;
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.
2357 if (match_beginning
&& try_lno
)
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
))
2366 if (preimage_limit
== preimage
->nr
) {
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
2375 ? (try + preimage
->len
== img
->len
)
2376 : (try + preimage
->len
<= img
->len
)) &&
2377 !memcmp(img
->buf
+ try, preimage
->buf
, preimage
->len
))
2381 * The preimage extends beyond the end of img, so
2382 * there cannot be an exact match.
2384 * There must be one non-blank context line that match
2385 * a line before the end of img.
2389 buf
= preimage
->buf
;
2391 for (i
= 0; i
< preimage_limit
; i
++)
2392 buf_end
+= preimage
->line
[i
].len
;
2394 for ( ; buf
< buf_end
; buf
++)
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.
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
);
2410 if (state
->ws_error_action
!= correct_ws_error
)
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.
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
2428 /* First count added lines in postimage */
2430 for (i
= 0; i
< postimage
->nr
; i
++) {
2431 if (!(postimage
->line
[i
].flag
& LINE_COMMON
))
2432 postlen
+= postimage
->line
[i
].len
;
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.
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
;
2450 /* Try fixing the line in the preimage */
2451 ws_fix_copy(&fixed
, orig
, oldlen
, ws_rule
, NULL
);
2453 /* Try fixing the line in the target */
2454 strbuf_init(&tgtfix
, tgtlen
);
2455 ws_fix_copy(&tgtfix
, target
, tgtlen
, ws_rule
, NULL
);
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
2467 match
= (tgtfix
.len
== fixed
.len
- fixstart
&&
2468 !memcmp(tgtfix
.buf
, fixed
.buf
+ fixstart
,
2469 fixed
.len
- fixstart
));
2471 /* Add the length if this is common with the postimage */
2472 if (preimage
->line
[i
].flag
& LINE_COMMON
)
2473 postlen
+= tgtfix
.len
;
2475 strbuf_release(&tgtfix
);
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
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
;
2495 /* Try fixing the line in the preimage */
2496 ws_fix_copy(&fixed
, orig
, oldlen
, ws_rule
, NULL
);
2498 for (j
= fixstart
; j
< fixed
.len
; j
++)
2499 if (!isspace(fixed
.buf
[j
]))
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.
2510 fixed_buf
= strbuf_detach(&fixed
, &fixed_len
);
2511 if (postlen
< postimage
->len
)
2513 update_pre_post_images(preimage
, postimage
,
2514 fixed_buf
, fixed_len
, postlen
);
2518 strbuf_release(&fixed
);
2522 static int find_pos(struct apply_state
*state
,
2524 struct image
*preimage
,
2525 struct image
*postimage
,
2528 int match_beginning
, int match_end
)
2531 unsigned long backwards
, forwards
, try;
2532 int backwards_lno
, forwards_lno
, try_lno
;
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.
2539 if (match_beginning
)
2542 line
= img
->nr
- preimage
->nr
;
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.
2549 if ((size_t) line
> img
->nr
)
2553 for (i
= 0; i
< line
; i
++)
2554 try += img
->line
[i
].len
;
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.
2561 backwards_lno
= line
;
2563 forwards_lno
= line
;
2566 for (i
= 0; ; i
++) {
2567 if (match_fragment(state
, img
, preimage
, postimage
,
2568 try, try_lno
, ws_rule
,
2569 match_beginning
, match_end
))
2573 if (backwards_lno
== 0 && forwards_lno
== img
->nr
)
2577 if (backwards_lno
== 0) {
2582 backwards
-= img
->line
[backwards_lno
].len
;
2584 try_lno
= backwards_lno
;
2586 if (forwards_lno
== img
->nr
) {
2590 forwards
+= img
->line
[forwards_lno
].len
;
2593 try_lno
= forwards_lno
;
2600 static void remove_first_line(struct image
*img
)
2602 img
->buf
+= img
->line
[0].len
;
2603 img
->len
-= img
->line
[0].len
;
2608 static void remove_last_line(struct image
*img
)
2610 img
->len
-= img
->line
[--img
->nr
].len
;
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".
2618 static void update_image(struct apply_state
*state
,
2621 struct image
*preimage
,
2622 struct image
*postimage
)
2625 * remove the copy of preimage at offset in img
2626 * and replace it with postimage
2629 size_t remove_count
, insert_count
, applied_at
= 0;
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.
2642 preimage_limit
= preimage
->nr
;
2643 if (preimage_limit
> img
->nr
- applied_pos
)
2644 preimage_limit
= img
->nr
- applied_pos
;
2646 for (i
= 0; i
< applied_pos
; i
++)
2647 applied_at
+= img
->line
[i
].len
;
2650 for (i
= 0; i
< preimage_limit
; i
++)
2651 remove_count
+= img
->line
[applied_pos
+ i
].len
;
2652 insert_count
= postimage
->len
;
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
));
2663 img
->len
+= insert_count
- remove_count
;
2664 result
[img
->len
] = '\0';
2666 /* Adjust the line table */
2667 nr
= img
->nr
+ postimage
->nr
- preimage_limit
;
2668 if (preimage_limit
< postimage
->nr
) {
2670 * NOTE: this knows that we never call remove_first_line()
2671 * on anything other than pre/post image.
2673 REALLOC_ARRAY(img
->line
, nr
);
2674 img
->line_allocated
= img
->line
;
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
,
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
;
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.
2695 static int apply_one_fragment(struct apply_state
*state
,
2696 struct image
*img
, struct fragment
*frag
,
2697 int inaccurate_eof
, unsigned ws_rule
,
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
;
2713 memset(&preimage
, 0, sizeof(preimage
));
2714 memset(&postimage
, 0, sizeof(postimage
));
2715 oldlines
= xmalloc(size
);
2716 strbuf_init(&newlines
, size
);
2721 int len
= linelen(patch
, size
);
2723 int added_blank_line
= 0;
2724 int is_blank_context
= 0;
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).
2738 if (len
< size
&& patch
[len
] == '\\')
2741 if (state
->apply_in_reverse
) {
2744 else if (first
== '+')
2750 /* Newer GNU diff, empty context line */
2752 /* ... followed by '\No newline'; nothing */
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;
2761 if (plen
&& (ws_rule
& WS_BLANK_AT_EOF
) &&
2762 ws_blank_line(patch
+ 1, plen
, ws_rule
))
2763 is_blank_context
= 1;
2765 memcpy(old
, patch
+ 1, plen
);
2766 add_line_info(&preimage
, old
, plen
,
2767 (first
== ' ' ? LINE_COMMON
: 0));
2771 /* Fall-through for ' ' */
2773 /* --no-add does not add new lines */
2774 if (first
== '+' && state
->no_add
)
2777 start
= newlines
.len
;
2779 !state
->whitespace_error
||
2780 state
->ws_error_action
!= correct_ws_error
) {
2781 strbuf_add(&newlines
, patch
+ 1, plen
);
2784 ws_fix_copy(&newlines
, patch
+ 1, plen
, ws_rule
, &state
->applied_after_fixing_ws
);
2786 add_line_info(&postimage
, newlines
.buf
+ start
, newlines
.len
- start
,
2787 (first
== '+' ? 0 : LINE_COMMON
));
2789 (ws_rule
& WS_BLANK_AT_EOF
) &&
2790 ws_blank_line(patch
+ 1, plen
, ws_rule
))
2791 added_blank_line
= 1;
2793 case '@': case '\\':
2794 /* Ignore it, we already handled it */
2797 if (state
->apply_verbosely
)
2798 error(_("invalid start of line: '%c'"), first
);
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
++;
2807 else if (is_blank_context
)
2810 new_blank_lines_at_end
= 0;
2815 if (inaccurate_eof
&&
2816 old
> oldlines
&& old
[-1] == '\n' &&
2817 newlines
.len
> 0 && newlines
.buf
[newlines
.len
- 1] == '\n') {
2819 strbuf_setlen(&newlines
, newlines
.len
- 1);
2822 leading
= frag
->leading
;
2823 trailing
= frag
->trailing
;
2826 * A hunk to change lines at the beginning would begin with
2828 * but we need to be careful. -U0 that inserts before the second
2829 * line also has this pattern.
2831 * And a hunk to add to an empty file would begin with
2834 * In other words, a hunk that is (frag->oldpos <= 1) with or
2835 * without leading context must match at the beginning.
2837 match_beginning
= (!frag
->oldpos
||
2838 (frag
->oldpos
== 1 && !state
->unidiff_zero
));
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.
2846 match_end
= !state
->unidiff_zero
&& !trailing
;
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
;
2858 applied_pos
= find_pos(state
, img
, &preimage
, &postimage
, pos
,
2859 ws_rule
, match_beginning
, match_end
);
2861 if (applied_pos
>= 0)
2864 /* Am I at my context limits? */
2865 if ((leading
<= state
->p_context
) && (trailing
<= state
->p_context
))
2867 if (match_beginning
|| match_end
) {
2868 match_beginning
= match_end
= 0;
2873 * Reduce the number of context lines; reduce both
2874 * leading and trailing if they are equal otherwise
2875 * just reduce the larger context.
2877 if (leading
>= trailing
) {
2878 remove_first_line(&preimage
);
2879 remove_first_line(&postimage
);
2883 if (trailing
> leading
) {
2884 remove_last_line(&preimage
);
2885 remove_last_line(&postimage
);
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
);
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
2908 if (state
->ws_error_action
== die_on_ws_error
)
2912 if (state
->apply_verbosely
&& applied_pos
!= pos
) {
2913 int offset
= applied_pos
- pos
;
2914 if (state
->apply_in_reverse
)
2915 offset
= 0 - offset
;
2917 Q_("Hunk #%d succeeded at %d (offset %d line).",
2918 "Hunk #%d succeeded at %d (offset %d lines).",
2920 nth_fragment
, applied_pos
+ 1, offset
);
2924 * Warn if it was necessary to reduce the number
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
);
2934 if (state
->apply_verbosely
)
2935 error(_("while searching for:\n%.*s"),
2936 (int)(old
- oldlines
), oldlines
);
2941 strbuf_release(&newlines
);
2942 free(preimage
.line_allocated
);
2943 free(postimage
.line_allocated
);
2945 return (applied_pos
< 0);
2948 static int apply_binary_fragment(struct apply_state
*state
,
2950 struct patch
*patch
)
2952 struct fragment
*fragment
= patch
->fragments
;
2957 return error(_("missing binary patch data for '%s'"),
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'",
2968 ? patch
->new_name
: patch
->old_name
);
2969 fragment
= fragment
->next
;
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
);
2981 case BINARY_LITERAL_DEFLATED
:
2983 img
->len
= fragment
->size
;
2984 img
->buf
= xmemdupz(fragment
->patch
, img
->len
);
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.
2996 static int apply_binary(struct apply_state
*state
,
2998 struct patch
*patch
)
3000 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
3001 unsigned char sha1
[20];
3004 * For safety, we require patch index line to contain
3005 * full 40-byte textual SHA1 for old and new, at least for now.
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
);
3014 if (patch
->old_name
) {
3016 * See if the old one matches what the patch
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
));
3027 /* Otherwise, the old one must be empty. */
3029 return error("the patch applies to an empty "
3030 "'%s' but it is not empty", name
);
3033 get_sha1_hex(patch
->new_sha1_prefix
, sha1
);
3034 if (is_null_sha1(sha1
)) {
3036 return 0; /* deletion patch */
3039 if (has_sha1_file(sha1
)) {
3040 /* We already have the postimage */
3041 enum object_type type
;
3045 result
= read_sha1_file(sha1
, &type
, &size
);
3047 return error("the necessary postimage %s for "
3048 "'%s' cannot be read",
3049 patch
->new_sha1_prefix
, name
);
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}.
3059 if (apply_binary_fragment(state
, img
, patch
))
3060 return error(_("binary patch does not apply to '%s'"),
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
));
3073 static int apply_fragments(struct apply_state
*state
, struct image
*img
, struct patch
*patch
)
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
;
3081 if (patch
->is_binary
)
3082 return apply_binary(state
, img
, patch
);
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
)
3097 static int read_blob_object(struct strbuf
*buf
, const unsigned char *sha1
, unsigned mode
)
3099 if (S_ISGITLINK(mode
)) {
3100 strbuf_grow(buf
, 100);
3101 strbuf_addf(buf
, "Subproject commit %s\n", sha1_to_hex(sha1
));
3103 enum object_type type
;
3107 result
= read_sha1_file(sha1
, &type
, &sz
);
3110 /* XXX read_sha1_file NUL-terminates */
3111 strbuf_attach(buf
, result
, sz
, sz
+ 1);
3116 static int read_file_or_gitlink(const struct cache_entry
*ce
, struct strbuf
*buf
)
3120 return read_blob_object(buf
, ce
->sha1
, ce
->ce_mode
);
3123 static struct patch
*in_fn_table(struct apply_state
*state
, const char *name
)
3125 struct string_list_item
*item
;
3130 item
= string_list_lookup(&state
->fn_table
, name
);
3132 return (struct patch
*)item
->util
;
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.
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.
3149 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3150 #define PATH_WAS_DELETED ((struct patch *) -1)
3152 static int to_be_deleted(struct patch
*patch
)
3154 return patch
== PATH_TO_BE_DELETED
;
3157 static int was_deleted(struct patch
*patch
)
3159 return patch
== PATH_WAS_DELETED
;
3162 static void add_to_fn_table(struct apply_state
*state
, struct patch
*patch
)
3164 struct string_list_item
*item
;
3167 * Always add new_name unless patch is a deletion
3168 * This should cover the cases for normal diffs,
3169 * file creations and copies
3171 if (patch
->new_name
!= NULL
) {
3172 item
= string_list_insert(&state
->fn_table
, patch
->new_name
);
3177 * store a failure on rename/deletion cases because
3178 * later chunks shouldn't patch old names
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
;
3186 static void prepare_fn_table(struct apply_state
*state
, struct patch
*patch
)
3189 * store information about incoming file deletion
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
;
3197 patch
= patch
->next
;
3201 static int checkout_target(struct index_state
*istate
,
3202 struct cache_entry
*ce
, struct stat
*st
)
3204 struct checkout costate
;
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
);
3215 static struct patch
*previous_patch(struct apply_state
*state
,
3216 struct patch
*patch
,
3219 struct patch
*previous
;
3222 if (patch
->is_copy
|| patch
->is_rename
)
3223 return NULL
; /* "git" patches do not depend on the order */
3225 previous
= in_fn_table(state
, patch
->old_name
);
3229 if (to_be_deleted(previous
))
3230 return NULL
; /* the deletion hasn't happened yet */
3232 if (was_deleted(previous
))
3238 static int verify_index_match(const struct cache_entry
*ce
, struct stat
*st
)
3240 if (S_ISGITLINK(ce
->ce_mode
)) {
3241 if (!S_ISDIR(st
->st_mode
))
3245 return ce_match_stat(ce
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
3248 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3250 static int load_patch_target(struct apply_state
*state
,
3252 const struct cache_entry
*ce
,
3255 unsigned expected_mode
)
3257 if (state
->cached
|| state
->check_index
) {
3258 if (read_file_or_gitlink(ce
, buf
))
3259 return error(_("failed to read %s"), name
);
3261 if (S_ISGITLINK(expected_mode
)) {
3263 return read_file_or_gitlink(ce
, buf
);
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
);
3269 if (read_old_data(st
, name
, buf
))
3270 return error(_("failed to read %s"), name
);
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.
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
)
3288 struct strbuf buf
= STRBUF_INIT
;
3291 struct patch
*previous
;
3294 previous
= previous_patch(state
, patch
, &status
);
3296 return error(_("path %s has been renamed/deleted"),
3299 /* We have a patched copy in memory; use that. */
3300 strbuf_add(&buf
, previous
->result
, previous
->resultsize
);
3302 status
= load_patch_target(state
, &buf
, ce
, st
,
3303 patch
->old_name
, patch
->old_mode
);
3306 else if (status
== SUBMODULE_PATCH_WITHOUT_INDEX
) {
3308 * There is no way to apply subproject
3309 * patch without looking at the index.
3310 * NEEDSWORK: shouldn't this be flagged
3313 free_fragment_list(patch
->fragments
);
3314 patch
->fragments
= NULL
;
3315 } else if (status
) {
3316 return error(_("failed to read %s"), patch
->old_name
);
3320 img
= strbuf_detach(&buf
, &len
);
3321 prepare_image(image
, img
, len
, !patch
->is_binary
);
3325 static int three_way_merge(struct image
*image
,
3327 const unsigned char *base
,
3328 const unsigned char *ours
,
3329 const unsigned char *theirs
)
3331 mmfile_t base_file
, our_file
, their_file
;
3332 mmbuffer_t result
= { NULL
};
3335 read_mmblob(&base_file
, base
);
3336 read_mmblob(&our_file
, ours
);
3337 read_mmblob(&their_file
, theirs
);
3338 status
= ll_merge(&result
, path
,
3341 &their_file
, "theirs", NULL
);
3342 free(base_file
.ptr
);
3344 free(their_file
.ptr
);
3345 if (status
< 0 || !result
.ptr
) {
3350 image
->buf
= result
.ptr
;
3351 image
->len
= result
.size
;
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.
3361 static int load_current(struct apply_state
*state
,
3362 struct image
*image
,
3363 struct patch
*patch
)
3365 struct strbuf buf
= STRBUF_INIT
;
3370 struct cache_entry
*ce
;
3371 char *name
= patch
->new_name
;
3372 unsigned mode
= patch
->new_mode
;
3375 die("BUG: patch to %s is not a creation", patch
->old_name
);
3377 pos
= cache_name_pos(name
, strlen(name
));
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
))
3387 if (verify_index_match(ce
, &st
))
3388 return error(_("%s: does not match index"), name
);
3390 status
= load_patch_target(state
, &buf
, ce
, &st
, name
, mode
);
3395 img
= strbuf_detach(&buf
, &len
);
3396 prepare_image(image
, img
, len
, !patch
->is_binary
);
3400 static int try_threeway(struct apply_state
*state
,
3401 struct image
*image
,
3402 struct patch
*patch
,
3404 const struct cache_entry
*ce
)
3406 unsigned char pre_sha1
[20], post_sha1
[20], our_sha1
[20];
3407 struct strbuf buf
= STRBUF_INIT
;
3411 struct image tmp_image
;
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
))
3418 /* Preimage the patch was prepared for */
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.");
3425 fprintf(stderr
, "Falling back to three-way merge...\n");
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
);
3434 /* post_sha1[] is theirs */
3435 write_sha1_file(tmp_image
.buf
, tmp_image
.len
, blob_type
, post_sha1
);
3436 clear_image(&tmp_image
);
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'",
3444 if (load_preimage(state
, &tmp_image
, patch
, st
, ce
))
3445 return error("cannot read the current contents of '%s'",
3448 write_sha1_file(tmp_image
.buf
, tmp_image
.len
, blob_type
, our_sha1
);
3449 clear_image(&tmp_image
);
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
);
3455 fprintf(stderr
, "Failed to fall back on three-way merge...\n");
3460 patch
->conflicted_threeway
= 1;
3462 oidclr(&patch
->threeway_stage
[0]);
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
);
3469 fprintf(stderr
, "Applied patch to '%s' cleanly.\n", patch
->new_name
);
3474 static int apply_data(struct apply_state
*state
, struct patch
*patch
,
3475 struct stat
*st
, const struct cache_entry
*ce
)
3479 if (load_preimage(state
, &image
, patch
, st
, ce
) < 0)
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)
3488 patch
->result
= image
.buf
;
3489 patch
->resultsize
= image
.len
;
3490 add_to_fn_table(state
, patch
);
3491 free(image
.line_allocated
);
3493 if (0 < patch
->is_delete
&& patch
->resultsize
)
3494 return error(_("removal patch leaves file contents"));
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.
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.
3510 static int check_preimage(struct apply_state
*state
,
3511 struct patch
*patch
,
3512 struct cache_entry
**ce
,
3515 const char *old_name
= patch
->old_name
;
3516 struct patch
*previous
= NULL
;
3517 int stat_ret
= 0, status
;
3518 unsigned st_mode
= 0;
3523 assert(patch
->is_new
<= 0);
3524 previous
= previous_patch(state
, patch
, &status
);
3527 return error(_("path %s has been renamed/deleted"), old_name
);
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
));
3536 if (state
->check_index
&& !previous
) {
3537 int pos
= cache_name_pos(old_name
, strlen(old_name
));
3539 if (patch
->is_new
< 0)
3541 return error(_("%s: does not exist in index"), old_name
);
3543 *ce
= active_cache
[pos
];
3545 if (checkout_target(&the_index
, *ce
, st
))
3548 if (!state
->cached
&& verify_index_match(*ce
, st
))
3549 return error(_("%s: does not match index"), old_name
);
3551 st_mode
= (*ce
)->ce_mode
;
3552 } else if (stat_ret
< 0) {
3553 if (patch
->is_new
< 0)
3555 return error(_("%s: %s"), old_name
, strerror(errno
));
3558 if (!state
->cached
&& !previous
)
3559 st_mode
= ce_mode_from_stat(*ce
, st
->st_mode
);
3561 if (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
;
3576 patch
->is_delete
= 0;
3577 free(patch
->old_name
);
3578 patch
->old_name
= NULL
;
3583 #define EXISTS_IN_INDEX 1
3584 #define EXISTS_IN_WORKTREE 2
3586 static int check_to_create(struct apply_state
*state
,
3587 const char *new_name
,
3592 if (state
->check_index
&&
3593 cache_name_pos(new_name
, strlen(new_name
)) >= 0 &&
3595 return EXISTS_IN_INDEX
;
3599 if (!lstat(new_name
, &nst
)) {
3600 if (S_ISDIR(nst
.st_mode
) || ok_if_exists
)
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.
3609 if (has_symlink_leading_path(new_name
, strlen(new_name
)))
3612 return EXISTS_IN_WORKTREE
;
3613 } else if ((errno
!= ENOENT
) && (errno
!= ENOTDIR
)) {
3614 return error("%s: %s", new_name
, strerror(errno
));
3619 static uintptr_t register_symlink_changes(struct apply_state
*state
,
3623 struct string_list_item
*ent
;
3625 ent
= string_list_lookup(&state
->symlink_changes
, path
);
3627 ent
= string_list_insert(&state
->symlink_changes
, path
);
3628 ent
->util
= (void *)0;
3630 ent
->util
= (void *)(what
| ((uintptr_t)ent
->util
));
3631 return (uintptr_t)ent
->util
;
3634 static uintptr_t check_symlink_changes(struct apply_state
*state
, const char *path
)
3636 struct string_list_item
*ent
;
3638 ent
= string_list_lookup(&state
->symlink_changes
, path
);
3641 return (uintptr_t)ent
->util
;
3644 static void prepare_symlink_changes(struct apply_state
*state
, struct patch
*patch
)
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
);
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
);
3658 static int path_is_beyond_symlink_1(struct apply_state
*state
, struct strbuf
*name
)
3661 unsigned int change
;
3663 while (--name
->len
&& name
->buf
[name
->len
] != '/')
3664 ; /* scan backwards */
3667 name
->buf
[name
->len
] = '\0';
3668 change
= check_symlink_changes(state
, name
->buf
);
3669 if (change
& APPLY_SYMLINK_IN_RESULT
)
3671 if (change
& APPLY_SYMLINK_GOES_AWAY
)
3673 * This cannot be "return 0", because we may
3674 * see a new one created at a higher level.
3678 /* otherwise, check the preimage */
3679 if (state
->check_index
) {
3680 struct cache_entry
*ce
;
3682 ce
= cache_file_exists(name
->buf
, name
->len
, ignore_case
);
3683 if (ce
&& S_ISLNK(ce
->ce_mode
))
3687 if (!lstat(name
->buf
, &st
) && S_ISLNK(st
.st_mode
))
3694 static int path_is_beyond_symlink(struct apply_state
*state
, const char *name_
)
3697 struct strbuf name
= STRBUF_INIT
;
3699 assert(*name_
!= '\0');
3700 strbuf_addstr(&name
, name_
);
3701 ret
= path_is_beyond_symlink_1(state
, &name
);
3702 strbuf_release(&name
);
3707 static int check_unsafe_path(struct patch
*patch
)
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
;
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
);
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.
3729 static int check_patch(struct apply_state
*state
, struct patch
*patch
)
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
;
3740 patch
->rejected
= 1; /* we will drop this after we succeed */
3742 status
= check_preimage(state
, patch
, &ce
, &st
);
3745 old_name
= patch
->old_name
;
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.
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
3761 if ((tpatch
= in_fn_table(state
, new_name
)) &&
3762 (was_deleted(tpatch
) || to_be_deleted(tpatch
)))
3768 ((0 < patch
->is_new
) || patch
->is_rename
|| patch
->is_copy
)) {
3769 int err
= check_to_create(state
, new_name
, ok_if_exists
);
3771 if (err
&& state
->threeway
) {
3772 patch
->direct_to_threeway
= 1;
3773 } else switch (err
) {
3776 case EXISTS_IN_INDEX
:
3777 return error(_("%s: already exists in index"), new_name
);
3779 case EXISTS_IN_WORKTREE
:
3780 return error(_("%s: already exists in working directory"),
3786 if (!patch
->new_mode
) {
3787 if (0 < patch
->is_new
)
3788 patch
->new_mode
= S_IFREG
| 0644;
3790 patch
->new_mode
= patch
->old_mode
;
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
) {
3800 return error(_("new mode (%o) of %s does not "
3801 "match old mode (%o)"),
3802 patch
->new_mode
, new_name
,
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
);
3812 if (!state
->unsafe_paths
&& check_unsafe_path(patch
))
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
3824 if (!patch
->is_delete
&& path_is_beyond_symlink(state
, patch
->new_name
))
3825 return error(_("affected file '%s' is beyond a symbolic link"),
3828 if (apply_data(state
, patch
, &st
, ce
) < 0)
3829 return error(_("%s: patch does not apply"), name
);
3830 patch
->rejected
= 0;
3834 static int check_patch_list(struct apply_state
*state
, struct patch
*patch
)
3838 prepare_symlink_changes(state
, patch
);
3839 prepare_fn_table(state
, patch
);
3842 if (state
->apply_verbosely
)
3843 say_patch_name(stderr
,
3844 _("Checking patch %s..."), patch
);
3845 res
= check_patch(state
, patch
);
3849 patch
= patch
->next
;
3854 /* This function tries to read the sha1 from the current index */
3855 static int get_current_sha1(const char *path
, unsigned char *sha1
)
3859 if (read_cache() < 0)
3861 pos
= cache_name_pos(path
, strlen(path
));
3864 hashcpy(sha1
, active_cache
[pos
]->sha1
);
3868 static int preimage_sha1_in_gitlink_patch(struct patch
*p
, unsigned char sha1
[20])
3871 * A usable gitlink patch has only one fragment (hunk) that looks like:
3873 * -Subproject commit <old sha1>
3874 * +Subproject commit <new sha1>
3877 * -Subproject commit <old sha1>
3878 * for a removal patch.
3880 struct fragment
*hunk
= p
->fragments
;
3881 static const char heading
[] = "-Subproject commit ";
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 */
3898 /* we may have full object name on the index line */
3899 return get_sha1_hex(p
->old_sha1_prefix
, sha1
);
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
)
3905 struct patch
*patch
;
3906 struct index_state result
= { NULL
};
3907 static struct lock_file lock
;
3910 /* Once we start supporting the reverse patch, it may be
3911 * worth showing the new sha1 prefix, but until then...
3913 for (patch
= list
; patch
; patch
= patch
->next
) {
3914 unsigned char sha1
[20];
3915 struct cache_entry
*ce
;
3918 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
3919 if (0 < patch
->is_new
)
3922 if (S_ISGITLINK(patch
->old_mode
)) {
3923 if (!preimage_sha1_in_gitlink_patch(patch
, sha1
))
3924 ; /* ok, the textual part looks sane */
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
)) {
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
);
3936 return error("sha1 information is lacking or useless "
3939 ce
= make_cache_entry(patch
->old_mode
, sha1
, name
, 0, 0);
3941 return error(_("make_cache_entry failed for path '%s'"),
3943 if (add_index_entry(&result
, ce
, ADD_CACHE_OK_TO_ADD
)) {
3945 return error("Could not add %s to temporary index",
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
);
3955 return error("Could not write temporary index to %s", filename
);
3960 static void stat_patch_list(struct apply_state
*state
, struct patch
*patch
)
3962 int files
, adds
, dels
;
3964 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
3966 adds
+= patch
->lines_added
;
3967 dels
+= patch
->lines_deleted
;
3968 show_stats(state
, patch
);
3971 print_stat_summary(stdout
, files
, adds
, dels
);
3974 static void numstat_patch_list(struct apply_state
*state
,
3975 struct patch
*patch
)
3977 for ( ; patch
; patch
= patch
->next
) {
3979 name
= patch
->new_name
? patch
->new_name
: patch
->old_name
;
3980 if (patch
->is_binary
)
3983 printf("%d\t%d\t", patch
->lines_added
, patch
->lines_deleted
);
3984 write_name_quoted(name
, stdout
, state
->line_termination
);
3988 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
3991 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
3993 printf(" %s %s\n", newdelete
, name
);
3996 static void show_mode_change(struct patch
*p
, int show_name
)
3998 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
4000 printf(" mode change %06o => %06o %s\n",
4001 p
->old_mode
, p
->new_mode
, p
->new_name
);
4003 printf(" mode change %06o => %06o\n",
4004 p
->old_mode
, p
->new_mode
);
4008 static void show_rename_copy(struct patch
*p
)
4010 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
4011 const char *old
, *new;
4013 /* Find common prefix */
4017 const char *slash_old
, *slash_new
;
4018 slash_old
= strchr(old
, '/');
4019 slash_new
= strchr(new, '/');
4022 slash_old
- old
!= slash_new
- new ||
4023 memcmp(old
, new, slash_new
- new))
4025 old
= slash_old
+ 1;
4026 new = slash_new
+ 1;
4028 /* p->old_name thru old is the common prefix, and old and new
4029 * through the end of names are renames
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
);
4036 printf(" %s %s => %s (%d%%)\n", renamecopy
,
4037 p
->old_name
, p
->new_name
, p
->score
);
4038 show_mode_change(p
, 0);
4041 static void summary_patch_list(struct patch
*patch
)
4045 for (p
= patch
; p
; p
= p
->next
) {
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
);
4051 if (p
->is_rename
|| p
->is_copy
)
4052 show_rename_copy(p
);
4055 printf(" rewrite %s (%d%%)\n",
4056 p
->new_name
, p
->score
);
4057 show_mode_change(p
, 0);
4060 show_mode_change(p
, 1);
4066 static void patch_stats(struct apply_state
*state
, struct patch
*patch
)
4068 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
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);
4075 len
= strlen(patch
->old_name
);
4076 if (len
> state
->max_len
)
4077 state
->max_len
= len
;
4079 if (patch
->new_name
) {
4080 int len
= quote_c_style(patch
->new_name
, NULL
, NULL
, 0);
4082 len
= strlen(patch
->new_name
);
4083 if (len
> state
->max_len
)
4084 state
->max_len
= len
;
4088 static int remove_file(struct apply_state
*state
, struct patch
*patch
, int rmdir_empty
)
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
);
4094 if (!state
->cached
) {
4095 if (!remove_or_warn(patch
->old_mode
, patch
->old_name
) && rmdir_empty
) {
4096 remove_path(patch
->old_name
);
4102 static int add_index_file(struct apply_state
*state
,
4109 struct cache_entry
*ce
;
4110 int namelen
= strlen(path
);
4111 unsigned ce_size
= cache_entry_size(namelen
);
4113 if (!state
->update_index
)
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
)) {
4124 if (!skip_prefix(buf
, "Subproject commit ", &s
) ||
4125 get_sha1_hex(s
, ce
->sha1
)) {
4127 return error(_("corrupt patch for submodule %s"), path
);
4130 if (!state
->cached
) {
4131 if (lstat(path
, &st
) < 0) {
4133 return error(_("unable to stat newly "
4134 "created file '%s': %s"),
4135 path
, strerror(errno
));
4137 fill_stat_cache_info(ce
, &st
);
4139 if (write_sha1_file(buf
, size
, blob_type
, ce
->sha1
) < 0) {
4141 return error(_("unable to create backing store "
4142 "for newly created file %s"), path
);
4145 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0) {
4147 return error(_("unable to add cache entry for %s"), path
);
4153 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
4156 struct strbuf nbuf
= STRBUF_INIT
;
4158 if (S_ISGITLINK(mode
)) {
4160 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
))
4162 return mkdir(path
, 0777);
4165 if (has_symlinks
&& S_ISLNK(mode
))
4166 /* Although buf:size is counted string, it also is NUL
4169 return symlink(buf
, path
);
4171 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, (mode
& 0100) ? 0777 : 0666);
4175 if (convert_to_working_tree(path
, buf
, size
, &nbuf
)) {
4179 write_or_die(fd
, buf
, size
);
4180 strbuf_release(&nbuf
);
4183 die_errno(_("closing file '%s'"), path
);
4188 * We optimistically assume that the directories exist,
4189 * which is true 99% of the time anyway. If they don't,
4190 * we create them and try again.
4192 static void create_one_file(struct apply_state
*state
,
4200 if (!try_create_file(path
, mode
, buf
, size
))
4203 if (errno
== ENOENT
) {
4204 if (safe_create_leading_directories(path
))
4206 if (!try_create_file(path
, mode
, buf
, size
))
4210 if (errno
== EEXIST
|| errno
== EACCES
) {
4211 /* We may be trying to create a file where a directory
4215 if (!lstat(path
, &st
) && (!S_ISDIR(st
.st_mode
) || !rmdir(path
)))
4219 if (errno
== EEXIST
) {
4220 unsigned int nr
= getpid();
4223 char newpath
[PATH_MAX
];
4224 mksnpath(newpath
, sizeof(newpath
), "%s~%u", path
, nr
);
4225 if (!try_create_file(newpath
, mode
, buf
, size
)) {
4226 if (!rename(newpath
, path
))
4228 unlink_or_warn(newpath
);
4231 if (errno
!= EEXIST
)
4236 die_errno(_("unable to write file '%s' mode %o"), path
, mode
);
4239 static int add_conflicted_stages_file(struct apply_state
*state
,
4240 struct patch
*patch
)
4243 unsigned ce_size
, mode
;
4244 struct cache_entry
*ce
;
4246 if (!state
->update_index
)
4248 namelen
= strlen(patch
->new_name
);
4249 ce_size
= cache_entry_size(namelen
);
4250 mode
= patch
->new_mode
? patch
->new_mode
: (S_IFREG
| 0644);
4252 remove_file_from_cache(patch
->new_name
);
4253 for (stage
= 1; stage
< 4; stage
++) {
4254 if (is_null_oid(&patch
->threeway_stage
[stage
- 1]))
4256 ce
= xcalloc(1, ce_size
);
4257 memcpy(ce
->name
, patch
->new_name
, namelen
);
4258 ce
->ce_mode
= create_ce_mode(mode
);
4259 ce
->ce_flags
= create_ce_flags(stage
);
4260 ce
->ce_namelen
= namelen
;
4261 hashcpy(ce
->sha1
, patch
->threeway_stage
[stage
- 1].hash
);
4262 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0) {
4264 return error(_("unable to add cache entry for %s"),
4272 static int create_file(struct apply_state
*state
, struct patch
*patch
)
4274 char *path
= patch
->new_name
;
4275 unsigned mode
= patch
->new_mode
;
4276 unsigned long size
= patch
->resultsize
;
4277 char *buf
= patch
->result
;
4280 mode
= S_IFREG
| 0644;
4281 create_one_file(state
, path
, mode
, buf
, size
);
4283 if (patch
->conflicted_threeway
)
4284 return add_conflicted_stages_file(state
, patch
);
4286 return add_index_file(state
, path
, mode
, buf
, size
);
4289 /* phase zero is to remove, phase one is to create */
4290 static int write_out_one_result(struct apply_state
*state
,
4291 struct patch
*patch
,
4294 if (patch
->is_delete
> 0) {
4296 return remove_file(state
, patch
, 1);
4299 if (patch
->is_new
> 0 || patch
->is_copy
) {
4301 return create_file(state
, patch
);
4305 * Rename or modification boils down to the same
4306 * thing: remove the old, write the new
4309 return remove_file(state
, patch
, patch
->is_rename
);
4311 return create_file(state
, patch
);
4315 static int write_out_one_reject(struct apply_state
*state
, struct patch
*patch
)
4318 char namebuf
[PATH_MAX
];
4319 struct fragment
*frag
;
4321 struct strbuf sb
= STRBUF_INIT
;
4323 for (cnt
= 0, frag
= patch
->fragments
; frag
; frag
= frag
->next
) {
4324 if (!frag
->rejected
)
4330 if (state
->apply_verbosely
)
4331 say_patch_name(stderr
,
4332 _("Applied patch %s cleanly."), patch
);
4336 /* This should not happen, because a removal patch that leaves
4337 * contents are marked "rejected" at the patch level.
4339 if (!patch
->new_name
)
4340 die(_("internal error"));
4342 /* Say this even without --verbose */
4343 strbuf_addf(&sb
, Q_("Applying patch %%s with %d reject...",
4344 "Applying patch %%s with %d rejects...",
4347 say_patch_name(stderr
, sb
.buf
, patch
);
4348 strbuf_release(&sb
);
4350 cnt
= strlen(patch
->new_name
);
4351 if (ARRAY_SIZE(namebuf
) <= cnt
+ 5) {
4352 cnt
= ARRAY_SIZE(namebuf
) - 5;
4353 warning(_("truncating .rej filename to %.*s.rej"),
4354 cnt
- 1, patch
->new_name
);
4356 memcpy(namebuf
, patch
->new_name
, cnt
);
4357 memcpy(namebuf
+ cnt
, ".rej", 5);
4359 rej
= fopen(namebuf
, "w");
4361 return error(_("cannot open %s: %s"), namebuf
, strerror(errno
));
4363 /* Normal git tools never deal with .rej, so do not pretend
4364 * this is a git patch by saying --git or giving extended
4365 * headers. While at it, maybe please "kompare" that wants
4366 * the trailing TAB and some garbage at the end of line ;-).
4368 fprintf(rej
, "diff a/%s b/%s\t(rejected hunks)\n",
4369 patch
->new_name
, patch
->new_name
);
4370 for (cnt
= 1, frag
= patch
->fragments
;
4372 cnt
++, frag
= frag
->next
) {
4373 if (!frag
->rejected
) {
4374 fprintf_ln(stderr
, _("Hunk #%d applied cleanly."), cnt
);
4377 fprintf_ln(stderr
, _("Rejected hunk #%d."), cnt
);
4378 fprintf(rej
, "%.*s", frag
->size
, frag
->patch
);
4379 if (frag
->patch
[frag
->size
-1] != '\n')
4388 * -1 if an error happened
4389 * 0 if the patch applied cleanly
4390 * 1 if the patch did not apply cleanly
4392 static int write_out_results(struct apply_state
*state
, struct patch
*list
)
4397 struct string_list cpath
= STRING_LIST_INIT_DUP
;
4399 for (phase
= 0; phase
< 2; phase
++) {
4405 if (write_out_one_result(state
, l
, phase
)) {
4406 string_list_clear(&cpath
, 0);
4410 if (write_out_one_reject(state
, l
))
4412 if (l
->conflicted_threeway
) {
4413 string_list_append(&cpath
, l
->new_name
);
4423 struct string_list_item
*item
;
4425 string_list_sort(&cpath
);
4426 for_each_string_list_item(item
, &cpath
)
4427 fprintf(stderr
, "U %s\n", item
->string
);
4428 string_list_clear(&cpath
, 0);
4436 static struct lock_file lock_file
;
4438 #define INACCURATE_EOF (1<<0)
4439 #define RECOUNT (1<<1)
4442 * Try to apply a patch.
4445 * -128 if a bad error happened (like patch unreadable)
4446 * -1 if patch did not apply and user cannot deal with it
4447 * 0 if the patch applied
4448 * 1 if the patch did not apply but user might fix it
4450 static int apply_patch(struct apply_state
*state
,
4452 const char *filename
,
4456 struct strbuf buf
= STRBUF_INIT
; /* owns the patch text */
4457 struct patch
*list
= NULL
, **listp
= &list
;
4458 int skipped_patch
= 0;
4461 state
->patch_input_file
= filename
;
4462 if (read_patch_file(&buf
, fd
) < 0)
4465 while (offset
< buf
.len
) {
4466 struct patch
*patch
;
4469 patch
= xcalloc(1, sizeof(*patch
));
4470 patch
->inaccurate_eof
= !!(options
& INACCURATE_EOF
);
4471 patch
->recount
= !!(options
& RECOUNT
);
4472 nr
= parse_chunk(state
, buf
.buf
+ offset
, buf
.len
- offset
, patch
);
4481 if (state
->apply_in_reverse
)
4482 reverse_patches(patch
);
4483 if (use_patch(state
, patch
)) {
4484 patch_stats(state
, patch
);
4486 listp
= &patch
->next
;
4489 if (state
->apply_verbosely
)
4490 say_patch_name(stderr
, _("Skipped patch '%s'."), patch
);
4497 if (!list
&& !skipped_patch
) {
4498 error(_("unrecognized input"));
4503 if (state
->whitespace_error
&& (state
->ws_error_action
== die_on_ws_error
))
4506 state
->update_index
= state
->check_index
&& state
->apply
;
4507 if (state
->update_index
&& state
->newfd
< 0)
4508 state
->newfd
= hold_locked_index(state
->lock_file
, 1);
4510 if (state
->check_index
&& read_cache() < 0) {
4511 error(_("unable to read index file"));
4516 if (state
->check
|| state
->apply
) {
4517 int r
= check_patch_list(state
, list
);
4522 if (r
< 0 && !state
->apply_with_reject
) {
4529 int write_res
= write_out_results(state
, list
);
4530 if (write_res
< 0) {
4534 if (write_res
> 0) {
4535 /* with --3way, we still need to write the index out */
4536 res
= state
->apply_with_reject
? -1 : 1;
4541 if (state
->fake_ancestor
&&
4542 build_fake_ancestor(list
, state
->fake_ancestor
)) {
4547 if (state
->diffstat
)
4548 stat_patch_list(state
, list
);
4551 numstat_patch_list(state
, list
);
4554 summary_patch_list(list
);
4557 free_patch_list(list
);
4558 strbuf_release(&buf
);
4559 string_list_clear(&state
->fn_table
, 0);
4563 static int option_parse_exclude(const struct option
*opt
,
4564 const char *arg
, int unset
)
4566 struct apply_state
*state
= opt
->value
;
4567 add_name_limit(state
, arg
, 1);
4571 static int option_parse_include(const struct option
*opt
,
4572 const char *arg
, int unset
)
4574 struct apply_state
*state
= opt
->value
;
4575 add_name_limit(state
, arg
, 0);
4576 state
->has_include
= 1;
4580 static int option_parse_p(const struct option
*opt
,
4584 struct apply_state
*state
= opt
->value
;
4585 state
->p_value
= atoi(arg
);
4586 state
->p_value_known
= 1;
4590 static int option_parse_space_change(const struct option
*opt
,
4591 const char *arg
, int unset
)
4593 struct apply_state
*state
= opt
->value
;
4595 state
->ws_ignore_action
= ignore_ws_none
;
4597 state
->ws_ignore_action
= ignore_ws_change
;
4601 static int option_parse_whitespace(const struct option
*opt
,
4602 const char *arg
, int unset
)
4604 struct apply_state
*state
= opt
->value
;
4605 state
->whitespace_option
= arg
;
4606 if (parse_whitespace_option(state
, arg
))
4611 static int option_parse_directory(const struct option
*opt
,
4612 const char *arg
, int unset
)
4614 struct apply_state
*state
= opt
->value
;
4615 strbuf_reset(&state
->root
);
4616 strbuf_addstr(&state
->root
, arg
);
4617 strbuf_complete(&state
->root
, '/');
4621 static int apply_all_patches(struct apply_state
*state
,
4631 for (i
= 0; i
< argc
; i
++) {
4632 const char *arg
= argv
[i
];
4635 if (!strcmp(arg
, "-")) {
4636 res
= apply_patch(state
, 0, "<stdin>", options
);
4642 } else if (0 < state
->prefix_length
)
4643 arg
= prefix_filename(state
->prefix
,
4644 state
->prefix_length
,
4647 fd
= open(arg
, O_RDONLY
);
4649 error(_("can't open patch '%s': %s"), arg
, strerror(errno
));
4654 set_default_whitespace_mode(state
);
4655 res
= apply_patch(state
, fd
, arg
, options
);
4661 set_default_whitespace_mode(state
);
4663 res
= apply_patch(state
, 0, "<stdin>", options
);
4669 if (state
->whitespace_error
) {
4670 if (state
->squelch_whitespace_errors
&&
4671 state
->squelch_whitespace_errors
< state
->whitespace_error
) {
4673 state
->whitespace_error
- state
->squelch_whitespace_errors
;
4674 warning(Q_("squelched %d whitespace error",
4675 "squelched %d whitespace errors",
4679 if (state
->ws_error_action
== die_on_ws_error
) {
4680 error(Q_("%d line adds whitespace errors.",
4681 "%d lines add whitespace errors.",
4682 state
->whitespace_error
),
4683 state
->whitespace_error
);
4687 if (state
->applied_after_fixing_ws
&& state
->apply
)
4688 warning("%d line%s applied after"
4689 " fixing whitespace errors.",
4690 state
->applied_after_fixing_ws
,
4691 state
->applied_after_fixing_ws
== 1 ? "" : "s");
4692 else if (state
->whitespace_error
)
4693 warning(Q_("%d line adds whitespace errors.",
4694 "%d lines add whitespace errors.",
4695 state
->whitespace_error
),
4696 state
->whitespace_error
);
4699 if (state
->update_index
) {
4700 res
= write_locked_index(&the_index
, state
->lock_file
, COMMIT_LOCK
);
4702 error(_("Unable to write new index file"));
4712 if (state
->newfd
>= 0) {
4713 rollback_lock_file(state
->lock_file
);
4717 return (res
== -1 ? 1 : 128);
4720 int cmd_apply(int argc
, const char **argv
, const char *prefix
)
4722 int force_apply
= 0;
4725 struct apply_state state
;
4727 struct option builtin_apply_options
[] = {
4728 { OPTION_CALLBACK
, 0, "exclude", &state
, N_("path"),
4729 N_("don't apply changes matching the given path"),
4730 0, option_parse_exclude
},
4731 { OPTION_CALLBACK
, 0, "include", &state
, N_("path"),
4732 N_("apply changes matching the given path"),
4733 0, option_parse_include
},
4734 { OPTION_CALLBACK
, 'p', NULL
, &state
, N_("num"),
4735 N_("remove <num> leading slashes from traditional diff paths"),
4736 0, option_parse_p
},
4737 OPT_BOOL(0, "no-add", &state
.no_add
,
4738 N_("ignore additions made by the patch")),
4739 OPT_BOOL(0, "stat", &state
.diffstat
,
4740 N_("instead of applying the patch, output diffstat for the input")),
4741 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4742 OPT_NOOP_NOARG(0, "binary"),
4743 OPT_BOOL(0, "numstat", &state
.numstat
,
4744 N_("show number of added and deleted lines in decimal notation")),
4745 OPT_BOOL(0, "summary", &state
.summary
,
4746 N_("instead of applying the patch, output a summary for the input")),
4747 OPT_BOOL(0, "check", &state
.check
,
4748 N_("instead of applying the patch, see if the patch is applicable")),
4749 OPT_BOOL(0, "index", &state
.check_index
,
4750 N_("make sure the patch is applicable to the current index")),
4751 OPT_BOOL(0, "cached", &state
.cached
,
4752 N_("apply a patch without touching the working tree")),
4753 OPT_BOOL(0, "unsafe-paths", &state
.unsafe_paths
,
4754 N_("accept a patch that touches outside the working area")),
4755 OPT_BOOL(0, "apply", &force_apply
,
4756 N_("also apply the patch (use with --stat/--summary/--check)")),
4757 OPT_BOOL('3', "3way", &state
.threeway
,
4758 N_( "attempt three-way merge if a patch does not apply")),
4759 OPT_FILENAME(0, "build-fake-ancestor", &state
.fake_ancestor
,
4760 N_("build a temporary index based on embedded index information")),
4761 /* Think twice before adding "--nul" synonym to this */
4762 OPT_SET_INT('z', NULL
, &state
.line_termination
,
4763 N_("paths are separated with NUL character"), '\0'),
4764 OPT_INTEGER('C', NULL
, &state
.p_context
,
4765 N_("ensure at least <n> lines of context match")),
4766 { OPTION_CALLBACK
, 0, "whitespace", &state
, N_("action"),
4767 N_("detect new or modified lines that have whitespace errors"),
4768 0, option_parse_whitespace
},
4769 { OPTION_CALLBACK
, 0, "ignore-space-change", &state
, NULL
,
4770 N_("ignore changes in whitespace when finding context"),
4771 PARSE_OPT_NOARG
, option_parse_space_change
},
4772 { OPTION_CALLBACK
, 0, "ignore-whitespace", &state
, NULL
,
4773 N_("ignore changes in whitespace when finding context"),
4774 PARSE_OPT_NOARG
, option_parse_space_change
},
4775 OPT_BOOL('R', "reverse", &state
.apply_in_reverse
,
4776 N_("apply the patch in reverse")),
4777 OPT_BOOL(0, "unidiff-zero", &state
.unidiff_zero
,
4778 N_("don't expect at least one line of context")),
4779 OPT_BOOL(0, "reject", &state
.apply_with_reject
,
4780 N_("leave the rejected hunks in corresponding *.rej files")),
4781 OPT_BOOL(0, "allow-overlap", &state
.allow_overlap
,
4782 N_("allow overlapping hunks")),
4783 OPT__VERBOSE(&state
.apply_verbosely
, N_("be verbose")),
4784 OPT_BIT(0, "inaccurate-eof", &options
,
4785 N_("tolerate incorrectly detected missing new-line at the end of file"),
4787 OPT_BIT(0, "recount", &options
,
4788 N_("do not trust the line counts in the hunk headers"),
4790 { OPTION_CALLBACK
, 0, "directory", &state
, N_("root"),
4791 N_("prepend <root> to all filenames"),
4792 0, option_parse_directory
},
4796 if (init_apply_state(&state
, prefix
, &lock_file
))
4799 argc
= parse_options(argc
, argv
, state
.prefix
, builtin_apply_options
,
4802 if (check_apply_state(&state
, force_apply
))
4805 ret
= apply_all_patches(&state
, argc
, argv
, options
);
4807 clear_apply_state(&state
);