Exemple #1
0
def test_git_status_no_origin(tmpdir):
    """Repo exists but has a different origin than expected"""
    # Create repository
    git.Repo.init(tmpdir)

    with pytest.raises(vcs_git.WrongOrigin):
        vcs_git.RepoTool(tmpdir, REPO_URL)
Exemple #2
0
def test_git_detect_root(tmpdir):
    """Get the root of the repository"""
    helpers.create_commits(tmpdir, REPO_URL)

    tmpdir.mkdir("a").mkdir("b").mkdir("c")
    repo = vcs_git.RepoTool(tmpdir.join("a/b/c"), REPO_URL, search_parent=True)

    assert repo.get_root_path() == tmpdir
Exemple #3
0
def test_git_status_wrong_origin(tmpdir):
    """Repo exists but has a different origin than expected"""
    # Create repository
    repo = git.Repo.init(tmpdir)
    repo.create_remote("origin", "git://127.0.0.1/wrongrepo")

    with pytest.raises(vcs_git.WrongOrigin):
        vcs_git.RepoTool(tmpdir, REPO_URL)
Exemple #4
0
def do_sync_repo(progress: ProgressBar, repo_path: Path, repo_data: Repository):
    """
    Perform synchronization of one repository
    :param progress: ProgressBar instance
    :param repo_path: Path to repository
    :param repo_data: Repository data
    :return: True if successful
    """
    pb = progress()
    pb.label = ANSI(ui.format_item(str(repo_data.path), ("track", repo_data.track)))

    if not repo_path.exists():
        repo = git.Repo.init(repo_path)
        repo.create_remote("origin", repo_data.url)

    try:
        repo = vcs_git.RepoTool(repo_path, repo_data.url)
    except vcs_git.InvalidRepository:
        pb.label = ANSI(ui.format_item_error(f"Unable to open {repo_data.path!s}", "Invalid repository"))
        return False

    # Fetch
    fetch_result = repo.fetch(repo_data.track)

    # Warn if ahead
    if fetch_result.ahead:
        pb.label = ANSI(
            ui.format_item_error(f"Skipped {repo_data.path!s}", err=f"Ahead by {len(fetch_result.ahead)} commit(s)")
        )
        return False

    status = repo.get_status()

    # Warn if dirty
    if status.is_dirty:
        pb.label = ANSI(ui.format_item_error(f"Skipped {repo_data.path!s}", err="Workspace is dirty"))
        return False

    current_commit = status.head
    new_commit = fetch_result.fetch_head

    extras = []

    if current_commit is None:
        extras.append(f"Checked out {repo_data.track}")
    elif current_commit == new_commit:
        extras.append(f"Already up to date")
    else:
        extras.append(("update", f"{str(current_commit)[0:7]} -> {str(new_commit)[0:7]}"))
        extras.append(("commits", len(fetch_result.behind)))

    repo.checkout("origin/" + repo_data.track, repo_data.track)
    pb.label = ANSI(ui.format_item_ok(str(repo_data.path), *extras))

    return True
Exemple #5
0
def test_git_status(tmpdir):
    """Check status of a clean repository"""
    commits, _ = helpers.create_commits(tmpdir, REPO_URL)

    repo = vcs_git.RepoTool(tmpdir, REPO_URL)
    status = repo.get_status()

    assert not status.is_dirty
    assert not status.is_detached
    assert status.active_branch.name == "master"
    assert status.head == commits[0]
    assert status.untracked_files == []
Exemple #6
0
def test_git_status_dirty(tmpdir):
    """Check status of a repository with modified files"""
    commits, _ = helpers.create_commits(tmpdir, REPO_URL)
    tmpdir.join("output.txt").write("Edit")

    repo = vcs_git.RepoTool(tmpdir, REPO_URL)
    status = repo.get_status()

    assert status.is_dirty
    assert not status.is_detached
    assert status.active_branch.name == "master"
    assert status.head == commits[0]
    assert status.untracked_files == []
Exemple #7
0
def test_git_status_detached_head(tmpdir):
    """Check status of a repository with a detached head"""
    commits, test_repo = helpers.create_commits(tmpdir, REPO_URL)

    test_repo.git.checkout(commits[3])

    repo = vcs_git.RepoTool(tmpdir, REPO_URL)
    status = repo.get_status()

    assert not status.is_dirty
    assert status.is_detached
    assert status.active_branch is None
    assert status.head == commits[3]
    assert status.untracked_files == []
Exemple #8
0
def status(manifest, root_path):
    """Show the status of all configured repositories"""
    repos = manifest.get_repos()

    ui.info(f"Checking status for {len(repos)} repositories")

    for repo_data in repos:
        repo_path = str(repo_data.path)

        try:
            repo = vcs_git.RepoTool(root_path / repo_data.path, repo_data.url)
            repo_status = repo.get_status()
            current_head = repo_status.active_branch.name if repo_status.active_branch else repo_status.head.hexsha[
                0:8]

            ui.item_ok(repo_path, ("track", repo_data.track),
                       ("head", current_head), ("dirty", repo_status.is_dirty))
        except vcs_git.NotFound:
            ui.item_error(repo_path, "NOT FOUND")
        except vcs_git.InvalidRepository:
            ui.item_error(repo_path, "INVALID")
        except vcs_git.WrongOrigin:
            ui.item_error(repo_path, "ORIGIN MISMATCH")
Exemple #9
0
def test_git_status_invalid(tmpdir):
    """Folder exists but is not a valid git repository"""
    with pytest.raises(vcs_git.InvalidRepository):
        vcs_git.RepoTool(tmpdir, REPO_URL)
Exemple #10
0
def test_git_status_nonexistant(tmpdir):
    """Folder does not exist"""
    with pytest.raises(vcs_git.NotFound):
        vcs_git.RepoTool(tmpdir.join("repo"), REPO_URL)