Skip to content
clone_forks.py 1.16 KiB
Newer Older
Cyril Matthey-Doret's avatar
Cyril Matthey-Doret committed
#!/usr/bin/env python
# Reads JSON output from collect-forks (either as a file or in stdin)
# and clone all target forks into provided directory at the target commit.

# usage ./clone_forks_from_json.sh OUT_DIR forks.json
#       ./collect_forks.py --token token.asc URL | ./clone_forks_from_json.sh OUT_DIR

import sys
from pathlib import Path
import json
import click
import git


def clone_fork(url: str, out_path: Path, commit: str):
    """
    Clone input repo in target directory
    and checkout desired commit."""
    cloned = git.Repo.clone_from(url, out_path)
    cloned.git.checkout(commit)


@click.command()
@click.argument("out_dir", type=click.Path(file_okay=False, exists=False))
@click.argument("forks", type=click.File(mode="r"), default=sys.stdin)
def clone_forks(out_dir, forks, token):
    """Clone all forks from input JSON into target directory. The input json should be produced by collect-forks."""
Cyril Matthey-Doret's avatar
Cyril Matthey-Doret committed

    forks = json.load(forks)
    for fork in forks:
        url, group, commit = [fork.get(key) for key in ["url", "group", "commit"]]
        path = Path(out_dir) / group
        clone_fork(url, path, commit)


if __name__ == "__main__":
    clone_forks()