Esempio n. 1
0
def _split_repo(repo_source: str, repo_dest: str, new_repo_name: str, keep_files: Sequence[str],
                github_token: str, all_branches: bool = False, include_tags: bool = False,
                follow_renames: bool = True) -> Repo:
    with tempfile.TemporaryDirectory(dir=os.path.expanduser('~')) as repo_temp_dest:
        print(f'Creating temporary repo from {repo_source}')
        repo = clone_repo(repo_source, repo_temp_dest, all_branches=all_branches)
        delete_remote(repo)

        print('Removing unwanted history from temporary repo')
        remove_history_for_files_not_matching(repo, keep_files, follow_renames=follow_renames)

        github_repo = _create_github_repo_connect_local_repo(
            repo,
            new_repo_name,
            github_token,
            all_branches=all_branches,
            include_tags=include_tags
        )

        print('Removing temporary directory')

    full_repo_dest = os.path.join(repo_dest, new_repo_name)
    print(f'Cloning {new_repo_name} in permanent spot {full_repo_dest}. Will wait 5s for changes to become available.')
    time.sleep(5)
    os.makedirs(full_repo_dest)
    repo = clone_repo(github_repo.clone_url, full_repo_dest, all_branches=all_branches)
    return repo
Esempio n. 2
0
def _clone_and_connect(repo_source: str, repo_temp_dest: str, github_token: str) -> Repo:
    print(f'Cloning {repo_source} into temporary directory {repo_temp_dest}')
    repo = clone_repo(repo_source, repo_temp_dest, all_branches=True)
    if is_remote_url(repo_source):
        # If remote, need to add authentication into the remote
        github_repo = github_repo_from_clone_url(repo_source, github_token)
        delete_remote(repo)
        connect_local_repo_to_github_repo(repo, github_repo, github_token)
    return repo
Esempio n. 3
0
 def make_backup_clone_old_repo():
     # TODO: better handling for backup location
     backup_dir = _set_backup_dir(None, os.getcwd())
     backup_repo = clone_repo(config.repo_loc,
                              backup_dir,
                              all_branches=True)
     repo = _clone_and_connect(config.repo_loc, repo_temp_dest,
                               config.gh_token)
     return repo
Esempio n. 4
0
def remove_from_repo_history(repo_source: str, drop_files: Sequence[str],
                             github_token: str, keep_backup: bool = True,
                             auto_push_remove: bool = False, backup_dir: Optional[str] = None,
                             follow_renames: bool = True):
    """
    Remove the passed files from the repo history entirely

    :param repo_source: clone url (remote) or file path (local) of repo that should be split
    :param drop_files: files to be dropped in the repo history
    :param github_token: personal access token for Github
    :param keep_backup: whether to retain a backup of the original repo in case something went wrong in removing history
    :param auto_push_remove: pass True to avoid prompt for whether to push the original repo with history removed
    :param backup_dir: pass file path to put backup of old repo there, otherwise uses repo_dest
    :param follow_renames: Whether to track previous names of files from the history and also include those
    :return:
    """
    if keep_backup:
        backup_dir = _set_backup_dir(backup_dir, os.getcwd())
        backup_repo = clone_repo(repo_source, backup_dir, all_branches=True)

    print(f'Cleaning up what was split off in the old repo')
    with tempfile.TemporaryDirectory(dir=os.path.expanduser('~')) as repo_temp_dest:
        repo = _clone_and_connect(repo_source, repo_temp_dest, github_token)

        print(f'Removing history in the original repo for files which were split off. '
              f'Note: this will take a long time for larger repos')
        remove_history_for_files_matching(repo, drop_files, follow_renames=follow_renames)

        if not auto_push_remove:
            print('Success. Please inspect the old repo to make sure nothing that was needed was removed.')
            print('Once the history looks correct, enter Y to replace the remote repo contents')
            print('If there is an issue with the history, enter N to exit')
            push_repo_raw = input(f'Push modified history to {repo_source}? Y/N: ')
            push_repo_str = push_repo_raw.strip().lower()[0]
            push_repo = push_repo_str == 'y'
        else:
            print('auto_push_remove passed. Will automatically push to remote for original repo.')
            push_repo = True
        if push_repo:
            print('Pushing to remote for the original repo')
            push_all_force(repo)
            print('If there is an issue, '
                  'then you can go to your original local repo and run git push --all --force to reverse it')
        else:
            print('Not pushing modified history to original remote.')
        print('Removing temporary directory')
Esempio n. 5
0
 def clone_and_delete_remote():
     repo = clone_repo(config.repo_loc,
                       repo_temp_dest,
                       all_branches=config.all_branches)
     delete_remote(repo)
     return repo