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"
24 enum ws_error_action
{
38 * We need to keep track of how symlinks in the preimage are
39 * manipulated by the patches. A patch to add a/b/c where a/b
40 * is a symlink should not be allowed to affect the directory
41 * the symlink points at, but if the same patch removes a/b,
42 * it is perfectly fine, as the patch removes a/b to make room
43 * to create a directory a/b so that a/b/c can be created.
45 * See also "struct string_list symlink_changes" in "struct
48 #define SYMLINK_GOES_AWAY 01
49 #define SYMLINK_IN_RESULT 02
55 /* These are lock_file related */
56 struct lock_file
*lock_file
;
59 /* These control what gets looked at and modified */
60 int apply
; /* this is not a dry-run */
61 int cached
; /* apply to the index only */
62 int check
; /* preimage must match working tree, don't actually apply */
63 int check_index
; /* preimage must match the indexed version */
64 int update_index
; /* check_index && apply */
66 /* These control cosmetic aspect of the output */
67 int diffstat
; /* just show a diffstat, and don't actually apply */
68 int numstat
; /* just show a numeric diffstat, and don't actually apply */
69 int summary
; /* just report creation, deletion, etc, and don't actually apply */
71 /* These boolean parameters control how the apply is done */
74 int apply_with_reject
;
81 /* Other non boolean parameters */
82 const char *fake_ancestor
;
83 const char *patch_input_file
;
88 unsigned int p_context
;
90 /* Exclude and include path parameters */
91 struct string_list limit_by_name
;
94 /* Various "current state" */
95 int linenr
; /* current line number */
96 struct string_list symlink_changes
; /* we have to track symlinks */
99 * For "diff-stat" like behaviour, we keep track of the biggest change
100 * we've seen, and the longest filename. That allows us to do simple
107 * Records filenames that have been touched, in order to handle
108 * the case where more than one patches touch the same file.
110 struct string_list fn_table
;
112 /* These control whitespace errors */
113 enum ws_error_action ws_error_action
;
114 enum ws_ignore ws_ignore_action
;
115 const char *whitespace_option
;
116 int whitespace_error
;
117 int squelch_whitespace_errors
;
118 int applied_after_fixing_ws
;
121 static const char * const apply_usage
[] = {
122 N_("git apply [<options>] [<patch>...]"),
126 static void parse_whitespace_option(struct apply_state
*state
, const char *option
)
129 state
->ws_error_action
= warn_on_ws_error
;
132 if (!strcmp(option
, "warn")) {
133 state
->ws_error_action
= warn_on_ws_error
;
136 if (!strcmp(option
, "nowarn")) {
137 state
->ws_error_action
= nowarn_ws_error
;
140 if (!strcmp(option
, "error")) {
141 state
->ws_error_action
= die_on_ws_error
;
144 if (!strcmp(option
, "error-all")) {
145 state
->ws_error_action
= die_on_ws_error
;
146 state
->squelch_whitespace_errors
= 0;
149 if (!strcmp(option
, "strip") || !strcmp(option
, "fix")) {
150 state
->ws_error_action
= correct_ws_error
;
153 die(_("unrecognized whitespace option '%s'"), option
);
156 static void parse_ignorewhitespace_option(struct apply_state
*state
,
159 if (!option
|| !strcmp(option
, "no") ||
160 !strcmp(option
, "false") || !strcmp(option
, "never") ||
161 !strcmp(option
, "none")) {
162 state
->ws_ignore_action
= ignore_ws_none
;
165 if (!strcmp(option
, "change")) {
166 state
->ws_ignore_action
= ignore_ws_change
;
169 die(_("unrecognized whitespace ignore option '%s'"), option
);
172 static void set_default_whitespace_mode(struct apply_state
*state
)
174 if (!state
->whitespace_option
&& !apply_default_whitespace
)
175 state
->ws_error_action
= (state
->apply
? warn_on_ws_error
: nowarn_ws_error
);
179 * This represents one "hunk" from a patch, starting with
180 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
181 * patch text is pointed at by patch, and its byte length
182 * is stored in size. leading and trailing are the number
186 unsigned long leading
, trailing
;
187 unsigned long oldpos
, oldlines
;
188 unsigned long newpos
, newlines
;
190 * 'patch' is usually borrowed from buf in apply_patch(),
191 * but some codepaths store an allocated buffer.
194 unsigned free_patch
:1,
198 struct fragment
*next
;
202 * When dealing with a binary patch, we reuse "leading" field
203 * to store the type of the binary hunk, either deflated "delta"
204 * or deflated "literal".
206 #define binary_patch_method leading
207 #define BINARY_DELTA_DEFLATED 1
208 #define BINARY_LITERAL_DEFLATED 2
211 * This represents a "patch" to a file, both metainfo changes
212 * such as creation/deletion, filemode and content changes represented
213 * as a series of fragments.
216 char *new_name
, *old_name
, *def_name
;
217 unsigned int old_mode
, new_mode
;
218 int is_new
, is_delete
; /* -1 = unknown, 0 = false, 1 = true */
221 int lines_added
, lines_deleted
;
223 unsigned int is_toplevel_relative
:1;
224 unsigned int inaccurate_eof
:1;
225 unsigned int is_binary
:1;
226 unsigned int is_copy
:1;
227 unsigned int is_rename
:1;
228 unsigned int recount
:1;
229 unsigned int conflicted_threeway
:1;
230 unsigned int direct_to_threeway
:1;
231 struct fragment
*fragments
;
234 char old_sha1_prefix
[41];
235 char new_sha1_prefix
[41];
238 /* three-way fallback result */
239 struct object_id threeway_stage
[3];
242 static void free_fragment_list(struct fragment
*list
)
245 struct fragment
*next
= list
->next
;
246 if (list
->free_patch
)
247 free((char *)list
->patch
);
253 static void free_patch(struct patch
*patch
)
255 free_fragment_list(patch
->fragments
);
256 free(patch
->def_name
);
257 free(patch
->old_name
);
258 free(patch
->new_name
);
263 static void free_patch_list(struct patch
*list
)
266 struct patch
*next
= list
->next
;
273 * A line in a file, len-bytes long (includes the terminating LF,
274 * except for an incomplete line at the end if the file ends with
275 * one), and its contents hashes to 'hash'.
281 #define LINE_COMMON 1
282 #define LINE_PATCHED 2
286 * This represents a "file", which is an array of "lines".
293 struct line
*line_allocated
;
297 static uint32_t hash_line(const char *cp
, size_t len
)
301 for (i
= 0, h
= 0; i
< len
; i
++) {
302 if (!isspace(cp
[i
])) {
303 h
= h
* 3 + (cp
[i
] & 0xff);
310 * Compare lines s1 of length n1 and s2 of length n2, ignoring
311 * whitespace difference. Returns 1 if they match, 0 otherwise
313 static int fuzzy_matchlines(const char *s1
, size_t n1
,
314 const char *s2
, size_t n2
)
316 const char *last1
= s1
+ n1
- 1;
317 const char *last2
= s2
+ n2
- 1;
320 /* ignore line endings */
321 while ((*last1
== '\r') || (*last1
== '\n'))
323 while ((*last2
== '\r') || (*last2
== '\n'))
326 /* skip leading whitespaces, if both begin with whitespace */
327 if (s1
<= last1
&& s2
<= last2
&& isspace(*s1
) && isspace(*s2
)) {
328 while (isspace(*s1
) && (s1
<= last1
))
330 while (isspace(*s2
) && (s2
<= last2
))
333 /* early return if both lines are empty */
334 if ((s1
> last1
) && (s2
> last2
))
337 result
= *s1
++ - *s2
++;
339 * Skip whitespace inside. We check for whitespace on
340 * both buffers because we don't want "a b" to match
343 if (isspace(*s1
) && isspace(*s2
)) {
344 while (isspace(*s1
) && s1
<= last1
)
346 while (isspace(*s2
) && s2
<= last2
)
350 * If we reached the end on one side only,
354 ((s2
> last2
) && (s1
<= last1
)) ||
355 ((s1
> last1
) && (s2
<= last2
)))
357 if ((s1
> last1
) && (s2
> last2
))
364 static void add_line_info(struct image
*img
, const char *bol
, size_t len
, unsigned flag
)
366 ALLOC_GROW(img
->line_allocated
, img
->nr
+ 1, img
->alloc
);
367 img
->line_allocated
[img
->nr
].len
= len
;
368 img
->line_allocated
[img
->nr
].hash
= hash_line(bol
, len
);
369 img
->line_allocated
[img
->nr
].flag
= flag
;
374 * "buf" has the file contents to be patched (read from various sources).
375 * attach it to "image" and add line-based index to it.
376 * "image" now owns the "buf".
378 static void prepare_image(struct image
*image
, char *buf
, size_t len
,
379 int prepare_linetable
)
383 memset(image
, 0, sizeof(*image
));
387 if (!prepare_linetable
)
390 ep
= image
->buf
+ image
->len
;
394 for (next
= cp
; next
< ep
&& *next
!= '\n'; next
++)
398 add_line_info(image
, cp
, next
- cp
, 0);
401 image
->line
= image
->line_allocated
;
404 static void clear_image(struct image
*image
)
407 free(image
->line_allocated
);
408 memset(image
, 0, sizeof(*image
));
411 /* fmt must contain _one_ %s and no other substitution */
412 static void say_patch_name(FILE *output
, const char *fmt
, struct patch
*patch
)
414 struct strbuf sb
= STRBUF_INIT
;
416 if (patch
->old_name
&& patch
->new_name
&&
417 strcmp(patch
->old_name
, patch
->new_name
)) {
418 quote_c_style(patch
->old_name
, &sb
, NULL
, 0);
419 strbuf_addstr(&sb
, " => ");
420 quote_c_style(patch
->new_name
, &sb
, NULL
, 0);
422 const char *n
= patch
->new_name
;
425 quote_c_style(n
, &sb
, NULL
, 0);
427 fprintf(output
, fmt
, sb
.buf
);
434 static void read_patch_file(struct strbuf
*sb
, int fd
)
436 if (strbuf_read(sb
, fd
, 0) < 0)
437 die_errno("git apply: failed to read");
440 * Make sure that we have some slop in the buffer
441 * so that we can do speculative "memcmp" etc, and
442 * see to it that it is NUL-filled.
444 strbuf_grow(sb
, SLOP
);
445 memset(sb
->buf
+ sb
->len
, 0, SLOP
);
448 static unsigned long linelen(const char *buffer
, unsigned long size
)
450 unsigned long len
= 0;
453 if (*buffer
++ == '\n')
459 static int is_dev_null(const char *str
)
461 return skip_prefix(str
, "/dev/null", &str
) && isspace(*str
);
467 static int name_terminate(int c
, int terminate
)
469 if (c
== ' ' && !(terminate
& TERM_SPACE
))
471 if (c
== '\t' && !(terminate
& TERM_TAB
))
477 /* remove double slashes to make --index work with such filenames */
478 static char *squash_slash(char *name
)
486 if ((name
[j
++] = name
[i
++]) == '/')
487 while (name
[i
] == '/')
494 static char *find_name_gnu(struct apply_state
*state
,
499 struct strbuf name
= STRBUF_INIT
;
503 * Proposed "new-style" GNU patch/diff format; see
504 * http://marc.info/?l=git&m=112927316408690&w=2
506 if (unquote_c_style(&name
, line
, NULL
)) {
507 strbuf_release(&name
);
511 for (cp
= name
.buf
; p_value
; p_value
--) {
512 cp
= strchr(cp
, '/');
514 strbuf_release(&name
);
520 strbuf_remove(&name
, 0, cp
- name
.buf
);
522 strbuf_insert(&name
, 0, state
->root
.buf
, state
->root
.len
);
523 return squash_slash(strbuf_detach(&name
, NULL
));
526 static size_t sane_tz_len(const char *line
, size_t len
)
530 if (len
< strlen(" +0500") || line
[len
-strlen(" +0500")] != ' ')
532 tz
= line
+ len
- strlen(" +0500");
534 if (tz
[1] != '+' && tz
[1] != '-')
537 for (p
= tz
+ 2; p
!= line
+ len
; p
++)
541 return line
+ len
- tz
;
544 static size_t tz_with_colon_len(const char *line
, size_t len
)
548 if (len
< strlen(" +08:00") || line
[len
- strlen(":00")] != ':')
550 tz
= line
+ len
- strlen(" +08:00");
552 if (tz
[0] != ' ' || (tz
[1] != '+' && tz
[1] != '-'))
555 if (!isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
556 !isdigit(*p
++) || !isdigit(*p
++))
559 return line
+ len
- tz
;
562 static size_t date_len(const char *line
, size_t len
)
564 const char *date
, *p
;
566 if (len
< strlen("72-02-05") || line
[len
-strlen("-05")] != '-')
568 p
= date
= line
+ len
- strlen("72-02-05");
570 if (!isdigit(*p
++) || !isdigit(*p
++) || *p
++ != '-' ||
571 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != '-' ||
572 !isdigit(*p
++) || !isdigit(*p
++)) /* Not a date. */
575 if (date
- line
>= strlen("19") &&
576 isdigit(date
[-1]) && isdigit(date
[-2])) /* 4-digit year */
577 date
-= strlen("19");
579 return line
+ len
- date
;
582 static size_t short_time_len(const char *line
, size_t len
)
584 const char *time
, *p
;
586 if (len
< strlen(" 07:01:32") || line
[len
-strlen(":32")] != ':')
588 p
= time
= line
+ len
- strlen(" 07:01:32");
590 /* Permit 1-digit hours? */
592 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
593 !isdigit(*p
++) || !isdigit(*p
++) || *p
++ != ':' ||
594 !isdigit(*p
++) || !isdigit(*p
++)) /* Not a time. */
597 return line
+ len
- time
;
600 static size_t fractional_time_len(const char *line
, size_t len
)
605 /* Expected format: 19:41:17.620000023 */
606 if (!len
|| !isdigit(line
[len
- 1]))
610 /* Fractional seconds. */
611 while (p
> line
&& isdigit(*p
))
616 /* Hours, minutes, and whole seconds. */
617 n
= short_time_len(line
, p
- line
);
621 return line
+ len
- p
+ n
;
624 static size_t trailing_spaces_len(const char *line
, size_t len
)
628 /* Expected format: ' ' x (1 or more) */
629 if (!len
|| line
[len
- 1] != ' ')
636 return line
+ len
- (p
+ 1);
643 static size_t diff_timestamp_len(const char *line
, size_t len
)
645 const char *end
= line
+ len
;
649 * Posix: 2010-07-05 19:41:17
650 * GNU: 2010-07-05 19:41:17.620000023 -0500
653 if (!isdigit(end
[-1]))
656 n
= sane_tz_len(line
, end
- line
);
658 n
= tz_with_colon_len(line
, end
- line
);
661 n
= short_time_len(line
, end
- line
);
663 n
= fractional_time_len(line
, end
- line
);
666 n
= date_len(line
, end
- line
);
667 if (!n
) /* No date. Too bad. */
671 if (end
== line
) /* No space before date. */
673 if (end
[-1] == '\t') { /* Success! */
675 return line
+ len
- end
;
677 if (end
[-1] != ' ') /* No space before date. */
680 /* Whitespace damage. */
681 end
-= trailing_spaces_len(line
, end
- line
);
682 return line
+ len
- end
;
685 static char *find_name_common(struct apply_state
*state
,
693 const char *start
= NULL
;
697 while (line
!= end
) {
700 if (!end
&& isspace(c
)) {
703 if (name_terminate(c
, terminate
))
707 if (c
== '/' && !--p_value
)
711 return squash_slash(xstrdup_or_null(def
));
714 return squash_slash(xstrdup_or_null(def
));
717 * Generally we prefer the shorter name, especially
718 * if the other one is just a variation of that with
719 * something else tacked on to the end (ie "file.orig"
723 int deflen
= strlen(def
);
724 if (deflen
< len
&& !strncmp(start
, def
, deflen
))
725 return squash_slash(xstrdup(def
));
728 if (state
->root
.len
) {
729 char *ret
= xstrfmt("%s%.*s", state
->root
.buf
, len
, start
);
730 return squash_slash(ret
);
733 return squash_slash(xmemdupz(start
, len
));
736 static char *find_name(struct apply_state
*state
,
743 char *name
= find_name_gnu(state
, line
, def
, p_value
);
748 return find_name_common(state
, line
, def
, p_value
, NULL
, terminate
);
751 static char *find_name_traditional(struct apply_state
*state
,
760 char *name
= find_name_gnu(state
, line
, def
, p_value
);
765 len
= strchrnul(line
, '\n') - line
;
766 date_len
= diff_timestamp_len(line
, len
);
768 return find_name_common(state
, line
, def
, p_value
, NULL
, TERM_TAB
);
771 return find_name_common(state
, line
, def
, p_value
, line
+ len
, 0);
774 static int count_slashes(const char *cp
)
786 * Given the string after "--- " or "+++ ", guess the appropriate
787 * p_value for the given patch.
789 static int guess_p_value(struct apply_state
*state
, const char *nameline
)
794 if (is_dev_null(nameline
))
796 name
= find_name_traditional(state
, nameline
, NULL
, 0);
799 cp
= strchr(name
, '/');
802 else if (state
->prefix
) {
804 * Does it begin with "a/$our-prefix" and such? Then this is
805 * very likely to apply to our directory.
807 if (!strncmp(name
, state
->prefix
, state
->prefix_length
))
808 val
= count_slashes(state
->prefix
);
811 if (!strncmp(cp
, state
->prefix
, state
->prefix_length
))
812 val
= count_slashes(state
->prefix
) + 1;
820 * Does the ---/+++ line have the POSIX timestamp after the last HT?
821 * GNU diff puts epoch there to signal a creation/deletion event. Is
822 * this such a timestamp?
824 static int has_epoch_timestamp(const char *nameline
)
827 * We are only interested in epoch timestamp; any non-zero
828 * fraction cannot be one, hence "(\.0+)?" in the regexp below.
829 * For the same reason, the date must be either 1969-12-31 or
830 * 1970-01-01, and the seconds part must be "00".
832 const char stamp_regexp
[] =
833 "^(1969-12-31|1970-01-01)"
835 "[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
837 "([-+][0-2][0-9]:?[0-5][0-9])\n";
838 const char *timestamp
= NULL
, *cp
, *colon
;
839 static regex_t
*stamp
;
845 for (cp
= nameline
; *cp
!= '\n'; cp
++) {
852 stamp
= xmalloc(sizeof(*stamp
));
853 if (regcomp(stamp
, stamp_regexp
, REG_EXTENDED
)) {
854 warning(_("Cannot prepare timestamp regexp %s"),
860 status
= regexec(stamp
, timestamp
, ARRAY_SIZE(m
), m
, 0);
862 if (status
!= REG_NOMATCH
)
863 warning(_("regexec returned %d for input: %s"),
868 zoneoffset
= strtol(timestamp
+ m
[3].rm_so
+ 1, (char **) &colon
, 10);
870 zoneoffset
= zoneoffset
* 60 + strtol(colon
+ 1, NULL
, 10);
872 zoneoffset
= (zoneoffset
/ 100) * 60 + (zoneoffset
% 100);
873 if (timestamp
[m
[3].rm_so
] == '-')
874 zoneoffset
= -zoneoffset
;
877 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
878 * (west of GMT) or 1970-01-01 (east of GMT)
880 if ((zoneoffset
< 0 && memcmp(timestamp
, "1969-12-31", 10)) ||
881 (0 <= zoneoffset
&& memcmp(timestamp
, "1970-01-01", 10)))
884 hourminute
= (strtol(timestamp
+ 11, NULL
, 10) * 60 +
885 strtol(timestamp
+ 14, NULL
, 10) -
888 return ((zoneoffset
< 0 && hourminute
== 1440) ||
889 (0 <= zoneoffset
&& !hourminute
));
893 * Get the name etc info from the ---/+++ lines of a traditional patch header
895 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
896 * files, we can happily check the index for a match, but for creating a
897 * new file we should try to match whatever "patch" does. I have no idea.
899 static void parse_traditional_patch(struct apply_state
*state
,
906 first
+= 4; /* skip "--- " */
907 second
+= 4; /* skip "+++ " */
908 if (!state
->p_value_known
) {
910 p
= guess_p_value(state
, first
);
911 q
= guess_p_value(state
, second
);
913 if (0 <= p
&& p
== q
) {
915 state
->p_value_known
= 1;
918 if (is_dev_null(first
)) {
920 patch
->is_delete
= 0;
921 name
= find_name_traditional(state
, second
, NULL
, state
->p_value
);
922 patch
->new_name
= name
;
923 } else if (is_dev_null(second
)) {
925 patch
->is_delete
= 1;
926 name
= find_name_traditional(state
, first
, NULL
, state
->p_value
);
927 patch
->old_name
= name
;
930 first_name
= find_name_traditional(state
, first
, NULL
, state
->p_value
);
931 name
= find_name_traditional(state
, second
, first_name
, state
->p_value
);
933 if (has_epoch_timestamp(first
)) {
935 patch
->is_delete
= 0;
936 patch
->new_name
= name
;
937 } else if (has_epoch_timestamp(second
)) {
939 patch
->is_delete
= 1;
940 patch
->old_name
= name
;
942 patch
->old_name
= name
;
943 patch
->new_name
= xstrdup_or_null(name
);
947 die(_("unable to find filename in patch at line %d"), state
->linenr
);
950 static int gitdiff_hdrend(struct apply_state
*state
,
958 * We're anal about diff header consistency, to make
959 * sure that we don't end up having strange ambiguous
960 * patches floating around.
962 * As a result, gitdiff_{old|new}name() will check
963 * their names against any previous information, just
966 #define DIFF_OLD_NAME 0
967 #define DIFF_NEW_NAME 1
969 static void gitdiff_verify_name(struct apply_state
*state
,
975 if (!*name
&& !isnull
) {
976 *name
= find_name(state
, line
, NULL
, state
->p_value
, TERM_TAB
);
981 int len
= strlen(*name
);
984 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
985 *name
, state
->linenr
);
986 another
= find_name(state
, line
, NULL
, state
->p_value
, TERM_TAB
);
987 if (!another
|| memcmp(another
, *name
, len
+ 1))
988 die((side
== DIFF_NEW_NAME
) ?
989 _("git apply: bad git-diff - inconsistent new filename on line %d") :
990 _("git apply: bad git-diff - inconsistent old filename on line %d"), state
->linenr
);
993 /* expect "/dev/null" */
994 if (memcmp("/dev/null", line
, 9) || line
[9] != '\n')
995 die(_("git apply: bad git-diff - expected /dev/null on line %d"), state
->linenr
);
999 static int gitdiff_oldname(struct apply_state
*state
,
1001 struct patch
*patch
)
1003 gitdiff_verify_name(state
, line
,
1004 patch
->is_new
, &patch
->old_name
,
1009 static int gitdiff_newname(struct apply_state
*state
,
1011 struct patch
*patch
)
1013 gitdiff_verify_name(state
, line
,
1014 patch
->is_delete
, &patch
->new_name
,
1019 static int gitdiff_oldmode(struct apply_state
*state
,
1021 struct patch
*patch
)
1023 patch
->old_mode
= strtoul(line
, NULL
, 8);
1027 static int gitdiff_newmode(struct apply_state
*state
,
1029 struct patch
*patch
)
1031 patch
->new_mode
= strtoul(line
, NULL
, 8);
1035 static int gitdiff_delete(struct apply_state
*state
,
1037 struct patch
*patch
)
1039 patch
->is_delete
= 1;
1040 free(patch
->old_name
);
1041 patch
->old_name
= xstrdup_or_null(patch
->def_name
);
1042 return gitdiff_oldmode(state
, line
, patch
);
1045 static int gitdiff_newfile(struct apply_state
*state
,
1047 struct patch
*patch
)
1050 free(patch
->new_name
);
1051 patch
->new_name
= xstrdup_or_null(patch
->def_name
);
1052 return gitdiff_newmode(state
, line
, patch
);
1055 static int gitdiff_copysrc(struct apply_state
*state
,
1057 struct patch
*patch
)
1060 free(patch
->old_name
);
1061 patch
->old_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
1065 static int gitdiff_copydst(struct apply_state
*state
,
1067 struct patch
*patch
)
1070 free(patch
->new_name
);
1071 patch
->new_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
1075 static int gitdiff_renamesrc(struct apply_state
*state
,
1077 struct patch
*patch
)
1079 patch
->is_rename
= 1;
1080 free(patch
->old_name
);
1081 patch
->old_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
1085 static int gitdiff_renamedst(struct apply_state
*state
,
1087 struct patch
*patch
)
1089 patch
->is_rename
= 1;
1090 free(patch
->new_name
);
1091 patch
->new_name
= find_name(state
, line
, NULL
, state
->p_value
? state
->p_value
- 1 : 0, 0);
1095 static int gitdiff_similarity(struct apply_state
*state
,
1097 struct patch
*patch
)
1099 unsigned long val
= strtoul(line
, NULL
, 10);
1105 static int gitdiff_dissimilarity(struct apply_state
*state
,
1107 struct patch
*patch
)
1109 unsigned long val
= strtoul(line
, NULL
, 10);
1115 static int gitdiff_index(struct apply_state
*state
,
1117 struct patch
*patch
)
1120 * index line is N hexadecimal, "..", N hexadecimal,
1121 * and optional space with octal mode.
1123 const char *ptr
, *eol
;
1126 ptr
= strchr(line
, '.');
1127 if (!ptr
|| ptr
[1] != '.' || 40 < ptr
- line
)
1130 memcpy(patch
->old_sha1_prefix
, line
, len
);
1131 patch
->old_sha1_prefix
[len
] = 0;
1134 ptr
= strchr(line
, ' ');
1135 eol
= strchrnul(line
, '\n');
1137 if (!ptr
|| eol
< ptr
)
1143 memcpy(patch
->new_sha1_prefix
, line
, len
);
1144 patch
->new_sha1_prefix
[len
] = 0;
1146 patch
->old_mode
= strtoul(ptr
+1, NULL
, 8);
1151 * This is normal for a diff that doesn't change anything: we'll fall through
1152 * into the next diff. Tell the parser to break out.
1154 static int gitdiff_unrecognized(struct apply_state
*state
,
1156 struct patch
*patch
)
1162 * Skip p_value leading components from "line"; as we do not accept
1163 * absolute paths, return NULL in that case.
1165 static const char *skip_tree_prefix(struct apply_state
*state
,
1172 if (!state
->p_value
)
1173 return (llen
&& line
[0] == '/') ? NULL
: line
;
1175 nslash
= state
->p_value
;
1176 for (i
= 0; i
< llen
; i
++) {
1178 if (ch
== '/' && --nslash
<= 0)
1179 return (i
== 0) ? NULL
: &line
[i
+ 1];
1185 * This is to extract the same name that appears on "diff --git"
1186 * line. We do not find and return anything if it is a rename
1187 * patch, and it is OK because we will find the name elsewhere.
1188 * We need to reliably find name only when it is mode-change only,
1189 * creation or deletion of an empty file. In any of these cases,
1190 * both sides are the same name under a/ and b/ respectively.
1192 static char *git_header_name(struct apply_state
*state
,
1197 const char *second
= NULL
;
1198 size_t len
, line_len
;
1200 line
+= strlen("diff --git ");
1201 llen
-= strlen("diff --git ");
1205 struct strbuf first
= STRBUF_INIT
;
1206 struct strbuf sp
= STRBUF_INIT
;
1208 if (unquote_c_style(&first
, line
, &second
))
1209 goto free_and_fail1
;
1211 /* strip the a/b prefix including trailing slash */
1212 cp
= skip_tree_prefix(state
, first
.buf
, first
.len
);
1214 goto free_and_fail1
;
1215 strbuf_remove(&first
, 0, cp
- first
.buf
);
1218 * second points at one past closing dq of name.
1219 * find the second name.
1221 while ((second
< line
+ llen
) && isspace(*second
))
1224 if (line
+ llen
<= second
)
1225 goto free_and_fail1
;
1226 if (*second
== '"') {
1227 if (unquote_c_style(&sp
, second
, NULL
))
1228 goto free_and_fail1
;
1229 cp
= skip_tree_prefix(state
, sp
.buf
, sp
.len
);
1231 goto free_and_fail1
;
1232 /* They must match, otherwise ignore */
1233 if (strcmp(cp
, first
.buf
))
1234 goto free_and_fail1
;
1235 strbuf_release(&sp
);
1236 return strbuf_detach(&first
, NULL
);
1239 /* unquoted second */
1240 cp
= skip_tree_prefix(state
, second
, line
+ llen
- second
);
1242 goto free_and_fail1
;
1243 if (line
+ llen
- cp
!= first
.len
||
1244 memcmp(first
.buf
, cp
, first
.len
))
1245 goto free_and_fail1
;
1246 return strbuf_detach(&first
, NULL
);
1249 strbuf_release(&first
);
1250 strbuf_release(&sp
);
1254 /* unquoted first name */
1255 name
= skip_tree_prefix(state
, line
, llen
);
1260 * since the first name is unquoted, a dq if exists must be
1261 * the beginning of the second name.
1263 for (second
= name
; second
< line
+ llen
; second
++) {
1264 if (*second
== '"') {
1265 struct strbuf sp
= STRBUF_INIT
;
1268 if (unquote_c_style(&sp
, second
, NULL
))
1269 goto free_and_fail2
;
1271 np
= skip_tree_prefix(state
, sp
.buf
, sp
.len
);
1273 goto free_and_fail2
;
1275 len
= sp
.buf
+ sp
.len
- np
;
1276 if (len
< second
- name
&&
1277 !strncmp(np
, name
, len
) &&
1278 isspace(name
[len
])) {
1280 strbuf_remove(&sp
, 0, np
- sp
.buf
);
1281 return strbuf_detach(&sp
, NULL
);
1285 strbuf_release(&sp
);
1291 * Accept a name only if it shows up twice, exactly the same
1294 second
= strchr(name
, '\n');
1297 line_len
= second
- name
;
1298 for (len
= 0 ; ; len
++) {
1299 switch (name
[len
]) {
1304 case '\t': case ' ':
1306 * Is this the separator between the preimage
1307 * and the postimage pathname? Again, we are
1308 * only interested in the case where there is
1309 * no rename, as this is only to set def_name
1310 * and a rename patch has the names elsewhere
1311 * in an unambiguous form.
1314 return NULL
; /* no postimage name */
1315 second
= skip_tree_prefix(state
, name
+ len
+ 1,
1316 line_len
- (len
+ 1));
1320 * Does len bytes starting at "name" and "second"
1321 * (that are separated by one HT or SP we just
1322 * found) exactly match?
1324 if (second
[len
] == '\n' && !strncmp(name
, second
, len
))
1325 return xmemdupz(name
, len
);
1330 /* Verify that we recognize the lines following a git header */
1331 static int parse_git_header(struct apply_state
*state
,
1335 struct patch
*patch
)
1337 unsigned long offset
;
1339 /* A git diff has explicit new/delete information, so we don't guess */
1341 patch
->is_delete
= 0;
1344 * Some things may not have the old name in the
1345 * rest of the headers anywhere (pure mode changes,
1346 * or removing or adding empty files), so we get
1347 * the default name from the header.
1349 patch
->def_name
= git_header_name(state
, line
, len
);
1350 if (patch
->def_name
&& state
->root
.len
) {
1351 char *s
= xstrfmt("%s%s", state
->root
.buf
, patch
->def_name
);
1352 free(patch
->def_name
);
1353 patch
->def_name
= s
;
1359 for (offset
= len
; size
> 0 ; offset
+= len
, size
-= len
, line
+= len
, state
->linenr
++) {
1360 static const struct opentry
{
1362 int (*fn
)(struct apply_state
*, const char *, struct patch
*);
1364 { "@@ -", gitdiff_hdrend
},
1365 { "--- ", gitdiff_oldname
},
1366 { "+++ ", gitdiff_newname
},
1367 { "old mode ", gitdiff_oldmode
},
1368 { "new mode ", gitdiff_newmode
},
1369 { "deleted file mode ", gitdiff_delete
},
1370 { "new file mode ", gitdiff_newfile
},
1371 { "copy from ", gitdiff_copysrc
},
1372 { "copy to ", gitdiff_copydst
},
1373 { "rename old ", gitdiff_renamesrc
},
1374 { "rename new ", gitdiff_renamedst
},
1375 { "rename from ", gitdiff_renamesrc
},
1376 { "rename to ", gitdiff_renamedst
},
1377 { "similarity index ", gitdiff_similarity
},
1378 { "dissimilarity index ", gitdiff_dissimilarity
},
1379 { "index ", gitdiff_index
},
1380 { "", gitdiff_unrecognized
},
1384 len
= linelen(line
, size
);
1385 if (!len
|| line
[len
-1] != '\n')
1387 for (i
= 0; i
< ARRAY_SIZE(optable
); i
++) {
1388 const struct opentry
*p
= optable
+ i
;
1389 int oplen
= strlen(p
->str
);
1390 if (len
< oplen
|| memcmp(p
->str
, line
, oplen
))
1392 if (p
->fn(state
, line
+ oplen
, patch
) < 0)
1401 static int parse_num(const char *line
, unsigned long *p
)
1405 if (!isdigit(*line
))
1407 *p
= strtoul(line
, &ptr
, 10);
1411 static int parse_range(const char *line
, int len
, int offset
, const char *expect
,
1412 unsigned long *p1
, unsigned long *p2
)
1416 if (offset
< 0 || offset
>= len
)
1421 digits
= parse_num(line
, p1
);
1431 digits
= parse_num(line
+1, p2
);
1440 ex
= strlen(expect
);
1443 if (memcmp(line
, expect
, ex
))
1449 static void recount_diff(const char *line
, int size
, struct fragment
*fragment
)
1451 int oldlines
= 0, newlines
= 0, ret
= 0;
1454 warning("recount: ignore empty hunk");
1459 int len
= linelen(line
, size
);
1467 case ' ': case '\n':
1479 ret
= size
< 3 || !starts_with(line
, "@@ ");
1482 ret
= size
< 5 || !starts_with(line
, "diff ");
1489 warning(_("recount: unexpected line: %.*s"),
1490 (int)linelen(line
, size
), line
);
1495 fragment
->oldlines
= oldlines
;
1496 fragment
->newlines
= newlines
;
1500 * Parse a unified diff fragment header of the
1501 * form "@@ -a,b +c,d @@"
1503 static int parse_fragment_header(const char *line
, int len
, struct fragment
*fragment
)
1507 if (!len
|| line
[len
-1] != '\n')
1510 /* Figure out the number of lines in a fragment */
1511 offset
= parse_range(line
, len
, 4, " +", &fragment
->oldpos
, &fragment
->oldlines
);
1512 offset
= parse_range(line
, len
, offset
, " @@", &fragment
->newpos
, &fragment
->newlines
);
1517 static int find_header(struct apply_state
*state
,
1521 struct patch
*patch
)
1523 unsigned long offset
, len
;
1525 patch
->is_toplevel_relative
= 0;
1526 patch
->is_rename
= patch
->is_copy
= 0;
1527 patch
->is_new
= patch
->is_delete
= -1;
1528 patch
->old_mode
= patch
->new_mode
= 0;
1529 patch
->old_name
= patch
->new_name
= NULL
;
1530 for (offset
= 0; size
> 0; offset
+= len
, size
-= len
, line
+= len
, state
->linenr
++) {
1531 unsigned long nextlen
;
1533 len
= linelen(line
, size
);
1537 /* Testing this early allows us to take a few shortcuts.. */
1542 * Make sure we don't find any unconnected patch fragments.
1543 * That's a sign that we didn't find a header, and that a
1544 * patch has become corrupted/broken up.
1546 if (!memcmp("@@ -", line
, 4)) {
1547 struct fragment dummy
;
1548 if (parse_fragment_header(line
, len
, &dummy
) < 0)
1550 die(_("patch fragment without header at line %d: %.*s"),
1551 state
->linenr
, (int)len
-1, line
);
1558 * Git patch? It might not have a real patch, just a rename
1559 * or mode change, so we handle that specially
1561 if (!memcmp("diff --git ", line
, 11)) {
1562 int git_hdr_len
= parse_git_header(state
, line
, len
, size
, patch
);
1563 if (git_hdr_len
<= len
)
1565 if (!patch
->old_name
&& !patch
->new_name
) {
1566 if (!patch
->def_name
)
1567 die(Q_("git diff header lacks filename information when removing "
1568 "%d leading pathname component (line %d)",
1569 "git diff header lacks filename information when removing "
1570 "%d leading pathname components (line %d)",
1572 state
->p_value
, state
->linenr
);
1573 patch
->old_name
= xstrdup(patch
->def_name
);
1574 patch
->new_name
= xstrdup(patch
->def_name
);
1576 if (!patch
->is_delete
&& !patch
->new_name
)
1577 die("git diff header lacks filename information "
1578 "(line %d)", state
->linenr
);
1579 patch
->is_toplevel_relative
= 1;
1580 *hdrsize
= git_hdr_len
;
1584 /* --- followed by +++ ? */
1585 if (memcmp("--- ", line
, 4) || memcmp("+++ ", line
+ len
, 4))
1589 * We only accept unified patches, so we want it to
1590 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
1591 * minimum ("@@ -0,0 +1 @@\n" is the shortest).
1593 nextlen
= linelen(line
+ len
, size
- len
);
1594 if (size
< nextlen
+ 14 || memcmp("@@ -", line
+ len
+ nextlen
, 4))
1597 /* Ok, we'll consider it a patch */
1598 parse_traditional_patch(state
, line
, line
+len
, patch
);
1599 *hdrsize
= len
+ nextlen
;
1606 static void record_ws_error(struct apply_state
*state
,
1617 state
->whitespace_error
++;
1618 if (state
->squelch_whitespace_errors
&&
1619 state
->squelch_whitespace_errors
< state
->whitespace_error
)
1622 err
= whitespace_error_string(result
);
1623 fprintf(stderr
, "%s:%d: %s.\n%.*s\n",
1624 state
->patch_input_file
, linenr
, err
, len
, line
);
1628 static void check_whitespace(struct apply_state
*state
,
1633 unsigned result
= ws_check(line
+ 1, len
- 1, ws_rule
);
1635 record_ws_error(state
, result
, line
+ 1, len
- 2, state
->linenr
);
1639 * Parse a unified diff. Note that this really needs to parse each
1640 * fragment separately, since the only way to know the difference
1641 * between a "---" that is part of a patch, and a "---" that starts
1642 * the next patch is to look at the line counts..
1644 static int parse_fragment(struct apply_state
*state
,
1647 struct patch
*patch
,
1648 struct fragment
*fragment
)
1651 int len
= linelen(line
, size
), offset
;
1652 unsigned long oldlines
, newlines
;
1653 unsigned long leading
, trailing
;
1655 offset
= parse_fragment_header(line
, len
, fragment
);
1658 if (offset
> 0 && patch
->recount
)
1659 recount_diff(line
+ offset
, size
- offset
, fragment
);
1660 oldlines
= fragment
->oldlines
;
1661 newlines
= fragment
->newlines
;
1665 /* Parse the thing.. */
1669 added
= deleted
= 0;
1672 offset
+= len
, size
-= len
, line
+= len
, state
->linenr
++) {
1673 if (!oldlines
&& !newlines
)
1675 len
= linelen(line
, size
);
1676 if (!len
|| line
[len
-1] != '\n')
1681 case '\n': /* newer GNU diff, an empty context line */
1685 if (!deleted
&& !added
)
1688 if (!state
->apply_in_reverse
&&
1689 state
->ws_error_action
== correct_ws_error
)
1690 check_whitespace(state
, line
, len
, patch
->ws_rule
);
1693 if (state
->apply_in_reverse
&&
1694 state
->ws_error_action
!= nowarn_ws_error
)
1695 check_whitespace(state
, line
, len
, patch
->ws_rule
);
1701 if (!state
->apply_in_reverse
&&
1702 state
->ws_error_action
!= nowarn_ws_error
)
1703 check_whitespace(state
, line
, len
, patch
->ws_rule
);
1710 * We allow "\ No newline at end of file". Depending
1711 * on locale settings when the patch was produced we
1712 * don't know what this line looks like. The only
1713 * thing we do know is that it begins with "\ ".
1714 * Checking for 12 is just for sanity check -- any
1715 * l10n of "\ No newline..." is at least that long.
1718 if (len
< 12 || memcmp(line
, "\\ ", 2))
1723 if (oldlines
|| newlines
)
1725 if (!deleted
&& !added
)
1728 fragment
->leading
= leading
;
1729 fragment
->trailing
= trailing
;
1732 * If a fragment ends with an incomplete line, we failed to include
1733 * it in the above loop because we hit oldlines == newlines == 0
1736 if (12 < size
&& !memcmp(line
, "\\ ", 2))
1737 offset
+= linelen(line
, size
);
1739 patch
->lines_added
+= added
;
1740 patch
->lines_deleted
+= deleted
;
1742 if (0 < patch
->is_new
&& oldlines
)
1743 return error(_("new file depends on old contents"));
1744 if (0 < patch
->is_delete
&& newlines
)
1745 return error(_("deleted file still has contents"));
1750 * We have seen "diff --git a/... b/..." header (or a traditional patch
1751 * header). Read hunks that belong to this patch into fragments and hang
1752 * them to the given patch structure.
1754 * The (fragment->patch, fragment->size) pair points into the memory given
1755 * by the caller, not a copy, when we return.
1757 static int parse_single_patch(struct apply_state
*state
,
1760 struct patch
*patch
)
1762 unsigned long offset
= 0;
1763 unsigned long oldlines
= 0, newlines
= 0, context
= 0;
1764 struct fragment
**fragp
= &patch
->fragments
;
1766 while (size
> 4 && !memcmp(line
, "@@ -", 4)) {
1767 struct fragment
*fragment
;
1770 fragment
= xcalloc(1, sizeof(*fragment
));
1771 fragment
->linenr
= state
->linenr
;
1772 len
= parse_fragment(state
, line
, size
, patch
, fragment
);
1774 die(_("corrupt patch at line %d"), state
->linenr
);
1775 fragment
->patch
= line
;
1776 fragment
->size
= len
;
1777 oldlines
+= fragment
->oldlines
;
1778 newlines
+= fragment
->newlines
;
1779 context
+= fragment
->leading
+ fragment
->trailing
;
1782 fragp
= &fragment
->next
;
1790 * If something was removed (i.e. we have old-lines) it cannot
1791 * be creation, and if something was added it cannot be
1792 * deletion. However, the reverse is not true; --unified=0
1793 * patches that only add are not necessarily creation even
1794 * though they do not have any old lines, and ones that only
1795 * delete are not necessarily deletion.
1797 * Unfortunately, a real creation/deletion patch do _not_ have
1798 * any context line by definition, so we cannot safely tell it
1799 * apart with --unified=0 insanity. At least if the patch has
1800 * more than one hunk it is not creation or deletion.
1802 if (patch
->is_new
< 0 &&
1803 (oldlines
|| (patch
->fragments
&& patch
->fragments
->next
)))
1805 if (patch
->is_delete
< 0 &&
1806 (newlines
|| (patch
->fragments
&& patch
->fragments
->next
)))
1807 patch
->is_delete
= 0;
1809 if (0 < patch
->is_new
&& oldlines
)
1810 die(_("new file %s depends on old contents"), patch
->new_name
);
1811 if (0 < patch
->is_delete
&& newlines
)
1812 die(_("deleted file %s still has contents"), patch
->old_name
);
1813 if (!patch
->is_delete
&& !newlines
&& context
)
1816 "file %s becomes empty but is not deleted"),
1822 static inline int metadata_changes(struct patch
*patch
)
1824 return patch
->is_rename
> 0 ||
1825 patch
->is_copy
> 0 ||
1826 patch
->is_new
> 0 ||
1828 (patch
->old_mode
&& patch
->new_mode
&&
1829 patch
->old_mode
!= patch
->new_mode
);
1832 static char *inflate_it(const void *data
, unsigned long size
,
1833 unsigned long inflated_size
)
1839 memset(&stream
, 0, sizeof(stream
));
1841 stream
.next_in
= (unsigned char *)data
;
1842 stream
.avail_in
= size
;
1843 stream
.next_out
= out
= xmalloc(inflated_size
);
1844 stream
.avail_out
= inflated_size
;
1845 git_inflate_init(&stream
);
1846 st
= git_inflate(&stream
, Z_FINISH
);
1847 git_inflate_end(&stream
);
1848 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= inflated_size
) {
1856 * Read a binary hunk and return a new fragment; fragment->patch
1857 * points at an allocated memory that the caller must free, so
1858 * it is marked as "->free_patch = 1".
1860 static struct fragment
*parse_binary_hunk(struct apply_state
*state
,
1862 unsigned long *sz_p
,
1867 * Expect a line that begins with binary patch method ("literal"
1868 * or "delta"), followed by the length of data before deflating.
1869 * a sequence of 'length-byte' followed by base-85 encoded data
1870 * should follow, terminated by a newline.
1872 * Each 5-byte sequence of base-85 encodes up to 4 bytes,
1873 * and we would limit the patch line to 66 characters,
1874 * so one line can fit up to 13 groups that would decode
1875 * to 52 bytes max. The length byte 'A'-'Z' corresponds
1876 * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
1879 unsigned long size
= *sz_p
;
1880 char *buffer
= *buf_p
;
1882 unsigned long origlen
;
1885 struct fragment
*frag
;
1887 llen
= linelen(buffer
, size
);
1892 if (starts_with(buffer
, "delta ")) {
1893 patch_method
= BINARY_DELTA_DEFLATED
;
1894 origlen
= strtoul(buffer
+ 6, NULL
, 10);
1896 else if (starts_with(buffer
, "literal ")) {
1897 patch_method
= BINARY_LITERAL_DEFLATED
;
1898 origlen
= strtoul(buffer
+ 8, NULL
, 10);
1906 int byte_length
, max_byte_length
, newsize
;
1907 llen
= linelen(buffer
, size
);
1911 /* consume the blank line */
1917 * Minimum line is "A00000\n" which is 7-byte long,
1918 * and the line length must be multiple of 5 plus 2.
1920 if ((llen
< 7) || (llen
-2) % 5)
1922 max_byte_length
= (llen
- 2) / 5 * 4;
1923 byte_length
= *buffer
;
1924 if ('A' <= byte_length
&& byte_length
<= 'Z')
1925 byte_length
= byte_length
- 'A' + 1;
1926 else if ('a' <= byte_length
&& byte_length
<= 'z')
1927 byte_length
= byte_length
- 'a' + 27;
1930 /* if the input length was not multiple of 4, we would
1931 * have filler at the end but the filler should never
1934 if (max_byte_length
< byte_length
||
1935 byte_length
<= max_byte_length
- 4)
1937 newsize
= hunk_size
+ byte_length
;
1938 data
= xrealloc(data
, newsize
);
1939 if (decode_85(data
+ hunk_size
, buffer
+ 1, byte_length
))
1941 hunk_size
= newsize
;
1946 frag
= xcalloc(1, sizeof(*frag
));
1947 frag
->patch
= inflate_it(data
, hunk_size
, origlen
);
1948 frag
->free_patch
= 1;
1952 frag
->size
= origlen
;
1956 frag
->binary_patch_method
= patch_method
;
1962 error(_("corrupt binary patch at line %d: %.*s"),
1963 state
->linenr
-1, llen
-1, buffer
);
1969 * -1 in case of error,
1970 * the length of the parsed binary patch otherwise
1972 static int parse_binary(struct apply_state
*state
,
1975 struct patch
*patch
)
1978 * We have read "GIT binary patch\n"; what follows is a line
1979 * that says the patch method (currently, either "literal" or
1980 * "delta") and the length of data before deflating; a
1981 * sequence of 'length-byte' followed by base-85 encoded data
1984 * When a binary patch is reversible, there is another binary
1985 * hunk in the same format, starting with patch method (either
1986 * "literal" or "delta") with the length of data, and a sequence
1987 * of length-byte + base-85 encoded data, terminated with another
1988 * empty line. This data, when applied to the postimage, produces
1991 struct fragment
*forward
;
1992 struct fragment
*reverse
;
1996 forward
= parse_binary_hunk(state
, &buffer
, &size
, &status
, &used
);
1997 if (!forward
&& !status
)
1998 /* there has to be one hunk (forward hunk) */
1999 return error(_("unrecognized binary patch at line %d"), state
->linenr
-1);
2001 /* otherwise we already gave an error message */
2004 reverse
= parse_binary_hunk(state
, &buffer
, &size
, &status
, &used_1
);
2009 * Not having reverse hunk is not an error, but having
2010 * a corrupt reverse hunk is.
2012 free((void*) forward
->patch
);
2016 forward
->next
= reverse
;
2017 patch
->fragments
= forward
;
2018 patch
->is_binary
= 1;
2022 static void prefix_one(struct apply_state
*state
, char **name
)
2024 char *old_name
= *name
;
2027 *name
= xstrdup(prefix_filename(state
->prefix
, state
->prefix_length
, *name
));
2031 static void prefix_patch(struct apply_state
*state
, struct patch
*p
)
2033 if (!state
->prefix
|| p
->is_toplevel_relative
)
2035 prefix_one(state
, &p
->new_name
);
2036 prefix_one(state
, &p
->old_name
);
2043 static void add_name_limit(struct apply_state
*state
,
2047 struct string_list_item
*it
;
2049 it
= string_list_append(&state
->limit_by_name
, name
);
2050 it
->util
= exclude
? NULL
: (void *) 1;
2053 static int use_patch(struct apply_state
*state
, struct patch
*p
)
2055 const char *pathname
= p
->new_name
? p
->new_name
: p
->old_name
;
2058 /* Paths outside are not touched regardless of "--include" */
2059 if (0 < state
->prefix_length
) {
2060 int pathlen
= strlen(pathname
);
2061 if (pathlen
<= state
->prefix_length
||
2062 memcmp(state
->prefix
, pathname
, state
->prefix_length
))
2066 /* See if it matches any of exclude/include rule */
2067 for (i
= 0; i
< state
->limit_by_name
.nr
; i
++) {
2068 struct string_list_item
*it
= &state
->limit_by_name
.items
[i
];
2069 if (!wildmatch(it
->string
, pathname
, 0, NULL
))
2070 return (it
->util
!= NULL
);
2074 * If we had any include, a path that does not match any rule is
2075 * not used. Otherwise, we saw bunch of exclude rules (or none)
2076 * and such a path is used.
2078 return !state
->has_include
;
2083 * Read the patch text in "buffer" that extends for "size" bytes; stop
2084 * reading after seeing a single patch (i.e. changes to a single file).
2085 * Create fragments (i.e. patch hunks) and hang them to the given patch.
2086 * Return the number of bytes consumed, so that the caller can call us
2087 * again for the next patch.
2089 static int parse_chunk(struct apply_state
*state
, char *buffer
, unsigned long size
, struct patch
*patch
)
2091 int hdrsize
, patchsize
;
2092 int offset
= find_header(state
, buffer
, size
, &hdrsize
, patch
);
2097 prefix_patch(state
, patch
);
2099 if (!use_patch(state
, patch
))
2102 patch
->ws_rule
= whitespace_rule(patch
->new_name
2106 patchsize
= parse_single_patch(state
,
2107 buffer
+ offset
+ hdrsize
,
2108 size
- offset
- hdrsize
,
2112 static const char git_binary
[] = "GIT binary patch\n";
2113 int hd
= hdrsize
+ offset
;
2114 unsigned long llen
= linelen(buffer
+ hd
, size
- hd
);
2116 if (llen
== sizeof(git_binary
) - 1 &&
2117 !memcmp(git_binary
, buffer
+ hd
, llen
)) {
2120 used
= parse_binary(state
, buffer
+ hd
+ llen
,
2121 size
- hd
- llen
, patch
);
2125 patchsize
= used
+ llen
;
2129 else if (!memcmp(" differ\n", buffer
+ hd
+ llen
- 8, 8)) {
2130 static const char *binhdr
[] = {
2136 for (i
= 0; binhdr
[i
]; i
++) {
2137 int len
= strlen(binhdr
[i
]);
2138 if (len
< size
- hd
&&
2139 !memcmp(binhdr
[i
], buffer
+ hd
, len
)) {
2141 patch
->is_binary
= 1;
2148 /* Empty patch cannot be applied if it is a text patch
2149 * without metadata change. A binary patch appears
2152 if ((state
->apply
|| state
->check
) &&
2153 (!patch
->is_binary
&& !metadata_changes(patch
)))
2154 die(_("patch with only garbage at line %d"), state
->linenr
);
2157 return offset
+ hdrsize
+ patchsize
;
2160 #define swap(a,b) myswap((a),(b),sizeof(a))
2162 #define myswap(a, b, size) do { \
2163 unsigned char mytmp[size]; \
2164 memcpy(mytmp, &a, size); \
2165 memcpy(&a, &b, size); \
2166 memcpy(&b, mytmp, size); \
2169 static void reverse_patches(struct patch
*p
)
2171 for (; p
; p
= p
->next
) {
2172 struct fragment
*frag
= p
->fragments
;
2174 swap(p
->new_name
, p
->old_name
);
2175 swap(p
->new_mode
, p
->old_mode
);
2176 swap(p
->is_new
, p
->is_delete
);
2177 swap(p
->lines_added
, p
->lines_deleted
);
2178 swap(p
->old_sha1_prefix
, p
->new_sha1_prefix
);
2180 for (; frag
; frag
= frag
->next
) {
2181 swap(frag
->newpos
, frag
->oldpos
);
2182 swap(frag
->newlines
, frag
->oldlines
);
2187 static const char pluses
[] =
2188 "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
2189 static const char minuses
[]=
2190 "----------------------------------------------------------------------";
2192 static void show_stats(struct apply_state
*state
, struct patch
*patch
)
2194 struct strbuf qname
= STRBUF_INIT
;
2195 char *cp
= patch
->new_name
? patch
->new_name
: patch
->old_name
;
2198 quote_c_style(cp
, &qname
, NULL
, 0);
2201 * "scale" the filename
2203 max
= state
->max_len
;
2207 if (qname
.len
> max
) {
2208 cp
= strchr(qname
.buf
+ qname
.len
+ 3 - max
, '/');
2210 cp
= qname
.buf
+ qname
.len
+ 3 - max
;
2211 strbuf_splice(&qname
, 0, cp
- qname
.buf
, "...", 3);
2214 if (patch
->is_binary
) {
2215 printf(" %-*s | Bin\n", max
, qname
.buf
);
2216 strbuf_release(&qname
);
2220 printf(" %-*s |", max
, qname
.buf
);
2221 strbuf_release(&qname
);
2224 * scale the add/delete
2226 max
= max
+ state
->max_change
> 70 ? 70 - max
: state
->max_change
;
2227 add
= patch
->lines_added
;
2228 del
= patch
->lines_deleted
;
2230 if (state
->max_change
> 0) {
2231 int total
= ((add
+ del
) * max
+ state
->max_change
/ 2) / state
->max_change
;
2232 add
= (add
* max
+ state
->max_change
/ 2) / state
->max_change
;
2235 printf("%5d %.*s%.*s\n", patch
->lines_added
+ patch
->lines_deleted
,
2236 add
, pluses
, del
, minuses
);
2239 static int read_old_data(struct stat
*st
, const char *path
, struct strbuf
*buf
)
2241 switch (st
->st_mode
& S_IFMT
) {
2243 if (strbuf_readlink(buf
, path
, st
->st_size
) < 0)
2244 return error(_("unable to read symlink %s"), path
);
2247 if (strbuf_read_file(buf
, path
, st
->st_size
) != st
->st_size
)
2248 return error(_("unable to open or read %s"), path
);
2249 convert_to_git(path
, buf
->buf
, buf
->len
, buf
, 0);
2257 * Update the preimage, and the common lines in postimage,
2258 * from buffer buf of length len. If postlen is 0 the postimage
2259 * is updated in place, otherwise it's updated on a new buffer
2263 static void update_pre_post_images(struct image
*preimage
,
2264 struct image
*postimage
,
2266 size_t len
, size_t postlen
)
2268 int i
, ctx
, reduced
;
2269 char *new, *old
, *fixed
;
2270 struct image fixed_preimage
;
2273 * Update the preimage with whitespace fixes. Note that we
2274 * are not losing preimage->buf -- apply_one_fragment() will
2277 prepare_image(&fixed_preimage
, buf
, len
, 1);
2279 ? fixed_preimage
.nr
== preimage
->nr
2280 : fixed_preimage
.nr
<= preimage
->nr
);
2281 for (i
= 0; i
< fixed_preimage
.nr
; i
++)
2282 fixed_preimage
.line
[i
].flag
= preimage
->line
[i
].flag
;
2283 free(preimage
->line_allocated
);
2284 *preimage
= fixed_preimage
;
2287 * Adjust the common context lines in postimage. This can be
2288 * done in-place when we are shrinking it with whitespace
2289 * fixing, but needs a new buffer when ignoring whitespace or
2290 * expanding leading tabs to spaces.
2292 * We trust the caller to tell us if the update can be done
2293 * in place (postlen==0) or not.
2295 old
= postimage
->buf
;
2297 new = postimage
->buf
= xmalloc(postlen
);
2300 fixed
= preimage
->buf
;
2302 for (i
= reduced
= ctx
= 0; i
< postimage
->nr
; i
++) {
2303 size_t l_len
= postimage
->line
[i
].len
;
2304 if (!(postimage
->line
[i
].flag
& LINE_COMMON
)) {
2305 /* an added line -- no counterparts in preimage */
2306 memmove(new, old
, l_len
);
2312 /* a common context -- skip it in the original postimage */
2315 /* and find the corresponding one in the fixed preimage */
2316 while (ctx
< preimage
->nr
&&
2317 !(preimage
->line
[ctx
].flag
& LINE_COMMON
)) {
2318 fixed
+= preimage
->line
[ctx
].len
;
2323 * preimage is expected to run out, if the caller
2324 * fixed addition of trailing blank lines.
2326 if (preimage
->nr
<= ctx
) {
2331 /* and copy it in, while fixing the line length */
2332 l_len
= preimage
->line
[ctx
].len
;
2333 memcpy(new, fixed
, l_len
);
2336 postimage
->line
[i
].len
= l_len
;
2341 ? postlen
< new - postimage
->buf
2342 : postimage
->len
< new - postimage
->buf
)
2343 die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
2344 (int)postlen
, (int) postimage
->len
, (int)(new - postimage
->buf
));
2346 /* Fix the length of the whole thing */
2347 postimage
->len
= new - postimage
->buf
;
2348 postimage
->nr
-= reduced
;
2351 static int line_by_line_fuzzy_match(struct image
*img
,
2352 struct image
*preimage
,
2353 struct image
*postimage
,
2361 size_t postlen
= postimage
->len
;
2366 struct strbuf fixed
;
2370 for (i
= 0; i
< preimage_limit
; i
++) {
2371 size_t prelen
= preimage
->line
[i
].len
;
2372 size_t imglen
= img
->line
[try_lno
+i
].len
;
2374 if (!fuzzy_matchlines(img
->buf
+ try + imgoff
, imglen
,
2375 preimage
->buf
+ preoff
, prelen
))
2377 if (preimage
->line
[i
].flag
& LINE_COMMON
)
2378 postlen
+= imglen
- prelen
;
2384 * Ok, the preimage matches with whitespace fuzz.
2386 * imgoff now holds the true length of the target that
2387 * matches the preimage before the end of the file.
2389 * Count the number of characters in the preimage that fall
2390 * beyond the end of the file and make sure that all of them
2391 * are whitespace characters. (This can only happen if
2392 * we are removing blank lines at the end of the file.)
2394 buf
= preimage_eof
= preimage
->buf
+ preoff
;
2395 for ( ; i
< preimage
->nr
; i
++)
2396 preoff
+= preimage
->line
[i
].len
;
2397 preimage_end
= preimage
->buf
+ preoff
;
2398 for ( ; buf
< preimage_end
; buf
++)
2403 * Update the preimage and the common postimage context
2404 * lines to use the same whitespace as the target.
2405 * If whitespace is missing in the target (i.e.
2406 * if the preimage extends beyond the end of the file),
2407 * use the whitespace from the preimage.
2409 extra_chars
= preimage_end
- preimage_eof
;
2410 strbuf_init(&fixed
, imgoff
+ extra_chars
);
2411 strbuf_add(&fixed
, img
->buf
+ try, imgoff
);
2412 strbuf_add(&fixed
, preimage_eof
, extra_chars
);
2413 fixed_buf
= strbuf_detach(&fixed
, &fixed_len
);
2414 update_pre_post_images(preimage
, postimage
,
2415 fixed_buf
, fixed_len
, postlen
);
2419 static int match_fragment(struct apply_state
*state
,
2421 struct image
*preimage
,
2422 struct image
*postimage
,
2426 int match_beginning
, int match_end
)
2429 char *fixed_buf
, *buf
, *orig
, *target
;
2430 struct strbuf fixed
;
2431 size_t fixed_len
, postlen
;
2434 if (preimage
->nr
+ try_lno
<= img
->nr
) {
2436 * The hunk falls within the boundaries of img.
2438 preimage_limit
= preimage
->nr
;
2439 if (match_end
&& (preimage
->nr
+ try_lno
!= img
->nr
))
2441 } else if (state
->ws_error_action
== correct_ws_error
&&
2442 (ws_rule
& WS_BLANK_AT_EOF
)) {
2444 * This hunk extends beyond the end of img, and we are
2445 * removing blank lines at the end of the file. This
2446 * many lines from the beginning of the preimage must
2447 * match with img, and the remainder of the preimage
2450 preimage_limit
= img
->nr
- try_lno
;
2453 * The hunk extends beyond the end of the img and
2454 * we are not removing blanks at the end, so we
2455 * should reject the hunk at this position.
2460 if (match_beginning
&& try_lno
)
2463 /* Quick hash check */
2464 for (i
= 0; i
< preimage_limit
; i
++)
2465 if ((img
->line
[try_lno
+ i
].flag
& LINE_PATCHED
) ||
2466 (preimage
->line
[i
].hash
!= img
->line
[try_lno
+ i
].hash
))
2469 if (preimage_limit
== preimage
->nr
) {
2471 * Do we have an exact match? If we were told to match
2472 * at the end, size must be exactly at try+fragsize,
2473 * otherwise try+fragsize must be still within the preimage,
2474 * and either case, the old piece should match the preimage
2478 ? (try + preimage
->len
== img
->len
)
2479 : (try + preimage
->len
<= img
->len
)) &&
2480 !memcmp(img
->buf
+ try, preimage
->buf
, preimage
->len
))
2484 * The preimage extends beyond the end of img, so
2485 * there cannot be an exact match.
2487 * There must be one non-blank context line that match
2488 * a line before the end of img.
2492 buf
= preimage
->buf
;
2494 for (i
= 0; i
< preimage_limit
; i
++)
2495 buf_end
+= preimage
->line
[i
].len
;
2497 for ( ; buf
< buf_end
; buf
++)
2505 * No exact match. If we are ignoring whitespace, run a line-by-line
2506 * fuzzy matching. We collect all the line length information because
2507 * we need it to adjust whitespace if we match.
2509 if (state
->ws_ignore_action
== ignore_ws_change
)
2510 return line_by_line_fuzzy_match(img
, preimage
, postimage
,
2511 try, try_lno
, preimage_limit
);
2513 if (state
->ws_error_action
!= correct_ws_error
)
2517 * The hunk does not apply byte-by-byte, but the hash says
2518 * it might with whitespace fuzz. We weren't asked to
2519 * ignore whitespace, we were asked to correct whitespace
2520 * errors, so let's try matching after whitespace correction.
2522 * While checking the preimage against the target, whitespace
2523 * errors in both fixed, we count how large the corresponding
2524 * postimage needs to be. The postimage prepared by
2525 * apply_one_fragment() has whitespace errors fixed on added
2526 * lines already, but the common lines were propagated as-is,
2527 * which may become longer when their whitespace errors are
2531 /* First count added lines in postimage */
2533 for (i
= 0; i
< postimage
->nr
; i
++) {
2534 if (!(postimage
->line
[i
].flag
& LINE_COMMON
))
2535 postlen
+= postimage
->line
[i
].len
;
2539 * The preimage may extend beyond the end of the file,
2540 * but in this loop we will only handle the part of the
2541 * preimage that falls within the file.
2543 strbuf_init(&fixed
, preimage
->len
+ 1);
2544 orig
= preimage
->buf
;
2545 target
= img
->buf
+ try;
2546 for (i
= 0; i
< preimage_limit
; i
++) {
2547 size_t oldlen
= preimage
->line
[i
].len
;
2548 size_t tgtlen
= img
->line
[try_lno
+ i
].len
;
2549 size_t fixstart
= fixed
.len
;
2550 struct strbuf tgtfix
;
2553 /* Try fixing the line in the preimage */
2554 ws_fix_copy(&fixed
, orig
, oldlen
, ws_rule
, NULL
);
2556 /* Try fixing the line in the target */
2557 strbuf_init(&tgtfix
, tgtlen
);
2558 ws_fix_copy(&tgtfix
, target
, tgtlen
, ws_rule
, NULL
);
2561 * If they match, either the preimage was based on
2562 * a version before our tree fixed whitespace breakage,
2563 * or we are lacking a whitespace-fix patch the tree
2564 * the preimage was based on already had (i.e. target
2565 * has whitespace breakage, the preimage doesn't).
2566 * In either case, we are fixing the whitespace breakages
2567 * so we might as well take the fix together with their
2570 match
= (tgtfix
.len
== fixed
.len
- fixstart
&&
2571 !memcmp(tgtfix
.buf
, fixed
.buf
+ fixstart
,
2572 fixed
.len
- fixstart
));
2574 /* Add the length if this is common with the postimage */
2575 if (preimage
->line
[i
].flag
& LINE_COMMON
)
2576 postlen
+= tgtfix
.len
;
2578 strbuf_release(&tgtfix
);
2588 * Now handle the lines in the preimage that falls beyond the
2589 * end of the file (if any). They will only match if they are
2590 * empty or only contain whitespace (if WS_BLANK_AT_EOL is
2593 for ( ; i
< preimage
->nr
; i
++) {
2594 size_t fixstart
= fixed
.len
; /* start of the fixed preimage */
2595 size_t oldlen
= preimage
->line
[i
].len
;
2598 /* Try fixing the line in the preimage */
2599 ws_fix_copy(&fixed
, orig
, oldlen
, ws_rule
, NULL
);
2601 for (j
= fixstart
; j
< fixed
.len
; j
++)
2602 if (!isspace(fixed
.buf
[j
]))
2609 * Yes, the preimage is based on an older version that still
2610 * has whitespace breakages unfixed, and fixing them makes the
2611 * hunk match. Update the context lines in the postimage.
2613 fixed_buf
= strbuf_detach(&fixed
, &fixed_len
);
2614 if (postlen
< postimage
->len
)
2616 update_pre_post_images(preimage
, postimage
,
2617 fixed_buf
, fixed_len
, postlen
);
2621 strbuf_release(&fixed
);
2625 static int find_pos(struct apply_state
*state
,
2627 struct image
*preimage
,
2628 struct image
*postimage
,
2631 int match_beginning
, int match_end
)
2634 unsigned long backwards
, forwards
, try;
2635 int backwards_lno
, forwards_lno
, try_lno
;
2638 * If match_beginning or match_end is specified, there is no
2639 * point starting from a wrong line that will never match and
2640 * wander around and wait for a match at the specified end.
2642 if (match_beginning
)
2645 line
= img
->nr
- preimage
->nr
;
2648 * Because the comparison is unsigned, the following test
2649 * will also take care of a negative line number that can
2650 * result when match_end and preimage is larger than the target.
2652 if ((size_t) line
> img
->nr
)
2656 for (i
= 0; i
< line
; i
++)
2657 try += img
->line
[i
].len
;
2660 * There's probably some smart way to do this, but I'll leave
2661 * that to the smart and beautiful people. I'm simple and stupid.
2664 backwards_lno
= line
;
2666 forwards_lno
= line
;
2669 for (i
= 0; ; i
++) {
2670 if (match_fragment(state
, img
, preimage
, postimage
,
2671 try, try_lno
, ws_rule
,
2672 match_beginning
, match_end
))
2676 if (backwards_lno
== 0 && forwards_lno
== img
->nr
)
2680 if (backwards_lno
== 0) {
2685 backwards
-= img
->line
[backwards_lno
].len
;
2687 try_lno
= backwards_lno
;
2689 if (forwards_lno
== img
->nr
) {
2693 forwards
+= img
->line
[forwards_lno
].len
;
2696 try_lno
= forwards_lno
;
2703 static void remove_first_line(struct image
*img
)
2705 img
->buf
+= img
->line
[0].len
;
2706 img
->len
-= img
->line
[0].len
;
2711 static void remove_last_line(struct image
*img
)
2713 img
->len
-= img
->line
[--img
->nr
].len
;
2717 * The change from "preimage" and "postimage" has been found to
2718 * apply at applied_pos (counts in line numbers) in "img".
2719 * Update "img" to remove "preimage" and replace it with "postimage".
2721 static void update_image(struct apply_state
*state
,
2724 struct image
*preimage
,
2725 struct image
*postimage
)
2728 * remove the copy of preimage at offset in img
2729 * and replace it with postimage
2732 size_t remove_count
, insert_count
, applied_at
= 0;
2737 * If we are removing blank lines at the end of img,
2738 * the preimage may extend beyond the end.
2739 * If that is the case, we must be careful only to
2740 * remove the part of the preimage that falls within
2741 * the boundaries of img. Initialize preimage_limit
2742 * to the number of lines in the preimage that falls
2743 * within the boundaries.
2745 preimage_limit
= preimage
->nr
;
2746 if (preimage_limit
> img
->nr
- applied_pos
)
2747 preimage_limit
= img
->nr
- applied_pos
;
2749 for (i
= 0; i
< applied_pos
; i
++)
2750 applied_at
+= img
->line
[i
].len
;
2753 for (i
= 0; i
< preimage_limit
; i
++)
2754 remove_count
+= img
->line
[applied_pos
+ i
].len
;
2755 insert_count
= postimage
->len
;
2757 /* Adjust the contents */
2758 result
= xmalloc(st_add3(st_sub(img
->len
, remove_count
), insert_count
, 1));
2759 memcpy(result
, img
->buf
, applied_at
);
2760 memcpy(result
+ applied_at
, postimage
->buf
, postimage
->len
);
2761 memcpy(result
+ applied_at
+ postimage
->len
,
2762 img
->buf
+ (applied_at
+ remove_count
),
2763 img
->len
- (applied_at
+ remove_count
));
2766 img
->len
+= insert_count
- remove_count
;
2767 result
[img
->len
] = '\0';
2769 /* Adjust the line table */
2770 nr
= img
->nr
+ postimage
->nr
- preimage_limit
;
2771 if (preimage_limit
< postimage
->nr
) {
2773 * NOTE: this knows that we never call remove_first_line()
2774 * on anything other than pre/post image.
2776 REALLOC_ARRAY(img
->line
, nr
);
2777 img
->line_allocated
= img
->line
;
2779 if (preimage_limit
!= postimage
->nr
)
2780 memmove(img
->line
+ applied_pos
+ postimage
->nr
,
2781 img
->line
+ applied_pos
+ preimage_limit
,
2782 (img
->nr
- (applied_pos
+ preimage_limit
)) *
2783 sizeof(*img
->line
));
2784 memcpy(img
->line
+ applied_pos
,
2786 postimage
->nr
* sizeof(*img
->line
));
2787 if (!state
->allow_overlap
)
2788 for (i
= 0; i
< postimage
->nr
; i
++)
2789 img
->line
[applied_pos
+ i
].flag
|= LINE_PATCHED
;
2794 * Use the patch-hunk text in "frag" to prepare two images (preimage and
2795 * postimage) for the hunk. Find lines that match "preimage" in "img" and
2796 * replace the part of "img" with "postimage" text.
2798 static int apply_one_fragment(struct apply_state
*state
,
2799 struct image
*img
, struct fragment
*frag
,
2800 int inaccurate_eof
, unsigned ws_rule
,
2803 int match_beginning
, match_end
;
2804 const char *patch
= frag
->patch
;
2805 int size
= frag
->size
;
2806 char *old
, *oldlines
;
2807 struct strbuf newlines
;
2808 int new_blank_lines_at_end
= 0;
2809 int found_new_blank_lines_at_end
= 0;
2810 int hunk_linenr
= frag
->linenr
;
2811 unsigned long leading
, trailing
;
2812 int pos
, applied_pos
;
2813 struct image preimage
;
2814 struct image postimage
;
2816 memset(&preimage
, 0, sizeof(preimage
));
2817 memset(&postimage
, 0, sizeof(postimage
));
2818 oldlines
= xmalloc(size
);
2819 strbuf_init(&newlines
, size
);
2824 int len
= linelen(patch
, size
);
2826 int added_blank_line
= 0;
2827 int is_blank_context
= 0;
2834 * "plen" is how much of the line we should use for
2835 * the actual patch data. Normally we just remove the
2836 * first character on the line, but if the line is
2837 * followed by "\ No newline", then we also remove the
2838 * last one (which is the newline, of course).
2841 if (len
< size
&& patch
[len
] == '\\')
2844 if (state
->apply_in_reverse
) {
2847 else if (first
== '+')
2853 /* Newer GNU diff, empty context line */
2855 /* ... followed by '\No newline'; nothing */
2858 strbuf_addch(&newlines
, '\n');
2859 add_line_info(&preimage
, "\n", 1, LINE_COMMON
);
2860 add_line_info(&postimage
, "\n", 1, LINE_COMMON
);
2861 is_blank_context
= 1;
2864 if (plen
&& (ws_rule
& WS_BLANK_AT_EOF
) &&
2865 ws_blank_line(patch
+ 1, plen
, ws_rule
))
2866 is_blank_context
= 1;
2868 memcpy(old
, patch
+ 1, plen
);
2869 add_line_info(&preimage
, old
, plen
,
2870 (first
== ' ' ? LINE_COMMON
: 0));
2874 /* Fall-through for ' ' */
2876 /* --no-add does not add new lines */
2877 if (first
== '+' && state
->no_add
)
2880 start
= newlines
.len
;
2882 !state
->whitespace_error
||
2883 state
->ws_error_action
!= correct_ws_error
) {
2884 strbuf_add(&newlines
, patch
+ 1, plen
);
2887 ws_fix_copy(&newlines
, patch
+ 1, plen
, ws_rule
, &state
->applied_after_fixing_ws
);
2889 add_line_info(&postimage
, newlines
.buf
+ start
, newlines
.len
- start
,
2890 (first
== '+' ? 0 : LINE_COMMON
));
2892 (ws_rule
& WS_BLANK_AT_EOF
) &&
2893 ws_blank_line(patch
+ 1, plen
, ws_rule
))
2894 added_blank_line
= 1;
2896 case '@': case '\\':
2897 /* Ignore it, we already handled it */
2900 if (state
->apply_verbosely
)
2901 error(_("invalid start of line: '%c'"), first
);
2905 if (added_blank_line
) {
2906 if (!new_blank_lines_at_end
)
2907 found_new_blank_lines_at_end
= hunk_linenr
;
2908 new_blank_lines_at_end
++;
2910 else if (is_blank_context
)
2913 new_blank_lines_at_end
= 0;
2918 if (inaccurate_eof
&&
2919 old
> oldlines
&& old
[-1] == '\n' &&
2920 newlines
.len
> 0 && newlines
.buf
[newlines
.len
- 1] == '\n') {
2922 strbuf_setlen(&newlines
, newlines
.len
- 1);
2925 leading
= frag
->leading
;
2926 trailing
= frag
->trailing
;
2929 * A hunk to change lines at the beginning would begin with
2931 * but we need to be careful. -U0 that inserts before the second
2932 * line also has this pattern.
2934 * And a hunk to add to an empty file would begin with
2937 * In other words, a hunk that is (frag->oldpos <= 1) with or
2938 * without leading context must match at the beginning.
2940 match_beginning
= (!frag
->oldpos
||
2941 (frag
->oldpos
== 1 && !state
->unidiff_zero
));
2944 * A hunk without trailing lines must match at the end.
2945 * However, we simply cannot tell if a hunk must match end
2946 * from the lack of trailing lines if the patch was generated
2947 * with unidiff without any context.
2949 match_end
= !state
->unidiff_zero
&& !trailing
;
2951 pos
= frag
->newpos
? (frag
->newpos
- 1) : 0;
2952 preimage
.buf
= oldlines
;
2953 preimage
.len
= old
- oldlines
;
2954 postimage
.buf
= newlines
.buf
;
2955 postimage
.len
= newlines
.len
;
2956 preimage
.line
= preimage
.line_allocated
;
2957 postimage
.line
= postimage
.line_allocated
;
2961 applied_pos
= find_pos(state
, img
, &preimage
, &postimage
, pos
,
2962 ws_rule
, match_beginning
, match_end
);
2964 if (applied_pos
>= 0)
2967 /* Am I at my context limits? */
2968 if ((leading
<= state
->p_context
) && (trailing
<= state
->p_context
))
2970 if (match_beginning
|| match_end
) {
2971 match_beginning
= match_end
= 0;
2976 * Reduce the number of context lines; reduce both
2977 * leading and trailing if they are equal otherwise
2978 * just reduce the larger context.
2980 if (leading
>= trailing
) {
2981 remove_first_line(&preimage
);
2982 remove_first_line(&postimage
);
2986 if (trailing
> leading
) {
2987 remove_last_line(&preimage
);
2988 remove_last_line(&postimage
);
2993 if (applied_pos
>= 0) {
2994 if (new_blank_lines_at_end
&&
2995 preimage
.nr
+ applied_pos
>= img
->nr
&&
2996 (ws_rule
& WS_BLANK_AT_EOF
) &&
2997 state
->ws_error_action
!= nowarn_ws_error
) {
2998 record_ws_error(state
, WS_BLANK_AT_EOF
, "+", 1,
2999 found_new_blank_lines_at_end
);
3000 if (state
->ws_error_action
== correct_ws_error
) {
3001 while (new_blank_lines_at_end
--)
3002 remove_last_line(&postimage
);
3005 * We would want to prevent write_out_results()
3006 * from taking place in apply_patch() that follows
3007 * the callchain led us here, which is:
3008 * apply_patch->check_patch_list->check_patch->
3009 * apply_data->apply_fragments->apply_one_fragment
3011 if (state
->ws_error_action
== die_on_ws_error
)
3015 if (state
->apply_verbosely
&& applied_pos
!= pos
) {
3016 int offset
= applied_pos
- pos
;
3017 if (state
->apply_in_reverse
)
3018 offset
= 0 - offset
;
3020 Q_("Hunk #%d succeeded at %d (offset %d line).",
3021 "Hunk #%d succeeded at %d (offset %d lines).",
3023 nth_fragment
, applied_pos
+ 1, offset
);
3027 * Warn if it was necessary to reduce the number
3030 if ((leading
!= frag
->leading
) ||
3031 (trailing
!= frag
->trailing
))
3032 fprintf_ln(stderr
, _("Context reduced to (%ld/%ld)"
3033 " to apply fragment at %d"),
3034 leading
, trailing
, applied_pos
+1);
3035 update_image(state
, img
, applied_pos
, &preimage
, &postimage
);
3037 if (state
->apply_verbosely
)
3038 error(_("while searching for:\n%.*s"),
3039 (int)(old
- oldlines
), oldlines
);
3044 strbuf_release(&newlines
);
3045 free(preimage
.line_allocated
);
3046 free(postimage
.line_allocated
);
3048 return (applied_pos
< 0);
3051 static int apply_binary_fragment(struct apply_state
*state
,
3053 struct patch
*patch
)
3055 struct fragment
*fragment
= patch
->fragments
;
3060 return error(_("missing binary patch data for '%s'"),
3065 /* Binary patch is irreversible without the optional second hunk */
3066 if (state
->apply_in_reverse
) {
3067 if (!fragment
->next
)
3068 return error("cannot reverse-apply a binary patch "
3069 "without the reverse hunk to '%s'",
3071 ? patch
->new_name
: patch
->old_name
);
3072 fragment
= fragment
->next
;
3074 switch (fragment
->binary_patch_method
) {
3075 case BINARY_DELTA_DEFLATED
:
3076 dst
= patch_delta(img
->buf
, img
->len
, fragment
->patch
,
3077 fragment
->size
, &len
);
3084 case BINARY_LITERAL_DEFLATED
:
3086 img
->len
= fragment
->size
;
3087 img
->buf
= xmemdupz(fragment
->patch
, img
->len
);
3094 * Replace "img" with the result of applying the binary patch.
3095 * The binary patch data itself in patch->fragment is still kept
3096 * but the preimage prepared by the caller in "img" is freed here
3097 * or in the helper function apply_binary_fragment() this calls.
3099 static int apply_binary(struct apply_state
*state
,
3101 struct patch
*patch
)
3103 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
3104 struct object_id oid
;
3107 * For safety, we require patch index line to contain
3108 * full 40-byte textual SHA1 for old and new, at least for now.
3110 if (strlen(patch
->old_sha1_prefix
) != GIT_SHA1_HEXSZ
||
3111 strlen(patch
->new_sha1_prefix
) != GIT_SHA1_HEXSZ
||
3112 get_oid_hex(patch
->old_sha1_prefix
, &oid
) ||
3113 get_oid_hex(patch
->new_sha1_prefix
, &oid
))
3114 return error("cannot apply binary patch to '%s' "
3115 "without full index line", name
);
3117 if (patch
->old_name
) {
3119 * See if the old one matches what the patch
3122 hash_sha1_file(img
->buf
, img
->len
, blob_type
, oid
.hash
);
3123 if (strcmp(oid_to_hex(&oid
), patch
->old_sha1_prefix
))
3124 return error("the patch applies to '%s' (%s), "
3125 "which does not match the "
3126 "current contents.",
3127 name
, oid_to_hex(&oid
));
3130 /* Otherwise, the old one must be empty. */
3132 return error("the patch applies to an empty "
3133 "'%s' but it is not empty", name
);
3136 get_oid_hex(patch
->new_sha1_prefix
, &oid
);
3137 if (is_null_oid(&oid
)) {
3139 return 0; /* deletion patch */
3142 if (has_sha1_file(oid
.hash
)) {
3143 /* We already have the postimage */
3144 enum object_type type
;
3148 result
= read_sha1_file(oid
.hash
, &type
, &size
);
3150 return error("the necessary postimage %s for "
3151 "'%s' cannot be read",
3152 patch
->new_sha1_prefix
, name
);
3158 * We have verified buf matches the preimage;
3159 * apply the patch data to it, which is stored
3160 * in the patch->fragments->{patch,size}.
3162 if (apply_binary_fragment(state
, img
, patch
))
3163 return error(_("binary patch does not apply to '%s'"),
3166 /* verify that the result matches */
3167 hash_sha1_file(img
->buf
, img
->len
, blob_type
, oid
.hash
);
3168 if (strcmp(oid_to_hex(&oid
), patch
->new_sha1_prefix
))
3169 return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"),
3170 name
, patch
->new_sha1_prefix
, oid_to_hex(&oid
));
3176 static int apply_fragments(struct apply_state
*state
, struct image
*img
, struct patch
*patch
)
3178 struct fragment
*frag
= patch
->fragments
;
3179 const char *name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
3180 unsigned ws_rule
= patch
->ws_rule
;
3181 unsigned inaccurate_eof
= patch
->inaccurate_eof
;
3184 if (patch
->is_binary
)
3185 return apply_binary(state
, img
, patch
);
3189 if (apply_one_fragment(state
, img
, frag
, inaccurate_eof
, ws_rule
, nth
)) {
3190 error(_("patch failed: %s:%ld"), name
, frag
->oldpos
);
3191 if (!state
->apply_with_reject
)
3200 static int read_blob_object(struct strbuf
*buf
, const struct object_id
*oid
, unsigned mode
)
3202 if (S_ISGITLINK(mode
)) {
3203 strbuf_grow(buf
, 100);
3204 strbuf_addf(buf
, "Subproject commit %s\n", oid_to_hex(oid
));
3206 enum object_type type
;
3210 result
= read_sha1_file(oid
->hash
, &type
, &sz
);
3213 /* XXX read_sha1_file NUL-terminates */
3214 strbuf_attach(buf
, result
, sz
, sz
+ 1);
3219 static int read_file_or_gitlink(const struct cache_entry
*ce
, struct strbuf
*buf
)
3223 return read_blob_object(buf
, &ce
->oid
, ce
->ce_mode
);
3226 static struct patch
*in_fn_table(struct apply_state
*state
, const char *name
)
3228 struct string_list_item
*item
;
3233 item
= string_list_lookup(&state
->fn_table
, name
);
3235 return (struct patch
*)item
->util
;
3241 * item->util in the filename table records the status of the path.
3242 * Usually it points at a patch (whose result records the contents
3243 * of it after applying it), but it could be PATH_WAS_DELETED for a
3244 * path that a previously applied patch has already removed, or
3245 * PATH_TO_BE_DELETED for a path that a later patch would remove.
3247 * The latter is needed to deal with a case where two paths A and B
3248 * are swapped by first renaming A to B and then renaming B to A;
3249 * moving A to B should not be prevented due to presence of B as we
3250 * will remove it in a later patch.
3252 #define PATH_TO_BE_DELETED ((struct patch *) -2)
3253 #define PATH_WAS_DELETED ((struct patch *) -1)
3255 static int to_be_deleted(struct patch
*patch
)
3257 return patch
== PATH_TO_BE_DELETED
;
3260 static int was_deleted(struct patch
*patch
)
3262 return patch
== PATH_WAS_DELETED
;
3265 static void add_to_fn_table(struct apply_state
*state
, struct patch
*patch
)
3267 struct string_list_item
*item
;
3270 * Always add new_name unless patch is a deletion
3271 * This should cover the cases for normal diffs,
3272 * file creations and copies
3274 if (patch
->new_name
!= NULL
) {
3275 item
= string_list_insert(&state
->fn_table
, patch
->new_name
);
3280 * store a failure on rename/deletion cases because
3281 * later chunks shouldn't patch old names
3283 if ((patch
->new_name
== NULL
) || (patch
->is_rename
)) {
3284 item
= string_list_insert(&state
->fn_table
, patch
->old_name
);
3285 item
->util
= PATH_WAS_DELETED
;
3289 static void prepare_fn_table(struct apply_state
*state
, struct patch
*patch
)
3292 * store information about incoming file deletion
3295 if ((patch
->new_name
== NULL
) || (patch
->is_rename
)) {
3296 struct string_list_item
*item
;
3297 item
= string_list_insert(&state
->fn_table
, patch
->old_name
);
3298 item
->util
= PATH_TO_BE_DELETED
;
3300 patch
= patch
->next
;
3304 static int checkout_target(struct index_state
*istate
,
3305 struct cache_entry
*ce
, struct stat
*st
)
3307 struct checkout costate
;
3309 memset(&costate
, 0, sizeof(costate
));
3310 costate
.base_dir
= "";
3311 costate
.refresh_cache
= 1;
3312 costate
.istate
= istate
;
3313 if (checkout_entry(ce
, &costate
, NULL
) || lstat(ce
->name
, st
))
3314 return error(_("cannot checkout %s"), ce
->name
);
3318 static struct patch
*previous_patch(struct apply_state
*state
,
3319 struct patch
*patch
,
3322 struct patch
*previous
;
3325 if (patch
->is_copy
|| patch
->is_rename
)
3326 return NULL
; /* "git" patches do not depend on the order */
3328 previous
= in_fn_table(state
, patch
->old_name
);
3332 if (to_be_deleted(previous
))
3333 return NULL
; /* the deletion hasn't happened yet */
3335 if (was_deleted(previous
))
3341 static int verify_index_match(const struct cache_entry
*ce
, struct stat
*st
)
3343 if (S_ISGITLINK(ce
->ce_mode
)) {
3344 if (!S_ISDIR(st
->st_mode
))
3348 return ce_match_stat(ce
, st
, CE_MATCH_IGNORE_VALID
|CE_MATCH_IGNORE_SKIP_WORKTREE
);
3351 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3353 static int load_patch_target(struct apply_state
*state
,
3355 const struct cache_entry
*ce
,
3358 unsigned expected_mode
)
3360 if (state
->cached
|| state
->check_index
) {
3361 if (read_file_or_gitlink(ce
, buf
))
3362 return error(_("failed to read %s"), name
);
3364 if (S_ISGITLINK(expected_mode
)) {
3366 return read_file_or_gitlink(ce
, buf
);
3368 return SUBMODULE_PATCH_WITHOUT_INDEX
;
3369 } else if (has_symlink_leading_path(name
, strlen(name
))) {
3370 return error(_("reading from '%s' beyond a symbolic link"), name
);
3372 if (read_old_data(st
, name
, buf
))
3373 return error(_("failed to read %s"), name
);
3380 * We are about to apply "patch"; populate the "image" with the
3381 * current version we have, from the working tree or from the index,
3382 * depending on the situation e.g. --cached/--index. If we are
3383 * applying a non-git patch that incrementally updates the tree,
3384 * we read from the result of a previous diff.
3386 static int load_preimage(struct apply_state
*state
,
3387 struct image
*image
,
3388 struct patch
*patch
, struct stat
*st
,
3389 const struct cache_entry
*ce
)
3391 struct strbuf buf
= STRBUF_INIT
;
3394 struct patch
*previous
;
3397 previous
= previous_patch(state
, patch
, &status
);
3399 return error(_("path %s has been renamed/deleted"),
3402 /* We have a patched copy in memory; use that. */
3403 strbuf_add(&buf
, previous
->result
, previous
->resultsize
);
3405 status
= load_patch_target(state
, &buf
, ce
, st
,
3406 patch
->old_name
, patch
->old_mode
);
3409 else if (status
== SUBMODULE_PATCH_WITHOUT_INDEX
) {
3411 * There is no way to apply subproject
3412 * patch without looking at the index.
3413 * NEEDSWORK: shouldn't this be flagged
3416 free_fragment_list(patch
->fragments
);
3417 patch
->fragments
= NULL
;
3418 } else if (status
) {
3419 return error(_("failed to read %s"), patch
->old_name
);
3423 img
= strbuf_detach(&buf
, &len
);
3424 prepare_image(image
, img
, len
, !patch
->is_binary
);
3428 static int three_way_merge(struct image
*image
,
3430 const struct object_id
*base
,
3431 const struct object_id
*ours
,
3432 const struct object_id
*theirs
)
3434 mmfile_t base_file
, our_file
, their_file
;
3435 mmbuffer_t result
= { NULL
};
3438 read_mmblob(&base_file
, base
->hash
);
3439 read_mmblob(&our_file
, ours
->hash
);
3440 read_mmblob(&their_file
, theirs
->hash
);
3441 status
= ll_merge(&result
, path
,
3444 &their_file
, "theirs", NULL
);
3445 free(base_file
.ptr
);
3447 free(their_file
.ptr
);
3448 if (status
< 0 || !result
.ptr
) {
3453 image
->buf
= result
.ptr
;
3454 image
->len
= result
.size
;
3460 * When directly falling back to add/add three-way merge, we read from
3461 * the current contents of the new_name. In no cases other than that
3462 * this function will be called.
3464 static int load_current(struct apply_state
*state
,
3465 struct image
*image
,
3466 struct patch
*patch
)
3468 struct strbuf buf
= STRBUF_INIT
;
3473 struct cache_entry
*ce
;
3474 char *name
= patch
->new_name
;
3475 unsigned mode
= patch
->new_mode
;
3478 die("BUG: patch to %s is not a creation", patch
->old_name
);
3480 pos
= cache_name_pos(name
, strlen(name
));
3482 return error(_("%s: does not exist in index"), name
);
3483 ce
= active_cache
[pos
];
3484 if (lstat(name
, &st
)) {
3485 if (errno
!= ENOENT
)
3486 return error(_("%s: %s"), name
, strerror(errno
));
3487 if (checkout_target(&the_index
, ce
, &st
))
3490 if (verify_index_match(ce
, &st
))
3491 return error(_("%s: does not match index"), name
);
3493 status
= load_patch_target(state
, &buf
, ce
, &st
, name
, mode
);
3498 img
= strbuf_detach(&buf
, &len
);
3499 prepare_image(image
, img
, len
, !patch
->is_binary
);
3503 static int try_threeway(struct apply_state
*state
,
3504 struct image
*image
,
3505 struct patch
*patch
,
3507 const struct cache_entry
*ce
)
3509 struct object_id pre_oid
, post_oid
, our_oid
;
3510 struct strbuf buf
= STRBUF_INIT
;
3514 struct image tmp_image
;
3516 /* No point falling back to 3-way merge in these cases */
3517 if (patch
->is_delete
||
3518 S_ISGITLINK(patch
->old_mode
) || S_ISGITLINK(patch
->new_mode
))
3521 /* Preimage the patch was prepared for */
3523 write_sha1_file("", 0, blob_type
, pre_oid
.hash
);
3524 else if (get_sha1(patch
->old_sha1_prefix
, pre_oid
.hash
) ||
3525 read_blob_object(&buf
, &pre_oid
, patch
->old_mode
))
3526 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3528 fprintf(stderr
, "Falling back to three-way merge...\n");
3530 img
= strbuf_detach(&buf
, &len
);
3531 prepare_image(&tmp_image
, img
, len
, 1);
3532 /* Apply the patch to get the post image */
3533 if (apply_fragments(state
, &tmp_image
, patch
) < 0) {
3534 clear_image(&tmp_image
);
3537 /* post_sha1[] is theirs */
3538 write_sha1_file(tmp_image
.buf
, tmp_image
.len
, blob_type
, post_oid
.hash
);
3539 clear_image(&tmp_image
);
3541 /* our_sha1[] is ours */
3542 if (patch
->is_new
) {
3543 if (load_current(state
, &tmp_image
, patch
))
3544 return error("cannot read the current contents of '%s'",
3547 if (load_preimage(state
, &tmp_image
, patch
, st
, ce
))
3548 return error("cannot read the current contents of '%s'",
3551 write_sha1_file(tmp_image
.buf
, tmp_image
.len
, blob_type
, our_oid
.hash
);
3552 clear_image(&tmp_image
);
3554 /* in-core three-way merge between post and our using pre as base */
3555 status
= three_way_merge(image
, patch
->new_name
,
3556 &pre_oid
, &our_oid
, &post_oid
);
3558 fprintf(stderr
, "Failed to fall back on three-way merge...\n");
3563 patch
->conflicted_threeway
= 1;
3565 oidclr(&patch
->threeway_stage
[0]);
3567 oidcpy(&patch
->threeway_stage
[0], &pre_oid
);
3568 oidcpy(&patch
->threeway_stage
[1], &our_oid
);
3569 oidcpy(&patch
->threeway_stage
[2], &post_oid
);
3570 fprintf(stderr
, "Applied patch to '%s' with conflicts.\n", patch
->new_name
);
3572 fprintf(stderr
, "Applied patch to '%s' cleanly.\n", patch
->new_name
);
3577 static int apply_data(struct apply_state
*state
, struct patch
*patch
,
3578 struct stat
*st
, const struct cache_entry
*ce
)
3582 if (load_preimage(state
, &image
, patch
, st
, ce
) < 0)
3585 if (patch
->direct_to_threeway
||
3586 apply_fragments(state
, &image
, patch
) < 0) {
3587 /* Note: with --reject, apply_fragments() returns 0 */
3588 if (!state
->threeway
|| try_threeway(state
, &image
, patch
, st
, ce
) < 0)
3591 patch
->result
= image
.buf
;
3592 patch
->resultsize
= image
.len
;
3593 add_to_fn_table(state
, patch
);
3594 free(image
.line_allocated
);
3596 if (0 < patch
->is_delete
&& patch
->resultsize
)
3597 return error(_("removal patch leaves file contents"));
3603 * If "patch" that we are looking at modifies or deletes what we have,
3604 * we would want it not to lose any local modification we have, either
3605 * in the working tree or in the index.
3607 * This also decides if a non-git patch is a creation patch or a
3608 * modification to an existing empty file. We do not check the state
3609 * of the current tree for a creation patch in this function; the caller
3610 * check_patch() separately makes sure (and errors out otherwise) that
3611 * the path the patch creates does not exist in the current tree.
3613 static int check_preimage(struct apply_state
*state
,
3614 struct patch
*patch
,
3615 struct cache_entry
**ce
,
3618 const char *old_name
= patch
->old_name
;
3619 struct patch
*previous
= NULL
;
3620 int stat_ret
= 0, status
;
3621 unsigned st_mode
= 0;
3626 assert(patch
->is_new
<= 0);
3627 previous
= previous_patch(state
, patch
, &status
);
3630 return error(_("path %s has been renamed/deleted"), old_name
);
3632 st_mode
= previous
->new_mode
;
3633 } else if (!state
->cached
) {
3634 stat_ret
= lstat(old_name
, st
);
3635 if (stat_ret
&& errno
!= ENOENT
)
3636 return error(_("%s: %s"), old_name
, strerror(errno
));
3639 if (state
->check_index
&& !previous
) {
3640 int pos
= cache_name_pos(old_name
, strlen(old_name
));
3642 if (patch
->is_new
< 0)
3644 return error(_("%s: does not exist in index"), old_name
);
3646 *ce
= active_cache
[pos
];
3648 if (checkout_target(&the_index
, *ce
, st
))
3651 if (!state
->cached
&& verify_index_match(*ce
, st
))
3652 return error(_("%s: does not match index"), old_name
);
3654 st_mode
= (*ce
)->ce_mode
;
3655 } else if (stat_ret
< 0) {
3656 if (patch
->is_new
< 0)
3658 return error(_("%s: %s"), old_name
, strerror(errno
));
3661 if (!state
->cached
&& !previous
)
3662 st_mode
= ce_mode_from_stat(*ce
, st
->st_mode
);
3664 if (patch
->is_new
< 0)
3666 if (!patch
->old_mode
)
3667 patch
->old_mode
= st_mode
;
3668 if ((st_mode
^ patch
->old_mode
) & S_IFMT
)
3669 return error(_("%s: wrong type"), old_name
);
3670 if (st_mode
!= patch
->old_mode
)
3671 warning(_("%s has type %o, expected %o"),
3672 old_name
, st_mode
, patch
->old_mode
);
3673 if (!patch
->new_mode
&& !patch
->is_delete
)
3674 patch
->new_mode
= st_mode
;
3679 patch
->is_delete
= 0;
3680 free(patch
->old_name
);
3681 patch
->old_name
= NULL
;
3686 #define EXISTS_IN_INDEX 1
3687 #define EXISTS_IN_WORKTREE 2
3689 static int check_to_create(struct apply_state
*state
,
3690 const char *new_name
,
3695 if (state
->check_index
&&
3696 cache_name_pos(new_name
, strlen(new_name
)) >= 0 &&
3698 return EXISTS_IN_INDEX
;
3702 if (!lstat(new_name
, &nst
)) {
3703 if (S_ISDIR(nst
.st_mode
) || ok_if_exists
)
3706 * A leading component of new_name might be a symlink
3707 * that is going to be removed with this patch, but
3708 * still pointing at somewhere that has the path.
3709 * In such a case, path "new_name" does not exist as
3710 * far as git is concerned.
3712 if (has_symlink_leading_path(new_name
, strlen(new_name
)))
3715 return EXISTS_IN_WORKTREE
;
3716 } else if ((errno
!= ENOENT
) && (errno
!= ENOTDIR
)) {
3717 return error("%s: %s", new_name
, strerror(errno
));
3722 static uintptr_t register_symlink_changes(struct apply_state
*state
,
3726 struct string_list_item
*ent
;
3728 ent
= string_list_lookup(&state
->symlink_changes
, path
);
3730 ent
= string_list_insert(&state
->symlink_changes
, path
);
3731 ent
->util
= (void *)0;
3733 ent
->util
= (void *)(what
| ((uintptr_t)ent
->util
));
3734 return (uintptr_t)ent
->util
;
3737 static uintptr_t check_symlink_changes(struct apply_state
*state
, const char *path
)
3739 struct string_list_item
*ent
;
3741 ent
= string_list_lookup(&state
->symlink_changes
, path
);
3744 return (uintptr_t)ent
->util
;
3747 static void prepare_symlink_changes(struct apply_state
*state
, struct patch
*patch
)
3749 for ( ; patch
; patch
= patch
->next
) {
3750 if ((patch
->old_name
&& S_ISLNK(patch
->old_mode
)) &&
3751 (patch
->is_rename
|| patch
->is_delete
))
3752 /* the symlink at patch->old_name is removed */
3753 register_symlink_changes(state
, patch
->old_name
, SYMLINK_GOES_AWAY
);
3755 if (patch
->new_name
&& S_ISLNK(patch
->new_mode
))
3756 /* the symlink at patch->new_name is created or remains */
3757 register_symlink_changes(state
, patch
->new_name
, SYMLINK_IN_RESULT
);
3761 static int path_is_beyond_symlink_1(struct apply_state
*state
, struct strbuf
*name
)
3764 unsigned int change
;
3766 while (--name
->len
&& name
->buf
[name
->len
] != '/')
3767 ; /* scan backwards */
3770 name
->buf
[name
->len
] = '\0';
3771 change
= check_symlink_changes(state
, name
->buf
);
3772 if (change
& SYMLINK_IN_RESULT
)
3774 if (change
& SYMLINK_GOES_AWAY
)
3776 * This cannot be "return 0", because we may
3777 * see a new one created at a higher level.
3781 /* otherwise, check the preimage */
3782 if (state
->check_index
) {
3783 struct cache_entry
*ce
;
3785 ce
= cache_file_exists(name
->buf
, name
->len
, ignore_case
);
3786 if (ce
&& S_ISLNK(ce
->ce_mode
))
3790 if (!lstat(name
->buf
, &st
) && S_ISLNK(st
.st_mode
))
3797 static int path_is_beyond_symlink(struct apply_state
*state
, const char *name_
)
3800 struct strbuf name
= STRBUF_INIT
;
3802 assert(*name_
!= '\0');
3803 strbuf_addstr(&name
, name_
);
3804 ret
= path_is_beyond_symlink_1(state
, &name
);
3805 strbuf_release(&name
);
3810 static void die_on_unsafe_path(struct patch
*patch
)
3812 const char *old_name
= NULL
;
3813 const char *new_name
= NULL
;
3814 if (patch
->is_delete
)
3815 old_name
= patch
->old_name
;
3816 else if (!patch
->is_new
&& !patch
->is_copy
)
3817 old_name
= patch
->old_name
;
3818 if (!patch
->is_delete
)
3819 new_name
= patch
->new_name
;
3821 if (old_name
&& !verify_path(old_name
))
3822 die(_("invalid path '%s'"), old_name
);
3823 if (new_name
&& !verify_path(new_name
))
3824 die(_("invalid path '%s'"), new_name
);
3828 * Check and apply the patch in-core; leave the result in patch->result
3829 * for the caller to write it out to the final destination.
3831 static int check_patch(struct apply_state
*state
, struct patch
*patch
)
3834 const char *old_name
= patch
->old_name
;
3835 const char *new_name
= patch
->new_name
;
3836 const char *name
= old_name
? old_name
: new_name
;
3837 struct cache_entry
*ce
= NULL
;
3838 struct patch
*tpatch
;
3842 patch
->rejected
= 1; /* we will drop this after we succeed */
3844 status
= check_preimage(state
, patch
, &ce
, &st
);
3847 old_name
= patch
->old_name
;
3850 * A type-change diff is always split into a patch to delete
3851 * old, immediately followed by a patch to create new (see
3852 * diff.c::run_diff()); in such a case it is Ok that the entry
3853 * to be deleted by the previous patch is still in the working
3854 * tree and in the index.
3856 * A patch to swap-rename between A and B would first rename A
3857 * to B and then rename B to A. While applying the first one,
3858 * the presence of B should not stop A from getting renamed to
3859 * B; ask to_be_deleted() about the later rename. Removal of
3860 * B and rename from A to B is handled the same way by asking
3863 if ((tpatch
= in_fn_table(state
, new_name
)) &&
3864 (was_deleted(tpatch
) || to_be_deleted(tpatch
)))
3870 ((0 < patch
->is_new
) || patch
->is_rename
|| patch
->is_copy
)) {
3871 int err
= check_to_create(state
, new_name
, ok_if_exists
);
3873 if (err
&& state
->threeway
) {
3874 patch
->direct_to_threeway
= 1;
3875 } else switch (err
) {
3878 case EXISTS_IN_INDEX
:
3879 return error(_("%s: already exists in index"), new_name
);
3881 case EXISTS_IN_WORKTREE
:
3882 return error(_("%s: already exists in working directory"),
3888 if (!patch
->new_mode
) {
3889 if (0 < patch
->is_new
)
3890 patch
->new_mode
= S_IFREG
| 0644;
3892 patch
->new_mode
= patch
->old_mode
;
3896 if (new_name
&& old_name
) {
3897 int same
= !strcmp(old_name
, new_name
);
3898 if (!patch
->new_mode
)
3899 patch
->new_mode
= patch
->old_mode
;
3900 if ((patch
->old_mode
^ patch
->new_mode
) & S_IFMT
) {
3902 return error(_("new mode (%o) of %s does not "
3903 "match old mode (%o)"),
3904 patch
->new_mode
, new_name
,
3907 return error(_("new mode (%o) of %s does not "
3908 "match old mode (%o) of %s"),
3909 patch
->new_mode
, new_name
,
3910 patch
->old_mode
, old_name
);
3914 if (!state
->unsafe_paths
)
3915 die_on_unsafe_path(patch
);
3918 * An attempt to read from or delete a path that is beyond a
3919 * symbolic link will be prevented by load_patch_target() that
3920 * is called at the beginning of apply_data() so we do not
3921 * have to worry about a patch marked with "is_delete" bit
3922 * here. We however need to make sure that the patch result
3923 * is not deposited to a path that is beyond a symbolic link
3926 if (!patch
->is_delete
&& path_is_beyond_symlink(state
, patch
->new_name
))
3927 return error(_("affected file '%s' is beyond a symbolic link"),
3930 if (apply_data(state
, patch
, &st
, ce
) < 0)
3931 return error(_("%s: patch does not apply"), name
);
3932 patch
->rejected
= 0;
3936 static int check_patch_list(struct apply_state
*state
, struct patch
*patch
)
3940 prepare_symlink_changes(state
, patch
);
3941 prepare_fn_table(state
, patch
);
3943 if (state
->apply_verbosely
)
3944 say_patch_name(stderr
,
3945 _("Checking patch %s..."), patch
);
3946 err
|= check_patch(state
, patch
);
3947 patch
= patch
->next
;
3952 /* This function tries to read the object ID from the current index */
3953 static int get_current_oid(const char *path
, struct object_id
*oid
)
3957 if (read_cache() < 0)
3959 pos
= cache_name_pos(path
, strlen(path
));
3962 oidcpy(oid
, &active_cache
[pos
]->oid
);
3966 static int preimage_oid_in_gitlink_patch(struct patch
*p
, struct object_id
*oid
)
3969 * A usable gitlink patch has only one fragment (hunk) that looks like:
3971 * -Subproject commit <old sha1>
3972 * +Subproject commit <new sha1>
3975 * -Subproject commit <old sha1>
3976 * for a removal patch.
3978 struct fragment
*hunk
= p
->fragments
;
3979 static const char heading
[] = "-Subproject commit ";
3982 if (/* does the patch have only one hunk? */
3983 hunk
&& !hunk
->next
&&
3984 /* is its preimage one line? */
3985 hunk
->oldpos
== 1 && hunk
->oldlines
== 1 &&
3986 /* does preimage begin with the heading? */
3987 (preimage
= memchr(hunk
->patch
, '\n', hunk
->size
)) != NULL
&&
3988 starts_with(++preimage
, heading
) &&
3989 /* does it record full SHA-1? */
3990 !get_oid_hex(preimage
+ sizeof(heading
) - 1, oid
) &&
3991 preimage
[sizeof(heading
) + GIT_SHA1_HEXSZ
- 1] == '\n' &&
3992 /* does the abbreviated name on the index line agree with it? */
3993 starts_with(preimage
+ sizeof(heading
) - 1, p
->old_sha1_prefix
))
3994 return 0; /* it all looks fine */
3996 /* we may have full object name on the index line */
3997 return get_oid_hex(p
->old_sha1_prefix
, oid
);
4000 /* Build an index that contains the just the files needed for a 3way merge */
4001 static void build_fake_ancestor(struct patch
*list
, const char *filename
)
4003 struct patch
*patch
;
4004 struct index_state result
= { NULL
};
4005 static struct lock_file lock
;
4007 /* Once we start supporting the reverse patch, it may be
4008 * worth showing the new sha1 prefix, but until then...
4010 for (patch
= list
; patch
; patch
= patch
->next
) {
4011 struct object_id oid
;
4012 struct cache_entry
*ce
;
4015 name
= patch
->old_name
? patch
->old_name
: patch
->new_name
;
4016 if (0 < patch
->is_new
)
4019 if (S_ISGITLINK(patch
->old_mode
)) {
4020 if (!preimage_oid_in_gitlink_patch(patch
, &oid
))
4021 ; /* ok, the textual part looks sane */
4023 die("sha1 information is lacking or useless for submodule %s",
4025 } else if (!get_sha1_blob(patch
->old_sha1_prefix
, oid
.hash
)) {
4027 } else if (!patch
->lines_added
&& !patch
->lines_deleted
) {
4028 /* mode-only change: update the current */
4029 if (get_current_oid(patch
->old_name
, &oid
))
4030 die("mode change for %s, which is not "
4031 "in current HEAD", name
);
4033 die("sha1 information is lacking or useless "
4036 ce
= make_cache_entry(patch
->old_mode
, oid
.hash
, name
, 0, 0);
4038 die(_("make_cache_entry failed for path '%s'"), name
);
4039 if (add_index_entry(&result
, ce
, ADD_CACHE_OK_TO_ADD
))
4040 die ("Could not add %s to temporary index", name
);
4043 hold_lock_file_for_update(&lock
, filename
, LOCK_DIE_ON_ERROR
);
4044 if (write_locked_index(&result
, &lock
, COMMIT_LOCK
))
4045 die ("Could not write temporary index to %s", filename
);
4047 discard_index(&result
);
4050 static void stat_patch_list(struct apply_state
*state
, struct patch
*patch
)
4052 int files
, adds
, dels
;
4054 for (files
= adds
= dels
= 0 ; patch
; patch
= patch
->next
) {
4056 adds
+= patch
->lines_added
;
4057 dels
+= patch
->lines_deleted
;
4058 show_stats(state
, patch
);
4061 print_stat_summary(stdout
, files
, adds
, dels
);
4064 static void numstat_patch_list(struct apply_state
*state
,
4065 struct patch
*patch
)
4067 for ( ; patch
; patch
= patch
->next
) {
4069 name
= patch
->new_name
? patch
->new_name
: patch
->old_name
;
4070 if (patch
->is_binary
)
4073 printf("%d\t%d\t", patch
->lines_added
, patch
->lines_deleted
);
4074 write_name_quoted(name
, stdout
, state
->line_termination
);
4078 static void show_file_mode_name(const char *newdelete
, unsigned int mode
, const char *name
)
4081 printf(" %s mode %06o %s\n", newdelete
, mode
, name
);
4083 printf(" %s %s\n", newdelete
, name
);
4086 static void show_mode_change(struct patch
*p
, int show_name
)
4088 if (p
->old_mode
&& p
->new_mode
&& p
->old_mode
!= p
->new_mode
) {
4090 printf(" mode change %06o => %06o %s\n",
4091 p
->old_mode
, p
->new_mode
, p
->new_name
);
4093 printf(" mode change %06o => %06o\n",
4094 p
->old_mode
, p
->new_mode
);
4098 static void show_rename_copy(struct patch
*p
)
4100 const char *renamecopy
= p
->is_rename
? "rename" : "copy";
4101 const char *old
, *new;
4103 /* Find common prefix */
4107 const char *slash_old
, *slash_new
;
4108 slash_old
= strchr(old
, '/');
4109 slash_new
= strchr(new, '/');
4112 slash_old
- old
!= slash_new
- new ||
4113 memcmp(old
, new, slash_new
- new))
4115 old
= slash_old
+ 1;
4116 new = slash_new
+ 1;
4118 /* p->old_name thru old is the common prefix, and old and new
4119 * through the end of names are renames
4121 if (old
!= p
->old_name
)
4122 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
4123 (int)(old
- p
->old_name
), p
->old_name
,
4124 old
, new, p
->score
);
4126 printf(" %s %s => %s (%d%%)\n", renamecopy
,
4127 p
->old_name
, p
->new_name
, p
->score
);
4128 show_mode_change(p
, 0);
4131 static void summary_patch_list(struct patch
*patch
)
4135 for (p
= patch
; p
; p
= p
->next
) {
4137 show_file_mode_name("create", p
->new_mode
, p
->new_name
);
4138 else if (p
->is_delete
)
4139 show_file_mode_name("delete", p
->old_mode
, p
->old_name
);
4141 if (p
->is_rename
|| p
->is_copy
)
4142 show_rename_copy(p
);
4145 printf(" rewrite %s (%d%%)\n",
4146 p
->new_name
, p
->score
);
4147 show_mode_change(p
, 0);
4150 show_mode_change(p
, 1);
4156 static void patch_stats(struct apply_state
*state
, struct patch
*patch
)
4158 int lines
= patch
->lines_added
+ patch
->lines_deleted
;
4160 if (lines
> state
->max_change
)
4161 state
->max_change
= lines
;
4162 if (patch
->old_name
) {
4163 int len
= quote_c_style(patch
->old_name
, NULL
, NULL
, 0);
4165 len
= strlen(patch
->old_name
);
4166 if (len
> state
->max_len
)
4167 state
->max_len
= len
;
4169 if (patch
->new_name
) {
4170 int len
= quote_c_style(patch
->new_name
, NULL
, NULL
, 0);
4172 len
= strlen(patch
->new_name
);
4173 if (len
> state
->max_len
)
4174 state
->max_len
= len
;
4178 static void remove_file(struct apply_state
*state
, struct patch
*patch
, int rmdir_empty
)
4180 if (state
->update_index
) {
4181 if (remove_file_from_cache(patch
->old_name
) < 0)
4182 die(_("unable to remove %s from index"), patch
->old_name
);
4184 if (!state
->cached
) {
4185 if (!remove_or_warn(patch
->old_mode
, patch
->old_name
) && rmdir_empty
) {
4186 remove_path(patch
->old_name
);
4191 static void add_index_file(struct apply_state
*state
,
4198 struct cache_entry
*ce
;
4199 int namelen
= strlen(path
);
4200 unsigned ce_size
= cache_entry_size(namelen
);
4202 if (!state
->update_index
)
4205 ce
= xcalloc(1, ce_size
);
4206 memcpy(ce
->name
, path
, namelen
);
4207 ce
->ce_mode
= create_ce_mode(mode
);
4208 ce
->ce_flags
= create_ce_flags(0);
4209 ce
->ce_namelen
= namelen
;
4210 if (S_ISGITLINK(mode
)) {
4213 if (!skip_prefix(buf
, "Subproject commit ", &s
) ||
4214 get_oid_hex(s
, &ce
->oid
))
4215 die(_("corrupt patch for submodule %s"), path
);
4217 if (!state
->cached
) {
4218 if (lstat(path
, &st
) < 0)
4219 die_errno(_("unable to stat newly created file '%s'"),
4221 fill_stat_cache_info(ce
, &st
);
4223 if (write_sha1_file(buf
, size
, blob_type
, ce
->oid
.hash
) < 0)
4224 die(_("unable to create backing store for newly created file %s"), path
);
4226 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
4227 die(_("unable to add cache entry for %s"), path
);
4230 static int try_create_file(const char *path
, unsigned int mode
, const char *buf
, unsigned long size
)
4233 struct strbuf nbuf
= STRBUF_INIT
;
4235 if (S_ISGITLINK(mode
)) {
4237 if (!lstat(path
, &st
) && S_ISDIR(st
.st_mode
))
4239 return mkdir(path
, 0777);
4242 if (has_symlinks
&& S_ISLNK(mode
))
4243 /* Although buf:size is counted string, it also is NUL
4246 return symlink(buf
, path
);
4248 fd
= open(path
, O_CREAT
| O_EXCL
| O_WRONLY
, (mode
& 0100) ? 0777 : 0666);
4252 if (convert_to_working_tree(path
, buf
, size
, &nbuf
)) {
4256 write_or_die(fd
, buf
, size
);
4257 strbuf_release(&nbuf
);
4260 die_errno(_("closing file '%s'"), path
);
4265 * We optimistically assume that the directories exist,
4266 * which is true 99% of the time anyway. If they don't,
4267 * we create them and try again.
4269 static void create_one_file(struct apply_state
*state
,
4277 if (!try_create_file(path
, mode
, buf
, size
))
4280 if (errno
== ENOENT
) {
4281 if (safe_create_leading_directories(path
))
4283 if (!try_create_file(path
, mode
, buf
, size
))
4287 if (errno
== EEXIST
|| errno
== EACCES
) {
4288 /* We may be trying to create a file where a directory
4292 if (!lstat(path
, &st
) && (!S_ISDIR(st
.st_mode
) || !rmdir(path
)))
4296 if (errno
== EEXIST
) {
4297 unsigned int nr
= getpid();
4300 char newpath
[PATH_MAX
];
4301 mksnpath(newpath
, sizeof(newpath
), "%s~%u", path
, nr
);
4302 if (!try_create_file(newpath
, mode
, buf
, size
)) {
4303 if (!rename(newpath
, path
))
4305 unlink_or_warn(newpath
);
4308 if (errno
!= EEXIST
)
4313 die_errno(_("unable to write file '%s' mode %o"), path
, mode
);
4316 static void add_conflicted_stages_file(struct apply_state
*state
,
4317 struct patch
*patch
)
4320 unsigned ce_size
, mode
;
4321 struct cache_entry
*ce
;
4323 if (!state
->update_index
)
4325 namelen
= strlen(patch
->new_name
);
4326 ce_size
= cache_entry_size(namelen
);
4327 mode
= patch
->new_mode
? patch
->new_mode
: (S_IFREG
| 0644);
4329 remove_file_from_cache(patch
->new_name
);
4330 for (stage
= 1; stage
< 4; stage
++) {
4331 if (is_null_oid(&patch
->threeway_stage
[stage
- 1]))
4333 ce
= xcalloc(1, ce_size
);
4334 memcpy(ce
->name
, patch
->new_name
, namelen
);
4335 ce
->ce_mode
= create_ce_mode(mode
);
4336 ce
->ce_flags
= create_ce_flags(stage
);
4337 ce
->ce_namelen
= namelen
;
4338 oidcpy(&ce
->oid
, &patch
->threeway_stage
[stage
- 1]);
4339 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
) < 0)
4340 die(_("unable to add cache entry for %s"), patch
->new_name
);
4344 static void create_file(struct apply_state
*state
, struct patch
*patch
)
4346 char *path
= patch
->new_name
;
4347 unsigned mode
= patch
->new_mode
;
4348 unsigned long size
= patch
->resultsize
;
4349 char *buf
= patch
->result
;
4352 mode
= S_IFREG
| 0644;
4353 create_one_file(state
, path
, mode
, buf
, size
);
4355 if (patch
->conflicted_threeway
)
4356 add_conflicted_stages_file(state
, patch
);
4358 add_index_file(state
, path
, mode
, buf
, size
);
4361 /* phase zero is to remove, phase one is to create */
4362 static void write_out_one_result(struct apply_state
*state
,
4363 struct patch
*patch
,
4366 if (patch
->is_delete
> 0) {
4368 remove_file(state
, patch
, 1);
4371 if (patch
->is_new
> 0 || patch
->is_copy
) {
4373 create_file(state
, patch
);
4377 * Rename or modification boils down to the same
4378 * thing: remove the old, write the new
4381 remove_file(state
, patch
, patch
->is_rename
);
4383 create_file(state
, patch
);
4386 static int write_out_one_reject(struct apply_state
*state
, struct patch
*patch
)
4389 char namebuf
[PATH_MAX
];
4390 struct fragment
*frag
;
4392 struct strbuf sb
= STRBUF_INIT
;
4394 for (cnt
= 0, frag
= patch
->fragments
; frag
; frag
= frag
->next
) {
4395 if (!frag
->rejected
)
4401 if (state
->apply_verbosely
)
4402 say_patch_name(stderr
,
4403 _("Applied patch %s cleanly."), patch
);
4407 /* This should not happen, because a removal patch that leaves
4408 * contents are marked "rejected" at the patch level.
4410 if (!patch
->new_name
)
4411 die(_("internal error"));
4413 /* Say this even without --verbose */
4414 strbuf_addf(&sb
, Q_("Applying patch %%s with %d reject...",
4415 "Applying patch %%s with %d rejects...",
4418 say_patch_name(stderr
, sb
.buf
, patch
);
4419 strbuf_release(&sb
);
4421 cnt
= strlen(patch
->new_name
);
4422 if (ARRAY_SIZE(namebuf
) <= cnt
+ 5) {
4423 cnt
= ARRAY_SIZE(namebuf
) - 5;
4424 warning(_("truncating .rej filename to %.*s.rej"),
4425 cnt
- 1, patch
->new_name
);
4427 memcpy(namebuf
, patch
->new_name
, cnt
);
4428 memcpy(namebuf
+ cnt
, ".rej", 5);
4430 rej
= fopen(namebuf
, "w");
4432 return error(_("cannot open %s: %s"), namebuf
, strerror(errno
));
4434 /* Normal git tools never deal with .rej, so do not pretend
4435 * this is a git patch by saying --git or giving extended
4436 * headers. While at it, maybe please "kompare" that wants
4437 * the trailing TAB and some garbage at the end of line ;-).
4439 fprintf(rej
, "diff a/%s b/%s\t(rejected hunks)\n",
4440 patch
->new_name
, patch
->new_name
);
4441 for (cnt
= 1, frag
= patch
->fragments
;
4443 cnt
++, frag
= frag
->next
) {
4444 if (!frag
->rejected
) {
4445 fprintf_ln(stderr
, _("Hunk #%d applied cleanly."), cnt
);
4448 fprintf_ln(stderr
, _("Rejected hunk #%d."), cnt
);
4449 fprintf(rej
, "%.*s", frag
->size
, frag
->patch
);
4450 if (frag
->patch
[frag
->size
-1] != '\n')
4457 static int write_out_results(struct apply_state
*state
, struct patch
*list
)
4462 struct string_list cpath
= STRING_LIST_INIT_DUP
;
4464 for (phase
= 0; phase
< 2; phase
++) {
4470 write_out_one_result(state
, l
, phase
);
4472 if (write_out_one_reject(state
, l
))
4474 if (l
->conflicted_threeway
) {
4475 string_list_append(&cpath
, l
->new_name
);
4485 struct string_list_item
*item
;
4487 string_list_sort(&cpath
);
4488 for_each_string_list_item(item
, &cpath
)
4489 fprintf(stderr
, "U %s\n", item
->string
);
4490 string_list_clear(&cpath
, 0);
4498 static struct lock_file lock_file
;
4500 #define INACCURATE_EOF (1<<0)
4501 #define RECOUNT (1<<1)
4503 static int apply_patch(struct apply_state
*state
,
4505 const char *filename
,
4509 struct strbuf buf
= STRBUF_INIT
; /* owns the patch text */
4510 struct patch
*list
= NULL
, **listp
= &list
;
4511 int skipped_patch
= 0;
4513 state
->patch_input_file
= filename
;
4514 read_patch_file(&buf
, fd
);
4516 while (offset
< buf
.len
) {
4517 struct patch
*patch
;
4520 patch
= xcalloc(1, sizeof(*patch
));
4521 patch
->inaccurate_eof
= !!(options
& INACCURATE_EOF
);
4522 patch
->recount
= !!(options
& RECOUNT
);
4523 nr
= parse_chunk(state
, buf
.buf
+ offset
, buf
.len
- offset
, patch
);
4528 if (state
->apply_in_reverse
)
4529 reverse_patches(patch
);
4530 if (use_patch(state
, patch
)) {
4531 patch_stats(state
, patch
);
4533 listp
= &patch
->next
;
4536 if (state
->apply_verbosely
)
4537 say_patch_name(stderr
, _("Skipped patch '%s'."), patch
);
4544 if (!list
&& !skipped_patch
)
4545 die(_("unrecognized input"));
4547 if (state
->whitespace_error
&& (state
->ws_error_action
== die_on_ws_error
))
4550 state
->update_index
= state
->check_index
&& state
->apply
;
4551 if (state
->update_index
&& state
->newfd
< 0)
4552 state
->newfd
= hold_locked_index(state
->lock_file
, 1);
4554 if (state
->check_index
) {
4555 if (read_cache() < 0)
4556 die(_("unable to read index file"));
4559 if ((state
->check
|| state
->apply
) &&
4560 check_patch_list(state
, list
) < 0 &&
4561 !state
->apply_with_reject
)
4564 if (state
->apply
&& write_out_results(state
, list
)) {
4565 if (state
->apply_with_reject
)
4567 /* with --3way, we still need to write the index out */
4571 if (state
->fake_ancestor
)
4572 build_fake_ancestor(list
, state
->fake_ancestor
);
4574 if (state
->diffstat
)
4575 stat_patch_list(state
, list
);
4578 numstat_patch_list(state
, list
);
4581 summary_patch_list(list
);
4583 free_patch_list(list
);
4584 strbuf_release(&buf
);
4585 string_list_clear(&state
->fn_table
, 0);
4589 static void git_apply_config(void)
4591 git_config_get_string_const("apply.whitespace", &apply_default_whitespace
);
4592 git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace
);
4593 git_config(git_default_config
, NULL
);
4596 static int option_parse_exclude(const struct option
*opt
,
4597 const char *arg
, int unset
)
4599 struct apply_state
*state
= opt
->value
;
4600 add_name_limit(state
, arg
, 1);
4604 static int option_parse_include(const struct option
*opt
,
4605 const char *arg
, int unset
)
4607 struct apply_state
*state
= opt
->value
;
4608 add_name_limit(state
, arg
, 0);
4609 state
->has_include
= 1;
4613 static int option_parse_p(const struct option
*opt
,
4617 struct apply_state
*state
= opt
->value
;
4618 state
->p_value
= atoi(arg
);
4619 state
->p_value_known
= 1;
4623 static int option_parse_space_change(const struct option
*opt
,
4624 const char *arg
, int unset
)
4626 struct apply_state
*state
= opt
->value
;
4628 state
->ws_ignore_action
= ignore_ws_none
;
4630 state
->ws_ignore_action
= ignore_ws_change
;
4634 static int option_parse_whitespace(const struct option
*opt
,
4635 const char *arg
, int unset
)
4637 struct apply_state
*state
= opt
->value
;
4638 state
->whitespace_option
= arg
;
4639 parse_whitespace_option(state
, arg
);
4643 static int option_parse_directory(const struct option
*opt
,
4644 const char *arg
, int unset
)
4646 struct apply_state
*state
= opt
->value
;
4647 strbuf_reset(&state
->root
);
4648 strbuf_addstr(&state
->root
, arg
);
4649 strbuf_complete(&state
->root
, '/');
4653 static void init_apply_state(struct apply_state
*state
,
4655 struct lock_file
*lock_file
)
4657 memset(state
, 0, sizeof(*state
));
4658 state
->prefix
= prefix
;
4659 state
->prefix_length
= state
->prefix
? strlen(state
->prefix
) : 0;
4660 state
->lock_file
= lock_file
;
4663 state
->line_termination
= '\n';
4665 state
->p_context
= UINT_MAX
;
4666 state
->squelch_whitespace_errors
= 5;
4667 state
->ws_error_action
= warn_on_ws_error
;
4668 state
->ws_ignore_action
= ignore_ws_none
;
4670 string_list_init(&state
->fn_table
, 0);
4671 string_list_init(&state
->limit_by_name
, 0);
4672 string_list_init(&state
->symlink_changes
, 0);
4673 strbuf_init(&state
->root
, 0);
4676 if (apply_default_whitespace
)
4677 parse_whitespace_option(state
, apply_default_whitespace
);
4678 if (apply_default_ignorewhitespace
)
4679 parse_ignorewhitespace_option(state
, apply_default_ignorewhitespace
);
4682 static void clear_apply_state(struct apply_state
*state
)
4684 string_list_clear(&state
->limit_by_name
, 0);
4685 string_list_clear(&state
->symlink_changes
, 0);
4686 strbuf_release(&state
->root
);
4688 /* &state->fn_table is cleared at the end of apply_patch() */
4691 static void check_apply_state(struct apply_state
*state
, int force_apply
)
4693 int is_not_gitdir
= !startup_info
->have_repository
;
4695 if (state
->apply_with_reject
&& state
->threeway
)
4696 die("--reject and --3way cannot be used together.");
4697 if (state
->cached
&& state
->threeway
)
4698 die("--cached and --3way cannot be used together.");
4699 if (state
->threeway
) {
4701 die(_("--3way outside a repository"));
4702 state
->check_index
= 1;
4704 if (state
->apply_with_reject
)
4705 state
->apply
= state
->apply_verbosely
= 1;
4706 if (!force_apply
&& (state
->diffstat
|| state
->numstat
|| state
->summary
|| state
->check
|| state
->fake_ancestor
))
4708 if (state
->check_index
&& is_not_gitdir
)
4709 die(_("--index outside a repository"));
4710 if (state
->cached
) {
4712 die(_("--cached outside a repository"));
4713 state
->check_index
= 1;
4715 if (state
->check_index
)
4716 state
->unsafe_paths
= 0;
4717 if (!state
->lock_file
)
4718 die("BUG: state->lock_file should not be NULL");
4721 static int apply_all_patches(struct apply_state
*state
,
4730 for (i
= 0; i
< argc
; i
++) {
4731 const char *arg
= argv
[i
];
4734 if (!strcmp(arg
, "-")) {
4735 errs
|= apply_patch(state
, 0, "<stdin>", options
);
4738 } else if (0 < state
->prefix_length
)
4739 arg
= prefix_filename(state
->prefix
,
4740 state
->prefix_length
,
4743 fd
= open(arg
, O_RDONLY
);
4745 die_errno(_("can't open patch '%s'"), arg
);
4747 set_default_whitespace_mode(state
);
4748 errs
|= apply_patch(state
, fd
, arg
, options
);
4751 set_default_whitespace_mode(state
);
4753 errs
|= apply_patch(state
, 0, "<stdin>", options
);
4755 if (state
->whitespace_error
) {
4756 if (state
->squelch_whitespace_errors
&&
4757 state
->squelch_whitespace_errors
< state
->whitespace_error
) {
4759 state
->whitespace_error
- state
->squelch_whitespace_errors
;
4760 warning(Q_("squelched %d whitespace error",
4761 "squelched %d whitespace errors",
4765 if (state
->ws_error_action
== die_on_ws_error
)
4766 die(Q_("%d line adds whitespace errors.",
4767 "%d lines add whitespace errors.",
4768 state
->whitespace_error
),
4769 state
->whitespace_error
);
4770 if (state
->applied_after_fixing_ws
&& state
->apply
)
4771 warning("%d line%s applied after"
4772 " fixing whitespace errors.",
4773 state
->applied_after_fixing_ws
,
4774 state
->applied_after_fixing_ws
== 1 ? "" : "s");
4775 else if (state
->whitespace_error
)
4776 warning(Q_("%d line adds whitespace errors.",
4777 "%d lines add whitespace errors.",
4778 state
->whitespace_error
),
4779 state
->whitespace_error
);
4782 if (state
->update_index
) {
4783 if (write_locked_index(&the_index
, state
->lock_file
, COMMIT_LOCK
))
4784 die(_("Unable to write new index file"));
4791 int cmd_apply(int argc
, const char **argv
, const char *prefix
)
4793 int force_apply
= 0;
4796 struct apply_state state
;
4798 struct option builtin_apply_options
[] = {
4799 { OPTION_CALLBACK
, 0, "exclude", &state
, N_("path"),
4800 N_("don't apply changes matching the given path"),
4801 0, option_parse_exclude
},
4802 { OPTION_CALLBACK
, 0, "include", &state
, N_("path"),
4803 N_("apply changes matching the given path"),
4804 0, option_parse_include
},
4805 { OPTION_CALLBACK
, 'p', NULL
, &state
, N_("num"),
4806 N_("remove <num> leading slashes from traditional diff paths"),
4807 0, option_parse_p
},
4808 OPT_BOOL(0, "no-add", &state
.no_add
,
4809 N_("ignore additions made by the patch")),
4810 OPT_BOOL(0, "stat", &state
.diffstat
,
4811 N_("instead of applying the patch, output diffstat for the input")),
4812 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
4813 OPT_NOOP_NOARG(0, "binary"),
4814 OPT_BOOL(0, "numstat", &state
.numstat
,
4815 N_("show number of added and deleted lines in decimal notation")),
4816 OPT_BOOL(0, "summary", &state
.summary
,
4817 N_("instead of applying the patch, output a summary for the input")),
4818 OPT_BOOL(0, "check", &state
.check
,
4819 N_("instead of applying the patch, see if the patch is applicable")),
4820 OPT_BOOL(0, "index", &state
.check_index
,
4821 N_("make sure the patch is applicable to the current index")),
4822 OPT_BOOL(0, "cached", &state
.cached
,
4823 N_("apply a patch without touching the working tree")),
4824 OPT_BOOL(0, "unsafe-paths", &state
.unsafe_paths
,
4825 N_("accept a patch that touches outside the working area")),
4826 OPT_BOOL(0, "apply", &force_apply
,
4827 N_("also apply the patch (use with --stat/--summary/--check)")),
4828 OPT_BOOL('3', "3way", &state
.threeway
,
4829 N_( "attempt three-way merge if a patch does not apply")),
4830 OPT_FILENAME(0, "build-fake-ancestor", &state
.fake_ancestor
,
4831 N_("build a temporary index based on embedded index information")),
4832 /* Think twice before adding "--nul" synonym to this */
4833 OPT_SET_INT('z', NULL
, &state
.line_termination
,
4834 N_("paths are separated with NUL character"), '\0'),
4835 OPT_INTEGER('C', NULL
, &state
.p_context
,
4836 N_("ensure at least <n> lines of context match")),
4837 { OPTION_CALLBACK
, 0, "whitespace", &state
, N_("action"),
4838 N_("detect new or modified lines that have whitespace errors"),
4839 0, option_parse_whitespace
},
4840 { OPTION_CALLBACK
, 0, "ignore-space-change", &state
, NULL
,
4841 N_("ignore changes in whitespace when finding context"),
4842 PARSE_OPT_NOARG
, option_parse_space_change
},
4843 { OPTION_CALLBACK
, 0, "ignore-whitespace", &state
, NULL
,
4844 N_("ignore changes in whitespace when finding context"),
4845 PARSE_OPT_NOARG
, option_parse_space_change
},
4846 OPT_BOOL('R', "reverse", &state
.apply_in_reverse
,
4847 N_("apply the patch in reverse")),
4848 OPT_BOOL(0, "unidiff-zero", &state
.unidiff_zero
,
4849 N_("don't expect at least one line of context")),
4850 OPT_BOOL(0, "reject", &state
.apply_with_reject
,
4851 N_("leave the rejected hunks in corresponding *.rej files")),
4852 OPT_BOOL(0, "allow-overlap", &state
.allow_overlap
,
4853 N_("allow overlapping hunks")),
4854 OPT__VERBOSE(&state
.apply_verbosely
, N_("be verbose")),
4855 OPT_BIT(0, "inaccurate-eof", &options
,
4856 N_("tolerate incorrectly detected missing new-line at the end of file"),
4858 OPT_BIT(0, "recount", &options
,
4859 N_("do not trust the line counts in the hunk headers"),
4861 { OPTION_CALLBACK
, 0, "directory", &state
, N_("root"),
4862 N_("prepend <root> to all filenames"),
4863 0, option_parse_directory
},
4867 init_apply_state(&state
, prefix
, &lock_file
);
4869 argc
= parse_options(argc
, argv
, state
.prefix
, builtin_apply_options
,
4872 check_apply_state(&state
, force_apply
);
4874 ret
= apply_all_patches(&state
, argc
, argv
, options
);
4876 clear_apply_state(&state
);