{"thread":{"id":"53091","subject":"Checking Out Branches Containing Submodules With update=none and submodule.recurse=true Fails With fatal:not a git repository","startedAt":"2020-03-25T04:19:41Z","lastAt":"2020-10-07T21:29:51Z","messageCount":2,"participants":["daniel mclean","Philippe Blain"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"393918","messageId":"CABVQXt5E-R22G62W-tQieA7XiZKZiOA8Hp2xewYhwwOS8wFh0Q@mail.gmail.com","threadId":"53091","inReplyTo":null,"subject":"Checking Out Branches Containing Submodules With update=none and submodule.recurse=true Fails With fatal:not a git repository","fromName":"daniel mclean","fromEmail":"maczor@gmail.com","sentAt":"2020-03-25T04:19:26Z","receivedAt":"2020-03-25T04:19:41Z","isPatch":false,"sender":{"key":"maczor@gmail.com","avatar":null},"body":"Hello git developers,\n\nFirstly thanks for a great tool!\n\nI have run into an issue that it would be good to get your advice on.\n\nI have a git repository with multiple submodules.  A few of these\nsubmodules are configured with update=none so that they point to a\nspecific commit but so that clone operations and submodule update\noperations don't retrieve their contents.\n\nWhen working with this repository and trying to change branches, git\nis reporting a fatal error when changing branches.  This can be worked\naround by deinit'ing the submodules before switching branches but that\ndoesn't seem like it should be necessary.\n\nThe reproduction steps are to:\n- Clone the repo\n- Checkout a branch containing submodules that are set with\nupdate=none (git checkout branch-name)\n- Update and init submodules (git submodule update --init)\n- Attempt to switch to a different branch (git checkout other-branch)\n\nAt this point git will report an error at the first submodule with update=none:\nfatal: not a git repository: ../.git/modules/no-update-submodule\nfatal: could not reset submodule index\n\nAfter doing some sleuthing it seems that this is because when\nupdate=none the submodule repository is not cloned to\n.git/module/<module name>, however when attempting to change branches\nusing a checkout, git then stumbles since it expects the repository to\nbe there.\n\nThis seems obvious when run with GIT_TRACE=1, git fails when it\nattempts to run the read-tree command:\n\n19:20:18.129343 run-command.c:663       trace: run_command: cd\nno-update-submodule; unset GIT_PREFIX; GIT_DIR=.git git\n--super-prefix= no-update-submodule/ read-tree -u --reset\n4b825dc642cb6eb9a060e54bf8d69288fbee4904\nfatal: not a git repository: ../.git/modules/no-update-submodule\nfatal: could not reset submodule index\n\nIf my understanding of how update=none for a submodule and git is\ncorrect, then it seems like it shouldn't be trying to do this since it\nshould know that the repository isn't there (update=none after all).\nThis behaviour occurs when I have submodule.recurse set to true,\nhowever interestingly when I set it to false I don't run into this\nissue.\nSo it seems like these two directives are providing mixed messages in\na sense, one says don't clone the submodule repo and the other says\nlook into the submodule repo and try to find more submodules.\n\nI've tried the 2.20 version of git that comes prepackaged with my OS\nand built git 2.26 from source with the same outcome.  I'm running on\nDebian stretch on x64 in case that matters.\n\nI'm happy to pursue trying to suggest a fix for this, but am new to\nthe git codebase and so would appreciate some feedback on whether this\nis in fact an issue and some advice on how to best approach a\nsolution.\n\nThanks\nDaniel\n"},{"id":"407104","messageId":"CDB7D2C8-3164-4860-A999-F9BE565F7750@gmail.com","threadId":"53091","inReplyTo":"CABVQXt5E-R22G62W-tQieA7XiZKZiOA8Hp2xewYhwwOS8wFh0Q@mail.gmail.com","subject":"Re: Checking Out Branches Containing Submodules With update=none and submodule.recurse=true Fails With fatal:not a git repository","fromName":"Philippe Blain","fromEmail":"levraiphilippeblain@gmail.com","sentAt":"2020-10-07T21:29:40Z","receivedAt":"2020-10-07T21:29:51Z","isPatch":false,"sender":{"key":"levraiphilippeblain@gmail.com","avatar":"https://avatars.githubusercontent.com/u/44212482?v=4"},"body":"Hi Daniel, \n\n> Le 25 mars 2020 à 00:19, daniel mclean <maczor@gmail.com> a écrit :\n> \n> Hello git developers,\n> \n> Firstly thanks for a great tool!\n> \n> I have run into an issue that it would be good to get your advice on.\n> \n> I have a git repository with multiple submodules.  A few of these\n> submodules are configured with update=none so that they point to a\n> specific commit but so that clone operations and submodule update\n> operations don't retrieve their contents.\n> \n> When working with this repository and trying to change branches, git\n> is reporting a fatal error when changing branches.  This can be worked\n> around by deinit'ing the submodules before switching branches but that\n> doesn't seem like it should be necessary.\n> \n> The reproduction steps are to:\n> - Clone the repo\n> - Checkout a branch containing submodules that are set with\n> update=none (git checkout branch-name)\n> - Update and init submodules (git submodule update --init)\n> - Attempt to switch to a different branch (git checkout other-branch)\n> \n> At this point git will report an error at the first submodule with update=none:\n> fatal: not a git repository: ../.git/modules/no-update-submodule\n> fatal: could not reset submodule index\n\nI tried reproducing this with the following script, but it does not fail for me:\n\n```\n!/bin/bash\n\nset -x\n\ncreate_repo () {\nname=$1\nrm -rf $name\ngit init $name\ncd $name\ndate>>file\ngit add file\ngit commit -m \"add file\"\ncd ../ \n}\n\n\ncreate_repo super\ncreate_repo sub1\ncreate_repo sub2\n\ncd super\ngit submodule add ../sub1\ngit submodule add ../sub2\ngit config -f .gitmodules submodule.sub1.update none\ngit add .gitmodules\ngit commit -m \"add sub1 and sub2\"\ngit co -b other-branch\ndate>>file\ngit add file\ngit commit -m \"update file\"\ngit checkout master\ncd ../ \n\nrm -rf clone\ngit clone super clone\ncd clone\ngit submodule update --init\ngit config submodule.recurse true\ngit checkout other-branch\n# no error !\nls -R\n```\n\nIf you could try to come up with a similar reproducer, it would help to better \ndiagnose this failure. I was not sure by reading your message if both branches\ninvolved had the same number of submodules, or the same 'update' settings\nfor each submodules, so maybe that's where your use case differs from my script.\n\n> After doing some sleuthing it seems that this is because when\n> update=none the submodule repository is not cloned to\n> .git/module/<module name>,\n\nI gather that's the expected behaviour, no ?\n\n> however when attempting to change branches\n> using a checkout, git then stumbles since it expects the repository to\n> be there.\n\nI've seen that happen when going from a commit where the submodule\ndoes not exist to a commit where it does exist, (see [1], [2]), but your situation\nis a little different.\n\n> If my understanding of how update=none for a submodule and git is\n> correct, then it seems like it shouldn't be trying to do this since it\n> should know that the repository isn't there (update=none after all).\n> This behaviour occurs when I have submodule.recurse set to true,\n> however interestingly when I set it to false I don't run into this\n> issue.\n> So it seems like these two directives are providing mixed messages in\n> a sense, one says don't clone the submodule repo and the other says\n> look into the submodule repo and try to find more submodules.\n\nFrom reading the documentation of `submodule.<name>.update` [3], it seems\nthat `git checkout` should not care about this config setting, but in my script above,\nit seems it does (at least for the value \"none\"), or else it would crash as you describe...\nI've not looked at the code in depth, but I might if you can come up with a reproducer.\n\nCheers,\n\nPhilippe.\n\n[1] https://lore.kernel.org/git/CAE5ih78zCR0ZdHAjoxguUb3Y6KFkZcoxJjhS7rkbtZpr+d1n=g@mail.gmail.com/\n[2] https://lore.kernel.org/git/20200501005432.h62dnpkx7feb7rto@glandium.org/T/#u\n[3] https://git-scm.com/docs/git-config#Documentation/git-config.txt-submoduleltnamegtupdate\n\n"}]}