blob: c2b20b80d5a448b7b5c76d2de4ac9358fa596b69 [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00001#include "git-compat-util.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +00002#include "advice.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04003#include "strvec.h"
Brandon Williams33028712017-08-03 11:19:59 -07004#include "repository.h"
Calvin Wanb1bda752023-09-29 14:20:51 -07005#include "parse.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -08006#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00007#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00008#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00009#include "hex.h"
Elijah Newrenf5653852023-05-16 06:33:50 +000010#include "name-hash.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020011#include "tree.h"
12#include "tree-walk.h"
Johannes Schindelin076b0ad2006-07-30 20:26:15 +020013#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020014#include "unpack-trees.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040015#include "progress.h"
Sven Verdoolaege0cf73752007-07-17 20:28:28 +020016#include "refs.h"
Junio C Hamano06f33c12009-03-13 21:24:08 -070017#include "attr.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000018#include "read-cache.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070019#include "split-index.h"
Victoria Dye0f329b92022-05-10 23:32:32 +000020#include "sparse-index.h"
Stefan Bellera7bc8452017-03-14 14:46:39 -070021#include "submodule.h"
22#include "submodule-config.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000023#include "symlinks.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000024#include "trace2.h"
Ben Peart883e2482017-09-22 12:35:40 -040025#include "fsmonitor.h"
Elijah Newrena034e912023-05-16 06:34:06 +000026#include "object-store-ll.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020027#include "promisor-remote.h"
Matheus Tavaresd052cc02021-03-23 11:19:32 -030028#include "entry.h"
Matheus Tavares04155bd2021-04-18 21:14:53 -030029#include "parallel-checkout.h"
Elijah Newrene38da482023-03-21 06:26:05 +000030#include "setup.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020031
Junio C Hamano8ccba002008-05-17 12:03:49 -070032/*
33 * Error messages expected by scripts out of plumbing commands such as
34 * read-tree. Non-scripted Porcelain is not required to use these messages
35 * and in fact are encouraged to reword them to better suit their particular
Diane Gasselin23cbf112010-08-11 10:38:05 +020036 * situation better. See how "git checkout" and "git merge" replaces
Matthieu Moydc1166e2010-09-02 13:57:33 +020037 * them using setup_unpack_trees_porcelain(), for example.
Junio C Hamano8ccba002008-05-17 12:03:49 -070038 */
Elijah Newren6271d772020-03-27 00:48:57 +000039static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
Matthieu Moy08353eb2010-08-11 10:38:04 +020040 /* ERROR_WOULD_OVERWRITE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070041 "Entry '%s' would be overwritten by merge. Cannot merge.",
42
Matthieu Moy08353eb2010-08-11 10:38:04 +020043 /* ERROR_NOT_UPTODATE_FILE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070044 "Entry '%s' not uptodate. Cannot merge.",
45
Matthieu Moy08353eb2010-08-11 10:38:04 +020046 /* ERROR_NOT_UPTODATE_DIR */
Junio C Hamano8ccba002008-05-17 12:03:49 -070047 "Updating '%s' would lose untracked files in it",
48
Elijah Newrenb817e542021-12-09 05:08:27 +000049 /* ERROR_CWD_IN_THE_WAY */
50 "Refusing to remove '%s' since it is the current working directory.",
51
Matthieu Moy08402b02010-08-11 10:38:06 +020052 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
53 "Untracked working tree file '%s' would be overwritten by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070054
Matthieu Moy08402b02010-08-11 10:38:06 +020055 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
56 "Untracked working tree file '%s' would be removed by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070057
Matthieu Moy08353eb2010-08-11 10:38:04 +020058 /* ERROR_BIND_OVERLAP */
Junio C Hamano8ccba002008-05-17 12:03:49 -070059 "Entry '%s' overlaps with '%s'. Cannot bind.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070060
Stefan Bellera7bc8452017-03-14 14:46:39 -070061 /* ERROR_WOULD_LOSE_SUBMODULE */
62 "Submodule '%s' cannot checkout new HEAD.",
Elijah Newrencd002c12020-03-27 00:48:55 +000063
Elijah Newren6271d772020-03-27 00:48:57 +000064 /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
65 "",
66
Elijah Newren1ac83f42020-03-27 00:48:56 +000067 /* WARNING_SPARSE_NOT_UPTODATE_FILE */
Elijah Newren22ab0b32020-03-27 00:48:58 +000068 "Path '%s' not uptodate; will not remove from working tree.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070069
Elijah Newrenebb568b2020-03-27 00:48:59 +000070 /* WARNING_SPARSE_UNMERGED_FILE */
71 "Path '%s' unmerged; will not remove from working tree.",
72
Elijah Newren1ac83f42020-03-27 00:48:56 +000073 /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
Elijah Newren22ab0b32020-03-27 00:48:58 +000074 "Path '%s' already present; will not overwrite with sparse update.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070075};
76
Matthieu Moy08353eb2010-08-11 10:38:04 +020077#define ERRORMSG(o,type) \
Elijah Newren13e1fd62023-02-27 15:28:17 +000078 ( ((o) && (o)->internal.msgs[(type)]) \
79 ? ((o)->internal.msgs[(type)]) \
Matthieu Moy08353eb2010-08-11 10:38:04 +020080 : (unpack_plumbing_errors[(type)]) )
Junio C Hamano8ccba002008-05-17 12:03:49 -070081
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +010082static const char *super_prefixed(const char *path, const char *super_prefix)
Stefan Beller3d415422017-01-17 17:05:20 -080083{
84 /*
85 * It is necessary and sufficient to have two static buffers
86 * here, as the return value of this function is fed to
87 * error() using the unpack_*_errors[] templates we see above.
88 */
89 static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};
90 static int super_prefix_len = -1;
91 static unsigned idx = ARRAY_SIZE(buf) - 1;
92
93 if (super_prefix_len < 0) {
Stefan Beller3d415422017-01-17 17:05:20 -080094 if (!super_prefix) {
95 super_prefix_len = 0;
96 } else {
97 int i;
98 for (i = 0; i < ARRAY_SIZE(buf); i++)
99 strbuf_addstr(&buf[i], super_prefix);
100 super_prefix_len = buf[0].len;
101 }
102 }
103
104 if (!super_prefix_len)
105 return path;
106
107 if (++idx >= ARRAY_SIZE(buf))
108 idx = 0;
109
110 strbuf_setlen(&buf[idx], super_prefix_len);
111 strbuf_addstr(&buf[idx], path);
112
113 return buf[idx].buf;
114}
115
Matthieu Moye2940302010-09-02 13:57:34 +0200116void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
117 const char *cmd)
Matthieu Moydc1166e2010-09-02 13:57:33 +0200118{
Clemens Buchacher79808722010-11-15 20:52:19 +0100119 int i;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000120 const char **msgs = opts->internal.msgs;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200121 const char *msg;
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400122
Elijah Newren13e1fd62023-02-27 15:28:17 +0000123 strvec_init(&opts->internal.msgs_to_free);
Martin Ågren1c41d282018-05-21 16:54:28 +0200124
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000125 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200126 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000127 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600128 "Please commit your changes or stash them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000129 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
130 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200131 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000132 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600133 "Please commit your changes or stash them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000134 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200135 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200136 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000137 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600138 "Please commit your changes or stash them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000139 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400140 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000141 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200142
143 msgs[ERROR_NOT_UPTODATE_DIR] =
Stefan Beller584f99c2016-12-02 11:17:41 -0800144 _("Updating the following directories would lose untracked files in them:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200145
Elijah Newrenb817e542021-12-09 05:08:27 +0000146 msgs[ERROR_CWD_IN_THE_WAY] =
147 _("Refusing to remove the current working directory:\n%s");
148
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000149 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200150 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000151 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600152 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000153 : _("The following untracked working tree files would be removed by checkout:\n%%s");
154 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200155 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000156 ? _("The following untracked working tree files would be removed by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600157 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000158 : _("The following untracked working tree files would be removed by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200159 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200160 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000161 ? _("The following untracked working tree files would be removed by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600162 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000163 : _("The following untracked working tree files would be removed by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200164 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000165 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Jeff Kingfa3f60b2014-06-18 16:02:13 -0400166
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000167 if (!strcmp(cmd, "checkout"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200168 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000169 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600170 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000171 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
172 else if (!strcmp(cmd, "merge"))
Ben Boeckeled9bff02021-08-23 12:44:00 +0200173 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000174 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600175 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000176 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
177 else
Ben Boeckeled9bff02021-08-23 12:44:00 +0200178 msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000179 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600180 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000181 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
Martin Ågren1c41d282018-05-21 16:54:28 +0200182 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =
Elijah Newren13e1fd62023-02-27 15:28:17 +0000183 strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200184
185 /*
186 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
187 * cannot easily display it as a list.
188 */
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000189 msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200190
Stefan Bellera7bc8452017-03-14 14:46:39 -0700191 msgs[ERROR_WOULD_LOSE_SUBMODULE] =
Stefan Beller6362ed02017-03-29 15:34:24 -0700192 _("Cannot update submodule:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200193
Elijah Newren1ac83f42020-03-27 00:48:56 +0000194 msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000195 _("The following paths are not up to date and were left despite sparse patterns:\n%s");
Elijah Newrenebb568b2020-03-27 00:48:59 +0000196 msgs[WARNING_SPARSE_UNMERGED_FILE] =
197 _("The following paths are unmerged and were left despite sparse patterns:\n%s");
Elijah Newren1ac83f42020-03-27 00:48:56 +0000198 msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] =
Elijah Newren22ab0b32020-03-27 00:48:58 +0000199 _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200200
Elijah Newren13e1fd62023-02-27 15:28:17 +0000201 opts->internal.show_all_errors = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100202 /* rejected paths may not have a static buffer */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000203 for (i = 0; i < ARRAY_SIZE(opts->internal.unpack_rejects); i++)
204 opts->internal.unpack_rejects[i].strdup_strings = 1;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200205}
206
Martin Ågren1c41d282018-05-21 16:54:28 +0200207void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
208{
Elijah Newren13e1fd62023-02-27 15:28:17 +0000209 strvec_clear(&opts->internal.msgs_to_free);
210 memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs));
Martin Ågren1c41d282018-05-21 16:54:28 +0200211}
212
Jeff King46169182014-11-24 13:36:51 -0500213static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
René Scharfe6ff264e2012-03-06 20:37:23 +0100214 unsigned int set, unsigned int clear)
215{
Karsten Blees419a5972013-11-14 20:22:27 +0100216 clear |= CE_HASHED;
René Scharfe6ff264e2012-03-06 20:37:23 +0100217
218 if (set & CE_REMOVE)
219 set |= CE_WT_REMOVE;
220
René Scharfe6ff264e2012-03-06 20:37:23 +0100221 ce->ce_flags = (ce->ce_flags & ~clear) | set;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000222 return add_index_entry(&o->internal.result, ce,
Jeff King46169182014-11-24 13:36:51 -0500223 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
René Scharfe6ff264e2012-03-06 20:37:23 +0100224}
225
René Scharfea33bd4d2013-06-02 17:46:53 +0200226static void add_entry(struct unpack_trees_options *o,
227 const struct cache_entry *ce,
228 unsigned int set, unsigned int clear)
229{
Elijah Newren13e1fd62023-02-27 15:28:17 +0000230 do_add_entry(o, dup_cache_entry(ce, &o->internal.result), set, clear);
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700231}
232
Kjetil Barvik78478922009-02-09 21:54:07 +0100233/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200234 * add error messages on path <path>
235 * corresponding to the type <e> with the message <msg>
236 * indicating if it should be display in porcelain or not
237 */
238static int add_rejected_path(struct unpack_trees_options *o,
239 enum unpack_trees_error_types e,
240 const char *path)
241{
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +0700242 if (o->quiet)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700243 return -1;
244
Elijah Newren13e1fd62023-02-27 15:28:17 +0000245 if (!o->internal.show_all_errors)
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100246 return error(ERRORMSG(o, e), super_prefixed(path,
247 o->super_prefix));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200248
249 /*
250 * Otherwise, insert in a list for future display by
Elijah Newren6271d772020-03-27 00:48:57 +0000251 * display_(error|warning)_msgs()
Matthieu Moye6c111b2010-08-11 10:38:07 +0200252 */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000253 string_list_append(&o->internal.unpack_rejects[e], path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200254 return -1;
255}
256
257/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200258 * display all the error messages stored in a nice way
259 */
260static void display_error_msgs(struct unpack_trees_options *o)
261{
Elijah Newren6271d772020-03-27 00:48:57 +0000262 int e;
263 unsigned error_displayed = 0;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200264 for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
Elijah Newren13e1fd62023-02-27 15:28:17 +0000265 struct string_list *rejects = &o->internal.unpack_rejects[e];
Elijah Newren6271d772020-03-27 00:48:57 +0000266
Clemens Buchacher79808722010-11-15 20:52:19 +0100267 if (rejects->nr > 0) {
Elijah Newren6271d772020-03-27 00:48:57 +0000268 int i;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200269 struct strbuf path = STRBUF_INIT;
Elijah Newren6271d772020-03-27 00:48:57 +0000270
271 error_displayed = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100272 for (i = 0; i < rejects->nr; i++)
273 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100274 error(ERRORMSG(o, e), super_prefixed(path.buf,
275 o->super_prefix));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200276 strbuf_release(&path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200277 }
Clemens Buchacher79808722010-11-15 20:52:19 +0100278 string_list_clear(rejects, 0);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200279 }
Elijah Newren6271d772020-03-27 00:48:57 +0000280 if (error_displayed)
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000281 fprintf(stderr, _("Aborting\n"));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200282}
283
Elijah Newren6271d772020-03-27 00:48:57 +0000284/*
285 * display all the warning messages stored in a nice way
286 */
287static void display_warning_msgs(struct unpack_trees_options *o)
288{
289 int e;
290 unsigned warning_displayed = 0;
291 for (e = NB_UNPACK_TREES_ERROR_TYPES + 1;
292 e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
Elijah Newren13e1fd62023-02-27 15:28:17 +0000293 struct string_list *rejects = &o->internal.unpack_rejects[e];
Elijah Newren6271d772020-03-27 00:48:57 +0000294
295 if (rejects->nr > 0) {
296 int i;
297 struct strbuf path = STRBUF_INIT;
298
299 warning_displayed = 1;
300 for (i = 0; i < rejects->nr; i++)
301 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100302 warning(ERRORMSG(o, e), super_prefixed(path.buf,
303 o->super_prefix));
Elijah Newren6271d772020-03-27 00:48:57 +0000304 strbuf_release(&path);
305 }
306 string_list_clear(rejects, 0);
307 }
308 if (warning_displayed)
309 fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
310}
Stefan Bellera7bc8452017-03-14 14:46:39 -0700311static int check_submodule_move_head(const struct cache_entry *ce,
312 const char *old_id,
313 const char *new_id,
314 struct unpack_trees_options *o)
315{
Stefan Bellercd279e22017-04-18 14:37:22 -0700316 unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700317 const struct submodule *sub = submodule_from_ce(ce);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700318
Stefan Bellera7bc8452017-03-14 14:46:39 -0700319 if (!sub)
320 return 0;
321
Stefan Bellercd279e22017-04-18 14:37:22 -0700322 if (o->reset)
323 flags |= SUBMODULE_MOVE_HEAD_FORCE;
324
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100325 if (submodule_move_head(ce->name, o->super_prefix, old_id, new_id,
326 flags))
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +0700327 return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
Brandon Williams7463e2e2017-08-03 11:19:53 -0700328 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -0700329}
330
Brandon Williams33028712017-08-03 11:19:59 -0700331/*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000332 * Perform the loading of the repository's gitmodules file. This function is
Brandon Williams33028712017-08-03 11:19:59 -0700333 * used by 'check_update()' to perform loading of the gitmodules file in two
Elijah Newren15beaaa2019-11-05 17:07:23 +0000334 * different situations:
Brandon Williams33028712017-08-03 11:19:59 -0700335 * (1) before removing entries from the working tree if the gitmodules file has
336 * been marked for removal. This situation is specified by 'state' == NULL.
337 * (2) before checking out entries to the working tree if the gitmodules file
338 * has been marked for update. This situation is specified by 'state' != NULL.
339 */
340static void load_gitmodules_file(struct index_state *index,
341 struct checkout *state)
Stefan Bellera7bc8452017-03-14 14:46:39 -0700342{
Brandon Williams33028712017-08-03 11:19:59 -0700343 int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
344
345 if (pos >= 0) {
346 struct cache_entry *ce = index->cache[pos];
347 if (!state && ce->ce_flags & CE_WT_REMOVE) {
Matheus Tavaresd7992422020-01-15 23:39:55 -0300348 repo_read_gitmodules(the_repository, 0);
Brandon Williams33028712017-08-03 11:19:59 -0700349 } else if (state && (ce->ce_flags & CE_UPDATE)) {
Stefan Bellerf793b892018-03-28 15:35:28 -0700350 submodule_free(the_repository);
Nguyễn Thái Ngọc Duy0f086e62018-11-13 19:28:00 +0100351 checkout_entry(ce, state, NULL, NULL);
Matheus Tavaresd7992422020-01-15 23:39:55 -0300352 repo_read_gitmodules(the_repository, 0);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700353 }
354 }
355}
356
Elijah Newren6c342392020-05-14 19:53:22 +0000357static struct progress *get_progress(struct unpack_trees_options *o,
358 struct index_state *index)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200359{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400360 unsigned cnt = 0, total = 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200361
Stefan Beller384f1a12017-01-09 11:46:19 -0800362 if (!o->update || !o->verbose_update)
363 return NULL;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200364
Stefan Beller384f1a12017-01-09 11:46:19 -0800365 for (; cnt < index->cache_nr; cnt++) {
366 const struct cache_entry *ce = index->cache[cnt];
367 if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
368 total++;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200369 }
370
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700371 return start_delayed_progress(_("Updating files"), total);
Stefan Beller384f1a12017-01-09 11:46:19 -0800372}
373
Duy Nguyenb8785792018-08-17 20:00:39 +0200374static void setup_collided_checkout_detection(struct checkout *state,
375 struct index_state *index)
376{
377 int i;
378
379 state->clone = 1;
380 for (i = 0; i < index->cache_nr; i++)
381 index->cache[i]->ce_flags &= ~CE_MATCHED;
382}
383
384static void report_collided_checkout(struct index_state *index)
385{
386 struct string_list list = STRING_LIST_INIT_NODUP;
387 int i;
388
389 for (i = 0; i < index->cache_nr; i++) {
390 struct cache_entry *ce = index->cache[i];
391
392 if (!(ce->ce_flags & CE_MATCHED))
393 continue;
394
395 string_list_append(&list, ce->name);
396 ce->ce_flags &= ~CE_MATCHED;
397 }
398
399 list.cmp = fspathcmp;
400 string_list_sort(&list);
401
402 if (list.nr) {
403 warning(_("the following paths have collided (e.g. case-sensitive paths\n"
404 "on a case-insensitive filesystem) and only one from the same\n"
405 "colliding group is in the working tree:\n"));
406
407 for (i = 0; i < list.nr; i++)
408 fprintf(stderr, " '%s'\n", list.items[i].string);
409 }
410
411 string_list_clear(&list, 0);
412}
413
Jonathan Tanb2896d22021-07-23 11:52:22 -0700414static int must_checkout(const struct cache_entry *ce)
415{
416 return ce->ce_flags & CE_UPDATE;
417}
418
Elijah Newrenb0a5a122020-03-27 00:48:49 +0000419static int check_updates(struct unpack_trees_options *o,
420 struct index_state *index)
Stefan Beller384f1a12017-01-09 11:46:19 -0800421{
422 unsigned cnt = 0;
Stefan Beller30ac2752017-01-09 11:46:17 -0800423 int errs = 0;
Carlo Marcelo Arenas Belón07f967a2018-10-18 11:46:04 -0700424 struct progress *progress;
Stefan Beller30ac2752017-01-09 11:46:17 -0800425 struct checkout state = CHECKOUT_INIT;
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300426 int i, pc_workers, pc_threshold;
Stefan Beller30ac2752017-01-09 11:46:17 -0800427
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200428 trace_performance_enter();
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100429 state.super_prefix = o->super_prefix;
Stefan Beller30ac2752017-01-09 11:46:17 -0800430 state.force = 1;
431 state.quiet = 1;
432 state.refresh_cache = 1;
433 state.istate = index;
brian m. carlson13e7ed62020-03-16 18:05:04 +0000434 clone_checkout_metadata(&state.meta, &o->meta, NULL);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200435
Elijah Newren26f924d2020-01-07 06:57:57 +0000436 if (!o->update || o->dry_run) {
437 remove_marked_cache_entries(index, 0);
438 trace_performance_leave("check_updates");
439 return 0;
440 }
441
Duy Nguyenb8785792018-08-17 20:00:39 +0200442 if (o->clone)
443 setup_collided_checkout_detection(&state, index);
444
Elijah Newren6c342392020-05-14 19:53:22 +0000445 progress = get_progress(o, index);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200446
Matheus Tavares22539ec2021-02-02 22:37:10 +0100447 /* Start with clean cache to avoid using any possibly outdated info. */
448 invalidate_lstat_cache();
449
Elijah Newren26f924d2020-01-07 06:57:57 +0000450 git_attr_set_direction(GIT_ATTR_CHECKOUT);
Brandon Williams33028712017-08-03 11:19:59 -0700451
Elijah Newren26f924d2020-01-07 06:57:57 +0000452 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700453 load_gitmodules_file(index, NULL);
454
Linus Torvalds34110cd2008-03-06 18:12:28 -0800455 for (i = 0; i < index->cache_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700456 const struct cache_entry *ce = index->cache[i];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200457
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700458 if (ce->ce_flags & CE_WT_REMOVE) {
459 display_progress(progress, ++cnt);
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +0100460 unlink_entry(ce, o->super_prefix);
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700461 }
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700462 }
Elijah Newren26f924d2020-01-07 06:57:57 +0000463
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000464 remove_marked_cache_entries(index, 0);
Kjetil Barvik78478922009-02-09 21:54:07 +0100465 remove_scheduled_dirs();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700466
Elijah Newren26f924d2020-01-07 06:57:57 +0000467 if (should_update_submodules())
Brandon Williams33028712017-08-03 11:19:59 -0700468 load_gitmodules_file(index, &state);
Stefan Bellera7bc8452017-03-14 14:46:39 -0700469
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +0200470 if (repo_has_promisor_remote(the_repository))
Jonathan Tanc0c578b2017-12-08 15:58:47 +0000471 /*
472 * Prefetch the objects that are to be checked out in the loop
473 * below.
474 */
Jonathan Tanb2896d22021-07-23 11:52:22 -0700475 prefetch_cache_entries(index, must_checkout);
Matheus Tavares04155bd2021-04-18 21:14:53 -0300476
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300477 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
478
Matheus Tavares04155bd2021-04-18 21:14:53 -0300479 enable_delayed_checkout(&state);
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300480 if (pc_workers > 1)
481 init_parallel_checkout();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700482 for (i = 0; i < index->cache_nr; i++) {
483 struct cache_entry *ce = index->cache[i];
484
Jonathan Tanb2896d22021-07-23 11:52:22 -0700485 if (must_checkout(ce)) {
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300486 size_t last_pc_queue_size = pc_queue_size();
487
David Turner7d782412015-07-17 17:19:27 -0400488 if (ce->ce_flags & CE_WT_REMOVE)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200489 BUG("both update and delete flags are set on %s",
David Turner7d782412015-07-17 17:19:27 -0400490 ce->name);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800491 ce->ce_flags &= ~CE_UPDATE;
Elijah Newren26f924d2020-01-07 06:57:57 +0000492 errs |= checkout_entry(ce, &state, NULL, NULL);
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300493
494 if (last_pc_queue_size == pc_queue_size())
495 display_progress(progress, ++cnt);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200496 }
497 }
Matheus Tavares7531e4b2021-04-18 21:14:55 -0300498 if (pc_workers > 1)
Matheus Tavares1c4d6f42021-04-18 21:14:56 -0300499 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
500 progress, &cnt);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400501 stop_progress(&progress);
Matheus Tavares611c7782022-07-14 08:49:12 -0300502 errs |= finish_delayed_checkout(&state, o->verbose_update);
Elijah Newren26f924d2020-01-07 06:57:57 +0000503 git_attr_set_direction(GIT_ATTR_CHECKIN);
Duy Nguyenb8785792018-08-17 20:00:39 +0200504
505 if (o->clone)
506 report_collided_checkout(index);
507
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +0200508 trace_performance_leave("check_updates");
Junio C Hamanoc4758d32008-03-18 22:01:28 -0700509 return errs != 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200510}
511
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200512static int verify_uptodate_sparse(const struct cache_entry *ce,
513 struct unpack_trees_options *o);
514static int verify_absent_sparse(const struct cache_entry *ce,
515 enum unpack_trees_error_types,
516 struct unpack_trees_options *o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700517
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700518static int apply_sparse_checkout(struct index_state *istate,
519 struct cache_entry *ce,
520 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700521{
522 int was_skip_worktree = ce_skip_worktree(ce);
523
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700524 if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700525 ce->ce_flags |= CE_SKIP_WORKTREE;
526 else
527 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700528 if (was_skip_worktree != ce_skip_worktree(ce)) {
529 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400530 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700531 istate->cache_changed |= CE_ENTRY_CHANGED;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700532 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700533
534 /*
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700535 * if (!was_skip_worktree && !ce_skip_worktree()) {
536 * This is perfectly normal. Move on;
537 * }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700538 */
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700539
540 /*
541 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
542 * area as a result of ce_skip_worktree() shortcuts in
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700543 * verify_absent() and verify_uptodate().
544 * Make sure they don't modify worktree if they are already
545 * outside checkout area
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700546 */
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700547 if (was_skip_worktree && ce_skip_worktree(ce)) {
548 ce->ce_flags &= ~CE_UPDATE;
549
550 /*
551 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
552 * on to get that file removed from both index and worktree.
553 * If that file is already outside worktree area, don't
554 * bother remove it.
555 */
556 if (ce->ce_flags & CE_REMOVE)
557 ce->ce_flags &= ~CE_WT_REMOVE;
558 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700559
560 if (!was_skip_worktree && ce_skip_worktree(ce)) {
561 /*
562 * If CE_UPDATE is set, verify_uptodate() must be called already
563 * also stat info may have lost after merged_entry() so calling
564 * verify_uptodate() again may fail
565 */
Elijah Newren3cc7c502020-03-27 00:48:50 +0000566 if (!(ce->ce_flags & CE_UPDATE) &&
567 verify_uptodate_sparse(ce, o)) {
568 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700569 return -1;
Elijah Newren3cc7c502020-03-27 00:48:50 +0000570 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700571 ce->ce_flags |= CE_WT_REMOVE;
David Turner7d782412015-07-17 17:19:27 -0400572 ce->ce_flags &= ~CE_UPDATE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700573 }
574 if (was_skip_worktree && !ce_skip_worktree(ce)) {
Elijah Newren1ac83f42020-03-27 00:48:56 +0000575 if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700576 return -1;
577 ce->ce_flags |= CE_UPDATE;
578 }
579 return 0;
580}
581
Elijah Newrenebb568b2020-03-27 00:48:59 +0000582static int warn_conflicted_path(struct index_state *istate,
583 int i,
584 struct unpack_trees_options *o)
585{
586 char *conflicting_path = istate->cache[i]->name;
587 int count = 0;
588
589 add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path);
590
Derrick Stolee0eeb3be2020-05-07 13:17:33 +0000591 /* Find out how many higher stage entries are at same path */
592 while ((++count) + i < istate->cache_nr &&
593 !strcmp(conflicting_path, istate->cache[count + i]->name))
594 ; /* do nothing */
595
Elijah Newrenebb568b2020-03-27 00:48:59 +0000596 return count;
597}
598
René Scharfe5828e832013-06-02 17:46:56 +0200599static inline int call_unpack_fn(const struct cache_entry * const *src,
600 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800601{
Linus Torvalds34110cd2008-03-06 18:12:28 -0800602 int ret = o->fn(src, o);
603 if (ret > 0)
Linus Torvalds01904572008-03-05 20:15:44 -0800604 ret = 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800605 return ret;
606}
607
Junio C Hamanoda165f42010-01-07 14:59:54 -0800608static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
609{
610 ce->ce_flags |= CE_UNPACKED;
611
Elijah Newren13e1fd62023-02-27 15:28:17 +0000612 if (o->internal.cache_bottom < o->src_index->cache_nr &&
613 o->src_index->cache[o->internal.cache_bottom] == ce) {
614 int bottom = o->internal.cache_bottom;
615
Junio C Hamanoda165f42010-01-07 14:59:54 -0800616 while (bottom < o->src_index->cache_nr &&
617 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
618 bottom++;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000619 o->internal.cache_bottom = bottom;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800620 }
621}
622
623static void mark_all_ce_unused(struct index_state *index)
624{
625 int i;
626 for (i = 0; i < index->cache_nr; i++)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700627 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
Junio C Hamanoda165f42010-01-07 14:59:54 -0800628}
629
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200630static int locate_in_src_index(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800631 struct unpack_trees_options *o)
632{
633 struct index_state *index = o->src_index;
634 int len = ce_namelen(ce);
635 int pos = index_name_pos(index, ce->name, len);
636 if (pos < 0)
637 pos = -1 - pos;
638 return pos;
639}
640
641/*
642 * We call unpack_index_entry() with an unmerged cache entry
643 * only in diff-index, and it wants a single callback. Skip
644 * the other unmerged entry with the same name.
645 */
646static void mark_ce_used_same_name(struct cache_entry *ce,
647 struct unpack_trees_options *o)
648{
649 struct index_state *index = o->src_index;
650 int len = ce_namelen(ce);
651 int pos;
652
653 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
654 struct cache_entry *next = index->cache[pos];
655 if (len != ce_namelen(next) ||
656 memcmp(ce->name, next->name, len))
657 break;
658 mark_ce_used(next, o);
659 }
660}
661
Victoria Dye99430aa2022-03-17 15:55:36 +0000662static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
Junio C Hamanoda165f42010-01-07 14:59:54 -0800663{
664 const struct index_state *index = o->src_index;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000665 int pos = o->internal.cache_bottom;
Junio C Hamanoda165f42010-01-07 14:59:54 -0800666
667 while (pos < index->cache_nr) {
668 struct cache_entry *ce = index->cache[pos];
Victoria Dye99430aa2022-03-17 15:55:36 +0000669 if (!(ce->ce_flags & CE_UNPACKED))
Junio C Hamanoda165f42010-01-07 14:59:54 -0800670 return ce;
671 pos++;
672 }
673 return NULL;
674}
675
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700676static void add_same_unmerged(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800677 struct unpack_trees_options *o)
678{
679 struct index_state *index = o->src_index;
680 int len = ce_namelen(ce);
681 int pos = index_name_pos(index, ce->name, len);
682
683 if (0 <= pos)
684 die("programming error in a caller of mark_ce_used_same_name");
685 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
686 struct cache_entry *next = index->cache[pos];
687 if (len != ce_namelen(next) ||
688 memcmp(ce->name, next->name, len))
689 break;
690 add_entry(o, next, 0, 0);
691 mark_ce_used(next, o);
692 }
693}
694
695static int unpack_index_entry(struct cache_entry *ce,
696 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800697{
René Scharfe5828e832013-06-02 17:46:56 +0200698 const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Junio C Hamanoda165f42010-01-07 14:59:54 -0800699 int ret;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800700
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000701 src[0] = ce;
702
Junio C Hamanoda165f42010-01-07 14:59:54 -0800703 mark_ce_used(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -0800704 if (ce_stage(ce)) {
705 if (o->skip_unmerged) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800706 add_entry(o, ce, 0, 0);
707 return 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800708 }
Linus Torvalds01904572008-03-05 20:15:44 -0800709 }
Junio C Hamanoda165f42010-01-07 14:59:54 -0800710 ret = call_unpack_fn(src, o);
711 if (ce_stage(ce))
712 mark_ce_used_same_name(ce, o);
713 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800714}
715
Jeff King90553842019-07-31 00:38:15 -0400716static int find_cache_pos(struct traverse_info *, const char *p, size_t len);
Junio C Hamano730f7282009-09-20 00:03:39 -0700717
718static void restore_cache_bottom(struct traverse_info *info, int bottom)
719{
720 struct unpack_trees_options *o = info->data;
721
722 if (o->diff_index_cached)
723 return;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000724 o->internal.cache_bottom = bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -0700725}
726
727static int switch_cache_bottom(struct traverse_info *info)
728{
729 struct unpack_trees_options *o = info->data;
730 int ret, pos;
731
732 if (o->diff_index_cached)
733 return 0;
Elijah Newren13e1fd62023-02-27 15:28:17 +0000734 ret = o->internal.cache_bottom;
Jeff King90553842019-07-31 00:38:15 -0400735 pos = find_cache_pos(info->prev, info->name, info->namelen);
Junio C Hamano730f7282009-09-20 00:03:39 -0700736
737 if (pos < -1)
Elijah Newren13e1fd62023-02-27 15:28:17 +0000738 o->internal.cache_bottom = -2 - pos;
Junio C Hamano730f7282009-09-20 00:03:39 -0700739 else if (pos < 0)
Elijah Newren13e1fd62023-02-27 15:28:17 +0000740 o->internal.cache_bottom = o->src_index->cache_nr;
Junio C Hamano730f7282009-09-20 00:03:39 -0700741 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800742}
743
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000744static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)
745{
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000746 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 +0000747}
748
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200749static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,
750 struct name_entry *names,
751 struct traverse_info *info)
752{
753 struct unpack_trees_options *o = info->data;
754 int i;
755
756 if (!o->merge || dirmask != ((1 << n) - 1))
757 return 0;
758
759 for (i = 1; i < n; i++)
760 if (!are_same_oid(names, names + i))
761 return 0;
762
763 return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);
764}
765
766static int index_pos_by_traverse_info(struct name_entry *names,
767 struct traverse_info *info)
768{
769 struct unpack_trees_options *o = info->data;
Jeff Kingc43ab062019-07-31 00:38:23 -0400770 struct strbuf name = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200771 int pos;
772
Jeff Kingc43ab062019-07-31 00:38:23 -0400773 strbuf_make_traverse_path(&name, info, names->path, names->pathlen);
774 strbuf_addch(&name, '/');
775 pos = index_name_pos(o->src_index, name.buf, name.len);
Derrick Stolee13e13312021-03-30 13:10:57 +0000776 if (pos >= 0) {
777 if (!o->src_index->sparse_index ||
778 !(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE))
779 BUG("This is a directory and should not exist in index");
780 } else {
781 pos = -pos - 1;
782 }
Emily Shaffer573117d2020-01-07 18:31:27 -0800783 if (pos >= o->src_index->cache_nr ||
784 !starts_with(o->src_index->cache[pos]->name, name.buf) ||
Jeff Kingc43ab062019-07-31 00:38:23 -0400785 (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf)))
Emily Shaffer573117d2020-01-07 18:31:27 -0800786 BUG("pos %d doesn't point to the first entry of %s in index",
787 pos, name.buf);
Jeff Kingc43ab062019-07-31 00:38:23 -0400788 strbuf_release(&name);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200789 return pos;
790}
791
792/*
793 * 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 +0200794 * path. We'll walk these trees in an iterative loop using cache-tree/index
795 * instead of ODB since we already know what these trees contain.
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200796 */
797static 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 +0200798 struct traverse_info *info)
799{
800 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
801 struct unpack_trees_options *o = info->data;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200802 struct cache_entry *tree_ce = NULL;
803 int ce_len = 0;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200804 int i, d;
805
806 if (!o->merge)
807 BUG("We need cache-tree to do this optimization");
808
809 /*
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +0000810 * Do what unpack_callback() and unpack_single_entry() normally
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200811 * do. But we walk all paths in an iterative loop instead.
812 *
813 * D/F conflicts and higher stage entries are not a concern
814 * because cache-tree would be invalidated and we would never
815 * get here in the first place.
816 */
817 for (i = 0; i < nr_entries; i++) {
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200818 int new_ce_len, len, rc;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200819
820 src[0] = o->src_index->cache[pos + i];
821
822 len = ce_namelen(src[0]);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200823 new_ce_len = cache_entry_size(len);
824
825 if (new_ce_len > ce_len) {
826 new_ce_len <<= 1;
827 tree_ce = xrealloc(tree_ce, new_ce_len);
828 memset(tree_ce, 0, new_ce_len);
829 ce_len = new_ce_len;
830
831 tree_ce->ce_flags = create_ce_flags(0);
832
833 for (d = 1; d <= nr_names; d++)
834 src[d] = tree_ce;
835 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200836
837 tree_ce->ce_mode = src[0]->ce_mode;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200838 tree_ce->ce_namelen = len;
839 oidcpy(&tree_ce->oid, &src[0]->oid);
840 memcpy(tree_ce->name, src[0]->name, len + 1);
841
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200842 rc = call_unpack_fn((const struct cache_entry * const *)src, o);
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200843 if (rc < 0) {
844 free(tree_ce);
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200845 return rc;
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200846 }
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200847
848 mark_ce_used(src[0], o);
849 }
Nguyễn Thái Ngọc Duyf1e11c62018-08-18 16:41:25 +0200850 free(tree_ce);
Elijah Newren1ca13dd2023-02-27 15:28:19 +0000851 if (o->internal.debug_unpack)
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200852 printf("Unpacked %d entries from %s to %s using cache-tree\n",
853 nr_entries,
854 o->src_index->cache[pos]->name,
855 o->src_index->cache[pos + nr_entries - 1]->name);
856 return 0;
857}
858
Junio C Hamano84563a62010-12-22 09:09:55 -0800859static int traverse_trees_recursive(int n, unsigned long dirmask,
860 unsigned long df_conflicts,
861 struct name_entry *names,
862 struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -0800863{
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100864 struct unpack_trees_options *o = info->data;
Junio C Hamano730f7282009-09-20 00:03:39 -0700865 int i, ret, bottom;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000866 int nr_buf = 0;
Jeff King59c4c7d2023-08-31 02:17:54 -0400867 struct tree_desc *t;
868 void **buf;
Linus Torvalds01904572008-03-05 20:15:44 -0800869 struct traverse_info newinfo;
870 struct name_entry *p;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200871 int nr_entries;
872
873 nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);
874 if (nr_entries > 0) {
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200875 int pos = index_pos_by_traverse_info(names, info);
876
877 if (!o->merge || df_conflicts)
878 BUG("Wrong condition to get here buddy");
879
880 /*
881 * All entries up to 'pos' must have been processed
882 * (i.e. marked CE_UNPACKED) at this point. But to be safe,
883 * save and restore cache_bottom anyway to not miss
884 * unprocessed entries before 'pos'.
885 */
Elijah Newren13e1fd62023-02-27 15:28:17 +0000886 bottom = o->internal.cache_bottom;
Jeff King66429692019-03-20 04:15:07 -0400887 ret = traverse_by_cache_tree(pos, nr_entries, n, info);
Elijah Newren13e1fd62023-02-27 15:28:17 +0000888 o->internal.cache_bottom = bottom;
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +0200889 return ret;
890 }
Linus Torvalds01904572008-03-05 20:15:44 -0800891
892 p = names;
893 while (!p->mode)
894 p++;
895
896 newinfo = *info;
897 newinfo.prev = info;
Junio C Hamano40e37252011-08-29 12:31:06 -0700898 newinfo.pathspec = info->pathspec;
Jeff King90553842019-07-31 00:38:15 -0400899 newinfo.name = p->path;
900 newinfo.namelen = p->pathlen;
901 newinfo.mode = p->mode;
Jeff King37806082019-07-31 00:38:18 -0400902 newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1);
René Scharfe603d2492013-06-16 01:44:43 +0200903 newinfo.df_conflicts |= df_conflicts;
Linus Torvalds01904572008-03-05 20:15:44 -0800904
Jeff King59c4c7d2023-08-31 02:17:54 -0400905 ALLOC_ARRAY(t, n);
906 ALLOC_ARRAY(buf, n);
907
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000908 /*
909 * Fetch the tree from the ODB for each peer directory in the
910 * n commits.
911 *
912 * For 2- and 3-way traversals, we try to avoid hitting the
913 * ODB twice for the same OID. This should yield a nice speed
914 * up in checkouts and merges when the commits are similar.
915 *
916 * We don't bother doing the full O(n^2) search for larger n,
917 * because wider traversals don't happen that often and we
918 * avoid the search setup.
919 *
920 * When 2 peer OIDs are the same, we just copy the tree
921 * descriptor data. This implicitly borrows the buffer
922 * data from the earlier cell.
923 */
Linus Torvalds01904572008-03-05 20:15:44 -0800924 for (i = 0; i < n; i++, dirmask >>= 1) {
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000925 if (i > 0 && are_same_oid(&names[i], &names[i - 1]))
926 t[i] = t[i - 1];
927 else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
928 t[i] = t[i - 2];
929 else {
René Scharfe5c377d32017-08-12 10:32:59 +0200930 const struct object_id *oid = NULL;
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000931 if (dirmask & 1)
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000932 oid = &names[i].oid;
Nguyễn Thái Ngọc Duy5e575802019-06-27 16:28:48 +0700933 buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid);
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000934 }
Linus Torvalds01904572008-03-05 20:15:44 -0800935 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700936
937 bottom = switch_cache_bottom(&newinfo);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100938 ret = traverse_trees(o->src_index, n, t, &newinfo);
Junio C Hamano730f7282009-09-20 00:03:39 -0700939 restore_cache_bottom(&newinfo, bottom);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500940
Jeff Hostetlerd12a8cf2017-04-14 19:25:54 +0000941 for (i = 0; i < nr_buf; i++)
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500942 free(buf[i]);
Jeff King59c4c7d2023-08-31 02:17:54 -0400943 free(buf);
944 free(t);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500945
Junio C Hamano730f7282009-09-20 00:03:39 -0700946 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800947}
948
949/*
950 * Compare the traverse-path to the cache entry without actually
951 * having to generate the textual representation of the traverse
952 * path.
953 *
954 * NOTE! This *only* compares up to the size of the traverse path
955 * itself - the caller needs to do the final check for the cache
956 * entry having more data at the end!
957 */
Jeff King90553842019-07-31 00:38:15 -0400958static int do_compare_entry_piecewise(const struct cache_entry *ce,
959 const struct traverse_info *info,
960 const char *name, size_t namelen,
961 unsigned mode)
Linus Torvalds01904572008-03-05 20:15:44 -0800962{
Jeff King90553842019-07-31 00:38:15 -0400963 int pathlen, ce_len;
Linus Torvalds01904572008-03-05 20:15:44 -0800964 const char *ce_name;
965
966 if (info->prev) {
David Turnerd9c2bd52015-12-21 17:34:20 -0500967 int cmp = do_compare_entry_piecewise(ce, info->prev,
Jeff King90553842019-07-31 00:38:15 -0400968 info->name, info->namelen,
969 info->mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800970 if (cmp)
971 return cmp;
972 }
973 pathlen = info->pathlen;
974 ce_len = ce_namelen(ce);
975
976 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
977 if (ce_len < pathlen)
978 return -1;
979
980 ce_len -= pathlen;
981 ce_name = ce->name + pathlen;
982
Jeff King90553842019-07-31 00:38:15 -0400983 return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
Linus Torvalds01904572008-03-05 20:15:44 -0800984}
985
David Turnerd9c2bd52015-12-21 17:34:20 -0500986static int do_compare_entry(const struct cache_entry *ce,
987 const struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -0400988 const char *name, size_t namelen,
989 unsigned mode)
David Turnerd9c2bd52015-12-21 17:34:20 -0500990{
Jeff King90553842019-07-31 00:38:15 -0400991 int pathlen, ce_len;
David Turnerd9c2bd52015-12-21 17:34:20 -0500992 const char *ce_name;
993 int cmp;
Derrick Stoleecd807a52021-07-14 13:12:31 +0000994 unsigned ce_mode;
David Turnerd9c2bd52015-12-21 17:34:20 -0500995
996 /*
997 * If we have not precomputed the traverse path, it is quicker
998 * to avoid doing so. But if we have precomputed it,
999 * it is quicker to use the precomputed version.
1000 */
1001 if (!info->traverse_path)
Jeff King90553842019-07-31 00:38:15 -04001002 return do_compare_entry_piecewise(ce, info, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -05001003
1004 cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
1005 if (cmp)
1006 return cmp;
1007
1008 pathlen = info->pathlen;
1009 ce_len = ce_namelen(ce);
1010
1011 if (ce_len < pathlen)
1012 return -1;
1013
1014 ce_len -= pathlen;
1015 ce_name = ce->name + pathlen;
1016
Derrick Stoleecd807a52021-07-14 13:12:31 +00001017 ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG;
1018 return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode);
David Turnerd9c2bd52015-12-21 17:34:20 -05001019}
1020
Linus Torvalds01904572008-03-05 20:15:44 -08001021static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
1022{
Jeff King90553842019-07-31 00:38:15 -04001023 int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode);
Linus Torvalds01904572008-03-05 20:15:44 -08001024 if (cmp)
1025 return cmp;
1026
1027 /*
Derrick Stoleecd807a52021-07-14 13:12:31 +00001028 * At this point, we know that we have a prefix match. If ce
1029 * is a sparse directory, then allow an exact match. This only
1030 * works when the input name is a directory, since ce->name
1031 * ends in a directory separator.
1032 */
1033 if (S_ISSPARSEDIR(ce->ce_mode) &&
1034 ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1)
1035 return 0;
1036
1037 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001038 * Even if the beginning compared identically, the ce should
1039 * compare as bigger than a directory leading up to it!
1040 */
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001041 return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));
Linus Torvalds01904572008-03-05 20:15:44 -08001042}
1043
Junio C Hamanoda165f42010-01-07 14:59:54 -08001044static int ce_in_traverse_path(const struct cache_entry *ce,
1045 const struct traverse_info *info)
1046{
1047 if (!info->prev)
1048 return 1;
Jeff King90553842019-07-31 00:38:15 -04001049 if (do_compare_entry(ce, info->prev,
1050 info->name, info->namelen, info->mode))
Junio C Hamanoda165f42010-01-07 14:59:54 -08001051 return 0;
1052 /*
1053 * If ce (blob) is the same name as the path (which is a tree
1054 * we will be descending into), it won't be inside it.
1055 */
1056 return (info->pathlen < ce_namelen(ce));
1057}
1058
Jameson Millera8497352018-07-02 19:49:31 +00001059static struct cache_entry *create_ce_entry(const struct traverse_info *info,
1060 const struct name_entry *n,
1061 int stage,
1062 struct index_state *istate,
Derrick Stolee523506d2021-07-14 13:12:33 +00001063 int is_transient,
1064 int is_sparse_directory)
Linus Torvalds01904572008-03-05 20:15:44 -08001065{
Jeff Kingb3b3cbc2019-07-31 00:38:20 -04001066 size_t len = traverse_path_len(info, tree_entry_len(n));
Derrick Stolee523506d2021-07-14 13:12:33 +00001067 size_t alloc_len = is_sparse_directory ? len + 1 : len;
Jameson Millera8497352018-07-02 19:49:31 +00001068 struct cache_entry *ce =
1069 is_transient ?
Derrick Stolee523506d2021-07-14 13:12:33 +00001070 make_empty_transient_cache_entry(alloc_len, NULL) :
1071 make_empty_cache_entry(istate, alloc_len);
Linus Torvalds01904572008-03-05 20:15:44 -08001072
1073 ce->ce_mode = create_ce_mode(n->mode);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001074 ce->ce_flags = create_ce_flags(stage);
1075 ce->ce_namelen = len;
brian m. carlsonea82b2a2019-01-15 00:39:44 +00001076 oidcpy(&ce->oid, &n->oid);
Jeff King5aa02f92019-07-31 00:38:25 -04001077 /* len+1 because the cache_entry allocates space for NUL */
1078 make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen);
Linus Torvalds01904572008-03-05 20:15:44 -08001079
Derrick Stolee523506d2021-07-14 13:12:33 +00001080 if (is_sparse_directory) {
1081 ce->name[len] = '/';
1082 ce->name[len + 1] = '\0';
1083 ce->ce_namelen++;
1084 ce->ce_flags |= CE_SKIP_WORKTREE;
1085 }
1086
Linus Torvalds01904572008-03-05 20:15:44 -08001087 return ce;
1088}
1089
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001090/*
Victoria Dyeb15207b2022-08-08 19:07:52 +00001091 * Determine whether the path specified by 'p' should be unpacked as a new
1092 * sparse directory in a sparse index. A new sparse directory 'A/':
1093 * - must be outside the sparse cone.
1094 * - must not already be in the index (i.e., no index entry with name 'A/'
1095 * exists).
1096 * - must not have any child entries in the index (i.e., no index entry
1097 * 'A/<something>' exists).
1098 * If 'p' meets the above requirements, return 1; otherwise, return 0.
1099 */
1100static int entry_is_new_sparse_dir(const struct traverse_info *info,
1101 const struct name_entry *p)
1102{
1103 int res, pos;
1104 struct strbuf dirpath = STRBUF_INIT;
1105 struct unpack_trees_options *o = info->data;
1106
1107 if (!S_ISDIR(p->mode))
1108 return 0;
1109
1110 /*
1111 * If the path is inside the sparse cone, it can't be a sparse directory.
1112 */
1113 strbuf_add(&dirpath, info->traverse_path, info->pathlen);
1114 strbuf_add(&dirpath, p->path, p->pathlen);
1115 strbuf_addch(&dirpath, '/');
1116 if (path_in_cone_mode_sparse_checkout(dirpath.buf, o->src_index)) {
1117 res = 0;
1118 goto cleanup;
1119 }
1120
1121 pos = index_name_pos_sparse(o->src_index, dirpath.buf, dirpath.len);
1122 if (pos >= 0) {
1123 /* Path is already in the index, not a new sparse dir */
1124 res = 0;
1125 goto cleanup;
1126 }
1127
1128 /* Where would this sparse dir be inserted into the index? */
1129 pos = -pos - 1;
1130 if (pos >= o->src_index->cache_nr) {
1131 /*
1132 * Sparse dir would be inserted at the end of the index, so we
1133 * know it has no child entries.
1134 */
1135 res = 1;
1136 goto cleanup;
1137 }
1138
1139 /*
1140 * If the dir has child entries in the index, the first would be at the
1141 * position the sparse directory would be inserted. If the entry at this
1142 * position is inside the dir, not a new sparse dir.
1143 */
1144 res = strncmp(o->src_index->cache[pos]->name, dirpath.buf, dirpath.len);
1145
1146cleanup:
1147 strbuf_release(&dirpath);
1148 return res;
1149}
1150
1151/*
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001152 * Note that traverse_by_cache_tree() duplicates some logic in this function
1153 * without actually calling it. If you change the logic here you may need to
1154 * check and change there as well.
1155 */
Derrick Stoleebd6a3fd2021-07-14 13:12:32 +00001156static int unpack_single_entry(int n, unsigned long mask,
1157 unsigned long dirmask,
1158 struct cache_entry **src,
1159 const struct name_entry *names,
Victoria Dyeb15207b2022-08-08 19:07:52 +00001160 const struct traverse_info *info,
1161 int *is_new_sparse_dir)
Linus Torvalds01904572008-03-05 20:15:44 -08001162{
1163 int i;
1164 struct unpack_trees_options *o = info->data;
René Scharfe603d2492013-06-16 01:44:43 +02001165 unsigned long conflicts = info->df_conflicts | dirmask;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001166 const struct name_entry *p = names;
Linus Torvalds01904572008-03-05 20:15:44 -08001167
Victoria Dyeb15207b2022-08-08 19:07:52 +00001168 *is_new_sparse_dir = 0;
1169 if (mask == dirmask && !src[0]) {
1170 /*
1171 * If we're not in a sparse index, we can't unpack a directory
1172 * without recursing into it, so we return.
1173 */
1174 if (!o->src_index->sparse_index)
1175 return 0;
1176
1177 /* Find first entry with a real name (we could use "mask" too) */
1178 while (!p->mode)
1179 p++;
1180
1181 /*
1182 * If the directory is completely missing from the index but
1183 * would otherwise be a sparse directory, we should unpack it.
1184 * If not, we'll return and continue recursively traversing the
1185 * tree.
1186 */
1187 *is_new_sparse_dir = entry_is_new_sparse_dir(info, p);
1188 if (!*is_new_sparse_dir)
1189 return 0;
1190 }
Linus Torvalds01904572008-03-05 20:15:44 -08001191
Linus Torvalds01904572008-03-05 20:15:44 -08001192 /*
Victoria Dyeb15207b2022-08-08 19:07:52 +00001193 * When we are unpacking a sparse directory, then this isn't necessarily
1194 * a directory-file conflict.
Derrick Stolee523506d2021-07-14 13:12:33 +00001195 */
Victoria Dyeb15207b2022-08-08 19:07:52 +00001196 if (mask == dirmask &&
1197 (*is_new_sparse_dir || (src[0] && S_ISSPARSEDIR(src[0]->ce_mode))))
Derrick Stolee523506d2021-07-14 13:12:33 +00001198 conflicts = 0;
1199
1200 /*
Linus Torvalds01904572008-03-05 20:15:44 -08001201 * Ok, we've filled in up to any potential index entry in src[0],
1202 * now do the rest.
1203 */
1204 for (i = 0; i < n; i++) {
1205 int stage;
1206 unsigned int bit = 1ul << i;
1207 if (conflicts & bit) {
1208 src[i + o->merge] = o->df_conflict_entry;
1209 continue;
1210 }
1211 if (!(mask & bit))
1212 continue;
1213 if (!o->merge)
1214 stage = 0;
1215 else if (i + 1 < o->head_idx)
1216 stage = 1;
1217 else if (i + 1 > o->head_idx)
1218 stage = 3;
1219 else
1220 stage = 2;
Jameson Millera8497352018-07-02 19:49:31 +00001221
1222 /*
1223 * If the merge bit is set, then the cache entries are
1224 * discarded in the following block. In this case,
1225 * construct "transient" cache_entries, as they are
1226 * not stored in the index. otherwise construct the
1227 * cache entry from the index aware logic.
1228 */
Derrick Stolee523506d2021-07-14 13:12:33 +00001229 src[i + o->merge] = create_ce_entry(info, names + i, stage,
Elijah Newren0d680a72023-02-27 15:28:18 +00001230 &o->internal.result,
1231 o->merge, bit & dirmask);
Linus Torvalds01904572008-03-05 20:15:44 -08001232 }
1233
René Scharfe5d80ef52013-06-02 17:46:57 +02001234 if (o->merge) {
1235 int rc = call_unpack_fn((const struct cache_entry * const *)src,
1236 o);
1237 for (i = 0; i < n; i++) {
1238 struct cache_entry *ce = src[i + o->merge];
1239 if (ce != o->df_conflict_entry)
Jameson Millera8497352018-07-02 19:49:31 +00001240 discard_cache_entry(ce);
René Scharfe5d80ef52013-06-02 17:46:57 +02001241 }
1242 return rc;
1243 }
Linus Torvalds01904572008-03-05 20:15:44 -08001244
Linus Torvalds01904572008-03-05 20:15:44 -08001245 for (i = 0; i < n; i++)
Junio C Hamanoaab3b9a2009-03-12 00:02:12 -07001246 if (src[i] && src[i] != o->df_conflict_entry)
Jeff King46169182014-11-24 13:36:51 -05001247 if (do_add_entry(o, src[i], 0, 0))
1248 return -1;
1249
Linus Torvalds01904572008-03-05 20:15:44 -08001250 return 0;
1251}
1252
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001253static int unpack_failed(struct unpack_trees_options *o, const char *message)
1254{
Elijah Newren13e1fd62023-02-27 15:28:17 +00001255 discard_index(&o->internal.result);
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07001256 if (!o->quiet && !o->exiting_early) {
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001257 if (message)
1258 return error("%s", message);
1259 return -1;
1260 }
1261 return -1;
1262}
1263
Junio C Hamano730f7282009-09-20 00:03:39 -07001264/*
1265 * The tree traversal is looking at name p. If we have a matching entry,
1266 * return it. If name p is a directory in the index, do not return
1267 * anything, as we will want to match it when the traversal descends into
1268 * the directory.
1269 */
1270static int find_cache_pos(struct traverse_info *info,
Jeff King90553842019-07-31 00:38:15 -04001271 const char *p, size_t p_len)
Junio C Hamano730f7282009-09-20 00:03:39 -07001272{
1273 int pos;
1274 struct unpack_trees_options *o = info->data;
1275 struct index_state *index = o->src_index;
1276 int pfxlen = info->pathlen;
Junio C Hamano730f7282009-09-20 00:03:39 -07001277
Elijah Newren13e1fd62023-02-27 15:28:17 +00001278 for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001279 const struct cache_entry *ce = index->cache[pos];
Junio C Hamano730f7282009-09-20 00:03:39 -07001280 const char *ce_name, *ce_slash;
1281 int cmp, ce_len;
1282
Brian Downinge53e6b42010-06-10 21:59:07 -05001283 if (ce->ce_flags & CE_UNPACKED) {
1284 /*
1285 * cache_bottom entry is already unpacked, so
1286 * we can never match it; don't check it
1287 * again.
1288 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00001289 if (pos == o->internal.cache_bottom)
1290 ++o->internal.cache_bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -07001291 continue;
Brian Downinge53e6b42010-06-10 21:59:07 -05001292 }
David Turnera6720952016-01-22 14:58:43 -05001293 if (!ce_in_traverse_path(ce, info)) {
1294 /*
1295 * Check if we can skip future cache checks
1296 * (because we're already past all possible
1297 * entries in the traverse path).
1298 */
1299 if (info->traverse_path) {
1300 if (strncmp(ce->name, info->traverse_path,
1301 info->pathlen) > 0)
1302 break;
1303 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001304 continue;
David Turnera6720952016-01-22 14:58:43 -05001305 }
Junio C Hamano730f7282009-09-20 00:03:39 -07001306 ce_name = ce->name + pfxlen;
1307 ce_slash = strchr(ce_name, '/');
1308 if (ce_slash)
1309 ce_len = ce_slash - ce_name;
1310 else
1311 ce_len = ce_namelen(ce) - pfxlen;
Jeff King90553842019-07-31 00:38:15 -04001312 cmp = name_compare(p, p_len, ce_name, ce_len);
Junio C Hamano730f7282009-09-20 00:03:39 -07001313 /*
1314 * Exact match; if we have a directory we need to
1315 * delay returning it.
1316 */
1317 if (!cmp)
1318 return ce_slash ? -2 - pos : pos;
1319 if (0 < cmp)
1320 continue; /* keep looking */
1321 /*
1322 * ce_name sorts after p->path; could it be that we
1323 * have files under p->path directory in the index?
1324 * E.g. ce_name == "t-i", and p->path == "t"; we may
1325 * have "t/a" in the index.
1326 */
Jeff King90553842019-07-31 00:38:15 -04001327 if (p_len < ce_len && !memcmp(ce_name, p, p_len) &&
Junio C Hamano730f7282009-09-20 00:03:39 -07001328 ce_name[p_len] < '/')
1329 continue; /* keep looking */
1330 break;
1331 }
1332 return -1;
1333}
1334
Derrick Stolee523506d2021-07-14 13:12:33 +00001335/*
1336 * Given a sparse directory entry 'ce', compare ce->name to
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001337 * info->traverse_path + p->path + '/' if info->traverse_path
1338 * is non-empty.
1339 *
Derrick Stolee523506d2021-07-14 13:12:33 +00001340 * Compare ce->name to p->path + '/' otherwise. Note that
1341 * ce->name must end in a trailing '/' because it is a sparse
1342 * directory entry.
1343 */
1344static int sparse_dir_matches_path(const struct cache_entry *ce,
1345 struct traverse_info *info,
1346 const struct name_entry *p)
1347{
1348 assert(S_ISSPARSEDIR(ce->ce_mode));
1349 assert(ce->name[ce->ce_namelen - 1] == '/');
1350
Derrick Stolee8c5de0d2021-12-06 14:10:37 +00001351 if (info->pathlen)
1352 return ce->ce_namelen == info->pathlen + p->pathlen + 1 &&
1353 ce->name[info->pathlen - 1] == '/' &&
1354 !strncmp(ce->name, info->traverse_path, info->pathlen) &&
1355 !strncmp(ce->name + info->pathlen, p->path, p->pathlen);
Derrick Stolee523506d2021-07-14 13:12:33 +00001356 return ce->ce_namelen == p->pathlen + 1 &&
1357 !strncmp(ce->name, p->path, p->pathlen);
1358}
1359
Junio C Hamano730f7282009-09-20 00:03:39 -07001360static struct cache_entry *find_cache_entry(struct traverse_info *info,
1361 const struct name_entry *p)
1362{
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001363 const char *path;
Jeff King90553842019-07-31 00:38:15 -04001364 int pos = find_cache_pos(info, p->path, p->pathlen);
Junio C Hamano730f7282009-09-20 00:03:39 -07001365 struct unpack_trees_options *o = info->data;
1366
1367 if (0 <= pos)
1368 return o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001369
1370 /*
1371 * Check for a sparse-directory entry named "path/".
1372 * Due to the input p->path not having a trailing
1373 * slash, the negative 'pos' value overshoots the
1374 * expected position, hence "-2" instead of "-1".
1375 */
1376 pos = -pos - 2;
1377
1378 if (pos < 0 || pos >= o->src_index->cache_nr)
Junio C Hamano730f7282009-09-20 00:03:39 -07001379 return NULL;
Derrick Stolee523506d2021-07-14 13:12:33 +00001380
1381 /*
1382 * Due to lexicographic sorting and sparse directory
1383 * entries ending with a trailing slash, our path as a
1384 * sparse directory (e.g "subdir/") and our path as a
1385 * file (e.g. "subdir") might be separated by other
1386 * paths (e.g. "subdir-").
1387 */
1388 while (pos >= 0) {
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001389 struct cache_entry *ce = o->src_index->cache[pos];
Derrick Stolee523506d2021-07-14 13:12:33 +00001390
Derrick Stolee72d84ea2021-09-08 01:42:27 +00001391 if (!skip_prefix(ce->name, info->traverse_path, &path) ||
1392 strncmp(path, p->path, p->pathlen) ||
1393 path[p->pathlen] != '/')
Derrick Stolee523506d2021-07-14 13:12:33 +00001394 return NULL;
1395
1396 if (S_ISSPARSEDIR(ce->ce_mode) &&
1397 sparse_dir_matches_path(ce, info, p))
1398 return ce;
1399
1400 pos--;
1401 }
1402
1403 return NULL;
Junio C Hamano730f7282009-09-20 00:03:39 -07001404}
1405
Junio C Hamanoba655da2009-09-14 02:22:00 -07001406static void debug_path(struct traverse_info *info)
1407{
1408 if (info->prev) {
1409 debug_path(info->prev);
Jeff King90553842019-07-31 00:38:15 -04001410 if (*info->prev->name)
Junio C Hamanoba655da2009-09-14 02:22:00 -07001411 putchar('/');
1412 }
Jeff King90553842019-07-31 00:38:15 -04001413 printf("%s", info->name);
Junio C Hamanoba655da2009-09-14 02:22:00 -07001414}
1415
1416static void debug_name_entry(int i, struct name_entry *n)
1417{
1418 printf("ent#%d %06o %s\n", i,
1419 n->path ? n->mode : 0,
1420 n->path ? n->path : "(missing)");
1421}
1422
1423static void debug_unpack_callback(int n,
1424 unsigned long mask,
1425 unsigned long dirmask,
1426 struct name_entry *names,
1427 struct traverse_info *info)
1428{
1429 int i;
1430 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
1431 mask, dirmask, n);
1432 debug_path(info);
1433 putchar('\n');
1434 for (i = 0; i < n; i++)
1435 debug_name_entry(i, names + i);
1436}
1437
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001438/*
Derrick Stolee523506d2021-07-14 13:12:33 +00001439 * Returns true if and only if the given cache_entry is a
1440 * sparse-directory entry that matches the given name_entry
1441 * from the tree walk at the given traverse_info.
1442 */
1443static int is_sparse_directory_entry(struct cache_entry *ce,
Victoria Dye037f8ea2022-09-01 21:02:33 +00001444 const struct name_entry *name,
Derrick Stolee523506d2021-07-14 13:12:33 +00001445 struct traverse_info *info)
1446{
1447 if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))
1448 return 0;
1449
1450 return sparse_dir_matches_path(ce, info, name);
1451}
1452
Victoria Dyeab810472022-03-01 20:24:30 +00001453static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1454{
1455 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
1456 struct unpack_trees_options *o = info->data;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001457 int ret, is_new_sparse_dir;
Victoria Dyeab810472022-03-01 20:24:30 +00001458
1459 assert(o->merge);
1460
1461 /*
1462 * Unlike in 'unpack_callback', where src[0] is derived from the index when
1463 * merging, src[0] is a transient cache entry derived from the first tree
1464 * provided. Create the temporary entry as if it came from a non-sparse index.
1465 */
1466 if (!is_null_oid(&names[0].oid)) {
1467 src[0] = create_ce_entry(info, &names[0], 0,
Elijah Newren13e1fd62023-02-27 15:28:17 +00001468 &o->internal.result, 1,
Victoria Dyeab810472022-03-01 20:24:30 +00001469 dirmask & (1ul << 0));
1470 src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1471 }
1472
1473 /*
1474 * 'unpack_single_entry' assumes that src[0] is derived directly from
1475 * the index, rather than from an entry in 'names'. This is *not* true when
1476 * merging a sparse directory, in which case names[0] is the "index" source
1477 * entry. To match the expectations of 'unpack_single_entry', shift past the
1478 * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1479 * 'dirmask' accordingly.
1480 */
Victoria Dyeb15207b2022-08-08 19:07:52 +00001481 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 +00001482
1483 if (src[0])
1484 discard_cache_entry(src[0]);
1485
1486 return ret >= 0 ? mask : -1;
1487}
1488
Derrick Stolee523506d2021-07-14 13:12:33 +00001489/*
Nguyễn Thái Ngọc Duyb4da3732018-08-18 16:41:24 +02001490 * Note that traverse_by_cache_tree() duplicates some logic in this function
1491 * without actually calling it. If you change the logic here you may need to
1492 * check and change there as well.
1493 */
Linus Torvalds01904572008-03-05 20:15:44 -08001494static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1495{
René Scharfec7cddc12009-01-31 15:39:10 +01001496 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Linus Torvalds01904572008-03-05 20:15:44 -08001497 struct unpack_trees_options *o = info->data;
Linus Torvalds01904572008-03-05 20:15:44 -08001498 const struct name_entry *p = names;
Victoria Dyeb15207b2022-08-08 19:07:52 +00001499 int is_new_sparse_dir;
Linus Torvalds01904572008-03-05 20:15:44 -08001500
1501 /* Find first entry with a real name (we could use "mask" too) */
1502 while (!p->mode)
1503 p++;
1504
Elijah Newren1ca13dd2023-02-27 15:28:19 +00001505 if (o->internal.debug_unpack)
Junio C Hamanoba655da2009-09-14 02:22:00 -07001506 debug_unpack_callback(n, mask, dirmask, names, info);
1507
Linus Torvalds01904572008-03-05 20:15:44 -08001508 /* Are we supposed to look at the index too? */
1509 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001510 while (1) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001511 int cmp;
Junio C Hamano730f7282009-09-20 00:03:39 -07001512 struct cache_entry *ce;
1513
1514 if (o->diff_index_cached)
Victoria Dye99430aa2022-03-17 15:55:36 +00001515 ce = next_cache_entry(o);
Junio C Hamano730f7282009-09-20 00:03:39 -07001516 else
1517 ce = find_cache_entry(info, p);
1518
Junio C Hamanoda165f42010-01-07 14:59:54 -08001519 if (!ce)
1520 break;
1521 cmp = compare_entry(ce, info, p);
Linus Torvalds01904572008-03-05 20:15:44 -08001522 if (cmp < 0) {
1523 if (unpack_index_entry(ce, o) < 0)
Junio C Hamano353c5ee2009-09-19 16:36:45 -07001524 return unpack_failed(o, NULL);
Linus Torvalds01904572008-03-05 20:15:44 -08001525 continue;
1526 }
1527 if (!cmp) {
1528 if (ce_stage(ce)) {
1529 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001530 * If we skip unmerged index
1531 * entries, we'll skip this
1532 * entry *and* the tree
1533 * entries associated with it!
Linus Torvalds01904572008-03-05 20:15:44 -08001534 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08001535 if (o->skip_unmerged) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001536 add_same_unmerged(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -08001537 return mask;
Linus Torvalds34110cd2008-03-06 18:12:28 -08001538 }
Linus Torvalds01904572008-03-05 20:15:44 -08001539 }
1540 src[0] = ce;
Linus Torvalds01904572008-03-05 20:15:44 -08001541 }
1542 break;
1543 }
1544 }
1545
Victoria Dyeb15207b2022-08-08 19:07:52 +00001546 if (unpack_single_entry(n, mask, dirmask, src, names, info, &is_new_sparse_dir))
Linus Torvalds01904572008-03-05 20:15:44 -08001547 return -1;
1548
René Scharfe97e59542012-04-10 20:55:58 +02001549 if (o->merge && src[0]) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001550 if (ce_stage(src[0]))
1551 mark_ce_used_same_name(src[0], o);
1552 else
1553 mark_ce_used(src[0], o);
1554 }
1555
Linus Torvalds01904572008-03-05 20:15:44 -08001556 /* Now handle any directories.. */
1557 if (dirmask) {
Junio C Hamanob65982b2009-05-20 15:57:22 -07001558 /* special case: "diff-index --cached" looking at a tree */
1559 if (o->diff_index_cached &&
1560 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
1561 int matches;
1562 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
1563 names, info);
1564 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001565 * Everything under the name matches; skip the
1566 * entire hierarchy. diff_index_cached codepath
1567 * special cases D/F conflicts in such a way that
1568 * it does not do any look-ahead, so this is safe.
Junio C Hamanob65982b2009-05-20 15:57:22 -07001569 */
1570 if (matches) {
Victoria Dyebfc763d2022-03-17 15:55:35 +00001571 /*
1572 * Only increment the cache_bottom if the
1573 * directory isn't a sparse directory index
1574 * entry (if it is, it was already incremented)
1575 * in 'mark_ce_used()'
1576 */
1577 if (!src[0] || !S_ISSPARSEDIR(src[0]->ce_mode))
Elijah Newren13e1fd62023-02-27 15:28:17 +00001578 o->internal.cache_bottom += matches;
Junio C Hamanob65982b2009-05-20 15:57:22 -07001579 return mask;
1580 }
1581 }
1582
Victoria Dye037f8ea2022-09-01 21:02:33 +00001583 if (!is_sparse_directory_entry(src[0], p, info) &&
Victoria Dyeb15207b2022-08-08 19:07:52 +00001584 !is_new_sparse_dir &&
Derrick Stolee523506d2021-07-14 13:12:33 +00001585 traverse_trees_recursive(n, dirmask, mask & ~dirmask,
1586 names, info) < 0) {
Junio C Hamano542c2642008-03-10 01:26:23 -07001587 return -1;
Derrick Stolee523506d2021-07-14 13:12:33 +00001588 }
1589
Linus Torvalds01904572008-03-05 20:15:44 -08001590 return mask;
1591 }
1592
1593 return mask;
1594}
1595
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001596static int clear_ce_flags_1(struct index_state *istate,
1597 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001598 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001599 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001600 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001601 enum pattern_match_result default_match,
1602 int progress_nr);
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001603
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001604/* Whole directory matching */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001605static int clear_ce_flags_dir(struct index_state *istate,
1606 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001607 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001608 char *basename,
1609 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001610 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001611 enum pattern_match_result default_match,
1612 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001613{
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001614 struct cache_entry **cache_end;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001615 int dtype = DT_DIR;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001616 int rc;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001617 enum pattern_match_result ret, orig_ret;
1618 orig_ret = path_matches_pattern_list(prefix->buf, prefix->len,
1619 basename, &dtype, pl, istate);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001620
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001621 strbuf_addch(prefix, '/');
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001622
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001623 /* If undecided, use matching result of parent dir in defval */
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001624 if (orig_ret == UNDECIDED)
Derrick Stolee468ce992019-09-03 11:04:58 -07001625 ret = default_match;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001626 else
1627 ret = orig_ret;
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001628
1629 for (cache_end = cache; cache_end != cache + nr; cache_end++) {
1630 struct cache_entry *ce = *cache_end;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001631 if (strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001632 break;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001633 }
1634
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001635 if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {
1636 struct cache_entry **ce = cache;
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001637 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001638
1639 while (ce < cache_end) {
1640 (*ce)->ce_flags &= ~clear_mask;
1641 ce++;
1642 }
1643 } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) {
Derrick Stolee via GitGitGadget4c6c7972020-01-10 01:59:30 +00001644 rc = cache_end - cache;
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001645 } else {
1646 rc = clear_ce_flags_1(istate, cache, cache_end - cache,
1647 prefix,
1648 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001649 pl, ret,
1650 progress_nr);
Derrick Stoleeeb42fec2019-11-21 22:04:43 +00001651 }
1652
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001653 strbuf_setlen(prefix, prefix->len - 1);
1654 return rc;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001655}
1656
1657/*
1658 * Traverse the index, find every entry that matches according to
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001659 * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001660 * number of traversed entries.
1661 *
1662 * If select_mask is non-zero, only entries whose ce_flags has on of
1663 * those bits enabled are traversed.
1664 *
1665 * cache : pointer to an index entry
1666 * prefix_len : an offset to its path
1667 *
1668 * The current path ("prefix") including the trailing '/' is
1669 * cache[0]->name[0..(prefix_len-1)]
1670 * Top level path has prefix_len zero.
1671 */
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001672static int clear_ce_flags_1(struct index_state *istate,
1673 struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001674 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001675 int select_mask, int clear_mask,
Derrick Stolee468ce992019-09-03 11:04:58 -07001676 struct pattern_list *pl,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001677 enum pattern_match_result default_match,
1678 int progress_nr)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001679{
Jeff Kingd20bc012020-01-29 00:46:47 -05001680 struct cache_entry **cache_end = nr ? cache + nr : cache;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001681
1682 /*
1683 * Process all entries that have the given prefix and meet
1684 * select_mask condition
1685 */
1686 while(cache != cache_end) {
1687 struct cache_entry *ce = *cache;
1688 const char *name, *slash;
Derrick Stolee468ce992019-09-03 11:04:58 -07001689 int len, dtype;
1690 enum pattern_match_result ret;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001691
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001692 display_progress(istate->progress, progress_nr);
1693
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001694 if (select_mask && !(ce->ce_flags & select_mask)) {
1695 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001696 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001697 continue;
1698 }
1699
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001700 if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001701 break;
1702
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001703 name = ce->name + prefix->len;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001704 slash = strchr(name, '/');
1705
1706 /* If it's a directory, try whole directory match first */
1707 if (slash) {
1708 int processed;
1709
1710 len = slash - name;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001711 strbuf_add(prefix, name, len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001712
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001713 processed = clear_ce_flags_dir(istate, cache, cache_end - cache,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001714 prefix,
1715 prefix->buf + prefix->len - len,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001716 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001717 pl, default_match,
1718 progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001719
1720 /* clear_c_f_dir eats a whole dir already? */
1721 if (processed) {
1722 cache += processed;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001723 progress_nr += processed;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001724 strbuf_setlen(prefix, prefix->len - len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001725 continue;
1726 }
1727
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001728 strbuf_addch(prefix, '/');
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001729 processed = clear_ce_flags_1(istate, cache, cache_end - cache,
1730 prefix,
1731 select_mask, clear_mask, pl,
1732 default_match, progress_nr);
1733
1734 cache += processed;
1735 progress_nr += processed;
1736
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001737 strbuf_setlen(prefix, prefix->len - len - 1);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001738 continue;
1739 }
1740
1741 /* Non-directory */
1742 dtype = ce_to_dtype(ce);
Derrick Stolee468ce992019-09-03 11:04:58 -07001743 ret = path_matches_pattern_list(ce->name,
1744 ce_namelen(ce),
1745 name, &dtype, pl, istate);
1746 if (ret == UNDECIDED)
1747 ret = default_match;
Derrick Stoleef998a3f2020-01-31 20:16:15 +00001748 if (ret == MATCHED || ret == MATCHED_RECURSIVE)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001749 ce->ce_flags &= ~clear_mask;
1750 cache++;
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001751 progress_nr++;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001752 }
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001753
1754 display_progress(istate->progress, progress_nr);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001755 return nr - (cache_end - cache);
1756}
1757
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001758static int clear_ce_flags(struct index_state *istate,
1759 int select_mask, int clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001760 struct pattern_list *pl,
1761 int show_progress)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001762{
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001763 static struct strbuf prefix = STRBUF_INIT;
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001764 char label[100];
1765 int rval;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001766
1767 strbuf_reset(&prefix);
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001768 if (show_progress)
1769 istate->progress = start_delayed_progress(
1770 _("Updating index flags"),
1771 istate->cache_nr);
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001772
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001773 xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)",
1774 (unsigned long)select_mask, (unsigned long)clear_mask);
1775 trace2_region_enter("unpack_trees", label, the_repository);
1776 rval = clear_ce_flags_1(istate,
Nguyễn Thái Ngọc Duy27c82fb2018-08-13 18:14:28 +02001777 istate->cache,
1778 istate->cache_nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001779 &prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001780 select_mask, clear_mask,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001781 pl, 0, 0);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001782 trace2_region_leave("unpack_trees", label, the_repository);
1783
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001784 stop_progress(&istate->progress);
Jeff Hostetlere6152e32019-11-21 22:04:39 +00001785 return rval;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001786}
1787
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001788/*
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001789 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1790 */
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001791static void mark_new_skip_worktree(struct pattern_list *pl,
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001792 struct index_state *istate,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001793 int select_flag, int skip_wt_flag,
1794 int show_progress)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001795{
1796 int i;
1797
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001798 /*
1799 * 1. Pretend the narrowest worktree: only unmerged entries
1800 * are checked out
1801 */
Nguyễn Thái Ngọc Duy86016ec2018-08-13 18:14:27 +02001802 for (i = 0; i < istate->cache_nr; i++) {
1803 struct cache_entry *ce = istate->cache[i];
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001804
1805 if (select_flag && !(ce->ce_flags & select_flag))
1806 continue;
1807
Max Kirillovb33fdfc2018-07-10 22:17:48 +03001808 if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001809 ce->ce_flags |= skip_wt_flag;
1810 else
1811 ce->ce_flags &= ~skip_wt_flag;
1812 }
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001813
1814 /*
1815 * 2. Widen worktree according to sparse-checkout file.
1816 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1817 */
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001818 clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001819}
1820
Elijah Newren30e89c12020-03-27 00:48:51 +00001821static void populate_from_existing_patterns(struct unpack_trees_options *o,
1822 struct pattern_list *pl)
1823{
Derrick Stoleedd230222021-01-23 19:58:17 +00001824 if (get_sparse_checkout_patterns(pl) < 0)
Elijah Newren30e89c12020-03-27 00:48:51 +00001825 o->skip_sparse_checkout = 1;
1826 else
Elijah Newren576de3d2023-02-27 15:28:16 +00001827 o->internal.pl = pl;
Elijah Newren30e89c12020-03-27 00:48:51 +00001828}
1829
Victoria Dye74970392022-03-01 20:24:29 +00001830static void update_sparsity_for_prefix(const char *prefix,
1831 struct index_state *istate)
1832{
1833 int prefix_len = strlen(prefix);
1834 struct strbuf ce_prefix = STRBUF_INIT;
1835
1836 if (!istate->sparse_index)
1837 return;
1838
1839 while (prefix_len > 0 && prefix[prefix_len - 1] == '/')
1840 prefix_len--;
1841
1842 if (prefix_len <= 0)
1843 BUG("Invalid prefix passed to update_sparsity_for_prefix");
1844
1845 strbuf_grow(&ce_prefix, prefix_len + 1);
1846 strbuf_add(&ce_prefix, prefix, prefix_len);
1847 strbuf_addch(&ce_prefix, '/');
1848
1849 /*
1850 * If the prefix points to a sparse directory or a path inside a sparse
1851 * directory, the index should be expanded. This is accomplished in one
1852 * of two ways:
1853 * - if the prefix is inside a sparse directory, it will be expanded by
1854 * the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1855 * - if the prefix matches an existing sparse directory entry,
1856 * 'index_name_pos(...)' will return its index position, triggering
1857 * the 'ensure_full_index(...)' below.
1858 */
1859 if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) &&
1860 index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0)
1861 ensure_full_index(istate);
1862
1863 strbuf_release(&ce_prefix);
1864}
Elijah Newren30e89c12020-03-27 00:48:51 +00001865
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001866static int verify_absent(const struct cache_entry *,
1867 enum unpack_trees_error_types,
1868 struct unpack_trees_options *);
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001869/*
1870 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1871 * 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 +07001872 *
1873 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001874 */
Linus Torvalds933bf402007-08-09 22:21:29 -07001875int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +02001876{
Derrick Stolee6863df32021-03-30 13:10:52 +00001877 struct repository *repo = the_repository;
Victoria Dye99430aa2022-03-17 15:55:36 +00001878 int i, ret;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001879 static struct cache_entry *dfc;
Derrick Stoleecaa3d552019-09-03 11:04:56 -07001880 struct pattern_list pl;
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001881 int free_pattern_list = 0;
Elijah Newrenc42e0b62021-09-27 16:33:42 +00001882 struct dir_struct dir = DIR_INIT;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001883
Elijah Newren480d3d62021-09-27 16:33:44 +00001884 if (o->reset == UNPACK_RESET_INVALID)
1885 BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001886
Junio C Hamanoca885a42008-03-13 22:07:18 -07001887 if (len > MAX_UNPACK_TREES)
1888 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
Elijah Newren576de3d2023-02-27 15:28:16 +00001889 if (o->internal.dir)
1890 BUG("o->internal.dir is for internal use only");
1891 if (o->internal.pl)
1892 BUG("o->internal.pl is for internal use only");
Elijah Newrenf2974242023-02-27 15:28:20 +00001893 if (o->df_conflict_entry)
1894 BUG("o->df_conflict_entry is an output only field");
Johannes Schindelin16da1342006-07-30 20:25:18 +02001895
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02001896 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00001897 trace2_region_enter("unpack_trees", "unpack_trees", the_repository);
1898
Derrick Stolee6863df32021-03-30 13:10:52 +00001899 prepare_repo_settings(repo);
1900 if (repo->settings.command_requires_full_index) {
1901 ensure_full_index(o->src_index);
Ævar Arnfjörð Bjarmason29fefaf2023-01-12 13:55:25 +01001902 if (o->dst_index)
1903 ensure_full_index(o->dst_index);
Derrick Stolee6863df32021-03-30 13:10:52 +00001904 }
1905
Elijah Newren480d3d62021-09-27 16:33:44 +00001906 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED &&
1907 o->preserve_ignored)
1908 BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");
1909
Elijah Newren04988c82021-09-27 16:33:41 +00001910 if (!o->preserve_ignored) {
Elijah Newren576de3d2023-02-27 15:28:16 +00001911 o->internal.dir = &dir;
1912 o->internal.dir->flags |= DIR_SHOW_IGNORED;
1913 setup_standard_excludes(o->internal.dir);
Elijah Newren04988c82021-09-27 16:33:41 +00001914 }
1915
Victoria Dye74970392022-03-01 20:24:29 +00001916 if (o->prefix)
1917 update_sparsity_for_prefix(o->prefix, o->src_index);
1918
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001919 if (!core_apply_sparse_checkout || !o->update)
1920 o->skip_sparse_checkout = 1;
Elijah Newren5d4f4a52023-02-27 15:28:13 +00001921 if (!o->skip_sparse_checkout) {
Elijah Newren30e89c12020-03-27 00:48:51 +00001922 memset(&pl, 0, sizeof(pl));
Elijah Newrenfa0bde42020-03-27 00:48:47 +00001923 free_pattern_list = 1;
Elijah Newren30e89c12020-03-27 00:48:51 +00001924 populate_from_existing_patterns(o, &pl);
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001925 }
1926
Elijah Newren13e1fd62023-02-27 15:28:17 +00001927 index_state_init(&o->internal.result, o->src_index->repo);
1928 o->internal.result.initialized = 1;
1929 o->internal.result.timestamp.sec = o->src_index->timestamp.sec;
1930 o->internal.result.timestamp.nsec = o->src_index->timestamp.nsec;
1931 o->internal.result.version = o->src_index->version;
Elijah Newren7db11832018-04-23 23:50:45 -07001932 if (!o->src_index->split_index) {
Elijah Newren13e1fd62023-02-27 15:28:17 +00001933 o->internal.result.split_index = NULL;
Elijah Newren7db11832018-04-23 23:50:45 -07001934 } else if (o->src_index == o->dst_index) {
1935 /*
1936 * o->dst_index (and thus o->src_index) will be discarded
Elijah Newren0d680a72023-02-27 15:28:18 +00001937 * and overwritten with o->internal.result at the end of
1938 * this function, so just use src_index's split_index to
1939 * avoid having to create a new one.
Elijah Newren7db11832018-04-23 23:50:45 -07001940 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00001941 o->internal.result.split_index = o->src_index->split_index;
Johannes Schindelin061dd722023-03-26 22:45:43 +00001942 if (o->src_index->cache_changed & SPLIT_INDEX_ORDERED)
Junio C Hamanof315a8b2023-04-04 14:28:27 -07001943 o->internal.result.cache_changed |= SPLIT_INDEX_ORDERED;
Elijah Newren13e1fd62023-02-27 15:28:17 +00001944 o->internal.result.split_index->refcount++;
Elijah Newren7db11832018-04-23 23:50:45 -07001945 } else {
Elijah Newren0d680a72023-02-27 15:28:18 +00001946 o->internal.result.split_index =
1947 init_split_index(&o->internal.result);
Elijah Newren7db11832018-04-23 23:50:45 -07001948 }
Elijah Newren13e1fd62023-02-27 15:28:17 +00001949 oidcpy(&o->internal.result.oid, &o->src_index->oid);
Elijah Newren1ca13dd2023-02-27 15:28:19 +00001950 o->internal.merge_size = len;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001951 mark_all_ce_unused(o->src_index);
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001952
Elijah Newren13e1fd62023-02-27 15:28:17 +00001953 o->internal.result.fsmonitor_last_update =
Johannes Schindelin3dfd3052021-03-17 15:30:48 +00001954 xstrdup_or_null(o->src_index->fsmonitor_last_update);
Elijah Newren13e1fd62023-02-27 15:28:17 +00001955 o->internal.result.fsmonitor_has_run_once = o->src_index->fsmonitor_has_run_once;
Utsav Shah679f2f92019-11-20 08:32:17 +00001956
Victoria Dye0f329b92022-05-10 23:32:32 +00001957 if (!o->src_index->initialized &&
1958 !repo->settings.command_requires_full_index &&
Elijah Newren13e1fd62023-02-27 15:28:17 +00001959 is_sparse_index_allowed(&o->internal.result, 0))
1960 o->internal.result.sparse_index = 1;
Victoria Dye0f329b92022-05-10 23:32:32 +00001961
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001962 /*
1963 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1964 */
1965 if (!o->skip_sparse_checkout)
Elijah Newren576de3d2023-02-27 15:28:16 +00001966 mark_new_skip_worktree(o->internal.pl, o->src_index, 0,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00001967 CE_NEW_SKIP_WORKTREE, o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001968
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001969 if (!dfc)
Jeff King13494ed2008-10-23 04:30:58 +00001970 dfc = xcalloc(1, cache_entry_size(0));
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001971 o->df_conflict_entry = dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001972
1973 if (len) {
Linus Torvalds01904572008-03-05 20:15:44 -08001974 const char *prefix = o->prefix ? o->prefix : "";
1975 struct traverse_info info;
Linus Torvalds933bf402007-08-09 22:21:29 -07001976
Linus Torvalds01904572008-03-05 20:15:44 -08001977 setup_traverse_info(&info, prefix);
1978 info.fn = unpack_callback;
1979 info.data = o;
Elijah Newren13e1fd62023-02-27 15:28:17 +00001980 info.show_all_errors = o->internal.show_all_errors;
Junio C Hamano40e37252011-08-29 12:31:06 -07001981 info.pathspec = o->pathspec;
Linus Torvalds01904572008-03-05 20:15:44 -08001982
Junio C Hamanoda165f42010-01-07 14:59:54 -08001983 if (o->prefix) {
1984 /*
1985 * Unpack existing index entries that sort before the
1986 * prefix the tree is spliced into. Note that o->merge
1987 * is always true in this case.
1988 */
1989 while (1) {
Victoria Dye99430aa2022-03-17 15:55:36 +00001990 struct cache_entry *ce = next_cache_entry(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001991 if (!ce)
1992 break;
1993 if (ce_in_traverse_path(ce, &info))
1994 break;
1995 if (unpack_index_entry(ce, o) < 0)
1996 goto return_failed;
1997 }
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001998 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08001999
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02002000 trace_performance_enter();
Derrick Stoleec3388982021-01-04 03:09:11 +00002001 trace2_region_enter("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +01002002 ret = traverse_trees(o->src_index, len, t, &info);
Derrick Stoleec3388982021-01-04 03:09:11 +00002003 trace2_region_leave("unpack_trees", "traverse_trees", the_repository);
Nguyễn Thái Ngọc Duy0d1ed592018-08-18 16:41:23 +02002004 trace_performance_leave("traverse_trees");
2005 if (ret < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08002006 goto return_failed;
Linus Torvalds01904572008-03-05 20:15:44 -08002007 }
2008
2009 /* Any left-over entries in the index? */
2010 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08002011 while (1) {
Victoria Dye99430aa2022-03-17 15:55:36 +00002012 struct cache_entry *ce = next_cache_entry(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002013 if (!ce)
2014 break;
Linus Torvalds01904572008-03-05 20:15:44 -08002015 if (unpack_index_entry(ce, o) < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08002016 goto return_failed;
Daniel Barkalow17e46422008-02-07 11:39:52 -05002017 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002018 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08002019 mark_all_ce_unused(o->src_index);
Johannes Schindelin16da1342006-07-30 20:25:18 +02002020
Elijah Newren13e1fd62023-02-27 15:28:17 +00002021 if (o->trivial_merges_only && o->internal.nontrivial_merge) {
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07002022 ret = unpack_failed(o, "Merge requires file-level merging");
2023 goto done;
2024 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002025
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002026 if (!o->skip_sparse_checkout) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002027 /*
2028 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
Elijah Newren031ba552020-03-27 00:48:44 +00002029 * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002030 * so apply_sparse_checkout() won't attempt to remove it from worktree
2031 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002032 mark_new_skip_worktree(o->internal.pl, &o->internal.result,
Derrick Stolee4dcd4de2019-11-21 22:04:44 +00002033 CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE,
2034 o->verbose_update);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002035
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10002036 ret = 0;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002037 for (i = 0; i < o->internal.result.cache_nr; i++) {
2038 struct cache_entry *ce = o->internal.result.cache[i];
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002039
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002040 /*
2041 * Entries marked with CE_ADDED in merged_entry() do not have
2042 * verify_absent() check (the check is effectively disabled
2043 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
2044 *
2045 * Do the real check now because we have had
2046 * correct CE_NEW_SKIP_WORKTREE
2047 */
2048 if (ce->ce_flags & CE_ADDED &&
Elijah Newren681c6372020-03-27 00:49:00 +00002049 verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
2050 ret = 1;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002051
Elijah Newren13e1fd62023-02-27 15:28:17 +00002052 if (apply_sparse_checkout(&o->internal.result, ce, o))
Elijah Newren681c6372020-03-27 00:49:00 +00002053 ret = 1;
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07002054 }
Elijah Newren681c6372020-03-27 00:49:00 +00002055 if (ret == 1) {
2056 /*
2057 * Inability to sparsify or de-sparsify individual
2058 * paths is not an error, but just a warning.
2059 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002060 if (o->internal.show_all_errors)
Elijah Newren681c6372020-03-27 00:49:00 +00002061 display_warning_msgs(o);
2062 ret = 0;
2063 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002064 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02002065
Elijah Newren13e1fd62023-02-27 15:28:17 +00002066 ret = check_updates(o, &o->internal.result) ? (-2) : 0;
Felipe Contrerase28f7642013-08-13 20:27:58 +02002067 if (o->dst_index) {
Elijah Newren13e1fd62023-02-27 15:28:17 +00002068 move_index_extensions(&o->internal.result, o->src_index);
Brian Degenhardt52fca212015-07-28 15:30:40 -04002069 if (!ret) {
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02002070 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
Elijah Newren0d680a72023-02-27 15:28:18 +00002071 cache_tree_verify(the_repository,
2072 &o->internal.result);
Victoria Dye68fcd482022-11-10 19:06:02 +00002073 if (!o->skip_cache_tree_update &&
Elijah Newren13e1fd62023-02-27 15:28:17 +00002074 !cache_tree_fully_valid(o->internal.result.cache_tree))
2075 cache_tree_update(&o->internal.result,
Brian Degenhardt52fca212015-07-28 15:30:40 -04002076 WRITE_TREE_SILENT |
2077 WRITE_TREE_REPAIR);
2078 }
Ben Peart1956ecd2019-02-15 12:59:21 -05002079
Elijah Newren13e1fd62023-02-27 15:28:17 +00002080 o->internal.result.updated_workdir = 1;
Felipe Contrerase28f7642013-08-13 20:27:58 +02002081 discard_index(o->dst_index);
Elijah Newren13e1fd62023-02-27 15:28:17 +00002082 *o->dst_index = o->internal.result;
Junio C Hamanoa16cc8b2014-11-17 12:12:41 -08002083 } else {
Elijah Newren13e1fd62023-02-27 15:28:17 +00002084 discard_index(&o->internal.result);
Felipe Contrerase28f7642013-08-13 20:27:58 +02002085 }
Elijah Newren7db11832018-04-23 23:50:45 -07002086 o->src_index = NULL;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07002087
2088done:
Elijah Newrenfa0bde42020-03-27 00:48:47 +00002089 if (free_pattern_list)
Derrick Stoleee0912282019-11-21 22:04:46 +00002090 clear_pattern_list(&pl);
Elijah Newren576de3d2023-02-27 15:28:16 +00002091 if (o->internal.dir) {
2092 dir_clear(o->internal.dir);
2093 o->internal.dir = NULL;
Elijah Newren04988c82021-09-27 16:33:41 +00002094 }
Derrick Stoleec3388982021-01-04 03:09:11 +00002095 trace2_region_leave("unpack_trees", "unpack_trees", the_repository);
Elijah Newrenfa0bde42020-03-27 00:48:47 +00002096 trace_performance_leave("unpack_trees");
Junio C Hamano2e2b8872008-05-28 15:12:30 -07002097 return ret;
Junio C Hamanoda165f42010-01-07 14:59:54 -08002098
2099return_failed:
Elijah Newren13e1fd62023-02-27 15:28:17 +00002100 if (o->internal.show_all_errors)
Matthieu Moye6c111b2010-08-11 10:38:07 +02002101 display_error_msgs(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002102 mark_all_ce_unused(o->src_index);
Junio C Hamano026680f2010-01-24 17:35:58 -08002103 ret = unpack_failed(o, NULL);
Junio C Hamanob4194822011-05-31 10:06:44 -07002104 if (o->exiting_early)
2105 ret = 0;
Junio C Hamano026680f2010-01-24 17:35:58 -08002106 goto done;
Johannes Schindelin16da1342006-07-30 20:25:18 +02002107}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002108
Elijah Newren7af7a252020-03-27 00:48:52 +00002109/*
2110 * Update SKIP_WORKTREE bits according to sparsity patterns, and update
2111 * working directory to match.
2112 *
2113 * CE_NEW_SKIP_WORKTREE is used internally.
2114 */
Elijah Newren1147c562023-02-27 15:28:14 +00002115enum update_sparsity_result update_sparsity(struct unpack_trees_options *o,
2116 struct pattern_list *pl)
Elijah Newren7af7a252020-03-27 00:48:52 +00002117{
2118 enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
Derrick Stoleeace224a2020-05-04 18:27:43 +00002119 int i;
Elijah Newren7af7a252020-03-27 00:48:52 +00002120 unsigned old_show_all_errors;
2121 int free_pattern_list = 0;
2122
Elijah Newren13e1fd62023-02-27 15:28:17 +00002123 old_show_all_errors = o->internal.show_all_errors;
2124 o->internal.show_all_errors = 1;
2125 index_state_init(&o->internal.result, o->src_index->repo);
Elijah Newren7af7a252020-03-27 00:48:52 +00002126
2127 /* Sanity checks */
2128 if (!o->update || o->index_only || o->skip_sparse_checkout)
2129 BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
2130 if (o->src_index != o->dst_index || o->fn)
2131 BUG("update_sparsity() called wrong");
2132
2133 trace_performance_enter();
2134
2135 /* If we weren't given patterns, use the recorded ones */
Elijah Newren1147c562023-02-27 15:28:14 +00002136 if (!pl) {
Elijah Newren7af7a252020-03-27 00:48:52 +00002137 free_pattern_list = 1;
Elijah Newren1147c562023-02-27 15:28:14 +00002138 pl = xcalloc(1, sizeof(*pl));
2139 populate_from_existing_patterns(o, pl);
Elijah Newren7af7a252020-03-27 00:48:52 +00002140 }
Elijah Newren576de3d2023-02-27 15:28:16 +00002141 o->internal.pl = pl;
Elijah Newren7af7a252020-03-27 00:48:52 +00002142
Derrick Stolee598b1e72022-05-23 13:48:46 +00002143 /* Expand sparse directories as needed */
Elijah Newren576de3d2023-02-27 15:28:16 +00002144 expand_index(o->src_index, o->internal.pl);
Derrick Stolee598b1e72022-05-23 13:48:46 +00002145
Elijah Newren7af7a252020-03-27 00:48:52 +00002146 /* Set NEW_SKIP_WORKTREE on existing entries. */
2147 mark_all_ce_unused(o->src_index);
Elijah Newren576de3d2023-02-27 15:28:16 +00002148 mark_new_skip_worktree(o->internal.pl, o->src_index, 0,
Elijah Newren7af7a252020-03-27 00:48:52 +00002149 CE_NEW_SKIP_WORKTREE, o->verbose_update);
2150
2151 /* Then loop over entries and update/remove as needed */
2152 ret = UPDATE_SPARSITY_SUCCESS;
Elijah Newren7af7a252020-03-27 00:48:52 +00002153 for (i = 0; i < o->src_index->cache_nr; i++) {
2154 struct cache_entry *ce = o->src_index->cache[i];
2155
Elijah Newrenebb568b2020-03-27 00:48:59 +00002156
2157 if (ce_stage(ce)) {
2158 /* -1 because for loop will increment by 1 */
2159 i += warn_conflicted_path(o->src_index, i, o) - 1;
2160 ret = UPDATE_SPARSITY_WARNINGS;
2161 continue;
2162 }
2163
Elijah Newren7af7a252020-03-27 00:48:52 +00002164 if (apply_sparse_checkout(o->src_index, ce, o))
2165 ret = UPDATE_SPARSITY_WARNINGS;
Elijah Newren7af7a252020-03-27 00:48:52 +00002166 }
2167
Elijah Newren7af7a252020-03-27 00:48:52 +00002168 if (check_updates(o, o->src_index))
2169 ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
2170
Elijah Newren6271d772020-03-27 00:48:57 +00002171 display_warning_msgs(o);
Elijah Newren13e1fd62023-02-27 15:28:17 +00002172 o->internal.show_all_errors = old_show_all_errors;
Elijah Newren1147c562023-02-27 15:28:14 +00002173 if (free_pattern_list) {
2174 clear_pattern_list(pl);
2175 free(pl);
Elijah Newren576de3d2023-02-27 15:28:16 +00002176 o->internal.pl = NULL;
Elijah Newren1147c562023-02-27 15:28:14 +00002177 }
Elijah Newren7af7a252020-03-27 00:48:52 +00002178 trace_performance_leave("update_sparsity");
2179 return ret;
2180}
2181
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002182/* Here come the merge functions */
2183
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002184static int reject_merge(const struct cache_entry *ce,
2185 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002186{
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002187 return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002188}
2189
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002190static int same(const struct cache_entry *a, const struct cache_entry *b)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002191{
2192 if (!!a != !!b)
2193 return 0;
2194 if (!a && !b)
2195 return 1;
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002196 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
2197 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002198 return a->ce_mode == b->ce_mode &&
Jeff King4a7e27e2018-08-28 17:22:40 -04002199 oideq(&a->oid, &b->oid);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002200}
2201
2202
2203/*
2204 * When a CE gets turned into an unmerged entry, we
2205 * want it to be up-to-date
2206 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002207static int verify_uptodate_1(const struct cache_entry *ce,
2208 struct unpack_trees_options *o,
2209 enum unpack_trees_error_types error_type)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002210{
2211 struct stat st;
2212
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002213 if (o->index_only)
2214 return 0;
2215
2216 /*
2217 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2218 * if this entry is truly up-to-date because this file may be
2219 * overwritten.
2220 */
2221 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
2222 ; /* keep checking */
2223 else if (o->reset || ce_uptodate(ce))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002224 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002225
2226 if (!lstat(ce->name, &st)) {
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07002227 int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
2228 unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002229
2230 if (submodule_from_ce(ce)) {
2231 int r = check_submodule_move_head(ce,
2232 "HEAD", oid_to_hex(&ce->oid), o);
2233 if (r)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002234 return add_rejected_path(o, error_type, ce->name);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002235 return 0;
2236 }
2237
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002238 if (!changed)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002239 return 0;
Junio C Hamano936492d2007-08-03 22:13:09 -07002240 /*
Stefan Bellera7bc8452017-03-14 14:46:39 -07002241 * Historic default policy was to allow submodule to be out
2242 * of sync wrt the superproject index. If the submodule was
2243 * not considered interesting above, we don't care here.
Junio C Hamano936492d2007-08-03 22:13:09 -07002244 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002245 if (S_ISGITLINK(ce->ce_mode))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002246 return 0;
Stefan Bellera7bc8452017-03-14 14:46:39 -07002247
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002248 errno = 0;
2249 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002250 if (errno == ENOENT)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002251 return 0;
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002252 return add_rejected_path(o, error_type, ce->name);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002253}
2254
Elijah Newren64b1abe2018-04-19 10:58:12 -07002255int verify_uptodate(const struct cache_entry *ce,
2256 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002257{
Elijah Newren26b5d6b2022-01-14 15:59:40 +00002258 if (!o->skip_sparse_checkout &&
2259 (ce->ce_flags & CE_SKIP_WORKTREE) &&
2260 (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002261 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02002262 return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002263}
2264
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002265static int verify_uptodate_sparse(const struct cache_entry *ce,
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002266 struct unpack_trees_options *o)
2267{
Elijah Newren1ac83f42020-03-27 00:48:56 +00002268 return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002269}
2270
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002271/*
Elijah Newren13e1fd62023-02-27 15:28:17 +00002272 * TODO: We should actually invalidate o->internal.result, not src_index [1].
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002273 * But since cache tree and untracked cache both are not copied to
Elijah Newren13e1fd62023-02-27 15:28:17 +00002274 * o->internal.result until unpacking is complete, we invalidate them on
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002275 * src_index instead with the assumption that they will be copied to
2276 * dst_index at the end.
2277 *
2278 * [1] src_index->cache_tree is also used in unpack_callback() so if
Elijah Newren13e1fd62023-02-27 15:28:17 +00002279 * we invalidate o->internal.result, we need to update it to use
2280 * o->internal.result.cache_tree as well.
Nguyễn Thái Ngọc Duy383480b2018-08-13 18:14:26 +02002281 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002282static void invalidate_ce_path(const struct cache_entry *ce,
2283 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002284{
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07002285 if (!ce)
2286 return;
2287 cache_tree_invalidate_path(o->src_index, ce->name);
Nguyễn Thái Ngọc Duy0cacebf2018-02-07 16:21:40 +07002288 untracked_cache_invalidate_path(o->src_index, ce->name, 1);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002289}
2290
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002291/*
2292 * Check that checking out ce->sha1 in subdir ce->name is not
2293 * going to overwrite any working files.
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002294 */
Stefan Bellerd6b12302017-03-14 14:46:38 -07002295static int verify_clean_submodule(const char *old_sha1,
2296 const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002297 struct unpack_trees_options *o)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002298{
Stefan Bellera7bc8452017-03-14 14:46:39 -07002299 if (!submodule_from_ce(ce))
2300 return 0;
2301
2302 return check_submodule_move_head(ce, old_sha1,
2303 oid_to_hex(&ce->oid), o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002304}
2305
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002306static int verify_clean_subdirectory(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002307 struct unpack_trees_options *o)
Junio C Hamanoc8193532007-03-15 23:25:22 -07002308{
2309 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002310 * we are about to extract "ce->name"; we would not want to lose
Junio C Hamanoc8193532007-03-15 23:25:22 -07002311 * anything in the existing directory there.
2312 */
2313 int namelen;
Clemens Buchacher7b9e3ce2009-01-01 21:54:33 +01002314 int i;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002315 struct dir_struct d;
2316 char *pathbuf;
2317 int cnt = 0;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002318
Stefan Bellerd6b12302017-03-14 14:46:38 -07002319 if (S_ISGITLINK(ce->ce_mode)) {
brian m. carlson1053fe82017-10-15 22:07:06 +00002320 struct object_id oid;
brian m. carlsona98e6102017-10-15 22:07:07 +00002321 int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
Stefan Bellerd6b12302017-03-14 14:46:38 -07002322 /*
2323 * If we are not going to update the submodule, then
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002324 * we don't care.
2325 */
Jeff King4a7e27e2018-08-28 17:22:40 -04002326 if (!sub_head && oideq(&oid, &ce->oid))
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002327 return 0;
brian m. carlson1053fe82017-10-15 22:07:06 +00002328 return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),
Jeff Kingdf351c62019-03-20 04:15:27 -04002329 ce, o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002330 }
Junio C Hamanoc8193532007-03-15 23:25:22 -07002331
2332 /*
2333 * First let's make sure we do not have a local modification
2334 * in that directory.
2335 */
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02002336 namelen = ce_namelen(ce);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002337 for (i = locate_in_src_index(ce, o);
2338 i < o->src_index->cache_nr;
2339 i++) {
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002340 struct cache_entry *ce2 = o->src_index->cache[i];
2341 int len = ce_namelen(ce2);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002342 if (len < namelen ||
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002343 strncmp(ce->name, ce2->name, namelen) ||
2344 ce2->name[namelen] != '/')
Junio C Hamanoc8193532007-03-15 23:25:22 -07002345 break;
2346 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08002347 * ce2->name is an entry in the subdirectory to be
2348 * removed.
Junio C Hamanoc8193532007-03-15 23:25:22 -07002349 */
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002350 if (!ce_stage(ce2)) {
2351 if (verify_uptodate(ce2, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002352 return -1;
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01002353 add_entry(o, ce2, CE_REMOVE, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002354 invalidate_ce_path(ce, o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08002355 mark_ce_used(ce2, o);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002356 }
2357 cnt++;
2358 }
2359
Elijah Newrenb817e542021-12-09 05:08:27 +00002360 /* Do not lose a locally present file that is not ignored. */
Jeff King75faa452015-09-24 17:07:03 -04002361 pathbuf = xstrfmt("%.*s/", namelen, ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002362
2363 memset(&d, 0, sizeof(d));
Elijah Newren576de3d2023-02-27 15:28:16 +00002364 if (o->internal.dir)
Elijah Newrenb413a822023-02-27 15:28:09 +00002365 setup_standard_excludes(&d);
Nguyễn Thái Ngọc Duyc7f32592018-08-13 18:14:29 +02002366 i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
Ævar Arnfjörð Bjarmasone5a917f2021-10-07 11:46:09 +02002367 dir_clear(&d);
2368 free(pathbuf);
Junio C Hamanoc8193532007-03-15 23:25:22 -07002369 if (i)
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002370 return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
Elijah Newrenb817e542021-12-09 05:08:27 +00002371
2372 /* Do not lose startup_info->original_cwd */
2373 if (startup_info->original_cwd &&
2374 !strcmp(startup_info->original_cwd, ce->name))
2375 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
2376
Junio C Hamanoc8193532007-03-15 23:25:22 -07002377 return cnt;
2378}
2379
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002380/*
Linus Torvalds32260ad2008-03-22 09:35:59 -07002381 * This gets called when there was no index entry for the tree entry 'dst',
2382 * but we found a file in the working tree that 'lstat()' said was fine,
2383 * and we're on a case-insensitive filesystem.
2384 *
2385 * See if we can find a case-insensitive match in the index that also
2386 * matches the stat information, and assume it's that other file!
2387 */
Clemens Buchachera9307f52010-10-09 15:52:58 +02002388static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
Linus Torvalds32260ad2008-03-22 09:35:59 -07002389{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002390 const struct cache_entry *src;
Linus Torvalds32260ad2008-03-22 09:35:59 -07002391
Eric Sunshineebbd7432013-09-17 03:06:15 -04002392 src = index_file_exists(o->src_index, name, len, 1);
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07002393 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 -07002394}
2395
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002396enum absent_checking_type {
2397 COMPLETELY_ABSENT,
2398 ABSENT_ANY_DIRECTORY
2399};
2400
Clemens Buchachera9307f52010-10-09 15:52:58 +02002401static int check_ok_to_remove(const char *name, int len, int dtype,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002402 const struct cache_entry *ce, struct stat *st,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002403 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002404 enum absent_checking_type absent_type,
Clemens Buchachera9307f52010-10-09 15:52:58 +02002405 struct unpack_trees_options *o)
2406{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07002407 const struct cache_entry *result;
Clemens Buchachera9307f52010-10-09 15:52:58 +02002408
2409 /*
2410 * It may be that the 'lstat()' succeeded even though
2411 * target 'ce' was absent, because there is an old
2412 * entry that is different only in case..
2413 *
2414 * Ignore that lstat() if it matches.
2415 */
2416 if (ignore_case && icase_exists(o, name, len, st))
2417 return 0;
2418
Elijah Newren576de3d2023-02-27 15:28:16 +00002419 if (o->internal.dir &&
2420 is_excluded(o->internal.dir, o->src_index, name, &dtype))
Clemens Buchachera9307f52010-10-09 15:52:58 +02002421 /*
2422 * ce->name is explicitly excluded, so it is Ok to
2423 * overwrite it.
2424 */
2425 return 0;
2426 if (S_ISDIR(st->st_mode)) {
2427 /*
2428 * We are checking out path "foo" and
2429 * found "foo/." in the working tree.
2430 * This is tricky -- if we have modified
2431 * files that are in "foo/" we would lose
2432 * them.
2433 */
Jeff Kingdf351c62019-03-20 04:15:27 -04002434 if (verify_clean_subdirectory(ce, o) < 0)
Clemens Buchachera9307f52010-10-09 15:52:58 +02002435 return -1;
2436 return 0;
2437 }
2438
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002439 /* If we only care about directories, then we can remove */
2440 if (absent_type == ABSENT_ANY_DIRECTORY)
2441 return 0;
2442
Clemens Buchachera9307f52010-10-09 15:52:58 +02002443 /*
2444 * The previous round may already have decided to
2445 * delete this path, which is in a subdirectory that
2446 * is being replaced with a blob.
2447 */
Elijah Newren13e1fd62023-02-27 15:28:17 +00002448 result = index_file_exists(&o->internal.result, name, len, 0);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002449 if (result) {
2450 if (result->ce_flags & CE_REMOVE)
2451 return 0;
2452 }
2453
Nguyễn Thái Ngọc Duy191e9d22019-03-22 16:31:35 +07002454 return add_rejected_path(o, error_type, name);
Clemens Buchachera9307f52010-10-09 15:52:58 +02002455}
2456
Linus Torvalds32260ad2008-03-22 09:35:59 -07002457/*
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002458 * We do not want to remove or overwrite a working tree file that
Junio C Hamanof8a9d422006-12-04 16:00:46 -08002459 * is not tracked, unless it is ignored.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002460 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002461static int verify_absent_1(const struct cache_entry *ce,
2462 enum unpack_trees_error_types error_type,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002463 enum absent_checking_type absent_type,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002464 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002465{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002466 int len;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002467 struct stat st;
2468
Elijah Newren0b0ee332021-12-09 05:08:28 +00002469 if (o->index_only || !o->update)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002470 return 0;
Junio C Hamanoc8193532007-03-15 23:25:22 -07002471
Elijah Newren0b0ee332021-12-09 05:08:28 +00002472 if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {
2473 /* Avoid nuking startup_info->original_cwd... */
2474 if (startup_info->original_cwd &&
2475 !strcmp(startup_info->original_cwd, ce->name))
2476 return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,
2477 ce->name);
2478 /* ...but nuke anything else. */
2479 return 0;
2480 }
2481
Matheus Tavaresfab78a02021-03-18 15:43:47 -03002482 len = check_leading_path(ce->name, ce_namelen(ce), 0);
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002483 if (!len)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002484 return 0;
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02002485 else if (len > 0) {
Jeff Kingf514ef92015-08-19 14:12:37 -04002486 char *path;
2487 int ret;
Junio C Hamanoec0603e2007-07-12 01:04:16 -07002488
Jeff Kingf514ef92015-08-19 14:12:37 -04002489 path = xmemdupz(ce->name, len);
2490 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002491 ret = error_errno("cannot stat '%s'", path);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002492 else {
2493 if (submodule_from_ce(ce))
2494 ret = check_submodule_move_head(ce,
2495 oid_to_hex(&ce->oid),
2496 NULL, o);
2497 else
2498 ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002499 &st, error_type,
2500 absent_type, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002501 }
Jeff Kingf514ef92015-08-19 14:12:37 -04002502 free(path);
2503 return ret;
Jonathan Nieder92fda792011-01-12 20:26:36 -06002504 } else if (lstat(ce->name, &st)) {
2505 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07002506 return error_errno("cannot stat '%s'", ce->name);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002507 return 0;
2508 } else {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002509 if (submodule_from_ce(ce))
2510 return check_submodule_move_head(ce, oid_to_hex(&ce->oid),
2511 NULL, o);
2512
Clemens Buchachera9307f52010-10-09 15:52:58 +02002513 return check_ok_to_remove(ce->name, ce_namelen(ce),
Jonathan Nieder92fda792011-01-12 20:26:36 -06002514 ce_to_dtype(ce), ce, &st,
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002515 error_type, absent_type, o);
Jonathan Nieder92fda792011-01-12 20:26:36 -06002516 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002517}
Clemens Buchachera9307f52010-10-09 15:52:58 +02002518
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002519static int verify_absent(const struct cache_entry *ce,
Matthieu Moy08402b02010-08-11 10:38:06 +02002520 enum unpack_trees_error_types error_type,
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002521 struct unpack_trees_options *o)
2522{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002523 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07002524 return 0;
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002525 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
2526}
2527
2528static int verify_absent_if_directory(const struct cache_entry *ce,
2529 enum unpack_trees_error_types error_type,
2530 struct unpack_trees_options *o)
2531{
2532 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
2533 return 0;
2534 return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07002535}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002536
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002537static int verify_absent_sparse(const struct cache_entry *ce,
2538 enum unpack_trees_error_types error_type,
2539 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002540{
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002541 return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07002542}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002543
René Scharfef2fa3542013-06-02 17:46:54 +02002544static int merged_entry(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002545 const struct cache_entry *old,
René Scharfef2fa3542013-06-02 17:46:54 +02002546 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002547{
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002548 int update = CE_UPDATE;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002549 struct cache_entry *merge = dup_cache_entry(ce, &o->internal.result);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002550
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002551 if (!old) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002552 /*
2553 * New index entries. In sparse checkout, the following
2554 * verify_absent() will be delayed until after
2555 * traverse_trees() finishes in unpack_trees(), then:
2556 *
2557 * - CE_NEW_SKIP_WORKTREE will be computed correctly
2558 * - verify_absent() be called again, this time with
2559 * correct CE_NEW_SKIP_WORKTREE
2560 *
2561 * verify_absent() call here does nothing in sparse
2562 * checkout (i.e. o->skip_sparse_checkout == 0)
2563 */
2564 update |= CE_ADDED;
2565 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
2566
René Scharfef2fa3542013-06-02 17:46:54 +02002567 if (verify_absent(merge,
2568 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002569 discard_cache_entry(merge);
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002570 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002571 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002572 invalidate_ce_path(merge, o);
Stefan Bellera7bc8452017-03-14 14:46:39 -07002573
Philippe Blaine84704f2020-02-17 04:53:05 +00002574 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002575 int ret = check_submodule_move_head(ce, NULL,
2576 oid_to_hex(&ce->oid),
2577 o);
2578 if (ret)
2579 return ret;
2580 }
2581
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002582 } else if (!(old->ce_flags & CE_CONFLICTED)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002583 /*
2584 * See if we can re-use the old CE directly?
2585 * That way we get the uptodate stat info.
2586 *
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002587 * This also removes the UPDATE flag on a match; otherwise
2588 * we will end up overwriting local changes in the work tree.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002589 */
2590 if (same(old, merge)) {
Linus Torvaldseb7a2f12008-02-22 20:41:17 -08002591 copy_cache_entry(merge, old);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07002592 update = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002593 } else {
René Scharfef2fa3542013-06-02 17:46:54 +02002594 if (verify_uptodate(old, o)) {
Jameson Millera8497352018-07-02 19:49:31 +00002595 discard_cache_entry(merge);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002596 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02002597 }
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07002598 /* Migrate old flags over */
2599 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002600 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002601 }
Stefan Bellera7bc8452017-03-14 14:46:39 -07002602
Philippe Blaine84704f2020-02-17 04:53:05 +00002603 if (submodule_from_ce(ce) && file_exists(ce->name)) {
Stefan Bellera7bc8452017-03-14 14:46:39 -07002604 int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),
2605 oid_to_hex(&ce->oid),
2606 o);
2607 if (ret)
2608 return ret;
2609 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002610 } else {
2611 /*
2612 * Previously unmerged entry left as an existence
2613 * marker by read_index_unmerged();
2614 */
Elijah Newren1fdd51a2021-09-27 16:33:45 +00002615 if (verify_absent_if_directory(merge,
2616 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
2617 discard_cache_entry(merge);
2618 return -1;
2619 }
2620
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002621 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002622 }
2623
Johannes Schindelincc756ed2019-09-09 13:56:15 +02002624 if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0)
2625 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002626 return 1;
2627}
2628
Victoria Dyeab810472022-03-01 20:24:30 +00002629static int merged_sparse_dir(const struct cache_entry * const *src, int n,
2630 struct unpack_trees_options *o)
2631{
2632 struct tree_desc t[MAX_UNPACK_TREES + 1];
2633 void * tree_bufs[MAX_UNPACK_TREES + 1];
2634 struct traverse_info info;
2635 int i, ret;
2636
2637 /*
2638 * Create the tree traversal information for traversing into *only* the
2639 * sparse directory.
2640 */
2641 setup_traverse_info(&info, src[0]->name);
2642 info.fn = unpack_sparse_callback;
2643 info.data = o;
Elijah Newren13e1fd62023-02-27 15:28:17 +00002644 info.show_all_errors = o->internal.show_all_errors;
Victoria Dyeab810472022-03-01 20:24:30 +00002645 info.pathspec = o->pathspec;
2646
2647 /* Get the tree descriptors of the sparse directory in each of the merging trees */
2648 for (i = 0; i < n; i++)
2649 tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i],
2650 src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL);
2651
2652 ret = traverse_trees(o->src_index, n, t, &info);
2653
2654 for (i = 0; i < n; i++)
2655 free(tree_bufs[i]);
2656
2657 return ret;
2658}
2659
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002660static int deleted_entry(const struct cache_entry *ce,
2661 const struct cache_entry *old,
2662 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002663{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002664 /* Did it exist in the index? */
2665 if (!old) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002666 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002667 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002668 return 0;
Elijah Newren56d06fe2021-09-27 16:33:46 +00002669 } else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) {
2670 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08002671 }
Elijah Newren56d06fe2021-09-27 16:33:46 +00002672
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08002673 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
Linus Torvalds34110cd2008-03-06 18:12:28 -08002674 return -1;
2675 add_entry(o, ce, CE_REMOVE, 0);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08002676 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002677 return 1;
2678}
2679
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002680static int keep_entry(const struct cache_entry *ce,
2681 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002682{
Linus Torvalds34110cd2008-03-06 18:12:28 -08002683 add_entry(o, ce, 0, 0);
Nguyễn Thái Ngọc Duy5697ca92018-08-18 16:41:27 +02002684 if (ce_stage(ce))
2685 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002686 return 1;
2687}
2688
2689#if DBRT_DEBUG
2690static void show_stage_entry(FILE *o,
2691 const char *label, const struct cache_entry *ce)
2692{
2693 if (!ce)
2694 fprintf(o, "%s (missing)\n", label);
2695 else
2696 fprintf(o, "%s%06o %s %d\t%s\n",
2697 label,
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002698 ce->ce_mode,
brian m. carlson99d1a982016-09-05 20:07:52 +00002699 oid_to_hex(&ce->oid),
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002700 ce_stage(ce),
2701 ce->name);
2702}
2703#endif
2704
René Scharfe5828e832013-06-02 17:46:56 +02002705int threeway_merge(const struct cache_entry * const *stages,
2706 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002707{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002708 const struct cache_entry *index;
2709 const struct cache_entry *head;
2710 const struct cache_entry *remote = stages[o->head_idx + 1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002711 int count;
2712 int head_match = 0;
2713 int remote_match = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002714
2715 int df_conflict_head = 0;
2716 int df_conflict_remote = 0;
2717
2718 int any_anc_missing = 0;
2719 int no_anc_exists = 1;
2720 int i;
2721
2722 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002723 if (!stages[i] || stages[i] == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002724 any_anc_missing = 1;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002725 else
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002726 no_anc_exists = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002727 }
2728
2729 index = stages[0];
2730 head = stages[o->head_idx];
2731
2732 if (head == o->df_conflict_entry) {
2733 df_conflict_head = 1;
2734 head = NULL;
2735 }
2736
2737 if (remote == o->df_conflict_entry) {
2738 df_conflict_remote = 1;
2739 remote = NULL;
2740 }
2741
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002742 /*
2743 * First, if there's a #16 situation, note that to prevent #13
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002744 * and #14.
2745 */
2746 if (!same(remote, head)) {
2747 for (i = 1; i < o->head_idx; i++) {
2748 if (same(stages[i], head)) {
2749 head_match = i;
2750 }
2751 if (same(stages[i], remote)) {
2752 remote_match = i;
2753 }
2754 }
2755 }
2756
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002757 /*
2758 * We start with cases where the index is allowed to match
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002759 * something other than the head: #14(ALT) and #2ALT, where it
2760 * is permitted to match the result instead.
2761 */
2762 /* #14, #14ALT, #2ALT */
2763 if (remote && !df_conflict_head && head_match && !remote_match) {
Victoria Dyef27c1702022-03-01 20:24:31 +00002764 if (index && !same(index, remote) && !same(index, head)) {
2765 if (S_ISSPARSEDIR(index->ce_mode))
2766 return merged_sparse_dir(stages, 4, o);
2767 else
2768 return reject_merge(index, o);
2769 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002770 return merged_entry(remote, index, o);
2771 }
2772 /*
2773 * If we have an entry in the index cache, then we want to
2774 * make sure that it matches head.
2775 */
Victoria Dyef27c1702022-03-01 20:24:31 +00002776 if (index && !same(index, head)) {
2777 if (S_ISSPARSEDIR(index->ce_mode))
2778 return merged_sparse_dir(stages, 4, o);
2779 else
2780 return reject_merge(index, o);
2781 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002782
2783 if (head) {
2784 /* #5ALT, #15 */
2785 if (same(head, remote))
2786 return merged_entry(head, index, o);
2787 /* #13, #3ALT */
2788 if (!df_conflict_remote && remote_match && !head_match)
2789 return merged_entry(head, index, o);
2790 }
2791
2792 /* #1 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08002793 if (!head && !remote && any_anc_missing)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002794 return 0;
2795
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002796 /*
2797 * Under the "aggressive" rule, we resolve mostly trivial
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002798 * cases that we historically had git-merge-one-file resolve.
2799 */
2800 if (o->aggressive) {
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07002801 int head_deleted = !head;
2802 int remote_deleted = !remote;
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002803 const struct cache_entry *ce = NULL;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002804
2805 if (index)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002806 ce = index;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002807 else if (head)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002808 ce = head;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002809 else if (remote)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002810 ce = remote;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002811 else {
2812 for (i = 1; i < o->head_idx; i++) {
2813 if (stages[i] && stages[i] != o->df_conflict_entry) {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02002814 ce = stages[i];
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002815 break;
2816 }
2817 }
2818 }
2819
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002820 /*
2821 * Deleted in both.
2822 * Deleted in one and unchanged in the other.
2823 */
2824 if ((head_deleted && remote_deleted) ||
2825 (head_deleted && remote && remote_match) ||
2826 (remote_deleted && head && head_match)) {
2827 if (index)
2828 return deleted_entry(index, index, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08002829 if (ce && !head_deleted) {
Matthieu Moy08402b02010-08-11 10:38:06 +02002830 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002831 return -1;
2832 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002833 return 0;
2834 }
2835 /*
2836 * Added in both, identically.
2837 */
2838 if (no_anc_exists && head && remote && same(head, remote))
2839 return merged_entry(head, index, o);
2840
2841 }
2842
Victoria Dyef27c1702022-03-01 20:24:31 +00002843 /* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002844 if (index) {
Victoria Dyef27c1702022-03-01 20:24:31 +00002845 /*
2846 * If we've reached the "no merge" cases and we're merging
2847 * a sparse directory, we may have an "edit/edit" conflict that
2848 * can be resolved by individually merging directory contents.
2849 */
2850 if (S_ISSPARSEDIR(index->ce_mode))
2851 return merged_sparse_dir(stages, 4, o);
2852
2853 /*
2854 * If we're not merging a sparse directory, ensure the index is
2855 * up-to-date to avoid files getting overwritten with conflict
2856 * resolution files
2857 */
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05002858 if (verify_uptodate(index, o))
2859 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002860 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002861
Elijah Newren13e1fd62023-02-27 15:28:17 +00002862 o->internal.nontrivial_merge = 1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002863
Junio C Hamanoea4b52a2007-04-07 05:42:01 -07002864 /* #2, #3, #4, #6, #7, #9, #10, #11. */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002865 count = 0;
2866 if (!head_match || !remote_match) {
2867 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07002868 if (stages[i] && stages[i] != o->df_conflict_entry) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002869 keep_entry(stages[i], o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002870 count++;
2871 break;
2872 }
2873 }
2874 }
2875#if DBRT_DEBUG
2876 else {
2877 fprintf(stderr, "read-tree: warning #16 detected\n");
2878 show_stage_entry(stderr, "head ", stages[head_match]);
2879 show_stage_entry(stderr, "remote ", stages[remote_match]);
2880 }
2881#endif
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002882 if (head) { count += keep_entry(head, o); }
2883 if (remote) { count += keep_entry(remote, o); }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002884 return count;
2885}
2886
2887/*
2888 * Two-way merge.
2889 *
2890 * The rule is to "carry forward" what is in the index without losing
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03002891 * information across a "fast-forward", favoring a successful merge
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002892 * over a merge failure when it makes sense. For details of the
2893 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2894 *
2895 */
René Scharfe5828e832013-06-02 17:46:56 +02002896int twoway_merge(const struct cache_entry * const *src,
2897 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002898{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002899 const struct cache_entry *current = src[0];
2900 const struct cache_entry *oldtree = src[1];
2901 const struct cache_entry *newtree = src[2];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002902
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002903 if (o->internal.merge_size != 2)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002904 return error("Cannot do a twoway merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002905 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002906
Junio C Hamanob8ba1532007-04-02 16:29:56 -07002907 if (oldtree == o->df_conflict_entry)
2908 oldtree = NULL;
2909 if (newtree == o->df_conflict_entry)
2910 newtree = NULL;
2911
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002912 if (current) {
Jeff Kingb018ff62012-12-29 15:51:54 -05002913 if (current->ce_flags & CE_CONFLICTED) {
2914 if (same(oldtree, newtree) || o->reset) {
2915 if (!newtree)
2916 return deleted_entry(current, current, o);
2917 else
2918 return merged_entry(newtree, current, o);
2919 }
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002920 return reject_merge(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002921 } else if ((!oldtree && !newtree) || /* 4 and 5 */
Jeff Kingb018ff62012-12-29 15:51:54 -05002922 (!oldtree && newtree &&
2923 same(current, newtree)) || /* 6 and 7 */
2924 (oldtree && newtree &&
2925 same(oldtree, newtree)) || /* 14 and 15 */
2926 (oldtree && newtree &&
2927 !same(oldtree, newtree) && /* 18 and 19 */
2928 same(current, newtree))) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002929 return keep_entry(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002930 } else if (oldtree && !newtree && same(current, oldtree)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002931 /* 10 or 11 */
2932 return deleted_entry(oldtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002933 } else if (oldtree && newtree &&
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002934 same(current, oldtree) && !same(current, newtree)) {
2935 /* 20 or 21 */
2936 return merged_entry(newtree, current, o);
Derrick Stoleee05cdb12021-07-20 20:14:41 +00002937 } else if (current && !oldtree && newtree &&
2938 S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) &&
2939 ce_stage(current) == 0) {
2940 /*
2941 * This case is a directory/file conflict across the sparse-index
2942 * boundary. When we are changing from one path to another via
2943 * 'git checkout', then we want to replace one entry with another
2944 * via merged_entry(). If there are staged changes, then we should
2945 * reject the merge instead.
2946 */
2947 return merged_entry(newtree, current, o);
Victoria Dyeab810472022-03-01 20:24:30 +00002948 } else if (S_ISSPARSEDIR(current->ce_mode)) {
2949 /*
2950 * The sparse directories differ, but we don't know whether that's
2951 * because of two different files in the directory being modified
2952 * (can be trivially merged) or if there is a real file conflict.
2953 * Merge the sparse directory by OID to compare file-by-file.
2954 */
2955 return merged_sparse_dir(src, 3, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07002956 } else
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07002957 return reject_merge(current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002958 }
Junio C Hamano55218832008-09-07 19:49:25 -07002959 else if (newtree) {
2960 if (oldtree && !o->initial_checkout) {
2961 /*
2962 * deletion of the path was staged;
2963 */
2964 if (same(oldtree, newtree))
2965 return 1;
2966 return reject_merge(oldtree, o);
2967 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002968 return merged_entry(newtree, current, o);
Junio C Hamano55218832008-09-07 19:49:25 -07002969 }
Linus Torvaldsd6996762007-08-10 12:31:20 -07002970 return deleted_entry(oldtree, current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002971}
2972
2973/*
2974 * Bind merge.
2975 *
2976 * Keep the index entries at stage0, collapse stage1 but make sure
2977 * stage0 does not have anything there.
2978 */
René Scharfe5828e832013-06-02 17:46:56 +02002979int bind_merge(const struct cache_entry * const *src,
2980 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002981{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02002982 const struct cache_entry *old = src[0];
2983 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002984
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002985 if (o->internal.merge_size != 1)
Pete Wyckoff82247e92012-04-29 20:28:45 -04002986 return error("Cannot do a bind merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00002987 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002988 if (a && old)
Nguyễn Thái Ngọc Duyb165fac2019-03-22 16:31:36 +07002989 return o->quiet ? -1 :
Stefan Beller3d415422017-01-17 17:05:20 -08002990 error(ERRORMSG(o, ERROR_BIND_OVERLAP),
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +01002991 super_prefixed(a->name, o->super_prefix),
2992 super_prefixed(old->name, o->super_prefix));
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002993 if (!a)
Junio C Hamano7f7932a2007-04-02 00:06:12 -07002994 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02002995 else
2996 return merged_entry(a, NULL, o);
2997}
2998
2999/*
3000 * One-way merge.
3001 *
3002 * The rule is:
3003 * - take the stat information from stage0, take the data from stage1
3004 */
René Scharfe5828e832013-06-02 17:46:56 +02003005int oneway_merge(const struct cache_entry * const *src,
3006 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003007{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02003008 const struct cache_entry *old = src[0];
3009 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003010
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003011 if (o->internal.merge_size != 1)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003012 return error("Cannot do a oneway merge of %d trees",
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003013 o->internal.merge_size);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003014
Junio C Hamano78d3b062009-07-18 12:26:38 -07003015 if (!a || a == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003016 return deleted_entry(old, old, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08003017
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003018 if (old && same(old, a)) {
Linus Torvalds34110cd2008-03-06 18:12:28 -08003019 int update = 0;
Utsav Shah679f2f92019-11-20 08:32:17 +00003020 if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) &&
3021 !(old->ce_flags & CE_FSMONITOR_VALID)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003022 struct stat st;
3023 if (lstat(old->name, &st) ||
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07003024 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
Linus Torvalds34110cd2008-03-06 18:12:28 -08003025 update |= CE_UPDATE;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003026 }
Stefan Bellerad173122018-01-05 12:03:03 -08003027 if (o->update && S_ISGITLINK(old->ce_mode) &&
3028 should_update_submodules() && !verify_uptodate(old, o))
3029 update |= CE_UPDATE;
Nguyễn Thái Ngọc Duyab5af822019-03-18 18:38:22 +07003030 add_entry(o, old, update, CE_STAGEMASK);
Linus Torvalds34110cd2008-03-06 18:12:28 -08003031 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02003032 }
3033 return merged_entry(a, old, o);
3034}
Denton Liud3c7bf72021-03-03 03:16:42 -08003035
3036/*
3037 * Merge worktree and untracked entries in a stash entry.
3038 *
3039 * Ignore all index entries. Collapse remaining trees but make sure that they
3040 * don't have any conflicting files.
3041 */
3042int stash_worktree_untracked_merge(const struct cache_entry * const *src,
3043 struct unpack_trees_options *o)
3044{
3045 const struct cache_entry *worktree = src[1];
3046 const struct cache_entry *untracked = src[2];
3047
Elijah Newren1ca13dd2023-02-27 15:28:19 +00003048 if (o->internal.merge_size != 2)
3049 BUG("invalid merge_size: %d", o->internal.merge_size);
Denton Liud3c7bf72021-03-03 03:16:42 -08003050
3051 if (worktree && untracked)
3052 return error(_("worktree and untracked commit have duplicate entries: %s"),
Ævar Arnfjörð Bjarmason4002ec32022-12-20 13:39:56 +01003053 super_prefixed(worktree->name, o->super_prefix));
Denton Liud3c7bf72021-03-03 03:16:42 -08003054
3055 return merged_entry(worktree ? worktree : untracked, NULL, o);
3056}