def add(self, repo): config = load_config() if repo.endswith(".git"): # Get relative path from project root to current directory challenge_path = Path(os.path.relpath(os.getcwd(), get_project_path())) # Get new directory that will add the git subtree base_repo_path = Path(os.path.basename(repo).rsplit(".", maxsplit=1)[0]) # Join targets challenge_path = challenge_path / base_repo_path print(challenge_path) config["challenges"][str(challenge_path)] = repo head_branch = get_git_repo_head_branch(repo) subprocess.call( [ "git", "subtree", "add", "--prefix", challenge_path, repo, head_branch, "--squash", ], cwd=get_project_path(), ) with open(get_config_path(), "w+") as f: config.write(f) subprocess.call( ["git", "add", ".ctf/config"], cwd=get_project_path(), ) subprocess.call( ["git", "commit", "-m", f"Added {str(challenge_path)}"], cwd=get_project_path(), ) elif Path(repo).exists(): config["challenges"][repo] = repo with open(get_config_path(), "w+") as f: config.write(f) else: click.secho( "Couldn't process that challenge path. Please check it for errors.", fg="red", )
def add(self, repo): config = load_config() if repo.endswith(".git"): # Get relative path from project root to current directory challenge_path = Path( os.path.relpath(os.getcwd(), get_project_path())) # Get new directory that will exist after clone base_repo_path = Path( os.path.basename(repo).rsplit(".", maxsplit=1)[0]) # Join targets challenge_path = challenge_path / base_repo_path print(challenge_path) config["challenges"][str(challenge_path)] = repo with open(get_config_path(), "w+") as f: config.write(f) subprocess.call(["git", "clone", "--depth", "1", repo]) shutil.rmtree(str(base_repo_path / ".git")) elif Path(repo).exists(): config["challenges"][repo] = repo with open(get_config_path(), "w+") as f: config.write(f) else: click.secho( "Couldn't process that challenge path. Please check it for errors.", fg="red", )
def update(self, challenge=None): config = load_config() challenges = dict(config["challenges"]) for folder, url in challenges.items(): if challenge and challenge != folder: continue if url.endswith(".git"): click.echo(f"Pulling latest {url} to {folder}") head_branch = get_git_repo_head_branch(url) subprocess.call( [ "git", "subtree", "pull", "--prefix", folder, url, head_branch, "--squash", ], cwd=get_project_path(), ) subprocess.call(["git", "mergetool"], cwd=folder) subprocess.call(["git", "clean", "-f"], cwd=folder) subprocess.call(["git", "commit", "--no-edit"], cwd=folder) else: click.echo(f"Skipping {url} - {folder}")
def push(self, challenge=None): config = load_config() challenges = dict(config["challenges"]) if challenge is None: # Get relative path from project root to current directory challenge_path = Path(os.path.relpath(os.getcwd(), get_project_path())) challenge = str(challenge_path) try: url = challenges[challenge] head_branch = get_git_repo_head_branch(url) subprocess.call( ["git", "subtree", "push", "--prefix", challenge, url, head_branch], cwd=get_project_path(), ) except KeyError: click.echo( "Couldn't process that challenge path. Please check that the challenge is added to .ctf/config and that your path matches." )
def restore(self, challenge=None): config = load_config() challenges = dict(config["challenges"]) for folder, url in challenges.items(): if url.endswith(".git"): if challenge is not None and folder != challenge: continue click.echo(f"Adding git repo {url} to {folder} as subtree") head_branch = get_git_repo_head_branch(url) subprocess.call( [ "git", "subtree", "add", "--prefix", folder, url, head_branch, "--squash", ], cwd=get_project_path(), ) else: click.echo(f"Skipping {url} - {folder}")