Patrick Steinhardt | e7da938 | 2024-06-14 08:50:23 +0200 | [diff] [blame] | 1 | #define USE_THE_REPOSITORY_VARIABLE |
Patrick Steinhardt | 41f43b8 | 2024-12-06 11:27:19 +0100 | [diff] [blame] | 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
Patrick Steinhardt | e7da938 | 2024-06-14 08:50:23 +0200 | [diff] [blame] | 3 | |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 5 | #include "advice.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 6 | #include "strvec.h" |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 7 | #include "repository.h" |
Calvin Wan | b1bda75 | 2023-09-29 14:20:51 -0700 | [diff] [blame] | 8 | #include "parse.h" |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 9 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 10 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 11 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 12 | #include "hex.h" |
Elijah Newren | f565385 | 2023-05-16 06:33:50 +0000 | [diff] [blame] | 13 | #include "name-hash.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 14 | #include "tree.h" |
| 15 | #include "tree-walk.h" |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 16 | #include "cache-tree.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 17 | #include "unpack-trees.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 18 | #include "progress.h" |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 19 | #include "refs.h" |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 20 | #include "attr.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 21 | #include "read-cache.h" |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 22 | #include "split-index.h" |
Victoria Dye | 0f329b9 | 2022-05-10 23:32:32 +0000 | [diff] [blame] | 23 | #include "sparse-index.h" |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 24 | #include "submodule.h" |
| 25 | #include "submodule-config.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 26 | #include "symlinks.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 27 | #include "trace2.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 28 | #include "fsmonitor.h" |
Patrick Steinhardt | 68cd492 | 2025-04-15 11:38:23 +0200 | [diff] [blame] | 29 | #include "object-store.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 30 | #include "promisor-remote.h" |
Matheus Tavares | d052cc0 | 2021-03-23 11:19:32 -0300 | [diff] [blame] | 31 | #include "entry.h" |
Matheus Tavares | 04155bd | 2021-04-18 21:14:53 -0300 | [diff] [blame] | 32 | #include "parallel-checkout.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 33 | #include "setup.h" |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 34 | |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Error messages expected by scripts out of plumbing commands such as |
| 37 | * read-tree. Non-scripted Porcelain is not required to use these messages |
| 38 | * and in fact are encouraged to reword them to better suit their particular |
Diane Gasselin | 23cbf11 | 2010-08-11 10:38:05 +0200 | [diff] [blame] | 39 | * situation better. See how "git checkout" and "git merge" replaces |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 40 | * them using setup_unpack_trees_porcelain(), for example. |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 41 | */ |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 42 | static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = { |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 43 | /* ERROR_WOULD_OVERWRITE */ |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 44 | "Entry '%s' would be overwritten by merge. Cannot merge.", |
| 45 | |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 46 | /* ERROR_NOT_UPTODATE_FILE */ |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 47 | "Entry '%s' not uptodate. Cannot merge.", |
| 48 | |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 49 | /* ERROR_NOT_UPTODATE_DIR */ |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 50 | "Updating '%s' would lose untracked files in it", |
| 51 | |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 52 | /* ERROR_CWD_IN_THE_WAY */ |
| 53 | "Refusing to remove '%s' since it is the current working directory.", |
| 54 | |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 55 | /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */ |
| 56 | "Untracked working tree file '%s' would be overwritten by merge.", |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 57 | |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 58 | /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */ |
| 59 | "Untracked working tree file '%s' would be removed by merge.", |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 60 | |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 61 | /* ERROR_BIND_OVERLAP */ |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 62 | "Entry '%s' overlaps with '%s'. Cannot bind.", |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 63 | |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 64 | /* ERROR_WOULD_LOSE_SUBMODULE */ |
| 65 | "Submodule '%s' cannot checkout new HEAD.", |
Elijah Newren | cd002c1 | 2020-03-27 00:48:55 +0000 | [diff] [blame] | 66 | |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 67 | /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */ |
| 68 | "", |
| 69 | |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 70 | /* WARNING_SPARSE_NOT_UPTODATE_FILE */ |
Elijah Newren | 22ab0b3 | 2020-03-27 00:48:58 +0000 | [diff] [blame] | 71 | "Path '%s' not uptodate; will not remove from working tree.", |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 72 | |
Elijah Newren | ebb568b | 2020-03-27 00:48:59 +0000 | [diff] [blame] | 73 | /* WARNING_SPARSE_UNMERGED_FILE */ |
| 74 | "Path '%s' unmerged; will not remove from working tree.", |
| 75 | |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 76 | /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */ |
Elijah Newren | 22ab0b3 | 2020-03-27 00:48:58 +0000 | [diff] [blame] | 77 | "Path '%s' already present; will not overwrite with sparse update.", |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 80 | #define ERRORMSG(o,type) \ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 81 | ( ((o) && (o)->internal.msgs[(type)]) \ |
| 82 | ? ((o)->internal.msgs[(type)]) \ |
Matthieu Moy | 08353eb | 2010-08-11 10:38:04 +0200 | [diff] [blame] | 83 | : (unpack_plumbing_errors[(type)]) ) |
Junio C Hamano | 8ccba00 | 2008-05-17 12:03:49 -0700 | [diff] [blame] | 84 | |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 85 | static const char *super_prefixed(const char *path, const char *super_prefix) |
Stefan Beller | 3d41542 | 2017-01-17 17:05:20 -0800 | [diff] [blame] | 86 | { |
| 87 | /* |
| 88 | * It is necessary and sufficient to have two static buffers |
| 89 | * here, as the return value of this function is fed to |
| 90 | * error() using the unpack_*_errors[] templates we see above. |
| 91 | */ |
| 92 | static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT}; |
| 93 | static int super_prefix_len = -1; |
| 94 | static unsigned idx = ARRAY_SIZE(buf) - 1; |
| 95 | |
| 96 | if (super_prefix_len < 0) { |
Stefan Beller | 3d41542 | 2017-01-17 17:05:20 -0800 | [diff] [blame] | 97 | if (!super_prefix) { |
| 98 | super_prefix_len = 0; |
| 99 | } else { |
| 100 | int i; |
| 101 | for (i = 0; i < ARRAY_SIZE(buf); i++) |
| 102 | strbuf_addstr(&buf[i], super_prefix); |
| 103 | super_prefix_len = buf[0].len; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!super_prefix_len) |
| 108 | return path; |
| 109 | |
| 110 | if (++idx >= ARRAY_SIZE(buf)) |
| 111 | idx = 0; |
| 112 | |
| 113 | strbuf_setlen(&buf[idx], super_prefix_len); |
| 114 | strbuf_addstr(&buf[idx], path); |
| 115 | |
| 116 | return buf[idx].buf; |
| 117 | } |
| 118 | |
Matthieu Moy | e294030 | 2010-09-02 13:57:34 +0200 | [diff] [blame] | 119 | void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, |
| 120 | const char *cmd) |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 121 | { |
Clemens Buchacher | 7980872 | 2010-11-15 20:52:19 +0100 | [diff] [blame] | 122 | int i; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 123 | const char **msgs = opts->internal.msgs; |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 124 | const char *msg; |
Jeff King | fa3f60b | 2014-06-18 16:02:13 -0400 | [diff] [blame] | 125 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 126 | strvec_init(&opts->internal.msgs_to_free); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 127 | |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 128 | if (!strcmp(cmd, "checkout")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 129 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 130 | ? _("Your local changes to the following files would be overwritten by checkout:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 131 | "Please commit your changes or stash them before you switch branches.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 132 | : _("Your local changes to the following files would be overwritten by checkout:\n%%s"); |
| 133 | else if (!strcmp(cmd, "merge")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 134 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 135 | ? _("Your local changes to the following files would be overwritten by merge:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 136 | "Please commit your changes or stash them before you merge.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 137 | : _("Your local changes to the following files would be overwritten by merge:\n%%s"); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 138 | else |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 139 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 140 | ? _("Your local changes to the following files would be overwritten by %s:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 141 | "Please commit your changes or stash them before you %s.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 142 | : _("Your local changes to the following files would be overwritten by %s:\n%%s"); |
Jeff King | fa3f60b | 2014-06-18 16:02:13 -0400 | [diff] [blame] | 143 | msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] = |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 144 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 145 | |
| 146 | msgs[ERROR_NOT_UPTODATE_DIR] = |
Stefan Beller | 584f99c | 2016-12-02 11:17:41 -0800 | [diff] [blame] | 147 | _("Updating the following directories would lose untracked files in them:\n%s"); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 148 | |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 149 | msgs[ERROR_CWD_IN_THE_WAY] = |
| 150 | _("Refusing to remove the current working directory:\n%s"); |
| 151 | |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 152 | if (!strcmp(cmd, "checkout")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 153 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 154 | ? _("The following untracked working tree files would be removed by checkout:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 155 | "Please move or remove them before you switch branches.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 156 | : _("The following untracked working tree files would be removed by checkout:\n%%s"); |
| 157 | else if (!strcmp(cmd, "merge")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 158 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 159 | ? _("The following untracked working tree files would be removed by merge:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 160 | "Please move or remove them before you merge.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 161 | : _("The following untracked working tree files would be removed by merge:\n%%s"); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 162 | else |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 163 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 164 | ? _("The following untracked working tree files would be removed by %s:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 165 | "Please move or remove them before you %s.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 166 | : _("The following untracked working tree files would be removed by %s:\n%%s"); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 167 | msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 168 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); |
Jeff King | fa3f60b | 2014-06-18 16:02:13 -0400 | [diff] [blame] | 169 | |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 170 | if (!strcmp(cmd, "checkout")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 171 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 172 | ? _("The following untracked working tree files would be overwritten by checkout:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 173 | "Please move or remove them before you switch branches.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 174 | : _("The following untracked working tree files would be overwritten by checkout:\n%%s"); |
| 175 | else if (!strcmp(cmd, "merge")) |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 176 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 177 | ? _("The following untracked working tree files would be overwritten by merge:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 178 | "Please move or remove them before you merge.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 179 | : _("The following untracked working tree files would be overwritten by merge:\n%%s"); |
| 180 | else |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 181 | msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE) |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 182 | ? _("The following untracked working tree files would be overwritten by %s:\n%%s" |
Alex Henrie | c2691e2 | 2016-06-25 00:34:04 -0600 | [diff] [blame] | 183 | "Please move or remove them before you %s.") |
Vasco Almeida | 2e3926b | 2016-05-12 23:16:26 +0000 | [diff] [blame] | 184 | : _("The following untracked working tree files would be overwritten by %s:\n%%s"); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 185 | msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 186 | strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we |
| 190 | * cannot easily display it as a list. |
| 191 | */ |
Vasco Almeida | ed47fdf | 2016-04-09 20:38:39 +0000 | [diff] [blame] | 192 | msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind."); |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 193 | |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 194 | msgs[ERROR_WOULD_LOSE_SUBMODULE] = |
Stefan Beller | 6362ed0 | 2017-03-29 15:34:24 -0700 | [diff] [blame] | 195 | _("Cannot update submodule:\n%s"); |
Matthieu Moy | 5e65ee3 | 2010-09-02 18:08:15 +0200 | [diff] [blame] | 196 | |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 197 | msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] = |
Elijah Newren | 22ab0b3 | 2020-03-27 00:48:58 +0000 | [diff] [blame] | 198 | _("The following paths are not up to date and were left despite sparse patterns:\n%s"); |
Elijah Newren | ebb568b | 2020-03-27 00:48:59 +0000 | [diff] [blame] | 199 | msgs[WARNING_SPARSE_UNMERGED_FILE] = |
| 200 | _("The following paths are unmerged and were left despite sparse patterns:\n%s"); |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 201 | msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] = |
Elijah Newren | 22ab0b3 | 2020-03-27 00:48:58 +0000 | [diff] [blame] | 202 | _("The following paths were already present and thus not updated despite sparse patterns:\n%s"); |
Matthieu Moy | 5e65ee3 | 2010-09-02 18:08:15 +0200 | [diff] [blame] | 203 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 204 | opts->internal.show_all_errors = 1; |
Clemens Buchacher | 7980872 | 2010-11-15 20:52:19 +0100 | [diff] [blame] | 205 | /* rejected paths may not have a static buffer */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 206 | for (i = 0; i < ARRAY_SIZE(opts->internal.unpack_rejects); i++) |
| 207 | opts->internal.unpack_rejects[i].strdup_strings = 1; |
Matthieu Moy | dc1166e | 2010-09-02 13:57:33 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 210 | void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) |
| 211 | { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 212 | strvec_clear(&opts->internal.msgs_to_free); |
| 213 | memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs)); |
Patrick Steinhardt | 4dfd4f1 | 2024-08-14 08:52:39 +0200 | [diff] [blame] | 214 | discard_index(&opts->internal.result); |
Martin Ågren | 1c41d28 | 2018-05-21 16:54:28 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Jeff King | 4616918 | 2014-11-24 13:36:51 -0500 | [diff] [blame] | 217 | static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce, |
René Scharfe | 6ff264e | 2012-03-06 20:37:23 +0100 | [diff] [blame] | 218 | unsigned int set, unsigned int clear) |
| 219 | { |
Karsten Blees | 419a597 | 2013-11-14 20:22:27 +0100 | [diff] [blame] | 220 | clear |= CE_HASHED; |
René Scharfe | 6ff264e | 2012-03-06 20:37:23 +0100 | [diff] [blame] | 221 | |
| 222 | if (set & CE_REMOVE) |
| 223 | set |= CE_WT_REMOVE; |
| 224 | |
René Scharfe | 6ff264e | 2012-03-06 20:37:23 +0100 | [diff] [blame] | 225 | ce->ce_flags = (ce->ce_flags & ~clear) | set; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 226 | return add_index_entry(&o->internal.result, ce, |
Jeff King | 4616918 | 2014-11-24 13:36:51 -0500 | [diff] [blame] | 227 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); |
René Scharfe | 6ff264e | 2012-03-06 20:37:23 +0100 | [diff] [blame] | 228 | } |
| 229 | |
René Scharfe | a33bd4d | 2013-06-02 17:46:53 +0200 | [diff] [blame] | 230 | static void add_entry(struct unpack_trees_options *o, |
| 231 | const struct cache_entry *ce, |
| 232 | unsigned int set, unsigned int clear) |
| 233 | { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 234 | do_add_entry(o, dup_cache_entry(ce, &o->internal.result), set, clear); |
Linus Torvalds | b48d5a0 | 2007-08-10 12:15:54 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Kjetil Barvik | 7847892 | 2009-02-09 21:54:07 +0100 | [diff] [blame] | 237 | /* |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 238 | * add error messages on path <path> |
| 239 | * corresponding to the type <e> with the message <msg> |
| 240 | * indicating if it should be display in porcelain or not |
| 241 | */ |
| 242 | static int add_rejected_path(struct unpack_trees_options *o, |
| 243 | enum unpack_trees_error_types e, |
| 244 | const char *path) |
| 245 | { |
Nguyễn Thái Ngọc Duy | b165fac | 2019-03-22 16:31:36 +0700 | [diff] [blame] | 246 | if (o->quiet) |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 247 | return -1; |
| 248 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 249 | if (!o->internal.show_all_errors) |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 250 | return error(ERRORMSG(o, e), super_prefixed(path, |
| 251 | o->super_prefix)); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Otherwise, insert in a list for future display by |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 255 | * display_(error|warning)_msgs() |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 256 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 257 | string_list_append(&o->internal.unpack_rejects[e], path); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /* |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 262 | * display all the error messages stored in a nice way |
| 263 | */ |
| 264 | static void display_error_msgs(struct unpack_trees_options *o) |
| 265 | { |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 266 | int e; |
| 267 | unsigned error_displayed = 0; |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 268 | for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 269 | struct string_list *rejects = &o->internal.unpack_rejects[e]; |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 270 | |
Clemens Buchacher | 7980872 | 2010-11-15 20:52:19 +0100 | [diff] [blame] | 271 | if (rejects->nr > 0) { |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 272 | int i; |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 273 | struct strbuf path = STRBUF_INIT; |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 274 | |
| 275 | error_displayed = 1; |
Clemens Buchacher | 7980872 | 2010-11-15 20:52:19 +0100 | [diff] [blame] | 276 | for (i = 0; i < rejects->nr; i++) |
| 277 | strbuf_addf(&path, "\t%s\n", rejects->items[i].string); |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 278 | error(ERRORMSG(o, e), super_prefixed(path.buf, |
| 279 | o->super_prefix)); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 280 | strbuf_release(&path); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 281 | } |
Clemens Buchacher | 7980872 | 2010-11-15 20:52:19 +0100 | [diff] [blame] | 282 | string_list_clear(rejects, 0); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 283 | } |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 284 | if (error_displayed) |
Vasco Almeida | ed47fdf | 2016-04-09 20:38:39 +0000 | [diff] [blame] | 285 | fprintf(stderr, _("Aborting\n")); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 288 | /* |
| 289 | * display all the warning messages stored in a nice way |
| 290 | */ |
| 291 | static void display_warning_msgs(struct unpack_trees_options *o) |
| 292 | { |
| 293 | int e; |
| 294 | unsigned warning_displayed = 0; |
| 295 | for (e = NB_UNPACK_TREES_ERROR_TYPES + 1; |
| 296 | e < NB_UNPACK_TREES_WARNING_TYPES; e++) { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 297 | struct string_list *rejects = &o->internal.unpack_rejects[e]; |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 298 | |
| 299 | if (rejects->nr > 0) { |
| 300 | int i; |
| 301 | struct strbuf path = STRBUF_INIT; |
| 302 | |
| 303 | warning_displayed = 1; |
| 304 | for (i = 0; i < rejects->nr; i++) |
| 305 | strbuf_addf(&path, "\t%s\n", rejects->items[i].string); |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 306 | warning(ERRORMSG(o, e), super_prefixed(path.buf, |
| 307 | o->super_prefix)); |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 308 | strbuf_release(&path); |
| 309 | } |
| 310 | string_list_clear(rejects, 0); |
| 311 | } |
| 312 | if (warning_displayed) |
| 313 | fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n")); |
| 314 | } |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 315 | static int check_submodule_move_head(const struct cache_entry *ce, |
| 316 | const char *old_id, |
| 317 | const char *new_id, |
| 318 | struct unpack_trees_options *o) |
| 319 | { |
Stefan Beller | cd279e2 | 2017-04-18 14:37:22 -0700 | [diff] [blame] | 320 | unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN; |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 321 | const struct submodule *sub = submodule_from_ce(ce); |
Brandon Williams | 7463e2e | 2017-08-03 11:19:53 -0700 | [diff] [blame] | 322 | |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 323 | if (!sub) |
| 324 | return 0; |
| 325 | |
Stefan Beller | cd279e2 | 2017-04-18 14:37:22 -0700 | [diff] [blame] | 326 | if (o->reset) |
| 327 | flags |= SUBMODULE_MOVE_HEAD_FORCE; |
| 328 | |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 329 | if (submodule_move_head(ce->name, o->super_prefix, old_id, new_id, |
| 330 | flags)) |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 331 | return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name); |
Brandon Williams | 7463e2e | 2017-08-03 11:19:53 -0700 | [diff] [blame] | 332 | return 0; |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 335 | /* |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 336 | * Perform the loading of the repository's gitmodules file. This function is |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 337 | * used by 'check_update()' to perform loading of the gitmodules file in two |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 338 | * different situations: |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 339 | * (1) before removing entries from the working tree if the gitmodules file has |
| 340 | * been marked for removal. This situation is specified by 'state' == NULL. |
| 341 | * (2) before checking out entries to the working tree if the gitmodules file |
| 342 | * has been marked for update. This situation is specified by 'state' != NULL. |
| 343 | */ |
| 344 | static void load_gitmodules_file(struct index_state *index, |
| 345 | struct checkout *state) |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 346 | { |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 347 | int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE)); |
| 348 | |
| 349 | if (pos >= 0) { |
| 350 | struct cache_entry *ce = index->cache[pos]; |
| 351 | if (!state && ce->ce_flags & CE_WT_REMOVE) { |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 352 | repo_read_gitmodules(the_repository, 0); |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 353 | } else if (state && (ce->ce_flags & CE_UPDATE)) { |
Stefan Beller | f793b89 | 2018-03-28 15:35:28 -0700 | [diff] [blame] | 354 | submodule_free(the_repository); |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 355 | checkout_entry(ce, state, NULL, NULL); |
Matheus Tavares | d799242 | 2020-01-15 23:39:55 -0300 | [diff] [blame] | 356 | repo_read_gitmodules(the_repository, 0); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Elijah Newren | 6c34239 | 2020-05-14 19:53:22 +0000 | [diff] [blame] | 361 | static struct progress *get_progress(struct unpack_trees_options *o, |
| 362 | struct index_state *index) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 363 | { |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 364 | unsigned cnt = 0, total = 0; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 365 | |
Stefan Beller | 384f1a1 | 2017-01-09 11:46:19 -0800 | [diff] [blame] | 366 | if (!o->update || !o->verbose_update) |
| 367 | return NULL; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 368 | |
Stefan Beller | 384f1a1 | 2017-01-09 11:46:19 -0800 | [diff] [blame] | 369 | for (; cnt < index->cache_nr; cnt++) { |
| 370 | const struct cache_entry *ce = index->cache[cnt]; |
| 371 | if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE)) |
| 372 | total++; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Patrick Steinhardt | 1f7e647 | 2024-12-17 07:43:48 +0100 | [diff] [blame] | 375 | return start_delayed_progress(the_repository, |
| 376 | _("Updating files"), total); |
Stefan Beller | 384f1a1 | 2017-01-09 11:46:19 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Duy Nguyen | b878579 | 2018-08-17 20:00:39 +0200 | [diff] [blame] | 379 | static void setup_collided_checkout_detection(struct checkout *state, |
| 380 | struct index_state *index) |
| 381 | { |
| 382 | int i; |
| 383 | |
| 384 | state->clone = 1; |
| 385 | for (i = 0; i < index->cache_nr; i++) |
| 386 | index->cache[i]->ce_flags &= ~CE_MATCHED; |
| 387 | } |
| 388 | |
| 389 | static void report_collided_checkout(struct index_state *index) |
| 390 | { |
| 391 | struct string_list list = STRING_LIST_INIT_NODUP; |
| 392 | int i; |
| 393 | |
| 394 | for (i = 0; i < index->cache_nr; i++) { |
| 395 | struct cache_entry *ce = index->cache[i]; |
| 396 | |
| 397 | if (!(ce->ce_flags & CE_MATCHED)) |
| 398 | continue; |
| 399 | |
| 400 | string_list_append(&list, ce->name); |
| 401 | ce->ce_flags &= ~CE_MATCHED; |
| 402 | } |
| 403 | |
| 404 | list.cmp = fspathcmp; |
| 405 | string_list_sort(&list); |
| 406 | |
| 407 | if (list.nr) { |
| 408 | warning(_("the following paths have collided (e.g. case-sensitive paths\n" |
| 409 | "on a case-insensitive filesystem) and only one from the same\n" |
| 410 | "colliding group is in the working tree:\n")); |
| 411 | |
| 412 | for (i = 0; i < list.nr; i++) |
| 413 | fprintf(stderr, " '%s'\n", list.items[i].string); |
| 414 | } |
| 415 | |
| 416 | string_list_clear(&list, 0); |
| 417 | } |
| 418 | |
Jonathan Tan | b2896d2 | 2021-07-23 11:52:22 -0700 | [diff] [blame] | 419 | static int must_checkout(const struct cache_entry *ce) |
| 420 | { |
| 421 | return ce->ce_flags & CE_UPDATE; |
| 422 | } |
| 423 | |
Elijah Newren | b0a5a12 | 2020-03-27 00:48:49 +0000 | [diff] [blame] | 424 | static int check_updates(struct unpack_trees_options *o, |
| 425 | struct index_state *index) |
Stefan Beller | 384f1a1 | 2017-01-09 11:46:19 -0800 | [diff] [blame] | 426 | { |
| 427 | unsigned cnt = 0; |
Stefan Beller | 30ac275 | 2017-01-09 11:46:17 -0800 | [diff] [blame] | 428 | int errs = 0; |
Carlo Marcelo Arenas Belón | 07f967a | 2018-10-18 11:46:04 -0700 | [diff] [blame] | 429 | struct progress *progress; |
Stefan Beller | 30ac275 | 2017-01-09 11:46:17 -0800 | [diff] [blame] | 430 | struct checkout state = CHECKOUT_INIT; |
Matheus Tavares | 7531e4b | 2021-04-18 21:14:55 -0300 | [diff] [blame] | 431 | int i, pc_workers, pc_threshold; |
Stefan Beller | 30ac275 | 2017-01-09 11:46:17 -0800 | [diff] [blame] | 432 | |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 433 | trace_performance_enter(); |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 434 | state.super_prefix = o->super_prefix; |
Stefan Beller | 30ac275 | 2017-01-09 11:46:17 -0800 | [diff] [blame] | 435 | state.force = 1; |
| 436 | state.quiet = 1; |
| 437 | state.refresh_cache = 1; |
| 438 | state.istate = index; |
brian m. carlson | 13e7ed6 | 2020-03-16 18:05:04 +0000 | [diff] [blame] | 439 | clone_checkout_metadata(&state.meta, &o->meta, NULL); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 440 | |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 441 | if (!o->update || o->dry_run) { |
| 442 | remove_marked_cache_entries(index, 0); |
| 443 | trace_performance_leave("check_updates"); |
| 444 | return 0; |
| 445 | } |
| 446 | |
Duy Nguyen | b878579 | 2018-08-17 20:00:39 +0200 | [diff] [blame] | 447 | if (o->clone) |
| 448 | setup_collided_checkout_detection(&state, index); |
| 449 | |
Elijah Newren | 6c34239 | 2020-05-14 19:53:22 +0000 | [diff] [blame] | 450 | progress = get_progress(o, index); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 451 | |
Matheus Tavares | 22539ec | 2021-02-02 22:37:10 +0100 | [diff] [blame] | 452 | /* Start with clean cache to avoid using any possibly outdated info. */ |
| 453 | invalidate_lstat_cache(); |
| 454 | |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 455 | git_attr_set_direction(GIT_ATTR_CHECKOUT); |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 456 | |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 457 | if (should_update_submodules()) |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 458 | load_gitmodules_file(index, NULL); |
| 459 | |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 460 | for (i = 0; i < index->cache_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 461 | const struct cache_entry *ce = index->cache[i]; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 462 | |
Nguyễn Thái Ngọc Duy | e663db2 | 2009-08-20 20:47:06 +0700 | [diff] [blame] | 463 | if (ce->ce_flags & CE_WT_REMOVE) { |
| 464 | display_progress(progress, ++cnt); |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 465 | unlink_entry(ce, o->super_prefix); |
Nguyễn Thái Ngọc Duy | e663db2 | 2009-08-20 20:47:06 +0700 | [diff] [blame] | 466 | } |
Linus Torvalds | 1fa6ead | 2008-03-22 09:48:41 -0700 | [diff] [blame] | 467 | } |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 468 | |
Thomas Gummerer | 6fdc205 | 2018-12-20 13:48:16 +0000 | [diff] [blame] | 469 | remove_marked_cache_entries(index, 0); |
Kjetil Barvik | 7847892 | 2009-02-09 21:54:07 +0100 | [diff] [blame] | 470 | remove_scheduled_dirs(); |
Linus Torvalds | 1fa6ead | 2008-03-22 09:48:41 -0700 | [diff] [blame] | 471 | |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 472 | if (should_update_submodules()) |
Brandon Williams | 3302871 | 2017-08-03 11:19:59 -0700 | [diff] [blame] | 473 | load_gitmodules_file(index, &state); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 474 | |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 475 | if (repo_has_promisor_remote(the_repository)) |
Jonathan Tan | c0c578b | 2017-12-08 15:58:47 +0000 | [diff] [blame] | 476 | /* |
| 477 | * Prefetch the objects that are to be checked out in the loop |
| 478 | * below. |
| 479 | */ |
Jonathan Tan | b2896d2 | 2021-07-23 11:52:22 -0700 | [diff] [blame] | 480 | prefetch_cache_entries(index, must_checkout); |
Matheus Tavares | 04155bd | 2021-04-18 21:14:53 -0300 | [diff] [blame] | 481 | |
Matheus Tavares | 7531e4b | 2021-04-18 21:14:55 -0300 | [diff] [blame] | 482 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); |
| 483 | |
Matheus Tavares | 04155bd | 2021-04-18 21:14:53 -0300 | [diff] [blame] | 484 | enable_delayed_checkout(&state); |
Matheus Tavares | 7531e4b | 2021-04-18 21:14:55 -0300 | [diff] [blame] | 485 | if (pc_workers > 1) |
| 486 | init_parallel_checkout(); |
Linus Torvalds | 1fa6ead | 2008-03-22 09:48:41 -0700 | [diff] [blame] | 487 | for (i = 0; i < index->cache_nr; i++) { |
| 488 | struct cache_entry *ce = index->cache[i]; |
| 489 | |
Jonathan Tan | b2896d2 | 2021-07-23 11:52:22 -0700 | [diff] [blame] | 490 | if (must_checkout(ce)) { |
Matheus Tavares | 1c4d6f4 | 2021-04-18 21:14:56 -0300 | [diff] [blame] | 491 | size_t last_pc_queue_size = pc_queue_size(); |
| 492 | |
David Turner | 7d78241 | 2015-07-17 17:19:27 -0400 | [diff] [blame] | 493 | if (ce->ce_flags & CE_WT_REMOVE) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 494 | BUG("both update and delete flags are set on %s", |
David Turner | 7d78241 | 2015-07-17 17:19:27 -0400 | [diff] [blame] | 495 | ce->name); |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 496 | ce->ce_flags &= ~CE_UPDATE; |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 497 | errs |= checkout_entry(ce, &state, NULL, NULL); |
Matheus Tavares | 1c4d6f4 | 2021-04-18 21:14:56 -0300 | [diff] [blame] | 498 | |
| 499 | if (last_pc_queue_size == pc_queue_size()) |
| 500 | display_progress(progress, ++cnt); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 501 | } |
| 502 | } |
Matheus Tavares | 7531e4b | 2021-04-18 21:14:55 -0300 | [diff] [blame] | 503 | if (pc_workers > 1) |
Matheus Tavares | 1c4d6f4 | 2021-04-18 21:14:56 -0300 | [diff] [blame] | 504 | errs |= run_parallel_checkout(&state, pc_workers, pc_threshold, |
| 505 | progress, &cnt); |
Nicolas Pitre | 4d4fcc5 | 2007-10-30 14:57:33 -0400 | [diff] [blame] | 506 | stop_progress(&progress); |
Matheus Tavares | 611c778 | 2022-07-14 08:49:12 -0300 | [diff] [blame] | 507 | errs |= finish_delayed_checkout(&state, o->verbose_update); |
Elijah Newren | 26f924d | 2020-01-07 06:57:57 +0000 | [diff] [blame] | 508 | git_attr_set_direction(GIT_ATTR_CHECKIN); |
Duy Nguyen | b878579 | 2018-08-17 20:00:39 +0200 | [diff] [blame] | 509 | |
| 510 | if (o->clone) |
| 511 | report_collided_checkout(index); |
| 512 | |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 513 | trace_performance_leave("check_updates"); |
Junio C Hamano | c4758d3 | 2008-03-18 22:01:28 -0700 | [diff] [blame] | 514 | return errs != 0; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 515 | } |
| 516 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 517 | static int verify_uptodate_sparse(const struct cache_entry *ce, |
| 518 | struct unpack_trees_options *o); |
| 519 | static int verify_absent_sparse(const struct cache_entry *ce, |
| 520 | enum unpack_trees_error_types, |
| 521 | struct unpack_trees_options *o); |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 522 | |
Nguyễn Thái Ngọc Duy | a5c446f | 2014-06-13 19:19:30 +0700 | [diff] [blame] | 523 | static int apply_sparse_checkout(struct index_state *istate, |
| 524 | struct cache_entry *ce, |
| 525 | struct unpack_trees_options *o) |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 526 | { |
| 527 | int was_skip_worktree = ce_skip_worktree(ce); |
| 528 | |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 529 | if (ce->ce_flags & CE_NEW_SKIP_WORKTREE) |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 530 | ce->ce_flags |= CE_SKIP_WORKTREE; |
| 531 | else |
| 532 | ce->ce_flags &= ~CE_SKIP_WORKTREE; |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 533 | if (was_skip_worktree != ce_skip_worktree(ce)) { |
| 534 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 535 | mark_fsmonitor_invalid(istate, ce); |
Nguyễn Thái Ngọc Duy | a5c446f | 2014-06-13 19:19:30 +0700 | [diff] [blame] | 536 | istate->cache_changed |= CE_ENTRY_CHANGED; |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 537 | } |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 538 | |
| 539 | /* |
Nguyễn Thái Ngọc Duy | eec3fc0 | 2010-07-31 13:14:26 +0700 | [diff] [blame] | 540 | * if (!was_skip_worktree && !ce_skip_worktree()) { |
| 541 | * This is perfectly normal. Move on; |
| 542 | * } |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 543 | */ |
Nguyễn Thái Ngọc Duy | eec3fc0 | 2010-07-31 13:14:26 +0700 | [diff] [blame] | 544 | |
| 545 | /* |
| 546 | * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout |
| 547 | * area as a result of ce_skip_worktree() shortcuts in |
Nguyễn Thái Ngọc Duy | 700e66d | 2010-07-31 13:14:27 +0700 | [diff] [blame] | 548 | * verify_absent() and verify_uptodate(). |
| 549 | * Make sure they don't modify worktree if they are already |
| 550 | * outside checkout area |
Nguyễn Thái Ngọc Duy | eec3fc0 | 2010-07-31 13:14:26 +0700 | [diff] [blame] | 551 | */ |
Nguyễn Thái Ngọc Duy | 700e66d | 2010-07-31 13:14:27 +0700 | [diff] [blame] | 552 | if (was_skip_worktree && ce_skip_worktree(ce)) { |
| 553 | ce->ce_flags &= ~CE_UPDATE; |
| 554 | |
| 555 | /* |
| 556 | * By default, when CE_REMOVE is on, CE_WT_REMOVE is also |
| 557 | * on to get that file removed from both index and worktree. |
| 558 | * If that file is already outside worktree area, don't |
| 559 | * bother remove it. |
| 560 | */ |
| 561 | if (ce->ce_flags & CE_REMOVE) |
| 562 | ce->ce_flags &= ~CE_WT_REMOVE; |
| 563 | } |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 564 | |
| 565 | if (!was_skip_worktree && ce_skip_worktree(ce)) { |
| 566 | /* |
| 567 | * If CE_UPDATE is set, verify_uptodate() must be called already |
| 568 | * also stat info may have lost after merged_entry() so calling |
| 569 | * verify_uptodate() again may fail |
| 570 | */ |
Elijah Newren | 3cc7c50 | 2020-03-27 00:48:50 +0000 | [diff] [blame] | 571 | if (!(ce->ce_flags & CE_UPDATE) && |
| 572 | verify_uptodate_sparse(ce, o)) { |
| 573 | ce->ce_flags &= ~CE_SKIP_WORKTREE; |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 574 | return -1; |
Elijah Newren | 3cc7c50 | 2020-03-27 00:48:50 +0000 | [diff] [blame] | 575 | } |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 576 | ce->ce_flags |= CE_WT_REMOVE; |
David Turner | 7d78241 | 2015-07-17 17:19:27 -0400 | [diff] [blame] | 577 | ce->ce_flags &= ~CE_UPDATE; |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 578 | } |
| 579 | if (was_skip_worktree && !ce_skip_worktree(ce)) { |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 580 | if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o)) |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 581 | return -1; |
| 582 | ce->ce_flags |= CE_UPDATE; |
| 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
Elijah Newren | ebb568b | 2020-03-27 00:48:59 +0000 | [diff] [blame] | 587 | static int warn_conflicted_path(struct index_state *istate, |
| 588 | int i, |
| 589 | struct unpack_trees_options *o) |
| 590 | { |
| 591 | char *conflicting_path = istate->cache[i]->name; |
| 592 | int count = 0; |
| 593 | |
| 594 | add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path); |
| 595 | |
Derrick Stolee | 0eeb3be | 2020-05-07 13:17:33 +0000 | [diff] [blame] | 596 | /* Find out how many higher stage entries are at same path */ |
| 597 | while ((++count) + i < istate->cache_nr && |
| 598 | !strcmp(conflicting_path, istate->cache[count + i]->name)) |
| 599 | ; /* do nothing */ |
| 600 | |
Elijah Newren | ebb568b | 2020-03-27 00:48:59 +0000 | [diff] [blame] | 601 | return count; |
| 602 | } |
| 603 | |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 604 | static inline int call_unpack_fn(const struct cache_entry * const *src, |
| 605 | struct unpack_trees_options *o) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 606 | { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 607 | int ret = o->fn(src, o); |
| 608 | if (ret > 0) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 609 | ret = 0; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 610 | return ret; |
| 611 | } |
| 612 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 613 | static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) |
| 614 | { |
| 615 | ce->ce_flags |= CE_UNPACKED; |
| 616 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 617 | if (o->internal.cache_bottom < o->src_index->cache_nr && |
| 618 | o->src_index->cache[o->internal.cache_bottom] == ce) { |
| 619 | int bottom = o->internal.cache_bottom; |
| 620 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 621 | while (bottom < o->src_index->cache_nr && |
| 622 | o->src_index->cache[bottom]->ce_flags & CE_UNPACKED) |
| 623 | bottom++; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 624 | o->internal.cache_bottom = bottom; |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | static void mark_all_ce_unused(struct index_state *index) |
| 629 | { |
| 630 | int i; |
| 631 | for (i = 0; i < index->cache_nr; i++) |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 632 | index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 633 | } |
| 634 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 635 | static int locate_in_src_index(const struct cache_entry *ce, |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 636 | struct unpack_trees_options *o) |
| 637 | { |
| 638 | struct index_state *index = o->src_index; |
| 639 | int len = ce_namelen(ce); |
| 640 | int pos = index_name_pos(index, ce->name, len); |
| 641 | if (pos < 0) |
| 642 | pos = -1 - pos; |
| 643 | return pos; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * We call unpack_index_entry() with an unmerged cache entry |
| 648 | * only in diff-index, and it wants a single callback. Skip |
| 649 | * the other unmerged entry with the same name. |
| 650 | */ |
| 651 | static void mark_ce_used_same_name(struct cache_entry *ce, |
| 652 | struct unpack_trees_options *o) |
| 653 | { |
| 654 | struct index_state *index = o->src_index; |
| 655 | int len = ce_namelen(ce); |
| 656 | int pos; |
| 657 | |
| 658 | for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) { |
| 659 | struct cache_entry *next = index->cache[pos]; |
| 660 | if (len != ce_namelen(next) || |
| 661 | memcmp(ce->name, next->name, len)) |
| 662 | break; |
| 663 | mark_ce_used(next, o); |
| 664 | } |
| 665 | } |
| 666 | |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 667 | static struct cache_entry *next_cache_entry(struct unpack_trees_options *o) |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 668 | { |
| 669 | const struct index_state *index = o->src_index; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 670 | int pos = o->internal.cache_bottom; |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 671 | |
| 672 | while (pos < index->cache_nr) { |
| 673 | struct cache_entry *ce = index->cache[pos]; |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 674 | if (!(ce->ce_flags & CE_UNPACKED)) |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 675 | return ce; |
| 676 | pos++; |
| 677 | } |
| 678 | return NULL; |
| 679 | } |
| 680 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 681 | static void add_same_unmerged(const struct cache_entry *ce, |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 682 | struct unpack_trees_options *o) |
| 683 | { |
| 684 | struct index_state *index = o->src_index; |
| 685 | int len = ce_namelen(ce); |
| 686 | int pos = index_name_pos(index, ce->name, len); |
| 687 | |
| 688 | if (0 <= pos) |
| 689 | die("programming error in a caller of mark_ce_used_same_name"); |
| 690 | for (pos = -pos - 1; pos < index->cache_nr; pos++) { |
| 691 | struct cache_entry *next = index->cache[pos]; |
| 692 | if (len != ce_namelen(next) || |
| 693 | memcmp(ce->name, next->name, len)) |
| 694 | break; |
| 695 | add_entry(o, next, 0, 0); |
| 696 | mark_ce_used(next, o); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static int unpack_index_entry(struct cache_entry *ce, |
| 701 | struct unpack_trees_options *o) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 702 | { |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 703 | const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 704 | int ret; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 705 | |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 706 | src[0] = ce; |
| 707 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 708 | mark_ce_used(ce, o); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 709 | if (ce_stage(ce)) { |
| 710 | if (o->skip_unmerged) { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 711 | add_entry(o, ce, 0, 0); |
| 712 | return 0; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 713 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 714 | } |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 715 | ret = call_unpack_fn(src, o); |
| 716 | if (ce_stage(ce)) |
| 717 | mark_ce_used_same_name(ce, o); |
| 718 | return ret; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 721 | static int find_cache_pos(struct traverse_info *, const char *p, size_t len); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 722 | |
| 723 | static void restore_cache_bottom(struct traverse_info *info, int bottom) |
| 724 | { |
| 725 | struct unpack_trees_options *o = info->data; |
| 726 | |
| 727 | if (o->diff_index_cached) |
| 728 | return; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 729 | o->internal.cache_bottom = bottom; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static int switch_cache_bottom(struct traverse_info *info) |
| 733 | { |
| 734 | struct unpack_trees_options *o = info->data; |
| 735 | int ret, pos; |
| 736 | |
| 737 | if (o->diff_index_cached) |
| 738 | return 0; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 739 | ret = o->internal.cache_bottom; |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 740 | pos = find_cache_pos(info->prev, info->name, info->namelen); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 741 | |
| 742 | if (pos < -1) |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 743 | o->internal.cache_bottom = -2 - pos; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 744 | else if (pos < 0) |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 745 | o->internal.cache_bottom = o->src_index->cache_nr; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 746 | return ret; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 749 | static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k) |
| 750 | { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 751 | return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid); |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 754 | static int all_trees_same_as_cache_tree(int n, unsigned long dirmask, |
| 755 | struct name_entry *names, |
| 756 | struct traverse_info *info) |
| 757 | { |
| 758 | struct unpack_trees_options *o = info->data; |
| 759 | int i; |
| 760 | |
| 761 | if (!o->merge || dirmask != ((1 << n) - 1)) |
| 762 | return 0; |
| 763 | |
| 764 | for (i = 1; i < n; i++) |
| 765 | if (!are_same_oid(names, names + i)) |
| 766 | return 0; |
| 767 | |
| 768 | return cache_tree_matches_traversal(o->src_index->cache_tree, names, info); |
| 769 | } |
| 770 | |
| 771 | static int index_pos_by_traverse_info(struct name_entry *names, |
| 772 | struct traverse_info *info) |
| 773 | { |
| 774 | struct unpack_trees_options *o = info->data; |
Jeff King | c43ab06 | 2019-07-31 00:38:23 -0400 | [diff] [blame] | 775 | struct strbuf name = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 776 | int pos; |
| 777 | |
Jeff King | c43ab06 | 2019-07-31 00:38:23 -0400 | [diff] [blame] | 778 | strbuf_make_traverse_path(&name, info, names->path, names->pathlen); |
| 779 | strbuf_addch(&name, '/'); |
| 780 | pos = index_name_pos(o->src_index, name.buf, name.len); |
Derrick Stolee | 13e1331 | 2021-03-30 13:10:57 +0000 | [diff] [blame] | 781 | if (pos >= 0) { |
| 782 | if (!o->src_index->sparse_index || |
| 783 | !(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE)) |
| 784 | BUG("This is a directory and should not exist in index"); |
| 785 | } else { |
| 786 | pos = -pos - 1; |
| 787 | } |
Emily Shaffer | 573117d | 2020-01-07 18:31:27 -0800 | [diff] [blame] | 788 | if (pos >= o->src_index->cache_nr || |
| 789 | !starts_with(o->src_index->cache[pos]->name, name.buf) || |
Jeff King | c43ab06 | 2019-07-31 00:38:23 -0400 | [diff] [blame] | 790 | (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf))) |
Emily Shaffer | 573117d | 2020-01-07 18:31:27 -0800 | [diff] [blame] | 791 | BUG("pos %d doesn't point to the first entry of %s in index", |
| 792 | pos, name.buf); |
Jeff King | c43ab06 | 2019-07-31 00:38:23 -0400 | [diff] [blame] | 793 | strbuf_release(&name); |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 794 | return pos; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Fast path if we detect that all trees are the same as cache-tree at this |
Nguyễn Thái Ngọc Duy | 5f4436a | 2018-08-25 15:02:09 +0200 | [diff] [blame] | 799 | * path. We'll walk these trees in an iterative loop using cache-tree/index |
| 800 | * instead of ODB since we already know what these trees contain. |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 801 | */ |
| 802 | static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names, |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 803 | struct traverse_info *info) |
| 804 | { |
| 805 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
| 806 | struct unpack_trees_options *o = info->data; |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 807 | struct cache_entry *tree_ce = NULL; |
| 808 | int ce_len = 0; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 809 | int i, d; |
| 810 | |
| 811 | if (!o->merge) |
| 812 | BUG("We need cache-tree to do this optimization"); |
Patrick Steinhardt | ecb5c43 | 2024-10-07 06:38:21 +0200 | [diff] [blame] | 813 | if (nr_entries + pos > o->src_index->cache_nr) |
| 814 | return error(_("corrupted cache-tree has entries not present in index")); |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 815 | |
| 816 | /* |
Derrick Stolee | bd6a3fd | 2021-07-14 13:12:32 +0000 | [diff] [blame] | 817 | * Do what unpack_callback() and unpack_single_entry() normally |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 818 | * do. But we walk all paths in an iterative loop instead. |
| 819 | * |
| 820 | * D/F conflicts and higher stage entries are not a concern |
| 821 | * because cache-tree would be invalidated and we would never |
| 822 | * get here in the first place. |
| 823 | */ |
| 824 | for (i = 0; i < nr_entries; i++) { |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 825 | int new_ce_len, len, rc; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 826 | |
| 827 | src[0] = o->src_index->cache[pos + i]; |
| 828 | |
| 829 | len = ce_namelen(src[0]); |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 830 | new_ce_len = cache_entry_size(len); |
| 831 | |
| 832 | if (new_ce_len > ce_len) { |
| 833 | new_ce_len <<= 1; |
| 834 | tree_ce = xrealloc(tree_ce, new_ce_len); |
| 835 | memset(tree_ce, 0, new_ce_len); |
| 836 | ce_len = new_ce_len; |
| 837 | |
| 838 | tree_ce->ce_flags = create_ce_flags(0); |
| 839 | |
| 840 | for (d = 1; d <= nr_names; d++) |
| 841 | src[d] = tree_ce; |
| 842 | } |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 843 | |
| 844 | tree_ce->ce_mode = src[0]->ce_mode; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 845 | tree_ce->ce_namelen = len; |
| 846 | oidcpy(&tree_ce->oid, &src[0]->oid); |
| 847 | memcpy(tree_ce->name, src[0]->name, len + 1); |
| 848 | |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 849 | rc = call_unpack_fn((const struct cache_entry * const *)src, o); |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 850 | if (rc < 0) { |
| 851 | free(tree_ce); |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 852 | return rc; |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 853 | } |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 854 | |
| 855 | mark_ce_used(src[0], o); |
| 856 | } |
Nguyễn Thái Ngọc Duy | f1e11c6 | 2018-08-18 16:41:25 +0200 | [diff] [blame] | 857 | free(tree_ce); |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 858 | if (o->internal.debug_unpack) |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 859 | printf("Unpacked %d entries from %s to %s using cache-tree\n", |
| 860 | nr_entries, |
| 861 | o->src_index->cache[pos]->name, |
| 862 | o->src_index->cache[pos + nr_entries - 1]->name); |
| 863 | return 0; |
| 864 | } |
| 865 | |
Junio C Hamano | 84563a6 | 2010-12-22 09:09:55 -0800 | [diff] [blame] | 866 | static int traverse_trees_recursive(int n, unsigned long dirmask, |
| 867 | unsigned long df_conflicts, |
| 868 | struct name_entry *names, |
| 869 | struct traverse_info *info) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 870 | { |
Nguyễn Thái Ngọc Duy | 67022e0 | 2018-11-18 17:47:57 +0100 | [diff] [blame] | 871 | struct unpack_trees_options *o = info->data; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 872 | int i, ret, bottom; |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 873 | int nr_buf = 0; |
Jeff King | 59c4c7d | 2023-08-31 02:17:54 -0400 | [diff] [blame] | 874 | struct tree_desc *t; |
| 875 | void **buf; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 876 | struct traverse_info newinfo; |
| 877 | struct name_entry *p; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 878 | int nr_entries; |
| 879 | |
| 880 | nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info); |
| 881 | if (nr_entries > 0) { |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 882 | int pos = index_pos_by_traverse_info(names, info); |
| 883 | |
| 884 | if (!o->merge || df_conflicts) |
| 885 | BUG("Wrong condition to get here buddy"); |
| 886 | |
| 887 | /* |
| 888 | * All entries up to 'pos' must have been processed |
| 889 | * (i.e. marked CE_UNPACKED) at this point. But to be safe, |
| 890 | * save and restore cache_bottom anyway to not miss |
| 891 | * unprocessed entries before 'pos'. |
| 892 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 893 | bottom = o->internal.cache_bottom; |
Jeff King | 6642969 | 2019-03-20 04:15:07 -0400 | [diff] [blame] | 894 | ret = traverse_by_cache_tree(pos, nr_entries, n, info); |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 895 | o->internal.cache_bottom = bottom; |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 896 | return ret; |
| 897 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 898 | |
| 899 | p = names; |
| 900 | while (!p->mode) |
| 901 | p++; |
| 902 | |
| 903 | newinfo = *info; |
| 904 | newinfo.prev = info; |
Junio C Hamano | 40e3725 | 2011-08-29 12:31:06 -0700 | [diff] [blame] | 905 | newinfo.pathspec = info->pathspec; |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 906 | newinfo.name = p->path; |
| 907 | newinfo.namelen = p->pathlen; |
| 908 | newinfo.mode = p->mode; |
Jeff King | 3780608 | 2019-07-31 00:38:18 -0400 | [diff] [blame] | 909 | newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1); |
René Scharfe | 603d249 | 2013-06-16 01:44:43 +0200 | [diff] [blame] | 910 | newinfo.df_conflicts |= df_conflicts; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 911 | |
Jeff King | 59c4c7d | 2023-08-31 02:17:54 -0400 | [diff] [blame] | 912 | ALLOC_ARRAY(t, n); |
| 913 | ALLOC_ARRAY(buf, n); |
| 914 | |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 915 | /* |
| 916 | * Fetch the tree from the ODB for each peer directory in the |
| 917 | * n commits. |
| 918 | * |
| 919 | * For 2- and 3-way traversals, we try to avoid hitting the |
| 920 | * ODB twice for the same OID. This should yield a nice speed |
| 921 | * up in checkouts and merges when the commits are similar. |
| 922 | * |
| 923 | * We don't bother doing the full O(n^2) search for larger n, |
| 924 | * because wider traversals don't happen that often and we |
| 925 | * avoid the search setup. |
| 926 | * |
| 927 | * When 2 peer OIDs are the same, we just copy the tree |
| 928 | * descriptor data. This implicitly borrows the buffer |
| 929 | * data from the earlier cell. |
| 930 | */ |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 931 | for (i = 0; i < n; i++, dirmask >>= 1) { |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 932 | if (i > 0 && are_same_oid(&names[i], &names[i - 1])) |
| 933 | t[i] = t[i - 1]; |
| 934 | else if (i > 1 && are_same_oid(&names[i], &names[i - 2])) |
| 935 | t[i] = t[i - 2]; |
| 936 | else { |
René Scharfe | 5c377d3 | 2017-08-12 10:32:59 +0200 | [diff] [blame] | 937 | const struct object_id *oid = NULL; |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 938 | if (dirmask & 1) |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 939 | oid = &names[i].oid; |
Nguyễn Thái Ngọc Duy | 5e57580 | 2019-06-27 16:28:48 +0700 | [diff] [blame] | 940 | buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid); |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 941 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 942 | } |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 943 | |
| 944 | bottom = switch_cache_bottom(&newinfo); |
Nguyễn Thái Ngọc Duy | 67022e0 | 2018-11-18 17:47:57 +0100 | [diff] [blame] | 945 | ret = traverse_trees(o->src_index, n, t, &newinfo); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 946 | restore_cache_bottom(&newinfo, bottom); |
Jonathan Nieder | 1ce584b | 2010-08-09 22:33:44 -0500 | [diff] [blame] | 947 | |
Jeff Hostetler | d12a8cf | 2017-04-14 19:25:54 +0000 | [diff] [blame] | 948 | for (i = 0; i < nr_buf; i++) |
Jonathan Nieder | 1ce584b | 2010-08-09 22:33:44 -0500 | [diff] [blame] | 949 | free(buf[i]); |
Jeff King | 59c4c7d | 2023-08-31 02:17:54 -0400 | [diff] [blame] | 950 | free(buf); |
| 951 | free(t); |
Jonathan Nieder | 1ce584b | 2010-08-09 22:33:44 -0500 | [diff] [blame] | 952 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 953 | return ret; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* |
| 957 | * Compare the traverse-path to the cache entry without actually |
| 958 | * having to generate the textual representation of the traverse |
| 959 | * path. |
| 960 | * |
| 961 | * NOTE! This *only* compares up to the size of the traverse path |
| 962 | * itself - the caller needs to do the final check for the cache |
| 963 | * entry having more data at the end! |
| 964 | */ |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 965 | static int do_compare_entry_piecewise(const struct cache_entry *ce, |
| 966 | const struct traverse_info *info, |
| 967 | const char *name, size_t namelen, |
| 968 | unsigned mode) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 969 | { |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 970 | int pathlen, ce_len; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 971 | const char *ce_name; |
| 972 | |
| 973 | if (info->prev) { |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 974 | int cmp = do_compare_entry_piecewise(ce, info->prev, |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 975 | info->name, info->namelen, |
| 976 | info->mode); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 977 | if (cmp) |
| 978 | return cmp; |
| 979 | } |
| 980 | pathlen = info->pathlen; |
| 981 | ce_len = ce_namelen(ce); |
| 982 | |
| 983 | /* If ce_len < pathlen then we must have previously hit "name == directory" entry */ |
| 984 | if (ce_len < pathlen) |
| 985 | return -1; |
| 986 | |
| 987 | ce_len -= pathlen; |
| 988 | ce_name = ce->name + pathlen; |
| 989 | |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 990 | return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 991 | } |
| 992 | |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 993 | static int do_compare_entry(const struct cache_entry *ce, |
| 994 | const struct traverse_info *info, |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 995 | const char *name, size_t namelen, |
| 996 | unsigned mode) |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 997 | { |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 998 | int pathlen, ce_len; |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 999 | const char *ce_name; |
| 1000 | int cmp; |
Derrick Stolee | cd807a5 | 2021-07-14 13:12:31 +0000 | [diff] [blame] | 1001 | unsigned ce_mode; |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 1002 | |
| 1003 | /* |
| 1004 | * If we have not precomputed the traverse path, it is quicker |
| 1005 | * to avoid doing so. But if we have precomputed it, |
| 1006 | * it is quicker to use the precomputed version. |
| 1007 | */ |
| 1008 | if (!info->traverse_path) |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1009 | return do_compare_entry_piecewise(ce, info, name, namelen, mode); |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 1010 | |
| 1011 | cmp = strncmp(ce->name, info->traverse_path, info->pathlen); |
| 1012 | if (cmp) |
| 1013 | return cmp; |
| 1014 | |
| 1015 | pathlen = info->pathlen; |
| 1016 | ce_len = ce_namelen(ce); |
| 1017 | |
| 1018 | if (ce_len < pathlen) |
| 1019 | return -1; |
| 1020 | |
| 1021 | ce_len -= pathlen; |
| 1022 | ce_name = ce->name + pathlen; |
| 1023 | |
Derrick Stolee | cd807a5 | 2021-07-14 13:12:31 +0000 | [diff] [blame] | 1024 | ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG; |
| 1025 | return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode); |
David Turner | d9c2bd5 | 2015-12-21 17:34:20 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1028 | static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n) |
| 1029 | { |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1030 | int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1031 | if (cmp) |
| 1032 | return cmp; |
| 1033 | |
| 1034 | /* |
Derrick Stolee | cd807a5 | 2021-07-14 13:12:31 +0000 | [diff] [blame] | 1035 | * At this point, we know that we have a prefix match. If ce |
| 1036 | * is a sparse directory, then allow an exact match. This only |
| 1037 | * works when the input name is a directory, since ce->name |
| 1038 | * ends in a directory separator. |
| 1039 | */ |
| 1040 | if (S_ISSPARSEDIR(ce->ce_mode) && |
| 1041 | ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1) |
| 1042 | return 0; |
| 1043 | |
| 1044 | /* |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1045 | * Even if the beginning compared identically, the ce should |
| 1046 | * compare as bigger than a directory leading up to it! |
| 1047 | */ |
Jeff King | b3b3cbc | 2019-07-31 00:38:20 -0400 | [diff] [blame] | 1048 | return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n)); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1051 | static int ce_in_traverse_path(const struct cache_entry *ce, |
| 1052 | const struct traverse_info *info) |
| 1053 | { |
| 1054 | if (!info->prev) |
| 1055 | return 1; |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1056 | if (do_compare_entry(ce, info->prev, |
| 1057 | info->name, info->namelen, info->mode)) |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1058 | return 0; |
| 1059 | /* |
| 1060 | * If ce (blob) is the same name as the path (which is a tree |
| 1061 | * we will be descending into), it won't be inside it. |
| 1062 | */ |
| 1063 | return (info->pathlen < ce_namelen(ce)); |
| 1064 | } |
| 1065 | |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1066 | static struct cache_entry *create_ce_entry(const struct traverse_info *info, |
| 1067 | const struct name_entry *n, |
| 1068 | int stage, |
| 1069 | struct index_state *istate, |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1070 | int is_transient, |
| 1071 | int is_sparse_directory) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1072 | { |
Jeff King | b3b3cbc | 2019-07-31 00:38:20 -0400 | [diff] [blame] | 1073 | size_t len = traverse_path_len(info, tree_entry_len(n)); |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1074 | size_t alloc_len = is_sparse_directory ? len + 1 : len; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1075 | struct cache_entry *ce = |
| 1076 | is_transient ? |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1077 | make_empty_transient_cache_entry(alloc_len, NULL) : |
| 1078 | make_empty_cache_entry(istate, alloc_len); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1079 | |
| 1080 | ce->ce_mode = create_ce_mode(n->mode); |
Thomas Gummerer | b60e188 | 2012-07-11 11:22:37 +0200 | [diff] [blame] | 1081 | ce->ce_flags = create_ce_flags(stage); |
| 1082 | ce->ce_namelen = len; |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 1083 | oidcpy(&ce->oid, &n->oid); |
Jeff King | 5aa02f9 | 2019-07-31 00:38:25 -0400 | [diff] [blame] | 1084 | /* len+1 because the cache_entry allocates space for NUL */ |
| 1085 | make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1086 | |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1087 | if (is_sparse_directory) { |
| 1088 | ce->name[len] = '/'; |
| 1089 | ce->name[len + 1] = '\0'; |
| 1090 | ce->ce_namelen++; |
| 1091 | ce->ce_flags |= CE_SKIP_WORKTREE; |
| 1092 | } |
| 1093 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1094 | return ce; |
| 1095 | } |
| 1096 | |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 1097 | /* |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1098 | * Determine whether the path specified by 'p' should be unpacked as a new |
| 1099 | * sparse directory in a sparse index. A new sparse directory 'A/': |
| 1100 | * - must be outside the sparse cone. |
| 1101 | * - must not already be in the index (i.e., no index entry with name 'A/' |
| 1102 | * exists). |
| 1103 | * - must not have any child entries in the index (i.e., no index entry |
| 1104 | * 'A/<something>' exists). |
| 1105 | * If 'p' meets the above requirements, return 1; otherwise, return 0. |
| 1106 | */ |
| 1107 | static int entry_is_new_sparse_dir(const struct traverse_info *info, |
| 1108 | const struct name_entry *p) |
| 1109 | { |
| 1110 | int res, pos; |
| 1111 | struct strbuf dirpath = STRBUF_INIT; |
| 1112 | struct unpack_trees_options *o = info->data; |
| 1113 | |
| 1114 | if (!S_ISDIR(p->mode)) |
| 1115 | return 0; |
| 1116 | |
| 1117 | /* |
| 1118 | * If the path is inside the sparse cone, it can't be a sparse directory. |
| 1119 | */ |
| 1120 | strbuf_add(&dirpath, info->traverse_path, info->pathlen); |
| 1121 | strbuf_add(&dirpath, p->path, p->pathlen); |
| 1122 | strbuf_addch(&dirpath, '/'); |
| 1123 | if (path_in_cone_mode_sparse_checkout(dirpath.buf, o->src_index)) { |
| 1124 | res = 0; |
| 1125 | goto cleanup; |
| 1126 | } |
| 1127 | |
| 1128 | pos = index_name_pos_sparse(o->src_index, dirpath.buf, dirpath.len); |
| 1129 | if (pos >= 0) { |
| 1130 | /* Path is already in the index, not a new sparse dir */ |
| 1131 | res = 0; |
| 1132 | goto cleanup; |
| 1133 | } |
| 1134 | |
| 1135 | /* Where would this sparse dir be inserted into the index? */ |
| 1136 | pos = -pos - 1; |
| 1137 | if (pos >= o->src_index->cache_nr) { |
| 1138 | /* |
| 1139 | * Sparse dir would be inserted at the end of the index, so we |
| 1140 | * know it has no child entries. |
| 1141 | */ |
| 1142 | res = 1; |
| 1143 | goto cleanup; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * If the dir has child entries in the index, the first would be at the |
| 1148 | * position the sparse directory would be inserted. If the entry at this |
| 1149 | * position is inside the dir, not a new sparse dir. |
| 1150 | */ |
| 1151 | res = strncmp(o->src_index->cache[pos]->name, dirpath.buf, dirpath.len); |
| 1152 | |
| 1153 | cleanup: |
| 1154 | strbuf_release(&dirpath); |
| 1155 | return res; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 1159 | * Note that traverse_by_cache_tree() duplicates some logic in this function |
| 1160 | * without actually calling it. If you change the logic here you may need to |
| 1161 | * check and change there as well. |
| 1162 | */ |
Derrick Stolee | bd6a3fd | 2021-07-14 13:12:32 +0000 | [diff] [blame] | 1163 | static int unpack_single_entry(int n, unsigned long mask, |
| 1164 | unsigned long dirmask, |
| 1165 | struct cache_entry **src, |
| 1166 | const struct name_entry *names, |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1167 | const struct traverse_info *info, |
| 1168 | int *is_new_sparse_dir) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1169 | { |
| 1170 | int i; |
| 1171 | struct unpack_trees_options *o = info->data; |
René Scharfe | 603d249 | 2013-06-16 01:44:43 +0200 | [diff] [blame] | 1172 | unsigned long conflicts = info->df_conflicts | dirmask; |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1173 | const struct name_entry *p = names; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1174 | |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1175 | *is_new_sparse_dir = 0; |
| 1176 | if (mask == dirmask && !src[0]) { |
| 1177 | /* |
| 1178 | * If we're not in a sparse index, we can't unpack a directory |
| 1179 | * without recursing into it, so we return. |
| 1180 | */ |
| 1181 | if (!o->src_index->sparse_index) |
| 1182 | return 0; |
| 1183 | |
| 1184 | /* Find first entry with a real name (we could use "mask" too) */ |
| 1185 | while (!p->mode) |
| 1186 | p++; |
| 1187 | |
| 1188 | /* |
| 1189 | * If the directory is completely missing from the index but |
| 1190 | * would otherwise be a sparse directory, we should unpack it. |
| 1191 | * If not, we'll return and continue recursively traversing the |
| 1192 | * tree. |
| 1193 | */ |
| 1194 | *is_new_sparse_dir = entry_is_new_sparse_dir(info, p); |
| 1195 | if (!*is_new_sparse_dir) |
| 1196 | return 0; |
| 1197 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1198 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1199 | /* |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1200 | * When we are unpacking a sparse directory, then this isn't necessarily |
| 1201 | * a directory-file conflict. |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1202 | */ |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1203 | if (mask == dirmask && |
| 1204 | (*is_new_sparse_dir || (src[0] && S_ISSPARSEDIR(src[0]->ce_mode)))) |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1205 | conflicts = 0; |
| 1206 | |
| 1207 | /* |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1208 | * Ok, we've filled in up to any potential index entry in src[0], |
| 1209 | * now do the rest. |
| 1210 | */ |
| 1211 | for (i = 0; i < n; i++) { |
| 1212 | int stage; |
| 1213 | unsigned int bit = 1ul << i; |
| 1214 | if (conflicts & bit) { |
| 1215 | src[i + o->merge] = o->df_conflict_entry; |
| 1216 | continue; |
| 1217 | } |
| 1218 | if (!(mask & bit)) |
| 1219 | continue; |
| 1220 | if (!o->merge) |
| 1221 | stage = 0; |
| 1222 | else if (i + 1 < o->head_idx) |
| 1223 | stage = 1; |
| 1224 | else if (i + 1 > o->head_idx) |
| 1225 | stage = 3; |
| 1226 | else |
| 1227 | stage = 2; |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* |
| 1230 | * If the merge bit is set, then the cache entries are |
| 1231 | * discarded in the following block. In this case, |
| 1232 | * construct "transient" cache_entries, as they are |
| 1233 | * not stored in the index. otherwise construct the |
| 1234 | * cache entry from the index aware logic. |
| 1235 | */ |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1236 | src[i + o->merge] = create_ce_entry(info, names + i, stage, |
Elijah Newren | 0d680a7 | 2023-02-27 15:28:18 +0000 | [diff] [blame] | 1237 | &o->internal.result, |
| 1238 | o->merge, bit & dirmask); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
René Scharfe | 5d80ef5 | 2013-06-02 17:46:57 +0200 | [diff] [blame] | 1241 | if (o->merge) { |
| 1242 | int rc = call_unpack_fn((const struct cache_entry * const *)src, |
| 1243 | o); |
| 1244 | for (i = 0; i < n; i++) { |
| 1245 | struct cache_entry *ce = src[i + o->merge]; |
| 1246 | if (ce != o->df_conflict_entry) |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 1247 | discard_cache_entry(ce); |
René Scharfe | 5d80ef5 | 2013-06-02 17:46:57 +0200 | [diff] [blame] | 1248 | } |
| 1249 | return rc; |
| 1250 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1251 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1252 | for (i = 0; i < n; i++) |
Junio C Hamano | aab3b9a | 2009-03-12 00:02:12 -0700 | [diff] [blame] | 1253 | if (src[i] && src[i] != o->df_conflict_entry) |
Jeff King | 4616918 | 2014-11-24 13:36:51 -0500 | [diff] [blame] | 1254 | if (do_add_entry(o, src[i], 0, 0)) |
| 1255 | return -1; |
| 1256 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
| 1259 | |
Junio C Hamano | 353c5ee | 2009-09-19 16:36:45 -0700 | [diff] [blame] | 1260 | static int unpack_failed(struct unpack_trees_options *o, const char *message) |
| 1261 | { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1262 | discard_index(&o->internal.result); |
Nguyễn Thái Ngọc Duy | b165fac | 2019-03-22 16:31:36 +0700 | [diff] [blame] | 1263 | if (!o->quiet && !o->exiting_early) { |
Junio C Hamano | 353c5ee | 2009-09-19 16:36:45 -0700 | [diff] [blame] | 1264 | if (message) |
| 1265 | return error("%s", message); |
| 1266 | return -1; |
| 1267 | } |
| 1268 | return -1; |
| 1269 | } |
| 1270 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1271 | /* |
| 1272 | * The tree traversal is looking at name p. If we have a matching entry, |
| 1273 | * return it. If name p is a directory in the index, do not return |
| 1274 | * anything, as we will want to match it when the traversal descends into |
| 1275 | * the directory. |
| 1276 | */ |
| 1277 | static int find_cache_pos(struct traverse_info *info, |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1278 | const char *p, size_t p_len) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1279 | { |
| 1280 | int pos; |
| 1281 | struct unpack_trees_options *o = info->data; |
| 1282 | struct index_state *index = o->src_index; |
| 1283 | int pfxlen = info->pathlen; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1284 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1285 | for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1286 | const struct cache_entry *ce = index->cache[pos]; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1287 | const char *ce_name, *ce_slash; |
| 1288 | int cmp, ce_len; |
| 1289 | |
Brian Downing | e53e6b4 | 2010-06-10 21:59:07 -0500 | [diff] [blame] | 1290 | if (ce->ce_flags & CE_UNPACKED) { |
| 1291 | /* |
| 1292 | * cache_bottom entry is already unpacked, so |
| 1293 | * we can never match it; don't check it |
| 1294 | * again. |
| 1295 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1296 | if (pos == o->internal.cache_bottom) |
| 1297 | ++o->internal.cache_bottom; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1298 | continue; |
Brian Downing | e53e6b4 | 2010-06-10 21:59:07 -0500 | [diff] [blame] | 1299 | } |
David Turner | a672095 | 2016-01-22 14:58:43 -0500 | [diff] [blame] | 1300 | if (!ce_in_traverse_path(ce, info)) { |
| 1301 | /* |
| 1302 | * Check if we can skip future cache checks |
| 1303 | * (because we're already past all possible |
| 1304 | * entries in the traverse path). |
| 1305 | */ |
| 1306 | if (info->traverse_path) { |
| 1307 | if (strncmp(ce->name, info->traverse_path, |
| 1308 | info->pathlen) > 0) |
| 1309 | break; |
| 1310 | } |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1311 | continue; |
David Turner | a672095 | 2016-01-22 14:58:43 -0500 | [diff] [blame] | 1312 | } |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1313 | ce_name = ce->name + pfxlen; |
| 1314 | ce_slash = strchr(ce_name, '/'); |
| 1315 | if (ce_slash) |
| 1316 | ce_len = ce_slash - ce_name; |
| 1317 | else |
| 1318 | ce_len = ce_namelen(ce) - pfxlen; |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1319 | cmp = name_compare(p, p_len, ce_name, ce_len); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1320 | /* |
| 1321 | * Exact match; if we have a directory we need to |
| 1322 | * delay returning it. |
| 1323 | */ |
| 1324 | if (!cmp) |
| 1325 | return ce_slash ? -2 - pos : pos; |
| 1326 | if (0 < cmp) |
| 1327 | continue; /* keep looking */ |
| 1328 | /* |
| 1329 | * ce_name sorts after p->path; could it be that we |
| 1330 | * have files under p->path directory in the index? |
| 1331 | * E.g. ce_name == "t-i", and p->path == "t"; we may |
| 1332 | * have "t/a" in the index. |
| 1333 | */ |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1334 | if (p_len < ce_len && !memcmp(ce_name, p, p_len) && |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1335 | ce_name[p_len] < '/') |
| 1336 | continue; /* keep looking */ |
| 1337 | break; |
| 1338 | } |
| 1339 | return -1; |
| 1340 | } |
| 1341 | |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1342 | /* |
| 1343 | * Given a sparse directory entry 'ce', compare ce->name to |
Derrick Stolee | 8c5de0d | 2021-12-06 14:10:37 +0000 | [diff] [blame] | 1344 | * info->traverse_path + p->path + '/' if info->traverse_path |
| 1345 | * is non-empty. |
| 1346 | * |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1347 | * Compare ce->name to p->path + '/' otherwise. Note that |
| 1348 | * ce->name must end in a trailing '/' because it is a sparse |
| 1349 | * directory entry. |
| 1350 | */ |
| 1351 | static int sparse_dir_matches_path(const struct cache_entry *ce, |
| 1352 | struct traverse_info *info, |
| 1353 | const struct name_entry *p) |
| 1354 | { |
| 1355 | assert(S_ISSPARSEDIR(ce->ce_mode)); |
| 1356 | assert(ce->name[ce->ce_namelen - 1] == '/'); |
| 1357 | |
Derrick Stolee | 8c5de0d | 2021-12-06 14:10:37 +0000 | [diff] [blame] | 1358 | if (info->pathlen) |
| 1359 | return ce->ce_namelen == info->pathlen + p->pathlen + 1 && |
| 1360 | ce->name[info->pathlen - 1] == '/' && |
| 1361 | !strncmp(ce->name, info->traverse_path, info->pathlen) && |
| 1362 | !strncmp(ce->name + info->pathlen, p->path, p->pathlen); |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1363 | return ce->ce_namelen == p->pathlen + 1 && |
| 1364 | !strncmp(ce->name, p->path, p->pathlen); |
| 1365 | } |
| 1366 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1367 | static struct cache_entry *find_cache_entry(struct traverse_info *info, |
| 1368 | const struct name_entry *p) |
| 1369 | { |
Derrick Stolee | 72d84ea | 2021-09-08 01:42:27 +0000 | [diff] [blame] | 1370 | const char *path; |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1371 | int pos = find_cache_pos(info, p->path, p->pathlen); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1372 | struct unpack_trees_options *o = info->data; |
| 1373 | |
| 1374 | if (0 <= pos) |
| 1375 | return o->src_index->cache[pos]; |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* |
| 1378 | * Check for a sparse-directory entry named "path/". |
| 1379 | * Due to the input p->path not having a trailing |
| 1380 | * slash, the negative 'pos' value overshoots the |
| 1381 | * expected position, hence "-2" instead of "-1". |
| 1382 | */ |
| 1383 | pos = -pos - 2; |
| 1384 | |
| 1385 | if (pos < 0 || pos >= o->src_index->cache_nr) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1386 | return NULL; |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1387 | |
| 1388 | /* |
| 1389 | * Due to lexicographic sorting and sparse directory |
| 1390 | * entries ending with a trailing slash, our path as a |
| 1391 | * sparse directory (e.g "subdir/") and our path as a |
| 1392 | * file (e.g. "subdir") might be separated by other |
| 1393 | * paths (e.g. "subdir-"). |
| 1394 | */ |
| 1395 | while (pos >= 0) { |
Derrick Stolee | 72d84ea | 2021-09-08 01:42:27 +0000 | [diff] [blame] | 1396 | struct cache_entry *ce = o->src_index->cache[pos]; |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1397 | |
Derrick Stolee | 72d84ea | 2021-09-08 01:42:27 +0000 | [diff] [blame] | 1398 | if (!skip_prefix(ce->name, info->traverse_path, &path) || |
| 1399 | strncmp(path, p->path, p->pathlen) || |
| 1400 | path[p->pathlen] != '/') |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1401 | return NULL; |
| 1402 | |
| 1403 | if (S_ISSPARSEDIR(ce->ce_mode) && |
| 1404 | sparse_dir_matches_path(ce, info, p)) |
| 1405 | return ce; |
| 1406 | |
| 1407 | pos--; |
| 1408 | } |
| 1409 | |
| 1410 | return NULL; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Junio C Hamano | ba655da | 2009-09-14 02:22:00 -0700 | [diff] [blame] | 1413 | static void debug_path(struct traverse_info *info) |
| 1414 | { |
| 1415 | if (info->prev) { |
| 1416 | debug_path(info->prev); |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1417 | if (*info->prev->name) |
Junio C Hamano | ba655da | 2009-09-14 02:22:00 -0700 | [diff] [blame] | 1418 | putchar('/'); |
| 1419 | } |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 1420 | printf("%s", info->name); |
Junio C Hamano | ba655da | 2009-09-14 02:22:00 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | static void debug_name_entry(int i, struct name_entry *n) |
| 1424 | { |
| 1425 | printf("ent#%d %06o %s\n", i, |
| 1426 | n->path ? n->mode : 0, |
| 1427 | n->path ? n->path : "(missing)"); |
| 1428 | } |
| 1429 | |
| 1430 | static void debug_unpack_callback(int n, |
| 1431 | unsigned long mask, |
| 1432 | unsigned long dirmask, |
| 1433 | struct name_entry *names, |
| 1434 | struct traverse_info *info) |
| 1435 | { |
| 1436 | int i; |
| 1437 | printf("* unpack mask %lu, dirmask %lu, cnt %d ", |
| 1438 | mask, dirmask, n); |
| 1439 | debug_path(info); |
| 1440 | putchar('\n'); |
| 1441 | for (i = 0; i < n; i++) |
| 1442 | debug_name_entry(i, names + i); |
| 1443 | } |
| 1444 | |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 1445 | /* |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1446 | * Returns true if and only if the given cache_entry is a |
| 1447 | * sparse-directory entry that matches the given name_entry |
| 1448 | * from the tree walk at the given traverse_info. |
| 1449 | */ |
| 1450 | static int is_sparse_directory_entry(struct cache_entry *ce, |
Victoria Dye | 037f8ea | 2022-09-01 21:02:33 +0000 | [diff] [blame] | 1451 | const struct name_entry *name, |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1452 | struct traverse_info *info) |
| 1453 | { |
| 1454 | if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode)) |
| 1455 | return 0; |
| 1456 | |
| 1457 | return sparse_dir_matches_path(ce, info, name); |
| 1458 | } |
| 1459 | |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 1460 | static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) |
| 1461 | { |
| 1462 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
| 1463 | struct unpack_trees_options *o = info->data; |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1464 | int ret, is_new_sparse_dir; |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 1465 | |
| 1466 | assert(o->merge); |
| 1467 | |
| 1468 | /* |
| 1469 | * Unlike in 'unpack_callback', where src[0] is derived from the index when |
| 1470 | * merging, src[0] is a transient cache entry derived from the first tree |
| 1471 | * provided. Create the temporary entry as if it came from a non-sparse index. |
| 1472 | */ |
| 1473 | if (!is_null_oid(&names[0].oid)) { |
| 1474 | src[0] = create_ce_entry(info, &names[0], 0, |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1475 | &o->internal.result, 1, |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 1476 | dirmask & (1ul << 0)); |
| 1477 | src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * 'unpack_single_entry' assumes that src[0] is derived directly from |
| 1482 | * the index, rather than from an entry in 'names'. This is *not* true when |
| 1483 | * merging a sparse directory, in which case names[0] is the "index" source |
| 1484 | * entry. To match the expectations of 'unpack_single_entry', shift past the |
| 1485 | * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and |
| 1486 | * 'dirmask' accordingly. |
| 1487 | */ |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1488 | ret = unpack_single_entry(n - 1, mask >> 1, dirmask >> 1, src, names + 1, info, &is_new_sparse_dir); |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 1489 | |
| 1490 | if (src[0]) |
| 1491 | discard_cache_entry(src[0]); |
| 1492 | |
| 1493 | return ret >= 0 ? mask : -1; |
| 1494 | } |
| 1495 | |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1496 | /* |
Nguyễn Thái Ngọc Duy | b4da373 | 2018-08-18 16:41:24 +0200 | [diff] [blame] | 1497 | * Note that traverse_by_cache_tree() duplicates some logic in this function |
| 1498 | * without actually calling it. If you change the logic here you may need to |
| 1499 | * check and change there as well. |
| 1500 | */ |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1501 | static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info) |
| 1502 | { |
René Scharfe | c7cddc1 | 2009-01-31 15:39:10 +0100 | [diff] [blame] | 1503 | struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, }; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1504 | struct unpack_trees_options *o = info->data; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1505 | const struct name_entry *p = names; |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1506 | int is_new_sparse_dir; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1507 | |
| 1508 | /* Find first entry with a real name (we could use "mask" too) */ |
| 1509 | while (!p->mode) |
| 1510 | p++; |
| 1511 | |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 1512 | if (o->internal.debug_unpack) |
Junio C Hamano | ba655da | 2009-09-14 02:22:00 -0700 | [diff] [blame] | 1513 | debug_unpack_callback(n, mask, dirmask, names, info); |
| 1514 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1515 | /* Are we supposed to look at the index too? */ |
| 1516 | if (o->merge) { |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1517 | while (1) { |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1518 | int cmp; |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1519 | struct cache_entry *ce; |
| 1520 | |
| 1521 | if (o->diff_index_cached) |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 1522 | ce = next_cache_entry(o); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 1523 | else |
| 1524 | ce = find_cache_entry(info, p); |
| 1525 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1526 | if (!ce) |
| 1527 | break; |
| 1528 | cmp = compare_entry(ce, info, p); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1529 | if (cmp < 0) { |
| 1530 | if (unpack_index_entry(ce, o) < 0) |
Junio C Hamano | 353c5ee | 2009-09-19 16:36:45 -0700 | [diff] [blame] | 1531 | return unpack_failed(o, NULL); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1532 | continue; |
| 1533 | } |
| 1534 | if (!cmp) { |
| 1535 | if (ce_stage(ce)) { |
| 1536 | /* |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1537 | * If we skip unmerged index |
| 1538 | * entries, we'll skip this |
| 1539 | * entry *and* the tree |
| 1540 | * entries associated with it! |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1541 | */ |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 1542 | if (o->skip_unmerged) { |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1543 | add_same_unmerged(ce, o); |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1544 | return mask; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 1545 | } |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1546 | } |
| 1547 | src[0] = ce; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1548 | } |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1553 | if (unpack_single_entry(n, mask, dirmask, src, names, info, &is_new_sparse_dir)) |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1554 | return -1; |
| 1555 | |
René Scharfe | 97e5954 | 2012-04-10 20:55:58 +0200 | [diff] [blame] | 1556 | if (o->merge && src[0]) { |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1557 | if (ce_stage(src[0])) |
| 1558 | mark_ce_used_same_name(src[0], o); |
| 1559 | else |
| 1560 | mark_ce_used(src[0], o); |
| 1561 | } |
| 1562 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1563 | /* Now handle any directories.. */ |
| 1564 | if (dirmask) { |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 1565 | /* special case: "diff-index --cached" looking at a tree */ |
| 1566 | if (o->diff_index_cached && |
| 1567 | n == 1 && dirmask == 1 && S_ISDIR(names->mode)) { |
| 1568 | int matches; |
| 1569 | matches = cache_tree_matches_traversal(o->src_index->cache_tree, |
| 1570 | names, info); |
| 1571 | /* |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1572 | * Everything under the name matches; skip the |
| 1573 | * entire hierarchy. diff_index_cached codepath |
| 1574 | * special cases D/F conflicts in such a way that |
| 1575 | * it does not do any look-ahead, so this is safe. |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 1576 | */ |
| 1577 | if (matches) { |
Victoria Dye | bfc763d | 2022-03-17 15:55:35 +0000 | [diff] [blame] | 1578 | /* |
| 1579 | * Only increment the cache_bottom if the |
| 1580 | * directory isn't a sparse directory index |
| 1581 | * entry (if it is, it was already incremented) |
| 1582 | * in 'mark_ce_used()' |
| 1583 | */ |
| 1584 | if (!src[0] || !S_ISSPARSEDIR(src[0]->ce_mode)) |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1585 | o->internal.cache_bottom += matches; |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 1586 | return mask; |
| 1587 | } |
| 1588 | } |
| 1589 | |
Victoria Dye | 037f8ea | 2022-09-01 21:02:33 +0000 | [diff] [blame] | 1590 | if (!is_sparse_directory_entry(src[0], p, info) && |
Victoria Dye | b15207b | 2022-08-08 19:07:52 +0000 | [diff] [blame] | 1591 | !is_new_sparse_dir && |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1592 | traverse_trees_recursive(n, dirmask, mask & ~dirmask, |
| 1593 | names, info) < 0) { |
Junio C Hamano | 542c264 | 2008-03-10 01:26:23 -0700 | [diff] [blame] | 1594 | return -1; |
Derrick Stolee | 523506d | 2021-07-14 13:12:33 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1597 | return mask; |
| 1598 | } |
| 1599 | |
| 1600 | return mask; |
| 1601 | } |
| 1602 | |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1603 | static int clear_ce_flags_1(struct index_state *istate, |
| 1604 | struct cache_entry **cache, int nr, |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1605 | struct strbuf *prefix, |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1606 | int select_mask, int clear_mask, |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1607 | struct pattern_list *pl, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1608 | enum pattern_match_result default_match, |
| 1609 | int progress_nr); |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1610 | |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1611 | /* Whole directory matching */ |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1612 | static int clear_ce_flags_dir(struct index_state *istate, |
| 1613 | struct cache_entry **cache, int nr, |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1614 | struct strbuf *prefix, |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1615 | char *basename, |
| 1616 | int select_mask, int clear_mask, |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1617 | struct pattern_list *pl, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1618 | enum pattern_match_result default_match, |
| 1619 | int progress_nr) |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1620 | { |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1621 | struct cache_entry **cache_end; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1622 | int dtype = DT_DIR; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1623 | int rc; |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1624 | enum pattern_match_result ret, orig_ret; |
| 1625 | orig_ret = path_matches_pattern_list(prefix->buf, prefix->len, |
| 1626 | basename, &dtype, pl, istate); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1627 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1628 | strbuf_addch(prefix, '/'); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1629 | |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1630 | /* If undecided, use matching result of parent dir in defval */ |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1631 | if (orig_ret == UNDECIDED) |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1632 | ret = default_match; |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1633 | else |
| 1634 | ret = orig_ret; |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1635 | |
| 1636 | for (cache_end = cache; cache_end != cache + nr; cache_end++) { |
| 1637 | struct cache_entry *ce = *cache_end; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1638 | if (strncmp(ce->name, prefix->buf, prefix->len)) |
Nguyễn Thái Ngọc Duy | 2891109 | 2011-05-09 22:43:01 +0700 | [diff] [blame] | 1639 | break; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1642 | if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) { |
| 1643 | struct cache_entry **ce = cache; |
Derrick Stolee via GitGitGadget | 4c6c797 | 2020-01-10 01:59:30 +0000 | [diff] [blame] | 1644 | rc = cache_end - cache; |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1645 | |
| 1646 | while (ce < cache_end) { |
| 1647 | (*ce)->ce_flags &= ~clear_mask; |
| 1648 | ce++; |
| 1649 | } |
| 1650 | } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) { |
Derrick Stolee via GitGitGadget | 4c6c797 | 2020-01-10 01:59:30 +0000 | [diff] [blame] | 1651 | rc = cache_end - cache; |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1652 | } else { |
| 1653 | rc = clear_ce_flags_1(istate, cache, cache_end - cache, |
| 1654 | prefix, |
| 1655 | select_mask, clear_mask, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1656 | pl, ret, |
| 1657 | progress_nr); |
Derrick Stolee | eb42fec | 2019-11-21 22:04:43 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1660 | strbuf_setlen(prefix, prefix->len - 1); |
| 1661 | return rc; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Traverse the index, find every entry that matches according to |
Derrick Stolee | caa3d55 | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 1666 | * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1667 | * number of traversed entries. |
| 1668 | * |
| 1669 | * If select_mask is non-zero, only entries whose ce_flags has on of |
| 1670 | * those bits enabled are traversed. |
| 1671 | * |
| 1672 | * cache : pointer to an index entry |
| 1673 | * prefix_len : an offset to its path |
| 1674 | * |
| 1675 | * The current path ("prefix") including the trailing '/' is |
| 1676 | * cache[0]->name[0..(prefix_len-1)] |
| 1677 | * Top level path has prefix_len zero. |
| 1678 | */ |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1679 | static int clear_ce_flags_1(struct index_state *istate, |
| 1680 | struct cache_entry **cache, int nr, |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1681 | struct strbuf *prefix, |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1682 | int select_mask, int clear_mask, |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1683 | struct pattern_list *pl, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1684 | enum pattern_match_result default_match, |
| 1685 | int progress_nr) |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1686 | { |
Jeff King | d20bc01 | 2020-01-29 00:46:47 -0500 | [diff] [blame] | 1687 | struct cache_entry **cache_end = nr ? cache + nr : cache; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1688 | |
| 1689 | /* |
| 1690 | * Process all entries that have the given prefix and meet |
| 1691 | * select_mask condition |
| 1692 | */ |
| 1693 | while(cache != cache_end) { |
| 1694 | struct cache_entry *ce = *cache; |
| 1695 | const char *name, *slash; |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1696 | int len, dtype; |
| 1697 | enum pattern_match_result ret; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1698 | |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1699 | display_progress(istate->progress, progress_nr); |
| 1700 | |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1701 | if (select_mask && !(ce->ce_flags & select_mask)) { |
| 1702 | cache++; |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1703 | progress_nr++; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1704 | continue; |
| 1705 | } |
| 1706 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1707 | if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len)) |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1708 | break; |
| 1709 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1710 | name = ce->name + prefix->len; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1711 | slash = strchr(name, '/'); |
| 1712 | |
| 1713 | /* If it's a directory, try whole directory match first */ |
| 1714 | if (slash) { |
| 1715 | int processed; |
| 1716 | |
| 1717 | len = slash - name; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1718 | strbuf_add(prefix, name, len); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1719 | |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1720 | processed = clear_ce_flags_dir(istate, cache, cache_end - cache, |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1721 | prefix, |
| 1722 | prefix->buf + prefix->len - len, |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1723 | select_mask, clear_mask, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1724 | pl, default_match, |
| 1725 | progress_nr); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1726 | |
| 1727 | /* clear_c_f_dir eats a whole dir already? */ |
| 1728 | if (processed) { |
| 1729 | cache += processed; |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1730 | progress_nr += processed; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1731 | strbuf_setlen(prefix, prefix->len - len); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1732 | continue; |
| 1733 | } |
| 1734 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1735 | strbuf_addch(prefix, '/'); |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1736 | processed = clear_ce_flags_1(istate, cache, cache_end - cache, |
| 1737 | prefix, |
| 1738 | select_mask, clear_mask, pl, |
| 1739 | default_match, progress_nr); |
| 1740 | |
| 1741 | cache += processed; |
| 1742 | progress_nr += processed; |
| 1743 | |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1744 | strbuf_setlen(prefix, prefix->len - len - 1); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1745 | continue; |
| 1746 | } |
| 1747 | |
| 1748 | /* Non-directory */ |
| 1749 | dtype = ce_to_dtype(ce); |
Derrick Stolee | 468ce99 | 2019-09-03 11:04:58 -0700 | [diff] [blame] | 1750 | ret = path_matches_pattern_list(ce->name, |
| 1751 | ce_namelen(ce), |
| 1752 | name, &dtype, pl, istate); |
| 1753 | if (ret == UNDECIDED) |
| 1754 | ret = default_match; |
Derrick Stolee | f998a3f | 2020-01-31 20:16:15 +0000 | [diff] [blame] | 1755 | if (ret == MATCHED || ret == MATCHED_RECURSIVE) |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1756 | ce->ce_flags &= ~clear_mask; |
| 1757 | cache++; |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1758 | progress_nr++; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1759 | } |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1760 | |
| 1761 | display_progress(istate->progress, progress_nr); |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1762 | return nr - (cache_end - cache); |
| 1763 | } |
| 1764 | |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1765 | static int clear_ce_flags(struct index_state *istate, |
| 1766 | int select_mask, int clear_mask, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1767 | struct pattern_list *pl, |
| 1768 | int show_progress) |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1769 | { |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1770 | static struct strbuf prefix = STRBUF_INIT; |
Jeff Hostetler | e6152e3 | 2019-11-21 22:04:39 +0000 | [diff] [blame] | 1771 | char label[100]; |
| 1772 | int rval; |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1773 | |
| 1774 | strbuf_reset(&prefix); |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1775 | if (show_progress) |
| 1776 | istate->progress = start_delayed_progress( |
Patrick Steinhardt | 1f7e647 | 2024-12-17 07:43:48 +0100 | [diff] [blame] | 1777 | the_repository, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1778 | _("Updating index flags"), |
| 1779 | istate->cache_nr); |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1780 | |
Jeff Hostetler | e6152e3 | 2019-11-21 22:04:39 +0000 | [diff] [blame] | 1781 | xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)", |
| 1782 | (unsigned long)select_mask, (unsigned long)clear_mask); |
| 1783 | trace2_region_enter("unpack_trees", label, the_repository); |
| 1784 | rval = clear_ce_flags_1(istate, |
Nguyễn Thái Ngọc Duy | 27c82fb | 2018-08-13 18:14:28 +0200 | [diff] [blame] | 1785 | istate->cache, |
| 1786 | istate->cache_nr, |
Antoine Pelisse | fc2b621 | 2013-12-14 12:31:16 +0100 | [diff] [blame] | 1787 | &prefix, |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1788 | select_mask, clear_mask, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1789 | pl, 0, 0); |
Jeff Hostetler | e6152e3 | 2019-11-21 22:04:39 +0000 | [diff] [blame] | 1790 | trace2_region_leave("unpack_trees", label, the_repository); |
| 1791 | |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1792 | stop_progress(&istate->progress); |
Jeff Hostetler | e6152e3 | 2019-11-21 22:04:39 +0000 | [diff] [blame] | 1793 | return rval; |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1794 | } |
| 1795 | |
Junio C Hamano | 2e2b887 | 2008-05-28 15:12:30 -0700 | [diff] [blame] | 1796 | /* |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1797 | * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout |
| 1798 | */ |
Derrick Stolee | caa3d55 | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 1799 | static void mark_new_skip_worktree(struct pattern_list *pl, |
Nguyễn Thái Ngọc Duy | 86016ec | 2018-08-13 18:14:27 +0200 | [diff] [blame] | 1800 | struct index_state *istate, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1801 | int select_flag, int skip_wt_flag, |
| 1802 | int show_progress) |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1803 | { |
| 1804 | int i; |
| 1805 | |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1806 | /* |
| 1807 | * 1. Pretend the narrowest worktree: only unmerged entries |
| 1808 | * are checked out |
| 1809 | */ |
Nguyễn Thái Ngọc Duy | 86016ec | 2018-08-13 18:14:27 +0200 | [diff] [blame] | 1810 | for (i = 0; i < istate->cache_nr; i++) { |
| 1811 | struct cache_entry *ce = istate->cache[i]; |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1812 | |
| 1813 | if (select_flag && !(ce->ce_flags & select_flag)) |
| 1814 | continue; |
| 1815 | |
Max Kirillov | b33fdfc | 2018-07-10 22:17:48 +0300 | [diff] [blame] | 1816 | if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED)) |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1817 | ce->ce_flags |= skip_wt_flag; |
| 1818 | else |
| 1819 | ce->ce_flags &= ~skip_wt_flag; |
| 1820 | } |
Nguyễn Thái Ngọc Duy | 9037026 | 2010-11-27 01:17:46 +0700 | [diff] [blame] | 1821 | |
| 1822 | /* |
| 1823 | * 2. Widen worktree according to sparse-checkout file. |
| 1824 | * Matched entries will have skip_wt_flag cleared (i.e. "in") |
| 1825 | */ |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1826 | clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress); |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1829 | static void populate_from_existing_patterns(struct unpack_trees_options *o, |
| 1830 | struct pattern_list *pl) |
| 1831 | { |
Derrick Stolee | dd23022 | 2021-01-23 19:58:17 +0000 | [diff] [blame] | 1832 | if (get_sparse_checkout_patterns(pl) < 0) |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1833 | o->skip_sparse_checkout = 1; |
| 1834 | else |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 1835 | o->internal.pl = pl; |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Victoria Dye | 7497039 | 2022-03-01 20:24:29 +0000 | [diff] [blame] | 1838 | static void update_sparsity_for_prefix(const char *prefix, |
| 1839 | struct index_state *istate) |
| 1840 | { |
| 1841 | int prefix_len = strlen(prefix); |
| 1842 | struct strbuf ce_prefix = STRBUF_INIT; |
| 1843 | |
| 1844 | if (!istate->sparse_index) |
| 1845 | return; |
| 1846 | |
| 1847 | while (prefix_len > 0 && prefix[prefix_len - 1] == '/') |
| 1848 | prefix_len--; |
| 1849 | |
| 1850 | if (prefix_len <= 0) |
| 1851 | BUG("Invalid prefix passed to update_sparsity_for_prefix"); |
| 1852 | |
| 1853 | strbuf_grow(&ce_prefix, prefix_len + 1); |
| 1854 | strbuf_add(&ce_prefix, prefix, prefix_len); |
| 1855 | strbuf_addch(&ce_prefix, '/'); |
| 1856 | |
| 1857 | /* |
| 1858 | * If the prefix points to a sparse directory or a path inside a sparse |
| 1859 | * directory, the index should be expanded. This is accomplished in one |
| 1860 | * of two ways: |
| 1861 | * - if the prefix is inside a sparse directory, it will be expanded by |
| 1862 | * the 'ensure_full_index(...)' call in 'index_name_pos(...)'. |
| 1863 | * - if the prefix matches an existing sparse directory entry, |
| 1864 | * 'index_name_pos(...)' will return its index position, triggering |
| 1865 | * the 'ensure_full_index(...)' below. |
| 1866 | */ |
| 1867 | if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) && |
| 1868 | index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0) |
| 1869 | ensure_full_index(istate); |
| 1870 | |
| 1871 | strbuf_release(&ce_prefix); |
| 1872 | } |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1873 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 1874 | static int verify_absent(const struct cache_entry *, |
| 1875 | enum unpack_trees_error_types, |
| 1876 | struct unpack_trees_options *); |
Junio C Hamano | 2e2b887 | 2008-05-28 15:12:30 -0700 | [diff] [blame] | 1877 | /* |
| 1878 | * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the |
| 1879 | * resulting index, -2 on failure to reflect the changes to the work tree. |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1880 | * |
| 1881 | * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally |
Junio C Hamano | 2e2b887 | 2008-05-28 15:12:30 -0700 | [diff] [blame] | 1882 | */ |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 1883 | int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1884 | { |
Derrick Stolee | 6863df3 | 2021-03-30 13:10:52 +0000 | [diff] [blame] | 1885 | struct repository *repo = the_repository; |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 1886 | int i, ret; |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 1887 | static struct cache_entry *dfc; |
Derrick Stolee | caa3d55 | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 1888 | struct pattern_list pl; |
Elijah Newren | fa0bde4 | 2020-03-27 00:48:47 +0000 | [diff] [blame] | 1889 | int free_pattern_list = 0; |
Elijah Newren | c42e0b6 | 2021-09-27 16:33:42 +0000 | [diff] [blame] | 1890 | struct dir_struct dir = DIR_INIT; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1891 | |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 1892 | if (o->reset == UNPACK_RESET_INVALID) |
| 1893 | BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED"); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1894 | |
Junio C Hamano | ca885a4 | 2008-03-13 22:07:18 -0700 | [diff] [blame] | 1895 | if (len > MAX_UNPACK_TREES) |
| 1896 | die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES); |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 1897 | if (o->internal.dir) |
| 1898 | BUG("o->internal.dir is for internal use only"); |
| 1899 | if (o->internal.pl) |
| 1900 | BUG("o->internal.pl is for internal use only"); |
Elijah Newren | f297424 | 2023-02-27 15:28:20 +0000 | [diff] [blame] | 1901 | if (o->df_conflict_entry) |
| 1902 | BUG("o->df_conflict_entry is an output only field"); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1903 | |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 1904 | trace_performance_enter(); |
Derrick Stolee | c338898 | 2021-01-04 03:09:11 +0000 | [diff] [blame] | 1905 | trace2_region_enter("unpack_trees", "unpack_trees", the_repository); |
| 1906 | |
Derrick Stolee | 6863df3 | 2021-03-30 13:10:52 +0000 | [diff] [blame] | 1907 | prepare_repo_settings(repo); |
| 1908 | if (repo->settings.command_requires_full_index) { |
| 1909 | ensure_full_index(o->src_index); |
Ævar Arnfjörð Bjarmason | 29fefaf | 2023-01-12 13:55:25 +0100 | [diff] [blame] | 1910 | if (o->dst_index) |
| 1911 | ensure_full_index(o->dst_index); |
Derrick Stolee | 6863df3 | 2021-03-30 13:10:52 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Elijah Newren | 480d3d6 | 2021-09-27 16:33:44 +0000 | [diff] [blame] | 1914 | if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED && |
| 1915 | o->preserve_ignored) |
| 1916 | BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files"); |
| 1917 | |
Elijah Newren | 04988c8 | 2021-09-27 16:33:41 +0000 | [diff] [blame] | 1918 | if (!o->preserve_ignored) { |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 1919 | o->internal.dir = &dir; |
| 1920 | o->internal.dir->flags |= DIR_SHOW_IGNORED; |
| 1921 | setup_standard_excludes(o->internal.dir); |
Elijah Newren | 04988c8 | 2021-09-27 16:33:41 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Victoria Dye | 7497039 | 2022-03-01 20:24:29 +0000 | [diff] [blame] | 1924 | if (o->prefix) |
| 1925 | update_sparsity_for_prefix(o->prefix, o->src_index); |
| 1926 | |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 1927 | if (!core_apply_sparse_checkout || !o->update) |
| 1928 | o->skip_sparse_checkout = 1; |
Elijah Newren | 5d4f4a5 | 2023-02-27 15:28:13 +0000 | [diff] [blame] | 1929 | if (!o->skip_sparse_checkout) { |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1930 | memset(&pl, 0, sizeof(pl)); |
Elijah Newren | fa0bde4 | 2020-03-27 00:48:47 +0000 | [diff] [blame] | 1931 | free_pattern_list = 1; |
Elijah Newren | 30e89c1 | 2020-03-27 00:48:51 +0000 | [diff] [blame] | 1932 | populate_from_existing_patterns(o, &pl); |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 1933 | } |
| 1934 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1935 | index_state_init(&o->internal.result, o->src_index->repo); |
| 1936 | o->internal.result.initialized = 1; |
| 1937 | o->internal.result.timestamp.sec = o->src_index->timestamp.sec; |
| 1938 | o->internal.result.timestamp.nsec = o->src_index->timestamp.nsec; |
| 1939 | o->internal.result.version = o->src_index->version; |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 1940 | if (!o->src_index->split_index) { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1941 | o->internal.result.split_index = NULL; |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 1942 | } else if (o->src_index == o->dst_index) { |
| 1943 | /* |
| 1944 | * o->dst_index (and thus o->src_index) will be discarded |
Elijah Newren | 0d680a7 | 2023-02-27 15:28:18 +0000 | [diff] [blame] | 1945 | * and overwritten with o->internal.result at the end of |
| 1946 | * this function, so just use src_index's split_index to |
| 1947 | * avoid having to create a new one. |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 1948 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1949 | o->internal.result.split_index = o->src_index->split_index; |
Johannes Schindelin | 061dd72 | 2023-03-26 22:45:43 +0000 | [diff] [blame] | 1950 | if (o->src_index->cache_changed & SPLIT_INDEX_ORDERED) |
Junio C Hamano | f315a8b | 2023-04-04 14:28:27 -0700 | [diff] [blame] | 1951 | o->internal.result.cache_changed |= SPLIT_INDEX_ORDERED; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1952 | o->internal.result.split_index->refcount++; |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 1953 | } else { |
Elijah Newren | 0d680a7 | 2023-02-27 15:28:18 +0000 | [diff] [blame] | 1954 | o->internal.result.split_index = |
| 1955 | init_split_index(&o->internal.result); |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 1956 | } |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1957 | oidcpy(&o->internal.result.oid, &o->src_index->oid); |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 1958 | o->internal.merge_size = len; |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1959 | mark_all_ce_unused(o->src_index); |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 1960 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1961 | o->internal.result.fsmonitor_last_update = |
Johannes Schindelin | 3dfd305 | 2021-03-17 15:30:48 +0000 | [diff] [blame] | 1962 | xstrdup_or_null(o->src_index->fsmonitor_last_update); |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1963 | o->internal.result.fsmonitor_has_run_once = o->src_index->fsmonitor_has_run_once; |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 1964 | |
Victoria Dye | 0f329b9 | 2022-05-10 23:32:32 +0000 | [diff] [blame] | 1965 | if (!o->src_index->initialized && |
| 1966 | !repo->settings.command_requires_full_index && |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1967 | is_sparse_index_allowed(&o->internal.result, 0)) |
| 1968 | o->internal.result.sparse_index = 1; |
Victoria Dye | 0f329b9 | 2022-05-10 23:32:32 +0000 | [diff] [blame] | 1969 | |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1970 | /* |
| 1971 | * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries |
| 1972 | */ |
| 1973 | if (!o->skip_sparse_checkout) |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 1974 | mark_new_skip_worktree(o->internal.pl, o->src_index, 0, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 1975 | CE_NEW_SKIP_WORKTREE, o->verbose_update); |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 1976 | |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 1977 | if (!dfc) |
Jeff King | 13494ed | 2008-10-23 04:30:58 +0000 | [diff] [blame] | 1978 | dfc = xcalloc(1, cache_entry_size(0)); |
Junio C Hamano | 0fb1eaa | 2006-12-04 02:11:39 -0800 | [diff] [blame] | 1979 | o->df_conflict_entry = dfc; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 1980 | |
| 1981 | if (len) { |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1982 | const char *prefix = o->prefix ? o->prefix : ""; |
| 1983 | struct traverse_info info; |
Linus Torvalds | 933bf40 | 2007-08-09 22:21:29 -0700 | [diff] [blame] | 1984 | |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1985 | setup_traverse_info(&info, prefix); |
| 1986 | info.fn = unpack_callback; |
| 1987 | info.data = o; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 1988 | info.show_all_errors = o->internal.show_all_errors; |
Junio C Hamano | 40e3725 | 2011-08-29 12:31:06 -0700 | [diff] [blame] | 1989 | info.pathspec = o->pathspec; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 1990 | |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1991 | if (o->prefix) { |
| 1992 | /* |
| 1993 | * Unpack existing index entries that sort before the |
| 1994 | * prefix the tree is spliced into. Note that o->merge |
| 1995 | * is always true in this case. |
| 1996 | */ |
| 1997 | while (1) { |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 1998 | struct cache_entry *ce = next_cache_entry(o); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 1999 | if (!ce) |
| 2000 | break; |
| 2001 | if (ce_in_traverse_path(ce, &info)) |
| 2002 | break; |
| 2003 | if (unpack_index_entry(ce, o) < 0) |
| 2004 | goto return_failed; |
| 2005 | } |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 2006 | } |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2007 | |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 2008 | trace_performance_enter(); |
Derrick Stolee | c338898 | 2021-01-04 03:09:11 +0000 | [diff] [blame] | 2009 | trace2_region_enter("unpack_trees", "traverse_trees", the_repository); |
Nguyễn Thái Ngọc Duy | 67022e0 | 2018-11-18 17:47:57 +0100 | [diff] [blame] | 2010 | ret = traverse_trees(o->src_index, len, t, &info); |
Derrick Stolee | c338898 | 2021-01-04 03:09:11 +0000 | [diff] [blame] | 2011 | trace2_region_leave("unpack_trees", "traverse_trees", the_repository); |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 2012 | trace_performance_leave("traverse_trees"); |
| 2013 | if (ret < 0) |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2014 | goto return_failed; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | /* Any left-over entries in the index? */ |
| 2018 | if (o->merge) { |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2019 | while (1) { |
Victoria Dye | 99430aa | 2022-03-17 15:55:36 +0000 | [diff] [blame] | 2020 | struct cache_entry *ce = next_cache_entry(o); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2021 | if (!ce) |
| 2022 | break; |
Linus Torvalds | 0190457 | 2008-03-05 20:15:44 -0800 | [diff] [blame] | 2023 | if (unpack_index_entry(ce, o) < 0) |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2024 | goto return_failed; |
Daniel Barkalow | 17e4642 | 2008-02-07 11:39:52 -0500 | [diff] [blame] | 2025 | } |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 2026 | } |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2027 | mark_all_ce_unused(o->src_index); |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 2028 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2029 | if (o->trivial_merges_only && o->internal.nontrivial_merge) { |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 2030 | ret = unpack_failed(o, "Merge requires file-level merging"); |
| 2031 | goto done; |
| 2032 | } |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 2033 | |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2034 | if (!o->skip_sparse_checkout) { |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2035 | /* |
| 2036 | * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1 |
Elijah Newren | 031ba55 | 2020-03-27 00:48:44 +0000 | [diff] [blame] | 2037 | * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2038 | * so apply_sparse_checkout() won't attempt to remove it from worktree |
| 2039 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2040 | mark_new_skip_worktree(o->internal.pl, &o->internal.result, |
Derrick Stolee | 4dcd4de | 2019-11-21 22:04:44 +0000 | [diff] [blame] | 2041 | CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE, |
| 2042 | o->verbose_update); |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2043 | |
Nguyễn Thái Ngọc Duy | 17d26a4 | 2011-09-22 21:24:22 +1000 | [diff] [blame] | 2044 | ret = 0; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2045 | for (i = 0; i < o->internal.result.cache_nr; i++) { |
| 2046 | struct cache_entry *ce = o->internal.result.cache[i]; |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2047 | |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2048 | /* |
| 2049 | * Entries marked with CE_ADDED in merged_entry() do not have |
| 2050 | * verify_absent() check (the check is effectively disabled |
| 2051 | * because CE_NEW_SKIP_WORKTREE is set unconditionally). |
| 2052 | * |
| 2053 | * Do the real check now because we have had |
| 2054 | * correct CE_NEW_SKIP_WORKTREE |
| 2055 | */ |
| 2056 | if (ce->ce_flags & CE_ADDED && |
Elijah Newren | 681c637 | 2020-03-27 00:49:00 +0000 | [diff] [blame] | 2057 | verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o)) |
| 2058 | ret = 1; |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2059 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2060 | if (apply_sparse_checkout(&o->internal.result, ce, o)) |
Elijah Newren | 681c637 | 2020-03-27 00:49:00 +0000 | [diff] [blame] | 2061 | ret = 1; |
Nguyễn Thái Ngọc Duy | 9e1afb1 | 2009-08-20 20:47:13 +0700 | [diff] [blame] | 2062 | } |
Elijah Newren | 681c637 | 2020-03-27 00:49:00 +0000 | [diff] [blame] | 2063 | if (ret == 1) { |
| 2064 | /* |
| 2065 | * Inability to sparsify or de-sparsify individual |
| 2066 | * paths is not an error, but just a warning. |
| 2067 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2068 | if (o->internal.show_all_errors) |
Elijah Newren | 681c637 | 2020-03-27 00:49:00 +0000 | [diff] [blame] | 2069 | display_warning_msgs(o); |
| 2070 | ret = 0; |
| 2071 | } |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2072 | } |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 2073 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2074 | ret = check_updates(o, &o->internal.result) ? (-2) : 0; |
Felipe Contreras | e28f764 | 2013-08-13 20:27:58 +0200 | [diff] [blame] | 2075 | if (o->dst_index) { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2076 | move_index_extensions(&o->internal.result, o->src_index); |
Brian Degenhardt | 52fca21 | 2015-07-28 15:30:40 -0400 | [diff] [blame] | 2077 | if (!ret) { |
Patrick Steinhardt | 9f11959 | 2024-10-07 06:38:15 +0200 | [diff] [blame] | 2078 | if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0) && |
| 2079 | cache_tree_verify(the_repository, |
| 2080 | &o->internal.result) < 0) { |
| 2081 | ret = -1; |
| 2082 | goto done; |
| 2083 | } |
| 2084 | |
Victoria Dye | 68fcd48 | 2022-11-10 19:06:02 +0000 | [diff] [blame] | 2085 | if (!o->skip_cache_tree_update && |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2086 | !cache_tree_fully_valid(o->internal.result.cache_tree)) |
| 2087 | cache_tree_update(&o->internal.result, |
Brian Degenhardt | 52fca21 | 2015-07-28 15:30:40 -0400 | [diff] [blame] | 2088 | WRITE_TREE_SILENT | |
| 2089 | WRITE_TREE_REPAIR); |
| 2090 | } |
Ben Peart | 1956ecd | 2019-02-15 12:59:21 -0500 | [diff] [blame] | 2091 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2092 | o->internal.result.updated_workdir = 1; |
Felipe Contreras | e28f764 | 2013-08-13 20:27:58 +0200 | [diff] [blame] | 2093 | discard_index(o->dst_index); |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2094 | *o->dst_index = o->internal.result; |
Patrick Steinhardt | 4dfd4f1 | 2024-08-14 08:52:39 +0200 | [diff] [blame] | 2095 | memset(&o->internal.result, 0, sizeof(o->internal.result)); |
Junio C Hamano | a16cc8b | 2014-11-17 12:12:41 -0800 | [diff] [blame] | 2096 | } else { |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2097 | discard_index(&o->internal.result); |
Felipe Contreras | e28f764 | 2013-08-13 20:27:58 +0200 | [diff] [blame] | 2098 | } |
Elijah Newren | 7db1183 | 2018-04-23 23:50:45 -0700 | [diff] [blame] | 2099 | o->src_index = NULL; |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 2100 | |
| 2101 | done: |
Elijah Newren | fa0bde4 | 2020-03-27 00:48:47 +0000 | [diff] [blame] | 2102 | if (free_pattern_list) |
Derrick Stolee | e091228 | 2019-11-21 22:04:46 +0000 | [diff] [blame] | 2103 | clear_pattern_list(&pl); |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2104 | if (o->internal.dir) { |
| 2105 | dir_clear(o->internal.dir); |
| 2106 | o->internal.dir = NULL; |
Elijah Newren | 04988c8 | 2021-09-27 16:33:41 +0000 | [diff] [blame] | 2107 | } |
Derrick Stolee | c338898 | 2021-01-04 03:09:11 +0000 | [diff] [blame] | 2108 | trace2_region_leave("unpack_trees", "unpack_trees", the_repository); |
Elijah Newren | fa0bde4 | 2020-03-27 00:48:47 +0000 | [diff] [blame] | 2109 | trace_performance_leave("unpack_trees"); |
Junio C Hamano | 2e2b887 | 2008-05-28 15:12:30 -0700 | [diff] [blame] | 2110 | return ret; |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2111 | |
| 2112 | return_failed: |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2113 | if (o->internal.show_all_errors) |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 2114 | display_error_msgs(o); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2115 | mark_all_ce_unused(o->src_index); |
Junio C Hamano | 026680f | 2010-01-24 17:35:58 -0800 | [diff] [blame] | 2116 | ret = unpack_failed(o, NULL); |
Junio C Hamano | b419482 | 2011-05-31 10:06:44 -0700 | [diff] [blame] | 2117 | if (o->exiting_early) |
| 2118 | ret = 0; |
Junio C Hamano | 026680f | 2010-01-24 17:35:58 -0800 | [diff] [blame] | 2119 | goto done; |
Johannes Schindelin | 16da134 | 2006-07-30 20:25:18 +0200 | [diff] [blame] | 2120 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2121 | |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2122 | /* |
| 2123 | * Update SKIP_WORKTREE bits according to sparsity patterns, and update |
| 2124 | * working directory to match. |
| 2125 | * |
| 2126 | * CE_NEW_SKIP_WORKTREE is used internally. |
| 2127 | */ |
Elijah Newren | 1147c56 | 2023-02-27 15:28:14 +0000 | [diff] [blame] | 2128 | enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, |
| 2129 | struct pattern_list *pl) |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2130 | { |
| 2131 | enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS; |
Derrick Stolee | ace224a | 2020-05-04 18:27:43 +0000 | [diff] [blame] | 2132 | int i; |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2133 | unsigned old_show_all_errors; |
| 2134 | int free_pattern_list = 0; |
| 2135 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2136 | old_show_all_errors = o->internal.show_all_errors; |
| 2137 | o->internal.show_all_errors = 1; |
| 2138 | index_state_init(&o->internal.result, o->src_index->repo); |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2139 | |
| 2140 | /* Sanity checks */ |
| 2141 | if (!o->update || o->index_only || o->skip_sparse_checkout) |
| 2142 | BUG("update_sparsity() is for reflecting sparsity patterns in working directory"); |
| 2143 | if (o->src_index != o->dst_index || o->fn) |
| 2144 | BUG("update_sparsity() called wrong"); |
| 2145 | |
| 2146 | trace_performance_enter(); |
| 2147 | |
| 2148 | /* If we weren't given patterns, use the recorded ones */ |
Elijah Newren | 1147c56 | 2023-02-27 15:28:14 +0000 | [diff] [blame] | 2149 | if (!pl) { |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2150 | free_pattern_list = 1; |
Elijah Newren | 1147c56 | 2023-02-27 15:28:14 +0000 | [diff] [blame] | 2151 | pl = xcalloc(1, sizeof(*pl)); |
| 2152 | populate_from_existing_patterns(o, pl); |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2153 | } |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2154 | o->internal.pl = pl; |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2155 | |
Derrick Stolee | 598b1e7 | 2022-05-23 13:48:46 +0000 | [diff] [blame] | 2156 | /* Expand sparse directories as needed */ |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2157 | expand_index(o->src_index, o->internal.pl); |
Derrick Stolee | 598b1e7 | 2022-05-23 13:48:46 +0000 | [diff] [blame] | 2158 | |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2159 | /* Set NEW_SKIP_WORKTREE on existing entries. */ |
| 2160 | mark_all_ce_unused(o->src_index); |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2161 | mark_new_skip_worktree(o->internal.pl, o->src_index, 0, |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2162 | CE_NEW_SKIP_WORKTREE, o->verbose_update); |
| 2163 | |
| 2164 | /* Then loop over entries and update/remove as needed */ |
| 2165 | ret = UPDATE_SPARSITY_SUCCESS; |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2166 | for (i = 0; i < o->src_index->cache_nr; i++) { |
| 2167 | struct cache_entry *ce = o->src_index->cache[i]; |
| 2168 | |
Elijah Newren | ebb568b | 2020-03-27 00:48:59 +0000 | [diff] [blame] | 2169 | |
| 2170 | if (ce_stage(ce)) { |
| 2171 | /* -1 because for loop will increment by 1 */ |
| 2172 | i += warn_conflicted_path(o->src_index, i, o) - 1; |
| 2173 | ret = UPDATE_SPARSITY_WARNINGS; |
| 2174 | continue; |
| 2175 | } |
| 2176 | |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2177 | if (apply_sparse_checkout(o->src_index, ce, o)) |
| 2178 | ret = UPDATE_SPARSITY_WARNINGS; |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2181 | if (check_updates(o, o->src_index)) |
| 2182 | ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES; |
| 2183 | |
Elijah Newren | 6271d77 | 2020-03-27 00:48:57 +0000 | [diff] [blame] | 2184 | display_warning_msgs(o); |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2185 | o->internal.show_all_errors = old_show_all_errors; |
Elijah Newren | 1147c56 | 2023-02-27 15:28:14 +0000 | [diff] [blame] | 2186 | if (free_pattern_list) { |
| 2187 | clear_pattern_list(pl); |
| 2188 | free(pl); |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2189 | o->internal.pl = NULL; |
Elijah Newren | 1147c56 | 2023-02-27 15:28:14 +0000 | [diff] [blame] | 2190 | } |
Elijah Newren | 7af7a25 | 2020-03-27 00:48:52 +0000 | [diff] [blame] | 2191 | trace_performance_leave("update_sparsity"); |
| 2192 | return ret; |
| 2193 | } |
| 2194 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2195 | /* Here come the merge functions */ |
| 2196 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2197 | static int reject_merge(const struct cache_entry *ce, |
| 2198 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2199 | { |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 2200 | return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2203 | static int same(const struct cache_entry *a, const struct cache_entry *b) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2204 | { |
| 2205 | if (!!a != !!b) |
| 2206 | return 0; |
| 2207 | if (!a && !b) |
| 2208 | return 1; |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2209 | if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED) |
| 2210 | return 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2211 | return a->ce_mode == b->ce_mode && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 2212 | oideq(&a->oid, &b->oid); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | |
| 2216 | /* |
| 2217 | * When a CE gets turned into an unmerged entry, we |
| 2218 | * want it to be up-to-date |
| 2219 | */ |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2220 | static int verify_uptodate_1(const struct cache_entry *ce, |
| 2221 | struct unpack_trees_options *o, |
| 2222 | enum unpack_trees_error_types error_type) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2223 | { |
| 2224 | struct stat st; |
| 2225 | |
Nguyễn Thái Ngọc Duy | d5b6629 | 2011-07-30 10:55:05 +0700 | [diff] [blame] | 2226 | if (o->index_only) |
| 2227 | return 0; |
| 2228 | |
| 2229 | /* |
| 2230 | * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again |
| 2231 | * if this entry is truly up-to-date because this file may be |
| 2232 | * overwritten. |
| 2233 | */ |
| 2234 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
| 2235 | ; /* keep checking */ |
| 2236 | else if (o->reset || ce_uptodate(ce)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2237 | return 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2238 | |
| 2239 | if (!lstat(ce->name, &st)) { |
Nguyễn Thái Ngọc Duy | d5b6629 | 2011-07-30 10:55:05 +0700 | [diff] [blame] | 2240 | int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE; |
| 2241 | unsigned changed = ie_match_stat(o->src_index, ce, &st, flags); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2242 | |
| 2243 | if (submodule_from_ce(ce)) { |
| 2244 | int r = check_submodule_move_head(ce, |
| 2245 | "HEAD", oid_to_hex(&ce->oid), o); |
| 2246 | if (r) |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 2247 | return add_rejected_path(o, error_type, ce->name); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2248 | return 0; |
| 2249 | } |
| 2250 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2251 | if (!changed) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2252 | return 0; |
Junio C Hamano | 936492d | 2007-08-03 22:13:09 -0700 | [diff] [blame] | 2253 | /* |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2254 | * Historic default policy was to allow submodule to be out |
| 2255 | * of sync wrt the superproject index. If the submodule was |
| 2256 | * not considered interesting above, we don't care here. |
Junio C Hamano | 936492d | 2007-08-03 22:13:09 -0700 | [diff] [blame] | 2257 | */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2258 | if (S_ISGITLINK(ce->ce_mode)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2259 | return 0; |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2260 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2261 | errno = 0; |
| 2262 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2263 | if (errno == ENOENT) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2264 | return 0; |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 2265 | return add_rejected_path(o, error_type, ce->name); |
Nguyễn Thái Ngọc Duy | 35a5aa7 | 2009-08-20 20:47:07 +0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Elijah Newren | 64b1abe | 2018-04-19 10:58:12 -0700 | [diff] [blame] | 2268 | int verify_uptodate(const struct cache_entry *ce, |
| 2269 | struct unpack_trees_options *o) |
Nguyễn Thái Ngọc Duy | 35a5aa7 | 2009-08-20 20:47:07 +0700 | [diff] [blame] | 2270 | { |
Elijah Newren | 26b5d6b | 2022-01-14 15:59:40 +0000 | [diff] [blame] | 2271 | if (!o->skip_sparse_checkout && |
| 2272 | (ce->ce_flags & CE_SKIP_WORKTREE) && |
| 2273 | (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) |
Nguyễn Thái Ngọc Duy | f1f523e | 2009-08-20 20:47:10 +0700 | [diff] [blame] | 2274 | return 0; |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 2275 | return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2278 | static int verify_uptodate_sparse(const struct cache_entry *ce, |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2279 | struct unpack_trees_options *o) |
| 2280 | { |
Elijah Newren | 1ac83f4 | 2020-03-27 00:48:56 +0000 | [diff] [blame] | 2281 | return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
Nguyễn Thái Ngọc Duy | 383480b | 2018-08-13 18:14:26 +0200 | [diff] [blame] | 2284 | /* |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2285 | * TODO: We should actually invalidate o->internal.result, not src_index [1]. |
Nguyễn Thái Ngọc Duy | 383480b | 2018-08-13 18:14:26 +0200 | [diff] [blame] | 2286 | * But since cache tree and untracked cache both are not copied to |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2287 | * o->internal.result until unpacking is complete, we invalidate them on |
Nguyễn Thái Ngọc Duy | 383480b | 2018-08-13 18:14:26 +0200 | [diff] [blame] | 2288 | * src_index instead with the assumption that they will be copied to |
| 2289 | * dst_index at the end. |
| 2290 | * |
| 2291 | * [1] src_index->cache_tree is also used in unpack_callback() so if |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2292 | * we invalidate o->internal.result, we need to update it to use |
| 2293 | * o->internal.result.cache_tree as well. |
Nguyễn Thái Ngọc Duy | 383480b | 2018-08-13 18:14:26 +0200 | [diff] [blame] | 2294 | */ |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2295 | static void invalidate_ce_path(const struct cache_entry *ce, |
| 2296 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2297 | { |
Nguyễn Thái Ngọc Duy | e931371 | 2015-03-08 17:12:35 +0700 | [diff] [blame] | 2298 | if (!ce) |
| 2299 | return; |
| 2300 | cache_tree_invalidate_path(o->src_index, ce->name); |
Nguyễn Thái Ngọc Duy | 0cacebf | 2018-02-07 16:21:40 +0700 | [diff] [blame] | 2301 | untracked_cache_invalidate_path(o->src_index, ce->name, 1); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2302 | } |
| 2303 | |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2304 | /* |
| 2305 | * Check that checking out ce->sha1 in subdir ce->name is not |
| 2306 | * going to overwrite any working files. |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2307 | */ |
Stefan Beller | d6b1230 | 2017-03-14 14:46:38 -0700 | [diff] [blame] | 2308 | static int verify_clean_submodule(const char *old_sha1, |
| 2309 | const struct cache_entry *ce, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2310 | struct unpack_trees_options *o) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2311 | { |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2312 | if (!submodule_from_ce(ce)) |
| 2313 | return 0; |
| 2314 | |
| 2315 | return check_submodule_move_head(ce, old_sha1, |
| 2316 | oid_to_hex(&ce->oid), o); |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2319 | static int verify_clean_subdirectory(const struct cache_entry *ce, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2320 | struct unpack_trees_options *o) |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2321 | { |
| 2322 | /* |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2323 | * we are about to extract "ce->name"; we would not want to lose |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2324 | * anything in the existing directory there. |
| 2325 | */ |
| 2326 | int namelen; |
Clemens Buchacher | 7b9e3ce | 2009-01-01 21:54:33 +0100 | [diff] [blame] | 2327 | int i; |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2328 | struct dir_struct d; |
| 2329 | char *pathbuf; |
| 2330 | int cnt = 0; |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2331 | |
Stefan Beller | d6b1230 | 2017-03-14 14:46:38 -0700 | [diff] [blame] | 2332 | if (S_ISGITLINK(ce->ce_mode)) { |
brian m. carlson | 1053fe8 | 2017-10-15 22:07:06 +0000 | [diff] [blame] | 2333 | struct object_id oid; |
Patrick Steinhardt | e19488a | 2024-05-17 10:18:39 +0200 | [diff] [blame] | 2334 | int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name, |
| 2335 | "HEAD", &oid); |
Stefan Beller | d6b1230 | 2017-03-14 14:46:38 -0700 | [diff] [blame] | 2336 | /* |
| 2337 | * If we are not going to update the submodule, then |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2338 | * we don't care. |
| 2339 | */ |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 2340 | if (!sub_head && oideq(&oid, &ce->oid)) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2341 | return 0; |
brian m. carlson | 1053fe8 | 2017-10-15 22:07:06 +0000 | [diff] [blame] | 2342 | return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid), |
Jeff King | df351c6 | 2019-03-20 04:15:27 -0400 | [diff] [blame] | 2343 | ce, o); |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2344 | } |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2345 | |
| 2346 | /* |
| 2347 | * First let's make sure we do not have a local modification |
| 2348 | * in that directory. |
| 2349 | */ |
Thomas Gummerer | 68c4f6a | 2012-07-06 18:07:30 +0200 | [diff] [blame] | 2350 | namelen = ce_namelen(ce); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2351 | for (i = locate_in_src_index(ce, o); |
| 2352 | i < o->src_index->cache_nr; |
| 2353 | i++) { |
Clemens Buchacher | 837e5fe | 2009-01-01 21:54:32 +0100 | [diff] [blame] | 2354 | struct cache_entry *ce2 = o->src_index->cache[i]; |
| 2355 | int len = ce_namelen(ce2); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2356 | if (len < namelen || |
Clemens Buchacher | 837e5fe | 2009-01-01 21:54:32 +0100 | [diff] [blame] | 2357 | strncmp(ce->name, ce2->name, namelen) || |
| 2358 | ce2->name[namelen] != '/') |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2359 | break; |
| 2360 | /* |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2361 | * ce2->name is an entry in the subdirectory to be |
| 2362 | * removed. |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2363 | */ |
Clemens Buchacher | 837e5fe | 2009-01-01 21:54:32 +0100 | [diff] [blame] | 2364 | if (!ce_stage(ce2)) { |
| 2365 | if (verify_uptodate(ce2, o)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2366 | return -1; |
Clemens Buchacher | 837e5fe | 2009-01-01 21:54:32 +0100 | [diff] [blame] | 2367 | add_entry(o, ce2, CE_REMOVE, 0); |
Nguyễn Thái Ngọc Duy | 5697ca9 | 2018-08-18 16:41:27 +0200 | [diff] [blame] | 2368 | invalidate_ce_path(ce, o); |
Junio C Hamano | da165f4 | 2010-01-07 14:59:54 -0800 | [diff] [blame] | 2369 | mark_ce_used(ce2, o); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2370 | } |
| 2371 | cnt++; |
| 2372 | } |
| 2373 | |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 2374 | /* Do not lose a locally present file that is not ignored. */ |
Jeff King | 75faa45 | 2015-09-24 17:07:03 -0400 | [diff] [blame] | 2375 | pathbuf = xstrfmt("%.*s/", namelen, ce->name); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2376 | |
| 2377 | memset(&d, 0, sizeof(d)); |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2378 | if (o->internal.dir) |
Elijah Newren | b413a82 | 2023-02-27 15:28:09 +0000 | [diff] [blame] | 2379 | setup_standard_excludes(&d); |
Nguyễn Thái Ngọc Duy | c7f3259 | 2018-08-13 18:14:29 +0200 | [diff] [blame] | 2380 | i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL); |
Ævar Arnfjörð Bjarmason | e5a917f | 2021-10-07 11:46:09 +0200 | [diff] [blame] | 2381 | dir_clear(&d); |
| 2382 | free(pathbuf); |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2383 | if (i) |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 2384 | return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name); |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 2385 | |
| 2386 | /* Do not lose startup_info->original_cwd */ |
| 2387 | if (startup_info->original_cwd && |
| 2388 | !strcmp(startup_info->original_cwd, ce->name)) |
| 2389 | return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name); |
| 2390 | |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2391 | return cnt; |
| 2392 | } |
| 2393 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2394 | /* |
Linus Torvalds | 32260ad | 2008-03-22 09:35:59 -0700 | [diff] [blame] | 2395 | * This gets called when there was no index entry for the tree entry 'dst', |
| 2396 | * but we found a file in the working tree that 'lstat()' said was fine, |
| 2397 | * and we're on a case-insensitive filesystem. |
| 2398 | * |
| 2399 | * See if we can find a case-insensitive match in the index that also |
| 2400 | * matches the stat information, and assume it's that other file! |
| 2401 | */ |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2402 | static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st) |
Linus Torvalds | 32260ad | 2008-03-22 09:35:59 -0700 | [diff] [blame] | 2403 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 2404 | const struct cache_entry *src; |
Linus Torvalds | 32260ad | 2008-03-22 09:35:59 -0700 | [diff] [blame] | 2405 | |
Eric Sunshine | ebbd743 | 2013-09-17 03:06:15 -0400 | [diff] [blame] | 2406 | src = index_file_exists(o->src_index, name, len, 1); |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 2407 | return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); |
Linus Torvalds | 32260ad | 2008-03-22 09:35:59 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2410 | enum absent_checking_type { |
| 2411 | COMPLETELY_ABSENT, |
| 2412 | ABSENT_ANY_DIRECTORY |
| 2413 | }; |
| 2414 | |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2415 | static int check_ok_to_remove(const char *name, int len, int dtype, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2416 | const struct cache_entry *ce, struct stat *st, |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2417 | enum unpack_trees_error_types error_type, |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2418 | enum absent_checking_type absent_type, |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2419 | struct unpack_trees_options *o) |
| 2420 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 2421 | const struct cache_entry *result; |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2422 | |
| 2423 | /* |
| 2424 | * It may be that the 'lstat()' succeeded even though |
| 2425 | * target 'ce' was absent, because there is an old |
| 2426 | * entry that is different only in case.. |
| 2427 | * |
| 2428 | * Ignore that lstat() if it matches. |
| 2429 | */ |
| 2430 | if (ignore_case && icase_exists(o, name, len, st)) |
| 2431 | return 0; |
| 2432 | |
Elijah Newren | 576de3d | 2023-02-27 15:28:16 +0000 | [diff] [blame] | 2433 | if (o->internal.dir && |
| 2434 | is_excluded(o->internal.dir, o->src_index, name, &dtype)) |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2435 | /* |
| 2436 | * ce->name is explicitly excluded, so it is Ok to |
| 2437 | * overwrite it. |
| 2438 | */ |
| 2439 | return 0; |
| 2440 | if (S_ISDIR(st->st_mode)) { |
| 2441 | /* |
| 2442 | * We are checking out path "foo" and |
| 2443 | * found "foo/." in the working tree. |
| 2444 | * This is tricky -- if we have modified |
| 2445 | * files that are in "foo/" we would lose |
| 2446 | * them. |
| 2447 | */ |
Jeff King | df351c6 | 2019-03-20 04:15:27 -0400 | [diff] [blame] | 2448 | if (verify_clean_subdirectory(ce, o) < 0) |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2449 | return -1; |
| 2450 | return 0; |
| 2451 | } |
| 2452 | |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2453 | /* If we only care about directories, then we can remove */ |
| 2454 | if (absent_type == ABSENT_ANY_DIRECTORY) |
| 2455 | return 0; |
| 2456 | |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2457 | /* |
| 2458 | * The previous round may already have decided to |
| 2459 | * delete this path, which is in a subdirectory that |
| 2460 | * is being replaced with a blob. |
| 2461 | */ |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2462 | result = index_file_exists(&o->internal.result, name, len, 0); |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2463 | if (result) { |
| 2464 | if (result->ce_flags & CE_REMOVE) |
| 2465 | return 0; |
| 2466 | } |
| 2467 | |
Nguyễn Thái Ngọc Duy | 191e9d2 | 2019-03-22 16:31:35 +0700 | [diff] [blame] | 2468 | return add_rejected_path(o, error_type, name); |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Linus Torvalds | 32260ad | 2008-03-22 09:35:59 -0700 | [diff] [blame] | 2471 | /* |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2472 | * We do not want to remove or overwrite a working tree file that |
Junio C Hamano | f8a9d42 | 2006-12-04 16:00:46 -0800 | [diff] [blame] | 2473 | * is not tracked, unless it is ignored. |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2474 | */ |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2475 | static int verify_absent_1(const struct cache_entry *ce, |
| 2476 | enum unpack_trees_error_types error_type, |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2477 | enum absent_checking_type absent_type, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2478 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2479 | { |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 2480 | int len; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2481 | struct stat st; |
| 2482 | |
Elijah Newren | 0b0ee33 | 2021-12-09 05:08:28 +0000 | [diff] [blame] | 2483 | if (o->index_only || !o->update) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2484 | return 0; |
Junio C Hamano | c819353 | 2007-03-15 23:25:22 -0700 | [diff] [blame] | 2485 | |
Elijah Newren | 0b0ee33 | 2021-12-09 05:08:28 +0000 | [diff] [blame] | 2486 | if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) { |
| 2487 | /* Avoid nuking startup_info->original_cwd... */ |
| 2488 | if (startup_info->original_cwd && |
| 2489 | !strcmp(startup_info->original_cwd, ce->name)) |
| 2490 | return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, |
| 2491 | ce->name); |
| 2492 | /* ...but nuke anything else. */ |
| 2493 | return 0; |
| 2494 | } |
| 2495 | |
Matheus Tavares | fab78a0 | 2021-03-18 15:43:47 -0300 | [diff] [blame] | 2496 | len = check_leading_path(ce->name, ce_namelen(ce), 0); |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 2497 | if (!len) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2498 | return 0; |
Clemens Buchacher | f66caaf | 2010-10-09 15:53:00 +0200 | [diff] [blame] | 2499 | else if (len > 0) { |
Jeff King | f514ef9 | 2015-08-19 14:12:37 -0400 | [diff] [blame] | 2500 | char *path; |
| 2501 | int ret; |
Junio C Hamano | ec0603e | 2007-07-12 01:04:16 -0700 | [diff] [blame] | 2502 | |
Jeff King | f514ef9 | 2015-08-19 14:12:37 -0400 | [diff] [blame] | 2503 | path = xmemdupz(ce->name, len); |
| 2504 | if (lstat(path, &st)) |
Nguyễn Thái Ngọc Duy | 43c728e | 2016-05-08 16:47:58 +0700 | [diff] [blame] | 2505 | ret = error_errno("cannot stat '%s'", path); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2506 | else { |
| 2507 | if (submodule_from_ce(ce)) |
| 2508 | ret = check_submodule_move_head(ce, |
| 2509 | oid_to_hex(&ce->oid), |
| 2510 | NULL, o); |
| 2511 | else |
| 2512 | ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL, |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2513 | &st, error_type, |
| 2514 | absent_type, o); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2515 | } |
Jeff King | f514ef9 | 2015-08-19 14:12:37 -0400 | [diff] [blame] | 2516 | free(path); |
| 2517 | return ret; |
Jonathan Nieder | 92fda79 | 2011-01-12 20:26:36 -0600 | [diff] [blame] | 2518 | } else if (lstat(ce->name, &st)) { |
| 2519 | if (errno != ENOENT) |
Nguyễn Thái Ngọc Duy | 43c728e | 2016-05-08 16:47:58 +0700 | [diff] [blame] | 2520 | return error_errno("cannot stat '%s'", ce->name); |
Jonathan Nieder | 92fda79 | 2011-01-12 20:26:36 -0600 | [diff] [blame] | 2521 | return 0; |
| 2522 | } else { |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2523 | if (submodule_from_ce(ce)) |
| 2524 | return check_submodule_move_head(ce, oid_to_hex(&ce->oid), |
| 2525 | NULL, o); |
| 2526 | |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2527 | return check_ok_to_remove(ce->name, ce_namelen(ce), |
Jonathan Nieder | 92fda79 | 2011-01-12 20:26:36 -0600 | [diff] [blame] | 2528 | ce_to_dtype(ce), ce, &st, |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2529 | error_type, absent_type, o); |
Jonathan Nieder | 92fda79 | 2011-01-12 20:26:36 -0600 | [diff] [blame] | 2530 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2531 | } |
Clemens Buchacher | a9307f5 | 2010-10-09 15:52:58 +0200 | [diff] [blame] | 2532 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2533 | static int verify_absent(const struct cache_entry *ce, |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 2534 | enum unpack_trees_error_types error_type, |
Nguyễn Thái Ngọc Duy | 35a5aa7 | 2009-08-20 20:47:07 +0700 | [diff] [blame] | 2535 | struct unpack_trees_options *o) |
| 2536 | { |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2537 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) |
Nguyễn Thái Ngọc Duy | f1f523e | 2009-08-20 20:47:10 +0700 | [diff] [blame] | 2538 | return 0; |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2539 | return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o); |
| 2540 | } |
| 2541 | |
| 2542 | static int verify_absent_if_directory(const struct cache_entry *ce, |
| 2543 | enum unpack_trees_error_types error_type, |
| 2544 | struct unpack_trees_options *o) |
| 2545 | { |
| 2546 | if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE)) |
| 2547 | return 0; |
| 2548 | return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o); |
Nguyễn Thái Ngọc Duy | 35a5aa7 | 2009-08-20 20:47:07 +0700 | [diff] [blame] | 2549 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2550 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2551 | static int verify_absent_sparse(const struct cache_entry *ce, |
| 2552 | enum unpack_trees_error_types error_type, |
| 2553 | struct unpack_trees_options *o) |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2554 | { |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2555 | return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o); |
Nguyễn Thái Ngọc Duy | e800ec9 | 2009-08-20 20:47:09 +0700 | [diff] [blame] | 2556 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2557 | |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2558 | static int merged_entry(const struct cache_entry *ce, |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2559 | const struct cache_entry *old, |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2560 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2561 | { |
Linus Torvalds | 7f8ab8d | 2008-03-16 11:42:50 -0700 | [diff] [blame] | 2562 | int update = CE_UPDATE; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2563 | struct cache_entry *merge = dup_cache_entry(ce, &o->internal.result); |
Linus Torvalds | 7f8ab8d | 2008-03-16 11:42:50 -0700 | [diff] [blame] | 2564 | |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2565 | if (!old) { |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2566 | /* |
| 2567 | * New index entries. In sparse checkout, the following |
| 2568 | * verify_absent() will be delayed until after |
| 2569 | * traverse_trees() finishes in unpack_trees(), then: |
| 2570 | * |
| 2571 | * - CE_NEW_SKIP_WORKTREE will be computed correctly |
| 2572 | * - verify_absent() be called again, this time with |
| 2573 | * correct CE_NEW_SKIP_WORKTREE |
| 2574 | * |
| 2575 | * verify_absent() call here does nothing in sparse |
| 2576 | * checkout (i.e. o->skip_sparse_checkout == 0) |
| 2577 | */ |
| 2578 | update |= CE_ADDED; |
| 2579 | merge->ce_flags |= CE_NEW_SKIP_WORKTREE; |
| 2580 | |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2581 | if (verify_absent(merge, |
| 2582 | ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 2583 | discard_cache_entry(merge); |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2584 | return -1; |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2585 | } |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2586 | invalidate_ce_path(merge, o); |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2587 | |
Philippe Blain | e84704f | 2020-02-17 04:53:05 +0000 | [diff] [blame] | 2588 | if (submodule_from_ce(ce) && file_exists(ce->name)) { |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2589 | int ret = check_submodule_move_head(ce, NULL, |
| 2590 | oid_to_hex(&ce->oid), |
| 2591 | o); |
| 2592 | if (ret) |
| 2593 | return ret; |
| 2594 | } |
| 2595 | |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2596 | } else if (!(old->ce_flags & CE_CONFLICTED)) { |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2597 | /* |
| 2598 | * See if we can re-use the old CE directly? |
| 2599 | * That way we get the uptodate stat info. |
| 2600 | * |
Linus Torvalds | 7f8ab8d | 2008-03-16 11:42:50 -0700 | [diff] [blame] | 2601 | * This also removes the UPDATE flag on a match; otherwise |
| 2602 | * we will end up overwriting local changes in the work tree. |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2603 | */ |
| 2604 | if (same(old, merge)) { |
Linus Torvalds | eb7a2f1 | 2008-02-22 20:41:17 -0800 | [diff] [blame] | 2605 | copy_cache_entry(merge, old); |
Linus Torvalds | 7f8ab8d | 2008-03-16 11:42:50 -0700 | [diff] [blame] | 2606 | update = 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2607 | } else { |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2608 | if (verify_uptodate(old, o)) { |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 2609 | discard_cache_entry(merge); |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2610 | return -1; |
René Scharfe | f2fa354 | 2013-06-02 17:46:54 +0200 | [diff] [blame] | 2611 | } |
Nguyễn Thái Ngọc Duy | 2431afb | 2010-11-27 13:24:04 +0700 | [diff] [blame] | 2612 | /* Migrate old flags over */ |
| 2613 | update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); |
Linus Torvalds | bc052d7 | 2008-03-06 12:26:14 -0800 | [diff] [blame] | 2614 | invalidate_ce_path(old, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2615 | } |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2616 | |
Philippe Blain | e84704f | 2020-02-17 04:53:05 +0000 | [diff] [blame] | 2617 | if (submodule_from_ce(ce) && file_exists(ce->name)) { |
Stefan Beller | a7bc845 | 2017-03-14 14:46:39 -0700 | [diff] [blame] | 2618 | int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid), |
| 2619 | oid_to_hex(&ce->oid), |
| 2620 | o); |
| 2621 | if (ret) |
| 2622 | return ret; |
| 2623 | } |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2624 | } else { |
| 2625 | /* |
| 2626 | * Previously unmerged entry left as an existence |
| 2627 | * marker by read_index_unmerged(); |
| 2628 | */ |
Elijah Newren | 1fdd51a | 2021-09-27 16:33:45 +0000 | [diff] [blame] | 2629 | if (verify_absent_if_directory(merge, |
| 2630 | ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { |
| 2631 | discard_cache_entry(merge); |
| 2632 | return -1; |
| 2633 | } |
| 2634 | |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2635 | invalidate_ce_path(old, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
Johannes Schindelin | cc756ed | 2019-09-09 13:56:15 +0200 | [diff] [blame] | 2638 | if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0) |
| 2639 | return -1; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2640 | return 1; |
| 2641 | } |
| 2642 | |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 2643 | static int merged_sparse_dir(const struct cache_entry * const *src, int n, |
| 2644 | struct unpack_trees_options *o) |
| 2645 | { |
| 2646 | struct tree_desc t[MAX_UNPACK_TREES + 1]; |
| 2647 | void * tree_bufs[MAX_UNPACK_TREES + 1]; |
| 2648 | struct traverse_info info; |
| 2649 | int i, ret; |
| 2650 | |
| 2651 | /* |
| 2652 | * Create the tree traversal information for traversing into *only* the |
| 2653 | * sparse directory. |
| 2654 | */ |
| 2655 | setup_traverse_info(&info, src[0]->name); |
| 2656 | info.fn = unpack_sparse_callback; |
| 2657 | info.data = o; |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2658 | info.show_all_errors = o->internal.show_all_errors; |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 2659 | info.pathspec = o->pathspec; |
| 2660 | |
| 2661 | /* Get the tree descriptors of the sparse directory in each of the merging trees */ |
| 2662 | for (i = 0; i < n; i++) |
| 2663 | tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i], |
| 2664 | src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL); |
| 2665 | |
| 2666 | ret = traverse_trees(o->src_index, n, t, &info); |
| 2667 | |
| 2668 | for (i = 0; i < n; i++) |
| 2669 | free(tree_bufs[i]); |
| 2670 | |
| 2671 | return ret; |
| 2672 | } |
| 2673 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2674 | static int deleted_entry(const struct cache_entry *ce, |
| 2675 | const struct cache_entry *old, |
| 2676 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2677 | { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2678 | /* Did it exist in the index? */ |
| 2679 | if (!old) { |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 2680 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2681 | return -1; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2682 | return 0; |
Elijah Newren | 56d06fe | 2021-09-27 16:33:46 +0000 | [diff] [blame] | 2683 | } else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) { |
| 2684 | return -1; |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2685 | } |
Elijah Newren | 56d06fe | 2021-09-27 16:33:46 +0000 | [diff] [blame] | 2686 | |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 2687 | if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o)) |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2688 | return -1; |
| 2689 | add_entry(o, ce, CE_REMOVE, 0); |
Linus Torvalds | bc052d7 | 2008-03-06 12:26:14 -0800 | [diff] [blame] | 2690 | invalidate_ce_path(ce, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2691 | return 1; |
| 2692 | } |
| 2693 | |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2694 | static int keep_entry(const struct cache_entry *ce, |
| 2695 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2696 | { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2697 | add_entry(o, ce, 0, 0); |
Nguyễn Thái Ngọc Duy | 5697ca9 | 2018-08-18 16:41:27 +0200 | [diff] [blame] | 2698 | if (ce_stage(ce)) |
| 2699 | invalidate_ce_path(ce, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | #if DBRT_DEBUG |
| 2704 | static void show_stage_entry(FILE *o, |
| 2705 | const char *label, const struct cache_entry *ce) |
| 2706 | { |
| 2707 | if (!ce) |
| 2708 | fprintf(o, "%s (missing)\n", label); |
| 2709 | else |
| 2710 | fprintf(o, "%s%06o %s %d\t%s\n", |
| 2711 | label, |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 2712 | ce->ce_mode, |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 2713 | oid_to_hex(&ce->oid), |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2714 | ce_stage(ce), |
| 2715 | ce->name); |
| 2716 | } |
| 2717 | #endif |
| 2718 | |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 2719 | int threeway_merge(const struct cache_entry * const *stages, |
| 2720 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2721 | { |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2722 | const struct cache_entry *index; |
| 2723 | const struct cache_entry *head; |
| 2724 | const struct cache_entry *remote = stages[o->head_idx + 1]; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2725 | int count; |
| 2726 | int head_match = 0; |
| 2727 | int remote_match = 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2728 | |
| 2729 | int df_conflict_head = 0; |
| 2730 | int df_conflict_remote = 0; |
| 2731 | |
| 2732 | int any_anc_missing = 0; |
| 2733 | int no_anc_exists = 1; |
| 2734 | int i; |
| 2735 | |
| 2736 | for (i = 1; i < o->head_idx; i++) { |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2737 | if (!stages[i] || stages[i] == o->df_conflict_entry) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2738 | any_anc_missing = 1; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2739 | else |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2740 | no_anc_exists = 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | index = stages[0]; |
| 2744 | head = stages[o->head_idx]; |
| 2745 | |
| 2746 | if (head == o->df_conflict_entry) { |
| 2747 | df_conflict_head = 1; |
| 2748 | head = NULL; |
| 2749 | } |
| 2750 | |
| 2751 | if (remote == o->df_conflict_entry) { |
| 2752 | df_conflict_remote = 1; |
| 2753 | remote = NULL; |
| 2754 | } |
| 2755 | |
Junio C Hamano | cee2d6a | 2009-10-11 13:38:11 -0700 | [diff] [blame] | 2756 | /* |
| 2757 | * First, if there's a #16 situation, note that to prevent #13 |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2758 | * and #14. |
| 2759 | */ |
| 2760 | if (!same(remote, head)) { |
| 2761 | for (i = 1; i < o->head_idx; i++) { |
| 2762 | if (same(stages[i], head)) { |
| 2763 | head_match = i; |
| 2764 | } |
| 2765 | if (same(stages[i], remote)) { |
| 2766 | remote_match = i; |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
Junio C Hamano | cee2d6a | 2009-10-11 13:38:11 -0700 | [diff] [blame] | 2771 | /* |
| 2772 | * We start with cases where the index is allowed to match |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2773 | * something other than the head: #14(ALT) and #2ALT, where it |
| 2774 | * is permitted to match the result instead. |
| 2775 | */ |
| 2776 | /* #14, #14ALT, #2ALT */ |
| 2777 | if (remote && !df_conflict_head && head_match && !remote_match) { |
Victoria Dye | f27c170 | 2022-03-01 20:24:31 +0000 | [diff] [blame] | 2778 | if (index && !same(index, remote) && !same(index, head)) { |
| 2779 | if (S_ISSPARSEDIR(index->ce_mode)) |
| 2780 | return merged_sparse_dir(stages, 4, o); |
| 2781 | else |
| 2782 | return reject_merge(index, o); |
| 2783 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2784 | return merged_entry(remote, index, o); |
| 2785 | } |
| 2786 | /* |
| 2787 | * If we have an entry in the index cache, then we want to |
| 2788 | * make sure that it matches head. |
| 2789 | */ |
Victoria Dye | f27c170 | 2022-03-01 20:24:31 +0000 | [diff] [blame] | 2790 | if (index && !same(index, head)) { |
| 2791 | if (S_ISSPARSEDIR(index->ce_mode)) |
| 2792 | return merged_sparse_dir(stages, 4, o); |
| 2793 | else |
| 2794 | return reject_merge(index, o); |
| 2795 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2796 | |
| 2797 | if (head) { |
| 2798 | /* #5ALT, #15 */ |
| 2799 | if (same(head, remote)) |
| 2800 | return merged_entry(head, index, o); |
| 2801 | /* #13, #3ALT */ |
| 2802 | if (!df_conflict_remote && remote_match && !head_match) |
| 2803 | return merged_entry(head, index, o); |
| 2804 | } |
| 2805 | |
| 2806 | /* #1 */ |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2807 | if (!head && !remote && any_anc_missing) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2808 | return 0; |
| 2809 | |
Junio C Hamano | cee2d6a | 2009-10-11 13:38:11 -0700 | [diff] [blame] | 2810 | /* |
| 2811 | * Under the "aggressive" rule, we resolve mostly trivial |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2812 | * cases that we historically had git-merge-one-file resolve. |
| 2813 | */ |
| 2814 | if (o->aggressive) { |
Junio C Hamano | cee2d6a | 2009-10-11 13:38:11 -0700 | [diff] [blame] | 2815 | int head_deleted = !head; |
| 2816 | int remote_deleted = !remote; |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2817 | const struct cache_entry *ce = NULL; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2818 | |
| 2819 | if (index) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2820 | ce = index; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2821 | else if (head) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2822 | ce = head; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2823 | else if (remote) |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2824 | ce = remote; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2825 | else { |
| 2826 | for (i = 1; i < o->head_idx; i++) { |
| 2827 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
Sven Verdoolaege | 0cf7375 | 2007-07-17 20:28:28 +0200 | [diff] [blame] | 2828 | ce = stages[i]; |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2829 | break; |
| 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2834 | /* |
| 2835 | * Deleted in both. |
| 2836 | * Deleted in one and unchanged in the other. |
| 2837 | */ |
| 2838 | if ((head_deleted && remote_deleted) || |
| 2839 | (head_deleted && remote && remote_match) || |
| 2840 | (remote_deleted && head && head_match)) { |
| 2841 | if (index) |
| 2842 | return deleted_entry(index, index, o); |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 2843 | if (ce && !head_deleted) { |
Matthieu Moy | 08402b0 | 2010-08-11 10:38:06 +0200 | [diff] [blame] | 2844 | if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2845 | return -1; |
| 2846 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2847 | return 0; |
| 2848 | } |
| 2849 | /* |
| 2850 | * Added in both, identically. |
| 2851 | */ |
| 2852 | if (no_anc_exists && head && remote && same(head, remote)) |
| 2853 | return merged_entry(head, index, o); |
| 2854 | |
| 2855 | } |
| 2856 | |
Victoria Dye | f27c170 | 2022-03-01 20:24:31 +0000 | [diff] [blame] | 2857 | /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */ |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2858 | if (index) { |
Victoria Dye | f27c170 | 2022-03-01 20:24:31 +0000 | [diff] [blame] | 2859 | /* |
| 2860 | * If we've reached the "no merge" cases and we're merging |
| 2861 | * a sparse directory, we may have an "edit/edit" conflict that |
| 2862 | * can be resolved by individually merging directory contents. |
| 2863 | */ |
| 2864 | if (S_ISSPARSEDIR(index->ce_mode)) |
| 2865 | return merged_sparse_dir(stages, 4, o); |
| 2866 | |
| 2867 | /* |
| 2868 | * If we're not merging a sparse directory, ensure the index is |
| 2869 | * up-to-date to avoid files getting overwritten with conflict |
| 2870 | * resolution files |
| 2871 | */ |
Daniel Barkalow | 203a2fe | 2008-02-07 11:39:48 -0500 | [diff] [blame] | 2872 | if (verify_uptodate(index, o)) |
| 2873 | return -1; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2874 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2875 | |
Elijah Newren | 13e1fd6 | 2023-02-27 15:28:17 +0000 | [diff] [blame] | 2876 | o->internal.nontrivial_merge = 1; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2877 | |
Junio C Hamano | ea4b52a | 2007-04-07 05:42:01 -0700 | [diff] [blame] | 2878 | /* #2, #3, #4, #6, #7, #9, #10, #11. */ |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2879 | count = 0; |
| 2880 | if (!head_match || !remote_match) { |
| 2881 | for (i = 1; i < o->head_idx; i++) { |
Junio C Hamano | 4c4caaf | 2007-04-07 05:49:19 -0700 | [diff] [blame] | 2882 | if (stages[i] && stages[i] != o->df_conflict_entry) { |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 2883 | keep_entry(stages[i], o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2884 | count++; |
| 2885 | break; |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | #if DBRT_DEBUG |
| 2890 | else { |
| 2891 | fprintf(stderr, "read-tree: warning #16 detected\n"); |
| 2892 | show_stage_entry(stderr, "head ", stages[head_match]); |
| 2893 | show_stage_entry(stderr, "remote ", stages[remote_match]); |
| 2894 | } |
| 2895 | #endif |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 2896 | if (head) { count += keep_entry(head, o); } |
| 2897 | if (remote) { count += keep_entry(remote, o); } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2898 | return count; |
| 2899 | } |
| 2900 | |
| 2901 | /* |
| 2902 | * Two-way merge. |
| 2903 | * |
| 2904 | * The rule is to "carry forward" what is in the index without losing |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 2905 | * information across a "fast-forward", favoring a successful merge |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2906 | * over a merge failure when it makes sense. For details of the |
Todd Zullinger | d6b67ce | 2025-03-03 15:44:31 -0500 | [diff] [blame] | 2907 | * "carry forward" rule, please see <Documentation/git-read-tree.adoc>. |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2908 | * |
| 2909 | */ |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 2910 | int twoway_merge(const struct cache_entry * const *src, |
| 2911 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2912 | { |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2913 | const struct cache_entry *current = src[0]; |
| 2914 | const struct cache_entry *oldtree = src[1]; |
| 2915 | const struct cache_entry *newtree = src[2]; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2916 | |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 2917 | if (o->internal.merge_size != 2) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2918 | return error("Cannot do a twoway merge of %d trees", |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 2919 | o->internal.merge_size); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2920 | |
Junio C Hamano | b8ba153 | 2007-04-02 16:29:56 -0700 | [diff] [blame] | 2921 | if (oldtree == o->df_conflict_entry) |
| 2922 | oldtree = NULL; |
| 2923 | if (newtree == o->df_conflict_entry) |
| 2924 | newtree = NULL; |
| 2925 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2926 | if (current) { |
Jeff King | b018ff6 | 2012-12-29 15:51:54 -0500 | [diff] [blame] | 2927 | if (current->ce_flags & CE_CONFLICTED) { |
| 2928 | if (same(oldtree, newtree) || o->reset) { |
| 2929 | if (!newtree) |
| 2930 | return deleted_entry(current, current, o); |
| 2931 | else |
| 2932 | return merged_entry(newtree, current, o); |
| 2933 | } |
Jonathan Nieder | 6a143aa | 2014-08-12 17:03:18 -0700 | [diff] [blame] | 2934 | return reject_merge(current, o); |
Jonathan Nieder | 6c1db1b | 2014-08-12 17:00:45 -0700 | [diff] [blame] | 2935 | } else if ((!oldtree && !newtree) || /* 4 and 5 */ |
Jeff King | b018ff6 | 2012-12-29 15:51:54 -0500 | [diff] [blame] | 2936 | (!oldtree && newtree && |
| 2937 | same(current, newtree)) || /* 6 and 7 */ |
| 2938 | (oldtree && newtree && |
| 2939 | same(oldtree, newtree)) || /* 14 and 15 */ |
| 2940 | (oldtree && newtree && |
| 2941 | !same(oldtree, newtree) && /* 18 and 19 */ |
| 2942 | same(current, newtree))) { |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 2943 | return keep_entry(current, o); |
Jonathan Nieder | 6c1db1b | 2014-08-12 17:00:45 -0700 | [diff] [blame] | 2944 | } else if (oldtree && !newtree && same(current, oldtree)) { |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2945 | /* 10 or 11 */ |
| 2946 | return deleted_entry(oldtree, current, o); |
Jonathan Nieder | 6c1db1b | 2014-08-12 17:00:45 -0700 | [diff] [blame] | 2947 | } else if (oldtree && newtree && |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2948 | same(current, oldtree) && !same(current, newtree)) { |
| 2949 | /* 20 or 21 */ |
| 2950 | return merged_entry(newtree, current, o); |
Derrick Stolee | e05cdb1 | 2021-07-20 20:14:41 +0000 | [diff] [blame] | 2951 | } else if (current && !oldtree && newtree && |
| 2952 | S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) && |
| 2953 | ce_stage(current) == 0) { |
| 2954 | /* |
| 2955 | * This case is a directory/file conflict across the sparse-index |
| 2956 | * boundary. When we are changing from one path to another via |
| 2957 | * 'git checkout', then we want to replace one entry with another |
| 2958 | * via merged_entry(). If there are staged changes, then we should |
| 2959 | * reject the merge instead. |
| 2960 | */ |
| 2961 | return merged_entry(newtree, current, o); |
Victoria Dye | ab81047 | 2022-03-01 20:24:30 +0000 | [diff] [blame] | 2962 | } else if (S_ISSPARSEDIR(current->ce_mode)) { |
| 2963 | /* |
| 2964 | * The sparse directories differ, but we don't know whether that's |
| 2965 | * because of two different files in the directory being modified |
| 2966 | * (can be trivially merged) or if there is a real file conflict. |
| 2967 | * Merge the sparse directory by OID to compare file-by-file. |
| 2968 | */ |
| 2969 | return merged_sparse_dir(src, 3, o); |
Jonathan Nieder | 6c1db1b | 2014-08-12 17:00:45 -0700 | [diff] [blame] | 2970 | } else |
Jonathan Nieder | 6a143aa | 2014-08-12 17:03:18 -0700 | [diff] [blame] | 2971 | return reject_merge(current, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2972 | } |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 2973 | else if (newtree) { |
| 2974 | if (oldtree && !o->initial_checkout) { |
| 2975 | /* |
| 2976 | * deletion of the path was staged; |
| 2977 | */ |
| 2978 | if (same(oldtree, newtree)) |
| 2979 | return 1; |
| 2980 | return reject_merge(oldtree, o); |
| 2981 | } |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2982 | return merged_entry(newtree, current, o); |
Junio C Hamano | 5521883 | 2008-09-07 19:49:25 -0700 | [diff] [blame] | 2983 | } |
Linus Torvalds | d699676 | 2007-08-10 12:31:20 -0700 | [diff] [blame] | 2984 | return deleted_entry(oldtree, current, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * Bind merge. |
| 2989 | * |
| 2990 | * Keep the index entries at stage0, collapse stage1 but make sure |
| 2991 | * stage0 does not have anything there. |
| 2992 | */ |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 2993 | int bind_merge(const struct cache_entry * const *src, |
| 2994 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2995 | { |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 2996 | const struct cache_entry *old = src[0]; |
| 2997 | const struct cache_entry *a = src[1]; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 2998 | |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 2999 | if (o->internal.merge_size != 1) |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 3000 | return error("Cannot do a bind merge of %d trees", |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 3001 | o->internal.merge_size); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3002 | if (a && old) |
Nguyễn Thái Ngọc Duy | b165fac | 2019-03-22 16:31:36 +0700 | [diff] [blame] | 3003 | return o->quiet ? -1 : |
Stefan Beller | 3d41542 | 2017-01-17 17:05:20 -0800 | [diff] [blame] | 3004 | error(ERRORMSG(o, ERROR_BIND_OVERLAP), |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 3005 | super_prefixed(a->name, o->super_prefix), |
| 3006 | super_prefixed(old->name, o->super_prefix)); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3007 | if (!a) |
Junio C Hamano | 7f7932a | 2007-04-02 00:06:12 -0700 | [diff] [blame] | 3008 | return keep_entry(old, o); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3009 | else |
| 3010 | return merged_entry(a, NULL, o); |
| 3011 | } |
| 3012 | |
| 3013 | /* |
| 3014 | * One-way merge. |
| 3015 | * |
| 3016 | * The rule is: |
| 3017 | * - take the stat information from stage0, take the data from stage1 |
| 3018 | */ |
René Scharfe | 5828e83 | 2013-06-02 17:46:56 +0200 | [diff] [blame] | 3019 | int oneway_merge(const struct cache_entry * const *src, |
| 3020 | struct unpack_trees_options *o) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3021 | { |
René Scharfe | eb9ae4b | 2013-06-02 17:46:55 +0200 | [diff] [blame] | 3022 | const struct cache_entry *old = src[0]; |
| 3023 | const struct cache_entry *a = src[1]; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3024 | |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 3025 | if (o->internal.merge_size != 1) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3026 | return error("Cannot do a oneway merge of %d trees", |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 3027 | o->internal.merge_size); |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3028 | |
Junio C Hamano | 78d3b06 | 2009-07-18 12:26:38 -0700 | [diff] [blame] | 3029 | if (!a || a == o->df_conflict_entry) |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3030 | return deleted_entry(old, old, o); |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 3031 | |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3032 | if (old && same(old, a)) { |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 3033 | int update = 0; |
Utsav Shah | 679f2f9 | 2019-11-20 08:32:17 +0000 | [diff] [blame] | 3034 | if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) && |
| 3035 | !(old->ce_flags & CE_FSMONITOR_VALID)) { |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3036 | struct stat st; |
| 3037 | if (lstat(old->name, &st) || |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 3038 | ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE)) |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 3039 | update |= CE_UPDATE; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3040 | } |
Stefan Beller | ad17312 | 2018-01-05 12:03:03 -0800 | [diff] [blame] | 3041 | if (o->update && S_ISGITLINK(old->ce_mode) && |
| 3042 | should_update_submodules() && !verify_uptodate(old, o)) |
| 3043 | update |= CE_UPDATE; |
Nguyễn Thái Ngọc Duy | ab5af82 | 2019-03-18 18:38:22 +0700 | [diff] [blame] | 3044 | add_entry(o, old, update, CE_STAGEMASK); |
Linus Torvalds | 34110cd | 2008-03-06 18:12:28 -0800 | [diff] [blame] | 3045 | return 0; |
Johannes Schindelin | 076b0ad | 2006-07-30 20:26:15 +0200 | [diff] [blame] | 3046 | } |
| 3047 | return merged_entry(a, old, o); |
| 3048 | } |
Denton Liu | d3c7bf7 | 2021-03-03 03:16:42 -0800 | [diff] [blame] | 3049 | |
| 3050 | /* |
| 3051 | * Merge worktree and untracked entries in a stash entry. |
| 3052 | * |
| 3053 | * Ignore all index entries. Collapse remaining trees but make sure that they |
| 3054 | * don't have any conflicting files. |
| 3055 | */ |
| 3056 | int stash_worktree_untracked_merge(const struct cache_entry * const *src, |
| 3057 | struct unpack_trees_options *o) |
| 3058 | { |
| 3059 | const struct cache_entry *worktree = src[1]; |
| 3060 | const struct cache_entry *untracked = src[2]; |
| 3061 | |
Elijah Newren | 1ca13dd | 2023-02-27 15:28:19 +0000 | [diff] [blame] | 3062 | if (o->internal.merge_size != 2) |
| 3063 | BUG("invalid merge_size: %d", o->internal.merge_size); |
Denton Liu | d3c7bf7 | 2021-03-03 03:16:42 -0800 | [diff] [blame] | 3064 | |
| 3065 | if (worktree && untracked) |
| 3066 | return error(_("worktree and untracked commit have duplicate entries: %s"), |
Ævar Arnfjörð Bjarmason | 4002ec3 | 2022-12-20 13:39:56 +0100 | [diff] [blame] | 3067 | super_prefixed(worktree->name, o->super_prefix)); |
Denton Liu | d3c7bf7 | 2021-03-03 03:16:42 -0800 | [diff] [blame] | 3068 | |
| 3069 | return merged_entry(worktree ? worktree : untracked, NULL, o); |
| 3070 | } |