unpack-trees: check for missing submodule directory in merged_entry

Using `git checkout --recurse-submodules` to switch between a
branch with no submodules and a branch with initialized nested
submodules currently causes a fatal error:

    $ git checkout --recurse-submodules branch-with-nested-submodules
    fatal: exec '--super-prefix=submodule/nested/': cd to 'nested'
failed: No such file or directory
    error: Submodule 'nested' could not be updated.
    error: Submodule 'submodule/nested' cannot checkout new HEAD.
    error: Submodule 'submodule' could not be updated.
    M	submodule
    Switched to branch 'branch-with-nested-submodules'

The checkout succeeds but the worktree and index of the first level
submodule are left empty:

    $ cd submodule
    $ git -c status.submoduleSummary=1 status
    HEAD detached at b3ce885
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
          deleted:    .gitmodules
          deleted:    first.t
          deleted:    nested

    fatal: not a git repository: 'nested/.git'
    Submodule changes to be committed:

    * nested 1e96f59...0000000:

    $ git ls-files -s
    $ # empty
    $ ls -A
    .git

The reason for the fatal error during the checkout is that a child git
process tries to cd into the yet unexisting nested submodule directory.
The sequence is the following:

1. The main git process (the one running in the superproject) eventually
reaches write_entry() in entry.c, which creates the first level
submodule directory and then calls submodule_move_head() in submodule.c,
which spawns `git read-tree` in the submodule directory.

2. The first child git process (the one in the submodule of the
superproject) eventually calls check_submodule_move_head() at
unpack_trees.c:2021, which calls submodule_move_head in dry-run mode,
which spawns `git read-tree` in the nested submodule directory.

3. The second child git process tries to chdir() in the yet unexisting
nested submodule directory in start_command() at run-command.c:829 and
dies before exec'ing.

The reason why check_submodule_move_head() is reached in the first child
and not in the main process is that it is inside an
if(submodule_from_ce()) construct, and submodule_from_ce() returns a
valid struct submodule pointer, whereas it returns a null pointer in the
main git process.

The reason why submodule_from_ce() returns a null pointer in the main
git process is because the call to cache_lookup_path() in config_from()
(called from submodule_from_path() in submodule_from_ce()) returns a
null pointer since the hashmap "for_path" in the submodule_cache of
the_repository is not yet populated. It is not populated because both
repo_get_oid(repo, GITMODULES_INDEX, &oid) and repo_get_oid(repo,
GITMODULES_HEAD, &oid) in config_from_gitmodules() at
submodule-config.c:639-640 return -1, as at this stage of the operation,
neither the HEAD of the superproject nor its index contain any
.gitmodules file.

In contrast, in the first child the hashmap is populated because
repo_get_oid(repo, GITMODULES_HEAD, &oid) returns 0 as the HEAD of the
first level submodule, i.e. .git/modules/submodule/HEAD, points to a
commit where .gitmodules is present and records 'nested' as a submodule.

Fix this bug by checking that the submodule directory exists before
calling check_submodule_move_head() in merged_entry() in the `if(!old)`
branch, i.e. if going from a commit with no submodule to a commit with a
submodule present.

Also protect the other call to check_submodule_move_head() in
merged_entry() the same way as it is safer, even though the `else if
(!(old->ce_flags & CE_CONFLICTED))` branch of the code is not at play in
the present bug.

The other calls to check_submodule_move_head() in other functions in
unpack_trees.c are all already protected by calls to lstat() somewhere
in
the program flow so we don't need additional protection for them.

All commands in the unpack_trees machinery are affected, i.e. checkout,
reset and read-tree when called with the --recurse-submodules flag.

This bug was first reported in [1].

[1]
https://lore.kernel.org/git/7437BB59-4605-48EC-B05E-E2BDB2D9DABC@gmail.com/

