From 8e18f34ddfdd18c1051c00647ce965f758e635cc Mon Sep 17 00:00:00 2001 From: Alexander Stehlik Date: Sat, 8 Apr 2023 01:17:40 -0700 Subject: [PATCH] Fix detection of submodule remote versions A new method get_submodule_versions_from_remote() is added to the git module and used to detect the remote revisions of the configured submodules. The new method loops over the detected submodules and determines the remote branch of each submodule as configured in the .gitmodules file. The default remains "master". --- lib/ansible/modules/git.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/git.py b/lib/ansible/modules/git.py index c9ccff4bfc8..c0b2d20299c 100644 --- a/lib/ansible/modules/git.py +++ b/lib/ansible/modules/git.py @@ -569,6 +569,31 @@ def get_submodule_versions(git_path, module, dest, version='HEAD'): return submodules +def get_submodule_config(git_path, module, dest, submodule_name, config_name): + config_path = 'submodule.' + submodule_name + '.' + config_name + cmd = [git_path, 'config', '-f', '.gitmodules', '--default', 'master', '--get', config_path] + (rc, out, err) = module.run_command(cmd, cwd=dest) + return out.rstrip('\n') + + +def get_submodule_versions_from_remote(git_path, module, dest, submodule_revisions): + submodule_revisions_remote = {} + for submodule_name in submodule_revisions: + submodule_path = get_submodule_config(git_path, module, dest, submodule_name, 'path') + if not submodule_path: + module.fail_json(msg='Unable to detect path of submodule: %s' % submodule_name) + + submodule_branch = get_submodule_config(git_path, module, dest, submodule_name, 'branch') + if not submodule_branch: + module.fail_json(msg='Unable to detect branch of submodule: %s' % submodule_name) + + submodule_path = os.path.join(dest, submodule_path) + revision = get_version(module, git_path, submodule_path, '%s/%s' % ('origin', submodule_branch)) + submodule_revisions_remote[submodule_name] = revision + + return submodule_revisions_remote + + def clone(git_path, module, repo, dest, remote, depth, version, bare, reference, refspec, git_version_used, verify_commit, separate_git_dir, result, gpg_allowlist, single_branch): ''' makes a new git repo if it does not already exist ''' @@ -935,7 +960,7 @@ def fetch(git_path, module, repo, dest, version, remote, depth, bare, refspec, g module.fail_json(msg="Failed to %s: %s %s" % (label, out, err), cmd=command) -def submodules_fetch(git_path, module, remote, track_submodules, dest): +def submodules_fetch(git_path, module, track_submodules, dest): changed = False if not os.path.exists(os.path.join(dest, '.gitmodules')): @@ -962,9 +987,7 @@ def submodules_fetch(git_path, module, remote, track_submodules, dest): if track_submodules: # Compare against submodule HEAD - # FIXME: determine this from .gitmodules - version = 'master' - after = get_submodule_versions(git_path, module, dest, '%s/%s' % (remote, version)) + after = get_submodule_versions_from_remote(git_path, module, dest, begin) if begin != after: changed = True else: @@ -1393,7 +1416,7 @@ def main(): # Deal with submodules submodules_updated = False if recursive and not bare: - submodules_updated = submodules_fetch(git_path, module, remote, track_submodules, dest) + submodules_updated = submodules_fetch(git_path, module, track_submodules, dest) if submodules_updated: result.update(submodules_changed=submodules_updated)