From 825d6d98c05742251ad60fb7926aaf58590d5fcc Mon Sep 17 00:00:00 2001 From: George Rawlinson Date: Thu, 22 Jul 2021 09:16:28 +0000 Subject: [PATCH] build: add script to rewrite git submodules --- .repo/rewrite-gitmodules | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 .repo/rewrite-gitmodules diff --git a/.repo/rewrite-gitmodules b/.repo/rewrite-gitmodules new file mode 100755 index 0000000..7d9468c --- /dev/null +++ b/.repo/rewrite-gitmodules @@ -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)