nmeum/android-tools

Synchronize git submodules automatically

Biswa96 opened this issue · 0 comments

I use a python script to shallow clone the git submodules manually. I wonder if it is possible to use GitHub Actions for that.

Click here to see the python script
#!/usr/bin/env python
# https://github.com/KuYaki/gitmodules/blob/master/gitmodules/gitmodules.py

"""
Clone a specific tag of git submodules with .gitmodules file
"""

import base64
import os
import sys
import subprocess
import requests

cwd = sys.path[0]
git_modules_file_path = os.path.join(cwd, ".gitmodules")
git_tag = "platform-tools-34.0.1"

if os.path.exists(git_modules_file_path):
    with open(git_modules_file_path) as f:
        path = None
        url = None
        for line in f.readlines():
            if "path = " in line:
                path = line.split("path = ")[-1][:-1]
            if "url = " in line:
                url = line.split("url = ")[-1][:-1]
            if path and url:
                if "boringssl" in path:
                    boring_url = f'https://android.googlesource.com/platform/external/boringssl/+/refs/tags/{git_tag}/BORINGSSL_REVISION?format=TEXT'
                    boring_sha = str(base64.b64decode(requests.get(boring_url, timeout=30).text), 'UTF-8').strip("\n")
                    print(f'git clone --filter=blob:none https://boringssl.googlesource.com/boringssl.git {path}')
                    subprocess.run(f'git clone --filter=blob:none https://boringssl.googlesource.com/boringssl.git {path}'.split(), check=True)
                    previous_dir = os.getcwd()
                    os.chdir(path)
                    print(f'git checkout {boring_sha}')
                    subprocess.run(f'git checkout {boring_sha}'.split(), check=True)
                    os.chdir(previous_dir)
                else:
                    print(f'git clone --depth=1 {url} {path} -b {git_tag}')
                    subprocess.run(f'git clone --depth=1 {url} {path} -b {git_tag}'.split(), check=True)
                path = None
                url = None

⚠️ Seek medical advice after seeing the python code.

  • What I can not do yet

    • Write correct python script.
    • Pass the submodules tag version to python script.
  • What I can try to do but need some guidance

    • Plug that python script in cmake with custom target.
    • Run that custom target in CI when invoked manually.
  • The plan

    • The python script will shallow clone each submodules using the .gitignore. The boringssl repository is an exception there as mentioned in cmake file.
    • After cloning, git diff --exit-code vendor command will check if there is any change.
    • If yes, tell developer to update submodules. Or CI can commit the changes when we become confident about that.
    • If there is no change in vendor directory, compile the project as usual.

Please share your ideas and thoughts about this.