build: add script to rewrite git submodules

This commit is contained in:
George Rawlinson 2021-07-22 09:16:28 +00:00
parent 538e64e59f
commit 825d6d98c0
Signed by: grawlinson
GPG key ID: E0959FEA8B550539

58
.repo/rewrite-gitmodules Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/python
from typing import List
from typing import Tuple
import configparser
import sys
#def add_modules(
# modules: List[Tuple[str, str]],
# parser: configparser.ConfigParser,
#) -> None:
# """Add set of submodules to gitconfig structure."""
# for module in modules:
# path, url = module
# section = f"submodule \"{path}\""
# parser.add_section(section)
# parser.set(section, "\tpath", path)
# parser.set(section, "\turl", url)
parser = configparser.ConfigParser()
parser.read(".gitmodules")
packages = []
other = []
for section in parser:
# skip default section, it's useless
if section == "DEFAULT":
continue
# extract path & url
path = parser.get(section, "path")
url = parser.get(section, "url")
# i've got access to the package, must be one of mine
if url.startswith("ssh://"):
packages.append((path, url))
# no idea who this belongs to
elif url.startswith("https://"):
other.append((path, url))
# sort modules alphabetically
packages.sort()
other.sort()
def write_packages(packages, f):
for package in packages:
path, url = package
section = f"[submodule \"{path}\"]"
f.write(f"{section}\n")
f.write(f"\tpath = {path}\n")
f.write(f"\turl = {url}\n")
# rewrite gitmodules
with open(".gitmodules", "w") as f:
write_packages(packages, f)
write_packages(other, f)