# 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


# By doing this import we make sure that the plugin is made available
# but the entry points loading inside gg.main.
# An alternative would we to set `PYTHONPATH=. py.test` (or something)
# but then that wouldn't test the entry point loading.



@pytest.yield_fixture
def temp_configfile():
    tmp_dir = tempfile.mkdtemp("gg-merge")
    fp = os.path.join(tmp_dir, "state.json")
    with open(fp, "w") as f:
        json.dump({}, f)

# 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



# By doing this import we make sure that the plugin is made available
# but the entry points loading inside gg.main.
# An alternative would we to set `PYTHONPATH=. py.test` (or something)
# but then that wouldn't test the entry point loading.



@pytest.yield_fixture
def temp_configfile():
    tmp_dir = tempfile.mkdtemp("gg-merge")
    fp = os.path.join(tmp_dir, "state.json")
    with open(fp, "w") as f:
        json.dump({}, f)

    yield fp