Joshua Salzedo 2 weeks ago committed by GitHub
commit 32707f5f5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,4 @@
bugfixes:
- git - Fixes submodule checkouts to track `.gitmodules` specification
Instead of hardcoding to the `"master"` branch.
(https://github.com/ansible/ansible/issues/77691)

@ -569,6 +569,36 @@ 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', '--get', config_path]
(rc, out, err) = module.run_command(cmd, cwd=dest)
out = out.rstrip('\n')
# if we can't resolve the actual branch, assume its master.
if not out:
out = "master"
return out
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 +965,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 +992,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 +1421,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)

@ -148,3 +148,51 @@
- name: SUBMODULES | Ensure submodule1 is at the appropriate commit
assert:
that: '{{ submodule1.stdout_lines | length }} == 4'
- name: SUBMODULES | clear checkout_dir
file:
state: absent
path: "{{ checkout_dir }}"
- name: SUBMODULES | Clone main_b submodule repository
git:
repo: "{{ repo_submodules_b }}"
dest: "{{ checkout_dir }}/test.gitdir"
recursive: yes # checkout the submodules as-is.
# For the initial clone, don't track submodules.
# this is done to simulate e.g. updating a repository that already exists.
track_submodules: no
- name: SUBMODULES | update main_b submodule repository
git:
repo: "{{ repo_submodules_b }}"
dest: "{{ checkout_dir }}/test.gitdir"
recursive: yes
# Track the submodules in the second deployment, to simulate updating an existing repository.
track_submodules: yes
- name: SUBMODULES | check presense of submodule3's flag
stat:
path: "{{ checkout_dir }}/test.gitdir/submodule3/FLAG"
# submodule3's commited state does not have this file, but the tip of the branch in .gitmodules has this file.
# for track_submodules to have behaved correctly, this file must exist in the checkout.
register: submodule3_flag_stat
- name: SUBMODULES | check presense of submodule4's flag
stat:
path: "{{ checkout_dir }}/test.gitdir/submodule4/FLAG"
# submodule4's commited state and tip of tracked branch have the FLAG present.
# however, a canary `master` branch exists with the flag removed.
# for track_submodules to have behaved correctly, this file must be present.
register: submodule4_flag_stat
- name: SUBMODULES | Assert submodule3 FLAG is present.
assert:
that: submodule3_flag_stat.stat.exists
msg: "submodule3's FLAG must exist on this branch, but its missing!"
- name: SUBMODULES | Assert submodule4 FLAG is present.
assert:
that: submodule4_flag_stat.stat.exists
msg: "submodule4's FLAG must exist on this branch, but its missing!"
- name: SUBMODULES | clear checkout_dir
file:
state: absent
path: "{{ checkout_dir }}"

@ -36,6 +36,9 @@ repo_format3: 'ssh://git@github.com/jimi-c/test_role.git'
repo_submodules: 'https://github.com/abadger/test_submodules_newer.git'
repo_submodule1: 'https://github.com/abadger/test_submodules_subm1.git'
repo_submodule2: 'https://github.com/abadger/test_submodules_subm2.git'
repo_submodules_b: 'https://github.com/theunkn0wn1/ansible_test_submodules_monorepo.git'
repo_submodule3: 'https://github.com/theunkn0wn1/ansible_test_submodules_subm3.git'
repo_submodule4: 'https://github.com/theunkn0wn1/ansible_test_submodules_subm4.git'
repo_update_url_1: 'https://github.com/ansible-test-robinro/git-test-old'
repo_update_url_2: 'https://github.com/ansible-test-robinro/git-test-new'
known_host_files:

Loading…
Cancel
Save