Reported-by: Philippe Blain <levraiphilippeblain@gmail.com>
Reported-by: Damien Robert <damien.olivier.robert@gmail.com>
Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2 files changed
tree: 7dda3c1806d3f5f6bbe6288922621f48a0ce5db9
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. negotiator/
  14. perl/
  15. po/
  16. ppc/
  17. refs/
  18. sha1dc/
  19. sha256/
  20. t/
  21. templates/
  22. trace2/
  23. vcs-svn/
  24. xdiff/
  25. .cirrus.yml
  26. .clang-format
  27. .editorconfig
  28. .gitattributes
  29. .gitignore
  30. .gitmodules
  31. .mailmap
  32. .travis.yml
  33. .tsan-suppressions
  34. abspath.c
  35. aclocal.m4
  36. add-interactive.c
  37. add-interactive.h
  38. add-patch.c
  39. advice.c
  40. advice.h
  41. alias.c
  42. alias.h
  43. alloc.c
  44. alloc.h
  45. apply.c
  46. apply.h
  47. archive-tar.c
  48. archive-zip.c
  49. archive.c
  50. archive.h
  51. argv-array.c
  52. argv-array.h
  53. attr.c
  54. attr.h
  55. azure-pipelines.yml
  56. banned.h
  57. base85.c
  58. bisect.c
  59. bisect.h
  60. blame.c
  61. blame.h
  62. blob.c
  63. blob.h
  64. branch.c
  65. branch.h
  66. builtin.h
  67. bulk-checkin.c
  68. bulk-checkin.h
  69. bundle.c
  70. bundle.h
  71. cache-tree.c
  72. cache-tree.h
  73. cache.h
  74. chdir-notify.c
  75. chdir-notify.h
  76. check-builtins.sh
  77. check_bindir
  78. checkout.c
  79. checkout.h
  80. CODE_OF_CONDUCT.md
  81. color.c
  82. color.h
  83. column.c
  84. column.h
  85. combine-diff.c
  86. command-list.txt
  87. commit-graph.c
  88. commit-graph.h
  89. commit-reach.c
  90. commit-reach.h
  91. commit-slab-decl.h
  92. commit-slab-impl.h
  93. commit-slab.h
  94. commit.c
  95. commit.h
  96. common-main.c
  97. config.c
  98. config.h
  99. config.mak.dev
  100. config.mak.in
  101. config.mak.uname
  102. configure.ac
  103. connect.c
  104. connect.h
  105. connected.c
  106. connected.h
  107. convert.c
  108. convert.h
  109. copy.c
  110. COPYING
  111. credential-cache--daemon.c
  112. credential-cache.c
  113. credential-store.c
  114. credential.c
  115. credential.h
  116. csum-file.c
  117. csum-file.h
  118. ctype.c
  119. daemon.c
  120. date.c
  121. decorate.c
  122. decorate.h
  123. delta-islands.c
  124. delta-islands.h
  125. delta.h
  126. detect-compiler
  127. diff-delta.c
  128. diff-lib.c
  129. diff-no-index.c
  130. diff.c
  131. diff.h
  132. diffcore-break.c
  133. diffcore-delta.c
  134. diffcore-order.c
  135. diffcore-pickaxe.c
  136. diffcore-rename.c
  137. diffcore.h
  138. dir-iterator.c
  139. dir-iterator.h
  140. dir.c
  141. dir.h
  142. editor.c
  143. entry.c
  144. environment.c
  145. exec-cmd.c
  146. exec-cmd.h
  147. fast-import.c
  148. fetch-negotiator.c
  149. fetch-negotiator.h
  150. fetch-pack.c
  151. fetch-pack.h
  152. fmt-merge-msg.h
  153. fsck.c
  154. fsck.h
  155. fsmonitor.c
  156. fsmonitor.h
  157. fuzz-commit-graph.c
  158. fuzz-pack-headers.c
  159. fuzz-pack-idx.c
  160. generate-cmdlist.sh
  161. gettext.c
  162. gettext.h
  163. git-add--interactive.perl
  164. git-archimport.perl
  165. git-bisect.sh
  166. git-compat-util.h
  167. git-cvsexportcommit.perl
  168. git-cvsimport.perl
  169. git-cvsserver.perl
  170. git-difftool--helper.sh
  171. git-filter-branch.sh
  172. git-instaweb.sh
  173. git-legacy-stash.sh
  174. git-merge-octopus.sh
  175. git-merge-one-file.sh
  176. git-merge-resolve.sh
  177. git-mergetool--lib.sh
  178. git-mergetool.sh
  179. git-p4.py
  180. git-parse-remote.sh
  181. git-quiltimport.sh
  182. git-rebase--preserve-merges.sh
  183. git-request-pull.sh
  184. git-send-email.perl
  185. git-sh-i18n.sh
  186. git-sh-setup.sh
  187. git-submodule.sh
  188. git-svn.perl
  189. GIT-VERSION-GEN
  190. git-web--browse.sh
  191. git.c
  192. git.rc
  193. gpg-interface.c
  194. gpg-interface.h
  195. graph.c
  196. graph.h
  197. grep.c
  198. grep.h
  199. hash.h
  200. hashmap.c
  201. hashmap.h
  202. help.c
  203. help.h
  204. hex.c
  205. http-backend.c
  206. http-fetch.c
  207. http-push.c
  208. http-walker.c
  209. http.c
  210. http.h
  211. ident.c
  212. imap-send.c
  213. INSTALL
  214. interdiff.c
  215. interdiff.h
  216. iterator.h
  217. json-writer.c
  218. json-writer.h
  219. khash.h
  220. kwset.c
  221. kwset.h
  222. levenshtein.c
  223. levenshtein.h
  224. LGPL-2.1
  225. line-log.c
  226. line-log.h
  227. line-range.c
  228. line-range.h
  229. linear-assignment.c
  230. linear-assignment.h
  231. list-objects-filter-options.c
  232. list-objects-filter-options.h
  233. list-objects-filter.c
  234. list-objects-filter.h
  235. list-objects.c
  236. list-objects.h
  237. list.h
  238. ll-merge.c
  239. ll-merge.h
  240. lockfile.c
  241. lockfile.h
  242. log-tree.c
  243. log-tree.h
  244. ls-refs.c
  245. ls-refs.h
  246. mailinfo.c
  247. mailinfo.h
  248. mailmap.c
  249. mailmap.h
  250. Makefile
  251. match-trees.c
  252. mem-pool.c
  253. mem-pool.h
  254. merge-blobs.c
  255. merge-blobs.h
  256. merge-recursive.c
  257. merge-recursive.h
  258. merge.c
  259. mergesort.c
  260. mergesort.h
  261. midx.c
  262. midx.h
  263. name-hash.c
  264. notes-cache.c
  265. notes-cache.h
  266. notes-merge.c
  267. notes-merge.h
  268. notes-utils.c
  269. notes-utils.h
  270. notes.c
  271. notes.h
  272. object-store.h
  273. object.c
  274. object.h
  275. oidmap.c
  276. oidmap.h
  277. oidset.c
  278. oidset.h
  279. pack-bitmap-write.c
  280. pack-bitmap.c
  281. pack-bitmap.h
  282. pack-check.c
  283. pack-objects.c
  284. pack-objects.h
  285. pack-revindex.c
  286. pack-revindex.h
  287. pack-write.c
  288. pack.h
  289. packfile.c
  290. packfile.h
  291. pager.c
  292. parse-options-cb.c
  293. parse-options.c
  294. parse-options.h
  295. patch-delta.c
  296. patch-ids.c
  297. patch-ids.h
  298. path.c
  299. path.h
  300. pathspec.c
  301. pathspec.h
  302. pkt-line.c
  303. pkt-line.h
  304. preload-index.c
  305. pretty.c
  306. pretty.h
  307. prio-queue.c
  308. prio-queue.h
  309. progress.c
  310. progress.h
  311. promisor-remote.c
  312. promisor-remote.h
  313. prompt.c
  314. prompt.h
  315. protocol.c
  316. protocol.h
  317. quote.c
  318. quote.h
  319. range-diff.c
  320. range-diff.h
  321. reachable.c
  322. reachable.h
  323. read-cache.c
  324. README.md
  325. rebase-interactive.c
  326. rebase-interactive.h
  327. ref-filter.c
  328. ref-filter.h
  329. reflog-walk.c
  330. reflog-walk.h
  331. refs.c
  332. refs.h
  333. refspec.c
  334. refspec.h
  335. remote-curl.c
  336. remote-testsvn.c
  337. remote.c
  338. remote.h
  339. replace-object.c
  340. replace-object.h
  341. repo-settings.c
  342. repository.c
  343. repository.h
  344. rerere.c
  345. rerere.h
  346. resolve-undo.c
  347. resolve-undo.h
  348. revision.c
  349. revision.h
  350. run-command.c
  351. run-command.h
  352. send-pack.c
  353. send-pack.h
  354. sequencer.c
  355. sequencer.h
  356. serve.c
  357. serve.h
  358. server-info.c
  359. setup.c
  360. sh-i18n--envsubst.c
  361. sha1-array.c
  362. sha1-array.h
  363. sha1-file.c
  364. sha1-lookup.c
  365. sha1-lookup.h
  366. sha1-name.c
  367. sha1dc_git.c
  368. sha1dc_git.h
  369. shallow.c
  370. shell.c
  371. shortlog.h
  372. sideband.c
  373. sideband.h
  374. sigchain.c
  375. sigchain.h
  376. split-index.c
  377. split-index.h
  378. stable-qsort.c
  379. strbuf.c
  380. strbuf.h
  381. streaming.c
  382. streaming.h
  383. string-list.c
  384. string-list.h
  385. sub-process.c
  386. sub-process.h
  387. submodule-config.c
  388. submodule-config.h
  389. submodule.c
  390. submodule.h
  391. symlinks.c
  392. tag.c
  393. tag.h
  394. tar.h
  395. tempfile.c
  396. tempfile.h
  397. thread-utils.c
  398. thread-utils.h
  399. tmp-objdir.c
  400. tmp-objdir.h
  401. trace.c
  402. trace.h
  403. trace2.c
  404. trace2.h
  405. trailer.c
  406. trailer.h
  407. transport-helper.c
  408. transport-internal.h
  409. transport.c
  410. transport.h
  411. tree-diff.c
  412. tree-walk.c
  413. tree-walk.h
  414. tree.c
  415. tree.h
  416. unicode-width.h
  417. unimplemented.sh
  418. unix-socket.c
  419. unix-socket.h
  420. unpack-trees.c
  421. unpack-trees.h
  422. upload-pack.c
  423. upload-pack.h
  424. url.c
  425. url.h
  426. urlmatch.c
  427. urlmatch.h
  428. usage.c
  429. userdiff.c
  430. userdiff.h
  431. utf8.c
  432. utf8.h
  433. varint.c
  434. varint.h
  435. version.c
  436. version.h
  437. versioncmp.c
  438. walker.c
  439. walker.h
  440. wildmatch.c
  441. wildmatch.h
  442. worktree.c
  443. worktree.h
  444. wrap-for-bin.sh
  445. wrapper.c
  446. write-or-die.c
  447. ws.c
  448. wt-status.c
  449. wt-status.h
  450. xdiff-interface.c
  451. xdiff-interface.h
  452. zlib.c
README.md

Build Status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission). To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org. The mailing list archives are available at https://lore.kernel.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks