git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[BUG] Recursive submodules fails to track branch correctly if . is used

From
KOKrzysztof Opasiak <k.opasiak@samsung.com>
Date
Jul 16, 2019, 21:49 UTC
Message-ID
<08844c07-5192-0645-9bfc-62e1b16e39a8@samsung.com>
In-Reply-To
<CGME20190716214945eucas1p299e23651b7bc1738d0c3bf2d227d4e46@eucas1p2.samsung.com>
Dear git community,
I'd like to report you a potential bug in git submodule implementation.
* Problem statement

According to doc a special value of "." can be used as a branch name to track. This means that git should trace the same branch as is currently in use in superproject. This works perfectly fine as long as you don't try to add superproject (SUP1) with such configuration as a submodule to another superproject (SUP2). The nesting structure would look like this:

SUP2
     - SUP1
         - SUB_PROJECT

If you try to do this you won't be able to execute any of the following commands:

$ git clone --recursive SUP2
$ git clone SUP2 & git submodule update --init --recursive --remote
$ git submodule update --init --recursive --remote
as every time you will get an error message similar to this one:

fatal: Submodule (SUB_PROJECT) branch configured to inherit branch from superproject, but the superproject is not on any branch fatal: Needed a single revision Unable to find current origin/ revision in submodule path 'SUB_PROJECT'

* Initial investigation results

The error message comes from remote_submodule_branch():1938 in submodule--helper.c and it is generated called from SUP1 dir (due to --recursive flag). Actually this function behaves correctly. If you go to SUP1 dir and check the branch info you will notice that the repo is in detached head state.

* Probable root cause

The root cause is that the traced branch information is never propagated to the submodules. All the submodules are always initialized in a detached head state and there is no way to change this behavior nor to propagate the traced branch information to submodules.

* Workaround script

I managed to workaround this bug by dropping the recursive option and after the top level project has been cloned I go through every submodule, set local branch to point to current head and then just do git checkout and I do this recursively in all modules. After this I'm able to successfully execute:

$ git submodule update --rebase --recursive --remote
The script (to be executed in SUP2 right after clone):
#!/bin/bash
function prepare_submodules {
	git submodule update --init
	local SUBMODULE_LIST=`git submodule--helper status | awk '{print $2}'`
	while read -r submod; do
		local PARENT_DIR=`pwd`
		local BRANCH=`git submodule--helper remote-branch ${submod}`
		cd ${submod}
		git branch -f ${BRANCH} HEAD;
		git checkout ${BRANCH}
		if [ ! -z "`git submodule status`" ]; then
			prepare_submodules;
		fi
		cd ${PARENT_DIR}
	done <<< "$SUBMODULE_LIST"
}
prepare_submodules
Best regards,
-- 
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
Message 1 of 1 in “[BUG] Recursive submodules fails to track branch correctly if . is used”
  1. Krzysztof OpasiakJul 16, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.