Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 1 | #ifndef APPLY_H |
| 2 | #define APPLY_H |
| 3 | |
Denton Liu | 97b989e | 2019-09-25 01:20:53 -0700 | [diff] [blame] | 4 | #include "hash.h" |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 5 | #include "lockfile.h" |
| 6 | #include "string-list.h" |
René Scharfe | 4e9a325 | 2022-01-07 13:16:53 +0100 | [diff] [blame] | 7 | #include "strmap.h" |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 8 | |
Nguyễn Thái Ngọc Duy | 82ea77e | 2018-08-13 18:14:39 +0200 | [diff] [blame] | 9 | struct repository; |
| 10 | |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 11 | enum apply_ws_error_action { |
| 12 | nowarn_ws_error, |
| 13 | warn_on_ws_error, |
| 14 | die_on_ws_error, |
| 15 | correct_ws_error |
| 16 | }; |
| 17 | |
| 18 | enum apply_ws_ignore { |
| 19 | ignore_ws_none, |
| 20 | ignore_ws_change |
| 21 | }; |
| 22 | |
Christian Couder | a46160d | 2016-09-04 22:18:25 +0200 | [diff] [blame] | 23 | enum apply_verbosity { |
| 24 | verbosity_silent = -1, |
| 25 | verbosity_normal = 0, |
| 26 | verbosity_verbose = 1 |
| 27 | }; |
| 28 | |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 29 | struct apply_state { |
| 30 | const char *prefix; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 31 | |
Martin Ågren | d13cd4c | 2017-10-05 22:32:10 +0200 | [diff] [blame] | 32 | /* Lock file */ |
Martin Ågren | 6d058c8 | 2017-10-05 22:32:09 +0200 | [diff] [blame] | 33 | struct lock_file lock_file; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 34 | |
| 35 | /* These control what gets looked at and modified */ |
| 36 | int apply; /* this is not a dry-run */ |
| 37 | int cached; /* apply to the index only */ |
| 38 | int check; /* preimage must match working tree, don't actually apply */ |
| 39 | int check_index; /* preimage must match the indexed version */ |
| 40 | int update_index; /* check_index && apply */ |
Nguyễn Thái Ngọc Duy | cff5dc0 | 2018-05-26 14:08:46 +0200 | [diff] [blame] | 41 | int ita_only; /* add intent-to-add entries to the index */ |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 42 | |
| 43 | /* These control cosmetic aspect of the output */ |
| 44 | int diffstat; /* just show a diffstat, and don't actually apply */ |
| 45 | int numstat; /* just show a numeric diffstat, and don't actually apply */ |
| 46 | int summary; /* just report creation, deletion, etc, and don't actually apply */ |
| 47 | |
| 48 | /* These boolean parameters control how the apply is done */ |
| 49 | int allow_overlap; |
| 50 | int apply_in_reverse; |
| 51 | int apply_with_reject; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 52 | int no_add; |
| 53 | int threeway; |
| 54 | int unidiff_zero; |
| 55 | int unsafe_paths; |
Jerry Zhang | 324eb77 | 2021-12-13 14:03:27 -0800 | [diff] [blame] | 56 | int allow_empty; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 57 | |
| 58 | /* Other non boolean parameters */ |
Nguyễn Thái Ngọc Duy | 82ea77e | 2018-08-13 18:14:39 +0200 | [diff] [blame] | 59 | struct repository *repo; |
Christian Couder | 5b0b57f | 2016-09-04 22:18:32 +0200 | [diff] [blame] | 60 | const char *index_file; |
Christian Couder | a46160d | 2016-09-04 22:18:25 +0200 | [diff] [blame] | 61 | enum apply_verbosity apply_verbosity; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 62 | const char *fake_ancestor; |
| 63 | const char *patch_input_file; |
| 64 | int line_termination; |
| 65 | struct strbuf root; |
| 66 | int p_value; |
| 67 | int p_value_known; |
| 68 | unsigned int p_context; |
| 69 | |
| 70 | /* Exclude and include path parameters */ |
| 71 | struct string_list limit_by_name; |
| 72 | int has_include; |
| 73 | |
| 74 | /* Various "current state" */ |
| 75 | int linenr; /* current line number */ |
René Scharfe | 4e9a325 | 2022-01-07 13:16:53 +0100 | [diff] [blame] | 76 | /* |
| 77 | * We need to keep track of how symlinks in the preimage are |
| 78 | * manipulated by the patches. A patch to add a/b/c where a/b |
| 79 | * is a symlink should not be allowed to affect the directory |
| 80 | * the symlink points at, but if the same patch removes a/b, |
| 81 | * it is perfectly fine, as the patch removes a/b to make room |
| 82 | * to create a directory a/b so that a/b/c can be created. |
| 83 | */ |
| 84 | struct strset removed_symlinks; |
| 85 | struct strset kept_symlinks; |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * For "diff-stat" like behaviour, we keep track of the biggest change |
| 89 | * we've seen, and the longest filename. That allows us to do simple |
| 90 | * scaling. |
| 91 | */ |
| 92 | int max_change; |
| 93 | int max_len; |
| 94 | |
| 95 | /* |
| 96 | * Records filenames that have been touched, in order to handle |
| 97 | * the case where more than one patches touch the same file. |
| 98 | */ |
| 99 | struct string_list fn_table; |
| 100 | |
Christian Couder | 45b78d8 | 2016-09-04 22:18:29 +0200 | [diff] [blame] | 101 | /* |
| 102 | * This is to save reporting routines before using |
| 103 | * set_error_routine() or set_warn_routine() to install muting |
| 104 | * routines when in verbosity_silent mode. |
| 105 | */ |
| 106 | void (*saved_error_routine)(const char *err, va_list params); |
| 107 | void (*saved_warn_routine)(const char *warn, va_list params); |
| 108 | |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 109 | /* These control whitespace errors */ |
| 110 | enum apply_ws_error_action ws_error_action; |
| 111 | enum apply_ws_ignore ws_ignore_action; |
| 112 | const char *whitespace_option; |
| 113 | int whitespace_error; |
| 114 | int squelch_whitespace_errors; |
| 115 | int applied_after_fixing_ws; |
| 116 | }; |
| 117 | |
Thomas Gummerer | ef283b3 | 2019-07-11 17:08:44 +0100 | [diff] [blame] | 118 | /* |
| 119 | * This represents a "patch" to a file, both metainfo changes |
| 120 | * such as creation/deletion, filemode and content changes represented |
| 121 | * as a series of fragments. |
| 122 | */ |
| 123 | struct patch { |
| 124 | char *new_name, *old_name, *def_name; |
| 125 | unsigned int old_mode, new_mode; |
| 126 | int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */ |
| 127 | int rejected; |
| 128 | unsigned ws_rule; |
| 129 | int lines_added, lines_deleted; |
| 130 | int score; |
| 131 | int extension_linenr; /* first line specifying delete/new/rename/copy */ |
| 132 | unsigned int is_toplevel_relative:1; |
| 133 | unsigned int inaccurate_eof:1; |
| 134 | unsigned int is_binary:1; |
| 135 | unsigned int is_copy:1; |
| 136 | unsigned int is_rename:1; |
| 137 | unsigned int recount:1; |
| 138 | unsigned int conflicted_threeway:1; |
| 139 | unsigned int direct_to_threeway:1; |
| 140 | unsigned int crlf_in_old:1; |
| 141 | struct fragment *fragments; |
| 142 | char *result; |
| 143 | size_t resultsize; |
| 144 | char old_oid_prefix[GIT_MAX_HEXSZ + 1]; |
| 145 | char new_oid_prefix[GIT_MAX_HEXSZ + 1]; |
| 146 | struct patch *next; |
| 147 | |
| 148 | /* three-way fallback result */ |
| 149 | struct object_id threeway_stage[3]; |
| 150 | }; |
| 151 | |
Nguyễn Thái Ngọc Duy | 45635ec | 2018-06-30 11:20:20 +0200 | [diff] [blame] | 152 | int apply_parse_options(int argc, const char **argv, |
| 153 | struct apply_state *state, |
| 154 | int *force_apply, int *options, |
| 155 | const char * const *apply_usage); |
| 156 | int init_apply_state(struct apply_state *state, |
Nguyễn Thái Ngọc Duy | 82ea77e | 2018-08-13 18:14:39 +0200 | [diff] [blame] | 157 | struct repository *repo, |
Nguyễn Thái Ngọc Duy | 45635ec | 2018-06-30 11:20:20 +0200 | [diff] [blame] | 158 | const char *prefix); |
| 159 | void clear_apply_state(struct apply_state *state); |
| 160 | int check_apply_state(struct apply_state *state, int force_apply); |
Christian Couder | bb493a5 | 2016-08-08 23:03:07 +0200 | [diff] [blame] | 161 | |
Christian Couder | 803bf4e | 2016-09-04 22:18:21 +0200 | [diff] [blame] | 162 | /* |
Thomas Gummerer | ef283b3 | 2019-07-11 17:08:44 +0100 | [diff] [blame] | 163 | * Parse a git diff header, starting at line. Fills the relevant |
| 164 | * metadata information in 'struct patch'. |
| 165 | * |
| 166 | * Returns -1 on failure, the length of the parsed header otherwise. |
| 167 | */ |
| 168 | int parse_git_diff_header(struct strbuf *root, |
| 169 | int *linenr, |
| 170 | int p_value, |
| 171 | const char *line, |
| 172 | int len, |
| 173 | unsigned int size, |
| 174 | struct patch *patch); |
| 175 | |
Ævar Arnfjörð Bjarmason | 4998e93 | 2022-03-04 19:32:15 +0100 | [diff] [blame] | 176 | void release_patch(struct patch *patch); |
| 177 | |
Thomas Gummerer | ef283b3 | 2019-07-11 17:08:44 +0100 | [diff] [blame] | 178 | /* |
Christian Couder | 803bf4e | 2016-09-04 22:18:21 +0200 | [diff] [blame] | 179 | * Some aspects of the apply behavior are controlled by the following |
| 180 | * bits in the "options" parameter passed to apply_all_patches(). |
| 181 | */ |
| 182 | #define APPLY_OPT_INACCURATE_EOF (1<<0) /* accept inaccurate eof */ |
| 183 | #define APPLY_OPT_RECOUNT (1<<1) /* accept inaccurate line count */ |
| 184 | |
Nguyễn Thái Ngọc Duy | 45635ec | 2018-06-30 11:20:20 +0200 | [diff] [blame] | 185 | int apply_all_patches(struct apply_state *state, |
| 186 | int argc, const char **argv, |
| 187 | int options); |
Christian Couder | 13b5af2 | 2016-04-22 20:55:46 +0200 | [diff] [blame] | 188 | |
Christian Couder | 71501a7 | 2016-08-08 23:02:59 +0200 | [diff] [blame] | 189 | #endif |