summaryrefslogtreecommitdiff
path: root/.gitlab/fix-submodules.py
blob: 621d692edb5ce97fbafff65dda9876161742102e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

"""
Fix submodule upstream URLs. This ensures that CI builds of GHC forks
clone their submodules from its usual location. Otherwise users would need to
fork all submodules before their CI builds would succeed.
"""

from typing import List, Dict
from pathlib import Path
import re

x = open('.gitmodules').read()
x = re.sub(r"url *= *\.\.", "url = https://gitlab.haskell.org/ghc", x)
open('.gitmodules', 'w').write(x)

import subprocess

def get_configs(config_file: Path) -> Dict[str, str]:
    args = ['git', 'config', '-f', config_file.as_posix(), '--list']
    out = subprocess.check_output(args)
    configs = {}
    for line in out.decode('UTF-8').split('\n'):
        if '=' in line:
            k,v = line.split('=')
            configs[k] = v

    return configs

def set_config(config_file: Path, key: str, value: str) -> None:
    args = ['git', 'config', '-f', config_file.as_posix(), '--replace', key, value]
    subprocess.check_call(args)

upstreams = {
    'utils/haddock': 'https://github.com/haskell/haddock'
}

modules_config = Path('.gitmodules')

def main():
    for k,v in get_configs(modules_config).items():
        match = re.match('submodule\.(.+)\.url', k)
        if match is not None:
            submod = match.group(1)
            if submod in upstreams:
                url = upstreams[submod]
            else:
                url = re.sub('\.\.', 'https://gitlab.haskell.org/ghc', v)

            print('Using {submod} from {url}'.format(submod=submod, url=url))
            set_config(modules_config, k, url)

if __name__ == '__main__':
    main()