blob: 471837f0329d63a2fdacef1427874a5a59dd0415 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
Patrick Steinhardt41f43b82024-12-06 11:27:19 +01002#define DISABLE_SIGN_COMPARE_WARNINGS
Patrick Steinhardte7da9382024-06-14 08:50:23 +02003
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00004#include "git-compat-util.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +00005#include "advice.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04006#include "strvec.h"
Brandon Williams33028712017-08-03 11:19:59 -07007#include "repository.h"
Calvin Wanb1bda752023-09-29 14:20:51 -07008#include "parse.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -08009#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000010#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000011#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000012#include "hex.h"
Elijah Newrenf5653852023-05-16 06:33:50 +000013#include "name-hash.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020014#include "tree.h"
15#include "tree-walk.h"
Johannes Schindelin076b0ad2006-07-30 20:26:15 +020016#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020017#include "unpack-trees.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040018#include "progress.h"
Sven Verdoolaege0cf73752007-07-17 20:28:28 +020019#include "refs.h"
Junio C Hamano06f33c12009-03-13 21:24:08 -070020#include "attr.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000021#include "read-cache.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070022#include "split-index.h"
Victoria Dye0f329b92022-05-10 23:32:32 +000023#include "sparse-index.h"
Stefan Bellera7bc8452017-03-14 14:46:39 -070024#include "submodule.h"
25#include "submodule-config.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000026#include "symlinks.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000027#include "trace2.h"
Ben Peart883e2482017-09-22 12:35:40 -040028#include "fsmonitor.h"
Patrick Steinhardt68cd4922025-04-15 11:38:23 +020029#include "object-store.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020030#include "promisor-remote.h"
Matheus Tavaresd052cc02021-03-23 11:19:32 -030031#include "entry.h"
Matheus Tavares04155bd2021-04-18 21:14:53 -030032#include "parallel-checkout.h"
Elijah Newrene38da482023-03-21 06:26:05 +000033#include "setup.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020034
Junio C Hamano8ccba002008-05-17 12:03:49 -070035/*
36 * Error messages expected by scripts out of plumbing commands such as
37 * read-tree. Non-scripted Porcelain is not required to use these messages
38 * and in fact are encouraged to reword them to better suit their particular
Diane Gasselin23cbf112010-08-11 10:38:05 +020039 * situation better. See how "git checkout" and "git merge" replaces
Matthieu Moydc1166e2010-09-02 13:57:33 +020040 * them using setup_unpack_trees_porcelain(), for example.
Junio C Hamano8ccba002008-05-17 12:03:49 -070041 */
Elijah Newren6271d772020-03-27 00:48:57 +000042static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
Matthieu Moy08353eb2010-08-11 10:38:04 +020043 /* ERROR_WOULD_OVERWRITE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070044 "Entry '%s' would be overwritten by merge. Cannot merge.",
45
Matthieu Moy08353eb2010-08-11 10:38:04 +020046 /* ERROR_NOT_UPTODATE_FILE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070047 "Entry '%s' not uptodate. Cannot merge.",
48
Matthieu Moy08353eb2010-08-11 10:38:04 +020049 /* ERROR_NOT_UPTODATE_DIR */
Junio C Hamano8ccba002008-05-17 12:03:49 -070050 "Updating '%s' would lose untracked files in it",
51
Elijah Newrenb817e542021-12-09 05:08:27 +000052 /* ERROR_CWD_IN_THE_WAY */
53 "Refusing to remove '%s' since it is the current working directory.",
54
Matthieu Moy08402b02010-08-11 10:38:06 +020055 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
56 "Untracked working tree file '%s' would be overwritten by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070057
Matthieu Moy08402b02010-08-11 10:38:06 +020058 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
59 "Untracked working tree file '%s' would be removed by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070060
Matthieu Moy08353eb2010-08-11 10:38:04 +020061 /* ERROR_BIND_OVERLAP */
Junio C Hamano8ccba002008-05-17 12:03:49 -070062 "Entry '%s' overlaps with '%s'. Cannot bind.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070063
Stefan Bellera7bc8452017-03-14 14:46:39 -070064 /* ERROR_WOULD_LOSE_SUBMODULE */
65 "Submodule '%s' cannot checkout new HEAD.",
Elijah Newrencd002c12020-03-27 00:48:55 +000066
Elijah Newren6271d772020-03-27 00:48:57 +000067 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
68 "",
69
Elijah Newren1ac83f42020-03-27 00:48:56 +000070 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
Elijah Newren22ab0b32020-03-27 00:48:58 +000071 "Path '%s' not uptodate; will not remove from working tree.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070072
Elijah Newrenebb568b2020-03-27 00:48:59 +000073 /* WARNING_SPARSE_UNMERGED_FILE */
74 "Path '%s' unmerged; will not remove from working tree.",
75
Elijah Newren1ac83f42020-03-27 00:48:56 +000076 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
Elijah Newren22ab0b32020-03-27 00:48:58 +000077 "Path '%s' already present; will not overwrite with sparse update.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070078};
79
Matthieu Moy08353eb2010-08-11 10:38:04 +020080#define ERRORMSG(o,type) \
Elijah Newren13e1fd62023-02-27 15:28:17 +000081 ( ((o) && (o)->internal.msgs[(type)]) \
82 ? ((o)->internal.msgs[(type)]) \
Matthieu Moy08353eb2010-08-11 10:38:04 +020083 : (unpack_plumbing_errors[(type)]) )
Junio C Hamano8ccba002008-05-17 12:03:49 -070084
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +010085static const char *super_prefixed(const char *path, const char *super_prefix)
Stefan Beller3d415422017-01-17 17:05:20 -080086{
87 /*
88 * It is necessary and sufficient to have two static buffers
89 * here, as the return value of this function is fed to
90 * error() using the unpack_*_errors[] templates we see above.
91 */
92 static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};
93 static int super_prefix_len = -1;
94 static unsigned idx = ARRAY_SIZE(buf) - 1;
95
96 if (super_prefix_len < 0) {
Stefan Beller3d415422017-01-17 17:05:20 -080097 if (!super_prefix) {
98 super_prefix_len = 0;
99 } else {
100 int i;
101 for (i = 0; i < ARRAY_SIZE(buf); i++)
102 strbuf_addstr(&buf[i], super_prefix);
103 super_prefix_len = buf[0].len;
104 }
105 }
106
107 if (!super_prefix_len)
108 return path;
109
110 if (++idx >= ARRAY_SIZE(buf))
111 idx = 0;
112
113 strbuf_setlen(&buf[idx], super_prefix_len);
114 strbuf_addstr(&buf[idx], path);
115
116 return buf[idx].buf;
117}
118
Matthieu Moye2940302010-09-02 13:57:34 +0200119void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
120 const char *cmd)
Matthieu Moydc1166e2010-09-02 13:57:33 +0200121{
Clemens Buchacher79808722010-11-15 20:52:19 +0100122 int i;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000123 const char **msgs = opts->internal.msgs;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200124 const char *msg;
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400125
Elijah Newren13e1fd62023-02-27 15:28:17 +0000126 strvec_init(&opts->internal.msgs_to_free);
Martin Ågren1c41d282018-05-21 16:54:28 +0200127
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000128 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200129 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000130 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600131 "Please commit your changes or stash them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000132 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
133 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200134 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000135 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600136 "Please commit your changes or stash them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000137 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200138 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200139 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000140 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600141 "Please commit your changes or stash them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000142 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400143 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000144 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200145
146 msgs[ERROR_NOT_UPTODATE_DIR] =
Stefan Beller584f99c2016-12-02 11:17:41 -0800147 _("Updating the following directories would lose untracked files in them:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200148
Elijah Newrenb817e542021-12-09 05:08:27 +0000149 msgs[ERROR_CWD_IN_THE_WAY] =
150 _("Refusing to remove the current working directory:\n%s");
151
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000152 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200153 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000154 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600155 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000156 : _("The following untracked working tree files would be removed by checkout:\n%%s");
157 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200158 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000159 ? _("The following untracked working tree files would be removed by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600160 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000161 : _("The following untracked working tree files would be removed by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200162 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200163 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000164 ? _("The following untracked working tree files would be removed by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600165 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000166 : _("The following untracked working tree files would be removed by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200167 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000168 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400169
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000170 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200171 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000172 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600173 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000174 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
175 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200176 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000177 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600178 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000179 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
180 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200181 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000182 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600183 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000184 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200185 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000186 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200187
188 /*
189 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
190 * cannot easily display it as a list.
191 */
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000192 msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200193
Stefan Bellera7bc8452017-03-14 14:46:39 -0700194 msgs[ERROR_WOULD_LOSE_SUBMODULE] =
Stefan Beller6362ed02017-03-29 15:34:24 -0700195 _("Cannot update submodule:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200196
Elijah Newren1ac83f42020-03-27 00:48:56 +0000197 msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000198 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
Elijah Newrenebb568b2020-03-27 00:48:59 +0000199 msgs[WARNING_SPARSE_UNMERGED_FILE] =
200 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
Elijah Newren1ac83f42020-03-27 00:48:56 +0000201 msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000202 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200203
Elijah Newren13e1fd62023-02-27 15:28:17 +0000204 opts->internal.show_all_errors = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100205 /* rejected paths may not have a static buffer */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000206 for (i = 0; i < ARRAY_SIZE(opts->internal.unpack_rejects); i++)
207 opts->internal.unpack_rejects[i].strdup_strings = 1;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200208}
209
Martin Ågren1c41d282018-05-21 16:54:28 +0200210void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
211{
Elijah Newren13e1fd62023-02-27 15:28:17 +0000212 strvec_clear(&opts->internal.msgs_to_free);
213 memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs));
Patrick Steinhardt4dfd4f12024-08-14 08:52:39 +0200214 discard_index(&opts->internal.result);
Martin Ågren1c41d282018-05-21 16:54:28 +0200215}
216
Jeff King46169182014-11-24 13:36:51 -0500217static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
René Scharfe6ff264e2012-03-06 20:37:23 +0100218 unsigned int set, unsigned int clear)
219{
Karsten Blees419a5972013-11-14 20:22:27 +0100220 clear |= CE_HASHED;
René Scharfe6ff264e2012-03-06 20:37:23 +0100221
222 if (set & CE_REMOVE)
223 set |= CE_WT_REMOVE;
224
René Scharfe6ff264e2012-03-06 20:37:23 +0100225 ce->ce_flags = (ce->ce_flags & ~clear) | set;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000226 return add_index_entry(&o->internal.result, ce,
Jeff King46169182014-11-24 13:36:51 -0500227 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
René Scharfe6ff264e2012-03-06 20:37:23 +0100228}
229
René Scharfea33bd4d2013-06-02 17:46:53 +0200230static void add_entry(struct unpack_trees_options *o,
231 const struct cache_entry *ce,
232 unsigned int set, unsigned int clear)
233{
Elijah Newren13e1fd62023-02-27 15:28:17 +0000234 do_add_entry(o, dup_cache_entry(ce, &o->internal.result), set, clear);
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700235}
236
Kjetil Barvik78478922009-02-09 21:54:07 +0100237/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200238 * add error messages on path <path>
239 * corresponding to the type <e> with the message <msg>
240 * indicating if it should be display in porcelain or not
241 */
242static int add_rejected_path(struct unpack_trees_options *o,
243 enum unpack_trees_error_types e,
244 const char *path)
245{
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +0700246 if (o->quiet)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700247 return -1;
248
Elijah Newren13e1fd62023-02-27 15:28:17 +0000249 if (!o->internal.show_all_errors)
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100250 return error(ERRORMSG(o, e), super_prefixed(path,
251 o->super_prefix));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200252
253 /*
254 * Otherwise, insert in a list for future display by
Elijah Newren6271d772020-03-27 00:48:57 +0000255 * display_(error|warning)_msgs()
Matthieu Moye6c111b2010-08-11 10:38:07 +0200256 */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000257 string_list_append(&o->internal.unpack_rejects[e], path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200258 return -1;
259}
260
261/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200262 * display all the error messages stored in a nice way
263 */
264static void display_error_msgs(struct unpack_trees_options *o)
265{
Elijah Newren6271d772020-03-27 00:48:57 +0000266 int e;
267 unsigned error_displayed = 0;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200268 for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
Elijah Newren13e1fd62023-02-27 15:28:17 +0000269 struct string_list *rejects = &o->internal.unpack_rejects[e];
Elijah Newren6271d772020-03-27 00:48:57 +0000270
Clemens Buchacher79808722010-11-15 20:52:19 +0100271 if (rejects->nr > 0) {
Elijah Newren6271d772020-03-27 00:48:57 +0000272 int i;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200273 struct strbuf path = STRBUF_INIT;
Elijah Newren6271d772020-03-27 00:48:57 +0000274
275 error_displayed = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100276 for (i = 0; i < rejects->nr; i++)
277 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100278 error(ERRORMSG(o, e), super_prefixed(path.buf,
279 o->super_prefix));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200280 strbuf_release(&path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200281 }
Clemens Buchacher79808722010-11-15 20:52:19 +0100282 string_list_clear(rejects, 0);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200283 }
Elijah Newren6271d772020-03-27 00:48:57 +0000284 if (error_displayed)
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000285 fprintf(stderr, _("Aborting\n"));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200286}
287
Elijah Newren6271d772020-03-27 00:48:57 +0000288/*
289 * display all the warning messages stored in a nice way
290 */
291static void display_warning_msgs(struct unpack_trees_options *o)
292{
293 int e;
294 unsigned warning_displayed = 0;
295 for (e = NB_UNPACK_TREES_ERROR_TYPES + 1;
296 e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
Elijah Newren13e1fd62023-02-27 15:28:17 +0000297 struct string_list *rejects = &o->internal.unpack_rejects[e];
Elijah Newren6271d772020-03-27 00:48:57 +0000298
299 if (rejects->nr > 0) {
300 int i;
301 struct strbuf path = STRBUF_INIT;
302
303 warning_displayed = 1;
304 for (i = 0; i < rejects->nr; i++)
305 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100306 warning(ERRORMSG(o, e), super_prefixed(path.buf,
307 o->super_prefix));
Elijah Newren6271d772020-03-27 00:48:57 +0000308 strbuf_release(&path);
309 }
310 string_list_clear(rejects, 0);
311 }
312 if (warning_displayed)
313 fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
314}
Stefan Bellera7bc8452017-03-14 14:46:39 -0700315static int check_submodule_move_head(const struct cache_entry *ce,
316 const char *old_id,
317 const char *new_id,
318 struct unpack_trees_options *o)
319{
Stefan Bellercd279e22017-04-18 14:37:22 -0700320 unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700321 const struct submodule *sub = submodule_from_ce(ce);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700322
Stefan Bellera7bc8452017-03-14 14:46:39 -0700323 if (!sub)
324 return 0;
325
Stefan Bellercd279e22017-04-18 14:37:22 -0700326 if (o->reset)
327 flags |= SUBMODULE_MOVE_HEAD_FORCE;
328
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100329 if (submodule_move_head(ce->name, o->super_prefix, old_id, new_id,
330 flags))
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700331 return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700332 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700333}
334
Brandon Williams33028712017-08-03 11:19:59 -0700335/*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000336 * Perform the loading of the repository's gitmodules file. This function is
Brandon Williams33028712017-08-03 11:19:59 -0700337 * used by 'check_update()' to perform loading of the gitmodules file in two
Elijah Newren15beaaa2019-11-05 17:07:23 +0000338 * different situations:
Brandon Williams33028712017-08-03 11:19:59 -0700339 * (1) before removing entries from the working tree if the gitmodules file has
340 * been marked for removal. This situation is specified by 'state' == NULL.
341 * (2) before checking out entries to the working tree if the gitmodules file
342 * has been marked for update. This situation is specified by 'state' != NULL.
343 */
344static void load_gitmodules_file(struct index_state *index,
345 struct checkout *state)
Stefan Bellera7bc8452017-03-14 14:46:39 -0700346{
Brandon Williams33028712017-08-03 11:19:59 -0700347 int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
348
349 if (pos >= 0) {
350 struct cache_entry *ce = index->cache[pos];
351 if (!state && ce->ce_flags & CE_WT_REMOVE) {
Matheus Tavaresd7992422020-01-15 23:39:55 -0300352 repo_read_gitmodules(the_repository, 0);
Brandon Williams33028712017-08-03 11:19:59 -0700353 } else if (state && (ce->ce_flags & CE_UPDATE)) {
Stefan Bellerf793b892018-03-28 15:35:28 -0700354 submodule_free(the_repository);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100355 checkout_entry(ce, state, NULL, NULL);
Matheus Tavaresd7992422020-01-15 23:39:55 -0300356 repo_read_gitmodules(the_repository, 0);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700357 }
358 }
359}
360
Elijah Newren6c342392020-05-14 19:53:22 +0000361static struct progress *get_progress(struct unpack_trees_options *o,
362 struct index_state *index)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200363{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400364 unsigned cnt = 0, total = 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200365
Stefan Beller384f1a12017-01-09 11:46:19 -0800366 if (!o->update || !o->verbose_update)
367 return NULL;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200368
Stefan Beller384f1a12017-01-09 11:46:19 -0800369 for (; cnt < index->cache_nr; cnt++) {
370 const struct cache_entry *ce = index->cache[cnt];
371 if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
372 total++;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200373 }
374
Patrick Steinhardt1f7e6472024-12-17 07:43:48 +0100375 return start_delayed_progress(the_repository,
376 _("Updating files"), total);
Stefan Beller384f1a12017-01-09 11:46:19 -0800377}
378
Duy Nguyenb8785792018-08-17 20:00:39 +0200379static void setup_collided_checkout_detection(struct checkout *state,
380 struct index_state *index)
381{
382 int i;
383
384 state->clone = 1;
385 for (i = 0; i < index->cache_nr; i++)
386 index->cache[i]->ce_flags &= ~CE_MATCHED;
387}
388
389static void report_collided_checkout(struct index_state *index)
390{
391 struct string_list list = STRING_LIST_INIT_NODUP;
392 int i;
393
394 for (i = 0; i < index->cache_nr; i++) {
395 struct cache_entry *ce = index->cache[i];
396
397 if (!(ce->ce_flags & CE_MATCHED))
398 continue;
399
400 string_list_append(&list, ce->name);
401 ce->ce_flags &= ~CE_MATCHED;
402 }
403
404 list.cmp = fspathcmp;
405 string_list_sort(&list);
406
407 if (list.nr) {
408 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
409 "on a case-insensitive filesystem) and only one from the same\n"
410 "colliding group is in the working tree:\n"));
411
412 for (i = 0; i < list.nr; i++)
413 fprintf(stderr, " '%s'\n", list.items[i].string);
414 }
415
416 string_list_clear(&list, 0);
417}
418
Jonathan Tanb2896d22021-07-23 11:52:22 -0700419static int must_checkout(const struct cache_entry *ce)
420{
421 return ce->ce_flags & CE_UPDATE;
422}
423
Elijah Newrenb0a5a122020-03-27 00:48:49 +0000424static int check_updates(struct unpack_trees_options *o,
425 struct index_state *index)
Stefan Beller384f1a12017-01-09 11:46:19 -0800426{
427 unsigned cnt = 0;
Stefan Beller30ac2752017-01-09 11:46:17 -0800428 int errs = 0;
Carlo Marcelo Arenas Belón07f967a2018-10-18 11:46:04 -0700429 struct progress *progress;
Stefan Beller30ac2752017-01-09 11:46:17 -0800430 struct checkout state = CHECKOUT_INIT;
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300431 int i, pc_workers, pc_threshold;
Stefan Beller30ac2752017-01-09 11:46:17 -0800432
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200433 trace_performance_enter();
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100434 state.super_prefix = o->super_prefix;
Stefan Beller30ac2752017-01-09 11:46:17 -0800435 state.force = 1;
436 state.quiet = 1;
437 state.refresh_cache = 1;
438 state.istate = index;
brian m. carlson13e7ed62020-03-16 18:05:04 +0000439 clone_checkout_metadata(&state.meta, &o->meta, NULL);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200440
Elijah Newren26f924d2020-01-07 06:57:57 +0000441 if (!o->update || o->dry_run) {
442 remove_marked_cache_entries(index, 0);
443 trace_performance_leave("check_updates");
444 return 0;
445 }
446
Duy Nguyenb8785792018-08-17 20:00:39 +0200447 if (o->clone)
448 setup_collided_checkout_detection(&state, index);
449
Elijah Newren6c342392020-05-14 19:53:22 +0000450 progress = get_progress(o, index);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200451
Matheus Tavares22539ec2021-02-02 22:37:10 +0100452 /* Start with clean cache to avoid using any possibly outdated info. */
453 invalidate_lstat_cache();
454
Elijah Newren26f924d2020-01-07 06:57:57 +0000455 git_attr_set_direction(GIT_ATTR_CHECKOUT);
Brandon Williams33028712017-08-03 11:19:59 -0700456
Elijah Newren26f924d2020-01-07 06:57:57 +0000457 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700458 load_gitmodules_file(index, NULL);
459
Linus Torvalds34110cd2008-03-06 18:12:28 -0800460 for (i = 0; i < index->cache_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700461 const struct cache_entry *ce = index->cache[i];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200462
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700463 if (ce->ce_flags & CE_WT_REMOVE) {
464 display_progress(progress, ++cnt);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100465 unlink_entry(ce, o->super_prefix);
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700466 }
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700467 }
Elijah Newren26f924d2020-01-07 06:57:57 +0000468
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000469 remove_marked_cache_entries(index, 0);
Kjetil Barvik78478922009-02-09 21:54:07 +0100470 remove_scheduled_dirs();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700471
Elijah Newren26f924d2020-01-07 06:57:57 +0000472 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700473 load_gitmodules_file(index, &state);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700474
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200475 if (repo_has_promisor_remote(the_repository))
Jonathan Tanc0c578b2017-12-08 15:58:47 +0000476 /*
477 * Prefetch the objects that are to be checked out in the loop
478 * below.
479 */
Jonathan Tanb2896d22021-07-23 11:52:22 -0700480 prefetch_cache_entries(index, must_checkout);
Matheus Tavares04155bd2021-04-18 21:14:53 -0300481
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300482 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
483
Matheus Tavares04155bd2021-04-18 21:14:53 -0300484 enable_delayed_checkout(&state);
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300485 if (pc_workers > 1)
486 init_parallel_checkout();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700487 for (i = 0; i < index->cache_nr; i++) {
488 struct cache_entry *ce = index->cache[i];
489
Jonathan Tanb2896d22021-07-23 11:52:22 -0700490 if (must_checkout(ce)) {
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300491 size_t last_pc_queue_size = pc_queue_size();
492
David Turner7d782412015-07-17 17:19:27 -0400493 if (ce->ce_flags & CE_WT_REMOVE)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200494 BUG("both update and delete flags are set on %s",
David Turner7d782412015-07-17 17:19:27 -0400495 ce->name);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800496 ce->ce_flags &= ~CE_UPDATE;
Elijah Newren26f924d2020-01-07 06:57:57 +0000497 errs |= checkout_entry(ce, &state, NULL, NULL);
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300498
499 if (last_pc_queue_size == pc_queue_size())
500 display_progress(progress, ++cnt);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200501 }
502 }
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300503 if (pc_workers > 1)
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300504 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
505 progress, &cnt);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400506 stop_progress(&progress);
Matheus Tavares611c7782022-07-14 08:49:12 -0300507 errs |= finish_delayed_checkout(&state, o->verbose_update);
Elijah Newren26f924d2020-01-07 06:57:57 +0000508 git_attr_set_direction(GIT_ATTR_CHECKIN);
Duy Nguyenb8785792018-08-17 20:00:39 +0200509
510 if (o->clone)
511 report_collided_checkout(index);
512
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200513 trace_performance_leave("check_updates");
Junio C Hamanoc4758d32008-03-18 22:01:28 -0700514 return errs != 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200515}
516
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200517static int verify_uptodate_sparse(const struct cache_entry *ce,
518 struct unpack_trees_options *o);
519static int verify_absent_sparse(const struct cache_entry *ce,
520 enum unpack_trees_error_types,
521 struct unpack_trees_options *o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700522
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700523static int apply_sparse_checkout(struct index_state *istate,
524 struct cache_entry *ce,
525 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700526{
527 int was_skip_worktree = ce_skip_worktree(ce);
528
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700529 if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700530 ce->ce_flags |= CE_SKIP_WORKTREE;
531 else
532 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700533 if (was_skip_worktree != ce_skip_worktree(ce)) {
534 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400535 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700536 istate->cache_changed |= CE_ENTRY_CHANGED;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700537 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700538
539 /*
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700540 * if (!was_skip_worktree && !ce_skip_worktree()) {
541 * This is perfectly normal. Move on;
542 * }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700543 */
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700544
545 /*
546 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
547 * area as a result of ce_skip_worktree() shortcuts in
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700548 * verify_absent() and verify_uptodate().
549 * Make sure they don't modify worktree if they are already
550 * outside checkout area
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700551 */
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700552 if (was_skip_worktree && ce_skip_worktree(ce)) {
553 ce->ce_flags &= ~CE_UPDATE;
554
555 /*
556 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
557 * on to get that file removed from both index and worktree.
558 * If that file is already outside worktree area, don't
559 * bother remove it.
560 */
561 if (ce->ce_flags & CE_REMOVE)
562 ce->ce_flags &= ~CE_WT_REMOVE;
563 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700564
565 if (!was_skip_worktree && ce_skip_worktree(ce)) {
566 /*
567 * If CE_UPDATE is set, verify_uptodate() must be called already
568 * also stat info may have lost after merged_entry() so calling
569 * verify_uptodate() again may fail
570 */
Elijah Newren3cc7c502020-03-27 00:48:50 +0000571 if (!(ce->ce_flags & CE_UPDATE) &&
572 verify_uptodate_sparse(ce, o)) {
573 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700574 return -1;
Elijah Newren3cc7c502020-03-27 00:48:50 +0000575 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700576 ce->ce_flags |= CE_WT_REMOVE;
David Turner7d782412015-07-17 17:19:27 -0400577 ce->ce_flags &= ~CE_UPDATE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700578 }
579 if (was_skip_worktree && !ce_skip_worktree(ce)) {
Elijah Newren1ac83f42020-03-27 00:48:56 +0000580 if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700581 return -1;
582 ce->ce_flags |= CE_UPDATE;
583 }
584 return 0;
585}
586
Elijah Newrenebb568b2020-03-27 00:48:59 +0000587static int warn_conflicted_path(struct index_state *istate,
588 int i,
589 struct unpack_trees_options *o)
590{
591 char *conflicting_path = istate->cache[i]->name;
592 int count = 0;
593
594 add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path);
595
Derrick Stolee0eeb3be2020-05-07 13:17:33 +0000596 /* Find out how many higher stage entries are at same path */
597 while ((++count) + i < istate->cache_nr &&
598 !strcmp(conflicting_path, istate->cache[count + i]->name))
599 ; /* do nothing */
600
Elijah Newrenebb568b2020-03-27 00:48:59 +0000601 return count;
602}
603
René Scharfe5828e832013-06-02 17:46:56 +0200604static inline int call_unpack_fn(const struct cache_entry * const *src,
605 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800606{
Linus Torvalds34110cd2008-03-06 18:12:28 -0800607 int ret = o->fn(src, o);
608 if (ret > 0)
Linus Torvalds01904572008-03-05 20:15:44 -0800609 ret = 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800610 return ret;
611}
612
Junio C Hamanoda165f42010-01-07 14:59:54 -0800613static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
614{
615 ce->ce_flags |= CE_UNPACKED;
616
Elijah Newren13e1fd62023-02-27 15:28:17 +0000617 if (o->internal.cache_bottom < o->src_index->cache_nr &&
618 o->src_index->cache[o->internal.cache_bottom] == ce) {
619 int bottom = o->internal.cache_bottom;
620
Junio C Hamanoda165f42010-01-07 14:59:54 -0800621 while (bottom < o->src_index->cache_nr &&
622 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
623 bottom++;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000624 o->internal.cache_bottom = bottom;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800625 }
626}
627
628static void mark_all_ce_unused(struct index_state *index)
629{
630 int i;
631 for (i = 0; i < index->cache_nr; i++)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700632 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
Junio C Hamanoda165f42010-01-07 14:59:54 -0800633}
634
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200635static int locate_in_src_index(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800636 struct unpack_trees_options *o)
637{
638 struct index_state *index = o->src_index;
639 int len = ce_namelen(ce);
640 int pos = index_name_pos(index, ce->name, len);
641 if (pos < 0)
642 pos = -1 - pos;
643 return pos;
644}
645
646/*
647 * We call unpack_index_entry() with an unmerged cache entry
648 * only in diff-index, and it wants a single callback. Skip
649 * the other unmerged entry with the same name.
650 */
651static void mark_ce_used_same_name(struct cache_entry *ce,
652 struct unpack_trees_options *o)
653{
654 struct index_state *index = o->src_index;
655 int len = ce_namelen(ce);
656 int pos;
657
658 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
659 struct cache_entry *next = index->cache[pos];
660 if (len != ce_namelen(next) ||
661 memcmp(ce->name, next->name, len))
662 break;
663 mark_ce_used(next, o);
664 }
665}
666
Victoria Dye99430aa2022-03-17 15:55:36 +0000667static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
Junio C Hamanoda165f42010-01-07 14:59:54 -0800668{
669 const struct index_state *index = o->src_index;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000670 int pos = o->internal.cache_bottom;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800671
672 while (pos < index->cache_nr) {
673 struct cache_entry *ce = index->cache[pos];
Victoria Dye99430aa2022-03-17 15:55:36 +0000674 if (!(ce->ce_flags & CE_UNPACKED))
Junio C Hamanoda165f42010-01-07 14:59:54 -0800675 return ce;
676 pos++;
677 }
678 return NULL;
679}
680
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700681static void add_same_unmerged(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800682 struct unpack_trees_options *o)
683{
684 struct index_state *index = o->src_index;
685 int len = ce_namelen(ce);
686 int pos = index_name_pos(index, ce->name, len);
687
688 if (0 <= pos)
689 die("programming error in a caller of mark_ce_used_same_name");
690 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
691 struct cache_entry *next = index->cache[pos];
692 if (len != ce_namelen(next) ||
693 memcmp(ce->name, next->name, len))
694 break;
695 add_entry(o, next, 0, 0);
696 mark_ce_used(next, o);
697 }
698}
699
700static int unpack_index_entry(struct cache_entry *ce,
701 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800702{
René Scharfe5828e832013-06-02 17:46:56 +0200703 const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Junio C Hamanoda165f42010-01-07 14:59:54 -0800704 int ret;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800705
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000706 src[0] = ce;
707
Junio C Hamanoda165f42010-01-07 14:59:54 -0800708 mark_ce_used(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -0800709 if (ce_stage(ce)) {
710 if (o->skip_unmerged) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800711 add_entry(o, ce, 0, 0);
712 return 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800713 }
Linus Torvalds01904572008-03-05 20:15:44 -0800714 }
Junio C Hamanoda165f42010-01-07 14:59:54 -0800715 ret = call_unpack_fn(src, o);
716 if (ce_stage(ce))
717 mark_ce_used_same_name(ce, o);
718 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800719}
720
Jeff King90553842019-07-31 00:38:15 -0400721static int find_cache_pos(struct traverse_info *, const char *p, size_t len);
Junio C Hamano730f7282009-09-20 00:03:39 -0700722
723static void restore_cache_bottom(struct traverse_info *info, int bottom)
724{
725 struct unpack_trees_options *o = info->data;
726
727 if (o->diff_index_cached)
728 return;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000729 o->internal.cache_bottom = bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -0700730}
731
732static int switch_cache_bottom(struct traverse_info *info)
733{
734 struct unpack_trees_options *o = info->data;
735 int ret, pos;
736
737 if (o->diff_index_cached)
738 return 0;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000739 ret = o->internal.cache_bottom;
Jeff King90553842019-07-31 00:38:15 -0400740 pos = find_cache_pos(info->prev, info->name, info->namelen);
Junio C Hamano730f7282009-09-20 00:03:39 -0700741
742 if (pos < -1)
Elijah Newren13e1fd62023-02-27 15:28:17 +0000743 o->internal.cache_bottom = -2 - pos;
Junio C Hamano730f7282009-09-20 00:03:39 -0700744 else if (pos < 0)
Elijah Newren13e1fd62023-02-27 15:28:17 +0000745 o->internal.cache_bottom = o->src_index->cache_nr;
Junio C Hamano730f7282009-09-20 00:03:39 -0700746 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800747}
748
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000749static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)
750{
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000751 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 +0000752}
753
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200754static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,
755 struct name_entry *names,
756 struct traverse_info *info)
757{
758 struct unpack_trees_options *o = info->data;
759 int i;
760
761 if (!o->merge || dirmask != ((1 << n) - 1))
762 return 0;
763
764 for (i = 1; i < n; i++)
765 if (!are_same_oid(names, names + i))
766 return 0;
767
768 return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);
769}
770
771static int index_pos_by_traverse_info(struct name_entry *names,
772 struct traverse_info *info)
773{
774 struct unpack_trees_options *o = info->data;
Jeff Kingc43ab062019-07-31 00:38:23 -0400775 struct strbuf name = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200776 int pos;
777
Jeff Kingc43ab062019-07-31 00:38:23 -0400778 strbuf_make_traverse_path(&name, info, names->path, names->pathlen);
779 strbuf_addch(&name, '/');
780 pos = index_name_pos(o->src_index, name.buf, name.len);
Derrick Stolee13e13312021-03-30 13:10:57 +0000781 if (pos >= 0) {
782 if (!o->src_index->sparse_index ||
783 !(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE))
784 BUG("This is a directory and should not exist in index");
785 } else {
786 pos = -pos - 1;
787 }
Emily Shaffer573117d2020-01-07 18:31:27 -0800788 if (pos >= o->src_index->cache_nr ||
789 !starts_with(o->src_index->cache[pos]->name, name.buf) ||
Jeff Kingc43ab062019-07-31 00:38:23 -0400790 (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf)))
Emily Shaffer573117d2020-01-07 18:31:27 -0800791 BUG("pos %d doesn't point to the first entry of %s in index",
792 pos, name.buf);
Jeff Kingc43ab062019-07-31 00:38:23 -0400793 strbuf_release(&name);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200794 return pos;
795}
796
797/*
798 * Fast path if we detect that all trees are the same as cache-tree at this
Nguyễn Thái Ngọc Duy5f4436a2018-08-25 15:02:09 +0200799 * path. We'll walk these trees in an iterative loop using cache-tree/index
800 * instead of ODB since we already know what these trees contain.
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200801 */
802static 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 +0200803 struct traverse_info *info)
804{
805 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
806 struct unpack_trees_options *o = info->data;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200807 struct cache_entry *tree_ce = NULL;
808 int ce_len = 0;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200809 int i, d;
810
811 if (!o->merge)
812 BUG("We need cache-tree to do this optimization");
Patrick Steinhardtecb5c432024-10-07 06:38:21 +0200813 if (nr_entries + pos > o->src_index->cache_nr)
814 return error(_("corrupted cache-tree has entries not present in index"));
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200815
816 /*
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +0000817 * Do what unpack_callback() and unpack_single_entry() normally
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200818 * do. But we walk all paths in an iterative loop instead.
819 *
820 * D/F conflicts and higher stage entries are not a concern
821 * because cache-tree would be invalidated and we would never
822 * get here in the first place.
823 */
824 for (i = 0; i < nr_entries; i++) {
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200825 int new_ce_len, len, rc;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200826
827 src[0] = o->src_index->cache[pos + i];
828
829 len = ce_namelen(src[0]);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200830 new_ce_len = cache_entry_size(len);
831
832 if (new_ce_len > ce_len) {
833 new_ce_len <<= 1;
834 tree_ce = xrealloc(tree_ce, new_ce_len);
835 memset(tree_ce, 0, new_ce_len);
836 ce_len = new_ce_len;
837
838 tree_ce->ce_flags = create_ce_flags(0);
839
840 for (d = 1; d <= nr_names; d++)
841 src[d] = tree_ce;
842 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200843
844 tree_ce->ce_mode = src[0]->ce_mode;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200845 tree_ce->ce_namelen = len;
846 oidcpy(&tree_ce->oid, &src[0]->oid);
847 memcpy(tree_ce->name, src[0]->name, len + 1);
848
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200849 rc = call_unpack_fn((const struct cache_entry * const *)src, o);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200850 if (rc < 0) {
851 free(tree_ce);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200852 return rc;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200853 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200854
855 mark_ce_used(src[0], o);
856 }
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200857 free(tree_ce);
Elijah Newren1ca13dd2023-02-27 15:28:19 +0000858 if (o->internal.debug_unpack)
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200859 printf("Unpacked %d entries from %s to %s using cache-tree\n",
860 nr_entries,
861 o->src_index->cache[pos]->name,
862 o->src_index->cache[pos + nr_entries - 1]->name);
863 return 0;
864}
865
Junio C Hamano84563a62010-12-22 09:09:55 -0800866static int traverse_trees_recursive(int n, unsigned long dirmask,
867 unsigned long df_conflicts,
868 struct name_entry *names,
869 struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -0800870{
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100871 struct unpack_trees_options *o = info->data;
Junio C Hamano730f7282009-09-20 00:03:39 -0700872 int i, ret, bottom;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000873 int nr_buf = 0;
Jeff King59c4c7d2023-08-31 02:17:54 -0400874 struct tree_desc *t;
875 void **buf;
Linus Torvalds01904572008-03-05 20:15:44 -0800876 struct traverse_info newinfo;
877 struct name_entry *p;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200878 int nr_entries;
879
880 nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);
881 if (nr_entries > 0) {
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200882 int pos = index_pos_by_traverse_info(names, info);
883
884 if (!o->merge || df_conflicts)
885 BUG("Wrong condition to get here buddy");
886
887 /*
888 * All entries up to 'pos' must have been processed
889 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
890 * save and restore cache_bottom anyway to not miss
891 * unprocessed entries before 'pos'.
892 */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000893 bottom = o->internal.cache_bottom;
Jeff King66429692019-03-20 04:15:07 -0400894 ret = traverse_by_cache_tree(pos, nr_entries, n, info);
Elijah Newren13e1fd62023-02-27 15:28:17 +0000895 o->internal.cache_bottom = bottom;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200896 return ret;
897 }
Linus Torvalds01904572008-03-05 20:15:44 -0800898
899 p = names;
900 while (!p->mode)
901 p++;
902
903 newinfo = *info;
904 newinfo.prev = info;
Junio C Hamano40e37252011-08-29 12:31:06 -0700905 newinfo.pathspec = info->pathspec;
Jeff King90553842019-07-31 00:38:15 -0400906 newinfo.name = p->path;
907 newinfo.namelen = p->pathlen;
908 newinfo.mode = p->mode;
Jeff King37806082019-07-31 00:38:18 -0400909 newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1);
René Scharfe603d2492013-06-16 01:44:43 +0200910 newinfo.df_conflicts |= df_conflicts;
Linus Torvalds01904572008-03-05 20:15:44 -0800911
Jeff King59c4c7d2023-08-31 02:17:54 -0400912 ALLOC_ARRAY(t, n);
913 ALLOC_ARRAY(buf, n);
914
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000915 /*
916 * Fetch the tree from the ODB for each peer directory in the
917 * n commits.
918 *
919 * For 2- and 3-way traversals, we try to avoid hitting the
920 * ODB twice for the same OID. This should yield a nice speed
921 * up in checkouts and merges when the commits are similar.
922 *
923 * We don't bother doing the full O(n^2) search for larger n,
924 * because wider traversals don't happen that often and we
925 * avoid the search setup.
926 *
927 * When 2 peer OIDs are the same, we just copy the tree
928 * descriptor data. This implicitly borrows the buffer
929 * data from the earlier cell.
930 */
Linus Torvalds01904572008-03-05 20:15:44 -0800931 for (i = 0; i < n; i++, dirmask >>= 1) {
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000932 if (i > 0 && are_same_oid(&names[i], &names[i - 1]))
933 t[i] = t[i - 1];
934 else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
935 t[i] = t[i - 2];
936 else {
René Scharfe5c377d32017-08-12 10:32:59 +0200937 const struct object_id *oid = NULL;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000938 if (dirmask & 1)
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000939 oid = &names[i].oid;
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700940 buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid);
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000941 }
Linus Torvalds01904572008-03-05 20:15:44 -0800942 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700943
944 bottom = switch_cache_bottom(&newinfo);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100945 ret = traverse_trees(o->src_index, n, t, &newinfo);
Junio C Hamano730f7282009-09-20 00:03:39 -0700946 restore_cache_bottom(&newinfo, bottom);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500947
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000948 for (i = 0; i < nr_buf; i++)
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500949 free(buf[i]);
Jeff King59c4c7d2023-08-31 02:17:54 -0400950 free(buf);
951 free(t);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500952
Junio C Hamano730f7282009-09-20 00:03:39 -0700953 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800954}
955
956/*
957 * Compare the traverse-path to the cache entry without actually
958 * having to generate the textual representation of the traverse
959 * path.
960 *
961 * NOTE! This *only* compares up to the size of the traverse path
962 * itself - the caller needs to do the final check for the cache
963 * entry having more data at the end!
964 */
Jeff King90553842019-07-31 00:38:15 -0400965static int do_compare_entry_piecewise(const struct cache_entry *ce,
966 const struct traverse_info *info,
967 const char *name, size_t namelen,
968 unsigned mode)
Linus Torvalds01904572008-03-05 20:15:44 -0800969{
Jeff King90553842019-07-31 00:38:15 -0400970 int pathlen, ce_len;
Linus Torvalds01904572008-03-05 20:15:44 -0800971 const char *ce_name;
972
973 if (info->prev) {
David Turnerd9c2bd52015-12-21 17:34:20 -0500974 int cmp = do_compare_entry_piecewise(ce, info->prev,
Jeff King90553842019-07-31 00:38:15 -0400975 info->name, info->namelen,
976 info->mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800977 if (cmp)
978 return cmp;
979 }
980 pathlen = info->pathlen;
981 ce_len = ce_namelen(ce);
982
983 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
984 if (ce_len < pathlen)
985 return -1;
986
987 ce_len -= pathlen;
988 ce_name = ce->name + pathlen;
989
Jeff King90553842019-07-31 00:38:15 -0400990 return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800991}
992
David Turnerd9c2bd52015-12-21 17:34:20 -0500993static int do_compare_entry(const struct cache_entry *ce,
994 const struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -0400995 const char *name, size_t namelen,
996 unsigned mode)
David Turnerd9c2bd52015-12-21 17:34:20 -0500997{
Jeff King90553842019-07-31 00:38:15 -0400998 int pathlen, ce_len;
David Turnerd9c2bd52015-12-21 17:34:20 -0500999 const char *ce_name;
1000 int cmp;
Derrick Stoleecd807a52021-07-14 13:12:31 +00001001 unsigned ce_mode;
David Turnerd9c2bd52015-12-21 17:34:20 -05001002
1003 /*
1004 * If we have not precomputed the traverse path, it is quicker
1005 * to avoid doing so. But if we have precomputed it,
1006 * it is quicker to use the precomputed version.
1007 */
1008 if (!info->traverse_path)
Jeff King90553842019-07-31 00:38:15 -04001009 return do_compare_entry_piecewise(ce, info, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -05001010
1011 cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
1012 if (cmp)
1013 return cmp;
1014
1015 pathlen = info->pathlen;
1016 ce_len = ce_namelen(ce);
1017
1018 if (ce_len < pathlen)
1019 return -1;
1020
1021 ce_len -= pathlen;
1022 ce_name = ce->name + pathlen;
1023
Derrick Stoleecd807a52021-07-14 13:12:31 +00001024 ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG;
1025 return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -05001026}
1027
Linus Torvalds01904572008-03-05 20:15:44 -08001028static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
1029{
Jeff King90553842019-07-31 00:38:15 -04001030 int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode);
Linus Torvalds01904572008-03-05 20:15:44 -08001031 if (cmp)
1032 return cmp;
1033
1034 /*
Derrick Stoleecd807a52021-07-14 13:12:31 +00001035 * At this point, we know that we have a prefix match. If ce
1036 * is a sparse directory, then allow an exact match. This only
1037 * works when the input name is a directory, since ce->name
1038 * ends in a directory separator.
1039 */
1040 if (S_ISSPARSEDIR(ce->ce_mode) &&
1041 ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1)
1042 return 0;
1043
1044 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001045 * Even if the beginning compared identically, the ce should
1046 * compare as bigger than a directory leading up to it!
1047 */
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001048 return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));
Linus Torvalds01904572008-03-05 20:15:44 -08001049}
1050
Junio C Hamanoda165f42010-01-07 14:59:54 -08001051static int ce_in_traverse_path(const struct cache_entry *ce,
1052 const struct traverse_info *info)
1053{
1054 if (!info->prev)
1055 return 1;
Jeff King90553842019-07-31 00:38:15 -04001056 if (do_compare_entry(ce, info->prev,
1057 info->name, info->namelen, info->mode))
Junio C Hamanoda165f42010-01-07 14:59:54 -08001058 return 0;
1059 /*
1060 * If ce (blob) is the same name as the path (which is a tree
1061 * we will be descending into), it won't be inside it.
1062 */
1063 return (info->pathlen < ce_namelen(ce));
1064}
1065
Jameson Millera8497352018-07-02 19:49:31 +00001066static struct cache_entry *create_ce_entry(const struct traverse_info *info,
1067 const struct name_entry *n,
1068 int stage,
1069 struct index_state *istate,
Derrick Stolee523506d2021-07-14 13:12:33 +00001070 int is_transient,
1071 int is_sparse_directory)
Linus Torvalds01904572008-03-05 20:15:44 -08001072{
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001073 size_t len = traverse_path_len(info, tree_entry_len(n));
Derrick Stolee523506d2021-07-14 13:12:33 +00001074 size_t alloc_len = is_sparse_directory ? len + 1 : len;
Jameson Millera8497352018-07-02 19:49:31 +00001075 struct cache_entry *ce =
1076 is_transient ?
Derrick Stolee523506d2021-07-14 13:12:33 +00001077 make_empty_transient_cache_entry(alloc_len, NULL) :
1078 make_empty_cache_entry(istate, alloc_len);
Linus Torvalds01904572008-03-05 20:15:44 -08001079
1080 ce->ce_mode = create_ce_mode(n->mode);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001081 ce->ce_flags = create_ce_flags(stage);
1082 ce->ce_namelen = len;
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001083 oidcpy(&ce->oid, &n->oid);
Jeff King5aa02f92019-07-31 00:38:25 -04001084 /* len+1 because the cache_entry allocates space for NUL */
1085 make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen);
Linus Torvalds01904572008-03-05 20:15:44 -08001086
Derrick Stolee523506d2021-07-14 13:12:33 +00001087 if (is_sparse_directory) {
1088 ce->name[len] = '/';
1089 ce->name[len + 1] = '\0';
1090 ce->ce_namelen++;
1091 ce->ce_flags |= CE_SKIP_WORKTREE;
1092 }
1093
Linus Torvalds01904572008-03-05 20:15:44 -08001094 return ce;
1095}
1096
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001097/*
Victoria Dyeb15207b2022-08-08 19:07:52 +00001098 * Determine whether the path specified by 'p' should be unpacked as a new
1099 * sparse directory in a sparse index. A new sparse directory 'A/':
1100 * - must be outside the sparse cone.
1101 * - must not already be in the index (i.e., no index entry with name 'A/'
1102 * exists).
1103 * - must not have any child entries in the index (i.e., no index entry
1104 * 'A/<something>' exists).
1105 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1106 */
1107static int entry_is_new_sparse_dir(const struct traverse_info *info,
1108 const struct name_entry *p)
1109{
1110 int res, pos;
1111 struct strbuf dirpath = STRBUF_INIT;
1112 struct unpack_trees_options *o = info->data;
1113
1114 if (!S_ISDIR(p->mode))
1115 return 0;
1116
1117 /*
1118 * If the path is inside the sparse cone, it can't be a sparse directory.
1119 */
1120 strbuf_add(&dirpath, info->traverse_path, info->pathlen);
1121 strbuf_add(&dirpath, p->path, p->pathlen);
1122 strbuf_addch(&dirpath, '/');
1123 if (path_in_cone_mode_sparse_checkout(dirpath.buf, o->src_index)) {
1124 res = 0;
1125 goto cleanup;
1126 }
1127
1128 pos = index_name_pos_sparse(o->src_index, dirpath.buf, dirpath.len);
1129 if (pos >= 0) {
1130 /* Path is already in the index, not a new sparse dir */
1131 res = 0;
1132 goto cleanup;
1133 }
1134
1135 /* Where would this sparse dir be inserted into the index? */
1136 pos = -pos - 1;
1137 if (pos >= o->src_index->cache_nr) {
1138 /*
1139 * Sparse dir would be inserted at the end of the index, so we
1140 * know it has no child entries.
1141 */
1142 res = 1;
1143 goto cleanup;
1144 }
1145
1146 /*
1147 * If the dir has child entries in the index, the first would be at the
1148 * position the sparse directory would be inserted. If the entry at this
1149 * position is inside the dir, not a new sparse dir.
1150 */
1151 res = strncmp(o->src_index->cache[pos]->name, dirpath.buf, dirpath.len);
1152
1153cleanup:
1154 strbuf_release(&dirpath);
1155 return res;
1156}
1157
1158/*
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001159 * Note that traverse_by_cache_tree() duplicates some logic in this function
1160 * without actually calling it. If you change the logic here you may need to
1161 * check and change there as well.
1162 */
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +00001163static int unpack_single_entry(int n, unsigned long mask,
1164 unsigned long dirmask,
1165 struct cache_entry **src,
1166 const struct name_entry *names,
Victoria Dyeb15207b2022-08-08 19:07:52 +00001167 const struct traverse_info *info,
1168 int *is_new_sparse_dir)
Linus Torvalds01904572008-03-05 20:15:44 -08001169{
1170 int i;
1171 struct unpack_trees_options *o = info->data;
René Scharfe603d2492013-06-16 01:44:43 +02001172 unsigned long conflicts = info->df_conflicts | dirmask;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001173 const struct name_entry *p = names;
Linus Torvalds01904572008-03-05 20:15:44 -08001174
Victoria Dyeb15207b2022-08-08 19:07:52 +00001175 *is_new_sparse_dir = 0;
1176 if (mask == dirmask && !src[0]) {
1177 /*
1178 * If we're not in a sparse index, we can't unpack a directory
1179 * without recursing into it, so we return.
1180 */
1181 if (!o->src_index->sparse_index)
1182 return 0;
1183
1184 /* Find first entry with a real name (we could use "mask" too) */
1185 while (!p->mode)
1186 p++;
1187
1188 /*
1189 * If the directory is completely missing from the index but
1190 * would otherwise be a sparse directory, we should unpack it.
1191 * If not, we'll return and continue recursively traversing the
1192 * tree.
1193 */
1194 *is_new_sparse_dir = entry_is_new_sparse_dir(info, p);
1195 if (!*is_new_sparse_dir)
1196 return 0;
1197 }
Linus Torvalds01904572008-03-05 20:15:44 -08001198
Linus Torvalds01904572008-03-05 20:15:44 -08001199 /*
Victoria Dyeb15207b2022-08-08 19:07:52 +00001200 * When we are unpacking a sparse directory, then this isn't necessarily
1201 * a directory-file conflict.
Derrick Stolee523506d2021-07-14 13:12:33 +00001202 */
Victoria Dyeb15207b2022-08-08 19:07:52 +00001203 if (mask == dirmask &&
1204 (*is_new_sparse_dir || (src[0] && S_ISSPARSEDIR(src[0]->ce_mode))))
Derrick Stolee523506d2021-07-14 13:12:33 +00001205 conflicts = 0;
1206
1207 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001208 * Ok, we've filled in up to any potential index entry in src[0],
1209 * now do the rest.
1210 */
1211 for (i = 0; i < n; i++) {
1212 int stage;
1213 unsigned int bit = 1ul << i;
1214 if (conflicts & bit) {
1215 src[i + o->merge] = o->df_conflict_entry;
1216 continue;
1217 }
1218 if (!(mask & bit))
1219 continue;
1220 if (!o->merge)
1221 stage = 0;
1222 else if (i + 1 < o->head_idx)
1223 stage = 1;
1224 else if (i + 1 > o->head_idx)
1225 stage = 3;
1226 else
1227 stage = 2;
Jameson Millera8497352018-07-02 19:49:31 +00001228
1229 /*
1230 * If the merge bit is set, then the cache entries are
1231 * discarded in the following block. In this case,
1232 * construct "transient" cache_entries, as they are
1233 * not stored in the index. otherwise construct the
1234 * cache entry from the index aware logic.
1235 */
Derrick Stolee523506d2021-07-14 13:12:33 +00001236 src[i + o->merge] = create_ce_entry(info, names + i, stage,
Elijah Newren0d680a72023-02-27 15:28:18 +00001237 &o->internal.result,
1238 o->merge, bit & dirmask);
Linus Torvalds01904572008-03-05 20:15:44 -08001239 }
1240
René Scharfe5d80ef52013-06-02 17:46:57 +02001241 if (o->merge) {
1242 int rc = call_unpack_fn((const struct cache_entry * const *)src,
1243 o);
1244 for (i = 0; i < n; i++) {
1245 struct cache_entry *ce = src[i + o->merge];
1246 if (ce != o->df_conflict_entry)
Jameson Millera8497352018-07-02 19:49:31 +00001247 discard_cache_entry(ce);
René Scharfe5d80ef52013-06-02 17:46:57 +02001248 }
1249 return rc;
1250 }
Linus Torvalds01904572008-03-05 20:15:44 -08001251
Linus Torvalds01904572008-03-05 20:15:44 -08001252 for (i = 0; i < n; i++)
Junio C Hamanoaab3b9a2009-03-12 00:02:12 -07001253 if (src[i] && src[i] != o->df_conflict_entry)
Jeff King46169182014-11-24 13:36:51 -05001254 if (do_add_entry(o, src[i], 0, 0))
1255 return -1;
1256
Linus Torvalds01904572008-03-05 20:15:44 -08001257 return 0;
1258}
1259
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001260static int unpack_failed(struct unpack_trees_options *o, const char *message)
1261{
Elijah Newren13e1fd62023-02-27 15:28:17 +00001262 discard_index(&o->internal.result);
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07001263 if (!o->quiet && !o->exiting_early) {
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001264 if (message)
1265 return error("%s", message);
1266 return -1;
1267 }
1268 return -1;
1269}
1270
Junio C Hamano730f7282009-09-20 00:03:39 -07001271/*
1272 * The tree traversal is looking at name p. If we have a matching entry,
1273 * return it. If name p is a directory in the index, do not return
1274 * anything, as we will want to match it when the traversal descends into
1275 * the directory.
1276 */
1277static int find_cache_pos(struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -04001278 const char *p, size_t p_len)
Junio C Hamano730f7282009-09-20 00:03:39 -07001279{
1280 int pos;
1281 struct unpack_trees_options *o = info->data;
1282 struct index_state *index = o->src_index;
1283 int pfxlen = info->pathlen;
Junio C Hamano730f7282009-09-20 00:03:39 -07001284
Elijah Newren13e1fd62023-02-27 15:28:17 +00001285 for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001286 const struct cache_entry *ce = index->cache[pos];
Junio C Hamano730f7282009-09-20 00:03:39 -07001287 const char *ce_name, *ce_slash;
1288 int cmp, ce_len;
1289
Brian Downinge53e6b42010-06-10 21:59:07 -05001290 if (ce->ce_flags & CE_UNPACKED) {
1291 /*
1292 * cache_bottom entry is already unpacked, so
1293 * we can never match it; don't check it
1294 * again.
1295 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00001296 if (pos == o->internal.cache_bottom)
1297 ++o->internal.cache_bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -07001298 continue;
Brian Downinge53e6b42010-06-10 21:59:07 -05001299 }
David Turnera6720952016-01-22 14:58:43 -05001300 if (!ce_in_traverse_path(ce, info)) {
1301 /*
1302 * Check if we can skip future cache checks
1303 * (because we're already past all possible
1304 * entries in the traverse path).
1305 */
1306 if (info->traverse_path) {
1307 if (strncmp(ce->name, info->traverse_path,
1308 info->pathlen) > 0)
1309 break;
1310 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001311 continue;
David Turnera6720952016-01-22 14:58:43 -05001312 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001313 ce_name = ce->name + pfxlen;
1314 ce_slash = strchr(ce_name, '/');
1315 if (ce_slash)
1316 ce_len = ce_slash - ce_name;
1317 else
1318 ce_len = ce_namelen(ce) - pfxlen;
Jeff King90553842019-07-31 00:38:15 -04001319 cmp = name_compare(p, p_len, ce_name, ce_len);
Junio C Hamano730f7282009-09-20 00:03:39 -07001320 /*
1321 * Exact match; if we have a directory we need to
1322 * delay returning it.
1323 */
1324 if (!cmp)
1325 return ce_slash ? -2 - pos : pos;
1326 if (0 < cmp)
1327 continue; /* keep looking */
1328 /*
1329 * ce_name sorts after p->path; could it be that we
1330 * have files under p->path directory in the index?
1331 * E.g. ce_name == "t-i", and p->path == "t"; we may
1332 * have "t/a" in the index.
1333 */
Jeff King90553842019-07-31 00:38:15 -04001334 if (p_len < ce_len && !memcmp(ce_name, p, p_len) &&
Junio C Hamano730f7282009-09-20 00:03:39 -07001335 ce_name[p_len] < '/')
1336 continue; /* keep looking */
1337 break;
1338 }
1339 return -1;
1340}
1341
Derrick Stolee523506d2021-07-14 13:12:33 +00001342/*
1343 * Given a sparse directory entry 'ce', compare ce->name to
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001344 * info->traverse_path + p->path + '/' if info->traverse_path
1345 * is non-empty.
1346 *
Derrick Stolee523506d2021-07-14 13:12:33 +00001347 * Compare ce->name to p->path + '/' otherwise. Note that
1348 * ce->name must end in a trailing '/' because it is a sparse
1349 * directory entry.
1350 */
1351static int sparse_dir_matches_path(const struct cache_entry *ce,
1352 struct traverse_info *info,
1353 const struct name_entry *p)
1354{
1355 assert(S_ISSPARSEDIR(ce->ce_mode));
1356 assert(ce->name[ce->ce_namelen - 1] == '/');
1357
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001358 if (info->pathlen)
1359 return ce->ce_namelen == info->pathlen + p->pathlen + 1 &&
1360 ce->name[info->pathlen - 1] == '/' &&
1361 !strncmp(ce->name, info->traverse_path, info->pathlen) &&
1362 !strncmp(ce->name + info->pathlen, p->path, p->pathlen);
Derrick Stolee523506d2021-07-14 13:12:33 +00001363 return ce->ce_namelen == p->pathlen + 1 &&
1364 !strncmp(ce->name, p->path, p->pathlen);
1365}
1366
Junio C Hamano730f7282009-09-20 00:03:39 -07001367static struct cache_entry *find_cache_entry(struct traverse_info *info,
1368 const struct name_entry *p)
1369{
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001370 const char *path;
Jeff King90553842019-07-31 00:38:15 -04001371 int pos = find_cache_pos(info, p->path, p->pathlen);
Junio C Hamano730f7282009-09-20 00:03:39 -07001372 struct unpack_trees_options *o = info->data;
1373
1374 if (0 <= pos)
1375 return o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001376
1377 /*
1378 * Check for a sparse-directory entry named "path/".
1379 * Due to the input p->path not having a trailing
1380 * slash, the negative 'pos' value overshoots the
1381 * expected position, hence "-2" instead of "-1".
1382 */
1383 pos = -pos - 2;
1384
1385 if (pos < 0 || pos >= o->src_index->cache_nr)
Junio C Hamano730f7282009-09-20 00:03:39 -07001386 return NULL;
Derrick Stolee523506d2021-07-14 13:12:33 +00001387
1388 /*
1389 * Due to lexicographic sorting and sparse directory
1390 * entries ending with a trailing slash, our path as a
1391 * sparse directory (e.g "subdir/") and our path as a
1392 * file (e.g. "subdir") might be separated by other
1393 * paths (e.g. "subdir-").
1394 */
1395 while (pos >= 0) {
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001396 struct cache_entry *ce = o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001397
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001398 if (!skip_prefix(ce->name, info->traverse_path, &path) ||
1399 strncmp(path, p->path, p->pathlen) ||
1400 path[p->pathlen] != '/')
Derrick Stolee523506d2021-07-14 13:12:33 +00001401 return NULL;
1402
1403 if (S_ISSPARSEDIR(ce->ce_mode) &&
1404 sparse_dir_matches_path(ce, info, p))
1405 return ce;
1406
1407 pos--;
1408 }
1409
1410 return NULL;
Junio C Hamano730f7282009-09-20 00:03:39 -07001411}
1412
Junio C Hamanoba655da2009-09-14 02:22:00 -07001413static void debug_path(struct traverse_info *info)
1414{
1415 if (info->prev) {
1416 debug_path(info->prev);
Jeff King90553842019-07-31 00:38:15 -04001417 if (*info->prev->name)
Junio C Hamanoba655da2009-09-14 02:22:00 -07001418 putchar('/');
1419 }
Jeff King90553842019-07-31 00:38:15 -04001420 printf("%s", info->name);
Junio C Hamanoba655da2009-09-14 02:22:00 -07001421}
1422
1423static void debug_name_entry(int i, struct name_entry *n)
1424{
1425 printf("ent#%d %06o %s\n", i,
1426 n->path ? n->mode : 0,
1427 n->path ? n->path : "(missing)");
1428}
1429
1430static void debug_unpack_callback(int n,
1431 unsigned long mask,
1432 unsigned long dirmask,
1433 struct name_entry *names,
1434 struct traverse_info *info)
1435{
1436 int i;
1437 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1438 mask, dirmask, n);
1439 debug_path(info);
1440 putchar('\n');
1441 for (i = 0; i < n; i++)
1442 debug_name_entry(i, names + i);
1443}
1444
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001445/*
Derrick Stolee523506d2021-07-14 13:12:33 +00001446 * Returns true if and only if the given cache_entry is a
1447 * sparse-directory entry that matches the given name_entry
1448 * from the tree walk at the given traverse_info.
1449 */
1450static int is_sparse_directory_entry(struct cache_entry *ce,
Victoria Dye037f8ea2022-09-01 21:02:33 +00001451 const struct name_entry *name,
Derrick Stolee523506d2021-07-14 13:12:33 +00001452 struct traverse_info *info)
1453{
1454 if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))
1455 return 0;
1456
1457 return sparse_dir_matches_path(ce, info, name);
1458}
1459
Victoria Dyeab810472022-03-01 20:24:30 +00001460static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1461{
1462 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
1463 struct unpack_trees_options *o = info->data;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001464 int ret, is_new_sparse_dir;
Victoria Dyeab810472022-03-01 20:24:30 +00001465
1466 assert(o->merge);
1467
1468 /*
1469 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1470 * merging, src[0] is a transient cache entry derived from the first tree
1471 * provided. Create the temporary entry as if it came from a non-sparse index.
1472 */
1473 if (!is_null_oid(&names[0].oid)) {
1474 src[0] = create_ce_entry(info, &names[0], 0,
Elijah Newren13e1fd62023-02-27 15:28:17 +00001475 &o->internal.result, 1,
Victoria Dyeab810472022-03-01 20:24:30 +00001476 dirmask & (1ul << 0));
1477 src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1478 }
1479
1480 /*
1481 * 'unpack_single_entry' assumes that src[0] is derived directly from
1482 * the index, rather than from an entry in 'names'. This is *not* true when
1483 * merging a sparse directory, in which case names[0] is the "index" source
1484 * entry. To match the expectations of 'unpack_single_entry', shift past the
1485 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1486 * 'dirmask' accordingly.
1487 */
Victoria Dyeb15207b2022-08-08 19:07:52 +00001488 ret = unpack_single_entry(n - 1, mask >> 1, dirmask >> 1, src, names + 1, info, &is_new_sparse_dir);
Victoria Dyeab810472022-03-01 20:24:30 +00001489
1490 if (src[0])
1491 discard_cache_entry(src[0]);
1492
1493 return ret >= 0 ? mask : -1;
1494}
1495
Derrick Stolee523506d2021-07-14 13:12:33 +00001496/*
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001497 * Note that traverse_by_cache_tree() duplicates some logic in this function
1498 * without actually calling it. If you change the logic here you may need to
1499 * check and change there as well.
1500 */
Linus Torvalds01904572008-03-05 20:15:44 -08001501static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1502{
René Scharfec7cddc12009-01-31 15:39:10 +01001503 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Linus Torvalds01904572008-03-05 20:15:44 -08001504 struct unpack_trees_options *o = info->data;
Linus Torvalds01904572008-03-05 20:15:44 -08001505 const struct name_entry *p = names;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001506 int is_new_sparse_dir;
Linus Torvalds01904572008-03-05 20:15:44 -08001507
1508 /* Find first entry with a real name (we could use "mask" too) */
1509 while (!p->mode)
1510 p++;
1511
Elijah Newren1ca13dd2023-02-27 15:28:19 +00001512 if (o->internal.debug_unpack)
Junio C Hamanoba655da2009-09-14 02:22:00 -07001513 debug_unpack_callback(n, mask, dirmask, names, info);
1514
Linus Torvalds01904572008-03-05 20:15:44 -08001515 /* Are we supposed to look at the index too? */
1516 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001517 while (1) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001518 int cmp;
Junio C Hamano730f7282009-09-20 00:03:39 -07001519 struct cache_entry *ce;
1520
1521 if (o->diff_index_cached)
Victoria Dye99430aa2022-03-17 15:55:36 +00001522 ce = next_cache_entry(o);
Junio C Hamano730f7282009-09-20 00:03:39 -07001523 else
1524 ce = find_cache_entry(info, p);
1525
Junio C Hamanoda165f42010-01-07 14:59:54 -08001526 if (!ce)
1527 break;
1528 cmp = compare_entry(ce, info, p);
Linus Torvalds01904572008-03-05 20:15:44 -08001529 if (cmp < 0) {
1530 if (unpack_index_entry(ce, o) < 0)
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001531 return unpack_failed(o, NULL);
Linus Torvalds01904572008-03-05 20:15:44 -08001532 continue;
1533 }
1534 if (!cmp) {
1535 if (ce_stage(ce)) {
1536 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001537 * If we skip unmerged index
1538 * entries, we'll skip this
1539 * entry *and* the tree
1540 * entries associated with it!
Linus Torvalds01904572008-03-05 20:15:44 -08001541 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08001542 if (o->skip_unmerged) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001543 add_same_unmerged(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -08001544 return mask;
Linus Torvalds34110cd2008-03-06 18:12:28 -08001545 }
Linus Torvalds01904572008-03-05 20:15:44 -08001546 }
1547 src[0] = ce;
Linus Torvalds01904572008-03-05 20:15:44 -08001548 }
1549 break;
1550 }
1551 }
1552
Victoria Dyeb15207b2022-08-08 19:07:52 +00001553 if (unpack_single_entry(n, mask, dirmask, src, names, info, &is_new_sparse_dir))
Linus Torvalds01904572008-03-05 20:15:44 -08001554 return -1;
1555
René Scharfe97e59542012-04-10 20:55:58 +02001556 if (o->merge && src[0]) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001557 if (ce_stage(src[0]))
1558 mark_ce_used_same_name(src[0], o);
1559 else
1560 mark_ce_used(src[0], o);
1561 }
1562
Linus Torvalds01904572008-03-05 20:15:44 -08001563 /* Now handle any directories.. */
1564 if (dirmask) {
Junio C Hamanob65982b2009-05-20 15:57:22 -07001565 /* special case: "diff-index --cached" looking at a tree */
1566 if (o->diff_index_cached &&
1567 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
1568 int matches;
1569 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
1570 names, info);
1571 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001572 * Everything under the name matches; skip the
1573 * entire hierarchy. diff_index_cached codepath
1574 * special cases D/F conflicts in such a way that
1575 * it does not do any look-ahead, so this is safe.
Junio C Hamanob65982b2009-05-20 15:57:22 -07001576 */
1577 if (matches) {
Victoria Dyebfc763d2022-03-17 15:55:35 +00001578 /*
1579 * Only increment the cache_bottom if the
1580 * directory isn't a sparse directory index
1581 * entry (if it is, it was already incremented)
1582 * in 'mark_ce_used()'
1583 */
1584 if (!src[0] || !S_ISSPARSEDIR(src[0]->ce_mode))
Elijah Newren13e1fd62023-02-27 15:28:17 +00001585 o->internal.cache_bottom += matches;
Junio C Hamanob65982b2009-05-20 15:57:22 -07001586 return mask;
1587 }
1588 }
1589
Victoria Dye037f8ea2022-09-01 21:02:33 +00001590 if (!is_sparse_directory_entry(src[0], p, info) &&
Victoria Dyeb15207b2022-08-08 19:07:52 +00001591 !is_new_sparse_dir &&
Derrick Stolee523506d2021-07-14 13:12:33 +00001592 traverse_trees_recursive(n, dirmask, mask & ~dirmask,
1593 names, info) < 0) {
Junio C Hamano542c2642008-03-10 01:26:23 -07001594 return -1;
Derrick Stolee523506d2021-07-14 13:12:33 +00001595 }
1596
Linus Torvalds01904572008-03-05 20:15:44 -08001597 return mask;
1598 }
1599
1600 return mask;
1601}
1602
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001603static int clear_ce_flags_1(struct index_state *istate,
1604 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001605 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001606 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001607 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001608 enum pattern_match_result default_match,
1609 int progress_nr);
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001610
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001611/* Whole directory matching */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001612static int clear_ce_flags_dir(struct index_state *istate,
1613 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001614 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001615 char *basename,
1616 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001617 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001618 enum pattern_match_result default_match,
1619 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001620{
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001621 struct cache_entry **cache_end;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001622 int dtype = DT_DIR;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001623 int rc;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001624 enum pattern_match_result ret, orig_ret;
1625 orig_ret = path_matches_pattern_list(prefix->buf, prefix->len,
1626 basename, &dtype, pl, istate);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001627
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001628 strbuf_addch(prefix, '/');
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001629
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001630 /* If undecided, use matching result of parent dir in defval */
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001631 if (orig_ret == UNDECIDED)
Derrick Stolee468ce992019-09-03 11:04:58 -07001632 ret = default_match;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001633 else
1634 ret = orig_ret;
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001635
1636 for (cache_end = cache; cache_end != cache + nr; cache_end++) {
1637 struct cache_entry *ce = *cache_end;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001638 if (strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001639 break;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001640 }
1641
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001642 if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {
1643 struct cache_entry **ce = cache;
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001644 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001645
1646 while (ce < cache_end) {
1647 (*ce)->ce_flags &= ~clear_mask;
1648 ce++;
1649 }
1650 } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) {
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001651 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001652 } else {
1653 rc = clear_ce_flags_1(istate, cache, cache_end - cache,
1654 prefix,
1655 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001656 pl, ret,
1657 progress_nr);
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001658 }
1659
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001660 strbuf_setlen(prefix, prefix->len - 1);
1661 return rc;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001662}
1663
1664/*
1665 * Traverse the index, find every entry that matches according to
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001666 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001667 * number of traversed entries.
1668 *
1669 * If select_mask is non-zero, only entries whose ce_flags has on of
1670 * those bits enabled are traversed.
1671 *
1672 * cache : pointer to an index entry
1673 * prefix_len : an offset to its path
1674 *
1675 * The current path ("prefix") including the trailing '/' is
1676 * cache[0]->name[0..(prefix_len-1)]
1677 * Top level path has prefix_len zero.
1678 */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001679static int clear_ce_flags_1(struct index_state *istate,
1680 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001681 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001682 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001683 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001684 enum pattern_match_result default_match,
1685 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001686{
Jeff Kingd20bc012020-01-29 00:46:47 -05001687 struct cache_entry **cache_end = nr ? cache + nr : cache;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001688
1689 /*
1690 * Process all entries that have the given prefix and meet
1691 * select_mask condition
1692 */
1693 while(cache != cache_end) {
1694 struct cache_entry *ce = *cache;
1695 const char *name, *slash;
Derrick Stolee468ce992019-09-03 11:04:58 -07001696 int len, dtype;
1697 enum pattern_match_result ret;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001698
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001699 display_progress(istate->progress, progress_nr);
1700
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001701 if (select_mask && !(ce->ce_flags & select_mask)) {
1702 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001703 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001704 continue;
1705 }
1706
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001707 if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001708 break;
1709
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001710 name = ce->name + prefix->len;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001711 slash = strchr(name, '/');
1712
1713 /* If it's a directory, try whole directory match first */
1714 if (slash) {
1715 int processed;
1716
1717 len = slash - name;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001718 strbuf_add(prefix, name, len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001719
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001720 processed = clear_ce_flags_dir(istate, cache, cache_end - cache,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001721 prefix,
1722 prefix->buf + prefix->len - len,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001723 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001724 pl, default_match,
1725 progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001726
1727 /* clear_c_f_dir eats a whole dir already? */
1728 if (processed) {
1729 cache += processed;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001730 progress_nr += processed;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001731 strbuf_setlen(prefix, prefix->len - len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001732 continue;
1733 }
1734
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001735 strbuf_addch(prefix, '/');
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001736 processed = clear_ce_flags_1(istate, cache, cache_end - cache,
1737 prefix,
1738 select_mask, clear_mask, pl,
1739 default_match, progress_nr);
1740
1741 cache += processed;
1742 progress_nr += processed;
1743
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001744 strbuf_setlen(prefix, prefix->len - len - 1);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001745 continue;
1746 }
1747
1748 /* Non-directory */
1749 dtype = ce_to_dtype(ce);
Derrick Stolee468ce992019-09-03 11:04:58 -07001750 ret = path_matches_pattern_list(ce->name,
1751 ce_namelen(ce),
1752 name, &dtype, pl, istate);
1753 if (ret == UNDECIDED)
1754 ret = default_match;
Derrick Stoleef998a3f2020-01-31 20:16:15 +00001755 if (ret == MATCHED || ret == MATCHED_RECURSIVE)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001756 ce->ce_flags &= ~clear_mask;
1757 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001758 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001759 }
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001760
1761 display_progress(istate->progress, progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001762 return nr - (cache_end - cache);
1763}
1764
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001765static int clear_ce_flags(struct index_state *istate,
1766 int select_mask, int clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001767 struct pattern_list *pl,
1768 int show_progress)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001769{
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001770 static struct strbuf prefix = STRBUF_INIT;
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001771 char label[100];
1772 int rval;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001773
1774 strbuf_reset(&prefix);
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001775 if (show_progress)
1776 istate->progress = start_delayed_progress(
Patrick Steinhardt1f7e6472024-12-17 07:43:48 +01001777 the_repository,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001778 _("Updating index flags"),
1779 istate->cache_nr);
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001780
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001781 xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)",
1782 (unsigned long)select_mask, (unsigned long)clear_mask);
1783 trace2_region_enter("unpack_trees", label, the_repository);
1784 rval = clear_ce_flags_1(istate,
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001785 istate->cache,
1786 istate->cache_nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001787 &prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001788 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001789 pl, 0, 0);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001790 trace2_region_leave("unpack_trees", label, the_repository);
1791
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001792 stop_progress(&istate->progress);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001793 return rval;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001794}
1795
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001796/*
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001797 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1798 */
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001799static void mark_new_skip_worktree(struct pattern_list *pl,
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001800 struct index_state *istate,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001801 int select_flag, int skip_wt_flag,
1802 int show_progress)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001803{
1804 int i;
1805
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001806 /*
1807 * 1. Pretend the narrowest worktree: only unmerged entries
1808 * are checked out
1809 */
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001810 for (i = 0; i < istate->cache_nr; i++) {
1811 struct cache_entry *ce = istate->cache[i];
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001812
1813 if (select_flag && !(ce->ce_flags & select_flag))
1814 continue;
1815
Max Kirillovb33fdfc2018-07-10 22:17:48 +03001816 if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001817 ce->ce_flags |= skip_wt_flag;
1818 else
1819 ce->ce_flags &= ~skip_wt_flag;
1820 }
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001821
1822 /*
1823 * 2. Widen worktree according to sparse-checkout file.
1824 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1825 */
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001826 clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001827}
1828
Elijah Newren30e89c12020-03-27 00:48:51 +00001829static void populate_from_existing_patterns(struct unpack_trees_options *o,
1830 struct pattern_list *pl)
1831{
Derrick Stoleedd230222021-01-23 19:58:17 +00001832 if (get_sparse_checkout_patterns(pl) < 0)
Elijah Newren30e89c12020-03-27 00:48:51 +00001833 o->skip_sparse_checkout = 1;
1834 else
Elijah Newren576de3d2023-02-27 15:28:16 +00001835 o->internal.pl = pl;
Elijah Newren30e89c12020-03-27 00:48:51 +00001836}
1837
Victoria Dye74970392022-03-01 20:24:29 +00001838static void update_sparsity_for_prefix(const char *prefix,
1839 struct index_state *istate)
1840{
1841 int prefix_len = strlen(prefix);
1842 struct strbuf ce_prefix = STRBUF_INIT;
1843
1844 if (!istate->sparse_index)
1845 return;
1846
1847 while (prefix_len > 0 && prefix[prefix_len - 1] == '/')
1848 prefix_len--;
1849
1850 if (prefix_len <= 0)
1851 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1852
1853 strbuf_grow(&ce_prefix, prefix_len + 1);
1854 strbuf_add(&ce_prefix, prefix, prefix_len);
1855 strbuf_addch(&ce_prefix, '/');
1856
1857 /*
1858 * If the prefix points to a sparse directory or a path inside a sparse
1859 * directory, the index should be expanded. This is accomplished in one
1860 * of two ways:
1861 * - if the prefix is inside a sparse directory, it will be expanded by
1862 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1863 * - if the prefix matches an existing sparse directory entry,
1864 * 'index_name_pos(...)' will return its index position, triggering
1865 * the 'ensure_full_index(...)' below.
1866 */
1867 if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) &&
1868 index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0)
1869 ensure_full_index(istate);
1870
1871 strbuf_release(&ce_prefix);
1872}
Elijah Newren30e89c12020-03-27 00:48:51 +00001873
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001874static int verify_absent(const struct cache_entry *,
1875 enum unpack_trees_error_types,
1876 struct unpack_trees_options *);
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001877/*
1878 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1879 * resulting index, -2 on failure to reflect the changes to the work tree.
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001880 *
1881 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001882 */
Linus Torvalds933bf402007-08-09 22:21:29 -07001883int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +02001884{
Derrick Stolee6863df32021-03-30 13:10:52 +00001885 struct repository *repo = the_repository;
Victoria Dye99430aa2022-03-17 15:55:36 +00001886 int i, ret;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001887 static struct cache_entry *dfc;
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001888 struct pattern_list pl;
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001889 int free_pattern_list = 0;
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001890 struct dir_struct dir = DIR_INIT;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001891
Elijah Newren480d3d62021-09-27 16:33:44 +00001892 if (o->reset == UNPACK_RESET_INVALID)
1893 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001894
Junio C Hamanoca885a42008-03-13 22:07:18 -07001895 if (len > MAX_UNPACK_TREES)
1896 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
Elijah Newren576de3d2023-02-27 15:28:16 +00001897 if (o->internal.dir)
1898 BUG("o->internal.dir is for internal use only");
1899 if (o->internal.pl)
1900 BUG("o->internal.pl is for internal use only");
Elijah Newrenf2974242023-02-27 15:28:20 +00001901 if (o->df_conflict_entry)
1902 BUG("o->df_conflict_entry is an output only field");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001903
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02001904 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00001905 trace2_region_enter("unpack_trees", "unpack_trees", the_repository);
1906
Derrick Stolee6863df32021-03-30 13:10:52 +00001907 prepare_repo_settings(repo);
1908 if (repo->settings.command_requires_full_index) {
1909 ensure_full_index(o->src_index);
Ævar Arnfjörð Bjarmason29fefaf2023-01-12 13:55:25 +01001910 if (o->dst_index)
1911 ensure_full_index(o->dst_index);
Derrick Stolee6863df32021-03-30 13:10:52 +00001912 }
1913
Elijah Newren480d3d62021-09-27 16:33:44 +00001914 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED &&
1915 o->preserve_ignored)
1916 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1917
Elijah Newren04988c82021-09-27 16:33:41 +00001918 if (!o->preserve_ignored) {
Elijah Newren576de3d2023-02-27 15:28:16 +00001919 o->internal.dir = &dir;
1920 o->internal.dir->flags |= DIR_SHOW_IGNORED;
1921 setup_standard_excludes(o->internal.dir);
Elijah Newren04988c82021-09-27 16:33:41 +00001922 }
1923
Victoria Dye74970392022-03-01 20:24:29 +00001924 if (o->prefix)
1925 update_sparsity_for_prefix(o->prefix, o->src_index);
1926
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001927 if (!core_apply_sparse_checkout || !o->update)
1928 o->skip_sparse_checkout = 1;
Elijah Newren5d4f4a52023-02-27 15:28:13 +00001929 if (!o->skip_sparse_checkout) {
Elijah Newren30e89c12020-03-27 00:48:51 +00001930 memset(&pl, 0, sizeof(pl));
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001931 free_pattern_list = 1;
Elijah Newren30e89c12020-03-27 00:48:51 +00001932 populate_from_existing_patterns(o, &pl);
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001933 }
1934
Elijah Newren13e1fd62023-02-27 15:28:17 +00001935 index_state_init(&o->internal.result, o->src_index->repo);
1936 o->internal.result.initialized = 1;
1937 o->internal.result.timestamp.sec = o->src_index->timestamp.sec;
1938 o->internal.result.timestamp.nsec = o->src_index->timestamp.nsec;
1939 o->internal.result.version = o->src_index->version;
Elijah Newren7db11832018-04-23 23:50:45 -07001940 if (!o->src_index->split_index) {
Elijah Newren13e1fd62023-02-27 15:28:17 +00001941 o->internal.result.split_index = NULL;
Elijah Newren7db11832018-04-23 23:50:45 -07001942 } else if (o->src_index == o->dst_index) {
1943 /*
1944 * o->dst_index (and thus o->src_index) will be discarded
Elijah Newren0d680a72023-02-27 15:28:18 +00001945 * and overwritten with o->internal.result at the end of
1946 * this function, so just use src_index's split_index to
1947 * avoid having to create a new one.
Elijah Newren7db11832018-04-23 23:50:45 -07001948 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00001949 o->internal.result.split_index = o->src_index->split_index;
Johannes Schindelin061dd722023-03-26 22:45:43 +00001950 if (o->src_index->cache_changed & SPLIT_INDEX_ORDERED)
Junio C Hamanof315a8b2023-04-04 14:28:27 -07001951 o->internal.result.cache_changed |= SPLIT_INDEX_ORDERED;
Elijah Newren13e1fd62023-02-27 15:28:17 +00001952 o->internal.result.split_index->refcount++;
Elijah Newren7db11832018-04-23 23:50:45 -07001953 } else {
Elijah Newren0d680a72023-02-27 15:28:18 +00001954 o->internal.result.split_index =
1955 init_split_index(&o->internal.result);
Elijah Newren7db11832018-04-23 23:50:45 -07001956 }
Elijah Newren13e1fd62023-02-27 15:28:17 +00001957 oidcpy(&o->internal.result.oid, &o->src_index->oid);
Elijah Newren1ca13dd2023-02-27 15:28:19 +00001958 o->internal.merge_size = len;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001959 mark_all_ce_unused(o->src_index);
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001960
Elijah Newren13e1fd62023-02-27 15:28:17 +00001961 o->internal.result.fsmonitor_last_update =
Johannes Schindelin3dfd3052021-03-17 15:30:48 +00001962 xstrdup_or_null(o->src_index->fsmonitor_last_update);
Elijah Newren13e1fd62023-02-27 15:28:17 +00001963 o->internal.result.fsmonitor_has_run_once = o->src_index->fsmonitor_has_run_once;
Utsav Shah679f2f92019-11-20 08:32:17 +00001964
Victoria Dye0f329b92022-05-10 23:32:32 +00001965 if (!o->src_index->initialized &&
1966 !repo->settings.command_requires_full_index &&
Elijah Newren13e1fd62023-02-27 15:28:17 +00001967 is_sparse_index_allowed(&o->internal.result, 0))
1968 o->internal.result.sparse_index = 1;
Victoria Dye0f329b92022-05-10 23:32:32 +00001969
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001970 /*
1971 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1972 */
1973 if (!o->skip_sparse_checkout)
Elijah Newren576de3d2023-02-27 15:28:16 +00001974 mark_new_skip_worktree(o->internal.pl, o->src_index, 0,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001975 CE_NEW_SKIP_WORKTREE, o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001976
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001977 if (!dfc)
Jeff King13494ed2008-10-23 04:30:58 +00001978 dfc = xcalloc(1, cache_entry_size(0));
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001979 o->df_conflict_entry = dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001980
1981 if (len) {
Linus Torvalds01904572008-03-05 20:15:44 -08001982 const char *prefix = o->prefix ? o->prefix : "";
1983 struct traverse_info info;
Linus Torvalds933bf402007-08-09 22:21:29 -07001984
Linus Torvalds01904572008-03-05 20:15:44 -08001985 setup_traverse_info(&info, prefix);
1986 info.fn = unpack_callback;
1987 info.data = o;
Elijah Newren13e1fd62023-02-27 15:28:17 +00001988 info.show_all_errors = o->internal.show_all_errors;
Junio C Hamano40e37252011-08-29 12:31:06 -07001989 info.pathspec = o->pathspec;
Linus Torvalds01904572008-03-05 20:15:44 -08001990
Junio C Hamanoda165f42010-01-07 14:59:54 -08001991 if (o->prefix) {
1992 /*
1993 * Unpack existing index entries that sort before the
1994 * prefix the tree is spliced into. Note that o->merge
1995 * is always true in this case.
1996 */
1997 while (1) {
Victoria Dye99430aa2022-03-17 15:55:36 +00001998 struct cache_entry *ce = next_cache_entry(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001999 if (!ce)
2000 break;
2001 if (ce_in_traverse_path(ce, &info))
2002 break;
2003 if (unpack_index_entry(ce, o) < 0)
2004 goto return_failed;
2005 }
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07002006 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08002007
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02002008 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00002009 trace2_region_enter("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01002010 ret = traverse_trees(o->src_index, len, t, &info);
Derrick Stoleec3388982021-01-04 03:09:11 +00002011 trace2_region_leave("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02002012 trace_performance_leave("traverse_trees");
2013 if (ret < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08002014 goto return_failed;
Linus Torvalds01904572008-03-05 20:15:44 -08002015 }
2016
2017 /* Any left-over entries in the index? */
2018 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08002019 while (1) {
Victoria Dye99430aa2022-03-17 15:55:36 +00002020 struct cache_entry *ce = next_cache_entry(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002021 if (!ce)
2022 break;
Linus Torvalds01904572008-03-05 20:15:44 -08002023 if (unpack_index_entry(ce, o) < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08002024 goto return_failed;
Daniel Barkalow17e46422008-02-07 11:39:52 -05002025 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002026 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08002027 mark_all_ce_unused(o->src_index);
Johannes Schindelin16da1342006-07-30 20:25:18 +02002028
Elijah Newren13e1fd62023-02-27 15:28:17 +00002029 if (o->trivial_merges_only && o->internal.nontrivial_merge) {
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07002030 ret = unpack_failed(o, "Merge requires file-level merging");
2031 goto done;
2032 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002033
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002034 if (!o->skip_sparse_checkout) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002035 /*
2036 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
Elijah Newren031ba552020-03-27 00:48:44 +00002037 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002038 * so apply_sparse_checkout() won't attempt to remove it from worktree
2039 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002040 mark_new_skip_worktree(o->internal.pl, &o->internal.result,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00002041 CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE,
2042 o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002043
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10002044 ret = 0;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002045 for (i = 0; i < o->internal.result.cache_nr; i++) {
2046 struct cache_entry *ce = o->internal.result.cache[i];
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002047
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002048 /*
2049 * Entries marked with CE_ADDED in merged_entry() do not have
2050 * verify_absent() check (the check is effectively disabled
2051 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2052 *
2053 * Do the real check now because we have had
2054 * correct CE_NEW_SKIP_WORKTREE
2055 */
2056 if (ce->ce_flags & CE_ADDED &&
Elijah Newren681c6372020-03-27 00:49:00 +00002057 verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
2058 ret = 1;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002059
Elijah Newren13e1fd62023-02-27 15:28:17 +00002060 if (apply_sparse_checkout(&o->internal.result, ce, o))
Elijah Newren681c6372020-03-27 00:49:00 +00002061 ret = 1;
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07002062 }
Elijah Newren681c6372020-03-27 00:49:00 +00002063 if (ret == 1) {
2064 /*
2065 * Inability to sparsify or de-sparsify individual
2066 * paths is not an error, but just a warning.
2067 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002068 if (o->internal.show_all_errors)
Elijah Newren681c6372020-03-27 00:49:00 +00002069 display_warning_msgs(o);
2070 ret = 0;
2071 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002072 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002073
Elijah Newren13e1fd62023-02-27 15:28:17 +00002074 ret = check_updates(o, &o->internal.result) ? (-2) : 0;
Felipe Contrerase28f7642013-08-13 20:27:58 +02002075 if (o->dst_index) {
Elijah Newren13e1fd62023-02-27 15:28:17 +00002076 move_index_extensions(&o->internal.result, o->src_index);
Brian Degenhardt52fca212015-07-28 15:30:40 -04002077 if (!ret) {
Patrick Steinhardt9f119592024-10-07 06:38:15 +02002078 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0) &&
2079 cache_tree_verify(the_repository,
2080 &o->internal.result) < 0) {
2081 ret = -1;
2082 goto done;
2083 }
2084
Victoria Dye68fcd482022-11-10 19:06:02 +00002085 if (!o->skip_cache_tree_update &&
Elijah Newren13e1fd62023-02-27 15:28:17 +00002086 !cache_tree_fully_valid(o->internal.result.cache_tree))
2087 cache_tree_update(&o->internal.result,
Brian Degenhardt52fca212015-07-28 15:30:40 -04002088 WRITE_TREE_SILENT |
2089 WRITE_TREE_REPAIR);
2090 }
Ben Peart1956ecd2019-02-15 12:59:21 -05002091
Elijah Newren13e1fd62023-02-27 15:28:17 +00002092 o->internal.result.updated_workdir = 1;
Felipe Contrerase28f7642013-08-13 20:27:58 +02002093 discard_index(o->dst_index);
Elijah Newren13e1fd62023-02-27 15:28:17 +00002094 *o->dst_index = o->internal.result;
Patrick Steinhardt4dfd4f12024-08-14 08:52:39 +02002095 memset(&o->internal.result, 0, sizeof(o->internal.result));
Junio C Hamanoa16cc8b2014-11-17 12:12:41 -08002096 } else {
Elijah Newren13e1fd62023-02-27 15:28:17 +00002097 discard_index(&o->internal.result);
Felipe Contrerase28f7642013-08-13 20:27:58 +02002098 }
Elijah Newren7db11832018-04-23 23:50:45 -07002099 o->src_index = NULL;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07002100
2101done:
Elijah Newrenfa0bde42020-03-27 00:48:47 +00002102 if (free_pattern_list)
Derrick Stoleee0912282019-11-21 22:04:46 +00002103 clear_pattern_list(&pl);
Elijah Newren576de3d2023-02-27 15:28:16 +00002104 if (o->internal.dir) {
2105 dir_clear(o->internal.dir);
2106 o->internal.dir = NULL;
Elijah Newren04988c82021-09-27 16:33:41 +00002107 }
Derrick Stoleec3388982021-01-04 03:09:11 +00002108 trace2_region_leave("unpack_trees", "unpack_trees", the_repository);
Elijah Newrenfa0bde42020-03-27 00:48:47 +00002109 trace_performance_leave("unpack_trees");
Junio C Hamano2e2b8872008-05-28 15:12:30 -07002110 return ret;
Junio C Hamanoda165f42010-01-07 14:59:54 -08002111
2112return_failed:
Elijah Newren13e1fd62023-02-27 15:28:17 +00002113 if (o->internal.show_all_errors)
Matthieu Moye6c111b2010-08-11 10:38:07 +02002114 display_error_msgs(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002115 mark_all_ce_unused(o->src_index);
Junio C Hamano026680f2010-01-24 17:35:58 -08002116 ret = unpack_failed(o, NULL);
Junio C Hamanob4194822011-05-31 10:06:44 -07002117 if (o->exiting_early)
2118 ret = 0;
Junio C Hamano026680f2010-01-24 17:35:58 -08002119 goto done;
Johannes Schindelin16da1342006-07-30 20:25:18 +02002120}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002121
Elijah Newren7af7a252020-03-27 00:48:52 +00002122/*
2123 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2124 * working directory to match.
2125 *
2126 * CE_NEW_SKIP_WORKTREE is used internally.
2127 */
Elijah Newren1147c562023-02-27 15:28:14 +00002128enum update_sparsity_result update_sparsity(struct unpack_trees_options *o,
2129 struct pattern_list *pl)
Elijah Newren7af7a252020-03-27 00:48:52 +00002130{
2131 enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
Derrick Stoleeace224a2020-05-04 18:27:43 +00002132 int i;
Elijah Newren7af7a252020-03-27 00:48:52 +00002133 unsigned old_show_all_errors;
2134 int free_pattern_list = 0;
2135
Elijah Newren13e1fd62023-02-27 15:28:17 +00002136 old_show_all_errors = o->internal.show_all_errors;
2137 o->internal.show_all_errors = 1;
2138 index_state_init(&o->internal.result, o->src_index->repo);
Elijah Newren7af7a252020-03-27 00:48:52 +00002139
2140 /* Sanity checks */
2141 if (!o->update || o->index_only || o->skip_sparse_checkout)
2142 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2143 if (o->src_index != o->dst_index || o->fn)
2144 BUG("update_sparsity() called wrong");
2145
2146 trace_performance_enter();
2147
2148 /* If we weren't given patterns, use the recorded ones */
Elijah Newren1147c562023-02-27 15:28:14 +00002149 if (!pl) {
Elijah Newren7af7a252020-03-27 00:48:52 +00002150 free_pattern_list = 1;
Elijah Newren1147c562023-02-27 15:28:14 +00002151 pl = xcalloc(1, sizeof(*pl));
2152 populate_from_existing_patterns(o, pl);
Elijah Newren7af7a252020-03-27 00:48:52 +00002153 }
Elijah Newren576de3d2023-02-27 15:28:16 +00002154 o->internal.pl = pl;
Elijah Newren7af7a252020-03-27 00:48:52 +00002155
Derrick Stolee598b1e72022-05-23 13:48:46 +00002156 /* Expand sparse directories as needed */
Elijah Newren576de3d2023-02-27 15:28:16 +00002157 expand_index(o->src_index, o->internal.pl);
Derrick Stolee598b1e72022-05-23 13:48:46 +00002158
Elijah Newren7af7a252020-03-27 00:48:52 +00002159 /* Set NEW_SKIP_WORKTREE on existing entries. */
2160 mark_all_ce_unused(o->src_index);
Elijah Newren576de3d2023-02-27 15:28:16 +00002161 mark_new_skip_worktree(o->internal.pl, o->src_index, 0,
Elijah Newren7af7a252020-03-27 00:48:52 +00002162 CE_NEW_SKIP_WORKTREE, o->verbose_update);
2163
2164 /* Then loop over entries and update/remove as needed */
2165 ret = UPDATE_SPARSITY_SUCCESS;
Elijah Newren7af7a252020-03-27 00:48:52 +00002166 for (i = 0; i < o->src_index->cache_nr; i++) {
2167 struct cache_entry *ce = o->src_index->cache[i];
2168
Elijah Newrenebb568b2020-03-27 00:48:59 +00002169
2170 if (ce_stage(ce)) {
2171 /* -1 because for loop will increment by 1 */
2172 i += warn_conflicted_path(o->src_index, i, o) - 1;
2173 ret = UPDATE_SPARSITY_WARNINGS;
2174 continue;
2175 }
2176
Elijah Newren7af7a252020-03-27 00:48:52 +00002177 if (apply_sparse_checkout(o->src_index, ce, o))
2178 ret = UPDATE_SPARSITY_WARNINGS;
Elijah Newren7af7a252020-03-27 00:48:52 +00002179 }
2180
Elijah Newren7af7a252020-03-27 00:48:52 +00002181 if (check_updates(o, o->src_index))
2182 ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
2183
Elijah Newren6271d772020-03-27 00:48:57 +00002184 display_warning_msgs(o);
Elijah Newren13e1fd62023-02-27 15:28:17 +00002185 o->internal.show_all_errors = old_show_all_errors;
Elijah Newren1147c562023-02-27 15:28:14 +00002186 if (free_pattern_list) {
2187 clear_pattern_list(pl);
2188 free(pl);
Elijah Newren576de3d2023-02-27 15:28:16 +00002189 o->internal.pl = NULL;
Elijah Newren1147c562023-02-27 15:28:14 +00002190 }
Elijah Newren7af7a252020-03-27 00:48:52 +00002191 trace_performance_leave("update_sparsity");
2192 return ret;
2193}
2194
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002195/* Here come the merge functions */
2196
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002197static int reject_merge(const struct cache_entry *ce,
2198 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002199{
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002200 return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002201}
2202
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002203static int same(const struct cache_entry *a, const struct cache_entry *b)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002204{
2205 if (!!a != !!b)
2206 return 0;
2207 if (!a && !b)
2208 return 1;
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002209 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
2210 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002211 return a->ce_mode == b->ce_mode &&
Jeff King4a7e27e2018-08-28 17:22:40 -04002212 oideq(&a->oid, &b->oid);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002213}
2214
2215
2216/*
2217 * When a CE gets turned into an unmerged entry, we
2218 * want it to be up-to-date
2219 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002220static int verify_uptodate_1(const struct cache_entry *ce,
2221 struct unpack_trees_options *o,
2222 enum unpack_trees_error_types error_type)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002223{
2224 struct stat st;
2225
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002226 if (o->index_only)
2227 return 0;
2228
2229 /*
2230 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2231 * if this entry is truly up-to-date because this file may be
2232 * overwritten.
2233 */
2234 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
2235 ; /* keep checking */
2236 else if (o->reset || ce_uptodate(ce))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002237 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002238
2239 if (!lstat(ce->name, &st)) {
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002240 int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
2241 unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002242
2243 if (submodule_from_ce(ce)) {
2244 int r = check_submodule_move_head(ce,
2245 "HEAD", oid_to_hex(&ce->oid), o);
2246 if (r)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002247 return add_rejected_path(o, error_type, ce->name);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002248 return 0;
2249 }
2250
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002251 if (!changed)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002252 return 0;
Junio C Hamano936492d2007-08-03 22:13:09 -07002253 /*
Stefan Bellera7bc8452017-03-14 14:46:39 -07002254 * Historic default policy was to allow submodule to be out
2255 * of sync wrt the superproject index. If the submodule was
2256 * not considered interesting above, we don't care here.
Junio C Hamano936492d2007-08-03 22:13:09 -07002257 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002258 if (S_ISGITLINK(ce->ce_mode))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002259 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -07002260
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002261 errno = 0;
2262 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002263 if (errno == ENOENT)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002264 return 0;
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002265 return add_rejected_path(o, error_type, ce->name);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002266}
2267
Elijah Newren64b1abe2018-04-19 10:58:12 -07002268int verify_uptodate(const struct cache_entry *ce,
2269 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002270{
Elijah Newren26b5d6b2022-01-14 15:59:40 +00002271 if (!o->skip_sparse_checkout &&
2272 (ce->ce_flags & CE_SKIP_WORKTREE) &&
2273 (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002274 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02002275 return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002276}
2277
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002278static int verify_uptodate_sparse(const struct cache_entry *ce,
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002279 struct unpack_trees_options *o)
2280{
Elijah Newren1ac83f42020-03-27 00:48:56 +00002281 return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002282}
2283
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002284/*
Elijah Newren13e1fd62023-02-27 15:28:17 +00002285 * TODO: We should actually invalidate o->internal.result, not src_index [1].
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002286 * But since cache tree and untracked cache both are not copied to
Elijah Newren13e1fd62023-02-27 15:28:17 +00002287 * o->internal.result until unpacking is complete, we invalidate them on
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002288 * src_index instead with the assumption that they will be copied to
2289 * dst_index at the end.
2290 *
2291 * [1] src_index->cache_tree is also used in unpack_callback() so if
Elijah Newren13e1fd62023-02-27 15:28:17 +00002292 * we invalidate o->internal.result, we need to update it to use
2293 * o->internal.result.cache_tree as well.
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002294 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002295static void invalidate_ce_path(const struct cache_entry *ce,
2296 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002297{
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002298 if (!ce)
2299 return;
2300 cache_tree_invalidate_path(o->src_index, ce->name);
Nguyễn Thái Ngọc Duy0cacebf2018-02-07 16:21:40 +07002301 untracked_cache_invalidate_path(o->src_index, ce->name, 1);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002302}
2303
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002304/*
2305 * Check that checking out ce->sha1 in subdir ce->name is not
2306 * going to overwrite any working files.
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002307 */
Stefan Bellerd6b12302017-03-14 14:46:38 -07002308static int verify_clean_submodule(const char *old_sha1,
2309 const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002310 struct unpack_trees_options *o)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002311{
Stefan Bellera7bc8452017-03-14 14:46:39 -07002312 if (!submodule_from_ce(ce))
2313 return 0;
2314
2315 return check_submodule_move_head(ce, old_sha1,
2316 oid_to_hex(&ce->oid), o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002317}
2318
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002319static int verify_clean_subdirectory(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002320 struct unpack_trees_options *o)
Junio C Hamanoc8193532007-03-15 23:25:22 -07002321{
2322 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002323 * we are about to extract "ce->name"; we would not want to lose
Junio C Hamanoc8193532007-03-15 23:25:22 -07002324 * anything in the existing directory there.
2325 */
2326 int namelen;
Clemens Buchacher7b9e3ce2009-01-01 21:54:33 +01002327 int i;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002328 struct dir_struct d;
2329 char *pathbuf;
2330 int cnt = 0;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002331
Stefan Bellerd6b12302017-03-14 14:46:38 -07002332 if (S_ISGITLINK(ce->ce_mode)) {
brian m. carlson1053fe82017-10-15 22:07:06 +00002333 struct object_id oid;
Patrick Steinhardte19488a2024-05-17 10:18:39 +02002334 int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name,
2335 "HEAD", &oid);
Stefan Bellerd6b12302017-03-14 14:46:38 -07002336 /*
2337 * If we are not going to update the submodule, then
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002338 * we don't care.
2339 */
Jeff King4a7e27e2018-08-28 17:22:40 -04002340 if (!sub_head && oideq(&oid, &ce->oid))
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002341 return 0;
brian m. carlson1053fe82017-10-15 22:07:06 +00002342 return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),
Jeff Kingdf351c62019-03-20 04:15:27 -04002343 ce, o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002344 }
Junio C Hamanoc8193532007-03-15 23:25:22 -07002345
2346 /*
2347 * First let's make sure we do not have a local modification
2348 * in that directory.
2349 */
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02002350 namelen = ce_namelen(ce);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002351 for (i = locate_in_src_index(ce, o);
2352 i < o->src_index->cache_nr;
2353 i++) {
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002354 struct cache_entry *ce2 = o->src_index->cache[i];
2355 int len = ce_namelen(ce2);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002356 if (len < namelen ||
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002357 strncmp(ce->name, ce2->name, namelen) ||
2358 ce2->name[namelen] != '/')
Junio C Hamanoc8193532007-03-15 23:25:22 -07002359 break;
2360 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08002361 * ce2->name is an entry in the subdirectory to be
2362 * removed.
Junio C Hamanoc8193532007-03-15 23:25:22 -07002363 */
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002364 if (!ce_stage(ce2)) {
2365 if (verify_uptodate(ce2, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002366 return -1;
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002367 add_entry(o, ce2, CE_REMOVE, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002368 invalidate_ce_path(ce, o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002369 mark_ce_used(ce2, o);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002370 }
2371 cnt++;
2372 }
2373
Elijah Newrenb817e542021-12-09 05:08:27 +00002374 /* Do not lose a locally present file that is not ignored. */
Jeff King75faa452015-09-24 17:07:03 -04002375 pathbuf = xstrfmt("%.*s/", namelen, ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002376
2377 memset(&d, 0, sizeof(d));
Elijah Newren576de3d2023-02-27 15:28:16 +00002378 if (o->internal.dir)
Elijah Newrenb413a822023-02-27 15:28:09 +00002379 setup_standard_excludes(&d);
Nguyễn Thái Ngọc Duyc7f32592018-08-13 18:14:29 +02002380 i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
Ævar Arnfjörð Bjarmasone5a917f2021-10-07 11:46:09 +02002381 dir_clear(&d);
2382 free(pathbuf);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002383 if (i)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002384 return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
Elijah Newrenb817e542021-12-09 05:08:27 +00002385
2386 /* Do not lose startup_info->original_cwd */
2387 if (startup_info->original_cwd &&
2388 !strcmp(startup_info->original_cwd, ce->name))
2389 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
2390
Junio C Hamanoc8193532007-03-15 23:25:22 -07002391 return cnt;
2392}
2393
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002394/*
Linus Torvalds32260ad2008-03-22 09:35:59 -07002395 * This gets called when there was no index entry for the tree entry 'dst',
2396 * but we found a file in the working tree that 'lstat()' said was fine,
2397 * and we're on a case-insensitive filesystem.
2398 *
2399 * See if we can find a case-insensitive match in the index that also
2400 * matches the stat information, and assume it's that other file!
2401 */
Clemens Buchachera9307f52010-10-09 15:52:58 +02002402static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
Linus Torvalds32260ad2008-03-22 09:35:59 -07002403{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002404 const struct cache_entry *src;
Linus Torvalds32260ad2008-03-22 09:35:59 -07002405
Eric Sunshineebbd7432013-09-17 03:06:15 -04002406 src = index_file_exists(o->src_index, name, len, 1);
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07002407 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 -07002408}
2409
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002410enum absent_checking_type {
2411 COMPLETELY_ABSENT,
2412 ABSENT_ANY_DIRECTORY
2413};
2414
Clemens Buchachera9307f52010-10-09 15:52:58 +02002415static int check_ok_to_remove(const char *name, int len, int dtype,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002416 const struct cache_entry *ce, struct stat *st,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002417 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002418 enum absent_checking_type absent_type,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002419 struct unpack_trees_options *o)
2420{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002421 const struct cache_entry *result;
Clemens Buchachera9307f52010-10-09 15:52:58 +02002422
2423 /*
2424 * It may be that the 'lstat()' succeeded even though
2425 * target 'ce' was absent, because there is an old
2426 * entry that is different only in case..
2427 *
2428 * Ignore that lstat() if it matches.
2429 */
2430 if (ignore_case && icase_exists(o, name, len, st))
2431 return 0;
2432
Elijah Newren576de3d2023-02-27 15:28:16 +00002433 if (o->internal.dir &&
2434 is_excluded(o->internal.dir, o->src_index, name, &dtype))
Clemens Buchachera9307f52010-10-09 15:52:58 +02002435 /*
2436 * ce->name is explicitly excluded, so it is Ok to
2437 * overwrite it.
2438 */
2439 return 0;
2440 if (S_ISDIR(st->st_mode)) {
2441 /*
2442 * We are checking out path "foo" and
2443 * found "foo/." in the working tree.
2444 * This is tricky -- if we have modified
2445 * files that are in "foo/" we would lose
2446 * them.
2447 */
Jeff Kingdf351c62019-03-20 04:15:27 -04002448 if (verify_clean_subdirectory(ce, o) < 0)
Clemens Buchachera9307f52010-10-09 15:52:58 +02002449 return -1;
2450 return 0;
2451 }
2452
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002453 /* If we only care about directories, then we can remove */
2454 if (absent_type == ABSENT_ANY_DIRECTORY)
2455 return 0;
2456
Clemens Buchachera9307f52010-10-09 15:52:58 +02002457 /*
2458 * The previous round may already have decided to
2459 * delete this path, which is in a subdirectory that
2460 * is being replaced with a blob.
2461 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002462 result = index_file_exists(&o->internal.result, name, len, 0);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002463 if (result) {
2464 if (result->ce_flags & CE_REMOVE)
2465 return 0;
2466 }
2467
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002468 return add_rejected_path(o, error_type, name);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002469}
2470
Linus Torvalds32260ad2008-03-22 09:35:59 -07002471/*
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002472 * We do not want to remove or overwrite a working tree file that
Junio C Hamanof8a9d422006-12-04 16:00:46 -08002473 * is not tracked, unless it is ignored.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002474 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002475static int verify_absent_1(const struct cache_entry *ce,
2476 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002477 enum absent_checking_type absent_type,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002478 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002479{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002480 int len;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002481 struct stat st;
2482
Elijah Newren0b0ee332021-12-09 05:08:28 +00002483 if (o->index_only || !o->update)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002484 return 0;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002485
Elijah Newren0b0ee332021-12-09 05:08:28 +00002486 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {
2487 /* Avoid nuking startup_info->original_cwd... */
2488 if (startup_info->original_cwd &&
2489 !strcmp(startup_info->original_cwd, ce->name))
2490 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,
2491 ce->name);
2492 /* ...but nuke anything else. */
2493 return 0;
2494 }
2495
Matheus Tavaresfab78a02021-03-18 15:43:47 -03002496 len = check_leading_path(ce->name, ce_namelen(ce), 0);
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002497 if (!len)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002498 return 0;
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002499 else if (len > 0) {
Jeff Kingf514ef92015-08-19 14:12:37 -04002500 char *path;
2501 int ret;
Junio C Hamanoec0603e2007-07-12 01:04:16 -07002502
Jeff Kingf514ef92015-08-19 14:12:37 -04002503 path = xmemdupz(ce->name, len);
2504 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002505 ret = error_errno("cannot stat '%s'", path);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002506 else {
2507 if (submodule_from_ce(ce))
2508 ret = check_submodule_move_head(ce,
2509 oid_to_hex(&ce->oid),
2510 NULL, o);
2511 else
2512 ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002513 &st, error_type,
2514 absent_type, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002515 }
Jeff Kingf514ef92015-08-19 14:12:37 -04002516 free(path);
2517 return ret;
Jonathan Nieder92fda792011-01-12 20:26:36 -06002518 } else if (lstat(ce->name, &st)) {
2519 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002520 return error_errno("cannot stat '%s'", ce->name);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002521 return 0;
2522 } else {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002523 if (submodule_from_ce(ce))
2524 return check_submodule_move_head(ce, oid_to_hex(&ce->oid),
2525 NULL, o);
2526
Clemens Buchachera9307f52010-10-09 15:52:58 +02002527 return check_ok_to_remove(ce->name, ce_namelen(ce),
Jonathan Nieder92fda792011-01-12 20:26:36 -06002528 ce_to_dtype(ce), ce, &st,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002529 error_type, absent_type, o);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002530 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002531}
Clemens Buchachera9307f52010-10-09 15:52:58 +02002532
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002533static int verify_absent(const struct cache_entry *ce,
Matthieu Moy08402b02010-08-11 10:38:06 +02002534 enum unpack_trees_error_types error_type,
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002535 struct unpack_trees_options *o)
2536{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002537 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002538 return 0;
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002539 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
2540}
2541
2542static int verify_absent_if_directory(const struct cache_entry *ce,
2543 enum unpack_trees_error_types error_type,
2544 struct unpack_trees_options *o)
2545{
2546 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
2547 return 0;
2548 return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002549}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002550
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002551static int verify_absent_sparse(const struct cache_entry *ce,
2552 enum unpack_trees_error_types error_type,
2553 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002554{
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002555 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002556}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002557
René Scharfef2fa3542013-06-02 17:46:54 +02002558static int merged_entry(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002559 const struct cache_entry *old,
René Scharfef2fa3542013-06-02 17:46:54 +02002560 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002561{
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002562 int update = CE_UPDATE;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002563 struct cache_entry *merge = dup_cache_entry(ce, &o->internal.result);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002564
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002565 if (!old) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002566 /*
2567 * New index entries. In sparse checkout, the following
2568 * verify_absent() will be delayed until after
2569 * traverse_trees() finishes in unpack_trees(), then:
2570 *
2571 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2572 * - verify_absent() be called again, this time with
2573 * correct CE_NEW_SKIP_WORKTREE
2574 *
2575 * verify_absent() call here does nothing in sparse
2576 * checkout (i.e. o->skip_sparse_checkout == 0)
2577 */
2578 update |= CE_ADDED;
2579 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
2580
René Scharfef2fa3542013-06-02 17:46:54 +02002581 if (verify_absent(merge,
2582 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002583 discard_cache_entry(merge);
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002584 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002585 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002586 invalidate_ce_path(merge, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002587
Philippe Blaine84704f2020-02-17 04:53:05 +00002588 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002589 int ret = check_submodule_move_head(ce, NULL,
2590 oid_to_hex(&ce->oid),
2591 o);
2592 if (ret)
2593 return ret;
2594 }
2595
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002596 } else if (!(old->ce_flags & CE_CONFLICTED)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002597 /*
2598 * See if we can re-use the old CE directly?
2599 * That way we get the uptodate stat info.
2600 *
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002601 * This also removes the UPDATE flag on a match; otherwise
2602 * we will end up overwriting local changes in the work tree.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002603 */
2604 if (same(old, merge)) {
Linus Torvaldseb7a2f12008-02-22 20:41:17 -08002605 copy_cache_entry(merge, old);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002606 update = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002607 } else {
René Scharfef2fa3542013-06-02 17:46:54 +02002608 if (verify_uptodate(old, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002609 discard_cache_entry(merge);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002610 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002611 }
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002612 /* Migrate old flags over */
2613 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002614 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002615 }
Stefan Bellera7bc8452017-03-14 14:46:39 -07002616
Philippe Blaine84704f2020-02-17 04:53:05 +00002617 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002618 int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),
2619 oid_to_hex(&ce->oid),
2620 o);
2621 if (ret)
2622 return ret;
2623 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002624 } else {
2625 /*
2626 * Previously unmerged entry left as an existence
2627 * marker by read_index_unmerged();
2628 */
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002629 if (verify_absent_if_directory(merge,
2630 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
2631 discard_cache_entry(merge);
2632 return -1;
2633 }
2634
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002635 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002636 }
2637
Johannes Schindelincc756ed2019-09-09 13:56:15 +02002638 if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0)
2639 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002640 return 1;
2641}
2642
Victoria Dyeab810472022-03-01 20:24:30 +00002643static int merged_sparse_dir(const struct cache_entry * const *src, int n,
2644 struct unpack_trees_options *o)
2645{
2646 struct tree_desc t[MAX_UNPACK_TREES + 1];
2647 void * tree_bufs[MAX_UNPACK_TREES + 1];
2648 struct traverse_info info;
2649 int i, ret;
2650
2651 /*
2652 * Create the tree traversal information for traversing into *only* the
2653 * sparse directory.
2654 */
2655 setup_traverse_info(&info, src[0]->name);
2656 info.fn = unpack_sparse_callback;
2657 info.data = o;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002658 info.show_all_errors = o->internal.show_all_errors;
Victoria Dyeab810472022-03-01 20:24:30 +00002659 info.pathspec = o->pathspec;
2660
2661 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2662 for (i = 0; i < n; i++)
2663 tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i],
2664 src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL);
2665
2666 ret = traverse_trees(o->src_index, n, t, &info);
2667
2668 for (i = 0; i < n; i++)
2669 free(tree_bufs[i]);
2670
2671 return ret;
2672}
2673
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002674static int deleted_entry(const struct cache_entry *ce,
2675 const struct cache_entry *old,
2676 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002677{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002678 /* Did it exist in the index? */
2679 if (!old) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002680 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002681 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002682 return 0;
Elijah Newren56d06fe2021-09-27 16:33:46 +00002683 } else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) {
2684 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002685 }
Elijah Newren56d06fe2021-09-27 16:33:46 +00002686
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002687 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
Linus Torvalds34110cd2008-03-06 18:12:28 -08002688 return -1;
2689 add_entry(o, ce, CE_REMOVE, 0);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002690 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002691 return 1;
2692}
2693
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002694static int keep_entry(const struct cache_entry *ce,
2695 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002696{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002697 add_entry(o, ce, 0, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002698 if (ce_stage(ce))
2699 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002700 return 1;
2701}
2702
2703#if DBRT_DEBUG
2704static void show_stage_entry(FILE *o,
2705 const char *label, const struct cache_entry *ce)
2706{
2707 if (!ce)
2708 fprintf(o, "%s (missing)\n", label);
2709 else
2710 fprintf(o, "%s%06o %s %d\t%s\n",
2711 label,
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002712 ce->ce_mode,
brian m. carlson99d1a982016-09-05 20:07:52 +00002713 oid_to_hex(&ce->oid),
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002714 ce_stage(ce),
2715 ce->name);
2716}
2717#endif
2718
René Scharfe5828e832013-06-02 17:46:56 +02002719int threeway_merge(const struct cache_entry * const *stages,
2720 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002721{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002722 const struct cache_entry *index;
2723 const struct cache_entry *head;
2724 const struct cache_entry *remote = stages[o->head_idx + 1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002725 int count;
2726 int head_match = 0;
2727 int remote_match = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002728
2729 int df_conflict_head = 0;
2730 int df_conflict_remote = 0;
2731
2732 int any_anc_missing = 0;
2733 int no_anc_exists = 1;
2734 int i;
2735
2736 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002737 if (!stages[i] || stages[i] == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002738 any_anc_missing = 1;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002739 else
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002740 no_anc_exists = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002741 }
2742
2743 index = stages[0];
2744 head = stages[o->head_idx];
2745
2746 if (head == o->df_conflict_entry) {
2747 df_conflict_head = 1;
2748 head = NULL;
2749 }
2750
2751 if (remote == o->df_conflict_entry) {
2752 df_conflict_remote = 1;
2753 remote = NULL;
2754 }
2755
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002756 /*
2757 * First, if there's a #16 situation, note that to prevent #13
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002758 * and #14.
2759 */
2760 if (!same(remote, head)) {
2761 for (i = 1; i < o->head_idx; i++) {
2762 if (same(stages[i], head)) {
2763 head_match = i;
2764 }
2765 if (same(stages[i], remote)) {
2766 remote_match = i;
2767 }
2768 }
2769 }
2770
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002771 /*
2772 * We start with cases where the index is allowed to match
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002773 * something other than the head: #14(ALT) and #2ALT, where it
2774 * is permitted to match the result instead.
2775 */
2776 /* #14, #14ALT, #2ALT */
2777 if (remote && !df_conflict_head && head_match && !remote_match) {
Victoria Dyef27c1702022-03-01 20:24:31 +00002778 if (index && !same(index, remote) && !same(index, head)) {
2779 if (S_ISSPARSEDIR(index->ce_mode))
2780 return merged_sparse_dir(stages, 4, o);
2781 else
2782 return reject_merge(index, o);
2783 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002784 return merged_entry(remote, index, o);
2785 }
2786 /*
2787 * If we have an entry in the index cache, then we want to
2788 * make sure that it matches head.
2789 */
Victoria Dyef27c1702022-03-01 20:24:31 +00002790 if (index && !same(index, head)) {
2791 if (S_ISSPARSEDIR(index->ce_mode))
2792 return merged_sparse_dir(stages, 4, o);
2793 else
2794 return reject_merge(index, o);
2795 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002796
2797 if (head) {
2798 /* #5ALT, #15 */
2799 if (same(head, remote))
2800 return merged_entry(head, index, o);
2801 /* #13, #3ALT */
2802 if (!df_conflict_remote && remote_match && !head_match)
2803 return merged_entry(head, index, o);
2804 }
2805
2806 /* #1 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08002807 if (!head && !remote && any_anc_missing)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002808 return 0;
2809
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002810 /*
2811 * Under the "aggressive" rule, we resolve mostly trivial
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002812 * cases that we historically had git-merge-one-file resolve.
2813 */
2814 if (o->aggressive) {
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002815 int head_deleted = !head;
2816 int remote_deleted = !remote;
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002817 const struct cache_entry *ce = NULL;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002818
2819 if (index)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002820 ce = index;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002821 else if (head)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002822 ce = head;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002823 else if (remote)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002824 ce = remote;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002825 else {
2826 for (i = 1; i < o->head_idx; i++) {
2827 if (stages[i] && stages[i] != o->df_conflict_entry) {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002828 ce = stages[i];
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002829 break;
2830 }
2831 }
2832 }
2833
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002834 /*
2835 * Deleted in both.
2836 * Deleted in one and unchanged in the other.
2837 */
2838 if ((head_deleted && remote_deleted) ||
2839 (head_deleted && remote && remote_match) ||
2840 (remote_deleted && head && head_match)) {
2841 if (index)
2842 return deleted_entry(index, index, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08002843 if (ce && !head_deleted) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002844 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002845 return -1;
2846 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002847 return 0;
2848 }
2849 /*
2850 * Added in both, identically.
2851 */
2852 if (no_anc_exists && head && remote && same(head, remote))
2853 return merged_entry(head, index, o);
2854
2855 }
2856
Victoria Dyef27c1702022-03-01 20:24:31 +00002857 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002858 if (index) {
Victoria Dyef27c1702022-03-01 20:24:31 +00002859 /*
2860 * If we've reached the "no merge" cases and we're merging
2861 * a sparse directory, we may have an "edit/edit" conflict that
2862 * can be resolved by individually merging directory contents.
2863 */
2864 if (S_ISSPARSEDIR(index->ce_mode))
2865 return merged_sparse_dir(stages, 4, o);
2866
2867 /*
2868 * If we're not merging a sparse directory, ensure the index is
2869 * up-to-date to avoid files getting overwritten with conflict
2870 * resolution files
2871 */
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002872 if (verify_uptodate(index, o))
2873 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002874 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002875
Elijah Newren13e1fd62023-02-27 15:28:17 +00002876 o->internal.nontrivial_merge = 1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002877
Junio C Hamanoea4b52a2007-04-07 05:42:01 -07002878 /* #2, #3, #4, #6, #7, #9, #10, #11. */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002879 count = 0;
2880 if (!head_match || !remote_match) {
2881 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002882 if (stages[i] && stages[i] != o->df_conflict_entry) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002883 keep_entry(stages[i], o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002884 count++;
2885 break;
2886 }
2887 }
2888 }
2889#if DBRT_DEBUG
2890 else {
2891 fprintf(stderr, "read-tree: warning #16 detected\n");
2892 show_stage_entry(stderr, "head ", stages[head_match]);
2893 show_stage_entry(stderr, "remote ", stages[remote_match]);
2894 }
2895#endif
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002896 if (head) { count += keep_entry(head, o); }
2897 if (remote) { count += keep_entry(remote, o); }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002898 return count;
2899}
2900
2901/*
2902 * Two-way merge.
2903 *
2904 * The rule is to "carry forward" what is in the index without losing
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03002905 * information across a "fast-forward", favoring a successful merge
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002906 * over a merge failure when it makes sense. For details of the
Todd Zullingerd6b67ce2025-03-03 15:44:31 -05002907 * "carry forward" rule, please see <Documentation/git-read-tree.adoc>.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002908 *
2909 */
René Scharfe5828e832013-06-02 17:46:56 +02002910int twoway_merge(const struct cache_entry * const *src,
2911 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002912{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002913 const struct cache_entry *current = src[0];
2914 const struct cache_entry *oldtree = src[1];
2915 const struct cache_entry *newtree = src[2];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002916
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002917 if (o->internal.merge_size != 2)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002918 return error("Cannot do a twoway merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002919 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002920
Junio C Hamanob8ba1532007-04-02 16:29:56 -07002921 if (oldtree == o->df_conflict_entry)
2922 oldtree = NULL;
2923 if (newtree == o->df_conflict_entry)
2924 newtree = NULL;
2925
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002926 if (current) {
Jeff Kingb018ff62012-12-29 15:51:54 -05002927 if (current->ce_flags & CE_CONFLICTED) {
2928 if (same(oldtree, newtree) || o->reset) {
2929 if (!newtree)
2930 return deleted_entry(current, current, o);
2931 else
2932 return merged_entry(newtree, current, o);
2933 }
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002934 return reject_merge(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002935 } else if ((!oldtree && !newtree) || /* 4 and 5 */
Jeff Kingb018ff62012-12-29 15:51:54 -05002936 (!oldtree && newtree &&
2937 same(current, newtree)) || /* 6 and 7 */
2938 (oldtree && newtree &&
2939 same(oldtree, newtree)) || /* 14 and 15 */
2940 (oldtree && newtree &&
2941 !same(oldtree, newtree) && /* 18 and 19 */
2942 same(current, newtree))) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002943 return keep_entry(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002944 } else if (oldtree && !newtree && same(current, oldtree)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002945 /* 10 or 11 */
2946 return deleted_entry(oldtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002947 } else if (oldtree && newtree &&
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002948 same(current, oldtree) && !same(current, newtree)) {
2949 /* 20 or 21 */
2950 return merged_entry(newtree, current, o);
Derrick Stoleee05cdb12021-07-20 20:14:41 +00002951 } else if (current && !oldtree && newtree &&
2952 S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) &&
2953 ce_stage(current) == 0) {
2954 /*
2955 * This case is a directory/file conflict across the sparse-index
2956 * boundary. When we are changing from one path to another via
2957 * 'git checkout', then we want to replace one entry with another
2958 * via merged_entry(). If there are staged changes, then we should
2959 * reject the merge instead.
2960 */
2961 return merged_entry(newtree, current, o);
Victoria Dyeab810472022-03-01 20:24:30 +00002962 } else if (S_ISSPARSEDIR(current->ce_mode)) {
2963 /*
2964 * The sparse directories differ, but we don't know whether that's
2965 * because of two different files in the directory being modified
2966 * (can be trivially merged) or if there is a real file conflict.
2967 * Merge the sparse directory by OID to compare file-by-file.
2968 */
2969 return merged_sparse_dir(src, 3, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002970 } else
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002971 return reject_merge(current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002972 }
Junio C Hamano55218832008-09-07 19:49:25 -07002973 else if (newtree) {
2974 if (oldtree && !o->initial_checkout) {
2975 /*
2976 * deletion of the path was staged;
2977 */
2978 if (same(oldtree, newtree))
2979 return 1;
2980 return reject_merge(oldtree, o);
2981 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002982 return merged_entry(newtree, current, o);
Junio C Hamano55218832008-09-07 19:49:25 -07002983 }
Linus Torvaldsd6996762007-08-10 12:31:20 -07002984 return deleted_entry(oldtree, current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002985}
2986
2987/*
2988 * Bind merge.
2989 *
2990 * Keep the index entries at stage0, collapse stage1 but make sure
2991 * stage0 does not have anything there.
2992 */
René Scharfe5828e832013-06-02 17:46:56 +02002993int bind_merge(const struct cache_entry * const *src,
2994 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002995{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002996 const struct cache_entry *old = src[0];
2997 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002998
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002999 if (o->internal.merge_size != 1)
Pete Wyckoff82247e92012-04-29 20:28:45 -04003000 return error("Cannot do a bind merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003001 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003002 if (a && old)
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07003003 return o->quiet ? -1 :
Stefan Beller3d415422017-01-17 17:05:20 -08003004 error(ERRORMSG(o, ERROR_BIND_OVERLAP),
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +01003005 super_prefixed(a->name, o->super_prefix),
3006 super_prefixed(old->name, o->super_prefix));
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003007 if (!a)
Junio C Hamano7f7932a2007-04-02 00:06:12 -07003008 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003009 else
3010 return merged_entry(a, NULL, o);
3011}
3012
3013/*
3014 * One-way merge.
3015 *
3016 * The rule is:
3017 * - take the stat information from stage0, take the data from stage1
3018 */
René Scharfe5828e832013-06-02 17:46:56 +02003019int oneway_merge(const struct cache_entry * const *src,
3020 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003021{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02003022 const struct cache_entry *old = src[0];
3023 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003024
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003025 if (o->internal.merge_size != 1)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003026 return error("Cannot do a oneway merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003027 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003028
Junio C Hamano78d3b062009-07-18 12:26:38 -07003029 if (!a || a == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003030 return deleted_entry(old, old, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08003031
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003032 if (old && same(old, a)) {
Linus Torvalds34110cd2008-03-06 18:12:28 -08003033 int update = 0;
Utsav Shah679f2f92019-11-20 08:32:17 +00003034 if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) &&
3035 !(old->ce_flags & CE_FSMONITOR_VALID)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003036 struct stat st;
3037 if (lstat(old->name, &st) ||
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07003038 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
Linus Torvalds34110cd2008-03-06 18:12:28 -08003039 update |= CE_UPDATE;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003040 }
Stefan Bellerad173122018-01-05 12:03:03 -08003041 if (o->update && S_ISGITLINK(old->ce_mode) &&
3042 should_update_submodules() && !verify_uptodate(old, o))
3043 update |= CE_UPDATE;
Nguyễn Thái Ngọc Duyab5af822019-03-18 18:38:22 +07003044 add_entry(o, old, update, CE_STAGEMASK);
Linus Torvalds34110cd2008-03-06 18:12:28 -08003045 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003046 }
3047 return merged_entry(a, old, o);
3048}
Denton Liud3c7bf72021-03-03 03:16:42 -08003049
3050/*
3051 * Merge worktree and untracked entries in a stash entry.
3052 *
3053 * Ignore all index entries. Collapse remaining trees but make sure that they
3054 * don't have any conflicting files.
3055 */
3056int stash_worktree_untracked_merge(const struct cache_entry * const *src,
3057 struct unpack_trees_options *o)
3058{
3059 const struct cache_entry *worktree = src[1];
3060 const struct cache_entry *untracked = src[2];
3061
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003062 if (o->internal.merge_size != 2)
3063 BUG("invalid merge_size: %d", o->internal.merge_size);
Denton Liud3c7bf72021-03-03 03:16:42 -08003064
3065 if (worktree && untracked)
3066 return error(_("worktree and untracked commit have duplicate entries: %s"),
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +01003067 super_prefixed(worktree->name, o->super_prefix));
Denton Liud3c7bf72021-03-03 03:16:42 -08003068
3069 return merged_entry(worktree ? worktree : untracked, NULL, o);
3070}