# 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/testing.py
# class Response:
#     def __init__(self, content, status_code=200):
#         self.content = content
#         self.status_code = status_code
# 
#     def json(self):
#         return self.content
# 
# Path: gg/builtins/merge/gg_merge.py
# @cli.command()
# @pass_config
# def merge(config):
#     """Merge the current branch into $default_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.")
# 
#     if repo.is_dirty():
#         error_out(
#             'Repo is "dirty". ({})'.format(
#                 ", ".join([repr(x.b_path) for x in repo.index.diff(None)])
#             )
#         )
# 
#     branch_name = active_branch.name
# 
#     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(f"No remote called {origin_name!r} found")
# 
#     repo.heads[default_branch].checkout()
#     upstream_remote.pull(repo.heads[default_branch])
# 
#     repo.git.merge(branch_name)
#     repo.git.branch("-d", branch_name)
#     success_out("Branch {!r} deleted.".format(branch_name))
# 
#     info_out("NOW, you might want to run:\n")
#     info_out(f"git push {origin_name} {default_branch}\n\n")
# 
#     push_for_you = input("Run that push? [Y/n] ").lower().strip() != "n"
#     if push_for_you:
#         upstream_remote.push(default_branch)
#         success_out(f"Current {default_branch} pushed to {upstream_remote.name}")
# 
#     # XXX perhaps we should delete the branch off the fork remote if it exists


import json
import os
import tempfile
import shutil
import pytest
from click.testing import CliRunner
from gg.main import Config
from gg.testing import Response
from gg.builtins.merge.gg_merge import merge
    origin_remote = mocker.MagicMock()
    origin_remote.url = "git@github.com:peterbe/gg-example.git"
    my_pushinfo = mocker.MagicMock()
    my_pushinfo.flags = 0
    my_remote.push.return_value = [my_pushinfo]
    # first we have to fake some previous information
    state = json.load(open(temp_configfile))
    state["gg-commit-test:my-topic-branch"] = {
        "description": "Some description",
        "bugnumber": None,
    }
    # state["FORK_NAME"] = "myusername"
    state["GITHUB"] = {
        "token": "somelongapitokenhere",
        "github_url": "https://example.com",
    }
    with open(temp_configfile, "w") as f:
        json.dump(state, f)

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(merge, [], input="\n\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


# 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/testing.py
# class Response:
#     def __init__(self, content, status_code=200):
#         self.content = content
#         self.status_code = status_code
# 
#     def json(self):
#         return self.content
# 
# Path: gg/builtins/merge/gg_merge.py
# @cli.command()
# @pass_config
# def merge(config):
#     """Merge the current branch into $default_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.")
# 
#     if repo.is_dirty():
#         error_out(
#             'Repo is "dirty". ({})'.format(
#                 ", ".join([repr(x.b_path) for x in repo.index.diff(None)])
#             )
#         )
# 
#     branch_name = active_branch.name
# 
#     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(f"No remote called {origin_name!r} found")
# 
#     repo.heads[default_branch].checkout()
#     upstream_remote.pull(repo.heads[default_branch])
# 
#     repo.git.merge(branch_name)
#     repo.git.branch("-d", branch_name)
#     success_out("Branch {!r} deleted.".format(branch_name))
# 
#     info_out("NOW, you might want to run:\n")
#     info_out(f"git push {origin_name} {default_branch}\n\n")
# 
#     push_for_you = input("Run that push? [Y/n] ").lower().strip() != "n"
#     if push_for_you:
#         upstream_remote.push(default_branch)
#         success_out(f"Current {default_branch} pushed to {upstream_remote.name}")
# 
#     # XXX perhaps we should delete the branch off the fork remote if it exists


# Path: gg/builtins/merge/tests/test_gg_merge.py
import json
import os
import tempfile
import shutil
import pytest
from click.testing import CliRunner
from gg.main import Config
from gg.testing import Response
from gg.builtins.merge.gg_merge import merge

    origin_remote = mocker.MagicMock()
    origin_remote.url = "git@github.com:peterbe/gg-example.git"
    my_pushinfo = mocker.MagicMock()
    my_pushinfo.flags = 0
    my_remote.push.return_value = [my_pushinfo]
    # first we have to fake some previous information
    state = json.load(open(temp_configfile))
    state["gg-commit-test:my-topic-branch"] = {
        "description": "Some description",
        "bugnumber": None,
    }
    # state["FORK_NAME"] = "myusername"
    state["GITHUB"] = {
        "token": "somelongapitokenhere",
        "github_url": "https://example.com",
    }
    with open(temp_configfile, "w") as f:
        json.dump(state, f)

    runner = CliRunner()
    config = Config()
    config.configfile = temp_configfile
    result = runner.invoke(merge, [], input="\n\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


    mocked_git().git.merge.assert_called_with("this-branch")