unpack-trees.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. #include "cache.h"
  2. #include "strvec.h"
  3. #include "repository.h"
  4. #include "config.h"
  5. #include "dir.h"
  6. #include "tree.h"
  7. #include "tree-walk.h"
  8. #include "cache-tree.h"
  9. #include "unpack-trees.h"
  10. #include "progress.h"
  11. #include "refs.h"
  12. #include "attr.h"
  13. #include "split-index.h"
  14. #include "submodule.h"
  15. #include "submodule-config.h"
  16. #include "fsmonitor.h"
  17. #include "object-store.h"
  18. #include "promisor-remote.h"
  19. /*
  20. * Error messages expected by scripts out of plumbing commands such as
  21. * read-tree. Non-scripted Porcelain is not required to use these messages
  22. * and in fact are encouraged to reword them to better suit their particular
  23. * situation better. See how "git checkout" and "git merge" replaces
  24. * them using setup_unpack_trees_porcelain(), for example.
  25. */
  26. static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
  27. /* ERROR_WOULD_OVERWRITE */
  28. "Entry '%s' would be overwritten by merge. Cannot merge.",
  29. /* ERROR_NOT_UPTODATE_FILE */
  30. "Entry '%s' not uptodate. Cannot merge.",
  31. /* ERROR_NOT_UPTODATE_DIR */
  32. "Updating '%s' would lose untracked files in it",
  33. /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
  34. "Untracked working tree file '%s' would be overwritten by merge.",
  35. /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
  36. "Untracked working tree file '%s' would be removed by merge.",
  37. /* ERROR_BIND_OVERLAP */
  38. "Entry '%s' overlaps with '%s'. Cannot bind.",
  39. /* ERROR_WOULD_LOSE_SUBMODULE */
  40. "Submodule '%s' cannot checkout new HEAD.",
  41. /* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
  42. "",
  43. /* WARNING_SPARSE_NOT_UPTODATE_FILE */
  44. "Path '%s' not uptodate; will not remove from working tree.",
  45. /* WARNING_SPARSE_UNMERGED_FILE */
  46. "Path '%s' unmerged; will not remove from working tree.",
  47. /* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */
  48. "Path '%s' already present; will not overwrite with sparse update.",
  49. };
  50. #define ERRORMSG(o,type) \
  51. ( ((o) && (o)->msgs[(type)]) \
  52. ? ((o)->msgs[(type)]) \
  53. : (unpack_plumbing_errors[(type)]) )
  54. static const char *super_prefixed(const char *path)
  55. {
  56. /*
  57. * It is necessary and sufficient to have two static buffers
  58. * here, as the return value of this function is fed to
  59. * error() using the unpack_*_errors[] templates we see above.
  60. */
  61. static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};
  62. static int super_prefix_len = -1;
  63. static unsigned idx = ARRAY_SIZE(buf) - 1;
  64. if (super_prefix_len < 0) {
  65. const char *super_prefix = get_super_prefix();
  66. if (!super_prefix) {
  67. super_prefix_len = 0;
  68. } else {
  69. int i;
  70. for (i = 0; i < ARRAY_SIZE(buf); i++)
  71. strbuf_addstr(&buf[i], super_prefix);
  72. super_prefix_len = buf[0].len;
  73. }
  74. }
  75. if (!super_prefix_len)
  76. return path;
  77. if (++idx >= ARRAY_SIZE(buf))
  78. idx = 0;
  79. strbuf_setlen(&buf[idx], super_prefix_len);
  80. strbuf_addstr(&buf[idx], path);
  81. return buf[idx].buf;
  82. }
  83. void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
  84. const char *cmd)
  85. {
  86. int i;
  87. const char **msgs = opts->msgs;
  88. const char *msg;
  89. strvec_init(&opts->msgs_to_free);
  90. if (!strcmp(cmd, "checkout"))
  91. msg = advice_commit_before_merge
  92. ? _("Your local changes to the following files would be overwritten by checkout:\n%%s"
  93. "Please commit your changes or stash them before you switch branches.")
  94. : _("Your local changes to the following files would be overwritten by checkout:\n%%s");
  95. else if (!strcmp(cmd, "merge"))
  96. msg = advice_commit_before_merge
  97. ? _("Your local changes to the following files would be overwritten by merge:\n%%s"
  98. "Please commit your changes or stash them before you merge.")
  99. : _("Your local changes to the following files would be overwritten by merge:\n%%s");
  100. else
  101. msg = advice_commit_before_merge
  102. ? _("Your local changes to the following files would be overwritten by %s:\n%%s"
  103. "Please commit your changes or stash them before you %s.")
  104. : _("Your local changes to the following files would be overwritten by %s:\n%%s");
  105. msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =
  106. strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
  107. msgs[ERROR_NOT_UPTODATE_DIR] =
  108. _("Updating the following directories would lose untracked files in them:\n%s");
  109. if (!strcmp(cmd, "checkout"))
  110. msg = advice_commit_before_merge
  111. ? _("The following untracked working tree files would be removed by checkout:\n%%s"
  112. "Please move or remove them before you switch branches.")
  113. : _("The following untracked working tree files would be removed by checkout:\n%%s");
  114. else if (!strcmp(cmd, "merge"))
  115. msg = advice_commit_before_merge
  116. ? _("The following untracked working tree files would be removed by merge:\n%%s"
  117. "Please move or remove them before you merge.")
  118. : _("The following untracked working tree files would be removed by merge:\n%%s");
  119. else
  120. msg = advice_commit_before_merge
  121. ? _("The following untracked working tree files would be removed by %s:\n%%s"
  122. "Please move or remove them before you %s.")
  123. : _("The following untracked working tree files would be removed by %s:\n%%s");
  124. msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =
  125. strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
  126. if (!strcmp(cmd, "checkout"))
  127. msg = advice_commit_before_merge
  128. ? _("The following untracked working tree files would be overwritten by checkout:\n%%s"
  129. "Please move or remove them before you switch branches.")
  130. : _("The following untracked working tree files would be overwritten by checkout:\n%%s");
  131. else if (!strcmp(cmd, "merge"))
  132. msg = advice_commit_before_merge
  133. ? _("The following untracked working tree files would be overwritten by merge:\n%%s"
  134. "Please move or remove them before you merge.")
  135. : _("The following untracked working tree files would be overwritten by merge:\n%%s");
  136. else
  137. msg = advice_commit_before_merge
  138. ? _("The following untracked working tree files would be overwritten by %s:\n%%s"
  139. "Please move or remove them before you %s.")
  140. : _("The following untracked working tree files would be overwritten by %s:\n%%s");
  141. msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =
  142. strvec_pushf(&opts->msgs_to_free, msg, cmd, cmd);
  143. /*
  144. * Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
  145. * cannot easily display it as a list.
  146. */
  147. msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");
  148. msgs[ERROR_WOULD_LOSE_SUBMODULE] =
  149. _("Cannot update submodule:\n%s");
  150. msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] =
  151. _("The following paths are not up to date and were left despite sparse patterns:\n%s");
  152. msgs[WARNING_SPARSE_UNMERGED_FILE] =
  153. _("The following paths are unmerged and were left despite sparse patterns:\n%s");
  154. msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] =
  155. _("The following paths were already present and thus not updated despite sparse patterns:\n%s");
  156. opts->show_all_errors = 1;
  157. /* rejected paths may not have a static buffer */
  158. for (i = 0; i < ARRAY_SIZE(opts->unpack_rejects); i++)
  159. opts->unpack_rejects[i].strdup_strings = 1;
  160. }
  161. void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)
  162. {
  163. strvec_clear(&opts->msgs_to_free);
  164. memset(opts->msgs, 0, sizeof(opts->msgs));
  165. }
  166. static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
  167. unsigned int set, unsigned int clear)
  168. {
  169. clear |= CE_HASHED;
  170. if (set & CE_REMOVE)
  171. set |= CE_WT_REMOVE;
  172. ce->ce_flags = (ce->ce_flags & ~clear) | set;
  173. return add_index_entry(&o->result, ce,
  174. ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
  175. }
  176. static void add_entry(struct unpack_trees_options *o,
  177. const struct cache_entry *ce,
  178. unsigned int set, unsigned int clear)
  179. {
  180. do_add_entry(o, dup_cache_entry(ce, &o->result), set, clear);
  181. }
  182. /*
  183. * add error messages on path <path>
  184. * corresponding to the type <e> with the message <msg>
  185. * indicating if it should be display in porcelain or not
  186. */
  187. static int add_rejected_path(struct unpack_trees_options *o,
  188. enum unpack_trees_error_types e,
  189. const char *path)
  190. {
  191. if (o->quiet)
  192. return -1;
  193. if (!o->show_all_errors)
  194. return error(ERRORMSG(o, e), super_prefixed(path));
  195. /*
  196. * Otherwise, insert in a list for future display by
  197. * display_(error|warning)_msgs()
  198. */
  199. string_list_append(&o->unpack_rejects[e], path);
  200. return -1;
  201. }
  202. /*
  203. * display all the error messages stored in a nice way
  204. */
  205. static void display_error_msgs(struct unpack_trees_options *o)
  206. {
  207. int e;
  208. unsigned error_displayed = 0;
  209. for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
  210. struct string_list *rejects = &o->unpack_rejects[e];
  211. if (rejects->nr > 0) {
  212. int i;
  213. struct strbuf path = STRBUF_INIT;
  214. error_displayed = 1;
  215. for (i = 0; i < rejects->nr; i++)
  216. strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
  217. error(ERRORMSG(o, e), super_prefixed(path.buf));
  218. strbuf_release(&path);
  219. }
  220. string_list_clear(rejects, 0);
  221. }
  222. if (error_displayed)
  223. fprintf(stderr, _("Aborting\n"));
  224. }
  225. /*
  226. * display all the warning messages stored in a nice way
  227. */
  228. static void display_warning_msgs(struct unpack_trees_options *o)
  229. {
  230. int e;
  231. unsigned warning_displayed = 0;
  232. for (e = NB_UNPACK_TREES_ERROR_TYPES + 1;
  233. e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
  234. struct string_list *rejects = &o->unpack_rejects[e];
  235. if (rejects->nr > 0) {
  236. int i;
  237. struct strbuf path = STRBUF_INIT;
  238. warning_displayed = 1;
  239. for (i = 0; i < rejects->nr; i++)
  240. strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
  241. warning(ERRORMSG(o, e), super_prefixed(path.buf));
  242. strbuf_release(&path);
  243. }
  244. string_list_clear(rejects, 0);
  245. }
  246. if (warning_displayed)
  247. fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
  248. }
  249. static int check_submodule_move_head(const struct cache_entry *ce,
  250. const char *old_id,
  251. const char *new_id,
  252. struct unpack_trees_options *o)
  253. {
  254. unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;
  255. const struct submodule *sub = submodule_from_ce(ce);
  256. if (!sub)
  257. return 0;
  258. if (o->reset)
  259. flags |= SUBMODULE_MOVE_HEAD_FORCE;
  260. if (submodule_move_head(ce->name, old_id, new_id, flags))
  261. return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);
  262. return 0;
  263. }
  264. /*
  265. * Perform the loading of the repository's gitmodules file. This function is
  266. * used by 'check_update()' to perform loading of the gitmodules file in two
  267. * different situations:
  268. * (1) before removing entries from the working tree if the gitmodules file has
  269. * been marked for removal. This situation is specified by 'state' == NULL.
  270. * (2) before checking out entries to the working tree if the gitmodules file
  271. * has been marked for update. This situation is specified by 'state' != NULL.
  272. */
  273. static void load_gitmodules_file(struct index_state *index,
  274. struct checkout *state)
  275. {
  276. int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));
  277. if (pos >= 0) {
  278. struct cache_entry *ce = index->cache[pos];
  279. if (!state && ce->ce_flags & CE_WT_REMOVE) {
  280. repo_read_gitmodules(the_repository, 0);
  281. } else if (state && (ce->ce_flags & CE_UPDATE)) {
  282. submodule_free(the_repository);
  283. checkout_entry(ce, state, NULL, NULL);
  284. repo_read_gitmodules(the_repository, 0);
  285. }
  286. }
  287. }
  288. static struct progress *get_progress(struct unpack_trees_options *o,
  289. struct index_state *index)
  290. {
  291. unsigned cnt = 0, total = 0;
  292. if (!o->update || !o->verbose_update)
  293. return NULL;
  294. for (; cnt < index->cache_nr; cnt++) {
  295. const struct cache_entry *ce = index->cache[cnt];
  296. if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
  297. total++;
  298. }
  299. return start_delayed_progress(_("Updating files"), total);
  300. }
  301. static void setup_collided_checkout_detection(struct checkout *state,
  302. struct index_state *index)
  303. {
  304. int i;
  305. state->clone = 1;
  306. for (i = 0; i < index->cache_nr; i++)
  307. index->cache[i]->ce_flags &= ~CE_MATCHED;
  308. }
  309. static void report_collided_checkout(struct index_state *index)
  310. {
  311. struct string_list list = STRING_LIST_INIT_NODUP;
  312. int i;
  313. for (i = 0; i < index->cache_nr; i++) {
  314. struct cache_entry *ce = index->cache[i];
  315. if (!(ce->ce_flags & CE_MATCHED))
  316. continue;
  317. string_list_append(&list, ce->name);
  318. ce->ce_flags &= ~CE_MATCHED;
  319. }
  320. list.cmp = fspathcmp;
  321. string_list_sort(&list);
  322. if (list.nr) {
  323. warning(_("the following paths have collided (e.g. case-sensitive paths\n"
  324. "on a case-insensitive filesystem) and only one from the same\n"
  325. "colliding group is in the working tree:\n"));
  326. for (i = 0; i < list.nr; i++)
  327. fprintf(stderr, " '%s'\n", list.items[i].string);
  328. }
  329. string_list_clear(&list, 0);
  330. }
  331. static int check_updates(struct unpack_trees_options *o,
  332. struct index_state *index)
  333. {
  334. unsigned cnt = 0;
  335. int errs = 0;
  336. struct progress *progress;
  337. struct checkout state = CHECKOUT_INIT;
  338. int i;
  339. trace_performance_enter();
  340. state.force = 1;
  341. state.quiet = 1;
  342. state.refresh_cache = 1;
  343. state.istate = index;
  344. clone_checkout_metadata(&state.meta, &o->meta, NULL);
  345. if (!o->update || o->dry_run) {
  346. remove_marked_cache_entries(index, 0);
  347. trace_performance_leave("check_updates");
  348. return 0;
  349. }
  350. if (o->clone)
  351. setup_collided_checkout_detection(&state, index);
  352. progress = get_progress(o, index);
  353. git_attr_set_direction(GIT_ATTR_CHECKOUT);
  354. if (should_update_submodules())
  355. load_gitmodules_file(index, NULL);
  356. for (i = 0; i < index->cache_nr; i++) {
  357. const struct cache_entry *ce = index->cache[i];
  358. if (ce->ce_flags & CE_WT_REMOVE) {
  359. display_progress(progress, ++cnt);
  360. unlink_entry(ce);
  361. }
  362. }
  363. remove_marked_cache_entries(index, 0);
  364. remove_scheduled_dirs();
  365. if (should_update_submodules())
  366. load_gitmodules_file(index, &state);
  367. enable_delayed_checkout(&state);
  368. if (has_promisor_remote()) {
  369. /*
  370. * Prefetch the objects that are to be checked out in the loop
  371. * below.
  372. */
  373. struct oid_array to_fetch = OID_ARRAY_INIT;
  374. for (i = 0; i < index->cache_nr; i++) {
  375. struct cache_entry *ce = index->cache[i];
  376. if (!(ce->ce_flags & CE_UPDATE) ||
  377. S_ISGITLINK(ce->ce_mode))
  378. continue;
  379. if (!oid_object_info_extended(the_repository, &ce->oid,
  380. NULL,
  381. OBJECT_INFO_FOR_PREFETCH))
  382. continue;
  383. oid_array_append(&to_fetch, &ce->oid);
  384. }
  385. promisor_remote_get_direct(the_repository,
  386. to_fetch.oid, to_fetch.nr);
  387. oid_array_clear(&to_fetch);
  388. }
  389. for (i = 0; i < index->cache_nr; i++) {
  390. struct cache_entry *ce = index->cache[i];
  391. if (ce->ce_flags & CE_UPDATE) {
  392. if (ce->ce_flags & CE_WT_REMOVE)
  393. BUG("both update and delete flags are set on %s",
  394. ce->name);
  395. display_progress(progress, ++cnt);
  396. ce->ce_flags &= ~CE_UPDATE;
  397. errs |= checkout_entry(ce, &state, NULL, NULL);
  398. }
  399. }
  400. stop_progress(&progress);
  401. errs |= finish_delayed_checkout(&state, NULL);
  402. git_attr_set_direction(GIT_ATTR_CHECKIN);
  403. if (o->clone)
  404. report_collided_checkout(index);
  405. trace_performance_leave("check_updates");
  406. return errs != 0;
  407. }
  408. static int verify_uptodate_sparse(const struct cache_entry *ce,
  409. struct unpack_trees_options *o);
  410. static int verify_absent_sparse(const struct cache_entry *ce,
  411. enum unpack_trees_error_types,
  412. struct unpack_trees_options *o);
  413. static int apply_sparse_checkout(struct index_state *istate,
  414. struct cache_entry *ce,
  415. struct unpack_trees_options *o)
  416. {
  417. int was_skip_worktree = ce_skip_worktree(ce);
  418. if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)
  419. ce->ce_flags |= CE_SKIP_WORKTREE;
  420. else
  421. ce->ce_flags &= ~CE_SKIP_WORKTREE;
  422. if (was_skip_worktree != ce_skip_worktree(ce)) {
  423. ce->ce_flags |= CE_UPDATE_IN_BASE;
  424. mark_fsmonitor_invalid(istate, ce);
  425. istate->cache_changed |= CE_ENTRY_CHANGED;
  426. }
  427. /*
  428. * if (!was_skip_worktree && !ce_skip_worktree()) {
  429. * This is perfectly normal. Move on;
  430. * }
  431. */
  432. /*
  433. * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
  434. * area as a result of ce_skip_worktree() shortcuts in
  435. * verify_absent() and verify_uptodate().
  436. * Make sure they don't modify worktree if they are already
  437. * outside checkout area
  438. */
  439. if (was_skip_worktree && ce_skip_worktree(ce)) {
  440. ce->ce_flags &= ~CE_UPDATE;
  441. /*
  442. * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
  443. * on to get that file removed from both index and worktree.
  444. * If that file is already outside worktree area, don't
  445. * bother remove it.
  446. */
  447. if (ce->ce_flags & CE_REMOVE)
  448. ce->ce_flags &= ~CE_WT_REMOVE;
  449. }
  450. if (!was_skip_worktree && ce_skip_worktree(ce)) {
  451. /*
  452. * If CE_UPDATE is set, verify_uptodate() must be called already
  453. * also stat info may have lost after merged_entry() so calling
  454. * verify_uptodate() again may fail
  455. */
  456. if (!(ce->ce_flags & CE_UPDATE) &&
  457. verify_uptodate_sparse(ce, o)) {
  458. ce->ce_flags &= ~CE_SKIP_WORKTREE;
  459. return -1;
  460. }
  461. ce->ce_flags |= CE_WT_REMOVE;
  462. ce->ce_flags &= ~CE_UPDATE;
  463. }
  464. if (was_skip_worktree && !ce_skip_worktree(ce)) {
  465. if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
  466. return -1;
  467. ce->ce_flags |= CE_UPDATE;
  468. }
  469. return 0;
  470. }
  471. static int warn_conflicted_path(struct index_state *istate,
  472. int i,
  473. struct unpack_trees_options *o)
  474. {
  475. char *conflicting_path = istate->cache[i]->name;
  476. int count = 0;
  477. add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path);
  478. /* Find out how many higher stage entries are at same path */
  479. while ((++count) + i < istate->cache_nr &&
  480. !strcmp(conflicting_path, istate->cache[count + i]->name))
  481. ; /* do nothing */
  482. return count;
  483. }
  484. static inline int call_unpack_fn(const struct cache_entry * const *src,
  485. struct unpack_trees_options *o)
  486. {
  487. int ret = o->fn(src, o);
  488. if (ret > 0)
  489. ret = 0;
  490. return ret;
  491. }
  492. static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
  493. {
  494. ce->ce_flags |= CE_UNPACKED;
  495. if (o->cache_bottom < o->src_index->cache_nr &&
  496. o->src_index->cache[o->cache_bottom] == ce) {
  497. int bottom = o->cache_bottom;
  498. while (bottom < o->src_index->cache_nr &&
  499. o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
  500. bottom++;
  501. o->cache_bottom = bottom;
  502. }
  503. }
  504. static void mark_all_ce_unused(struct index_state *index)
  505. {
  506. int i;
  507. for (i = 0; i < index->cache_nr; i++)
  508. index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);
  509. }
  510. static int locate_in_src_index(const struct cache_entry *ce,
  511. struct unpack_trees_options *o)
  512. {
  513. struct index_state *index = o->src_index;
  514. int len = ce_namelen(ce);
  515. int pos = index_name_pos(index, ce->name, len);
  516. if (pos < 0)
  517. pos = -1 - pos;
  518. return pos;
  519. }
  520. /*
  521. * We call unpack_index_entry() with an unmerged cache entry
  522. * only in diff-index, and it wants a single callback. Skip
  523. * the other unmerged entry with the same name.
  524. */
  525. static void mark_ce_used_same_name(struct cache_entry *ce,
  526. struct unpack_trees_options *o)
  527. {
  528. struct index_state *index = o->src_index;
  529. int len = ce_namelen(ce);
  530. int pos;
  531. for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
  532. struct cache_entry *next = index->cache[pos];
  533. if (len != ce_namelen(next) ||
  534. memcmp(ce->name, next->name, len))
  535. break;
  536. mark_ce_used(next, o);
  537. }
  538. }
  539. static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
  540. {
  541. const struct index_state *index = o->src_index;
  542. int pos = o->cache_bottom;
  543. while (pos < index->cache_nr) {
  544. struct cache_entry *ce = index->cache[pos];
  545. if (!(ce->ce_flags & CE_UNPACKED))
  546. return ce;
  547. pos++;
  548. }
  549. return NULL;
  550. }
  551. static void add_same_unmerged(const struct cache_entry *ce,
  552. struct unpack_trees_options *o)
  553. {
  554. struct index_state *index = o->src_index;
  555. int len = ce_namelen(ce);
  556. int pos = index_name_pos(index, ce->name, len);
  557. if (0 <= pos)
  558. die("programming error in a caller of mark_ce_used_same_name");
  559. for (pos = -pos - 1; pos < index->cache_nr; pos++) {
  560. struct cache_entry *next = index->cache[pos];
  561. if (len != ce_namelen(next) ||
  562. memcmp(ce->name, next->name, len))
  563. break;
  564. add_entry(o, next, 0, 0);
  565. mark_ce_used(next, o);
  566. }
  567. }
  568. static int unpack_index_entry(struct cache_entry *ce,
  569. struct unpack_trees_options *o)
  570. {
  571. const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
  572. int ret;
  573. src[0] = ce;
  574. mark_ce_used(ce, o);
  575. if (ce_stage(ce)) {
  576. if (o->skip_unmerged) {
  577. add_entry(o, ce, 0, 0);
  578. return 0;
  579. }
  580. }
  581. ret = call_unpack_fn(src, o);
  582. if (ce_stage(ce))
  583. mark_ce_used_same_name(ce, o);
  584. return ret;
  585. }
  586. static int find_cache_pos(struct traverse_info *, const char *p, size_t len);
  587. static void restore_cache_bottom(struct traverse_info *info, int bottom)
  588. {
  589. struct unpack_trees_options *o = info->data;
  590. if (o->diff_index_cached)
  591. return;
  592. o->cache_bottom = bottom;
  593. }
  594. static int switch_cache_bottom(struct traverse_info *info)
  595. {
  596. struct unpack_trees_options *o = info->data;
  597. int ret, pos;
  598. if (o->diff_index_cached)
  599. return 0;
  600. ret = o->cache_bottom;
  601. pos = find_cache_pos(info->prev, info->name, info->namelen);
  602. if (pos < -1)
  603. o->cache_bottom = -2 - pos;
  604. else if (pos < 0)
  605. o->cache_bottom = o->src_index->cache_nr;
  606. return ret;
  607. }
  608. static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)
  609. {
  610. return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid);
  611. }
  612. static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,
  613. struct name_entry *names,
  614. struct traverse_info *info)
  615. {
  616. struct unpack_trees_options *o = info->data;
  617. int i;
  618. if (!o->merge || dirmask != ((1 << n) - 1))
  619. return 0;
  620. for (i = 1; i < n; i++)
  621. if (!are_same_oid(names, names + i))
  622. return 0;
  623. return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);
  624. }
  625. static int index_pos_by_traverse_info(struct name_entry *names,
  626. struct traverse_info *info)
  627. {
  628. struct unpack_trees_options *o = info->data;
  629. struct strbuf name = STRBUF_INIT;
  630. int pos;
  631. strbuf_make_traverse_path(&name, info, names->path, names->pathlen);
  632. strbuf_addch(&name, '/');
  633. pos = index_name_pos(o->src_index, name.buf, name.len);
  634. if (pos >= 0)
  635. BUG("This is a directory and should not exist in index");
  636. pos = -pos - 1;
  637. if (pos >= o->src_index->cache_nr ||
  638. !starts_with(o->src_index->cache[pos]->name, name.buf) ||
  639. (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf)))
  640. BUG("pos %d doesn't point to the first entry of %s in index",
  641. pos, name.buf);
  642. strbuf_release(&name);
  643. return pos;
  644. }
  645. /*
  646. * Fast path if we detect that all trees are the same as cache-tree at this
  647. * path. We'll walk these trees in an iterative loop using cache-tree/index
  648. * instead of ODB since we already know what these trees contain.
  649. */
  650. static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
  651. struct traverse_info *info)
  652. {
  653. struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
  654. struct unpack_trees_options *o = info->data;
  655. struct cache_entry *tree_ce = NULL;
  656. int ce_len = 0;
  657. int i, d;
  658. if (!o->merge)
  659. BUG("We need cache-tree to do this optimization");
  660. /*
  661. * Do what unpack_callback() and unpack_nondirectories() normally
  662. * do. But we walk all paths in an iterative loop instead.
  663. *
  664. * D/F conflicts and higher stage entries are not a concern
  665. * because cache-tree would be invalidated and we would never
  666. * get here in the first place.
  667. */
  668. for (i = 0; i < nr_entries; i++) {
  669. int new_ce_len, len, rc;
  670. src[0] = o->src_index->cache[pos + i];
  671. len = ce_namelen(src[0]);
  672. new_ce_len = cache_entry_size(len);
  673. if (new_ce_len > ce_len) {
  674. new_ce_len <<= 1;
  675. tree_ce = xrealloc(tree_ce, new_ce_len);
  676. memset(tree_ce, 0, new_ce_len);
  677. ce_len = new_ce_len;
  678. tree_ce->ce_flags = create_ce_flags(0);
  679. for (d = 1; d <= nr_names; d++)
  680. src[d] = tree_ce;
  681. }
  682. tree_ce->ce_mode = src[0]->ce_mode;
  683. tree_ce->ce_namelen = len;
  684. oidcpy(&tree_ce->oid, &src[0]->oid);
  685. memcpy(tree_ce->name, src[0]->name, len + 1);
  686. rc = call_unpack_fn((const struct cache_entry * const *)src, o);
  687. if (rc < 0) {
  688. free(tree_ce);
  689. return rc;
  690. }
  691. mark_ce_used(src[0], o);
  692. }
  693. free(tree_ce);
  694. if (o->debug_unpack)
  695. printf("Unpacked %d entries from %s to %s using cache-tree\n",
  696. nr_entries,
  697. o->src_index->cache[pos]->name,
  698. o->src_index->cache[pos + nr_entries - 1]->name);
  699. return 0;
  700. }
  701. static int traverse_trees_recursive(int n, unsigned long dirmask,
  702. unsigned long df_conflicts,
  703. struct name_entry *names,
  704. struct traverse_info *info)
  705. {
  706. struct unpack_trees_options *o = info->data;
  707. int i, ret, bottom;
  708. int nr_buf = 0;
  709. struct tree_desc t[MAX_UNPACK_TREES];
  710. void *buf[MAX_UNPACK_TREES];
  711. struct traverse_info newinfo;
  712. struct name_entry *p;
  713. int nr_entries;
  714. nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);
  715. if (nr_entries > 0) {
  716. int pos = index_pos_by_traverse_info(names, info);
  717. if (!o->merge || df_conflicts)
  718. BUG("Wrong condition to get here buddy");
  719. /*
  720. * All entries up to 'pos' must have been processed
  721. * (i.e. marked CE_UNPACKED) at this point. But to be safe,
  722. * save and restore cache_bottom anyway to not miss
  723. * unprocessed entries before 'pos'.
  724. */
  725. bottom = o->cache_bottom;
  726. ret = traverse_by_cache_tree(pos, nr_entries, n, info);
  727. o->cache_bottom = bottom;
  728. return ret;
  729. }
  730. p = names;
  731. while (!p->mode)
  732. p++;
  733. newinfo = *info;
  734. newinfo.prev = info;
  735. newinfo.pathspec = info->pathspec;
  736. newinfo.name = p->path;
  737. newinfo.namelen = p->pathlen;
  738. newinfo.mode = p->mode;
  739. newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1);
  740. newinfo.df_conflicts |= df_conflicts;
  741. /*
  742. * Fetch the tree from the ODB for each peer directory in the
  743. * n commits.
  744. *
  745. * For 2- and 3-way traversals, we try to avoid hitting the
  746. * ODB twice for the same OID. This should yield a nice speed
  747. * up in checkouts and merges when the commits are similar.
  748. *
  749. * We don't bother doing the full O(n^2) search for larger n,
  750. * because wider traversals don't happen that often and we
  751. * avoid the search setup.
  752. *
  753. * When 2 peer OIDs are the same, we just copy the tree
  754. * descriptor data. This implicitly borrows the buffer
  755. * data from the earlier cell.
  756. */
  757. for (i = 0; i < n; i++, dirmask >>= 1) {
  758. if (i > 0 && are_same_oid(&names[i], &names[i - 1]))
  759. t[i] = t[i - 1];
  760. else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))
  761. t[i] = t[i - 2];
  762. else {
  763. const struct object_id *oid = NULL;
  764. if (dirmask & 1)
  765. oid = &names[i].oid;
  766. buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid);
  767. }
  768. }
  769. bottom = switch_cache_bottom(&newinfo);
  770. ret = traverse_trees(o->src_index, n, t, &newinfo);
  771. restore_cache_bottom(&newinfo, bottom);
  772. for (i = 0; i < nr_buf; i++)
  773. free(buf[i]);
  774. return ret;
  775. }
  776. /*
  777. * Compare the traverse-path to the cache entry without actually
  778. * having to generate the textual representation of the traverse
  779. * path.
  780. *
  781. * NOTE! This *only* compares up to the size of the traverse path
  782. * itself - the caller needs to do the final check for the cache
  783. * entry having more data at the end!
  784. */
  785. static int do_compare_entry_piecewise(const struct cache_entry *ce,
  786. const struct traverse_info *info,
  787. const char *name, size_t namelen,
  788. unsigned mode)
  789. {
  790. int pathlen, ce_len;
  791. const char *ce_name;
  792. if (info->prev) {
  793. int cmp = do_compare_entry_piecewise(ce, info->prev,
  794. info->name, info->namelen,
  795. info->mode);
  796. if (cmp)
  797. return cmp;
  798. }
  799. pathlen = info->pathlen;
  800. ce_len = ce_namelen(ce);
  801. /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
  802. if (ce_len < pathlen)
  803. return -1;
  804. ce_len -= pathlen;
  805. ce_name = ce->name + pathlen;
  806. return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
  807. }
  808. static int do_compare_entry(const struct cache_entry *ce,
  809. const struct traverse_info *info,
  810. const char *name, size_t namelen,
  811. unsigned mode)
  812. {
  813. int pathlen, ce_len;
  814. const char *ce_name;
  815. int cmp;
  816. /*
  817. * If we have not precomputed the traverse path, it is quicker
  818. * to avoid doing so. But if we have precomputed it,
  819. * it is quicker to use the precomputed version.
  820. */
  821. if (!info->traverse_path)
  822. return do_compare_entry_piecewise(ce, info, name, namelen, mode);
  823. cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
  824. if (cmp)
  825. return cmp;
  826. pathlen = info->pathlen;
  827. ce_len = ce_namelen(ce);
  828. if (ce_len < pathlen)
  829. return -1;
  830. ce_len -= pathlen;
  831. ce_name = ce->name + pathlen;
  832. return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
  833. }
  834. static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
  835. {
  836. int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode);
  837. if (cmp)
  838. return cmp;
  839. /*
  840. * Even if the beginning compared identically, the ce should
  841. * compare as bigger than a directory leading up to it!
  842. */
  843. return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));
  844. }
  845. static int ce_in_traverse_path(const struct cache_entry *ce,
  846. const struct traverse_info *info)
  847. {
  848. if (!info->prev)
  849. return 1;
  850. if (do_compare_entry(ce, info->prev,
  851. info->name, info->namelen, info->mode))
  852. return 0;
  853. /*
  854. * If ce (blob) is the same name as the path (which is a tree
  855. * we will be descending into), it won't be inside it.
  856. */
  857. return (info->pathlen < ce_namelen(ce));
  858. }
  859. static struct cache_entry *create_ce_entry(const struct traverse_info *info,
  860. const struct name_entry *n,
  861. int stage,
  862. struct index_state *istate,
  863. int is_transient)
  864. {
  865. size_t len = traverse_path_len(info, tree_entry_len(n));
  866. struct cache_entry *ce =
  867. is_transient ?
  868. make_empty_transient_cache_entry(len) :
  869. make_empty_cache_entry(istate, len);
  870. ce->ce_mode = create_ce_mode(n->mode);
  871. ce->ce_flags = create_ce_flags(stage);
  872. ce->ce_namelen = len;
  873. oidcpy(&ce->oid, &n->oid);
  874. /* len+1 because the cache_entry allocates space for NUL */
  875. make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen);
  876. return ce;
  877. }
  878. /*
  879. * Note that traverse_by_cache_tree() duplicates some logic in this function
  880. * without actually calling it. If you change the logic here you may need to
  881. * check and change there as well.
  882. */
  883. static int unpack_nondirectories(int n, unsigned long mask,
  884. unsigned long dirmask,
  885. struct cache_entry **src,
  886. const struct name_entry *names,
  887. const struct traverse_info *info)
  888. {
  889. int i;
  890. struct unpack_trees_options *o = info->data;
  891. unsigned long conflicts = info->df_conflicts | dirmask;
  892. /* Do we have *only* directories? Nothing to do */
  893. if (mask == dirmask && !src[0])
  894. return 0;
  895. /*
  896. * Ok, we've filled in up to any potential index entry in src[0],
  897. * now do the rest.
  898. */
  899. for (i = 0; i < n; i++) {
  900. int stage;
  901. unsigned int bit = 1ul << i;
  902. if (conflicts & bit) {
  903. src[i + o->merge] = o->df_conflict_entry;
  904. continue;
  905. }
  906. if (!(mask & bit))
  907. continue;
  908. if (!o->merge)
  909. stage = 0;
  910. else if (i + 1 < o->head_idx)
  911. stage = 1;
  912. else if (i + 1 > o->head_idx)
  913. stage = 3;
  914. else
  915. stage = 2;
  916. /*
  917. * If the merge bit is set, then the cache entries are
  918. * discarded in the following block. In this case,
  919. * construct "transient" cache_entries, as they are
  920. * not stored in the index. otherwise construct the
  921. * cache entry from the index aware logic.
  922. */
  923. src[i + o->merge] = create_ce_entry(info, names + i, stage, &o->result, o->merge);
  924. }
  925. if (o->merge) {
  926. int rc = call_unpack_fn((const struct cache_entry * const *)src,
  927. o);
  928. for (i = 0; i < n; i++) {
  929. struct cache_entry *ce = src[i + o->merge];
  930. if (ce != o->df_conflict_entry)
  931. discard_cache_entry(ce);
  932. }
  933. return rc;
  934. }
  935. for (i = 0; i < n; i++)
  936. if (src[i] && src[i] != o->df_conflict_entry)
  937. if (do_add_entry(o, src[i], 0, 0))
  938. return -1;
  939. return 0;
  940. }
  941. static int unpack_failed(struct unpack_trees_options *o, const char *message)
  942. {
  943. discard_index(&o->result);
  944. if (!o->quiet && !o->exiting_early) {
  945. if (message)
  946. return error("%s", message);
  947. return -1;
  948. }
  949. return -1;
  950. }
  951. /*
  952. * The tree traversal is looking at name p. If we have a matching entry,
  953. * return it. If name p is a directory in the index, do not return
  954. * anything, as we will want to match it when the traversal descends into
  955. * the directory.
  956. */
  957. static int find_cache_pos(struct traverse_info *info,
  958. const char *p, size_t p_len)
  959. {
  960. int pos;
  961. struct unpack_trees_options *o = info->data;
  962. struct index_state *index = o->src_index;
  963. int pfxlen = info->pathlen;
  964. for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
  965. const struct cache_entry *ce = index->cache[pos];
  966. const char *ce_name, *ce_slash;
  967. int cmp, ce_len;
  968. if (ce->ce_flags & CE_UNPACKED) {
  969. /*
  970. * cache_bottom entry is already unpacked, so
  971. * we can never match it; don't check it
  972. * again.
  973. */
  974. if (pos == o->cache_bottom)
  975. ++o->cache_bottom;
  976. continue;
  977. }
  978. if (!ce_in_traverse_path(ce, info)) {
  979. /*
  980. * Check if we can skip future cache checks
  981. * (because we're already past all possible
  982. * entries in the traverse path).
  983. */
  984. if (info->traverse_path) {
  985. if (strncmp(ce->name, info->traverse_path,
  986. info->pathlen) > 0)
  987. break;
  988. }
  989. continue;
  990. }
  991. ce_name = ce->name + pfxlen;
  992. ce_slash = strchr(ce_name, '/');
  993. if (ce_slash)
  994. ce_len = ce_slash - ce_name;
  995. else
  996. ce_len = ce_namelen(ce) - pfxlen;
  997. cmp = name_compare(p, p_len, ce_name, ce_len);
  998. /*
  999. * Exact match; if we have a directory we need to
  1000. * delay returning it.
  1001. */
  1002. if (!cmp)
  1003. return ce_slash ? -2 - pos : pos;
  1004. if (0 < cmp)
  1005. continue; /* keep looking */
  1006. /*
  1007. * ce_name sorts after p->path; could it be that we
  1008. * have files under p->path directory in the index?
  1009. * E.g. ce_name == "t-i", and p->path == "t"; we may
  1010. * have "t/a" in the index.
  1011. */
  1012. if (p_len < ce_len && !memcmp(ce_name, p, p_len) &&
  1013. ce_name[p_len] < '/')
  1014. continue; /* keep looking */
  1015. break;
  1016. }
  1017. return -1;
  1018. }
  1019. static struct cache_entry *find_cache_entry(struct traverse_info *info,
  1020. const struct name_entry *p)
  1021. {
  1022. int pos = find_cache_pos(info, p->path, p->pathlen);
  1023. struct unpack_trees_options *o = info->data;
  1024. if (0 <= pos)
  1025. return o->src_index->cache[pos];
  1026. else
  1027. return NULL;
  1028. }
  1029. static void debug_path(struct traverse_info *info)
  1030. {
  1031. if (info->prev) {
  1032. debug_path(info->prev);
  1033. if (*info->prev->name)
  1034. putchar('/');
  1035. }
  1036. printf("%s", info->name);
  1037. }
  1038. static void debug_name_entry(int i, struct name_entry *n)
  1039. {
  1040. printf("ent#%d %06o %s\n", i,
  1041. n->path ? n->mode : 0,
  1042. n->path ? n->path : "(missing)");
  1043. }
  1044. static void debug_unpack_callback(int n,
  1045. unsigned long mask,
  1046. unsigned long dirmask,
  1047. struct name_entry *names,
  1048. struct traverse_info *info)
  1049. {
  1050. int i;
  1051. printf("* unpack mask %lu, dirmask %lu, cnt %d ",
  1052. mask, dirmask, n);
  1053. debug_path(info);
  1054. putchar('\n');
  1055. for (i = 0; i < n; i++)
  1056. debug_name_entry(i, names + i);
  1057. }
  1058. /*
  1059. * Note that traverse_by_cache_tree() duplicates some logic in this function
  1060. * without actually calling it. If you change the logic here you may need to
  1061. * check and change there as well.
  1062. */
  1063. static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
  1064. {
  1065. struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
  1066. struct unpack_trees_options *o = info->data;
  1067. const struct name_entry *p = names;
  1068. /* Find first entry with a real name (we could use "mask" too) */
  1069. while (!p->mode)
  1070. p++;
  1071. if (o->debug_unpack)
  1072. debug_unpack_callback(n, mask, dirmask, names, info);
  1073. /* Are we supposed to look at the index too? */
  1074. if (o->merge) {
  1075. while (1) {
  1076. int cmp;
  1077. struct cache_entry *ce;
  1078. if (o->diff_index_cached)
  1079. ce = next_cache_entry(o);
  1080. else
  1081. ce = find_cache_entry(info, p);
  1082. if (!ce)
  1083. break;
  1084. cmp = compare_entry(ce, info, p);
  1085. if (cmp < 0) {
  1086. if (unpack_index_entry(ce, o) < 0)
  1087. return unpack_failed(o, NULL);
  1088. continue;
  1089. }
  1090. if (!cmp) {
  1091. if (ce_stage(ce)) {
  1092. /*
  1093. * If we skip unmerged index
  1094. * entries, we'll skip this
  1095. * entry *and* the tree
  1096. * entries associated with it!
  1097. */
  1098. if (o->skip_unmerged) {
  1099. add_same_unmerged(ce, o);
  1100. return mask;
  1101. }
  1102. }
  1103. src[0] = ce;
  1104. }
  1105. break;
  1106. }
  1107. }
  1108. if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
  1109. return -1;
  1110. if (o->merge && src[0]) {
  1111. if (ce_stage(src[0]))
  1112. mark_ce_used_same_name(src[0], o);
  1113. else
  1114. mark_ce_used(src[0], o);
  1115. }
  1116. /* Now handle any directories.. */
  1117. if (dirmask) {
  1118. /* special case: "diff-index --cached" looking at a tree */
  1119. if (o->diff_index_cached &&
  1120. n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
  1121. int matches;
  1122. matches = cache_tree_matches_traversal(o->src_index->cache_tree,
  1123. names, info);
  1124. /*
  1125. * Everything under the name matches; skip the
  1126. * entire hierarchy. diff_index_cached codepath
  1127. * special cases D/F conflicts in such a way that
  1128. * it does not do any look-ahead, so this is safe.
  1129. */
  1130. if (matches) {
  1131. o->cache_bottom += matches;
  1132. return mask;
  1133. }
  1134. }
  1135. if (traverse_trees_recursive(n, dirmask, mask & ~dirmask,
  1136. names, info) < 0)
  1137. return -1;
  1138. return mask;
  1139. }
  1140. return mask;
  1141. }
  1142. static int clear_ce_flags_1(struct index_state *istate,
  1143. struct cache_entry **cache, int nr,
  1144. struct strbuf *prefix,
  1145. int select_mask, int clear_mask,
  1146. struct pattern_list *pl,
  1147. enum pattern_match_result default_match,
  1148. int progress_nr);
  1149. /* Whole directory matching */
  1150. static int clear_ce_flags_dir(struct index_state *istate,
  1151. struct cache_entry **cache, int nr,
  1152. struct strbuf *prefix,
  1153. char *basename,
  1154. int select_mask, int clear_mask,
  1155. struct pattern_list *pl,
  1156. enum pattern_match_result default_match,
  1157. int progress_nr)
  1158. {
  1159. struct cache_entry **cache_end;
  1160. int dtype = DT_DIR;
  1161. int rc;
  1162. enum pattern_match_result ret, orig_ret;
  1163. orig_ret = path_matches_pattern_list(prefix->buf, prefix->len,
  1164. basename, &dtype, pl, istate);
  1165. strbuf_addch(prefix, '/');
  1166. /* If undecided, use matching result of parent dir in defval */
  1167. if (orig_ret == UNDECIDED)
  1168. ret = default_match;
  1169. else
  1170. ret = orig_ret;
  1171. for (cache_end = cache; cache_end != cache + nr; cache_end++) {
  1172. struct cache_entry *ce = *cache_end;
  1173. if (strncmp(ce->name, prefix->buf, prefix->len))
  1174. break;
  1175. }
  1176. if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {
  1177. struct cache_entry **ce = cache;
  1178. rc = cache_end - cache;
  1179. while (ce < cache_end) {
  1180. (*ce)->ce_flags &= ~clear_mask;
  1181. ce++;
  1182. }
  1183. } else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) {
  1184. rc = cache_end - cache;
  1185. } else {
  1186. rc = clear_ce_flags_1(istate, cache, cache_end - cache,
  1187. prefix,
  1188. select_mask, clear_mask,
  1189. pl, ret,
  1190. progress_nr);
  1191. }
  1192. strbuf_setlen(prefix, prefix->len - 1);
  1193. return rc;
  1194. }
  1195. /*
  1196. * Traverse the index, find every entry that matches according to
  1197. * o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
  1198. * number of traversed entries.
  1199. *
  1200. * If select_mask is non-zero, only entries whose ce_flags has on of
  1201. * those bits enabled are traversed.
  1202. *
  1203. * cache : pointer to an index entry
  1204. * prefix_len : an offset to its path
  1205. *
  1206. * The current path ("prefix") including the trailing '/' is
  1207. * cache[0]->name[0..(prefix_len-1)]
  1208. * Top level path has prefix_len zero.
  1209. */
  1210. static int clear_ce_flags_1(struct index_state *istate,
  1211. struct cache_entry **cache, int nr,
  1212. struct strbuf *prefix,
  1213. int select_mask, int clear_mask,
  1214. struct pattern_list *pl,
  1215. enum pattern_match_result default_match,
  1216. int progress_nr)
  1217. {
  1218. struct cache_entry **cache_end = nr ? cache + nr : cache;
  1219. /*
  1220. * Process all entries that have the given prefix and meet
  1221. * select_mask condition
  1222. */
  1223. while(cache != cache_end) {
  1224. struct cache_entry *ce = *cache;
  1225. const char *name, *slash;
  1226. int len, dtype;
  1227. enum pattern_match_result ret;
  1228. display_progress(istate->progress, progress_nr);
  1229. if (select_mask && !(ce->ce_flags & select_mask)) {
  1230. cache++;
  1231. progress_nr++;
  1232. continue;
  1233. }
  1234. if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))
  1235. break;
  1236. name = ce->name + prefix->len;
  1237. slash = strchr(name, '/');
  1238. /* If it's a directory, try whole directory match first */
  1239. if (slash) {
  1240. int processed;
  1241. len = slash - name;
  1242. strbuf_add(prefix, name, len);
  1243. processed = clear_ce_flags_dir(istate, cache, cache_end - cache,
  1244. prefix,
  1245. prefix->buf + prefix->len - len,
  1246. select_mask, clear_mask,
  1247. pl, default_match,
  1248. progress_nr);
  1249. /* clear_c_f_dir eats a whole dir already? */
  1250. if (processed) {
  1251. cache += processed;
  1252. progress_nr += processed;
  1253. strbuf_setlen(prefix, prefix->len - len);
  1254. continue;
  1255. }
  1256. strbuf_addch(prefix, '/');
  1257. processed = clear_ce_flags_1(istate, cache, cache_end - cache,
  1258. prefix,
  1259. select_mask, clear_mask, pl,
  1260. default_match, progress_nr);
  1261. cache += processed;
  1262. progress_nr += processed;
  1263. strbuf_setlen(prefix, prefix->len - len - 1);
  1264. continue;
  1265. }
  1266. /* Non-directory */
  1267. dtype = ce_to_dtype(ce);
  1268. ret = path_matches_pattern_list(ce->name,
  1269. ce_namelen(ce),
  1270. name, &dtype, pl, istate);
  1271. if (ret == UNDECIDED)
  1272. ret = default_match;
  1273. if (ret == MATCHED || ret == MATCHED_RECURSIVE)
  1274. ce->ce_flags &= ~clear_mask;
  1275. cache++;
  1276. progress_nr++;
  1277. }
  1278. display_progress(istate->progress, progress_nr);
  1279. return nr - (cache_end - cache);
  1280. }
  1281. static int clear_ce_flags(struct index_state *istate,
  1282. int select_mask, int clear_mask,
  1283. struct pattern_list *pl,
  1284. int show_progress)
  1285. {
  1286. static struct strbuf prefix = STRBUF_INIT;
  1287. char label[100];
  1288. int rval;
  1289. strbuf_reset(&prefix);
  1290. if (show_progress)
  1291. istate->progress = start_delayed_progress(
  1292. _("Updating index flags"),
  1293. istate->cache_nr);
  1294. xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)",
  1295. (unsigned long)select_mask, (unsigned long)clear_mask);
  1296. trace2_region_enter("unpack_trees", label, the_repository);
  1297. rval = clear_ce_flags_1(istate,
  1298. istate->cache,
  1299. istate->cache_nr,
  1300. &prefix,
  1301. select_mask, clear_mask,
  1302. pl, 0, 0);
  1303. trace2_region_leave("unpack_trees", label, the_repository);
  1304. stop_progress(&istate->progress);
  1305. return rval;
  1306. }
  1307. /*
  1308. * Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
  1309. */
  1310. static void mark_new_skip_worktree(struct pattern_list *pl,
  1311. struct index_state *istate,
  1312. int select_flag, int skip_wt_flag,
  1313. int show_progress)
  1314. {
  1315. int i;
  1316. /*
  1317. * 1. Pretend the narrowest worktree: only unmerged entries
  1318. * are checked out
  1319. */
  1320. for (i = 0; i < istate->cache_nr; i++) {
  1321. struct cache_entry *ce = istate->cache[i];
  1322. if (select_flag && !(ce->ce_flags & select_flag))
  1323. continue;
  1324. if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))
  1325. ce->ce_flags |= skip_wt_flag;
  1326. else
  1327. ce->ce_flags &= ~skip_wt_flag;
  1328. }
  1329. /*
  1330. * 2. Widen worktree according to sparse-checkout file.
  1331. * Matched entries will have skip_wt_flag cleared (i.e. "in")
  1332. */
  1333. clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress);
  1334. }
  1335. static void populate_from_existing_patterns(struct unpack_trees_options *o,
  1336. struct pattern_list *pl)
  1337. {
  1338. char *sparse = git_pathdup("info/sparse-checkout");
  1339. pl->use_cone_patterns = core_sparse_checkout_cone;
  1340. if (add_patterns_from_file_to_list(sparse, "", 0, pl, NULL) < 0)
  1341. o->skip_sparse_checkout = 1;
  1342. else
  1343. o->pl = pl;
  1344. free(sparse);
  1345. }
  1346. static int verify_absent(const struct cache_entry *,
  1347. enum unpack_trees_error_types,
  1348. struct unpack_trees_options *);
  1349. /*
  1350. * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
  1351. * resulting index, -2 on failure to reflect the changes to the work tree.
  1352. *
  1353. * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
  1354. */
  1355. int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
  1356. {
  1357. int i, ret;
  1358. static struct cache_entry *dfc;
  1359. struct pattern_list pl;
  1360. int free_pattern_list = 0;
  1361. if (len > MAX_UNPACK_TREES)
  1362. die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
  1363. trace_performance_enter();
  1364. if (!core_apply_sparse_checkout || !o->update)
  1365. o->skip_sparse_checkout = 1;
  1366. if (!o->skip_sparse_checkout && !o->pl) {
  1367. memset(&pl, 0, sizeof(pl));
  1368. free_pattern_list = 1;
  1369. populate_from_existing_patterns(o, &pl);
  1370. }
  1371. memset(&o->result, 0, sizeof(o->result));
  1372. o->result.initialized = 1;
  1373. o->result.timestamp.sec = o->src_index->timestamp.sec;
  1374. o->result.timestamp.nsec = o->src_index->timestamp.nsec;
  1375. o->result.version = o->src_index->version;
  1376. if (!o->src_index->split_index) {
  1377. o->result.split_index = NULL;
  1378. } else if (o->src_index == o->dst_index) {
  1379. /*
  1380. * o->dst_index (and thus o->src_index) will be discarded
  1381. * and overwritten with o->result at the end of this function,
  1382. * so just use src_index's split_index to avoid having to
  1383. * create a new one.
  1384. */
  1385. o->result.split_index = o->src_index->split_index;
  1386. o->result.split_index->refcount++;
  1387. } else {
  1388. o->result.split_index = init_split_index(&o->result);
  1389. }
  1390. oidcpy(&o->result.oid, &o->src_index->oid);
  1391. o->merge_size = len;
  1392. mark_all_ce_unused(o->src_index);
  1393. if (o->src_index->fsmonitor_last_update)
  1394. o->result.fsmonitor_last_update = o->src_index->fsmonitor_last_update;
  1395. /*
  1396. * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
  1397. */
  1398. if (!o->skip_sparse_checkout)
  1399. mark_new_skip_worktree(o->pl, o->src_index, 0,
  1400. CE_NEW_SKIP_WORKTREE, o->verbose_update);
  1401. if (!dfc)
  1402. dfc = xcalloc(1, cache_entry_size(0));
  1403. o->df_conflict_entry = dfc;
  1404. if (len) {
  1405. const char *prefix = o->prefix ? o->prefix : "";
  1406. struct traverse_info info;
  1407. setup_traverse_info(&info, prefix);
  1408. info.fn = unpack_callback;
  1409. info.data = o;
  1410. info.show_all_errors = o->show_all_errors;
  1411. info.pathspec = o->pathspec;
  1412. if (o->prefix) {
  1413. /*
  1414. * Unpack existing index entries that sort before the
  1415. * prefix the tree is spliced into. Note that o->merge
  1416. * is always true in this case.
  1417. */
  1418. while (1) {
  1419. struct cache_entry *ce = next_cache_entry(o);
  1420. if (!ce)
  1421. break;
  1422. if (ce_in_traverse_path(ce, &info))
  1423. break;
  1424. if (unpack_index_entry(ce, o) < 0)
  1425. goto return_failed;
  1426. }
  1427. }
  1428. trace_performance_enter();
  1429. ret = traverse_trees(o->src_index, len, t, &info);
  1430. trace_performance_leave("traverse_trees");
  1431. if (ret < 0)
  1432. goto return_failed;
  1433. }
  1434. /* Any left-over entries in the index? */
  1435. if (o->merge) {
  1436. while (1) {
  1437. struct cache_entry *ce = next_cache_entry(o);
  1438. if (!ce)
  1439. break;
  1440. if (unpack_index_entry(ce, o) < 0)
  1441. goto return_failed;
  1442. }
  1443. }
  1444. mark_all_ce_unused(o->src_index);
  1445. if (o->trivial_merges_only && o->nontrivial_merge) {
  1446. ret = unpack_failed(o, "Merge requires file-level merging");
  1447. goto done;
  1448. }
  1449. if (!o->skip_sparse_checkout) {
  1450. /*
  1451. * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
  1452. * If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
  1453. * so apply_sparse_checkout() won't attempt to remove it from worktree
  1454. */
  1455. mark_new_skip_worktree(o->pl, &o->result,
  1456. CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE,
  1457. o->verbose_update);
  1458. ret = 0;
  1459. for (i = 0; i < o->result.cache_nr; i++) {
  1460. struct cache_entry *ce = o->result.cache[i];
  1461. /*
  1462. * Entries marked with CE_ADDED in merged_entry() do not have
  1463. * verify_absent() check (the check is effectively disabled
  1464. * because CE_NEW_SKIP_WORKTREE is set unconditionally).
  1465. *
  1466. * Do the real check now because we have had
  1467. * correct CE_NEW_SKIP_WORKTREE
  1468. */
  1469. if (ce->ce_flags & CE_ADDED &&
  1470. verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
  1471. ret = 1;
  1472. if (apply_sparse_checkout(&o->result, ce, o))
  1473. ret = 1;
  1474. }
  1475. if (ret == 1) {
  1476. /*
  1477. * Inability to sparsify or de-sparsify individual
  1478. * paths is not an error, but just a warning.
  1479. */
  1480. if (o->show_all_errors)
  1481. display_warning_msgs(o);
  1482. ret = 0;
  1483. }
  1484. }
  1485. ret = check_updates(o, &o->result) ? (-2) : 0;
  1486. if (o->dst_index) {
  1487. move_index_extensions(&o->result, o->src_index);
  1488. if (!ret) {
  1489. if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
  1490. cache_tree_verify(the_repository, &o->result);
  1491. if (!o->result.cache_tree)
  1492. o->result.cache_tree = cache_tree();
  1493. if (!cache_tree_fully_valid(o->result.cache_tree))
  1494. cache_tree_update(&o->result,
  1495. WRITE_TREE_SILENT |
  1496. WRITE_TREE_REPAIR);
  1497. }
  1498. o->result.updated_workdir = 1;
  1499. discard_index(o->dst_index);
  1500. *o->dst_index = o->result;
  1501. } else {
  1502. discard_index(&o->result);
  1503. }
  1504. o->src_index = NULL;
  1505. done:
  1506. if (free_pattern_list)
  1507. clear_pattern_list(&pl);
  1508. trace_performance_leave("unpack_trees");
  1509. return ret;
  1510. return_failed:
  1511. if (o->show_all_errors)
  1512. display_error_msgs(o);
  1513. mark_all_ce_unused(o->src_index);
  1514. ret = unpack_failed(o, NULL);
  1515. if (o->exiting_early)
  1516. ret = 0;
  1517. goto done;
  1518. }
  1519. /*
  1520. * Update SKIP_WORKTREE bits according to sparsity patterns, and update
  1521. * working directory to match.
  1522. *
  1523. * CE_NEW_SKIP_WORKTREE is used internally.
  1524. */
  1525. enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
  1526. {
  1527. enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
  1528. struct pattern_list pl;
  1529. int i;
  1530. unsigned old_show_all_errors;
  1531. int free_pattern_list = 0;
  1532. old_show_all_errors = o->show_all_errors;
  1533. o->show_all_errors = 1;
  1534. /* Sanity checks */
  1535. if (!o->update || o->index_only || o->skip_sparse_checkout)
  1536. BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
  1537. if (o->src_index != o->dst_index || o->fn)
  1538. BUG("update_sparsity() called wrong");
  1539. trace_performance_enter();
  1540. /* If we weren't given patterns, use the recorded ones */
  1541. if (!o->pl) {
  1542. memset(&pl, 0, sizeof(pl));
  1543. free_pattern_list = 1;
  1544. populate_from_existing_patterns(o, &pl);
  1545. if (o->skip_sparse_checkout)
  1546. goto skip_sparse_checkout;
  1547. }
  1548. /* Set NEW_SKIP_WORKTREE on existing entries. */
  1549. mark_all_ce_unused(o->src_index);
  1550. mark_new_skip_worktree(o->pl, o->src_index, 0,
  1551. CE_NEW_SKIP_WORKTREE, o->verbose_update);
  1552. /* Then loop over entries and update/remove as needed */
  1553. ret = UPDATE_SPARSITY_SUCCESS;
  1554. for (i = 0; i < o->src_index->cache_nr; i++) {
  1555. struct cache_entry *ce = o->src_index->cache[i];
  1556. if (ce_stage(ce)) {
  1557. /* -1 because for loop will increment by 1 */
  1558. i += warn_conflicted_path(o->src_index, i, o) - 1;
  1559. ret = UPDATE_SPARSITY_WARNINGS;
  1560. continue;
  1561. }
  1562. if (apply_sparse_checkout(o->src_index, ce, o))
  1563. ret = UPDATE_SPARSITY_WARNINGS;
  1564. }
  1565. skip_sparse_checkout:
  1566. if (check_updates(o, o->src_index))
  1567. ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
  1568. display_warning_msgs(o);
  1569. o->show_all_errors = old_show_all_errors;
  1570. if (free_pattern_list)
  1571. clear_pattern_list(&pl);
  1572. trace_performance_leave("update_sparsity");
  1573. return ret;
  1574. }
  1575. /* Here come the merge functions */
  1576. static int reject_merge(const struct cache_entry *ce,
  1577. struct unpack_trees_options *o)
  1578. {
  1579. return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
  1580. }
  1581. static int same(const struct cache_entry *a, const struct cache_entry *b)
  1582. {
  1583. if (!!a != !!b)
  1584. return 0;
  1585. if (!a && !b)
  1586. return 1;
  1587. if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
  1588. return 0;
  1589. return a->ce_mode == b->ce_mode &&
  1590. oideq(&a->oid, &b->oid);
  1591. }
  1592. /*
  1593. * When a CE gets turned into an unmerged entry, we
  1594. * want it to be up-to-date
  1595. */
  1596. static int verify_uptodate_1(const struct cache_entry *ce,
  1597. struct unpack_trees_options *o,
  1598. enum unpack_trees_error_types error_type)
  1599. {
  1600. struct stat st;
  1601. if (o->index_only)
  1602. return 0;
  1603. /*
  1604. * CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
  1605. * if this entry is truly up-to-date because this file may be
  1606. * overwritten.
  1607. */
  1608. if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
  1609. ; /* keep checking */
  1610. else if (o->reset || ce_uptodate(ce))
  1611. return 0;
  1612. if (!lstat(ce->name, &st)) {
  1613. int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;
  1614. unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);
  1615. if (submodule_from_ce(ce)) {
  1616. int r = check_submodule_move_head(ce,
  1617. "HEAD", oid_to_hex(&ce->oid), o);
  1618. if (r)
  1619. return add_rejected_path(o, error_type, ce->name);
  1620. return 0;
  1621. }
  1622. if (!changed)
  1623. return 0;
  1624. /*
  1625. * Historic default policy was to allow submodule to be out
  1626. * of sync wrt the superproject index. If the submodule was
  1627. * not considered interesting above, we don't care here.
  1628. */
  1629. if (S_ISGITLINK(ce->ce_mode))
  1630. return 0;
  1631. errno = 0;
  1632. }
  1633. if (errno == ENOENT)
  1634. return 0;
  1635. return add_rejected_path(o, error_type, ce->name);
  1636. }
  1637. int verify_uptodate(const struct cache_entry *ce,
  1638. struct unpack_trees_options *o)
  1639. {
  1640. if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
  1641. return 0;
  1642. return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
  1643. }
  1644. static int verify_uptodate_sparse(const struct cache_entry *ce,
  1645. struct unpack_trees_options *o)
  1646. {
  1647. return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE);
  1648. }
  1649. /*
  1650. * TODO: We should actually invalidate o->result, not src_index [1].
  1651. * But since cache tree and untracked cache both are not copied to
  1652. * o->result until unpacking is complete, we invalidate them on
  1653. * src_index instead with the assumption that they will be copied to
  1654. * dst_index at the end.
  1655. *
  1656. * [1] src_index->cache_tree is also used in unpack_callback() so if
  1657. * we invalidate o->result, we need to update it to use
  1658. * o->result.cache_tree as well.
  1659. */
  1660. static void invalidate_ce_path(const struct cache_entry *ce,
  1661. struct unpack_trees_options *o)
  1662. {
  1663. if (!ce)
  1664. return;
  1665. cache_tree_invalidate_path(o->src_index, ce->name);
  1666. untracked_cache_invalidate_path(o->src_index, ce->name, 1);
  1667. }
  1668. /*
  1669. * Check that checking out ce->sha1 in subdir ce->name is not
  1670. * going to overwrite any working files.
  1671. */
  1672. static int verify_clean_submodule(const char *old_sha1,
  1673. const struct cache_entry *ce,
  1674. struct unpack_trees_options *o)
  1675. {
  1676. if (!submodule_from_ce(ce))
  1677. return 0;
  1678. return check_submodule_move_head(ce, old_sha1,
  1679. oid_to_hex(&ce->oid), o);
  1680. }
  1681. static int verify_clean_subdirectory(const struct cache_entry *ce,
  1682. struct unpack_trees_options *o)
  1683. {
  1684. /*
  1685. * we are about to extract "ce->name"; we would not want to lose
  1686. * anything in the existing directory there.
  1687. */
  1688. int namelen;
  1689. int i;
  1690. struct dir_struct d;
  1691. char *pathbuf;
  1692. int cnt = 0;
  1693. if (S_ISGITLINK(ce->ce_mode)) {
  1694. struct object_id oid;
  1695. int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
  1696. /*
  1697. * If we are not going to update the submodule, then
  1698. * we don't care.
  1699. */
  1700. if (!sub_head && oideq(&oid, &ce->oid))
  1701. return 0;
  1702. return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),
  1703. ce, o);
  1704. }
  1705. /*
  1706. * First let's make sure we do not have a local modification
  1707. * in that directory.
  1708. */
  1709. namelen = ce_namelen(ce);
  1710. for (i = locate_in_src_index(ce, o);
  1711. i < o->src_index->cache_nr;
  1712. i++) {
  1713. struct cache_entry *ce2 = o->src_index->cache[i];
  1714. int len = ce_namelen(ce2);
  1715. if (len < namelen ||
  1716. strncmp(ce->name, ce2->name, namelen) ||
  1717. ce2->name[namelen] != '/')
  1718. break;
  1719. /*
  1720. * ce2->name is an entry in the subdirectory to be
  1721. * removed.
  1722. */
  1723. if (!ce_stage(ce2)) {
  1724. if (verify_uptodate(ce2, o))
  1725. return -1;
  1726. add_entry(o, ce2, CE_REMOVE, 0);
  1727. invalidate_ce_path(ce, o);
  1728. mark_ce_used(ce2, o);
  1729. }
  1730. cnt++;
  1731. }
  1732. /*
  1733. * Then we need to make sure that we do not lose a locally
  1734. * present file that is not ignored.
  1735. */
  1736. pathbuf = xstrfmt("%.*s/", namelen, ce->name);
  1737. memset(&d, 0, sizeof(d));
  1738. if (o->dir)
  1739. d.exclude_per_dir = o->dir->exclude_per_dir;
  1740. i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);
  1741. if (i)
  1742. return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
  1743. free(pathbuf);
  1744. return cnt;
  1745. }
  1746. /*
  1747. * This gets called when there was no index entry for the tree entry 'dst',
  1748. * but we found a file in the working tree that 'lstat()' said was fine,
  1749. * and we're on a case-insensitive filesystem.
  1750. *
  1751. * See if we can find a case-insensitive match in the index that also
  1752. * matches the stat information, and assume it's that other file!
  1753. */
  1754. static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)
  1755. {
  1756. const struct cache_entry *src;
  1757. src = index_file_exists(o->src_index, name, len, 1);
  1758. return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
  1759. }
  1760. static int check_ok_to_remove(const char *name, int len, int dtype,
  1761. const struct cache_entry *ce, struct stat *st,
  1762. enum unpack_trees_error_types error_type,
  1763. struct unpack_trees_options *o)
  1764. {
  1765. const struct cache_entry *result;
  1766. /*
  1767. * It may be that the 'lstat()' succeeded even though
  1768. * target 'ce' was absent, because there is an old
  1769. * entry that is different only in case..
  1770. *
  1771. * Ignore that lstat() if it matches.
  1772. */
  1773. if (ignore_case && icase_exists(o, name, len, st))
  1774. return 0;
  1775. if (o->dir &&
  1776. is_excluded(o->dir, o->src_index, name, &dtype))
  1777. /*
  1778. * ce->name is explicitly excluded, so it is Ok to
  1779. * overwrite it.
  1780. */
  1781. return 0;
  1782. if (S_ISDIR(st->st_mode)) {
  1783. /*
  1784. * We are checking out path "foo" and
  1785. * found "foo/." in the working tree.
  1786. * This is tricky -- if we have modified
  1787. * files that are in "foo/" we would lose
  1788. * them.
  1789. */
  1790. if (verify_clean_subdirectory(ce, o) < 0)
  1791. return -1;
  1792. return 0;
  1793. }
  1794. /*
  1795. * The previous round may already have decided to
  1796. * delete this path, which is in a subdirectory that
  1797. * is being replaced with a blob.
  1798. */
  1799. result = index_file_exists(&o->result, name, len, 0);
  1800. if (result) {
  1801. if (result->ce_flags & CE_REMOVE)
  1802. return 0;
  1803. }
  1804. return add_rejected_path(o, error_type, name);
  1805. }
  1806. /*
  1807. * We do not want to remove or overwrite a working tree file that
  1808. * is not tracked, unless it is ignored.
  1809. */
  1810. static int verify_absent_1(const struct cache_entry *ce,
  1811. enum unpack_trees_error_types error_type,
  1812. struct unpack_trees_options *o)
  1813. {
  1814. int len;
  1815. struct stat st;
  1816. if (o->index_only || o->reset || !o->update)
  1817. return 0;
  1818. len = check_leading_path(ce->name, ce_namelen(ce));
  1819. if (!len)
  1820. return 0;
  1821. else if (len > 0) {
  1822. char *path;
  1823. int ret;
  1824. path = xmemdupz(ce->name, len);
  1825. if (lstat(path, &st))
  1826. ret = error_errno("cannot stat '%s'", path);
  1827. else {
  1828. if (submodule_from_ce(ce))
  1829. ret = check_submodule_move_head(ce,
  1830. oid_to_hex(&ce->oid),
  1831. NULL, o);
  1832. else
  1833. ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
  1834. &st, error_type, o);
  1835. }
  1836. free(path);
  1837. return ret;
  1838. } else if (lstat(ce->name, &st)) {
  1839. if (errno != ENOENT)
  1840. return error_errno("cannot stat '%s'", ce->name);
  1841. return 0;
  1842. } else {
  1843. if (submodule_from_ce(ce))
  1844. return check_submodule_move_head(ce, oid_to_hex(&ce->oid),
  1845. NULL, o);
  1846. return check_ok_to_remove(ce->name, ce_namelen(ce),
  1847. ce_to_dtype(ce), ce, &st,
  1848. error_type, o);
  1849. }
  1850. }
  1851. static int verify_absent(const struct cache_entry *ce,
  1852. enum unpack_trees_error_types error_type,
  1853. struct unpack_trees_options *o)
  1854. {
  1855. if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))
  1856. return 0;
  1857. return verify_absent_1(ce, error_type, o);
  1858. }
  1859. static int verify_absent_sparse(const struct cache_entry *ce,
  1860. enum unpack_trees_error_types error_type,
  1861. struct unpack_trees_options *o)
  1862. {
  1863. return verify_absent_1(ce, error_type, o);
  1864. }
  1865. static int merged_entry(const struct cache_entry *ce,
  1866. const struct cache_entry *old,
  1867. struct unpack_trees_options *o)
  1868. {
  1869. int update = CE_UPDATE;
  1870. struct cache_entry *merge = dup_cache_entry(ce, &o->result);
  1871. if (!old) {
  1872. /*
  1873. * New index entries. In sparse checkout, the following
  1874. * verify_absent() will be delayed until after
  1875. * traverse_trees() finishes in unpack_trees(), then:
  1876. *
  1877. * - CE_NEW_SKIP_WORKTREE will be computed correctly
  1878. * - verify_absent() be called again, this time with
  1879. * correct CE_NEW_SKIP_WORKTREE
  1880. *
  1881. * verify_absent() call here does nothing in sparse
  1882. * checkout (i.e. o->skip_sparse_checkout == 0)
  1883. */
  1884. update |= CE_ADDED;
  1885. merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
  1886. if (verify_absent(merge,
  1887. ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
  1888. discard_cache_entry(merge);
  1889. return -1;
  1890. }
  1891. invalidate_ce_path(merge, o);
  1892. if (submodule_from_ce(ce) && file_exists(ce->name)) {
  1893. int ret = check_submodule_move_head(ce, NULL,
  1894. oid_to_hex(&ce->oid),
  1895. o);
  1896. if (ret)
  1897. return ret;
  1898. }
  1899. } else if (!(old->ce_flags & CE_CONFLICTED)) {
  1900. /*
  1901. * See if we can re-use the old CE directly?
  1902. * That way we get the uptodate stat info.
  1903. *
  1904. * This also removes the UPDATE flag on a match; otherwise
  1905. * we will end up overwriting local changes in the work tree.
  1906. */
  1907. if (same(old, merge)) {
  1908. copy_cache_entry(merge, old);
  1909. update = 0;
  1910. } else {
  1911. if (verify_uptodate(old, o)) {
  1912. discard_cache_entry(merge);
  1913. return -1;
  1914. }
  1915. /* Migrate old flags over */
  1916. update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
  1917. invalidate_ce_path(old, o);
  1918. }
  1919. if (submodule_from_ce(ce) && file_exists(ce->name)) {
  1920. int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),
  1921. oid_to_hex(&ce->oid),
  1922. o);
  1923. if (ret)
  1924. return ret;
  1925. }
  1926. } else {
  1927. /*
  1928. * Previously unmerged entry left as an existence
  1929. * marker by read_index_unmerged();
  1930. */
  1931. invalidate_ce_path(old, o);
  1932. }
  1933. if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0)
  1934. return -1;
  1935. return 1;
  1936. }
  1937. static int deleted_entry(const struct cache_entry *ce,
  1938. const struct cache_entry *old,
  1939. struct unpack_trees_options *o)
  1940. {
  1941. /* Did it exist in the index? */
  1942. if (!old) {
  1943. if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
  1944. return -1;
  1945. return 0;
  1946. }
  1947. if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
  1948. return -1;
  1949. add_entry(o, ce, CE_REMOVE, 0);
  1950. invalidate_ce_path(ce, o);
  1951. return 1;
  1952. }
  1953. static int keep_entry(const struct cache_entry *ce,
  1954. struct unpack_trees_options *o)
  1955. {
  1956. add_entry(o, ce, 0, 0);
  1957. if (ce_stage(ce))
  1958. invalidate_ce_path(ce, o);
  1959. return 1;
  1960. }
  1961. #if DBRT_DEBUG
  1962. static void show_stage_entry(FILE *o,
  1963. const char *label, const struct cache_entry *ce)
  1964. {
  1965. if (!ce)
  1966. fprintf(o, "%s (missing)\n", label);
  1967. else
  1968. fprintf(o, "%s%06o %s %d\t%s\n",
  1969. label,
  1970. ce->ce_mode,
  1971. oid_to_hex(&ce->oid),
  1972. ce_stage(ce),
  1973. ce->name);
  1974. }
  1975. #endif
  1976. int threeway_merge(const struct cache_entry * const *stages,
  1977. struct unpack_trees_options *o)
  1978. {
  1979. const struct cache_entry *index;
  1980. const struct cache_entry *head;
  1981. const struct cache_entry *remote = stages[o->head_idx + 1];
  1982. int count;
  1983. int head_match = 0;
  1984. int remote_match = 0;
  1985. int df_conflict_head = 0;
  1986. int df_conflict_remote = 0;
  1987. int any_anc_missing = 0;
  1988. int no_anc_exists = 1;
  1989. int i;
  1990. for (i = 1; i < o->head_idx; i++) {
  1991. if (!stages[i] || stages[i] == o->df_conflict_entry)
  1992. any_anc_missing = 1;
  1993. else
  1994. no_anc_exists = 0;
  1995. }
  1996. index = stages[0];
  1997. head = stages[o->head_idx];
  1998. if (head == o->df_conflict_entry) {
  1999. df_conflict_head = 1;
  2000. head = NULL;
  2001. }
  2002. if (remote == o->df_conflict_entry) {
  2003. df_conflict_remote = 1;
  2004. remote = NULL;
  2005. }
  2006. /*
  2007. * First, if there's a #16 situation, note that to prevent #13
  2008. * and #14.
  2009. */
  2010. if (!same(remote, head)) {
  2011. for (i = 1; i < o->head_idx; i++) {
  2012. if (same(stages[i], head)) {
  2013. head_match = i;
  2014. }
  2015. if (same(stages[i], remote)) {
  2016. remote_match = i;
  2017. }
  2018. }
  2019. }
  2020. /*
  2021. * We start with cases where the index is allowed to match
  2022. * something other than the head: #14(ALT) and #2ALT, where it
  2023. * is permitted to match the result instead.
  2024. */
  2025. /* #14, #14ALT, #2ALT */
  2026. if (remote && !df_conflict_head && head_match && !remote_match) {
  2027. if (index && !same(index, remote) && !same(index, head))
  2028. return reject_merge(index, o);
  2029. return merged_entry(remote, index, o);
  2030. }
  2031. /*
  2032. * If we have an entry in the index cache, then we want to
  2033. * make sure that it matches head.
  2034. */
  2035. if (index && !same(index, head))
  2036. return reject_merge(index, o);
  2037. if (head) {
  2038. /* #5ALT, #15 */
  2039. if (same(head, remote))
  2040. return merged_entry(head, index, o);
  2041. /* #13, #3ALT */
  2042. if (!df_conflict_remote && remote_match && !head_match)
  2043. return merged_entry(head, index, o);
  2044. }
  2045. /* #1 */
  2046. if (!head && !remote && any_anc_missing)
  2047. return 0;
  2048. /*
  2049. * Under the "aggressive" rule, we resolve mostly trivial
  2050. * cases that we historically had git-merge-one-file resolve.
  2051. */
  2052. if (o->aggressive) {
  2053. int head_deleted = !head;
  2054. int remote_deleted = !remote;
  2055. const struct cache_entry *ce = NULL;
  2056. if (index)
  2057. ce = index;
  2058. else if (head)
  2059. ce = head;
  2060. else if (remote)
  2061. ce = remote;
  2062. else {
  2063. for (i = 1; i < o->head_idx; i++) {
  2064. if (stages[i] && stages[i] != o->df_conflict_entry) {
  2065. ce = stages[i];
  2066. break;
  2067. }
  2068. }
  2069. }
  2070. /*
  2071. * Deleted in both.
  2072. * Deleted in one and unchanged in the other.
  2073. */
  2074. if ((head_deleted && remote_deleted) ||
  2075. (head_deleted && remote && remote_match) ||
  2076. (remote_deleted && head && head_match)) {
  2077. if (index)
  2078. return deleted_entry(index, index, o);
  2079. if (ce && !head_deleted) {
  2080. if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
  2081. return -1;
  2082. }
  2083. return 0;
  2084. }
  2085. /*
  2086. * Added in both, identically.
  2087. */
  2088. if (no_anc_exists && head && remote && same(head, remote))
  2089. return merged_entry(head, index, o);
  2090. }
  2091. /* Below are "no merge" cases, which require that the index be
  2092. * up-to-date to avoid the files getting overwritten with
  2093. * conflict resolution files.
  2094. */
  2095. if (index) {
  2096. if (verify_uptodate(index, o))
  2097. return -1;
  2098. }
  2099. o->nontrivial_merge = 1;
  2100. /* #2, #3, #4, #6, #7, #9, #10, #11. */
  2101. count = 0;
  2102. if (!head_match || !remote_match) {
  2103. for (i = 1; i < o->head_idx; i++) {
  2104. if (stages[i] && stages[i] != o->df_conflict_entry) {
  2105. keep_entry(stages[i], o);
  2106. count++;
  2107. break;
  2108. }
  2109. }
  2110. }
  2111. #if DBRT_DEBUG
  2112. else {
  2113. fprintf(stderr, "read-tree: warning #16 detected\n");
  2114. show_stage_entry(stderr, "head ", stages[head_match]);
  2115. show_stage_entry(stderr, "remote ", stages[remote_match]);
  2116. }
  2117. #endif
  2118. if (head) { count += keep_entry(head, o); }
  2119. if (remote) { count += keep_entry(remote, o); }
  2120. return count;
  2121. }
  2122. /*
  2123. * Two-way merge.
  2124. *
  2125. * The rule is to "carry forward" what is in the index without losing
  2126. * information across a "fast-forward", favoring a successful merge
  2127. * over a merge failure when it makes sense. For details of the
  2128. * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
  2129. *
  2130. */
  2131. int twoway_merge(const struct cache_entry * const *src,
  2132. struct unpack_trees_options *o)
  2133. {
  2134. const struct cache_entry *current = src[0];
  2135. const struct cache_entry *oldtree = src[1];
  2136. const struct cache_entry *newtree = src[2];
  2137. if (o->merge_size != 2)
  2138. return error("Cannot do a twoway merge of %d trees",
  2139. o->merge_size);
  2140. if (oldtree == o->df_conflict_entry)
  2141. oldtree = NULL;
  2142. if (newtree == o->df_conflict_entry)
  2143. newtree = NULL;
  2144. if (current) {
  2145. if (current->ce_flags & CE_CONFLICTED) {
  2146. if (same(oldtree, newtree) || o->reset) {
  2147. if (!newtree)
  2148. return deleted_entry(current, current, o);
  2149. else
  2150. return merged_entry(newtree, current, o);
  2151. }
  2152. return reject_merge(current, o);
  2153. } else if ((!oldtree && !newtree) || /* 4 and 5 */
  2154. (!oldtree && newtree &&
  2155. same(current, newtree)) || /* 6 and 7 */
  2156. (oldtree && newtree &&
  2157. same(oldtree, newtree)) || /* 14 and 15 */
  2158. (oldtree && newtree &&
  2159. !same(oldtree, newtree) && /* 18 and 19 */
  2160. same(current, newtree))) {
  2161. return keep_entry(current, o);
  2162. } else if (oldtree && !newtree && same(current, oldtree)) {
  2163. /* 10 or 11 */
  2164. return deleted_entry(oldtree, current, o);
  2165. } else if (oldtree && newtree &&
  2166. same(current, oldtree) && !same(current, newtree)) {
  2167. /* 20 or 21 */
  2168. return merged_entry(newtree, current, o);
  2169. } else
  2170. return reject_merge(current, o);
  2171. }
  2172. else if (newtree) {
  2173. if (oldtree && !o->initial_checkout) {
  2174. /*
  2175. * deletion of the path was staged;
  2176. */
  2177. if (same(oldtree, newtree))
  2178. return 1;
  2179. return reject_merge(oldtree, o);
  2180. }
  2181. return merged_entry(newtree, current, o);
  2182. }
  2183. return deleted_entry(oldtree, current, o);
  2184. }
  2185. /*
  2186. * Bind merge.
  2187. *
  2188. * Keep the index entries at stage0, collapse stage1 but make sure
  2189. * stage0 does not have anything there.
  2190. */
  2191. int bind_merge(const struct cache_entry * const *src,
  2192. struct unpack_trees_options *o)
  2193. {
  2194. const struct cache_entry *old = src[0];
  2195. const struct cache_entry *a = src[1];
  2196. if (o->merge_size != 1)
  2197. return error("Cannot do a bind merge of %d trees",
  2198. o->merge_size);
  2199. if (a && old)
  2200. return o->quiet ? -1 :
  2201. error(ERRORMSG(o, ERROR_BIND_OVERLAP),
  2202. super_prefixed(a->name),
  2203. super_prefixed(old->name));
  2204. if (!a)
  2205. return keep_entry(old, o);
  2206. else
  2207. return merged_entry(a, NULL, o);
  2208. }
  2209. /*
  2210. * One-way merge.
  2211. *
  2212. * The rule is:
  2213. * - take the stat information from stage0, take the data from stage1
  2214. */
  2215. int oneway_merge(const struct cache_entry * const *src,
  2216. struct unpack_trees_options *o)
  2217. {
  2218. const struct cache_entry *old = src[0];
  2219. const struct cache_entry *a = src[1];
  2220. if (o->merge_size != 1)
  2221. return error("Cannot do a oneway merge of %d trees",
  2222. o->merge_size);
  2223. if (!a || a == o->df_conflict_entry)
  2224. return deleted_entry(old, old, o);
  2225. if (old && same(old, a)) {
  2226. int update = 0;
  2227. if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) &&
  2228. !(old->ce_flags & CE_FSMONITOR_VALID)) {
  2229. struct stat st;
  2230. if (lstat(old->name, &st) ||
  2231. ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
  2232. update |= CE_UPDATE;
  2233. }
  2234. if (o->update && S_ISGITLINK(old->ce_mode) &&
  2235. should_update_submodules() && !verify_uptodate(old, o))
  2236. update |= CE_UPDATE;
  2237. add_entry(o, old, update, CE_STAGEMASK);
  2238. return 0;
  2239. }
  2240. return merged_entry(a, old, o);
  2241. }