* Try to apply a patch. * * Returns: * -128 if a bad error happened (like patch unreadable) * -1 if patch did not apply and user cannot deal with it * 0 if the patch applied * 1 if the patch did not apply but user might fix it */
| 4868 | * 1 if the patch did not apply but user might fix it |
| 4869 | */ |
| 4870 | static int apply_patch(struct apply_state *state, |
| 4871 | int fd, |
| 4872 | const char *filename, |
| 4873 | int options) |
| 4874 | { |
| 4875 | size_t offset; |
| 4876 | struct strbuf buf = STRBUF_INIT; /* owns the patch text */ |
| 4877 | struct patch *list = NULL, **listp = &list; |
| 4878 | int skipped_patch = 0; |
| 4879 | int res = 0; |
| 4880 | int flush_attributes = 0; |
| 4881 | |
| 4882 | state->patch_input_file = filename; |
| 4883 | state->linenr = 1; |
| 4884 | if (read_patch_file(&buf, fd) < 0) { |
| 4885 | res = -128; |
| 4886 | goto end; |
| 4887 | } |
| 4888 | offset = 0; |
| 4889 | while (offset < buf.len) { |
| 4890 | struct patch *patch; |
| 4891 | int nr; |
| 4892 | |
| 4893 | CALLOC_ARRAY(patch, 1); |
| 4894 | patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF); |
| 4895 | patch->recount = !!(options & APPLY_OPT_RECOUNT); |
| 4896 | nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch); |
| 4897 | if (nr < 0) { |
| 4898 | free_patch(patch); |
| 4899 | if (nr == -128) { |
| 4900 | res = -128; |
| 4901 | goto end; |
| 4902 | } |
| 4903 | break; |
| 4904 | } |
| 4905 | if (state->apply_in_reverse) |
| 4906 | reverse_patches(patch); |
| 4907 | if (use_patch(state, patch)) { |
| 4908 | patch_stats(state, patch); |
| 4909 | if (!list || !state->apply_in_reverse) { |
| 4910 | *listp = patch; |
| 4911 | listp = &patch->next; |
| 4912 | } else { |
| 4913 | patch->next = list; |
| 4914 | list = patch; |
| 4915 | } |
| 4916 | |
| 4917 | if ((patch->new_name && |
| 4918 | ends_with_path_components(patch->new_name, |
| 4919 | GITATTRIBUTES_FILE)) || |
| 4920 | (patch->old_name && |
| 4921 | ends_with_path_components(patch->old_name, |
| 4922 | GITATTRIBUTES_FILE))) |
| 4923 | flush_attributes = 1; |
| 4924 | } |
| 4925 | else { |
| 4926 | if (state->apply_verbosity > verbosity_normal) |
| 4927 | say_patch_name(stderr, _("Skipped patch '%s'."), patch); |
no test coverage detected