diff --git a/.github/workflows/general-ci-tests.yml b/.github/workflows/general-ci-tests.yml index 86683e1..1bcb788 100644 --- a/.github/workflows/general-ci-tests.yml +++ b/.github/workflows/general-ci-tests.yml @@ -86,6 +86,19 @@ jobs: - name: Run the check_default_params script run: python tests/check_default_params.py + # Check that MOM6 submodule commit hashes are consistent + check_commit_hashes: + + runs-on: ubuntu-latest + + steps: + # Checkout the repo + - uses: actions/checkout@v4 + + # Run the test + - name: Run the check_commit_hashes script + run: python tests/check_commit_hashes.py + # Job to run check_input_data_list script check_input_data_list: diff --git a/tests/check_commit_hashes.py b/tests/check_commit_hashes.py new file mode 100644 index 0000000..234d836 --- /dev/null +++ b/tests/check_commit_hashes.py @@ -0,0 +1,78 @@ +# check that the MOM6 submodule commit is the same as what's specificed in .gitmodules: + +import sys +import subprocess +import re +from pathlib import Path + +# path to this file: +this_file = Path(__file__).resolve() +mom_interface_root = this_file.parent.parent + +def get_mom6_gitmodules_commit(): + """Get the commit hash of the MOM6 submodule from .gitmodules""" + + with open(mom_interface_root / ".gitmodules") as f: + fxtag = None + url = None + in_MOM6_block = False + for line in f: + line = line.strip('[ ]\n').split() + if not line: + continue # skip empty lines + if len(line) == 2 and line[0] == "submodule": + in_MOM6_block = line[1] == '"MOM6"' + if in_MOM6_block: + if line[0] == "fxtag": + fxtag = line[2] + elif line[0] == "url": + url = line[2] + + # get commit hash corresponding to fxtag + if fxtag is None: + raise Exception("Could not get MOM6 commit from .gitmodules") + elif url is None: + raise Exception("Could not get MOM6 url from .gitmodules") + + # check if fxtag is a commit hash and not a tag + if re.match(r"^[0-9a-f]{40}$", fxtag) or re.match(r"^[0-9a-f]{7}$", fxtag): + return fxtag + + # get commit hash from tag + p = subprocess.Popen( + ["git", "ls-remote", url, fxtag], + stdout=subprocess.PIPE, + ) + out, _ = p.communicate() + if p.returncode != 0: + raise Exception("Could not get MOM6 commit from .gitmodules") + return out.decode().split()[0][:40] # first 40 characters are the commit hash + + +def get_mom6_submodule_commit(): + """Get the commit hash of the MOM6 submodule from the src/MOM6 directory""" + + p = subprocess.Popen( + ["git", "submodule", "status", mom_interface_root / "MOM6"], + stdout=subprocess.PIPE, + ) + out, _ = p.communicate() + if p.returncode != 0: + raise Exception("Could not get MOM6 submodule commit") + return out.decode().split()[0] + + +def main(): + """Check that the MOM6 submodule commit is the same as what's specificed in .gitmodules""" + + mom6_commit = get_mom6_gitmodules_commit() + mom6_submodule_commit = get_mom6_submodule_commit() + if mom6_commit[:7] != mom6_submodule_commit[:7]: + print("ERROR: MOM6 commit in .gitmodules does not match the commit in src/MOM6") + print("MOM6 commit in .gitmodules: {}".format(mom6_commit)) + print("MOM6 commit in src/MOM6: {}".format(mom6_submodule_commit)) + sys.exit(1) + + +if __name__ == "__main__": + main()