blob: 360844bda3ab976c73e9443b318b58dfc400f475 [file] [log] [blame]
Johannes Schindelin16da1342006-07-30 20:25:18 +02001#include "cache.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04002#include "strvec.h"
Brandon Williams33028712017-08-03 11:19:59 -07003#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -08005#include "dir.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02006#include "tree.h"
7#include "tree-walk.h"
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02008#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02009#include "unpack-trees.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040010#include "progress.h"
Sven Verdoolaege0cf73752007-07-17 20:28:28 +020011#include "refs.h"
Junio C Hamano06f33c12009-03-13 21:24:08 -070012#include "attr.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070013#include "split-index.h"
Stefan Bellera7bc8452017-03-14 14:46:39 -070014#include "submodule.h"
15#include "submodule-config.h"
Ben Peart883e2482017-09-22 12:35:40 -040016#include "fsmonitor.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070017#include "object-store.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020018#include "promisor-remote.h"
Matheus Tavaresd052cc02021-03-23 11:19:32 -030019#include "entry.h"
Matheus Tavares04155bd2021-04-18 21:14:53 -030020#include "parallel-checkout.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020021
Junio C Hamano8ccba002008-05-17 12:03:49 -070022/*
23 * Error messages expected by scripts out of plumbing commands such as
24 * read-tree. Non-scripted Porcelain is not required to use these messages
25 * and in fact are encouraged to reword them to better suit their particular
Diane Gasselin23cbf112010-08-11 10:38:05 +020026 * situation better. See how "git checkout" and "git merge" replaces
Matthieu Moydc1166e2010-09-02 13:57:33 +020027 * them using setup_unpack_trees_porcelain(), for example.
Junio C Hamano8ccba002008-05-17 12:03:49 -070028 */
Elijah Newren6271d772020-03-27 00:48:57 +000029static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
Matthieu Moy08353eb2010-08-11 10:38:04 +020030 /* ERROR_WOULD_OVERWRITE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070031 "Entry '%s' would be overwritten by merge. Cannot merge.",
32
Matthieu Moy08353eb2010-08-11 10:38:04 +020033 /* ERROR_NOT_UPTODATE_FILE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070034 "Entry '%s' not uptodate. Cannot merge.",
35
Matthieu Moy08353eb2010-08-11 10:38:04 +020036 /* ERROR_NOT_UPTODATE_DIR */
Junio C Hamano8ccba002008-05-17 12:03:49 -070037 "Updating '%s' would lose untracked files in it",
38
Elijah Newrenb817e542021-12-09 05:08:27 +000039 /* ERROR_CWD_IN_THE_WAY */
40 "Refusing to remove '%s' since it is the current working directory.",
41
Matthieu Moy08402b02010-08-11 10:38:06 +020042 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
43 "Untracked working tree file '%s' would be overwritten by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070044
Matthieu Moy08402b02010-08-11 10:38:06 +020045 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
46 "Untracked working tree file '%s' would be removed by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070047
Matthieu Moy08353eb2010-08-11 10:38:04 +020048 /* ERROR_BIND_OVERLAP */
Junio C Hamano8ccba002008-05-17 12:03:49 -070049 "Entry '%s' overlaps with '%s'. Cannot bind.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070050
Stefan Bellera7bc8452017-03-14 14:46:39 -070051 /* ERROR_WOULD_LOSE_SUBMODULE */
52 "Submodule '%s' cannot checkout new HEAD.",
Elijah Newrencd002c12020-03-27 00:48:55 +000053
Elijah Newren6271d772020-03-27 00:48:57 +000054 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
55 "",
56
Elijah Newren1ac83f42020-03-27 00:48:56 +000057 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
Elijah Newren22ab0b32020-03-27 00:48:58 +000058 "Path '%s' not uptodate; will not remove from working tree.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070059
Elijah Newrenebb568b2020-03-27 00:48:59 +000060 /* WARNING_SPARSE_UNMERGED_FILE */
61 "Path '%s' unmerged; will not remove from working tree.",
62
Elijah Newren1ac83f42020-03-27 00:48:56 +000063 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
Elijah Newren22ab0b32020-03-27 00:48:58 +000064 "Path '%s' already present; will not overwrite with sparse update.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070065};
66
Matthieu Moy08353eb2010-08-11 10:38:04 +020067#define ERRORMSG(o,type) \
68 ( ((o) && (o)->msgs[(type)]) \
69 ? ((o)->msgs[(type)]) \
70 : (unpack_plumbing_errors[(type)]) )
Junio C Hamano8ccba002008-05-17 12:03:49 -070071
Stefan Beller3d415422017-01-17 17:05:20 -080072static const char *super_prefixed(const char *path)
73{
74 /*
75 * It is necessary and sufficient to have two static buffers
76 * here, as the return value of this function is fed to
77 * error() using the unpack_*_errors[] templates we see above.
78 */
79 static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};
80 static int super_prefix_len = -1;
81 static unsigned idx = ARRAY_SIZE(buf) - 1;
82
83 if (super_prefix_len < 0) {
84 const char *super_prefix = get_super_prefix();
85 if (!super_prefix) {
86 super_prefix_len = 0;
87 } else {
88 int i;
89 for (i = 0; i < ARRAY_SIZE(buf); i++)
90 strbuf_addstr(&buf[i], super_prefix);
91 super_prefix_len = buf[0].len;
92 }
93 }
94
95 if (!super_prefix_len)
96 return path;
97
98 if (++idx >= ARRAY_SIZE(buf))
99 idx = 0;
100
101 strbuf_setlen(&buf[idx], super_prefix_len);
102 strbuf_addstr(&buf[idx], path);
103
104 return buf[idx].buf;
105}
106
Matthieu Moye2940302010-09-02 13:57:34 +0200107void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
108 const char *cmd)
Matthieu Moydc1166e2010-09-02 13:57:33 +0200109{
Clemens Buchacher79808722010-11-15 20:52:19 +0100110 int i;
Matthieu Moye2940302010-09-02 13:57:34 +0200111 const char **msgs = opts->msgs;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200112 const char *msg;
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400113
Jeff Kingc972bf42020-07-28 16:25:12 -0400114 strvec_init(&opts->msgs_to_free);
Martin Ågren1c41d282018-05-21 16:54:28 +0200115
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000116 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200117 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000118 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600119 "Please commit your changes or stash them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000120 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
121 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200122 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000123 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600124 "Please commit your changes or stash them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000125 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200126 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200127 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000128 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600129 "Please commit your changes or stash them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000130 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400131 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
Jeff Kingc972bf42020-07-28 16:25:12 -0400132 strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200133
134 msgs[ERROR_NOT_UPTODATE_DIR] =
Stefan Beller584f99c2016-12-02 11:17:41 -0800135 _("Updating the following directories would lose untracked files in them:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200136
Elijah Newrenb817e542021-12-09 05:08:27 +0000137 msgs[ERROR_CWD_IN_THE_WAY] =
138 _("Refusing to remove the current working directory:\n%s");
139
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000140 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200141 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000142 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600143 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000144 : _("The following untracked working tree files would be removed by checkout:\n%%s");
145 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200146 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000147 ? _("The following untracked working tree files would be removed by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600148 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000149 : _("The following untracked working tree files would be removed by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200150 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200151 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000152 ? _("The following untracked working tree files would be removed by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600153 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000154 : _("The following untracked working tree files would be removed by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200155 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =
Jeff Kingc972bf42020-07-28 16:25:12 -0400156 strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400157
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000158 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200159 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000160 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600161 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000162 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
163 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200164 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000165 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600166 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000167 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
168 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200169 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000170 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600171 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000172 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200173 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =
Jeff Kingc972bf42020-07-28 16:25:12 -0400174 strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200175
176 /*
177 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
178 * cannot easily display it as a list.
179 */
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000180 msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200181
Stefan Bellera7bc8452017-03-14 14:46:39 -0700182 msgs[ERROR_WOULD_LOSE_SUBMODULE] =
Stefan Beller6362ed02017-03-29 15:34:24 -0700183 _("Cannot update submodule:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200184
Elijah Newren1ac83f42020-03-27 00:48:56 +0000185 msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000186 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
Elijah Newrenebb568b2020-03-27 00:48:59 +0000187 msgs[WARNING_SPARSE_UNMERGED_FILE] =
188 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
Elijah Newren1ac83f42020-03-27 00:48:56 +0000189 msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000190 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200191
192 opts->show_all_errors = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100193 /* rejected paths may not have a static buffer */
194 for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
195 opts->unpack_rejects[i].strdup_strings = 1;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200196}
197
Martin Ågren1c41d282018-05-21 16:54:28 +0200198void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
199{
Jeff Kingc972bf42020-07-28 16:25:12 -0400200 strvec_clear(&opts->msgs_to_free);
Martin Ågren1c41d282018-05-21 16:54:28 +0200201 memset(opts->msgs, 0, sizeof(opts->msgs));
202}
203
Jeff King46169182014-11-24 13:36:51 -0500204static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
René Scharfe6ff264e2012-03-06 20:37:23 +0100205 unsigned int set, unsigned int clear)
206{
Karsten Blees419a5972013-11-14 20:22:27 +0100207 clear |= CE_HASHED;
René Scharfe6ff264e2012-03-06 20:37:23 +0100208
209 if (set & CE_REMOVE)
210 set |= CE_WT_REMOVE;
211
René Scharfe6ff264e2012-03-06 20:37:23 +0100212 ce->ce_flags = (ce->ce_flags & ~clear) | set;
Jeff King46169182014-11-24 13:36:51 -0500213 return add_index_entry(&o->result, ce,
214 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
René Scharfe6ff264e2012-03-06 20:37:23 +0100215}
216
René Scharfea33bd4d2013-06-02 17:46:53 +0200217static void add_entry(struct unpack_trees_options *o,
218 const struct cache_entry *ce,
219 unsigned int set, unsigned int clear)
220{
Jameson Miller8e72d672018-07-02 19:49:37 +0000221 do_add_entry(o, dup_cache_entry(ce, &o->result), set, clear);
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700222}
223
Kjetil Barvik78478922009-02-09 21:54:07 +0100224/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200225 * add error messages on path <path>
226 * corresponding to the type <e> with the message <msg>
227 * indicating if it should be display in porcelain or not
228 */
229static int add_rejected_path(struct unpack_trees_options *o,
230 enum unpack_trees_error_types e,
231 const char *path)
232{
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +0700233 if (o->quiet)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700234 return -1;
235
Matthieu Moye6c111b2010-08-11 10:38:07 +0200236 if (!o->show_all_errors)
Stefan Beller3d415422017-01-17 17:05:20 -0800237 return error(ERRORMSG(o, e), super_prefixed(path));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200238
239 /*
240 * Otherwise, insert in a list for future display by
Elijah Newren6271d772020-03-27 00:48:57 +0000241 * display_(error|warning)_msgs()
Matthieu Moye6c111b2010-08-11 10:38:07 +0200242 */
Clemens Buchacher79808722010-11-15 20:52:19 +0100243 string_list_append(&o->unpack_rejects[e], path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200244 return -1;
245}
246
247/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200248 * display all the error messages stored in a nice way
249 */
250static void display_error_msgs(struct unpack_trees_options *o)
251{
Elijah Newren6271d772020-03-27 00:48:57 +0000252 int e;
253 unsigned error_displayed = 0;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200254 for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
Clemens Buchacher79808722010-11-15 20:52:19 +0100255 struct string_list *rejects = &o->unpack_rejects[e];
Elijah Newren6271d772020-03-27 00:48:57 +0000256
Clemens Buchacher79808722010-11-15 20:52:19 +0100257 if (rejects->nr > 0) {
Elijah Newren6271d772020-03-27 00:48:57 +0000258 int i;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200259 struct strbuf path = STRBUF_INIT;
Elijah Newren6271d772020-03-27 00:48:57 +0000260
261 error_displayed = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100262 for (i = 0; i < rejects->nr; i++)
263 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Stefan Beller3d415422017-01-17 17:05:20 -0800264 error(ERRORMSG(o, e), super_prefixed(path.buf));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200265 strbuf_release(&path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200266 }
Clemens Buchacher79808722010-11-15 20:52:19 +0100267 string_list_clear(rejects, 0);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200268 }
Elijah Newren6271d772020-03-27 00:48:57 +0000269 if (error_displayed)
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000270 fprintf(stderr, _("Aborting\n"));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200271}
272
Elijah Newren6271d772020-03-27 00:48:57 +0000273/*
274 * display all the warning messages stored in a nice way
275 */
276static void display_warning_msgs(struct unpack_trees_options *o)
277{
278 int e;
279 unsigned warning_displayed = 0;
280 for (e = NB_UNPACK_TREES_ERROR_TYPES + 1;
281 e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
282 struct string_list *rejects = &o->unpack_rejects[e];
283
284 if (rejects->nr > 0) {
285 int i;
286 struct strbuf path = STRBUF_INIT;
287
288 warning_displayed = 1;
289 for (i = 0; i < rejects->nr; i++)
290 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
291 warning(ERRORMSG(o, e), super_prefixed(path.buf));
292 strbuf_release(&path);
293 }
294 string_list_clear(rejects, 0);
295 }
296 if (warning_displayed)
297 fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
298}
Stefan Bellera7bc8452017-03-14 14:46:39 -0700299static int check_submodule_move_head(const struct cache_entry *ce,
300 const char *old_id,
301 const char *new_id,
302 struct unpack_trees_options *o)
303{
Stefan Bellercd279e22017-04-18 14:37:22 -0700304 unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700305 const struct submodule *sub = submodule_from_ce(ce);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700306
Stefan Bellera7bc8452017-03-14 14:46:39 -0700307 if (!sub)
308 return 0;
309
Stefan Bellercd279e22017-04-18 14:37:22 -0700310 if (o->reset)
311 flags |= SUBMODULE_MOVE_HEAD_FORCE;
312
Brandon Williams7463e2e2017-08-03 11:19:53 -0700313 if (submodule_move_head(ce->name, old_id, new_id, flags))
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700314 return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700315 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700316}
317
Brandon Williams33028712017-08-03 11:19:59 -0700318/*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000319 * Perform the loading of the repository's gitmodules file. This function is
Brandon Williams33028712017-08-03 11:19:59 -0700320 * used by 'check_update()' to perform loading of the gitmodules file in two
Elijah Newren15beaaa2019-11-05 17:07:23 +0000321 * different situations:
Brandon Williams33028712017-08-03 11:19:59 -0700322 * (1) before removing entries from the working tree if the gitmodules file has
323 * been marked for removal. This situation is specified by 'state' == NULL.
324 * (2) before checking out entries to the working tree if the gitmodules file
325 * has been marked for update. This situation is specified by 'state' != NULL.
326 */
327static void load_gitmodules_file(struct index_state *index,
328 struct checkout *state)
Stefan Bellera7bc8452017-03-14 14:46:39 -0700329{
Brandon Williams33028712017-08-03 11:19:59 -0700330 int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
331
332 if (pos >= 0) {
333 struct cache_entry *ce = index->cache[pos];
334 if (!state && ce->ce_flags & CE_WT_REMOVE) {
Matheus Tavaresd7992422020-01-15 23:39:55 -0300335 repo_read_gitmodules(the_repository, 0);
Brandon Williams33028712017-08-03 11:19:59 -0700336 } else if (state && (ce->ce_flags & CE_UPDATE)) {
Stefan Bellerf793b892018-03-28 15:35:28 -0700337 submodule_free(the_repository);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100338 checkout_entry(ce, state, NULL, NULL);
Matheus Tavaresd7992422020-01-15 23:39:55 -0300339 repo_read_gitmodules(the_repository, 0);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700340 }
341 }
342}
343
Elijah Newren6c342392020-05-14 19:53:22 +0000344static struct progress *get_progress(struct unpack_trees_options *o,
345 struct index_state *index)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200346{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400347 unsigned cnt = 0, total = 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200348
Stefan Beller384f1a12017-01-09 11:46:19 -0800349 if (!o->update || !o->verbose_update)
350 return NULL;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200351
Stefan Beller384f1a12017-01-09 11:46:19 -0800352 for (; cnt < index->cache_nr; cnt++) {
353 const struct cache_entry *ce = index->cache[cnt];
354 if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
355 total++;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200356 }
357
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700358 return start_delayed_progress(_("Updating files"), total);
Stefan Beller384f1a12017-01-09 11:46:19 -0800359}
360
Duy Nguyenb8785792018-08-17 20:00:39 +0200361static void setup_collided_checkout_detection(struct checkout *state,
362 struct index_state *index)
363{
364 int i;
365
366 state->clone = 1;
367 for (i = 0; i < index->cache_nr; i++)
368 index->cache[i]->ce_flags &= ~CE_MATCHED;
369}
370
371static void report_collided_checkout(struct index_state *index)
372{
373 struct string_list list = STRING_LIST_INIT_NODUP;
374 int i;
375
376 for (i = 0; i < index->cache_nr; i++) {
377 struct cache_entry *ce = index->cache[i];
378
379 if (!(ce->ce_flags & CE_MATCHED))
380 continue;
381
382 string_list_append(&list, ce->name);
383 ce->ce_flags &= ~CE_MATCHED;
384 }
385
386 list.cmp = fspathcmp;
387 string_list_sort(&list);
388
389 if (list.nr) {
390 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
391 "on a case-insensitive filesystem) and only one from the same\n"
392 "colliding group is in the working tree:\n"));
393
394 for (i = 0; i < list.nr; i++)
395 fprintf(stderr, " '%s'\n", list.items[i].string);
396 }
397
398 string_list_clear(&list, 0);
399}
400
Jonathan Tanb2896d22021-07-23 11:52:22 -0700401static int must_checkout(const struct cache_entry *ce)
402{
403 return ce->ce_flags & CE_UPDATE;
404}
405
Elijah Newrenb0a5a122020-03-27 00:48:49 +0000406static int check_updates(struct unpack_trees_options *o,
407 struct index_state *index)
Stefan Beller384f1a12017-01-09 11:46:19 -0800408{
409 unsigned cnt = 0;
Stefan Beller30ac2752017-01-09 11:46:17 -0800410 int errs = 0;
Carlo Marcelo Arenas Belón07f967a2018-10-18 11:46:04 -0700411 struct progress *progress;
Stefan Beller30ac2752017-01-09 11:46:17 -0800412 struct checkout state = CHECKOUT_INIT;
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300413 int i, pc_workers, pc_threshold;
Stefan Beller30ac2752017-01-09 11:46:17 -0800414
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200415 trace_performance_enter();
Stefan Beller30ac2752017-01-09 11:46:17 -0800416 state.force = 1;
417 state.quiet = 1;
418 state.refresh_cache = 1;
419 state.istate = index;
brian m. carlson13e7ed62020-03-16 18:05:04 +0000420 clone_checkout_metadata(&state.meta, &o->meta, NULL);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200421
Elijah Newren26f924d2020-01-07 06:57:57 +0000422 if (!o->update || o->dry_run) {
423 remove_marked_cache_entries(index, 0);
424 trace_performance_leave("check_updates");
425 return 0;
426 }
427
Duy Nguyenb8785792018-08-17 20:00:39 +0200428 if (o->clone)
429 setup_collided_checkout_detection(&state, index);
430
Elijah Newren6c342392020-05-14 19:53:22 +0000431 progress = get_progress(o, index);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200432
Matheus Tavares22539ec2021-02-02 22:37:10 +0100433 /* Start with clean cache to avoid using any possibly outdated info. */
434 invalidate_lstat_cache();
435
Elijah Newren26f924d2020-01-07 06:57:57 +0000436 git_attr_set_direction(GIT_ATTR_CHECKOUT);
Brandon Williams33028712017-08-03 11:19:59 -0700437
Elijah Newren26f924d2020-01-07 06:57:57 +0000438 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700439 load_gitmodules_file(index, NULL);
440
Linus Torvalds34110cd2008-03-06 18:12:28 -0800441 for (i = 0; i < index->cache_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700442 const struct cache_entry *ce = index->cache[i];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200443
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700444 if (ce->ce_flags & CE_WT_REMOVE) {
445 display_progress(progress, ++cnt);
Elijah Newren26f924d2020-01-07 06:57:57 +0000446 unlink_entry(ce);
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700447 }
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700448 }
Elijah Newren26f924d2020-01-07 06:57:57 +0000449
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000450 remove_marked_cache_entries(index, 0);
Kjetil Barvik78478922009-02-09 21:54:07 +0100451 remove_scheduled_dirs();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700452
Elijah Newren26f924d2020-01-07 06:57:57 +0000453 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700454 load_gitmodules_file(index, &state);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700455
Jonathan Tanb2896d22021-07-23 11:52:22 -0700456 if (has_promisor_remote())
Jonathan Tanc0c578b2017-12-08 15:58:47 +0000457 /*
458 * Prefetch the objects that are to be checked out in the loop
459 * below.
460 */
Jonathan Tanb2896d22021-07-23 11:52:22 -0700461 prefetch_cache_entries(index, must_checkout);
Matheus Tavares04155bd2021-04-18 21:14:53 -0300462
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300463 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
464
Matheus Tavares04155bd2021-04-18 21:14:53 -0300465 enable_delayed_checkout(&state);
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300466 if (pc_workers > 1)
467 init_parallel_checkout();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700468 for (i = 0; i < index->cache_nr; i++) {
469 struct cache_entry *ce = index->cache[i];
470
Jonathan Tanb2896d22021-07-23 11:52:22 -0700471 if (must_checkout(ce)) {
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300472 size_t last_pc_queue_size = pc_queue_size();
473
David Turner7d782412015-07-17 17:19:27 -0400474 if (ce->ce_flags & CE_WT_REMOVE)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200475 BUG("both update and delete flags are set on %s",
David Turner7d782412015-07-17 17:19:27 -0400476 ce->name);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800477 ce->ce_flags &= ~CE_UPDATE;
Elijah Newren26f924d2020-01-07 06:57:57 +0000478 errs |= checkout_entry(ce, &state, NULL, NULL);
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300479
480 if (last_pc_queue_size == pc_queue_size())
481 display_progress(progress, ++cnt);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200482 }
483 }
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300484 if (pc_workers > 1)
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300485 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
486 progress, &cnt);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400487 stop_progress(&progress);
Matheus Tavares7a132c62021-08-26 16:10:06 -0300488 errs |= finish_delayed_checkout(&state, NULL, o->verbose_update);
Elijah Newren26f924d2020-01-07 06:57:57 +0000489 git_attr_set_direction(GIT_ATTR_CHECKIN);
Duy Nguyenb8785792018-08-17 20:00:39 +0200490
491 if (o->clone)
492 report_collided_checkout(index);
493
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200494 trace_performance_leave("check_updates");
Junio C Hamanoc4758d32008-03-18 22:01:28 -0700495 return errs != 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200496}
497
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200498static int verify_uptodate_sparse(const struct cache_entry *ce,
499 struct unpack_trees_options *o);
500static int verify_absent_sparse(const struct cache_entry *ce,
501 enum unpack_trees_error_types,
502 struct unpack_trees_options *o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700503
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700504static int apply_sparse_checkout(struct index_state *istate,
505 struct cache_entry *ce,
506 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700507{
508 int was_skip_worktree = ce_skip_worktree(ce);
509
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700510 if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700511 ce->ce_flags |= CE_SKIP_WORKTREE;
512 else
513 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700514 if (was_skip_worktree != ce_skip_worktree(ce)) {
515 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400516 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700517 istate->cache_changed |= CE_ENTRY_CHANGED;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700518 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700519
520 /*
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700521 * if (!was_skip_worktree && !ce_skip_worktree()) {
522 * This is perfectly normal. Move on;
523 * }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700524 */
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700525
526 /*
527 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
528 * area as a result of ce_skip_worktree() shortcuts in
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700529 * verify_absent() and verify_uptodate().
530 * Make sure they don't modify worktree if they are already
531 * outside checkout area
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700532 */
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700533 if (was_skip_worktree && ce_skip_worktree(ce)) {
534 ce->ce_flags &= ~CE_UPDATE;
535
536 /*
537 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
538 * on to get that file removed from both index and worktree.
539 * If that file is already outside worktree area, don't
540 * bother remove it.
541 */
542 if (ce->ce_flags & CE_REMOVE)
543 ce->ce_flags &= ~CE_WT_REMOVE;
544 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700545
546 if (!was_skip_worktree && ce_skip_worktree(ce)) {
547 /*
548 * If CE_UPDATE is set, verify_uptodate() must be called already
549 * also stat info may have lost after merged_entry() so calling
550 * verify_uptodate() again may fail
551 */
Elijah Newren3cc7c502020-03-27 00:48:50 +0000552 if (!(ce->ce_flags & CE_UPDATE) &&
553 verify_uptodate_sparse(ce, o)) {
554 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700555 return -1;
Elijah Newren3cc7c502020-03-27 00:48:50 +0000556 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700557 ce->ce_flags |= CE_WT_REMOVE;
David Turner7d782412015-07-17 17:19:27 -0400558 ce->ce_flags &= ~CE_UPDATE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700559 }
560 if (was_skip_worktree && !ce_skip_worktree(ce)) {
Elijah Newren1ac83f42020-03-27 00:48:56 +0000561 if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700562 return -1;
563 ce->ce_flags |= CE_UPDATE;
564 }
565 return 0;
566}
567
Elijah Newrenebb568b2020-03-27 00:48:59 +0000568static int warn_conflicted_path(struct index_state *istate,
569 int i,
570 struct unpack_trees_options *o)
571{
572 char *conflicting_path = istate->cache[i]->name;
573 int count = 0;
574
575 add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path);
576
Derrick Stolee0eeb3be2020-05-07 13:17:33 +0000577 /* Find out how many higher stage entries are at same path */
578 while ((++count) + i < istate->cache_nr &&
579 !strcmp(conflicting_path, istate->cache[count + i]->name))
580 ; /* do nothing */
581
Elijah Newrenebb568b2020-03-27 00:48:59 +0000582 return count;
583}
584
René Scharfe5828e832013-06-02 17:46:56 +0200585static inline int call_unpack_fn(const struct cache_entry * const *src,
586 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800587{
Linus Torvalds34110cd2008-03-06 18:12:28 -0800588 int ret = o->fn(src, o);
589 if (ret > 0)
Linus Torvalds01904572008-03-05 20:15:44 -0800590 ret = 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800591 return ret;
592}
593
Junio C Hamanoda165f42010-01-07 14:59:54 -0800594static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
595{
596 ce->ce_flags |= CE_UNPACKED;
597
Derrick Stolee17a1bb52021-07-14 13:12:30 +0000598 /*
599 * If this is a sparse directory, don't advance cache_bottom.
600 * That will be advanced later using the cache-tree data.
601 */
602 if (S_ISSPARSEDIR(ce->ce_mode))
603 return;
604
Junio C Hamanoda165f42010-01-07 14:59:54 -0800605 if (o->cache_bottom < o->src_index->cache_nr &&
606 o->src_index->cache[o->cache_bottom] == ce) {
607 int bottom = o->cache_bottom;
608 while (bottom < o->src_index->cache_nr &&
609 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
610 bottom++;
611 o->cache_bottom = bottom;
612 }
613}
614
615static void mark_all_ce_unused(struct index_state *index)
616{
617 int i;
618 for (i = 0; i < index->cache_nr; i++)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700619 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
Junio C Hamanoda165f42010-01-07 14:59:54 -0800620}
621
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200622static int locate_in_src_index(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800623 struct unpack_trees_options *o)
624{
625 struct index_state *index = o->src_index;
626 int len = ce_namelen(ce);
627 int pos = index_name_pos(index, ce->name, len);
628 if (pos < 0)
629 pos = -1 - pos;
630 return pos;
631}
632
633/*
634 * We call unpack_index_entry() with an unmerged cache entry
635 * only in diff-index, and it wants a single callback. Skip
636 * the other unmerged entry with the same name.
637 */
638static void mark_ce_used_same_name(struct cache_entry *ce,
639 struct unpack_trees_options *o)
640{
641 struct index_state *index = o->src_index;
642 int len = ce_namelen(ce);
643 int pos;
644
645 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
646 struct cache_entry *next = index->cache[pos];
647 if (len != ce_namelen(next) ||
648 memcmp(ce->name, next->name, len))
649 break;
650 mark_ce_used(next, o);
651 }
652}
653
Victoria Dyef2a454e2021-11-29 15:52:43 +0000654static struct cache_entry *next_cache_entry(struct unpack_trees_options *o, int *hint)
Junio C Hamanoda165f42010-01-07 14:59:54 -0800655{
656 const struct index_state *index = o->src_index;
657 int pos = o->cache_bottom;
658
Victoria Dyef2a454e2021-11-29 15:52:43 +0000659 if (*hint > pos)
660 pos = *hint;
661
Junio C Hamanoda165f42010-01-07 14:59:54 -0800662 while (pos < index->cache_nr) {
663 struct cache_entry *ce = index->cache[pos];
Victoria Dyef2a454e2021-11-29 15:52:43 +0000664 if (!(ce->ce_flags & CE_UNPACKED)) {
665 *hint = pos + 1;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800666 return ce;
Victoria Dyef2a454e2021-11-29 15:52:43 +0000667 }
Junio C Hamanoda165f42010-01-07 14:59:54 -0800668 pos++;
669 }
Victoria Dyef2a454e2021-11-29 15:52:43 +0000670
671 *hint = pos;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800672 return NULL;
673}
674
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700675static void add_same_unmerged(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800676 struct unpack_trees_options *o)
677{
678 struct index_state *index = o->src_index;
679 int len = ce_namelen(ce);
680 int pos = index_name_pos(index, ce->name, len);
681
682 if (0 <= pos)
683 die("programming error in a caller of mark_ce_used_same_name");
684 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
685 struct cache_entry *next = index->cache[pos];
686 if (len != ce_namelen(next) ||
687 memcmp(ce->name, next->name, len))
688 break;
689 add_entry(o, next, 0, 0);
690 mark_ce_used(next, o);
691 }
692}
693
694static int unpack_index_entry(struct cache_entry *ce,
695 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800696{
René Scharfe5828e832013-06-02 17:46:56 +0200697 const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Junio C Hamanoda165f42010-01-07 14:59:54 -0800698 int ret;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800699
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000700 src[0] = ce;
701
Junio C Hamanoda165f42010-01-07 14:59:54 -0800702 mark_ce_used(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -0800703 if (ce_stage(ce)) {
704 if (o->skip_unmerged) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800705 add_entry(o, ce, 0, 0);
706 return 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800707 }
Linus Torvalds01904572008-03-05 20:15:44 -0800708 }
Junio C Hamanoda165f42010-01-07 14:59:54 -0800709 ret = call_unpack_fn(src, o);
710 if (ce_stage(ce))
711 mark_ce_used_same_name(ce, o);
712 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800713}
714
Jeff King90553842019-07-31 00:38:15 -0400715static int find_cache_pos(struct traverse_info *, const char *p, size_t len);
Junio C Hamano730f7282009-09-20 00:03:39 -0700716
717static void restore_cache_bottom(struct traverse_info *info, int bottom)
718{
719 struct unpack_trees_options *o = info->data;
720
721 if (o->diff_index_cached)
722 return;
723 o->cache_bottom = bottom;
724}
725
726static int switch_cache_bottom(struct traverse_info *info)
727{
728 struct unpack_trees_options *o = info->data;
729 int ret, pos;
730
731 if (o->diff_index_cached)
732 return 0;
733 ret = o->cache_bottom;
Jeff King90553842019-07-31 00:38:15 -0400734 pos = find_cache_pos(info->prev, info->name, info->namelen);
Junio C Hamano730f7282009-09-20 00:03:39 -0700735
736 if (pos < -1)
737 o->cache_bottom = -2 - pos;
738 else if (pos < 0)
739 o->cache_bottom = o->src_index->cache_nr;
740 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800741}
742
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000743static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)
744{
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000745 return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid);
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000746}
747
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200748static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,
749 struct name_entry *names,
750 struct traverse_info *info)
751{
752 struct unpack_trees_options *o = info->data;
753 int i;
754
755 if (!o->merge || dirmask != ((1 << n) - 1))
756 return 0;
757
758 for (i = 1; i < n; i++)
759 if (!are_same_oid(names, names + i))
760 return 0;
761
762 return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);
763}
764
765static int index_pos_by_traverse_info(struct name_entry *names,
766 struct traverse_info *info)
767{
768 struct unpack_trees_options *o = info->data;
Jeff Kingc43ab062019-07-31 00:38:23 -0400769 struct strbuf name = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200770 int pos;
771
Jeff Kingc43ab062019-07-31 00:38:23 -0400772 strbuf_make_traverse_path(&name, info, names->path, names->pathlen);
773 strbuf_addch(&name, '/');
774 pos = index_name_pos(o->src_index, name.buf, name.len);
Derrick Stolee13e13312021-03-30 13:10:57 +0000775 if (pos >= 0) {
776 if (!o->src_index->sparse_index ||
777 !(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE))
778 BUG("This is a directory and should not exist in index");
779 } else {
780 pos = -pos - 1;
781 }
Emily Shaffer573117d2020-01-07 18:31:27 -0800782 if (pos >= o->src_index->cache_nr ||
783 !starts_with(o->src_index->cache[pos]->name, name.buf) ||
Jeff Kingc43ab062019-07-31 00:38:23 -0400784 (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf)))
Emily Shaffer573117d2020-01-07 18:31:27 -0800785 BUG("pos %d doesn't point to the first entry of %s in index",
786 pos, name.buf);
Jeff Kingc43ab062019-07-31 00:38:23 -0400787 strbuf_release(&name);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200788 return pos;
789}
790
791/*
792 * Fast path if we detect that all trees are the same as cache-tree at this
Nguyễn Thái Ngọc Duy5f4436a2018-08-25 15:02:09 +0200793 * path. We'll walk these trees in an iterative loop using cache-tree/index
794 * instead of ODB since we already know what these trees contain.
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200795 */
796static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200797 struct traverse_info *info)
798{
799 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
800 struct unpack_trees_options *o = info->data;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200801 struct cache_entry *tree_ce = NULL;
802 int ce_len = 0;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200803 int i, d;
804
805 if (!o->merge)
806 BUG("We need cache-tree to do this optimization");
807
808 /*
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +0000809 * Do what unpack_callback() and unpack_single_entry() normally
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200810 * do. But we walk all paths in an iterative loop instead.
811 *
812 * D/F conflicts and higher stage entries are not a concern
813 * because cache-tree would be invalidated and we would never
814 * get here in the first place.
815 */
816 for (i = 0; i < nr_entries; i++) {
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200817 int new_ce_len, len, rc;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200818
819 src[0] = o->src_index->cache[pos + i];
820
821 len = ce_namelen(src[0]);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200822 new_ce_len = cache_entry_size(len);
823
824 if (new_ce_len > ce_len) {
825 new_ce_len <<= 1;
826 tree_ce = xrealloc(tree_ce, new_ce_len);
827 memset(tree_ce, 0, new_ce_len);
828 ce_len = new_ce_len;
829
830 tree_ce->ce_flags = create_ce_flags(0);
831
832 for (d = 1; d <= nr_names; d++)
833 src[d] = tree_ce;
834 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200835
836 tree_ce->ce_mode = src[0]->ce_mode;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200837 tree_ce->ce_namelen = len;
838 oidcpy(&tree_ce->oid, &src[0]->oid);
839 memcpy(tree_ce->name, src[0]->name, len + 1);
840
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200841 rc = call_unpack_fn((const struct cache_entry * const *)src, o);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200842 if (rc < 0) {
843 free(tree_ce);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200844 return rc;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200845 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200846
847 mark_ce_used(src[0], o);
848 }
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200849 free(tree_ce);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200850 if (o->debug_unpack)
851 printf("Unpacked %d entries from %s to %s using cache-tree\n",
852 nr_entries,
853 o->src_index->cache[pos]->name,
854 o->src_index->cache[pos + nr_entries - 1]->name);
855 return 0;
856}
857
Junio C Hamano84563a62010-12-22 09:09:55 -0800858static int traverse_trees_recursive(int n, unsigned long dirmask,
859 unsigned long df_conflicts,
860 struct name_entry *names,
861 struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -0800862{
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100863 struct unpack_trees_options *o = info->data;
Junio C Hamano730f7282009-09-20 00:03:39 -0700864 int i, ret, bottom;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000865 int nr_buf = 0;
Junio C Hamanoca885a42008-03-13 22:07:18 -0700866 struct tree_desc t[MAX_UNPACK_TREES];
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500867 void *buf[MAX_UNPACK_TREES];
Linus Torvalds01904572008-03-05 20:15:44 -0800868 struct traverse_info newinfo;
869 struct name_entry *p;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200870 int nr_entries;
871
872 nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);
873 if (nr_entries > 0) {
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200874 int pos = index_pos_by_traverse_info(names, info);
875
876 if (!o->merge || df_conflicts)
877 BUG("Wrong condition to get here buddy");
878
879 /*
880 * All entries up to 'pos' must have been processed
881 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
882 * save and restore cache_bottom anyway to not miss
883 * unprocessed entries before 'pos'.
884 */
885 bottom = o->cache_bottom;
Jeff King66429692019-03-20 04:15:07 -0400886 ret = traverse_by_cache_tree(pos, nr_entries, n, info);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200887 o->cache_bottom = bottom;
888 return ret;
889 }
Linus Torvalds01904572008-03-05 20:15:44 -0800890
891 p = names;
892 while (!p->mode)
893 p++;
894
895 newinfo = *info;
896 newinfo.prev = info;
Junio C Hamano40e37252011-08-29 12:31:06 -0700897 newinfo.pathspec = info->pathspec;
Jeff King90553842019-07-31 00:38:15 -0400898 newinfo.name = p->path;
899 newinfo.namelen = p->pathlen;
900 newinfo.mode = p->mode;
Jeff King37806082019-07-31 00:38:18 -0400901 newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1);
René Scharfe603d2492013-06-16 01:44:43 +0200902 newinfo.df_conflicts |= df_conflicts;
Linus Torvalds01904572008-03-05 20:15:44 -0800903
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000904 /*
905 * Fetch the tree from the ODB for each peer directory in the
906 * n commits.
907 *
908 * For 2- and 3-way traversals, we try to avoid hitting the
909 * ODB twice for the same OID. This should yield a nice speed
910 * up in checkouts and merges when the commits are similar.
911 *
912 * We don't bother doing the full O(n^2) search for larger n,
913 * because wider traversals don't happen that often and we
914 * avoid the search setup.
915 *
916 * When 2 peer OIDs are the same, we just copy the tree
917 * descriptor data. This implicitly borrows the buffer
918 * data from the earlier cell.
919 */
Linus Torvalds01904572008-03-05 20:15:44 -0800920 for (i = 0; i < n; i++, dirmask >>= 1) {
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000921 if (i > 0 && are_same_oid(&names[i], &names[i - 1]))
922 t[i] = t[i - 1];
923 else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
924 t[i] = t[i - 2];
925 else {
René Scharfe5c377d32017-08-12 10:32:59 +0200926 const struct object_id *oid = NULL;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000927 if (dirmask & 1)
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000928 oid = &names[i].oid;
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700929 buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid);
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000930 }
Linus Torvalds01904572008-03-05 20:15:44 -0800931 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700932
933 bottom = switch_cache_bottom(&newinfo);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100934 ret = traverse_trees(o->src_index, n, t, &newinfo);
Junio C Hamano730f7282009-09-20 00:03:39 -0700935 restore_cache_bottom(&newinfo, bottom);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500936
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000937 for (i = 0; i < nr_buf; i++)
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500938 free(buf[i]);
939
Junio C Hamano730f7282009-09-20 00:03:39 -0700940 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800941}
942
943/*
944 * Compare the traverse-path to the cache entry without actually
945 * having to generate the textual representation of the traverse
946 * path.
947 *
948 * NOTE! This *only* compares up to the size of the traverse path
949 * itself - the caller needs to do the final check for the cache
950 * entry having more data at the end!
951 */
Jeff King90553842019-07-31 00:38:15 -0400952static int do_compare_entry_piecewise(const struct cache_entry *ce,
953 const struct traverse_info *info,
954 const char *name, size_t namelen,
955 unsigned mode)
Linus Torvalds01904572008-03-05 20:15:44 -0800956{
Jeff King90553842019-07-31 00:38:15 -0400957 int pathlen, ce_len;
Linus Torvalds01904572008-03-05 20:15:44 -0800958 const char *ce_name;
959
960 if (info->prev) {
David Turnerd9c2bd52015-12-21 17:34:20 -0500961 int cmp = do_compare_entry_piecewise(ce, info->prev,
Jeff King90553842019-07-31 00:38:15 -0400962 info->name, info->namelen,
963 info->mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800964 if (cmp)
965 return cmp;
966 }
967 pathlen = info->pathlen;
968 ce_len = ce_namelen(ce);
969
970 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
971 if (ce_len < pathlen)
972 return -1;
973
974 ce_len -= pathlen;
975 ce_name = ce->name + pathlen;
976
Jeff King90553842019-07-31 00:38:15 -0400977 return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800978}
979
David Turnerd9c2bd52015-12-21 17:34:20 -0500980static int do_compare_entry(const struct cache_entry *ce,
981 const struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -0400982 const char *name, size_t namelen,
983 unsigned mode)
David Turnerd9c2bd52015-12-21 17:34:20 -0500984{
Jeff King90553842019-07-31 00:38:15 -0400985 int pathlen, ce_len;
David Turnerd9c2bd52015-12-21 17:34:20 -0500986 const char *ce_name;
987 int cmp;
Derrick Stoleecd807a52021-07-14 13:12:31 +0000988 unsigned ce_mode;
David Turnerd9c2bd52015-12-21 17:34:20 -0500989
990 /*
991 * If we have not precomputed the traverse path, it is quicker
992 * to avoid doing so. But if we have precomputed it,
993 * it is quicker to use the precomputed version.
994 */
995 if (!info->traverse_path)
Jeff King90553842019-07-31 00:38:15 -0400996 return do_compare_entry_piecewise(ce, info, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -0500997
998 cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
999 if (cmp)
1000 return cmp;
1001
1002 pathlen = info->pathlen;
1003 ce_len = ce_namelen(ce);
1004
1005 if (ce_len < pathlen)
1006 return -1;
1007
1008 ce_len -= pathlen;
1009 ce_name = ce->name + pathlen;
1010
Derrick Stoleecd807a52021-07-14 13:12:31 +00001011 ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG;
1012 return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -05001013}
1014
Linus Torvalds01904572008-03-05 20:15:44 -08001015static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
1016{
Jeff King90553842019-07-31 00:38:15 -04001017 int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode);
Linus Torvalds01904572008-03-05 20:15:44 -08001018 if (cmp)
1019 return cmp;
1020
1021 /*
Derrick Stoleecd807a52021-07-14 13:12:31 +00001022 * At this point, we know that we have a prefix match. If ce
1023 * is a sparse directory, then allow an exact match. This only
1024 * works when the input name is a directory, since ce->name
1025 * ends in a directory separator.
1026 */
1027 if (S_ISSPARSEDIR(ce->ce_mode) &&
1028 ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1)
1029 return 0;
1030
1031 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001032 * Even if the beginning compared identically, the ce should
1033 * compare as bigger than a directory leading up to it!
1034 */
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001035 return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));
Linus Torvalds01904572008-03-05 20:15:44 -08001036}
1037
Junio C Hamanoda165f42010-01-07 14:59:54 -08001038static int ce_in_traverse_path(const struct cache_entry *ce,
1039 const struct traverse_info *info)
1040{
1041 if (!info->prev)
1042 return 1;
Jeff King90553842019-07-31 00:38:15 -04001043 if (do_compare_entry(ce, info->prev,
1044 info->name, info->namelen, info->mode))
Junio C Hamanoda165f42010-01-07 14:59:54 -08001045 return 0;
1046 /*
1047 * If ce (blob) is the same name as the path (which is a tree
1048 * we will be descending into), it won't be inside it.
1049 */
1050 return (info->pathlen < ce_namelen(ce));
1051}
1052
Jameson Millera8497352018-07-02 19:49:31 +00001053static struct cache_entry *create_ce_entry(const struct traverse_info *info,
1054 const struct name_entry *n,
1055 int stage,
1056 struct index_state *istate,
Derrick Stolee523506d2021-07-14 13:12:33 +00001057 int is_transient,
1058 int is_sparse_directory)
Linus Torvalds01904572008-03-05 20:15:44 -08001059{
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001060 size_t len = traverse_path_len(info, tree_entry_len(n));
Derrick Stolee523506d2021-07-14 13:12:33 +00001061 size_t alloc_len = is_sparse_directory ? len + 1 : len;
Jameson Millera8497352018-07-02 19:49:31 +00001062 struct cache_entry *ce =
1063 is_transient ?
Derrick Stolee523506d2021-07-14 13:12:33 +00001064 make_empty_transient_cache_entry(alloc_len, NULL) :
1065 make_empty_cache_entry(istate, alloc_len);
Linus Torvalds01904572008-03-05 20:15:44 -08001066
1067 ce->ce_mode = create_ce_mode(n->mode);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001068 ce->ce_flags = create_ce_flags(stage);
1069 ce->ce_namelen = len;
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001070 oidcpy(&ce->oid, &n->oid);
Jeff King5aa02f92019-07-31 00:38:25 -04001071 /* len+1 because the cache_entry allocates space for NUL */
1072 make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen);
Linus Torvalds01904572008-03-05 20:15:44 -08001073
Derrick Stolee523506d2021-07-14 13:12:33 +00001074 if (is_sparse_directory) {
1075 ce->name[len] = '/';
1076 ce->name[len + 1] = '\0';
1077 ce->ce_namelen++;
1078 ce->ce_flags |= CE_SKIP_WORKTREE;
1079 }
1080
Linus Torvalds01904572008-03-05 20:15:44 -08001081 return ce;
1082}
1083
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001084/*
1085 * Note that traverse_by_cache_tree() duplicates some logic in this function
1086 * without actually calling it. If you change the logic here you may need to
1087 * check and change there as well.
1088 */
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +00001089static int unpack_single_entry(int n, unsigned long mask,
1090 unsigned long dirmask,
1091 struct cache_entry **src,
1092 const struct name_entry *names,
1093 const struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -08001094{
1095 int i;
1096 struct unpack_trees_options *o = info->data;
René Scharfe603d2492013-06-16 01:44:43 +02001097 unsigned long conflicts = info->df_conflicts | dirmask;
Linus Torvalds01904572008-03-05 20:15:44 -08001098
Linus Torvalds01904572008-03-05 20:15:44 -08001099 if (mask == dirmask && !src[0])
1100 return 0;
1101
Linus Torvalds01904572008-03-05 20:15:44 -08001102 /*
Derrick Stolee523506d2021-07-14 13:12:33 +00001103 * When we have a sparse directory entry for src[0],
1104 * then this isn't necessarily a directory-file conflict.
1105 */
1106 if (mask == dirmask && src[0] &&
1107 S_ISSPARSEDIR(src[0]->ce_mode))
1108 conflicts = 0;
1109
1110 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001111 * Ok, we've filled in up to any potential index entry in src[0],
1112 * now do the rest.
1113 */
1114 for (i = 0; i < n; i++) {
1115 int stage;
1116 unsigned int bit = 1ul << i;
1117 if (conflicts & bit) {
1118 src[i + o->merge] = o->df_conflict_entry;
1119 continue;
1120 }
1121 if (!(mask & bit))
1122 continue;
1123 if (!o->merge)
1124 stage = 0;
1125 else if (i + 1 < o->head_idx)
1126 stage = 1;
1127 else if (i + 1 > o->head_idx)
1128 stage = 3;
1129 else
1130 stage = 2;
Jameson Millera8497352018-07-02 19:49:31 +00001131
1132 /*
1133 * If the merge bit is set, then the cache entries are
1134 * discarded in the following block. In this case,
1135 * construct "transient" cache_entries, as they are
1136 * not stored in the index. otherwise construct the
1137 * cache entry from the index aware logic.
1138 */
Derrick Stolee523506d2021-07-14 13:12:33 +00001139 src[i + o->merge] = create_ce_entry(info, names + i, stage,
1140 &o->result, o->merge,
1141 bit & dirmask);
Linus Torvalds01904572008-03-05 20:15:44 -08001142 }
1143
René Scharfe5d80ef52013-06-02 17:46:57 +02001144 if (o->merge) {
1145 int rc = call_unpack_fn((const struct cache_entry * const *)src,
1146 o);
1147 for (i = 0; i < n; i++) {
1148 struct cache_entry *ce = src[i + o->merge];
1149 if (ce != o->df_conflict_entry)
Jameson Millera8497352018-07-02 19:49:31 +00001150 discard_cache_entry(ce);
René Scharfe5d80ef52013-06-02 17:46:57 +02001151 }
1152 return rc;
1153 }
Linus Torvalds01904572008-03-05 20:15:44 -08001154
Linus Torvalds01904572008-03-05 20:15:44 -08001155 for (i = 0; i < n; i++)
Junio C Hamanoaab3b9a2009-03-12 00:02:12 -07001156 if (src[i] && src[i] != o->df_conflict_entry)
Jeff King46169182014-11-24 13:36:51 -05001157 if (do_add_entry(o, src[i], 0, 0))
1158 return -1;
1159
Linus Torvalds01904572008-03-05 20:15:44 -08001160 return 0;
1161}
1162
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001163static int unpack_failed(struct unpack_trees_options *o, const char *message)
1164{
1165 discard_index(&o->result);
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07001166 if (!o->quiet && !o->exiting_early) {
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001167 if (message)
1168 return error("%s", message);
1169 return -1;
1170 }
1171 return -1;
1172}
1173
Junio C Hamano730f7282009-09-20 00:03:39 -07001174/*
1175 * The tree traversal is looking at name p. If we have a matching entry,
1176 * return it. If name p is a directory in the index, do not return
1177 * anything, as we will want to match it when the traversal descends into
1178 * the directory.
1179 */
1180static int find_cache_pos(struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -04001181 const char *p, size_t p_len)
Junio C Hamano730f7282009-09-20 00:03:39 -07001182{
1183 int pos;
1184 struct unpack_trees_options *o = info->data;
1185 struct index_state *index = o->src_index;
1186 int pfxlen = info->pathlen;
Junio C Hamano730f7282009-09-20 00:03:39 -07001187
1188 for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001189 const struct cache_entry *ce = index->cache[pos];
Junio C Hamano730f7282009-09-20 00:03:39 -07001190 const char *ce_name, *ce_slash;
1191 int cmp, ce_len;
1192
Brian Downinge53e6b42010-06-10 21:59:07 -05001193 if (ce->ce_flags & CE_UNPACKED) {
1194 /*
1195 * cache_bottom entry is already unpacked, so
1196 * we can never match it; don't check it
1197 * again.
1198 */
1199 if (pos == o->cache_bottom)
1200 ++o->cache_bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -07001201 continue;
Brian Downinge53e6b42010-06-10 21:59:07 -05001202 }
David Turnera6720952016-01-22 14:58:43 -05001203 if (!ce_in_traverse_path(ce, info)) {
1204 /*
1205 * Check if we can skip future cache checks
1206 * (because we're already past all possible
1207 * entries in the traverse path).
1208 */
1209 if (info->traverse_path) {
1210 if (strncmp(ce->name, info->traverse_path,
1211 info->pathlen) > 0)
1212 break;
1213 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001214 continue;
David Turnera6720952016-01-22 14:58:43 -05001215 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001216 ce_name = ce->name + pfxlen;
1217 ce_slash = strchr(ce_name, '/');
1218 if (ce_slash)
1219 ce_len = ce_slash - ce_name;
1220 else
1221 ce_len = ce_namelen(ce) - pfxlen;
Jeff King90553842019-07-31 00:38:15 -04001222 cmp = name_compare(p, p_len, ce_name, ce_len);
Junio C Hamano730f7282009-09-20 00:03:39 -07001223 /*
1224 * Exact match; if we have a directory we need to
1225 * delay returning it.
1226 */
1227 if (!cmp)
1228 return ce_slash ? -2 - pos : pos;
1229 if (0 < cmp)
1230 continue; /* keep looking */
1231 /*
1232 * ce_name sorts after p->path; could it be that we
1233 * have files under p->path directory in the index?
1234 * E.g. ce_name == "t-i", and p->path == "t"; we may
1235 * have "t/a" in the index.
1236 */
Jeff King90553842019-07-31 00:38:15 -04001237 if (p_len < ce_len && !memcmp(ce_name, p, p_len) &&
Junio C Hamano730f7282009-09-20 00:03:39 -07001238 ce_name[p_len] < '/')
1239 continue; /* keep looking */
1240 break;
1241 }
1242 return -1;
1243}
1244
Derrick Stolee523506d2021-07-14 13:12:33 +00001245/*
1246 * Given a sparse directory entry 'ce', compare ce->name to
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001247 * info->traverse_path + p->path + '/' if info->traverse_path
1248 * is non-empty.
1249 *
Derrick Stolee523506d2021-07-14 13:12:33 +00001250 * Compare ce->name to p->path + '/' otherwise. Note that
1251 * ce->name must end in a trailing '/' because it is a sparse
1252 * directory entry.
1253 */
1254static int sparse_dir_matches_path(const struct cache_entry *ce,
1255 struct traverse_info *info,
1256 const struct name_entry *p)
1257{
1258 assert(S_ISSPARSEDIR(ce->ce_mode));
1259 assert(ce->name[ce->ce_namelen - 1] == '/');
1260
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001261 if (info->pathlen)
1262 return ce->ce_namelen == info->pathlen + p->pathlen + 1 &&
1263 ce->name[info->pathlen - 1] == '/' &&
1264 !strncmp(ce->name, info->traverse_path, info->pathlen) &&
1265 !strncmp(ce->name + info->pathlen, p->path, p->pathlen);
Derrick Stolee523506d2021-07-14 13:12:33 +00001266 return ce->ce_namelen == p->pathlen + 1 &&
1267 !strncmp(ce->name, p->path, p->pathlen);
1268}
1269
Junio C Hamano730f7282009-09-20 00:03:39 -07001270static struct cache_entry *find_cache_entry(struct traverse_info *info,
1271 const struct name_entry *p)
1272{
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001273 const char *path;
Jeff King90553842019-07-31 00:38:15 -04001274 int pos = find_cache_pos(info, p->path, p->pathlen);
Junio C Hamano730f7282009-09-20 00:03:39 -07001275 struct unpack_trees_options *o = info->data;
1276
1277 if (0 <= pos)
1278 return o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001279
1280 /*
1281 * Check for a sparse-directory entry named "path/".
1282 * Due to the input p->path not having a trailing
1283 * slash, the negative 'pos' value overshoots the
1284 * expected position, hence "-2" instead of "-1".
1285 */
1286 pos = -pos - 2;
1287
1288 if (pos < 0 || pos >= o->src_index->cache_nr)
Junio C Hamano730f7282009-09-20 00:03:39 -07001289 return NULL;
Derrick Stolee523506d2021-07-14 13:12:33 +00001290
1291 /*
1292 * Due to lexicographic sorting and sparse directory
1293 * entries ending with a trailing slash, our path as a
1294 * sparse directory (e.g "subdir/") and our path as a
1295 * file (e.g. "subdir") might be separated by other
1296 * paths (e.g. "subdir-").
1297 */
1298 while (pos >= 0) {
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001299 struct cache_entry *ce = o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001300
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001301 if (!skip_prefix(ce->name, info->traverse_path, &path) ||
1302 strncmp(path, p->path, p->pathlen) ||
1303 path[p->pathlen] != '/')
Derrick Stolee523506d2021-07-14 13:12:33 +00001304 return NULL;
1305
1306 if (S_ISSPARSEDIR(ce->ce_mode) &&
1307 sparse_dir_matches_path(ce, info, p))
1308 return ce;
1309
1310 pos--;
1311 }
1312
1313 return NULL;
Junio C Hamano730f7282009-09-20 00:03:39 -07001314}
1315
Junio C Hamanoba655da2009-09-14 02:22:00 -07001316static void debug_path(struct traverse_info *info)
1317{
1318 if (info->prev) {
1319 debug_path(info->prev);
Jeff King90553842019-07-31 00:38:15 -04001320 if (*info->prev->name)
Junio C Hamanoba655da2009-09-14 02:22:00 -07001321 putchar('/');
1322 }
Jeff King90553842019-07-31 00:38:15 -04001323 printf("%s", info->name);
Junio C Hamanoba655da2009-09-14 02:22:00 -07001324}
1325
1326static void debug_name_entry(int i, struct name_entry *n)
1327{
1328 printf("ent#%d %06o %s\n", i,
1329 n->path ? n->mode : 0,
1330 n->path ? n->path : "(missing)");
1331}
1332
1333static void debug_unpack_callback(int n,
1334 unsigned long mask,
1335 unsigned long dirmask,
1336 struct name_entry *names,
1337 struct traverse_info *info)
1338{
1339 int i;
1340 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1341 mask, dirmask, n);
1342 debug_path(info);
1343 putchar('\n');
1344 for (i = 0; i < n; i++)
1345 debug_name_entry(i, names + i);
1346}
1347
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001348/*
Derrick Stolee523506d2021-07-14 13:12:33 +00001349 * Returns true if and only if the given cache_entry is a
1350 * sparse-directory entry that matches the given name_entry
1351 * from the tree walk at the given traverse_info.
1352 */
1353static int is_sparse_directory_entry(struct cache_entry *ce,
1354 struct name_entry *name,
1355 struct traverse_info *info)
1356{
1357 if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))
1358 return 0;
1359
1360 return sparse_dir_matches_path(ce, info, name);
1361}
1362
1363/*
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001364 * Note that traverse_by_cache_tree() duplicates some logic in this function
1365 * without actually calling it. If you change the logic here you may need to
1366 * check and change there as well.
1367 */
Linus Torvalds01904572008-03-05 20:15:44 -08001368static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1369{
René Scharfec7cddc12009-01-31 15:39:10 +01001370 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Linus Torvalds01904572008-03-05 20:15:44 -08001371 struct unpack_trees_options *o = info->data;
Linus Torvalds01904572008-03-05 20:15:44 -08001372 const struct name_entry *p = names;
1373
1374 /* Find first entry with a real name (we could use "mask" too) */
1375 while (!p->mode)
1376 p++;
1377
Junio C Hamanoba655da2009-09-14 02:22:00 -07001378 if (o->debug_unpack)
1379 debug_unpack_callback(n, mask, dirmask, names, info);
1380
Linus Torvalds01904572008-03-05 20:15:44 -08001381 /* Are we supposed to look at the index too? */
1382 if (o->merge) {
Victoria Dyef2a454e2021-11-29 15:52:43 +00001383 int hint = -1;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001384 while (1) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001385 int cmp;
Junio C Hamano730f7282009-09-20 00:03:39 -07001386 struct cache_entry *ce;
1387
1388 if (o->diff_index_cached)
Victoria Dyef2a454e2021-11-29 15:52:43 +00001389 ce = next_cache_entry(o, &hint);
Junio C Hamano730f7282009-09-20 00:03:39 -07001390 else
1391 ce = find_cache_entry(info, p);
1392
Junio C Hamanoda165f42010-01-07 14:59:54 -08001393 if (!ce)
1394 break;
1395 cmp = compare_entry(ce, info, p);
Linus Torvalds01904572008-03-05 20:15:44 -08001396 if (cmp < 0) {
1397 if (unpack_index_entry(ce, o) < 0)
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001398 return unpack_failed(o, NULL);
Linus Torvalds01904572008-03-05 20:15:44 -08001399 continue;
1400 }
1401 if (!cmp) {
1402 if (ce_stage(ce)) {
1403 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001404 * If we skip unmerged index
1405 * entries, we'll skip this
1406 * entry *and* the tree
1407 * entries associated with it!
Linus Torvalds01904572008-03-05 20:15:44 -08001408 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08001409 if (o->skip_unmerged) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001410 add_same_unmerged(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -08001411 return mask;
Linus Torvalds34110cd2008-03-06 18:12:28 -08001412 }
Linus Torvalds01904572008-03-05 20:15:44 -08001413 }
1414 src[0] = ce;
Linus Torvalds01904572008-03-05 20:15:44 -08001415 }
1416 break;
1417 }
1418 }
1419
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +00001420 if (unpack_single_entry(n, mask, dirmask, src, names, info) < 0)
Linus Torvalds01904572008-03-05 20:15:44 -08001421 return -1;
1422
René Scharfe97e59542012-04-10 20:55:58 +02001423 if (o->merge && src[0]) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001424 if (ce_stage(src[0]))
1425 mark_ce_used_same_name(src[0], o);
1426 else
1427 mark_ce_used(src[0], o);
1428 }
1429
Linus Torvalds01904572008-03-05 20:15:44 -08001430 /* Now handle any directories.. */
1431 if (dirmask) {
Junio C Hamanob65982b2009-05-20 15:57:22 -07001432 /* special case: "diff-index --cached" looking at a tree */
1433 if (o->diff_index_cached &&
1434 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
1435 int matches;
1436 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
1437 names, info);
1438 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001439 * Everything under the name matches; skip the
1440 * entire hierarchy. diff_index_cached codepath
1441 * special cases D/F conflicts in such a way that
1442 * it does not do any look-ahead, so this is safe.
Junio C Hamanob65982b2009-05-20 15:57:22 -07001443 */
1444 if (matches) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001445 o->cache_bottom += matches;
Junio C Hamanob65982b2009-05-20 15:57:22 -07001446 return mask;
1447 }
1448 }
1449
Derrick Stolee523506d2021-07-14 13:12:33 +00001450 if (!is_sparse_directory_entry(src[0], names, info) &&
1451 traverse_trees_recursive(n, dirmask, mask & ~dirmask,
1452 names, info) < 0) {
Junio C Hamano542c2642008-03-10 01:26:23 -07001453 return -1;
Derrick Stolee523506d2021-07-14 13:12:33 +00001454 }
1455
Linus Torvalds01904572008-03-05 20:15:44 -08001456 return mask;
1457 }
1458
1459 return mask;
1460}
1461
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001462static int clear_ce_flags_1(struct index_state *istate,
1463 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001464 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001465 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001466 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001467 enum pattern_match_result default_match,
1468 int progress_nr);
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001469
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001470/* Whole directory matching */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001471static int clear_ce_flags_dir(struct index_state *istate,
1472 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001473 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001474 char *basename,
1475 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001476 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001477 enum pattern_match_result default_match,
1478 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001479{
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001480 struct cache_entry **cache_end;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001481 int dtype = DT_DIR;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001482 int rc;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001483 enum pattern_match_result ret, orig_ret;
1484 orig_ret = path_matches_pattern_list(prefix->buf, prefix->len,
1485 basename, &dtype, pl, istate);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001486
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001487 strbuf_addch(prefix, '/');
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001488
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001489 /* If undecided, use matching result of parent dir in defval */
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001490 if (orig_ret == UNDECIDED)
Derrick Stolee468ce992019-09-03 11:04:58 -07001491 ret = default_match;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001492 else
1493 ret = orig_ret;
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001494
1495 for (cache_end = cache; cache_end != cache + nr; cache_end++) {
1496 struct cache_entry *ce = *cache_end;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001497 if (strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001498 break;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001499 }
1500
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001501 if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {
1502 struct cache_entry **ce = cache;
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001503 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001504
1505 while (ce < cache_end) {
1506 (*ce)->ce_flags &= ~clear_mask;
1507 ce++;
1508 }
1509 } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) {
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001510 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001511 } else {
1512 rc = clear_ce_flags_1(istate, cache, cache_end - cache,
1513 prefix,
1514 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001515 pl, ret,
1516 progress_nr);
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001517 }
1518
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001519 strbuf_setlen(prefix, prefix->len - 1);
1520 return rc;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001521}
1522
1523/*
1524 * Traverse the index, find every entry that matches according to
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001525 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001526 * number of traversed entries.
1527 *
1528 * If select_mask is non-zero, only entries whose ce_flags has on of
1529 * those bits enabled are traversed.
1530 *
1531 * cache : pointer to an index entry
1532 * prefix_len : an offset to its path
1533 *
1534 * The current path ("prefix") including the trailing '/' is
1535 * cache[0]->name[0..(prefix_len-1)]
1536 * Top level path has prefix_len zero.
1537 */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001538static int clear_ce_flags_1(struct index_state *istate,
1539 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001540 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001541 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001542 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001543 enum pattern_match_result default_match,
1544 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001545{
Jeff Kingd20bc012020-01-29 00:46:47 -05001546 struct cache_entry **cache_end = nr ? cache + nr : cache;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001547
1548 /*
1549 * Process all entries that have the given prefix and meet
1550 * select_mask condition
1551 */
1552 while(cache != cache_end) {
1553 struct cache_entry *ce = *cache;
1554 const char *name, *slash;
Derrick Stolee468ce992019-09-03 11:04:58 -07001555 int len, dtype;
1556 enum pattern_match_result ret;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001557
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001558 display_progress(istate->progress, progress_nr);
1559
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001560 if (select_mask && !(ce->ce_flags & select_mask)) {
1561 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001562 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001563 continue;
1564 }
1565
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001566 if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001567 break;
1568
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001569 name = ce->name + prefix->len;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001570 slash = strchr(name, '/');
1571
1572 /* If it's a directory, try whole directory match first */
1573 if (slash) {
1574 int processed;
1575
1576 len = slash - name;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001577 strbuf_add(prefix, name, len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001578
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001579 processed = clear_ce_flags_dir(istate, cache, cache_end - cache,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001580 prefix,
1581 prefix->buf + prefix->len - len,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001582 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001583 pl, default_match,
1584 progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001585
1586 /* clear_c_f_dir eats a whole dir already? */
1587 if (processed) {
1588 cache += processed;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001589 progress_nr += processed;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001590 strbuf_setlen(prefix, prefix->len - len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001591 continue;
1592 }
1593
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001594 strbuf_addch(prefix, '/');
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001595 processed = clear_ce_flags_1(istate, cache, cache_end - cache,
1596 prefix,
1597 select_mask, clear_mask, pl,
1598 default_match, progress_nr);
1599
1600 cache += processed;
1601 progress_nr += processed;
1602
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001603 strbuf_setlen(prefix, prefix->len - len - 1);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001604 continue;
1605 }
1606
1607 /* Non-directory */
1608 dtype = ce_to_dtype(ce);
Derrick Stolee468ce992019-09-03 11:04:58 -07001609 ret = path_matches_pattern_list(ce->name,
1610 ce_namelen(ce),
1611 name, &dtype, pl, istate);
1612 if (ret == UNDECIDED)
1613 ret = default_match;
Derrick Stoleef998a3f2020-01-31 20:16:15 +00001614 if (ret == MATCHED || ret == MATCHED_RECURSIVE)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001615 ce->ce_flags &= ~clear_mask;
1616 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001617 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001618 }
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001619
1620 display_progress(istate->progress, progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001621 return nr - (cache_end - cache);
1622}
1623
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001624static int clear_ce_flags(struct index_state *istate,
1625 int select_mask, int clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001626 struct pattern_list *pl,
1627 int show_progress)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001628{
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001629 static struct strbuf prefix = STRBUF_INIT;
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001630 char label[100];
1631 int rval;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001632
1633 strbuf_reset(&prefix);
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001634 if (show_progress)
1635 istate->progress = start_delayed_progress(
1636 _("Updating index flags"),
1637 istate->cache_nr);
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001638
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001639 xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)",
1640 (unsigned long)select_mask, (unsigned long)clear_mask);
1641 trace2_region_enter("unpack_trees", label, the_repository);
1642 rval = clear_ce_flags_1(istate,
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001643 istate->cache,
1644 istate->cache_nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001645 &prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001646 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001647 pl, 0, 0);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001648 trace2_region_leave("unpack_trees", label, the_repository);
1649
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001650 stop_progress(&istate->progress);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001651 return rval;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001652}
1653
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001654/*
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001655 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1656 */
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001657static void mark_new_skip_worktree(struct pattern_list *pl,
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001658 struct index_state *istate,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001659 int select_flag, int skip_wt_flag,
1660 int show_progress)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001661{
1662 int i;
1663
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001664 /*
1665 * 1. Pretend the narrowest worktree: only unmerged entries
1666 * are checked out
1667 */
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001668 for (i = 0; i < istate->cache_nr; i++) {
1669 struct cache_entry *ce = istate->cache[i];
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001670
1671 if (select_flag && !(ce->ce_flags & select_flag))
1672 continue;
1673
Max Kirillovb33fdfc2018-07-10 22:17:48 +03001674 if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001675 ce->ce_flags |= skip_wt_flag;
1676 else
1677 ce->ce_flags &= ~skip_wt_flag;
1678 }
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001679
1680 /*
1681 * 2. Widen worktree according to sparse-checkout file.
1682 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1683 */
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001684 clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001685}
1686
Elijah Newren30e89c12020-03-27 00:48:51 +00001687static void populate_from_existing_patterns(struct unpack_trees_options *o,
1688 struct pattern_list *pl)
1689{
Derrick Stoleedd230222021-01-23 19:58:17 +00001690 if (get_sparse_checkout_patterns(pl) < 0)
Elijah Newren30e89c12020-03-27 00:48:51 +00001691 o->skip_sparse_checkout = 1;
1692 else
1693 o->pl = pl;
Elijah Newren30e89c12020-03-27 00:48:51 +00001694}
1695
1696
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001697static int verify_absent(const struct cache_entry *,
1698 enum unpack_trees_error_types,
1699 struct unpack_trees_options *);
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001700/*
1701 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1702 * resulting index, -2 on failure to reflect the changes to the work tree.
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001703 *
1704 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001705 */
Linus Torvalds933bf402007-08-09 22:21:29 -07001706int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +02001707{
Derrick Stolee6863df32021-03-30 13:10:52 +00001708 struct repository *repo = the_repository;
Victoria Dyef2a454e2021-11-29 15:52:43 +00001709 int i, hint, ret;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001710 static struct cache_entry *dfc;
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001711 struct pattern_list pl;
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001712 int free_pattern_list = 0;
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001713 struct dir_struct dir = DIR_INIT;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001714
Elijah Newren480d3d62021-09-27 16:33:44 +00001715 if (o->reset == UNPACK_RESET_INVALID)
1716 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001717
Junio C Hamanoca885a42008-03-13 22:07:18 -07001718 if (len > MAX_UNPACK_TREES)
1719 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001720 if (o->dir)
1721 BUG("o->dir is for internal use only");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001722
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02001723 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00001724 trace2_region_enter("unpack_trees", "unpack_trees", the_repository);
1725
Derrick Stolee6863df32021-03-30 13:10:52 +00001726 prepare_repo_settings(repo);
1727 if (repo->settings.command_requires_full_index) {
1728 ensure_full_index(o->src_index);
1729 ensure_full_index(o->dst_index);
1730 }
1731
Elijah Newren480d3d62021-09-27 16:33:44 +00001732 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED &&
1733 o->preserve_ignored)
1734 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1735
Elijah Newren04988c82021-09-27 16:33:41 +00001736 if (!o->preserve_ignored) {
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001737 o->dir = &dir;
Elijah Newren04988c82021-09-27 16:33:41 +00001738 o->dir->flags |= DIR_SHOW_IGNORED;
1739 setup_standard_excludes(o->dir);
1740 }
1741
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001742 if (!core_apply_sparse_checkout || !o->update)
1743 o->skip_sparse_checkout = 1;
Derrick Stoleee0912282019-11-21 22:04:46 +00001744 if (!o->skip_sparse_checkout && !o->pl) {
Elijah Newren30e89c12020-03-27 00:48:51 +00001745 memset(&pl, 0, sizeof(pl));
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001746 free_pattern_list = 1;
Elijah Newren30e89c12020-03-27 00:48:51 +00001747 populate_from_existing_patterns(o, &pl);
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001748 }
1749
Linus Torvalds34110cd2008-03-06 18:12:28 -08001750 memset(&o->result, 0, sizeof(o->result));
Junio C Hamano913e0e92008-08-23 12:57:30 -07001751 o->result.initialized = 1;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001752 o->result.timestamp.sec = o->src_index->timestamp.sec;
1753 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
Junio C Hamano9170c7a2012-04-27 15:58:13 -07001754 o->result.version = o->src_index->version;
Elijah Newren7db11832018-04-23 23:50:45 -07001755 if (!o->src_index->split_index) {
1756 o->result.split_index = NULL;
1757 } else if (o->src_index == o->dst_index) {
1758 /*
1759 * o->dst_index (and thus o->src_index) will be discarded
1760 * and overwritten with o->result at the end of this function,
1761 * so just use src_index's split_index to avoid having to
1762 * create a new one.
1763 */
1764 o->result.split_index = o->src_index->split_index;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001765 o->result.split_index->refcount++;
Elijah Newren7db11832018-04-23 23:50:45 -07001766 } else {
1767 o->result.split_index = init_split_index(&o->result);
1768 }
brian m. carlson75691ea2018-05-02 00:25:44 +00001769 oidcpy(&o->result.oid, &o->src_index->oid);
Johannes Schindelin16da1342006-07-30 20:25:18 +02001770 o->merge_size = len;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001771 mark_all_ce_unused(o->src_index);
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001772
Johannes Schindelin3dfd3052021-03-17 15:30:48 +00001773 o->result.fsmonitor_last_update =
1774 xstrdup_or_null(o->src_index->fsmonitor_last_update);
Utsav Shah679f2f92019-11-20 08:32:17 +00001775
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001776 /*
1777 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1778 */
1779 if (!o->skip_sparse_checkout)
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001780 mark_new_skip_worktree(o->pl, o->src_index, 0,
1781 CE_NEW_SKIP_WORKTREE, o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001782
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001783 if (!dfc)
Jeff King13494ed2008-10-23 04:30:58 +00001784 dfc = xcalloc(1, cache_entry_size(0));
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001785 o->df_conflict_entry = dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001786
1787 if (len) {
Linus Torvalds01904572008-03-05 20:15:44 -08001788 const char *prefix = o->prefix ? o->prefix : "";
1789 struct traverse_info info;
Linus Torvalds933bf402007-08-09 22:21:29 -07001790
Linus Torvalds01904572008-03-05 20:15:44 -08001791 setup_traverse_info(&info, prefix);
1792 info.fn = unpack_callback;
1793 info.data = o;
Matthieu Moye6c111b2010-08-11 10:38:07 +02001794 info.show_all_errors = o->show_all_errors;
Junio C Hamano40e37252011-08-29 12:31:06 -07001795 info.pathspec = o->pathspec;
Linus Torvalds01904572008-03-05 20:15:44 -08001796
Junio C Hamanoda165f42010-01-07 14:59:54 -08001797 if (o->prefix) {
Victoria Dyef2a454e2021-11-29 15:52:43 +00001798 hint = -1;
1799
Junio C Hamanoda165f42010-01-07 14:59:54 -08001800 /*
1801 * Unpack existing index entries that sort before the
1802 * prefix the tree is spliced into. Note that o->merge
1803 * is always true in this case.
1804 */
1805 while (1) {
Victoria Dyef2a454e2021-11-29 15:52:43 +00001806 struct cache_entry *ce = next_cache_entry(o, &hint);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001807 if (!ce)
1808 break;
1809 if (ce_in_traverse_path(ce, &info))
1810 break;
1811 if (unpack_index_entry(ce, o) < 0)
1812 goto return_failed;
1813 }
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001814 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08001815
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02001816 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00001817 trace2_region_enter("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01001818 ret = traverse_trees(o->src_index, len, t, &info);
Derrick Stoleec3388982021-01-04 03:09:11 +00001819 trace2_region_leave("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02001820 trace_performance_leave("traverse_trees");
1821 if (ret < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08001822 goto return_failed;
Linus Torvalds01904572008-03-05 20:15:44 -08001823 }
1824
1825 /* Any left-over entries in the index? */
1826 if (o->merge) {
Victoria Dyef2a454e2021-11-29 15:52:43 +00001827 hint = -1;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001828 while (1) {
Victoria Dyef2a454e2021-11-29 15:52:43 +00001829 struct cache_entry *ce = next_cache_entry(o, &hint);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001830 if (!ce)
1831 break;
Linus Torvalds01904572008-03-05 20:15:44 -08001832 if (unpack_index_entry(ce, o) < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08001833 goto return_failed;
Daniel Barkalow17e46422008-02-07 11:39:52 -05001834 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001835 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08001836 mark_all_ce_unused(o->src_index);
Johannes Schindelin16da1342006-07-30 20:25:18 +02001837
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001838 if (o->trivial_merges_only && o->nontrivial_merge) {
1839 ret = unpack_failed(o, "Merge requires file-level merging");
1840 goto done;
1841 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001842
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001843 if (!o->skip_sparse_checkout) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001844 /*
1845 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
Elijah Newren031ba552020-03-27 00:48:44 +00001846 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001847 * so apply_sparse_checkout() won't attempt to remove it from worktree
1848 */
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001849 mark_new_skip_worktree(o->pl, &o->result,
1850 CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE,
1851 o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001852
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10001853 ret = 0;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001854 for (i = 0; i < o->result.cache_nr; i++) {
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001855 struct cache_entry *ce = o->result.cache[i];
1856
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001857 /*
1858 * Entries marked with CE_ADDED in merged_entry() do not have
1859 * verify_absent() check (the check is effectively disabled
1860 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
1861 *
1862 * Do the real check now because we have had
1863 * correct CE_NEW_SKIP_WORKTREE
1864 */
1865 if (ce->ce_flags & CE_ADDED &&
Elijah Newren681c6372020-03-27 00:49:00 +00001866 verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
1867 ret = 1;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001868
Elijah Newren681c6372020-03-27 00:49:00 +00001869 if (apply_sparse_checkout(&o->result, ce, o))
1870 ret = 1;
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07001871 }
Elijah Newren681c6372020-03-27 00:49:00 +00001872 if (ret == 1) {
1873 /*
1874 * Inability to sparsify or de-sparsify individual
1875 * paths is not an error, but just a warning.
1876 */
1877 if (o->show_all_errors)
1878 display_warning_msgs(o);
1879 ret = 0;
1880 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001881 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001882
Elijah Newrenb0a5a122020-03-27 00:48:49 +00001883 ret = check_updates(o, &o->result) ? (-2) : 0;
Felipe Contrerase28f7642013-08-13 20:27:58 +02001884 if (o->dst_index) {
Nguyễn Thái Ngọc Duy836ef2b2018-08-18 16:41:26 +02001885 move_index_extensions(&o->result, o->src_index);
Brian Degenhardt52fca212015-07-28 15:30:40 -04001886 if (!ret) {
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02001887 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +01001888 cache_tree_verify(the_repository, &o->result);
Brian Degenhardt52fca212015-07-28 15:30:40 -04001889 if (!cache_tree_fully_valid(o->result.cache_tree))
1890 cache_tree_update(&o->result,
1891 WRITE_TREE_SILENT |
1892 WRITE_TREE_REPAIR);
1893 }
Ben Peart1956ecd2019-02-15 12:59:21 -05001894
1895 o->result.updated_workdir = 1;
Felipe Contrerase28f7642013-08-13 20:27:58 +02001896 discard_index(o->dst_index);
Linus Torvalds34110cd2008-03-06 18:12:28 -08001897 *o->dst_index = o->result;
Junio C Hamanoa16cc8b2014-11-17 12:12:41 -08001898 } else {
1899 discard_index(&o->result);
Felipe Contrerase28f7642013-08-13 20:27:58 +02001900 }
Elijah Newren7db11832018-04-23 23:50:45 -07001901 o->src_index = NULL;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001902
1903done:
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001904 if (free_pattern_list)
Derrick Stoleee0912282019-11-21 22:04:46 +00001905 clear_pattern_list(&pl);
Elijah Newren04988c82021-09-27 16:33:41 +00001906 if (o->dir) {
1907 dir_clear(o->dir);
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001908 o->dir = NULL;
Elijah Newren04988c82021-09-27 16:33:41 +00001909 }
Derrick Stoleec3388982021-01-04 03:09:11 +00001910 trace2_region_leave("unpack_trees", "unpack_trees", the_repository);
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001911 trace_performance_leave("unpack_trees");
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001912 return ret;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001913
1914return_failed:
Matthieu Moye6c111b2010-08-11 10:38:07 +02001915 if (o->show_all_errors)
1916 display_error_msgs(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001917 mark_all_ce_unused(o->src_index);
Junio C Hamano026680f2010-01-24 17:35:58 -08001918 ret = unpack_failed(o, NULL);
Junio C Hamanob4194822011-05-31 10:06:44 -07001919 if (o->exiting_early)
1920 ret = 0;
Junio C Hamano026680f2010-01-24 17:35:58 -08001921 goto done;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001922}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001923
Elijah Newren7af7a252020-03-27 00:48:52 +00001924/*
1925 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
1926 * working directory to match.
1927 *
1928 * CE_NEW_SKIP_WORKTREE is used internally.
1929 */
1930enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
1931{
1932 enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
1933 struct pattern_list pl;
Derrick Stoleeace224a2020-05-04 18:27:43 +00001934 int i;
Elijah Newren7af7a252020-03-27 00:48:52 +00001935 unsigned old_show_all_errors;
1936 int free_pattern_list = 0;
1937
1938 old_show_all_errors = o->show_all_errors;
1939 o->show_all_errors = 1;
1940
1941 /* Sanity checks */
1942 if (!o->update || o->index_only || o->skip_sparse_checkout)
1943 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
1944 if (o->src_index != o->dst_index || o->fn)
1945 BUG("update_sparsity() called wrong");
1946
1947 trace_performance_enter();
1948
1949 /* If we weren't given patterns, use the recorded ones */
1950 if (!o->pl) {
1951 memset(&pl, 0, sizeof(pl));
1952 free_pattern_list = 1;
1953 populate_from_existing_patterns(o, &pl);
1954 if (o->skip_sparse_checkout)
1955 goto skip_sparse_checkout;
1956 }
1957
1958 /* Set NEW_SKIP_WORKTREE on existing entries. */
1959 mark_all_ce_unused(o->src_index);
1960 mark_new_skip_worktree(o->pl, o->src_index, 0,
1961 CE_NEW_SKIP_WORKTREE, o->verbose_update);
1962
1963 /* Then loop over entries and update/remove as needed */
1964 ret = UPDATE_SPARSITY_SUCCESS;
Elijah Newren7af7a252020-03-27 00:48:52 +00001965 for (i = 0; i < o->src_index->cache_nr; i++) {
1966 struct cache_entry *ce = o->src_index->cache[i];
1967
Elijah Newrenebb568b2020-03-27 00:48:59 +00001968
1969 if (ce_stage(ce)) {
1970 /* -1 because for loop will increment by 1 */
1971 i += warn_conflicted_path(o->src_index, i, o) - 1;
1972 ret = UPDATE_SPARSITY_WARNINGS;
1973 continue;
1974 }
1975
Elijah Newren7af7a252020-03-27 00:48:52 +00001976 if (apply_sparse_checkout(o->src_index, ce, o))
1977 ret = UPDATE_SPARSITY_WARNINGS;
Elijah Newren7af7a252020-03-27 00:48:52 +00001978 }
1979
1980skip_sparse_checkout:
1981 if (check_updates(o, o->src_index))
1982 ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
1983
Elijah Newren6271d772020-03-27 00:48:57 +00001984 display_warning_msgs(o);
Elijah Newren7af7a252020-03-27 00:48:52 +00001985 o->show_all_errors = old_show_all_errors;
1986 if (free_pattern_list)
1987 clear_pattern_list(&pl);
1988 trace_performance_leave("update_sparsity");
1989 return ret;
1990}
1991
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001992/* Here come the merge functions */
1993
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001994static int reject_merge(const struct cache_entry *ce,
1995 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001996{
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07001997 return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001998}
1999
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002000static int same(const struct cache_entry *a, const struct cache_entry *b)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002001{
2002 if (!!a != !!b)
2003 return 0;
2004 if (!a && !b)
2005 return 1;
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002006 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
2007 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002008 return a->ce_mode == b->ce_mode &&
Jeff King4a7e27e2018-08-28 17:22:40 -04002009 oideq(&a->oid, &b->oid);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002010}
2011
2012
2013/*
2014 * When a CE gets turned into an unmerged entry, we
2015 * want it to be up-to-date
2016 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002017static int verify_uptodate_1(const struct cache_entry *ce,
2018 struct unpack_trees_options *o,
2019 enum unpack_trees_error_types error_type)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002020{
2021 struct stat st;
2022
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002023 if (o->index_only)
2024 return 0;
2025
2026 /*
2027 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2028 * if this entry is truly up-to-date because this file may be
2029 * overwritten.
2030 */
2031 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
2032 ; /* keep checking */
2033 else if (o->reset || ce_uptodate(ce))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002034 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002035
2036 if (!lstat(ce->name, &st)) {
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002037 int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
2038 unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002039
2040 if (submodule_from_ce(ce)) {
2041 int r = check_submodule_move_head(ce,
2042 "HEAD", oid_to_hex(&ce->oid), o);
2043 if (r)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002044 return add_rejected_path(o, error_type, ce->name);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002045 return 0;
2046 }
2047
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002048 if (!changed)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002049 return 0;
Junio C Hamano936492d2007-08-03 22:13:09 -07002050 /*
Stefan Bellera7bc8452017-03-14 14:46:39 -07002051 * Historic default policy was to allow submodule to be out
2052 * of sync wrt the superproject index. If the submodule was
2053 * not considered interesting above, we don't care here.
Junio C Hamano936492d2007-08-03 22:13:09 -07002054 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002055 if (S_ISGITLINK(ce->ce_mode))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002056 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -07002057
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002058 errno = 0;
2059 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002060 if (errno == ENOENT)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002061 return 0;
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002062 return add_rejected_path(o, error_type, ce->name);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002063}
2064
Elijah Newren64b1abe2018-04-19 10:58:12 -07002065int verify_uptodate(const struct cache_entry *ce,
2066 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002067{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002068 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002069 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02002070 return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002071}
2072
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002073static int verify_uptodate_sparse(const struct cache_entry *ce,
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002074 struct unpack_trees_options *o)
2075{
Elijah Newren1ac83f42020-03-27 00:48:56 +00002076 return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002077}
2078
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002079/*
2080 * TODO: We should actually invalidate o->result, not src_index [1].
2081 * But since cache tree and untracked cache both are not copied to
2082 * o->result until unpacking is complete, we invalidate them on
2083 * src_index instead with the assumption that they will be copied to
2084 * dst_index at the end.
2085 *
2086 * [1] src_index->cache_tree is also used in unpack_callback() so if
2087 * we invalidate o->result, we need to update it to use
2088 * o->result.cache_tree as well.
2089 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002090static void invalidate_ce_path(const struct cache_entry *ce,
2091 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002092{
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002093 if (!ce)
2094 return;
2095 cache_tree_invalidate_path(o->src_index, ce->name);
Nguyễn Thái Ngọc Duy0cacebf2018-02-07 16:21:40 +07002096 untracked_cache_invalidate_path(o->src_index, ce->name, 1);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002097}
2098
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002099/*
2100 * Check that checking out ce->sha1 in subdir ce->name is not
2101 * going to overwrite any working files.
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002102 */
Stefan Bellerd6b12302017-03-14 14:46:38 -07002103static int verify_clean_submodule(const char *old_sha1,
2104 const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002105 struct unpack_trees_options *o)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002106{
Stefan Bellera7bc8452017-03-14 14:46:39 -07002107 if (!submodule_from_ce(ce))
2108 return 0;
2109
2110 return check_submodule_move_head(ce, old_sha1,
2111 oid_to_hex(&ce->oid), o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002112}
2113
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002114static int verify_clean_subdirectory(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002115 struct unpack_trees_options *o)
Junio C Hamanoc8193532007-03-15 23:25:22 -07002116{
2117 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002118 * we are about to extract "ce->name"; we would not want to lose
Junio C Hamanoc8193532007-03-15 23:25:22 -07002119 * anything in the existing directory there.
2120 */
2121 int namelen;
Clemens Buchacher7b9e3ce2009-01-01 21:54:33 +01002122 int i;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002123 struct dir_struct d;
2124 char *pathbuf;
2125 int cnt = 0;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002126
Stefan Bellerd6b12302017-03-14 14:46:38 -07002127 if (S_ISGITLINK(ce->ce_mode)) {
brian m. carlson1053fe82017-10-15 22:07:06 +00002128 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00002129 int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
Stefan Bellerd6b12302017-03-14 14:46:38 -07002130 /*
2131 * If we are not going to update the submodule, then
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002132 * we don't care.
2133 */
Jeff King4a7e27e2018-08-28 17:22:40 -04002134 if (!sub_head && oideq(&oid, &ce->oid))
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002135 return 0;
brian m. carlson1053fe82017-10-15 22:07:06 +00002136 return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),
Jeff Kingdf351c62019-03-20 04:15:27 -04002137 ce, o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002138 }
Junio C Hamanoc8193532007-03-15 23:25:22 -07002139
2140 /*
2141 * First let's make sure we do not have a local modification
2142 * in that directory.
2143 */
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02002144 namelen = ce_namelen(ce);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002145 for (i = locate_in_src_index(ce, o);
2146 i < o->src_index->cache_nr;
2147 i++) {
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002148 struct cache_entry *ce2 = o->src_index->cache[i];
2149 int len = ce_namelen(ce2);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002150 if (len < namelen ||
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002151 strncmp(ce->name, ce2->name, namelen) ||
2152 ce2->name[namelen] != '/')
Junio C Hamanoc8193532007-03-15 23:25:22 -07002153 break;
2154 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08002155 * ce2->name is an entry in the subdirectory to be
2156 * removed.
Junio C Hamanoc8193532007-03-15 23:25:22 -07002157 */
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002158 if (!ce_stage(ce2)) {
2159 if (verify_uptodate(ce2, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002160 return -1;
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002161 add_entry(o, ce2, CE_REMOVE, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002162 invalidate_ce_path(ce, o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002163 mark_ce_used(ce2, o);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002164 }
2165 cnt++;
2166 }
2167
Elijah Newrenb817e542021-12-09 05:08:27 +00002168 /* Do not lose a locally present file that is not ignored. */
Jeff King75faa452015-09-24 17:07:03 -04002169 pathbuf = xstrfmt("%.*s/", namelen, ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002170
2171 memset(&d, 0, sizeof(d));
2172 if (o->dir)
2173 d.exclude_per_dir = o->dir->exclude_per_dir;
Nguyễn Thái Ngọc Duyc7f32592018-08-13 18:14:29 +02002174 i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
Ævar Arnfjörð Bjarmasone5a917f2021-10-07 11:46:09 +02002175 dir_clear(&d);
2176 free(pathbuf);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002177 if (i)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002178 return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
Elijah Newrenb817e542021-12-09 05:08:27 +00002179
2180 /* Do not lose startup_info->original_cwd */
2181 if (startup_info->original_cwd &&
2182 !strcmp(startup_info->original_cwd, ce->name))
2183 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
2184
Junio C Hamanoc8193532007-03-15 23:25:22 -07002185 return cnt;
2186}
2187
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002188/*
Linus Torvalds32260ad2008-03-22 09:35:59 -07002189 * This gets called when there was no index entry for the tree entry 'dst',
2190 * but we found a file in the working tree that 'lstat()' said was fine,
2191 * and we're on a case-insensitive filesystem.
2192 *
2193 * See if we can find a case-insensitive match in the index that also
2194 * matches the stat information, and assume it's that other file!
2195 */
Clemens Buchachera9307f52010-10-09 15:52:58 +02002196static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
Linus Torvalds32260ad2008-03-22 09:35:59 -07002197{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002198 const struct cache_entry *src;
Linus Torvalds32260ad2008-03-22 09:35:59 -07002199
Eric Sunshineebbd7432013-09-17 03:06:15 -04002200 src = index_file_exists(o->src_index, name, len, 1);
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07002201 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Linus Torvalds32260ad2008-03-22 09:35:59 -07002202}
2203
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002204enum absent_checking_type {
2205 COMPLETELY_ABSENT,
2206 ABSENT_ANY_DIRECTORY
2207};
2208
Clemens Buchachera9307f52010-10-09 15:52:58 +02002209static int check_ok_to_remove(const char *name, int len, int dtype,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002210 const struct cache_entry *ce, struct stat *st,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002211 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002212 enum absent_checking_type absent_type,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002213 struct unpack_trees_options *o)
2214{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002215 const struct cache_entry *result;
Clemens Buchachera9307f52010-10-09 15:52:58 +02002216
2217 /*
2218 * It may be that the 'lstat()' succeeded even though
2219 * target 'ce' was absent, because there is an old
2220 * entry that is different only in case..
2221 *
2222 * Ignore that lstat() if it matches.
2223 */
2224 if (ignore_case && icase_exists(o, name, len, st))
2225 return 0;
2226
Junio C Hamano589570d2012-06-05 22:21:42 -07002227 if (o->dir &&
Nguyễn Thái Ngọc Duyc7f32592018-08-13 18:14:29 +02002228 is_excluded(o->dir, o->src_index, name, &dtype))
Clemens Buchachera9307f52010-10-09 15:52:58 +02002229 /*
2230 * ce->name is explicitly excluded, so it is Ok to
2231 * overwrite it.
2232 */
2233 return 0;
2234 if (S_ISDIR(st->st_mode)) {
2235 /*
2236 * We are checking out path "foo" and
2237 * found "foo/." in the working tree.
2238 * This is tricky -- if we have modified
2239 * files that are in "foo/" we would lose
2240 * them.
2241 */
Jeff Kingdf351c62019-03-20 04:15:27 -04002242 if (verify_clean_subdirectory(ce, o) < 0)
Clemens Buchachera9307f52010-10-09 15:52:58 +02002243 return -1;
2244 return 0;
2245 }
2246
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002247 /* If we only care about directories, then we can remove */
2248 if (absent_type == ABSENT_ANY_DIRECTORY)
2249 return 0;
2250
Clemens Buchachera9307f52010-10-09 15:52:58 +02002251 /*
2252 * The previous round may already have decided to
2253 * delete this path, which is in a subdirectory that
2254 * is being replaced with a blob.
2255 */
Eric Sunshineebbd7432013-09-17 03:06:15 -04002256 result = index_file_exists(&o->result, name, len, 0);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002257 if (result) {
2258 if (result->ce_flags & CE_REMOVE)
2259 return 0;
2260 }
2261
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002262 return add_rejected_path(o, error_type, name);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002263}
2264
Linus Torvalds32260ad2008-03-22 09:35:59 -07002265/*
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002266 * We do not want to remove or overwrite a working tree file that
Junio C Hamanof8a9d422006-12-04 16:00:46 -08002267 * is not tracked, unless it is ignored.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002268 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002269static int verify_absent_1(const struct cache_entry *ce,
2270 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002271 enum absent_checking_type absent_type,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002272 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002273{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002274 int len;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002275 struct stat st;
2276
Elijah Newren0b0ee332021-12-09 05:08:28 +00002277 if (o->index_only || !o->update)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002278 return 0;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002279
Elijah Newren0b0ee332021-12-09 05:08:28 +00002280 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {
2281 /* Avoid nuking startup_info->original_cwd... */
2282 if (startup_info->original_cwd &&
2283 !strcmp(startup_info->original_cwd, ce->name))
2284 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,
2285 ce->name);
2286 /* ...but nuke anything else. */
2287 return 0;
2288 }
2289
Matheus Tavaresfab78a02021-03-18 15:43:47 -03002290 len = check_leading_path(ce->name, ce_namelen(ce), 0);
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002291 if (!len)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002292 return 0;
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002293 else if (len > 0) {
Jeff Kingf514ef92015-08-19 14:12:37 -04002294 char *path;
2295 int ret;
Junio C Hamanoec0603e2007-07-12 01:04:16 -07002296
Jeff Kingf514ef92015-08-19 14:12:37 -04002297 path = xmemdupz(ce->name, len);
2298 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002299 ret = error_errno("cannot stat '%s'", path);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002300 else {
2301 if (submodule_from_ce(ce))
2302 ret = check_submodule_move_head(ce,
2303 oid_to_hex(&ce->oid),
2304 NULL, o);
2305 else
2306 ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002307 &st, error_type,
2308 absent_type, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002309 }
Jeff Kingf514ef92015-08-19 14:12:37 -04002310 free(path);
2311 return ret;
Jonathan Nieder92fda792011-01-12 20:26:36 -06002312 } else if (lstat(ce->name, &st)) {
2313 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002314 return error_errno("cannot stat '%s'", ce->name);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002315 return 0;
2316 } else {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002317 if (submodule_from_ce(ce))
2318 return check_submodule_move_head(ce, oid_to_hex(&ce->oid),
2319 NULL, o);
2320
Clemens Buchachera9307f52010-10-09 15:52:58 +02002321 return check_ok_to_remove(ce->name, ce_namelen(ce),
Jonathan Nieder92fda792011-01-12 20:26:36 -06002322 ce_to_dtype(ce), ce, &st,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002323 error_type, absent_type, o);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002324 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002325}
Clemens Buchachera9307f52010-10-09 15:52:58 +02002326
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002327static int verify_absent(const struct cache_entry *ce,
Matthieu Moy08402b02010-08-11 10:38:06 +02002328 enum unpack_trees_error_types error_type,
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002329 struct unpack_trees_options *o)
2330{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002331 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002332 return 0;
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002333 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
2334}
2335
2336static int verify_absent_if_directory(const struct cache_entry *ce,
2337 enum unpack_trees_error_types error_type,
2338 struct unpack_trees_options *o)
2339{
2340 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
2341 return 0;
2342 return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002343}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002344
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002345static int verify_absent_sparse(const struct cache_entry *ce,
2346 enum unpack_trees_error_types error_type,
2347 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002348{
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002349 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002350}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002351
René Scharfef2fa3542013-06-02 17:46:54 +02002352static int merged_entry(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002353 const struct cache_entry *old,
René Scharfef2fa3542013-06-02 17:46:54 +02002354 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002355{
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002356 int update = CE_UPDATE;
Jameson Miller8e72d672018-07-02 19:49:37 +00002357 struct cache_entry *merge = dup_cache_entry(ce, &o->result);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002358
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002359 if (!old) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002360 /*
2361 * New index entries. In sparse checkout, the following
2362 * verify_absent() will be delayed until after
2363 * traverse_trees() finishes in unpack_trees(), then:
2364 *
2365 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2366 * - verify_absent() be called again, this time with
2367 * correct CE_NEW_SKIP_WORKTREE
2368 *
2369 * verify_absent() call here does nothing in sparse
2370 * checkout (i.e. o->skip_sparse_checkout == 0)
2371 */
2372 update |= CE_ADDED;
2373 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
2374
René Scharfef2fa3542013-06-02 17:46:54 +02002375 if (verify_absent(merge,
2376 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002377 discard_cache_entry(merge);
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002378 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002379 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002380 invalidate_ce_path(merge, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002381
Philippe Blaine84704f2020-02-17 04:53:05 +00002382 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002383 int ret = check_submodule_move_head(ce, NULL,
2384 oid_to_hex(&ce->oid),
2385 o);
2386 if (ret)
2387 return ret;
2388 }
2389
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002390 } else if (!(old->ce_flags & CE_CONFLICTED)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002391 /*
2392 * See if we can re-use the old CE directly?
2393 * That way we get the uptodate stat info.
2394 *
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002395 * This also removes the UPDATE flag on a match; otherwise
2396 * we will end up overwriting local changes in the work tree.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002397 */
2398 if (same(old, merge)) {
Linus Torvaldseb7a2f12008-02-22 20:41:17 -08002399 copy_cache_entry(merge, old);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002400 update = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002401 } else {
René Scharfef2fa3542013-06-02 17:46:54 +02002402 if (verify_uptodate(old, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002403 discard_cache_entry(merge);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002404 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002405 }
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002406 /* Migrate old flags over */
2407 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002408 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002409 }
Stefan Bellera7bc8452017-03-14 14:46:39 -07002410
Philippe Blaine84704f2020-02-17 04:53:05 +00002411 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002412 int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),
2413 oid_to_hex(&ce->oid),
2414 o);
2415 if (ret)
2416 return ret;
2417 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002418 } else {
2419 /*
2420 * Previously unmerged entry left as an existence
2421 * marker by read_index_unmerged();
2422 */
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002423 if (verify_absent_if_directory(merge,
2424 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
2425 discard_cache_entry(merge);
2426 return -1;
2427 }
2428
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002429 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002430 }
2431
Johannes Schindelincc756ed2019-09-09 13:56:15 +02002432 if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0)
2433 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002434 return 1;
2435}
2436
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002437static int deleted_entry(const struct cache_entry *ce,
2438 const struct cache_entry *old,
2439 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002440{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002441 /* Did it exist in the index? */
2442 if (!old) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002443 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002444 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002445 return 0;
Elijah Newren56d06fe2021-09-27 16:33:46 +00002446 } else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) {
2447 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002448 }
Elijah Newren56d06fe2021-09-27 16:33:46 +00002449
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002450 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
Linus Torvalds34110cd2008-03-06 18:12:28 -08002451 return -1;
2452 add_entry(o, ce, CE_REMOVE, 0);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002453 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002454 return 1;
2455}
2456
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002457static int keep_entry(const struct cache_entry *ce,
2458 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002459{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002460 add_entry(o, ce, 0, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002461 if (ce_stage(ce))
2462 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002463 return 1;
2464}
2465
2466#if DBRT_DEBUG
2467static void show_stage_entry(FILE *o,
2468 const char *label, const struct cache_entry *ce)
2469{
2470 if (!ce)
2471 fprintf(o, "%s (missing)\n", label);
2472 else
2473 fprintf(o, "%s%06o %s %d\t%s\n",
2474 label,
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002475 ce->ce_mode,
brian m. carlson99d1a982016-09-05 20:07:52 +00002476 oid_to_hex(&ce->oid),
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002477 ce_stage(ce),
2478 ce->name);
2479}
2480#endif
2481
René Scharfe5828e832013-06-02 17:46:56 +02002482int threeway_merge(const struct cache_entry * const *stages,
2483 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002484{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002485 const struct cache_entry *index;
2486 const struct cache_entry *head;
2487 const struct cache_entry *remote = stages[o->head_idx + 1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002488 int count;
2489 int head_match = 0;
2490 int remote_match = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002491
2492 int df_conflict_head = 0;
2493 int df_conflict_remote = 0;
2494
2495 int any_anc_missing = 0;
2496 int no_anc_exists = 1;
2497 int i;
2498
2499 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002500 if (!stages[i] || stages[i] == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002501 any_anc_missing = 1;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002502 else
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002503 no_anc_exists = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002504 }
2505
2506 index = stages[0];
2507 head = stages[o->head_idx];
2508
2509 if (head == o->df_conflict_entry) {
2510 df_conflict_head = 1;
2511 head = NULL;
2512 }
2513
2514 if (remote == o->df_conflict_entry) {
2515 df_conflict_remote = 1;
2516 remote = NULL;
2517 }
2518
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002519 /*
2520 * First, if there's a #16 situation, note that to prevent #13
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002521 * and #14.
2522 */
2523 if (!same(remote, head)) {
2524 for (i = 1; i < o->head_idx; i++) {
2525 if (same(stages[i], head)) {
2526 head_match = i;
2527 }
2528 if (same(stages[i], remote)) {
2529 remote_match = i;
2530 }
2531 }
2532 }
2533
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002534 /*
2535 * We start with cases where the index is allowed to match
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002536 * something other than the head: #14(ALT) and #2ALT, where it
2537 * is permitted to match the result instead.
2538 */
2539 /* #14, #14ALT, #2ALT */
2540 if (remote && !df_conflict_head && head_match && !remote_match) {
2541 if (index && !same(index, remote) && !same(index, head))
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002542 return reject_merge(index, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002543 return merged_entry(remote, index, o);
2544 }
2545 /*
2546 * If we have an entry in the index cache, then we want to
2547 * make sure that it matches head.
2548 */
Daniel Barkalow4e7c4572008-02-07 11:40:02 -05002549 if (index && !same(index, head))
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002550 return reject_merge(index, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002551
2552 if (head) {
2553 /* #5ALT, #15 */
2554 if (same(head, remote))
2555 return merged_entry(head, index, o);
2556 /* #13, #3ALT */
2557 if (!df_conflict_remote && remote_match && !head_match)
2558 return merged_entry(head, index, o);
2559 }
2560
2561 /* #1 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08002562 if (!head && !remote && any_anc_missing)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002563 return 0;
2564
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002565 /*
2566 * Under the "aggressive" rule, we resolve mostly trivial
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002567 * cases that we historically had git-merge-one-file resolve.
2568 */
2569 if (o->aggressive) {
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002570 int head_deleted = !head;
2571 int remote_deleted = !remote;
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002572 const struct cache_entry *ce = NULL;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002573
2574 if (index)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002575 ce = index;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002576 else if (head)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002577 ce = head;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002578 else if (remote)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002579 ce = remote;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002580 else {
2581 for (i = 1; i < o->head_idx; i++) {
2582 if (stages[i] && stages[i] != o->df_conflict_entry) {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002583 ce = stages[i];
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002584 break;
2585 }
2586 }
2587 }
2588
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002589 /*
2590 * Deleted in both.
2591 * Deleted in one and unchanged in the other.
2592 */
2593 if ((head_deleted && remote_deleted) ||
2594 (head_deleted && remote && remote_match) ||
2595 (remote_deleted && head && head_match)) {
2596 if (index)
2597 return deleted_entry(index, index, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08002598 if (ce && !head_deleted) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002599 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002600 return -1;
2601 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002602 return 0;
2603 }
2604 /*
2605 * Added in both, identically.
2606 */
2607 if (no_anc_exists && head && remote && same(head, remote))
2608 return merged_entry(head, index, o);
2609
2610 }
2611
2612 /* Below are "no merge" cases, which require that the index be
2613 * up-to-date to avoid the files getting overwritten with
2614 * conflict resolution files.
2615 */
2616 if (index) {
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002617 if (verify_uptodate(index, o))
2618 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002619 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002620
2621 o->nontrivial_merge = 1;
2622
Junio C Hamanoea4b52a2007-04-07 05:42:01 -07002623 /* #2, #3, #4, #6, #7, #9, #10, #11. */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002624 count = 0;
2625 if (!head_match || !remote_match) {
2626 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002627 if (stages[i] && stages[i] != o->df_conflict_entry) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002628 keep_entry(stages[i], o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002629 count++;
2630 break;
2631 }
2632 }
2633 }
2634#if DBRT_DEBUG
2635 else {
2636 fprintf(stderr, "read-tree: warning #16 detected\n");
2637 show_stage_entry(stderr, "head ", stages[head_match]);
2638 show_stage_entry(stderr, "remote ", stages[remote_match]);
2639 }
2640#endif
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002641 if (head) { count += keep_entry(head, o); }
2642 if (remote) { count += keep_entry(remote, o); }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002643 return count;
2644}
2645
2646/*
2647 * Two-way merge.
2648 *
2649 * The rule is to "carry forward" what is in the index without losing
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03002650 * information across a "fast-forward", favoring a successful merge
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002651 * over a merge failure when it makes sense. For details of the
2652 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2653 *
2654 */
René Scharfe5828e832013-06-02 17:46:56 +02002655int twoway_merge(const struct cache_entry * const *src,
2656 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002657{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002658 const struct cache_entry *current = src[0];
2659 const struct cache_entry *oldtree = src[1];
2660 const struct cache_entry *newtree = src[2];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002661
2662 if (o->merge_size != 2)
2663 return error("Cannot do a twoway merge of %d trees",
2664 o->merge_size);
2665
Junio C Hamanob8ba1532007-04-02 16:29:56 -07002666 if (oldtree == o->df_conflict_entry)
2667 oldtree = NULL;
2668 if (newtree == o->df_conflict_entry)
2669 newtree = NULL;
2670
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002671 if (current) {
Jeff Kingb018ff62012-12-29 15:51:54 -05002672 if (current->ce_flags & CE_CONFLICTED) {
2673 if (same(oldtree, newtree) || o->reset) {
2674 if (!newtree)
2675 return deleted_entry(current, current, o);
2676 else
2677 return merged_entry(newtree, current, o);
2678 }
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002679 return reject_merge(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002680 } else if ((!oldtree && !newtree) || /* 4 and 5 */
Jeff Kingb018ff62012-12-29 15:51:54 -05002681 (!oldtree && newtree &&
2682 same(current, newtree)) || /* 6 and 7 */
2683 (oldtree && newtree &&
2684 same(oldtree, newtree)) || /* 14 and 15 */
2685 (oldtree && newtree &&
2686 !same(oldtree, newtree) && /* 18 and 19 */
2687 same(current, newtree))) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002688 return keep_entry(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002689 } else if (oldtree && !newtree && same(current, oldtree)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002690 /* 10 or 11 */
2691 return deleted_entry(oldtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002692 } else if (oldtree && newtree &&
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002693 same(current, oldtree) && !same(current, newtree)) {
2694 /* 20 or 21 */
2695 return merged_entry(newtree, current, o);
Derrick Stoleee05cdb12021-07-20 20:14:41 +00002696 } else if (current && !oldtree && newtree &&
2697 S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) &&
2698 ce_stage(current) == 0) {
2699 /*
2700 * This case is a directory/file conflict across the sparse-index
2701 * boundary. When we are changing from one path to another via
2702 * 'git checkout', then we want to replace one entry with another
2703 * via merged_entry(). If there are staged changes, then we should
2704 * reject the merge instead.
2705 */
2706 return merged_entry(newtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002707 } else
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002708 return reject_merge(current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002709 }
Junio C Hamano55218832008-09-07 19:49:25 -07002710 else if (newtree) {
2711 if (oldtree && !o->initial_checkout) {
2712 /*
2713 * deletion of the path was staged;
2714 */
2715 if (same(oldtree, newtree))
2716 return 1;
2717 return reject_merge(oldtree, o);
2718 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002719 return merged_entry(newtree, current, o);
Junio C Hamano55218832008-09-07 19:49:25 -07002720 }
Linus Torvaldsd6996762007-08-10 12:31:20 -07002721 return deleted_entry(oldtree, current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002722}
2723
2724/*
2725 * Bind merge.
2726 *
2727 * Keep the index entries at stage0, collapse stage1 but make sure
2728 * stage0 does not have anything there.
2729 */
René Scharfe5828e832013-06-02 17:46:56 +02002730int bind_merge(const struct cache_entry * const *src,
2731 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002732{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002733 const struct cache_entry *old = src[0];
2734 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002735
2736 if (o->merge_size != 1)
Pete Wyckoff82247e92012-04-29 20:28:45 -04002737 return error("Cannot do a bind merge of %d trees",
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002738 o->merge_size);
2739 if (a && old)
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07002740 return o->quiet ? -1 :
Stefan Beller3d415422017-01-17 17:05:20 -08002741 error(ERRORMSG(o, ERROR_BIND_OVERLAP),
2742 super_prefixed(a->name),
2743 super_prefixed(old->name));
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002744 if (!a)
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002745 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002746 else
2747 return merged_entry(a, NULL, o);
2748}
2749
2750/*
2751 * One-way merge.
2752 *
2753 * The rule is:
2754 * - take the stat information from stage0, take the data from stage1
2755 */
René Scharfe5828e832013-06-02 17:46:56 +02002756int oneway_merge(const struct cache_entry * const *src,
2757 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002758{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002759 const struct cache_entry *old = src[0];
2760 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002761
2762 if (o->merge_size != 1)
2763 return error("Cannot do a oneway merge of %d trees",
2764 o->merge_size);
2765
Junio C Hamano78d3b062009-07-18 12:26:38 -07002766 if (!a || a == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002767 return deleted_entry(old, old, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08002768
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002769 if (old && same(old, a)) {
Linus Torvalds34110cd2008-03-06 18:12:28 -08002770 int update = 0;
Utsav Shah679f2f92019-11-20 08:32:17 +00002771 if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) &&
2772 !(old->ce_flags & CE_FSMONITOR_VALID)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002773 struct stat st;
2774 if (lstat(old->name, &st) ||
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07002775 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
Linus Torvalds34110cd2008-03-06 18:12:28 -08002776 update |= CE_UPDATE;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002777 }
Stefan Bellerad173122018-01-05 12:03:03 -08002778 if (o->update && S_ISGITLINK(old->ce_mode) &&
2779 should_update_submodules() && !verify_uptodate(old, o))
2780 update |= CE_UPDATE;
Nguyễn Thái Ngọc Duyab5af822019-03-18 18:38:22 +07002781 add_entry(o, old, update, CE_STAGEMASK);
Linus Torvalds34110cd2008-03-06 18:12:28 -08002782 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002783 }
2784 return merged_entry(a, old, o);
2785}
Denton Liud3c7bf72021-03-03 03:16:42 -08002786
2787/*
2788 * Merge worktree and untracked entries in a stash entry.
2789 *
2790 * Ignore all index entries. Collapse remaining trees but make sure that they
2791 * don't have any conflicting files.
2792 */
2793int stash_worktree_untracked_merge(const struct cache_entry * const *src,
2794 struct unpack_trees_options *o)
2795{
2796 const struct cache_entry *worktree = src[1];
2797 const struct cache_entry *untracked = src[2];
2798
2799 if (o->merge_size != 2)
2800 BUG("invalid merge_size: %d", o->merge_size);
2801
2802 if (worktree && untracked)
2803 return error(_("worktree and untracked commit have duplicate entries: %s"),
2804 super_prefixed(worktree->name));
2805
2806 return merged_entry(worktree ? worktree : untracked, NULL, o);
2807}