# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/rebase/gg_rebase.py
# @cli.command()
# @pass_config
# def rebase(config):
#     """Rebase the current branch against $origin/$branch"""
#     repo = config.repo
# 
#     state = read(config.configfile)
#     default_branch = state.get("DEFAULT_BRANCH", "master")
# 
#     active_branch = repo.active_branch
#     if active_branch.name == default_branch:
#         error_out(f"You're already on the {default_branch} branch.")
#     active_branch_name = active_branch.name
# 
#     if repo.is_dirty():
#         error_out(
#             'Repo is "dirty". ({})'.format(
#                 ", ".join([repr(x.b_path) for x in repo.index.diff(None)])
#             )
#         )
# 
#     origin_name = state.get("ORIGIN_NAME", "origin")
#     upstream_remote = None
#     for remote in repo.remotes:
#         if remote.name == origin_name:
#             upstream_remote = remote
#             break
#     if not upstream_remote:
#         error_out("No remote called {!r} found".format(origin_name))
# 
#     repo.heads[default_branch].checkout()
#     repo.remotes[origin_name].pull(default_branch)
# 
#     repo.heads[active_branch_name].checkout()
# 
#     print(repo.git.rebase(default_branch))
#     success_out(f"Rebased against {origin_name}/{default_branch}")
#     info_out(
#         f"If you want to start interactive rebase "
#         f"run:\n\n\tgit rebase -i {default_branch}\n"
#     )


import json
import os
import tempfile
import shutil
import pytest
from click.testing import CliRunner
from gg.main import Config
from gg.builtins.rebase.gg_rebase import rebase
    shutil.rmtree(tmp_dir)


def test_rebase(temp_configfile, mocker):
    mocked_git = mocker.patch("git.Repo")
    mocked_git().working_dir = "gg-start-test"
    active_branch = mocker.MagicMock()
    mocked_git().active_branch = active_branch
    mocked_remote = mocker.MagicMock()
    mocked_remote.name = "origin"
    mocked_git().remotes.__iter__.return_value = [mocked_remote]
    mocked_git().is_dirty.return_value = False

    state = json.load(open(temp_configfile))
    state["FORK_NAME"] = "peterbe"
    with open(temp_configfile, "w") as f:
        json.dump(state, f)

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(rebase, [], input="\n", obj=config)
    # if result.exception:
    #     print(mocked_git.mock_calls)
    #     print(result.output)
    #     print(result.exception)
    #     raise result.exception
    assert result.exit_code == 0
    assert not result.exception


# Path: gg/main.py
# class Config:
#     def __init__(self):
#         self.verbose = False  # default
#         self.configfile = DEFAULT_CONFIGFILE
#         try:
#             self.repo = get_repo()
#         except git.InvalidGitRepositoryError as exception:
#             error_out(f"{exception.args[0]} is not a git repository")
# 
# Path: gg/builtins/rebase/gg_rebase.py
# @cli.command()
# @pass_config
# def rebase(config):
#     """Rebase the current branch against $origin/$branch"""
#     repo = config.repo
# 
#     state = read(config.configfile)
#     default_branch = state.get("DEFAULT_BRANCH", "master")
# 
#     active_branch = repo.active_branch
#     if active_branch.name == default_branch:
#         error_out(f"You're already on the {default_branch} branch.")
#     active_branch_name = active_branch.name
# 
#     if repo.is_dirty():
#         error_out(
#             'Repo is "dirty". ({})'.format(
#                 ", ".join([repr(x.b_path) for x in repo.index.diff(None)])
#             )
#         )
# 
#     origin_name = state.get("ORIGIN_NAME", "origin")
#     upstream_remote = None
#     for remote in repo.remotes:
#         if remote.name == origin_name:
#             upstream_remote = remote
#             break
#     if not upstream_remote:
#         error_out("No remote called {!r} found".format(origin_name))
# 
#     repo.heads[default_branch].checkout()
#     repo.remotes[origin_name].pull(default_branch)
# 
#     repo.heads[active_branch_name].checkout()
# 
#     print(repo.git.rebase(default_branch))
#     success_out(f"Rebased against {origin_name}/{default_branch}")
#     info_out(
#         f"If you want to start interactive rebase "
#         f"run:\n\n\tgit rebase -i {default_branch}\n"
#     )


# Path: gg/builtins/rebase/tests/test_gg_rebase.py
import json
import os
import tempfile
import shutil
import pytest
from click.testing import CliRunner
from gg.main import Config
from gg.builtins.rebase.gg_rebase import rebase

    shutil.rmtree(tmp_dir)


def test_rebase(temp_configfile, mocker):
    mocked_git = mocker.patch("git.Repo")
    mocked_git().working_dir = "gg-start-test"
    active_branch = mocker.MagicMock()
    mocked_git().active_branch = active_branch
    mocked_remote = mocker.MagicMock()
    mocked_remote.name = "origin"
    mocked_git().remotes.__iter__.return_value = [mocked_remote]
    mocked_git().is_dirty.return_value = False

    state = json.load(open(temp_configfile))
    state["FORK_NAME"] = "peterbe"
    with open(temp_configfile, "w") as f:
        json.dump(state, f)

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(rebase, [], input="\n", obj=config)
    # if result.exception:
    #     print(mocked_git.mock_calls)
    #     print(result.output)
    #     print(result.exception)
    #     raise result.exception
    assert result.exit_code == 0
    assert not result.exception


    mocked_git().git.rebase.assert_called_with("master")