blob: 7a6df99d10e1dbabc9f7df74316fd91bd55c1fd5 [file] [log] [blame]
Linus Torvaldsbc052d72008-03-06 12:26:14 -08001#define NO_THE_INDEX_COMPATIBILITY_MACROS
Johannes Schindelin16da1342006-07-30 20:25:18 +02002#include "cache.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -08003#include "dir.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02004#include "tree.h"
5#include "tree-walk.h"
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02006#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +02007#include "unpack-trees.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -04008#include "progress.h"
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02009#include "refs.h"
Junio C Hamano06f33c12009-03-13 21:24:08 -070010#include "attr.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070011#include "split-index.h"
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +070012#include "dir.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020013
Junio C Hamano8ccba002008-05-17 12:03:49 -070014/*
15 * Error messages expected by scripts out of plumbing commands such as
16 * read-tree. Non-scripted Porcelain is not required to use these messages
17 * and in fact are encouraged to reword them to better suit their particular
Diane Gasselin23cbf112010-08-11 10:38:05 +020018 * situation better. See how "git checkout" and "git merge" replaces
Matthieu Moydc1166e2010-09-02 13:57:33 +020019 * them using setup_unpack_trees_porcelain(), for example.
Junio C Hamano8ccba002008-05-17 12:03:49 -070020 */
Stephen Boydc2e86ad2011-03-22 00:51:05 -070021static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
Matthieu Moy08353eb2010-08-11 10:38:04 +020022 /* ERROR_WOULD_OVERWRITE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070023 "Entry '%s' would be overwritten by merge. Cannot merge.",
24
Matthieu Moy08353eb2010-08-11 10:38:04 +020025 /* ERROR_NOT_UPTODATE_FILE */
Junio C Hamano8ccba002008-05-17 12:03:49 -070026 "Entry '%s' not uptodate. Cannot merge.",
27
Matthieu Moy08353eb2010-08-11 10:38:04 +020028 /* ERROR_NOT_UPTODATE_DIR */
Junio C Hamano8ccba002008-05-17 12:03:49 -070029 "Updating '%s' would lose untracked files in it",
30
Matthieu Moy08402b02010-08-11 10:38:06 +020031 /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
32 "Untracked working tree file '%s' would be overwritten by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070033
Matthieu Moy08402b02010-08-11 10:38:06 +020034 /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
35 "Untracked working tree file '%s' would be removed by merge.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070036
Matthieu Moy08353eb2010-08-11 10:38:04 +020037 /* ERROR_BIND_OVERLAP */
Junio C Hamano8ccba002008-05-17 12:03:49 -070038 "Entry '%s' overlaps with '%s'. Cannot bind.",
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070039
Matthieu Moy08353eb2010-08-11 10:38:04 +020040 /* ERROR_SPARSE_NOT_UPTODATE_FILE */
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +070041 "Entry '%s' not uptodate. Cannot update sparse checkout.",
42
Matthieu Moy08402b02010-08-11 10:38:06 +020043 /* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */
44 "Working tree file '%s' would be overwritten by sparse checkout update.",
45
46 /* ERROR_WOULD_LOSE_ORPHANED_REMOVED */
47 "Working tree file '%s' would be removed by sparse checkout update.",
Junio C Hamano8ccba002008-05-17 12:03:49 -070048};
49
Matthieu Moy08353eb2010-08-11 10:38:04 +020050#define ERRORMSG(o,type) \
51 ( ((o) && (o)->msgs[(type)]) \
52 ? ((o)->msgs[(type)]) \
53 : (unpack_plumbing_errors[(type)]) )
Junio C Hamano8ccba002008-05-17 12:03:49 -070054
Matthieu Moye2940302010-09-02 13:57:34 +020055void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
56 const char *cmd)
Matthieu Moydc1166e2010-09-02 13:57:33 +020057{
Clemens Buchacher79808722010-11-15 20:52:19 +010058 int i;
Matthieu Moye2940302010-09-02 13:57:34 +020059 const char **msgs = opts->msgs;
Matthieu Moydc1166e2010-09-02 13:57:33 +020060 const char *msg;
Jeff Kingfa3f60b2014-06-18 16:02:13 -040061
Vasco Almeida2e3926b2016-05-12 23:16:26 +000062 if (!strcmp(cmd, "checkout"))
63 msg = advice_commit_before_merge
64 ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060065 "Please commit your changes or stash them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000066 : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
67 else if (!strcmp(cmd, "merge"))
68 msg = advice_commit_before_merge
69 ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060070 "Please commit your changes or stash them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000071 : _("Your local changes to the following files would be overwritten by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +020072 else
Vasco Almeida2e3926b2016-05-12 23:16:26 +000073 msg = advice_commit_before_merge
74 ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060075 "Please commit your changes or stash them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000076 : _("Your local changes to the following files would be overwritten by %s:\n%%s");
Jeff Kingfa3f60b2014-06-18 16:02:13 -040077 msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
Vasco Almeida2e3926b2016-05-12 23:16:26 +000078 xstrfmt(msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +020079
80 msgs[ERROR_NOT_UPTODATE_DIR] =
Stefan Beller584f99c2016-12-02 11:17:41 -080081 _("Updating the following directories would lose untracked files in them:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +020082
Vasco Almeida2e3926b2016-05-12 23:16:26 +000083 if (!strcmp(cmd, "checkout"))
84 msg = advice_commit_before_merge
85 ? _("The following untracked working tree files would be removed by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060086 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000087 : _("The following untracked working tree files would be removed by checkout:\n%%s");
88 else if (!strcmp(cmd, "merge"))
89 msg = advice_commit_before_merge
90 ? _("The following untracked working tree files would be removed by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060091 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000092 : _("The following untracked working tree files would be removed by merge:\n%%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +020093 else
Vasco Almeida2e3926b2016-05-12 23:16:26 +000094 msg = advice_commit_before_merge
95 ? _("The following untracked working tree files would be removed by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -060096 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +000097 : _("The following untracked working tree files would be removed by %s:\n%%s");
98 msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] = xstrfmt(msg, cmd, cmd);
Jeff Kingfa3f60b2014-06-18 16:02:13 -040099
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000100 if (!strcmp(cmd, "checkout"))
101 msg = advice_commit_before_merge
102 ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600103 "Please move or remove them before you switch branches.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000104 : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
105 else if (!strcmp(cmd, "merge"))
106 msg = advice_commit_before_merge
107 ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600108 "Please move or remove them before you merge.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000109 : _("The following untracked working tree files would be overwritten by merge:\n%%s");
110 else
111 msg = advice_commit_before_merge
112 ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
Alex Henriec2691e22016-06-25 00:34:04 -0600113 "Please move or remove them before you %s.")
Vasco Almeida2e3926b2016-05-12 23:16:26 +0000114 : _("The following untracked working tree files would be overwritten by %s:\n%%s");
115 msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] = xstrfmt(msg, cmd, cmd);
Matthieu Moydc1166e2010-09-02 13:57:33 +0200116
117 /*
118 * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
119 * cannot easily display it as a list.
120 */
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000121 msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200122
123 msgs[ERROR_SPARSE_NOT_UPTODATE_FILE] =
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000124 _("Cannot update sparse checkout: the following entries are not up-to-date:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200125 msgs[ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN] =
Alex Henriea1c80442016-09-07 22:34:53 -0600126 _("The following working tree files would be overwritten by sparse checkout update:\n%s");
Matthieu Moydc1166e2010-09-02 13:57:33 +0200127 msgs[ERROR_WOULD_LOSE_ORPHANED_REMOVED] =
Alex Henriea1c80442016-09-07 22:34:53 -0600128 _("The following working tree files would be removed by sparse checkout update:\n%s");
Matthieu Moy5e65ee32010-09-02 18:08:15 +0200129
130 opts->show_all_errors = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100131 /* rejected paths may not have a static buffer */
132 for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
133 opts->unpack_rejects[i].strdup_strings = 1;
Matthieu Moydc1166e2010-09-02 13:57:33 +0200134}
135
Jeff King46169182014-11-24 13:36:51 -0500136static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
René Scharfe6ff264e2012-03-06 20:37:23 +0100137 unsigned int set, unsigned int clear)
138{
Karsten Blees419a5972013-11-14 20:22:27 +0100139 clear |= CE_HASHED;
René Scharfe6ff264e2012-03-06 20:37:23 +0100140
141 if (set & CE_REMOVE)
142 set |= CE_WT_REMOVE;
143
René Scharfe6ff264e2012-03-06 20:37:23 +0100144 ce->ce_flags = (ce->ce_flags & ~clear) | set;
Jeff King46169182014-11-24 13:36:51 -0500145 return add_index_entry(&o->result, ce,
146 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
René Scharfe6ff264e2012-03-06 20:37:23 +0100147}
148
René Scharfea33bd4d2013-06-02 17:46:53 +0200149static struct cache_entry *dup_entry(const struct cache_entry *ce)
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700150{
Linus Torvalds34110cd2008-03-06 18:12:28 -0800151 unsigned int size = ce_size(ce);
152 struct cache_entry *new = xmalloc(size);
153
Linus Torvalds34110cd2008-03-06 18:12:28 -0800154 memcpy(new, ce, size);
René Scharfea33bd4d2013-06-02 17:46:53 +0200155 return new;
156}
157
158static void add_entry(struct unpack_trees_options *o,
159 const struct cache_entry *ce,
160 unsigned int set, unsigned int clear)
161{
162 do_add_entry(o, dup_entry(ce), set, clear);
Linus Torvaldsb48d5a02007-08-10 12:15:54 -0700163}
164
Kjetil Barvik78478922009-02-09 21:54:07 +0100165/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200166 * add error messages on path <path>
167 * corresponding to the type <e> with the message <msg>
168 * indicating if it should be display in porcelain or not
169 */
170static int add_rejected_path(struct unpack_trees_options *o,
171 enum unpack_trees_error_types e,
172 const char *path)
173{
Matthieu Moye6c111b2010-08-11 10:38:07 +0200174 if (!o->show_all_errors)
175 return error(ERRORMSG(o, e), path);
176
177 /*
178 * Otherwise, insert in a list for future display by
179 * display_error_msgs()
180 */
Clemens Buchacher79808722010-11-15 20:52:19 +0100181 string_list_append(&o->unpack_rejects[e], path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200182 return -1;
183}
184
185/*
Matthieu Moye6c111b2010-08-11 10:38:07 +0200186 * display all the error messages stored in a nice way
187 */
188static void display_error_msgs(struct unpack_trees_options *o)
189{
Clemens Buchacher79808722010-11-15 20:52:19 +0100190 int e, i;
Matthieu Moye6c111b2010-08-11 10:38:07 +0200191 int something_displayed = 0;
192 for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
Clemens Buchacher79808722010-11-15 20:52:19 +0100193 struct string_list *rejects = &o->unpack_rejects[e];
194 if (rejects->nr > 0) {
Matthieu Moye6c111b2010-08-11 10:38:07 +0200195 struct strbuf path = STRBUF_INIT;
196 something_displayed = 1;
Clemens Buchacher79808722010-11-15 20:52:19 +0100197 for (i = 0; i < rejects->nr; i++)
198 strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200199 error(ERRORMSG(o, e), path.buf);
200 strbuf_release(&path);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200201 }
Clemens Buchacher79808722010-11-15 20:52:19 +0100202 string_list_clear(rejects, 0);
Matthieu Moye6c111b2010-08-11 10:38:07 +0200203 }
204 if (something_displayed)
Vasco Almeidaed47fdf2016-04-09 20:38:39 +0000205 fprintf(stderr, _("Aborting\n"));
Matthieu Moye6c111b2010-08-11 10:38:07 +0200206}
207
208/*
Kjetil Barvik78478922009-02-09 21:54:07 +0100209 * Unlink the last component and schedule the leading directories for
210 * removal, such that empty directories get removed.
Johannes Schindelin16da1342006-07-30 20:25:18 +0200211 */
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700212static void unlink_entry(const struct cache_entry *ce)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200213{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +0200214 if (!check_leading_path(ce->name, ce_namelen(ce)))
Junio C Hamano16a4c612007-05-10 23:44:53 -0700215 return;
Peter Collingbourne80d706a2010-03-26 15:25:34 +0000216 if (remove_or_warn(ce->ce_mode, ce->name))
217 return;
Kjetil Barvik78478922009-02-09 21:54:07 +0100218 schedule_dir_for_removal(ce->name, ce_namelen(ce));
Johannes Schindelin16da1342006-07-30 20:25:18 +0200219}
220
René Scharfeb56aa5b2016-09-13 19:37:06 +0200221static int check_updates(struct unpack_trees_options *o,
222 const struct checkout *state)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200223{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400224 unsigned cnt = 0, total = 0;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400225 struct progress *progress = NULL;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800226 struct index_state *index = &o->result;
Daniel Barkalow33ecf7e2008-02-07 11:39:59 -0500227 int i;
Junio C Hamanoc4758d32008-03-18 22:01:28 -0700228 int errs = 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200229
230 if (o->update && o->verbose_update) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800231 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700232 const struct cache_entry *ce = index->cache[cnt];
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700233 if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
Johannes Schindelin16da1342006-07-30 20:25:18 +0200234 total++;
235 }
236
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700237 progress = start_progress_delay(_("Checking out files"),
Linus Torvaldse8548642008-02-23 13:36:08 -0800238 total, 50, 1);
Johannes Schindelin16da1342006-07-30 20:25:18 +0200239 cnt = 0;
240 }
241
Junio C Hamano66985e62009-04-18 00:18:01 +0200242 if (o->update)
243 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
Linus Torvalds34110cd2008-03-06 18:12:28 -0800244 for (i = 0; i < index->cache_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700245 const struct cache_entry *ce = index->cache[i];
Johannes Schindelin16da1342006-07-30 20:25:18 +0200246
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700247 if (ce->ce_flags & CE_WT_REMOVE) {
248 display_progress(progress, ++cnt);
Jens Lehmann2c9078d2011-05-25 22:07:51 +0200249 if (o->update && !o->dry_run)
Nguyễn Thái Ngọc Duye663db22009-08-20 20:47:06 +0700250 unlink_entry(ce);
251 continue;
252 }
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700253 }
Kjetil Barvik36419c82009-02-18 23:18:03 +0100254 remove_marked_cache_entries(&o->result);
Kjetil Barvik78478922009-02-09 21:54:07 +0100255 remove_scheduled_dirs();
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700256
257 for (i = 0; i < index->cache_nr; i++) {
258 struct cache_entry *ce = index->cache[i];
259
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800260 if (ce->ce_flags & CE_UPDATE) {
David Turner7d782412015-07-17 17:19:27 -0400261 if (ce->ce_flags & CE_WT_REMOVE)
262 die("BUG: both update and delete flags are set on %s",
263 ce->name);
Linus Torvalds1fa6ead2008-03-22 09:48:41 -0700264 display_progress(progress, ++cnt);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800265 ce->ce_flags &= ~CE_UPDATE;
Jens Lehmann2c9078d2011-05-25 22:07:51 +0200266 if (o->update && !o->dry_run) {
René Scharfeb56aa5b2016-09-13 19:37:06 +0200267 errs |= checkout_entry(ce, state, NULL);
Junio C Hamano16a4c612007-05-10 23:44:53 -0700268 }
Johannes Schindelin16da1342006-07-30 20:25:18 +0200269 }
270 }
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400271 stop_progress(&progress);
Junio C Hamano66985e62009-04-18 00:18:01 +0200272 if (o->update)
273 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
Junio C Hamanoc4758d32008-03-18 22:01:28 -0700274 return errs != 0;
Johannes Schindelin16da1342006-07-30 20:25:18 +0200275}
276
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200277static int verify_uptodate_sparse(const struct cache_entry *ce,
278 struct unpack_trees_options *o);
279static int verify_absent_sparse(const struct cache_entry *ce,
280 enum unpack_trees_error_types,
281 struct unpack_trees_options *o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700282
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700283static int apply_sparse_checkout(struct index_state *istate,
284 struct cache_entry *ce,
285 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700286{
287 int was_skip_worktree = ce_skip_worktree(ce);
288
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700289 if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700290 ce->ce_flags |= CE_SKIP_WORKTREE;
291 else
292 ce->ce_flags &= ~CE_SKIP_WORKTREE;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700293 if (was_skip_worktree != ce_skip_worktree(ce)) {
294 ce->ce_flags |= CE_UPDATE_IN_BASE;
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +0700295 istate->cache_changed |= CE_ENTRY_CHANGED;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700296 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700297
298 /*
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700299 * if (!was_skip_worktree && !ce_skip_worktree()) {
300 * This is perfectly normal. Move on;
301 * }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700302 */
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700303
304 /*
305 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
306 * area as a result of ce_skip_worktree() shortcuts in
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700307 * verify_absent() and verify_uptodate().
308 * Make sure they don't modify worktree if they are already
309 * outside checkout area
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +0700310 */
Nguyễn Thái Ngọc Duy700e66d2010-07-31 13:14:27 +0700311 if (was_skip_worktree && ce_skip_worktree(ce)) {
312 ce->ce_flags &= ~CE_UPDATE;
313
314 /*
315 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
316 * on to get that file removed from both index and worktree.
317 * If that file is already outside worktree area, don't
318 * bother remove it.
319 */
320 if (ce->ce_flags & CE_REMOVE)
321 ce->ce_flags &= ~CE_WT_REMOVE;
322 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700323
324 if (!was_skip_worktree && ce_skip_worktree(ce)) {
325 /*
326 * If CE_UPDATE is set, verify_uptodate() must be called already
327 * also stat info may have lost after merged_entry() so calling
328 * verify_uptodate() again may fail
329 */
330 if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
331 return -1;
332 ce->ce_flags |= CE_WT_REMOVE;
David Turner7d782412015-07-17 17:19:27 -0400333 ce->ce_flags &= ~CE_UPDATE;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700334 }
335 if (was_skip_worktree && !ce_skip_worktree(ce)) {
Matthieu Moy08402b02010-08-11 10:38:06 +0200336 if (verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +0700337 return -1;
338 ce->ce_flags |= CE_UPDATE;
339 }
340 return 0;
341}
342
René Scharfe5828e832013-06-02 17:46:56 +0200343static inline int call_unpack_fn(const struct cache_entry * const *src,
344 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800345{
Linus Torvalds34110cd2008-03-06 18:12:28 -0800346 int ret = o->fn(src, o);
347 if (ret > 0)
Linus Torvalds01904572008-03-05 20:15:44 -0800348 ret = 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800349 return ret;
350}
351
Junio C Hamanoda165f42010-01-07 14:59:54 -0800352static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
353{
354 ce->ce_flags |= CE_UNPACKED;
355
356 if (o->cache_bottom < o->src_index->cache_nr &&
357 o->src_index->cache[o->cache_bottom] == ce) {
358 int bottom = o->cache_bottom;
359 while (bottom < o->src_index->cache_nr &&
360 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
361 bottom++;
362 o->cache_bottom = bottom;
363 }
364}
365
366static void mark_all_ce_unused(struct index_state *index)
367{
368 int i;
369 for (i = 0; i < index->cache_nr; i++)
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +0700370 index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
Junio C Hamanoda165f42010-01-07 14:59:54 -0800371}
372
René Scharfeeb9ae4b2013-06-02 17:46:55 +0200373static int locate_in_src_index(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800374 struct unpack_trees_options *o)
375{
376 struct index_state *index = o->src_index;
377 int len = ce_namelen(ce);
378 int pos = index_name_pos(index, ce->name, len);
379 if (pos < 0)
380 pos = -1 - pos;
381 return pos;
382}
383
384/*
385 * We call unpack_index_entry() with an unmerged cache entry
386 * only in diff-index, and it wants a single callback. Skip
387 * the other unmerged entry with the same name.
388 */
389static void mark_ce_used_same_name(struct cache_entry *ce,
390 struct unpack_trees_options *o)
391{
392 struct index_state *index = o->src_index;
393 int len = ce_namelen(ce);
394 int pos;
395
396 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
397 struct cache_entry *next = index->cache[pos];
398 if (len != ce_namelen(next) ||
399 memcmp(ce->name, next->name, len))
400 break;
401 mark_ce_used(next, o);
402 }
403}
404
405static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
406{
407 const struct index_state *index = o->src_index;
408 int pos = o->cache_bottom;
409
410 while (pos < index->cache_nr) {
411 struct cache_entry *ce = index->cache[pos];
412 if (!(ce->ce_flags & CE_UNPACKED))
413 return ce;
414 pos++;
415 }
416 return NULL;
417}
418
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700419static void add_same_unmerged(const struct cache_entry *ce,
Junio C Hamanoda165f42010-01-07 14:59:54 -0800420 struct unpack_trees_options *o)
421{
422 struct index_state *index = o->src_index;
423 int len = ce_namelen(ce);
424 int pos = index_name_pos(index, ce->name, len);
425
426 if (0 <= pos)
427 die("programming error in a caller of mark_ce_used_same_name");
428 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
429 struct cache_entry *next = index->cache[pos];
430 if (len != ce_namelen(next) ||
431 memcmp(ce->name, next->name, len))
432 break;
433 add_entry(o, next, 0, 0);
434 mark_ce_used(next, o);
435 }
436}
437
438static int unpack_index_entry(struct cache_entry *ce,
439 struct unpack_trees_options *o)
Linus Torvalds01904572008-03-05 20:15:44 -0800440{
René Scharfe5828e832013-06-02 17:46:56 +0200441 const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Junio C Hamanoda165f42010-01-07 14:59:54 -0800442 int ret;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800443
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000444 src[0] = ce;
445
Junio C Hamanoda165f42010-01-07 14:59:54 -0800446 mark_ce_used(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -0800447 if (ce_stage(ce)) {
448 if (o->skip_unmerged) {
Linus Torvalds34110cd2008-03-06 18:12:28 -0800449 add_entry(o, ce, 0, 0);
450 return 0;
Linus Torvalds01904572008-03-05 20:15:44 -0800451 }
Linus Torvalds01904572008-03-05 20:15:44 -0800452 }
Junio C Hamanoda165f42010-01-07 14:59:54 -0800453 ret = call_unpack_fn(src, o);
454 if (ce_stage(ce))
455 mark_ce_used_same_name(ce, o);
456 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800457}
458
Junio C Hamano730f7282009-09-20 00:03:39 -0700459static int find_cache_pos(struct traverse_info *, const struct name_entry *);
460
461static void restore_cache_bottom(struct traverse_info *info, int bottom)
462{
463 struct unpack_trees_options *o = info->data;
464
465 if (o->diff_index_cached)
466 return;
467 o->cache_bottom = bottom;
468}
469
470static int switch_cache_bottom(struct traverse_info *info)
471{
472 struct unpack_trees_options *o = info->data;
473 int ret, pos;
474
475 if (o->diff_index_cached)
476 return 0;
477 ret = o->cache_bottom;
478 pos = find_cache_pos(info->prev, &info->name);
479
480 if (pos < -1)
481 o->cache_bottom = -2 - pos;
482 else if (pos < 0)
483 o->cache_bottom = o->src_index->cache_nr;
484 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800485}
486
Junio C Hamano84563a62010-12-22 09:09:55 -0800487static int traverse_trees_recursive(int n, unsigned long dirmask,
488 unsigned long df_conflicts,
489 struct name_entry *names,
490 struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -0800491{
Junio C Hamano730f7282009-09-20 00:03:39 -0700492 int i, ret, bottom;
Junio C Hamanoca885a42008-03-13 22:07:18 -0700493 struct tree_desc t[MAX_UNPACK_TREES];
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500494 void *buf[MAX_UNPACK_TREES];
Linus Torvalds01904572008-03-05 20:15:44 -0800495 struct traverse_info newinfo;
496 struct name_entry *p;
497
498 p = names;
499 while (!p->mode)
500 p++;
501
502 newinfo = *info;
503 newinfo.prev = info;
Junio C Hamano40e37252011-08-29 12:31:06 -0700504 newinfo.pathspec = info->pathspec;
Linus Torvalds01904572008-03-05 20:15:44 -0800505 newinfo.name = *p;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100506 newinfo.pathlen += tree_entry_len(p) + 1;
René Scharfe603d2492013-06-16 01:44:43 +0200507 newinfo.df_conflicts |= df_conflicts;
Linus Torvalds01904572008-03-05 20:15:44 -0800508
509 for (i = 0; i < n; i++, dirmask >>= 1) {
510 const unsigned char *sha1 = NULL;
511 if (dirmask & 1)
brian m. carlson7d924c92016-04-17 23:10:39 +0000512 sha1 = names[i].oid->hash;
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500513 buf[i] = fill_tree_descriptor(t+i, sha1);
Linus Torvalds01904572008-03-05 20:15:44 -0800514 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700515
516 bottom = switch_cache_bottom(&newinfo);
517 ret = traverse_trees(n, t, &newinfo);
518 restore_cache_bottom(&newinfo, bottom);
Jonathan Nieder1ce584b2010-08-09 22:33:44 -0500519
520 for (i = 0; i < n; i++)
521 free(buf[i]);
522
Junio C Hamano730f7282009-09-20 00:03:39 -0700523 return ret;
Linus Torvalds01904572008-03-05 20:15:44 -0800524}
525
526/*
527 * Compare the traverse-path to the cache entry without actually
528 * having to generate the textual representation of the traverse
529 * path.
530 *
531 * NOTE! This *only* compares up to the size of the traverse path
532 * itself - the caller needs to do the final check for the cache
533 * entry having more data at the end!
534 */
David Turnerd9c2bd52015-12-21 17:34:20 -0500535static int do_compare_entry_piecewise(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
Linus Torvalds01904572008-03-05 20:15:44 -0800536{
537 int len, pathlen, ce_len;
538 const char *ce_name;
539
540 if (info->prev) {
David Turnerd9c2bd52015-12-21 17:34:20 -0500541 int cmp = do_compare_entry_piecewise(ce, info->prev,
542 &info->name);
Linus Torvalds01904572008-03-05 20:15:44 -0800543 if (cmp)
544 return cmp;
545 }
546 pathlen = info->pathlen;
547 ce_len = ce_namelen(ce);
548
549 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
550 if (ce_len < pathlen)
551 return -1;
552
553 ce_len -= pathlen;
554 ce_name = ce->name + pathlen;
555
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100556 len = tree_entry_len(n);
Linus Torvalds01904572008-03-05 20:15:44 -0800557 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
558}
559
David Turnerd9c2bd52015-12-21 17:34:20 -0500560static int do_compare_entry(const struct cache_entry *ce,
561 const struct traverse_info *info,
562 const struct name_entry *n)
563{
564 int len, pathlen, ce_len;
565 const char *ce_name;
566 int cmp;
567
568 /*
569 * If we have not precomputed the traverse path, it is quicker
570 * to avoid doing so. But if we have precomputed it,
571 * it is quicker to use the precomputed version.
572 */
573 if (!info->traverse_path)
574 return do_compare_entry_piecewise(ce, info, n);
575
576 cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
577 if (cmp)
578 return cmp;
579
580 pathlen = info->pathlen;
581 ce_len = ce_namelen(ce);
582
583 if (ce_len < pathlen)
584 return -1;
585
586 ce_len -= pathlen;
587 ce_name = ce->name + pathlen;
588
589 len = tree_entry_len(n);
590 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
591}
592
Linus Torvalds01904572008-03-05 20:15:44 -0800593static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
594{
595 int cmp = do_compare_entry(ce, info, n);
596 if (cmp)
597 return cmp;
598
599 /*
600 * Even if the beginning compared identically, the ce should
601 * compare as bigger than a directory leading up to it!
602 */
603 return ce_namelen(ce) > traverse_path_len(info, n);
604}
605
Junio C Hamanoda165f42010-01-07 14:59:54 -0800606static int ce_in_traverse_path(const struct cache_entry *ce,
607 const struct traverse_info *info)
608{
609 if (!info->prev)
610 return 1;
611 if (do_compare_entry(ce, info->prev, &info->name))
612 return 0;
613 /*
614 * If ce (blob) is the same name as the path (which is a tree
615 * we will be descending into), it won't be inside it.
616 */
617 return (info->pathlen < ce_namelen(ce));
618}
619
Linus Torvalds01904572008-03-05 20:15:44 -0800620static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
621{
622 int len = traverse_path_len(info, n);
623 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
624
625 ce->ce_mode = create_ce_mode(n->mode);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200626 ce->ce_flags = create_ce_flags(stage);
627 ce->ce_namelen = len;
brian m. carlson99d1a982016-09-05 20:07:52 +0000628 oidcpy(&ce->oid, n->oid);
Linus Torvalds01904572008-03-05 20:15:44 -0800629 make_traverse_path(ce->name, info, n);
630
631 return ce;
632}
633
René Scharfec7cddc12009-01-31 15:39:10 +0100634static int unpack_nondirectories(int n, unsigned long mask,
635 unsigned long dirmask,
636 struct cache_entry **src,
637 const struct name_entry *names,
638 const struct traverse_info *info)
Linus Torvalds01904572008-03-05 20:15:44 -0800639{
640 int i;
641 struct unpack_trees_options *o = info->data;
René Scharfe603d2492013-06-16 01:44:43 +0200642 unsigned long conflicts = info->df_conflicts | dirmask;
Linus Torvalds01904572008-03-05 20:15:44 -0800643
644 /* Do we have *only* directories? Nothing to do */
645 if (mask == dirmask && !src[0])
646 return 0;
647
Linus Torvalds01904572008-03-05 20:15:44 -0800648 /*
649 * Ok, we've filled in up to any potential index entry in src[0],
650 * now do the rest.
651 */
652 for (i = 0; i < n; i++) {
653 int stage;
654 unsigned int bit = 1ul << i;
655 if (conflicts & bit) {
656 src[i + o->merge] = o->df_conflict_entry;
657 continue;
658 }
659 if (!(mask & bit))
660 continue;
661 if (!o->merge)
662 stage = 0;
663 else if (i + 1 < o->head_idx)
664 stage = 1;
665 else if (i + 1 > o->head_idx)
666 stage = 3;
667 else
668 stage = 2;
669 src[i + o->merge] = create_ce_entry(info, names + i, stage);
670 }
671
René Scharfe5d80ef52013-06-02 17:46:57 +0200672 if (o->merge) {
673 int rc = call_unpack_fn((const struct cache_entry * const *)src,
674 o);
675 for (i = 0; i < n; i++) {
676 struct cache_entry *ce = src[i + o->merge];
677 if (ce != o->df_conflict_entry)
678 free(ce);
679 }
680 return rc;
681 }
Linus Torvalds01904572008-03-05 20:15:44 -0800682
Linus Torvalds01904572008-03-05 20:15:44 -0800683 for (i = 0; i < n; i++)
Junio C Hamanoaab3b9a2009-03-12 00:02:12 -0700684 if (src[i] && src[i] != o->df_conflict_entry)
Jeff King46169182014-11-24 13:36:51 -0500685 if (do_add_entry(o, src[i], 0, 0))
686 return -1;
687
Linus Torvalds01904572008-03-05 20:15:44 -0800688 return 0;
689}
690
Junio C Hamano353c5ee2009-09-19 16:36:45 -0700691static int unpack_failed(struct unpack_trees_options *o, const char *message)
692{
693 discard_index(&o->result);
Junio C Hamanob4194822011-05-31 10:06:44 -0700694 if (!o->gently && !o->exiting_early) {
Junio C Hamano353c5ee2009-09-19 16:36:45 -0700695 if (message)
696 return error("%s", message);
697 return -1;
698 }
699 return -1;
700}
701
Junio C Hamano730f7282009-09-20 00:03:39 -0700702/*
703 * The tree traversal is looking at name p. If we have a matching entry,
704 * return it. If name p is a directory in the index, do not return
705 * anything, as we will want to match it when the traversal descends into
706 * the directory.
707 */
708static int find_cache_pos(struct traverse_info *info,
709 const struct name_entry *p)
710{
711 int pos;
712 struct unpack_trees_options *o = info->data;
713 struct index_state *index = o->src_index;
714 int pfxlen = info->pathlen;
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100715 int p_len = tree_entry_len(p);
Junio C Hamano730f7282009-09-20 00:03:39 -0700716
717 for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700718 const struct cache_entry *ce = index->cache[pos];
Junio C Hamano730f7282009-09-20 00:03:39 -0700719 const char *ce_name, *ce_slash;
720 int cmp, ce_len;
721
Brian Downinge53e6b42010-06-10 21:59:07 -0500722 if (ce->ce_flags & CE_UNPACKED) {
723 /*
724 * cache_bottom entry is already unpacked, so
725 * we can never match it; don't check it
726 * again.
727 */
728 if (pos == o->cache_bottom)
729 ++o->cache_bottom;
Junio C Hamano730f7282009-09-20 00:03:39 -0700730 continue;
Brian Downinge53e6b42010-06-10 21:59:07 -0500731 }
David Turnera6720952016-01-22 14:58:43 -0500732 if (!ce_in_traverse_path(ce, info)) {
733 /*
734 * Check if we can skip future cache checks
735 * (because we're already past all possible
736 * entries in the traverse path).
737 */
738 if (info->traverse_path) {
739 if (strncmp(ce->name, info->traverse_path,
740 info->pathlen) > 0)
741 break;
742 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700743 continue;
David Turnera6720952016-01-22 14:58:43 -0500744 }
Junio C Hamano730f7282009-09-20 00:03:39 -0700745 ce_name = ce->name + pfxlen;
746 ce_slash = strchr(ce_name, '/');
747 if (ce_slash)
748 ce_len = ce_slash - ce_name;
749 else
750 ce_len = ce_namelen(ce) - pfxlen;
751 cmp = name_compare(p->path, p_len, ce_name, ce_len);
752 /*
753 * Exact match; if we have a directory we need to
754 * delay returning it.
755 */
756 if (!cmp)
757 return ce_slash ? -2 - pos : pos;
758 if (0 < cmp)
759 continue; /* keep looking */
760 /*
761 * ce_name sorts after p->path; could it be that we
762 * have files under p->path directory in the index?
763 * E.g. ce_name == "t-i", and p->path == "t"; we may
764 * have "t/a" in the index.
765 */
766 if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&
767 ce_name[p_len] < '/')
768 continue; /* keep looking */
769 break;
770 }
771 return -1;
772}
773
774static struct cache_entry *find_cache_entry(struct traverse_info *info,
775 const struct name_entry *p)
776{
777 int pos = find_cache_pos(info, p);
778 struct unpack_trees_options *o = info->data;
779
780 if (0 <= pos)
781 return o->src_index->cache[pos];
782 else
783 return NULL;
784}
785
Junio C Hamanoba655da2009-09-14 02:22:00 -0700786static void debug_path(struct traverse_info *info)
787{
788 if (info->prev) {
789 debug_path(info->prev);
790 if (*info->prev->name.path)
791 putchar('/');
792 }
793 printf("%s", info->name.path);
794}
795
796static void debug_name_entry(int i, struct name_entry *n)
797{
798 printf("ent#%d %06o %s\n", i,
799 n->path ? n->mode : 0,
800 n->path ? n->path : "(missing)");
801}
802
803static void debug_unpack_callback(int n,
804 unsigned long mask,
805 unsigned long dirmask,
806 struct name_entry *names,
807 struct traverse_info *info)
808{
809 int i;
810 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
811 mask, dirmask, n);
812 debug_path(info);
813 putchar('\n');
814 for (i = 0; i < n; i++)
815 debug_name_entry(i, names + i);
816}
817
Linus Torvalds01904572008-03-05 20:15:44 -0800818static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
819{
René Scharfec7cddc12009-01-31 15:39:10 +0100820 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
Linus Torvalds01904572008-03-05 20:15:44 -0800821 struct unpack_trees_options *o = info->data;
Linus Torvalds01904572008-03-05 20:15:44 -0800822 const struct name_entry *p = names;
823
824 /* Find first entry with a real name (we could use "mask" too) */
825 while (!p->mode)
826 p++;
827
Junio C Hamanoba655da2009-09-14 02:22:00 -0700828 if (o->debug_unpack)
829 debug_unpack_callback(n, mask, dirmask, names, info);
830
Linus Torvalds01904572008-03-05 20:15:44 -0800831 /* Are we supposed to look at the index too? */
832 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -0800833 while (1) {
Junio C Hamanoda165f42010-01-07 14:59:54 -0800834 int cmp;
Junio C Hamano730f7282009-09-20 00:03:39 -0700835 struct cache_entry *ce;
836
837 if (o->diff_index_cached)
838 ce = next_cache_entry(o);
839 else
840 ce = find_cache_entry(info, p);
841
Junio C Hamanoda165f42010-01-07 14:59:54 -0800842 if (!ce)
843 break;
844 cmp = compare_entry(ce, info, p);
Linus Torvalds01904572008-03-05 20:15:44 -0800845 if (cmp < 0) {
846 if (unpack_index_entry(ce, o) < 0)
Junio C Hamano353c5ee2009-09-19 16:36:45 -0700847 return unpack_failed(o, NULL);
Linus Torvalds01904572008-03-05 20:15:44 -0800848 continue;
849 }
850 if (!cmp) {
851 if (ce_stage(ce)) {
852 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -0800853 * If we skip unmerged index
854 * entries, we'll skip this
855 * entry *and* the tree
856 * entries associated with it!
Linus Torvalds01904572008-03-05 20:15:44 -0800857 */
Linus Torvalds34110cd2008-03-06 18:12:28 -0800858 if (o->skip_unmerged) {
Junio C Hamanoda165f42010-01-07 14:59:54 -0800859 add_same_unmerged(ce, o);
Linus Torvalds01904572008-03-05 20:15:44 -0800860 return mask;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800861 }
Linus Torvalds01904572008-03-05 20:15:44 -0800862 }
863 src[0] = ce;
Linus Torvalds01904572008-03-05 20:15:44 -0800864 }
865 break;
866 }
867 }
868
Linus Torvalds34110cd2008-03-06 18:12:28 -0800869 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
Linus Torvalds01904572008-03-05 20:15:44 -0800870 return -1;
871
René Scharfe97e59542012-04-10 20:55:58 +0200872 if (o->merge && src[0]) {
Junio C Hamanoda165f42010-01-07 14:59:54 -0800873 if (ce_stage(src[0]))
874 mark_ce_used_same_name(src[0], o);
875 else
876 mark_ce_used(src[0], o);
877 }
878
Linus Torvalds01904572008-03-05 20:15:44 -0800879 /* Now handle any directories.. */
880 if (dirmask) {
Junio C Hamanob65982b2009-05-20 15:57:22 -0700881 /* special case: "diff-index --cached" looking at a tree */
882 if (o->diff_index_cached &&
883 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
884 int matches;
885 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
886 names, info);
887 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -0800888 * Everything under the name matches; skip the
889 * entire hierarchy. diff_index_cached codepath
890 * special cases D/F conflicts in such a way that
891 * it does not do any look-ahead, so this is safe.
Junio C Hamanob65982b2009-05-20 15:57:22 -0700892 */
893 if (matches) {
Junio C Hamanoda165f42010-01-07 14:59:54 -0800894 o->cache_bottom += matches;
Junio C Hamanob65982b2009-05-20 15:57:22 -0700895 return mask;
896 }
897 }
898
René Scharfe603d2492013-06-16 01:44:43 +0200899 if (traverse_trees_recursive(n, dirmask, mask & ~dirmask,
Junio C Hamano542c2642008-03-10 01:26:23 -0700900 names, info) < 0)
901 return -1;
Linus Torvalds01904572008-03-05 20:15:44 -0800902 return mask;
903 }
904
905 return mask;
906}
907
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700908static int clear_ce_flags_1(struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100909 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700910 int select_mask, int clear_mask,
911 struct exclude_list *el, int defval);
912
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700913/* Whole directory matching */
914static int clear_ce_flags_dir(struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100915 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700916 char *basename,
917 int select_mask, int clear_mask,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700918 struct exclude_list *el, int defval)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700919{
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700920 struct cache_entry **cache_end;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700921 int dtype = DT_DIR;
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100922 int ret = is_excluded_from_list(prefix->buf, prefix->len,
Adam Spiers07958052012-12-27 02:32:24 +0000923 basename, &dtype, el);
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100924 int rc;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700925
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100926 strbuf_addch(prefix, '/');
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700927
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700928 /* If undecided, use matching result of parent dir in defval */
929 if (ret < 0)
930 ret = defval;
931
932 for (cache_end = cache; cache_end != cache + nr; cache_end++) {
933 struct cache_entry *ce = *cache_end;
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100934 if (strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700935 break;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700936 }
937
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700938 /*
939 * TODO: check el, if there are no patterns that may conflict
940 * with ret (iow, we know in advance the incl/excl
941 * decision for the entire directory), clear flag here without
942 * calling clear_ce_flags_1(). That function will call
Adam Spiers07958052012-12-27 02:32:24 +0000943 * the expensive is_excluded_from_list() on every entry.
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700944 */
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100945 rc = clear_ce_flags_1(cache, cache_end - cache,
946 prefix,
947 select_mask, clear_mask,
948 el, ret);
949 strbuf_setlen(prefix, prefix->len - 1);
950 return rc;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700951}
952
953/*
954 * Traverse the index, find every entry that matches according to
955 * o->el. Do "ce_flags &= ~clear_mask" on those entries. Return the
956 * number of traversed entries.
957 *
958 * If select_mask is non-zero, only entries whose ce_flags has on of
959 * those bits enabled are traversed.
960 *
961 * cache : pointer to an index entry
962 * prefix_len : an offset to its path
963 *
964 * The current path ("prefix") including the trailing '/' is
965 * cache[0]->name[0..(prefix_len-1)]
966 * Top level path has prefix_len zero.
967 */
968static int clear_ce_flags_1(struct cache_entry **cache, int nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100969 struct strbuf *prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700970 int select_mask, int clear_mask,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700971 struct exclude_list *el, int defval)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700972{
973 struct cache_entry **cache_end = cache + nr;
974
975 /*
976 * Process all entries that have the given prefix and meet
977 * select_mask condition
978 */
979 while(cache != cache_end) {
980 struct cache_entry *ce = *cache;
981 const char *name, *slash;
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +0700982 int len, dtype, ret;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700983
984 if (select_mask && !(ce->ce_flags & select_mask)) {
985 cache++;
986 continue;
987 }
988
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100989 if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700990 break;
991
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100992 name = ce->name + prefix->len;
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +0700993 slash = strchr(name, '/');
994
995 /* If it's a directory, try whole directory match first */
996 if (slash) {
997 int processed;
998
999 len = slash - name;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001000 strbuf_add(prefix, name, len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001001
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001002 processed = clear_ce_flags_dir(cache, cache_end - cache,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001003 prefix,
1004 prefix->buf + prefix->len - len,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001005 select_mask, clear_mask,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001006 el, defval);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001007
1008 /* clear_c_f_dir eats a whole dir already? */
1009 if (processed) {
1010 cache += processed;
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001011 strbuf_setlen(prefix, prefix->len - len);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001012 continue;
1013 }
1014
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001015 strbuf_addch(prefix, '/');
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001016 cache += clear_ce_flags_1(cache, cache_end - cache,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001017 prefix,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001018 select_mask, clear_mask, el, defval);
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001019 strbuf_setlen(prefix, prefix->len - len - 1);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001020 continue;
1021 }
1022
1023 /* Non-directory */
1024 dtype = ce_to_dtype(ce);
Adam Spiers07958052012-12-27 02:32:24 +00001025 ret = is_excluded_from_list(ce->name, ce_namelen(ce),
1026 name, &dtype, el);
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001027 if (ret < 0)
1028 ret = defval;
1029 if (ret > 0)
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001030 ce->ce_flags &= ~clear_mask;
1031 cache++;
1032 }
1033 return nr - (cache_end - cache);
1034}
1035
1036static int clear_ce_flags(struct cache_entry **cache, int nr,
1037 int select_mask, int clear_mask,
1038 struct exclude_list *el)
1039{
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001040 static struct strbuf prefix = STRBUF_INIT;
1041
1042 strbuf_reset(&prefix);
1043
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001044 return clear_ce_flags_1(cache, nr,
Antoine Pelissefc2b6212013-12-14 12:31:16 +01001045 &prefix,
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001046 select_mask, clear_mask,
Nguyễn Thái Ngọc Duy28911092011-05-09 22:43:01 +07001047 el, 0);
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001048}
1049
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001050/*
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001051 * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1052 */
1053static void mark_new_skip_worktree(struct exclude_list *el,
1054 struct index_state *the_index,
1055 int select_flag, int skip_wt_flag)
1056{
1057 int i;
1058
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001059 /*
1060 * 1. Pretend the narrowest worktree: only unmerged entries
1061 * are checked out
1062 */
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001063 for (i = 0; i < the_index->cache_nr; i++) {
1064 struct cache_entry *ce = the_index->cache[i];
1065
1066 if (select_flag && !(ce->ce_flags & select_flag))
1067 continue;
1068
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001069 if (!ce_stage(ce))
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001070 ce->ce_flags |= skip_wt_flag;
1071 else
1072 ce->ce_flags &= ~skip_wt_flag;
1073 }
Nguyễn Thái Ngọc Duy90370262010-11-27 01:17:46 +07001074
1075 /*
1076 * 2. Widen worktree according to sparse-checkout file.
1077 * Matched entries will have skip_wt_flag cleared (i.e. "in")
1078 */
1079 clear_ce_flags(the_index->cache, the_index->cache_nr,
1080 select_flag, skip_wt_flag, el);
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001081}
1082
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001083static int verify_absent(const struct cache_entry *,
1084 enum unpack_trees_error_types,
1085 struct unpack_trees_options *);
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001086/*
1087 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1088 * 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 +07001089 *
1090 * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001091 */
Linus Torvalds933bf402007-08-09 22:21:29 -07001092int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
Johannes Schindelin16da1342006-07-30 20:25:18 +02001093{
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001094 int i, ret;
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001095 static struct cache_entry *dfc;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001096 struct exclude_list el;
René Scharfe68e3d622016-09-22 18:11:33 +02001097 struct checkout state = CHECKOUT_INIT;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001098
Junio C Hamanoca885a42008-03-13 22:07:18 -07001099 if (len > MAX_UNPACK_TREES)
1100 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
Johannes Schindelin16da1342006-07-30 20:25:18 +02001101 state.force = 1;
1102 state.quiet = 1;
1103 state.refresh_cache = 1;
Nguyễn Thái Ngọc Duyd4a20242014-06-13 19:19:34 +07001104 state.istate = &o->result;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001105
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001106 memset(&el, 0, sizeof(el));
1107 if (!core_apply_sparse_checkout || !o->update)
1108 o->skip_sparse_checkout = 1;
1109 if (!o->skip_sparse_checkout) {
Jeff Kingfcd12db2015-08-10 05:35:31 -04001110 char *sparse = git_pathdup("info/sparse-checkout");
1111 if (add_excludes_from_file_to_list(sparse, "", 0, &el, 0) < 0)
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001112 o->skip_sparse_checkout = 1;
1113 else
1114 o->el = &el;
Jeff Kingfcd12db2015-08-10 05:35:31 -04001115 free(sparse);
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001116 }
1117
Linus Torvalds34110cd2008-03-06 18:12:28 -08001118 memset(&o->result, 0, sizeof(o->result));
Junio C Hamano913e0e92008-08-23 12:57:30 -07001119 o->result.initialized = 1;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001120 o->result.timestamp.sec = o->src_index->timestamp.sec;
1121 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
Junio C Hamano9170c7a2012-04-27 15:58:13 -07001122 o->result.version = o->src_index->version;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001123 o->result.split_index = o->src_index->split_index;
1124 if (o->result.split_index)
1125 o->result.split_index->refcount++;
Nguyễn Thái Ngọc Duye93021b2014-06-13 19:19:35 +07001126 hashcpy(o->result.sha1, o->src_index->sha1);
Johannes Schindelin16da1342006-07-30 20:25:18 +02001127 o->merge_size = len;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001128 mark_all_ce_unused(o->src_index);
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001129
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001130 /*
1131 * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1132 */
1133 if (!o->skip_sparse_checkout)
1134 mark_new_skip_worktree(o->el, o->src_index, 0, CE_NEW_SKIP_WORKTREE);
1135
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001136 if (!dfc)
Jeff King13494ed2008-10-23 04:30:58 +00001137 dfc = xcalloc(1, cache_entry_size(0));
Junio C Hamano0fb1eaa2006-12-04 02:11:39 -08001138 o->df_conflict_entry = dfc;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001139
1140 if (len) {
Linus Torvalds01904572008-03-05 20:15:44 -08001141 const char *prefix = o->prefix ? o->prefix : "";
1142 struct traverse_info info;
Linus Torvalds933bf402007-08-09 22:21:29 -07001143
Linus Torvalds01904572008-03-05 20:15:44 -08001144 setup_traverse_info(&info, prefix);
1145 info.fn = unpack_callback;
1146 info.data = o;
Matthieu Moye6c111b2010-08-11 10:38:07 +02001147 info.show_all_errors = o->show_all_errors;
Junio C Hamano40e37252011-08-29 12:31:06 -07001148 info.pathspec = o->pathspec;
Linus Torvalds01904572008-03-05 20:15:44 -08001149
Junio C Hamanoda165f42010-01-07 14:59:54 -08001150 if (o->prefix) {
1151 /*
1152 * Unpack existing index entries that sort before the
1153 * prefix the tree is spliced into. Note that o->merge
1154 * is always true in this case.
1155 */
1156 while (1) {
1157 struct cache_entry *ce = next_cache_entry(o);
1158 if (!ce)
1159 break;
1160 if (ce_in_traverse_path(ce, &info))
1161 break;
1162 if (unpack_index_entry(ce, o) < 0)
1163 goto return_failed;
1164 }
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001165 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08001166
Linus Torvalds01904572008-03-05 20:15:44 -08001167 if (traverse_trees(len, t, &info) < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08001168 goto return_failed;
Linus Torvalds01904572008-03-05 20:15:44 -08001169 }
1170
1171 /* Any left-over entries in the index? */
1172 if (o->merge) {
Junio C Hamanoda165f42010-01-07 14:59:54 -08001173 while (1) {
1174 struct cache_entry *ce = next_cache_entry(o);
1175 if (!ce)
1176 break;
Linus Torvalds01904572008-03-05 20:15:44 -08001177 if (unpack_index_entry(ce, o) < 0)
Junio C Hamanoda165f42010-01-07 14:59:54 -08001178 goto return_failed;
Daniel Barkalow17e46422008-02-07 11:39:52 -05001179 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001180 }
Junio C Hamanoda165f42010-01-07 14:59:54 -08001181 mark_all_ce_unused(o->src_index);
Johannes Schindelin16da1342006-07-30 20:25:18 +02001182
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001183 if (o->trivial_merges_only && o->nontrivial_merge) {
1184 ret = unpack_failed(o, "Merge requires file-level merging");
1185 goto done;
1186 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001187
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001188 if (!o->skip_sparse_checkout) {
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07001189 int empty_worktree = 1;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001190
1191 /*
1192 * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
1193 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
1194 * so apply_sparse_checkout() won't attempt to remove it from worktree
1195 */
1196 mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1197
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10001198 ret = 0;
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001199 for (i = 0; i < o->result.cache_nr; i++) {
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001200 struct cache_entry *ce = o->result.cache[i];
1201
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001202 /*
1203 * Entries marked with CE_ADDED in merged_entry() do not have
1204 * verify_absent() check (the check is effectively disabled
1205 * because CE_NEW_SKIP_WORKTREE is set unconditionally).
1206 *
1207 * Do the real check now because we have had
1208 * correct CE_NEW_SKIP_WORKTREE
1209 */
1210 if (ce->ce_flags & CE_ADDED &&
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10001211 verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
1212 if (!o->show_all_errors)
1213 goto return_failed;
1214 ret = -1;
1215 }
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001216
Nguyễn Thái Ngọc Duya5c446f2014-06-13 19:19:30 +07001217 if (apply_sparse_checkout(&o->result, ce, o)) {
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10001218 if (!o->show_all_errors)
1219 goto return_failed;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001220 ret = -1;
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001221 }
Nguyễn Thái Ngọc Duyeec3fc02010-07-31 13:14:26 +07001222 if (!ce_skip_worktree(ce))
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07001223 empty_worktree = 0;
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07001224
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001225 }
Nguyễn Thái Ngọc Duy17d26a42011-09-22 21:24:22 +10001226 if (ret < 0)
1227 goto return_failed;
Nguyễn Thái Ngọc Duya7bc9062011-09-22 21:24:23 +10001228 /*
1229 * Sparse checkout is meant to narrow down checkout area
1230 * but it does not make sense to narrow down to empty working
1231 * tree. This is usually a mistake in sparse checkout rules.
1232 * Do not allow users to do that.
1233 */
Nguyễn Thái Ngọc Duy9e1afb12009-08-20 20:47:13 +07001234 if (o->result.cache_nr && empty_worktree) {
1235 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
1236 goto done;
1237 }
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001238 }
Johannes Schindelin16da1342006-07-30 20:25:18 +02001239
Linus Torvalds34110cd2008-03-06 18:12:28 -08001240 o->src_index = NULL;
René Scharfeb56aa5b2016-09-13 19:37:06 +02001241 ret = check_updates(o, &state) ? (-2) : 0;
Felipe Contrerase28f7642013-08-13 20:27:58 +02001242 if (o->dst_index) {
Brian Degenhardt52fca212015-07-28 15:30:40 -04001243 if (!ret) {
1244 if (!o->result.cache_tree)
1245 o->result.cache_tree = cache_tree();
1246 if (!cache_tree_fully_valid(o->result.cache_tree))
1247 cache_tree_update(&o->result,
1248 WRITE_TREE_SILENT |
1249 WRITE_TREE_REPAIR);
1250 }
Felipe Contrerase28f7642013-08-13 20:27:58 +02001251 discard_index(o->dst_index);
Linus Torvalds34110cd2008-03-06 18:12:28 -08001252 *o->dst_index = o->result;
Junio C Hamanoa16cc8b2014-11-17 12:12:41 -08001253 } else {
1254 discard_index(&o->result);
Felipe Contrerase28f7642013-08-13 20:27:58 +02001255 }
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +07001256
1257done:
Adam Spiersf6198812012-12-27 02:32:29 +00001258 clear_exclude_list(&el);
Junio C Hamano2e2b8872008-05-28 15:12:30 -07001259 return ret;
Junio C Hamanoda165f42010-01-07 14:59:54 -08001260
1261return_failed:
Matthieu Moye6c111b2010-08-11 10:38:07 +02001262 if (o->show_all_errors)
1263 display_error_msgs(o);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001264 mark_all_ce_unused(o->src_index);
Junio C Hamano026680f2010-01-24 17:35:58 -08001265 ret = unpack_failed(o, NULL);
Junio C Hamanob4194822011-05-31 10:06:44 -07001266 if (o->exiting_early)
1267 ret = 0;
Junio C Hamano026680f2010-01-24 17:35:58 -08001268 goto done;
Johannes Schindelin16da1342006-07-30 20:25:18 +02001269}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001270
1271/* Here come the merge functions */
1272
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001273static int reject_merge(const struct cache_entry *ce,
1274 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001275{
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07001276 return o->gently ? -1 :
1277 add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001278}
1279
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001280static int same(const struct cache_entry *a, const struct cache_entry *b)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001281{
1282 if (!!a != !!b)
1283 return 0;
1284 if (!a && !b)
1285 return 1;
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001286 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
1287 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001288 return a->ce_mode == b->ce_mode &&
brian m. carlson99d1a982016-09-05 20:07:52 +00001289 !oidcmp(&a->oid, &b->oid);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001290}
1291
1292
1293/*
1294 * When a CE gets turned into an unmerged entry, we
1295 * want it to be up-to-date
1296 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001297static int verify_uptodate_1(const struct cache_entry *ce,
1298 struct unpack_trees_options *o,
1299 enum unpack_trees_error_types error_type)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001300{
1301 struct stat st;
1302
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07001303 if (o->index_only)
1304 return 0;
1305
1306 /*
1307 * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
1308 * if this entry is truly up-to-date because this file may be
1309 * overwritten.
1310 */
1311 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
1312 ; /* keep checking */
1313 else if (o->reset || ce_uptodate(ce))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001314 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001315
1316 if (!lstat(ce->name, &st)) {
Nguyễn Thái Ngọc Duyd5b66292011-07-30 10:55:05 +07001317 int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
1318 unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001319 if (!changed)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001320 return 0;
Junio C Hamano936492d2007-08-03 22:13:09 -07001321 /*
1322 * NEEDSWORK: the current default policy is to allow
Jens Lehmanncb8ad282012-05-20 15:28:26 +02001323 * submodule to be out of sync wrt the superproject
Junio C Hamano936492d2007-08-03 22:13:09 -07001324 * index. This needs to be tightened later for
1325 * submodules that are marked to be automatically
1326 * checked out.
1327 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001328 if (S_ISGITLINK(ce->ce_mode))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001329 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001330 errno = 0;
1331 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001332 if (errno == ENOENT)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001333 return 0;
Daniel Barkalow17e46422008-02-07 11:39:52 -05001334 return o->gently ? -1 :
Matthieu Moye6c111b2010-08-11 10:38:07 +02001335 add_rejected_path(o, error_type, ce->name);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07001336}
1337
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001338static int verify_uptodate(const struct cache_entry *ce,
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07001339 struct unpack_trees_options *o)
1340{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001341 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07001342 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02001343 return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001344}
1345
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001346static int verify_uptodate_sparse(const struct cache_entry *ce,
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001347 struct unpack_trees_options *o)
1348{
Matthieu Moy08402b02010-08-11 10:38:06 +02001349 return verify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001350}
1351
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001352static void invalidate_ce_path(const struct cache_entry *ce,
1353 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001354{
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07001355 if (!ce)
1356 return;
1357 cache_tree_invalidate_path(o->src_index, ce->name);
1358 untracked_cache_invalidate_path(o->src_index, ce->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001359}
1360
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001361/*
1362 * Check that checking out ce->sha1 in subdir ce->name is not
1363 * going to overwrite any working files.
1364 *
1365 * Currently, git does not checkout subprojects during a superproject
1366 * checkout, so it is not going to overwrite anything.
1367 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001368static int verify_clean_submodule(const struct cache_entry *ce,
1369 enum unpack_trees_error_types error_type,
1370 struct unpack_trees_options *o)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001371{
1372 return 0;
1373}
1374
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001375static int verify_clean_subdirectory(const struct cache_entry *ce,
1376 enum unpack_trees_error_types error_type,
1377 struct unpack_trees_options *o)
Junio C Hamanoc8193532007-03-15 23:25:22 -07001378{
1379 /*
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001380 * we are about to extract "ce->name"; we would not want to lose
Junio C Hamanoc8193532007-03-15 23:25:22 -07001381 * anything in the existing directory there.
1382 */
1383 int namelen;
Clemens Buchacher7b9e3ce2009-01-01 21:54:33 +01001384 int i;
Junio C Hamanoc8193532007-03-15 23:25:22 -07001385 struct dir_struct d;
1386 char *pathbuf;
1387 int cnt = 0;
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001388 unsigned char sha1[20];
1389
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001390 if (S_ISGITLINK(ce->ce_mode) &&
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001391 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
1392 /* If we are not going to update the submodule, then
1393 * we don't care.
1394 */
brian m. carlson99d1a982016-09-05 20:07:52 +00001395 if (!hashcmp(sha1, ce->oid.hash))
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001396 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02001397 return verify_clean_submodule(ce, error_type, o);
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001398 }
Junio C Hamanoc8193532007-03-15 23:25:22 -07001399
1400 /*
1401 * First let's make sure we do not have a local modification
1402 * in that directory.
1403 */
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02001404 namelen = ce_namelen(ce);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001405 for (i = locate_in_src_index(ce, o);
1406 i < o->src_index->cache_nr;
1407 i++) {
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01001408 struct cache_entry *ce2 = o->src_index->cache[i];
1409 int len = ce_namelen(ce2);
Junio C Hamanoc8193532007-03-15 23:25:22 -07001410 if (len < namelen ||
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01001411 strncmp(ce->name, ce2->name, namelen) ||
1412 ce2->name[namelen] != '/')
Junio C Hamanoc8193532007-03-15 23:25:22 -07001413 break;
1414 /*
Junio C Hamanoda165f42010-01-07 14:59:54 -08001415 * ce2->name is an entry in the subdirectory to be
1416 * removed.
Junio C Hamanoc8193532007-03-15 23:25:22 -07001417 */
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01001418 if (!ce_stage(ce2)) {
1419 if (verify_uptodate(ce2, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001420 return -1;
Clemens Buchacher837e5fe2009-01-01 21:54:32 +01001421 add_entry(o, ce2, CE_REMOVE, 0);
Junio C Hamanoda165f42010-01-07 14:59:54 -08001422 mark_ce_used(ce2, o);
Junio C Hamanoc8193532007-03-15 23:25:22 -07001423 }
1424 cnt++;
1425 }
1426
1427 /*
1428 * Then we need to make sure that we do not lose a locally
1429 * present file that is not ignored.
1430 */
Jeff King75faa452015-09-24 17:07:03 -04001431 pathbuf = xstrfmt("%.*s/", namelen, ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -07001432
1433 memset(&d, 0, sizeof(d));
1434 if (o->dir)
1435 d.exclude_per_dir = o->dir->exclude_per_dir;
Linus Torvaldsdba2e202009-07-08 19:24:39 -07001436 i = read_directory(&d, pathbuf, namelen+1, NULL);
Junio C Hamanoc8193532007-03-15 23:25:22 -07001437 if (i)
Daniel Barkalow17e46422008-02-07 11:39:52 -05001438 return o->gently ? -1 :
Matthieu Moye6c111b2010-08-11 10:38:07 +02001439 add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
Junio C Hamanoc8193532007-03-15 23:25:22 -07001440 free(pathbuf);
1441 return cnt;
1442}
1443
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001444/*
Linus Torvalds32260ad2008-03-22 09:35:59 -07001445 * This gets called when there was no index entry for the tree entry 'dst',
1446 * but we found a file in the working tree that 'lstat()' said was fine,
1447 * and we're on a case-insensitive filesystem.
1448 *
1449 * See if we can find a case-insensitive match in the index that also
1450 * matches the stat information, and assume it's that other file!
1451 */
Clemens Buchachera9307f52010-10-09 15:52:58 +02001452static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
Linus Torvalds32260ad2008-03-22 09:35:59 -07001453{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001454 const struct cache_entry *src;
Linus Torvalds32260ad2008-03-22 09:35:59 -07001455
Eric Sunshineebbd7432013-09-17 03:06:15 -04001456 src = index_file_exists(o->src_index, name, len, 1);
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001457 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 -07001458}
1459
Clemens Buchachera9307f52010-10-09 15:52:58 +02001460static int check_ok_to_remove(const char *name, int len, int dtype,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001461 const struct cache_entry *ce, struct stat *st,
Clemens Buchachera9307f52010-10-09 15:52:58 +02001462 enum unpack_trees_error_types error_type,
1463 struct unpack_trees_options *o)
1464{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07001465 const struct cache_entry *result;
Clemens Buchachera9307f52010-10-09 15:52:58 +02001466
1467 /*
1468 * It may be that the 'lstat()' succeeded even though
1469 * target 'ce' was absent, because there is an old
1470 * entry that is different only in case..
1471 *
1472 * Ignore that lstat() if it matches.
1473 */
1474 if (ignore_case && icase_exists(o, name, len, st))
1475 return 0;
1476
Junio C Hamano589570d2012-06-05 22:21:42 -07001477 if (o->dir &&
Karsten Bleesb07bc8c2013-04-15 21:12:57 +02001478 is_excluded(o->dir, name, &dtype))
Clemens Buchachera9307f52010-10-09 15:52:58 +02001479 /*
1480 * ce->name is explicitly excluded, so it is Ok to
1481 * overwrite it.
1482 */
1483 return 0;
1484 if (S_ISDIR(st->st_mode)) {
1485 /*
1486 * We are checking out path "foo" and
1487 * found "foo/." in the working tree.
1488 * This is tricky -- if we have modified
1489 * files that are in "foo/" we would lose
1490 * them.
1491 */
1492 if (verify_clean_subdirectory(ce, error_type, o) < 0)
1493 return -1;
1494 return 0;
1495 }
1496
1497 /*
1498 * The previous round may already have decided to
1499 * delete this path, which is in a subdirectory that
1500 * is being replaced with a blob.
1501 */
Eric Sunshineebbd7432013-09-17 03:06:15 -04001502 result = index_file_exists(&o->result, name, len, 0);
Clemens Buchachera9307f52010-10-09 15:52:58 +02001503 if (result) {
1504 if (result->ce_flags & CE_REMOVE)
1505 return 0;
1506 }
1507
1508 return o->gently ? -1 :
1509 add_rejected_path(o, error_type, name);
1510}
1511
Linus Torvalds32260ad2008-03-22 09:35:59 -07001512/*
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001513 * We do not want to remove or overwrite a working tree file that
Junio C Hamanof8a9d422006-12-04 16:00:46 -08001514 * is not tracked, unless it is ignored.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001515 */
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001516static int verify_absent_1(const struct cache_entry *ce,
1517 enum unpack_trees_error_types error_type,
1518 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001519{
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02001520 int len;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001521 struct stat st;
1522
1523 if (o->index_only || o->reset || !o->update)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001524 return 0;
Junio C Hamanoc8193532007-03-15 23:25:22 -07001525
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02001526 len = check_leading_path(ce->name, ce_namelen(ce));
1527 if (!len)
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001528 return 0;
Clemens Buchacherf66caaf2010-10-09 15:53:00 +02001529 else if (len > 0) {
Jeff Kingf514ef92015-08-19 14:12:37 -04001530 char *path;
1531 int ret;
Junio C Hamanoec0603e2007-07-12 01:04:16 -07001532
Jeff Kingf514ef92015-08-19 14:12:37 -04001533 path = xmemdupz(ce->name, len);
1534 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07001535 ret = error_errno("cannot stat '%s'", path);
Jeff Kingf514ef92015-08-19 14:12:37 -04001536 else
1537 ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
1538 &st, error_type, o);
1539 free(path);
1540 return ret;
Jonathan Nieder92fda792011-01-12 20:26:36 -06001541 } else if (lstat(ce->name, &st)) {
1542 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy43c728e2016-05-08 16:47:58 +07001543 return error_errno("cannot stat '%s'", ce->name);
Jonathan Nieder92fda792011-01-12 20:26:36 -06001544 return 0;
1545 } else {
Clemens Buchachera9307f52010-10-09 15:52:58 +02001546 return check_ok_to_remove(ce->name, ce_namelen(ce),
Jonathan Nieder92fda792011-01-12 20:26:36 -06001547 ce_to_dtype(ce), ce, &st,
1548 error_type, o);
1549 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001550}
Clemens Buchachera9307f52010-10-09 15:52:58 +02001551
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001552static int verify_absent(const struct cache_entry *ce,
Matthieu Moy08402b02010-08-11 10:38:06 +02001553 enum unpack_trees_error_types error_type,
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07001554 struct unpack_trees_options *o)
1555{
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001556 if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
Nguyễn Thái Ngọc Duyf1f523e2009-08-20 20:47:10 +07001557 return 0;
Matthieu Moy08402b02010-08-11 10:38:06 +02001558 return verify_absent_1(ce, error_type, o);
Nguyễn Thái Ngọc Duy35a5aa72009-08-20 20:47:07 +07001559}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001560
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001561static int verify_absent_sparse(const struct cache_entry *ce,
1562 enum unpack_trees_error_types error_type,
1563 struct unpack_trees_options *o)
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001564{
Matthieu Moy08402b02010-08-11 10:38:06 +02001565 enum unpack_trees_error_types orphaned_error = error_type;
1566 if (orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)
1567 orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;
1568
1569 return verify_absent_1(ce, orphaned_error, o);
Nguyễn Thái Ngọc Duye800ec92009-08-20 20:47:09 +07001570}
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001571
René Scharfef2fa3542013-06-02 17:46:54 +02001572static int merged_entry(const struct cache_entry *ce,
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001573 const struct cache_entry *old,
René Scharfef2fa3542013-06-02 17:46:54 +02001574 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001575{
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07001576 int update = CE_UPDATE;
René Scharfef2fa3542013-06-02 17:46:54 +02001577 struct cache_entry *merge = dup_entry(ce);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07001578
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001579 if (!old) {
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001580 /*
1581 * New index entries. In sparse checkout, the following
1582 * verify_absent() will be delayed until after
1583 * traverse_trees() finishes in unpack_trees(), then:
1584 *
1585 * - CE_NEW_SKIP_WORKTREE will be computed correctly
1586 * - verify_absent() be called again, this time with
1587 * correct CE_NEW_SKIP_WORKTREE
1588 *
1589 * verify_absent() call here does nothing in sparse
1590 * checkout (i.e. o->skip_sparse_checkout == 0)
1591 */
1592 update |= CE_ADDED;
1593 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
1594
René Scharfef2fa3542013-06-02 17:46:54 +02001595 if (verify_absent(merge,
1596 ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
1597 free(merge);
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001598 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02001599 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001600 invalidate_ce_path(merge, o);
1601 } else if (!(old->ce_flags & CE_CONFLICTED)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001602 /*
1603 * See if we can re-use the old CE directly?
1604 * That way we get the uptodate stat info.
1605 *
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07001606 * This also removes the UPDATE flag on a match; otherwise
1607 * we will end up overwriting local changes in the work tree.
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001608 */
1609 if (same(old, merge)) {
Linus Torvaldseb7a2f12008-02-22 20:41:17 -08001610 copy_cache_entry(merge, old);
Linus Torvalds7f8ab8d2008-03-16 11:42:50 -07001611 update = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001612 } else {
René Scharfef2fa3542013-06-02 17:46:54 +02001613 if (verify_uptodate(old, o)) {
1614 free(merge);
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001615 return -1;
René Scharfef2fa3542013-06-02 17:46:54 +02001616 }
Nguyễn Thái Ngọc Duy2431afb2010-11-27 13:24:04 +07001617 /* Migrate old flags over */
1618 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08001619 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001620 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001621 } else {
1622 /*
1623 * Previously unmerged entry left as an existence
1624 * marker by read_index_unmerged();
1625 */
1626 invalidate_ce_path(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001627 }
1628
René Scharfef2fa3542013-06-02 17:46:54 +02001629 do_add_entry(o, merge, update, CE_STAGEMASK);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001630 return 1;
1631}
1632
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001633static int deleted_entry(const struct cache_entry *ce,
1634 const struct cache_entry *old,
1635 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001636{
Linus Torvalds34110cd2008-03-06 18:12:28 -08001637 /* Did it exist in the index? */
1638 if (!old) {
Matthieu Moy08402b02010-08-11 10:38:06 +02001639 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001640 return -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -08001641 return 0;
1642 }
Junio C Hamanoe11d7b52009-12-31 23:04:04 -08001643 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
Linus Torvalds34110cd2008-03-06 18:12:28 -08001644 return -1;
1645 add_entry(o, ce, CE_REMOVE, 0);
Linus Torvaldsbc052d72008-03-06 12:26:14 -08001646 invalidate_ce_path(ce, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001647 return 1;
1648}
1649
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001650static int keep_entry(const struct cache_entry *ce,
1651 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001652{
Linus Torvalds34110cd2008-03-06 18:12:28 -08001653 add_entry(o, ce, 0, 0);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001654 return 1;
1655}
1656
1657#if DBRT_DEBUG
1658static void show_stage_entry(FILE *o,
1659 const char *label, const struct cache_entry *ce)
1660{
1661 if (!ce)
1662 fprintf(o, "%s (missing)\n", label);
1663 else
1664 fprintf(o, "%s%06o %s %d\t%s\n",
1665 label,
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001666 ce->ce_mode,
brian m. carlson99d1a982016-09-05 20:07:52 +00001667 oid_to_hex(&ce->oid),
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001668 ce_stage(ce),
1669 ce->name);
1670}
1671#endif
1672
René Scharfe5828e832013-06-02 17:46:56 +02001673int threeway_merge(const struct cache_entry * const *stages,
1674 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001675{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001676 const struct cache_entry *index;
1677 const struct cache_entry *head;
1678 const struct cache_entry *remote = stages[o->head_idx + 1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001679 int count;
1680 int head_match = 0;
1681 int remote_match = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001682
1683 int df_conflict_head = 0;
1684 int df_conflict_remote = 0;
1685
1686 int any_anc_missing = 0;
1687 int no_anc_exists = 1;
1688 int i;
1689
1690 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001691 if (!stages[i] || stages[i] == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001692 any_anc_missing = 1;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001693 else
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001694 no_anc_exists = 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001695 }
1696
1697 index = stages[0];
1698 head = stages[o->head_idx];
1699
1700 if (head == o->df_conflict_entry) {
1701 df_conflict_head = 1;
1702 head = NULL;
1703 }
1704
1705 if (remote == o->df_conflict_entry) {
1706 df_conflict_remote = 1;
1707 remote = NULL;
1708 }
1709
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07001710 /*
1711 * First, if there's a #16 situation, note that to prevent #13
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001712 * and #14.
1713 */
1714 if (!same(remote, head)) {
1715 for (i = 1; i < o->head_idx; i++) {
1716 if (same(stages[i], head)) {
1717 head_match = i;
1718 }
1719 if (same(stages[i], remote)) {
1720 remote_match = i;
1721 }
1722 }
1723 }
1724
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07001725 /*
1726 * We start with cases where the index is allowed to match
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001727 * something other than the head: #14(ALT) and #2ALT, where it
1728 * is permitted to match the result instead.
1729 */
1730 /* #14, #14ALT, #2ALT */
1731 if (remote && !df_conflict_head && head_match && !remote_match) {
1732 if (index && !same(index, remote) && !same(index, head))
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07001733 return reject_merge(index, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001734 return merged_entry(remote, index, o);
1735 }
1736 /*
1737 * If we have an entry in the index cache, then we want to
1738 * make sure that it matches head.
1739 */
Daniel Barkalow4e7c4572008-02-07 11:40:02 -05001740 if (index && !same(index, head))
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07001741 return reject_merge(index, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001742
1743 if (head) {
1744 /* #5ALT, #15 */
1745 if (same(head, remote))
1746 return merged_entry(head, index, o);
1747 /* #13, #3ALT */
1748 if (!df_conflict_remote && remote_match && !head_match)
1749 return merged_entry(head, index, o);
1750 }
1751
1752 /* #1 */
Linus Torvalds34110cd2008-03-06 18:12:28 -08001753 if (!head && !remote && any_anc_missing)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001754 return 0;
1755
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07001756 /*
1757 * Under the "aggressive" rule, we resolve mostly trivial
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001758 * cases that we historically had git-merge-one-file resolve.
1759 */
1760 if (o->aggressive) {
Junio C Hamanocee2d6a2009-10-11 13:38:11 -07001761 int head_deleted = !head;
1762 int remote_deleted = !remote;
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001763 const struct cache_entry *ce = NULL;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001764
1765 if (index)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001766 ce = index;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001767 else if (head)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001768 ce = head;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001769 else if (remote)
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001770 ce = remote;
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001771 else {
1772 for (i = 1; i < o->head_idx; i++) {
1773 if (stages[i] && stages[i] != o->df_conflict_entry) {
Sven Verdoolaege0cf73752007-07-17 20:28:28 +02001774 ce = stages[i];
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001775 break;
1776 }
1777 }
1778 }
1779
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001780 /*
1781 * Deleted in both.
1782 * Deleted in one and unchanged in the other.
1783 */
1784 if ((head_deleted && remote_deleted) ||
1785 (head_deleted && remote && remote_match) ||
1786 (remote_deleted && head && head_match)) {
1787 if (index)
1788 return deleted_entry(index, index, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08001789 if (ce && !head_deleted) {
Matthieu Moy08402b02010-08-11 10:38:06 +02001790 if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001791 return -1;
1792 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001793 return 0;
1794 }
1795 /*
1796 * Added in both, identically.
1797 */
1798 if (no_anc_exists && head && remote && same(head, remote))
1799 return merged_entry(head, index, o);
1800
1801 }
1802
1803 /* Below are "no merge" cases, which require that the index be
1804 * up-to-date to avoid the files getting overwritten with
1805 * conflict resolution files.
1806 */
1807 if (index) {
Daniel Barkalow203a2fe2008-02-07 11:39:48 -05001808 if (verify_uptodate(index, o))
1809 return -1;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001810 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001811
1812 o->nontrivial_merge = 1;
1813
Junio C Hamanoea4b52a2007-04-07 05:42:01 -07001814 /* #2, #3, #4, #6, #7, #9, #10, #11. */
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001815 count = 0;
1816 if (!head_match || !remote_match) {
1817 for (i = 1; i < o->head_idx; i++) {
Junio C Hamano4c4caaf2007-04-07 05:49:19 -07001818 if (stages[i] && stages[i] != o->df_conflict_entry) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07001819 keep_entry(stages[i], o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001820 count++;
1821 break;
1822 }
1823 }
1824 }
1825#if DBRT_DEBUG
1826 else {
1827 fprintf(stderr, "read-tree: warning #16 detected\n");
1828 show_stage_entry(stderr, "head ", stages[head_match]);
1829 show_stage_entry(stderr, "remote ", stages[remote_match]);
1830 }
1831#endif
Junio C Hamano7f7932a2007-04-02 00:06:12 -07001832 if (head) { count += keep_entry(head, o); }
1833 if (remote) { count += keep_entry(remote, o); }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001834 return count;
1835}
1836
1837/*
1838 * Two-way merge.
1839 *
1840 * The rule is to "carry forward" what is in the index without losing
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03001841 * information across a "fast-forward", favoring a successful merge
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001842 * over a merge failure when it makes sense. For details of the
1843 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1844 *
1845 */
René Scharfe5828e832013-06-02 17:46:56 +02001846int twoway_merge(const struct cache_entry * const *src,
1847 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001848{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001849 const struct cache_entry *current = src[0];
1850 const struct cache_entry *oldtree = src[1];
1851 const struct cache_entry *newtree = src[2];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001852
1853 if (o->merge_size != 2)
1854 return error("Cannot do a twoway merge of %d trees",
1855 o->merge_size);
1856
Junio C Hamanob8ba1532007-04-02 16:29:56 -07001857 if (oldtree == o->df_conflict_entry)
1858 oldtree = NULL;
1859 if (newtree == o->df_conflict_entry)
1860 newtree = NULL;
1861
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001862 if (current) {
Jeff Kingb018ff62012-12-29 15:51:54 -05001863 if (current->ce_flags & CE_CONFLICTED) {
1864 if (same(oldtree, newtree) || o->reset) {
1865 if (!newtree)
1866 return deleted_entry(current, current, o);
1867 else
1868 return merged_entry(newtree, current, o);
1869 }
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07001870 return reject_merge(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07001871 } else if ((!oldtree && !newtree) || /* 4 and 5 */
Jeff Kingb018ff62012-12-29 15:51:54 -05001872 (!oldtree && newtree &&
1873 same(current, newtree)) || /* 6 and 7 */
1874 (oldtree && newtree &&
1875 same(oldtree, newtree)) || /* 14 and 15 */
1876 (oldtree && newtree &&
1877 !same(oldtree, newtree) && /* 18 and 19 */
1878 same(current, newtree))) {
Junio C Hamano7f7932a2007-04-02 00:06:12 -07001879 return keep_entry(current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07001880 } else if (oldtree && !newtree && same(current, oldtree)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001881 /* 10 or 11 */
1882 return deleted_entry(oldtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07001883 } else if (oldtree && newtree &&
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001884 same(current, oldtree) && !same(current, newtree)) {
1885 /* 20 or 21 */
1886 return merged_entry(newtree, current, o);
Jonathan Nieder6c1db1b2014-08-12 17:00:45 -07001887 } else
Jonathan Nieder6a143aa2014-08-12 17:03:18 -07001888 return reject_merge(current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001889 }
Junio C Hamano55218832008-09-07 19:49:25 -07001890 else if (newtree) {
1891 if (oldtree && !o->initial_checkout) {
1892 /*
1893 * deletion of the path was staged;
1894 */
1895 if (same(oldtree, newtree))
1896 return 1;
1897 return reject_merge(oldtree, o);
1898 }
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001899 return merged_entry(newtree, current, o);
Junio C Hamano55218832008-09-07 19:49:25 -07001900 }
Linus Torvaldsd6996762007-08-10 12:31:20 -07001901 return deleted_entry(oldtree, current, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001902}
1903
1904/*
1905 * Bind merge.
1906 *
1907 * Keep the index entries at stage0, collapse stage1 but make sure
1908 * stage0 does not have anything there.
1909 */
René Scharfe5828e832013-06-02 17:46:56 +02001910int bind_merge(const struct cache_entry * const *src,
1911 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001912{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001913 const struct cache_entry *old = src[0];
1914 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001915
1916 if (o->merge_size != 1)
Pete Wyckoff82247e92012-04-29 20:28:45 -04001917 return error("Cannot do a bind merge of %d trees",
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001918 o->merge_size);
1919 if (a && old)
Daniel Barkalow17e46422008-02-07 11:39:52 -05001920 return o->gently ? -1 :
Matthieu Moy08353eb2010-08-11 10:38:04 +02001921 error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001922 if (!a)
Junio C Hamano7f7932a2007-04-02 00:06:12 -07001923 return keep_entry(old, o);
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001924 else
1925 return merged_entry(a, NULL, o);
1926}
1927
1928/*
1929 * One-way merge.
1930 *
1931 * The rule is:
1932 * - take the stat information from stage0, take the data from stage1
1933 */
René Scharfe5828e832013-06-02 17:46:56 +02001934int oneway_merge(const struct cache_entry * const *src,
1935 struct unpack_trees_options *o)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001936{
René Scharfeeb9ae4b2013-06-02 17:46:55 +02001937 const struct cache_entry *old = src[0];
1938 const struct cache_entry *a = src[1];
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001939
1940 if (o->merge_size != 1)
1941 return error("Cannot do a oneway merge of %d trees",
1942 o->merge_size);
1943
Junio C Hamano78d3b062009-07-18 12:26:38 -07001944 if (!a || a == o->df_conflict_entry)
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001945 return deleted_entry(old, old, o);
Linus Torvalds34110cd2008-03-06 18:12:28 -08001946
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001947 if (old && same(old, a)) {
Linus Torvalds34110cd2008-03-06 18:12:28 -08001948 int update = 0;
Martin von Zweigbergk686b2de2012-12-20 13:03:36 -08001949 if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old)) {
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001950 struct stat st;
1951 if (lstat(old->name, &st) ||
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001952 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
Linus Torvalds34110cd2008-03-06 18:12:28 -08001953 update |= CE_UPDATE;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001954 }
Linus Torvalds34110cd2008-03-06 18:12:28 -08001955 add_entry(o, old, update, 0);
1956 return 0;
Johannes Schindelin076b0ad2006-07-30 20:26:15 +02001957 }
1958 return merged_entry(a, old, o);
1959}