arch-pkgs/.repo/rewrite-gitmodules

58 lines
1.4 KiB
Python
Executable file

#!/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)