diff --git a/.repo/rewrite-gitmodules b/.repo/rewrite-gitmodules index 7d9468c..446b1b4 100755 --- a/.repo/rewrite-gitmodules +++ b/.repo/rewrite-gitmodules @@ -1,58 +1,59 @@ #!/usr/bin/python - -from typing import List -from typing import Tuple import configparser import sys +import re -#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) +submodule_pattern = re.compile(r"^submodule\ \"(?P.+)\"$") parser = configparser.ConfigParser() parser.read(".gitmodules") -packages = [] -other = [] +# criteria to group packages by +groups = ["ssh://", "https://"] + +buckets = {} + +valid = 0 +assigned = 0 + +for group in groups: + buckets[group] = [] for section in parser: - # skip default section, it's useless - if section == "DEFAULT": - continue + match = re.match(submodule_pattern, section) + if match: + valid += 1 + submodule = match.group("submodule") + path = parser.get(section, "path") + url = parser.get(section, "url") - # extract path & url - path = parser.get(section, "path") - url = parser.get(section, "url") + for group in groups: + grp_match = re.match(group, url) + if grp_match: + assigned += 1 + buckets[group].append((submodule, path, url)) + break - # 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)) +if valid != assigned: + print("We have a problem.") + sys.exit(1) -# 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") +# indentation for submodule path/url. defaults to tab. +indentation = "\t" # rewrite gitmodules with open(".gitmodules", "w") as f: - write_packages(packages, f) - write_packages(other, f) + for group in groups: + # skip empty buckets + if len(buckets[group]) == 0: + continue + + # sort alphabetically + buckets[group].sort() + + # write submodules to file + for module in buckets[group]: + submodule, path, url = module + f.write(f'[submodule "{submodule}"]\n') + f.write(f"{indentation}path = {path}\n") + f.write(f"{indentation}url = {url}\n")