# Path: gg/utils.py
# def error_out(msg, raise_abort=True):
#     click.echo(click.style(msg, fg="red"))
#     if raise_abort:
#         raise click.Abort
# 
# def info_out(msg):
#     click.echo(msg)
# 
# def get_default_branch(repo, origin_name):
#     # TODO: Figure out how to do this with GitPython
#     res = subprocess.run(
#         f"git remote show {origin_name}".split(),
#         check=True,
#         capture_output=True,
#         cwd=repo.working_tree_dir,
#     )
#     for line in res.stdout.decode("utf-8").splitlines():
#         if line.strip().startswith("HEAD branch:"):
#             return line.replace("HEAD branch:", "").strip()
# 
#     raise NotImplementedError(f"No remote called {origin_name!r}")
# 
# Path: gg/state.py
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# Path: gg/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):
# 
# Path: gg/builtins/branches/gg_branches.py
# def find(repo, searchstring, exact=False):
#     # When you copy-to-clipboard from GitHub you get something like
#     # 'peterbe:1545809-urllib3-1242' for example.
#     # But first, it exists as a local branch, use that.
#     if searchstring and ":" in searchstring:
#         remote_name = searchstring.split(":")[0]
#         for remote in repo.remotes:
#             if remote.name == remote_name:
#                 # remote.pull()
#                 found_remote = remote
#                 break
#         else:
#             raise InvalidRemoteName(remote_name)
# 
#         for head in repo.heads:
#             if exact:
#                 if searchstring.split(":", 1)[1].lower() == head.name.lower():
#                     yield head
#                     return
#             else:
#                 if searchstring.split(":", 1)[1].lower() in head.name.lower():
#                     yield head
#                     return
# 
#         info_out(f"Fetching the latest from {found_remote}")
#         for fetchinfo in found_remote.fetch():
#             if fetchinfo.flags & git.remote.FetchInfo.HEAD_UPTODATE:
#                 # Most boring
#                 pass
#             else:
#                 msg = "updated"
#                 if fetchinfo.flags & git.remote.FetchInfo.FORCED_UPDATE:
#                     msg += " (force updated)"
#                 print(fetchinfo.ref, msg)
# 
#             if str(fetchinfo.ref) == searchstring.replace(":", "/", 1):
#                 yield fetchinfo.ref
# 
#     for head in repo.heads:
#         if searchstring:
#             if exact:
#                 if searchstring.lower() != head.name.lower():
#                     continue
#             else:
#                 if searchstring.lower() not in head.name.lower():
#                     continue
#         yield head


import click
from gg.utils import error_out, info_out, get_default_branch
from gg.state import read
from gg.main import cli, pass_config
from gg.builtins.branches.gg_branches import find
    origin_name = state.get("ORIGIN_NAME", "origin")
    # default_branch = state.get("DEFAULT_BRANCH", "master")
    default_branch = get_default_branch(repo, origin_name)

    branches_ = list(find(repo, searchstring))
    if not branches_:
        error_out("No branches found")
    elif len(branches_) > 1:
        error_out(
            "More than one branch found.{}".format(
                "\n\t".join([""] + [x.name for x in branches_])
            )
        )

    assert len(branches_) == 1
    branch_name = branches_[0].name
    active_branch = repo.active_branch
    if branch_name == active_branch.name:
        error_out("Can't clean up the current active branch.")
    # branch_name = active_branch.name
    upstream_remote = None
    fork_remote = None

    origin_name = state.get("ORIGIN_NAME", "origin")
    for remote in repo.remotes:
        if remote.name == origin_name:
            # remote.pull()
            upstream_remote = remote
            break
    if not upstream_remote:

# Path: gg/utils.py
# def error_out(msg, raise_abort=True):
#     click.echo(click.style(msg, fg="red"))
#     if raise_abort:
#         raise click.Abort
# 
# def info_out(msg):
#     click.echo(msg)
# 
# def get_default_branch(repo, origin_name):
#     # TODO: Figure out how to do this with GitPython
#     res = subprocess.run(
#         f"git remote show {origin_name}".split(),
#         check=True,
#         capture_output=True,
#         cwd=repo.working_tree_dir,
#     )
#     for line in res.stdout.decode("utf-8").splitlines():
#         if line.strip().startswith("HEAD branch:"):
#             return line.replace("HEAD branch:", "").strip()
# 
#     raise NotImplementedError(f"No remote called {origin_name!r}")
# 
# Path: gg/state.py
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# Path: gg/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):
# 
# Path: gg/builtins/branches/gg_branches.py
# def find(repo, searchstring, exact=False):
#     # When you copy-to-clipboard from GitHub you get something like
#     # 'peterbe:1545809-urllib3-1242' for example.
#     # But first, it exists as a local branch, use that.
#     if searchstring and ":" in searchstring:
#         remote_name = searchstring.split(":")[0]
#         for remote in repo.remotes:
#             if remote.name == remote_name:
#                 # remote.pull()
#                 found_remote = remote
#                 break
#         else:
#             raise InvalidRemoteName(remote_name)
# 
#         for head in repo.heads:
#             if exact:
#                 if searchstring.split(":", 1)[1].lower() == head.name.lower():
#                     yield head
#                     return
#             else:
#                 if searchstring.split(":", 1)[1].lower() in head.name.lower():
#                     yield head
#                     return
# 
#         info_out(f"Fetching the latest from {found_remote}")
#         for fetchinfo in found_remote.fetch():
#             if fetchinfo.flags & git.remote.FetchInfo.HEAD_UPTODATE:
#                 # Most boring
#                 pass
#             else:
#                 msg = "updated"
#                 if fetchinfo.flags & git.remote.FetchInfo.FORCED_UPDATE:
#                     msg += " (force updated)"
#                 print(fetchinfo.ref, msg)
# 
#             if str(fetchinfo.ref) == searchstring.replace(":", "/", 1):
#                 yield fetchinfo.ref
# 
#     for head in repo.heads:
#         if searchstring:
#             if exact:
#                 if searchstring.lower() != head.name.lower():
#                     continue
#             else:
#                 if searchstring.lower() not in head.name.lower():
#                     continue
#         yield head


# Path: gg/builtins/cleanup/gg_cleanup.py
import click
from gg.utils import error_out, info_out, get_default_branch
from gg.state import read
from gg.main import cli, pass_config
from gg.builtins.branches.gg_branches import find

    origin_name = state.get("ORIGIN_NAME", "origin")
    # default_branch = state.get("DEFAULT_BRANCH", "master")
    default_branch = get_default_branch(repo, origin_name)

    branches_ = list(find(repo, searchstring))
    if not branches_:
        error_out("No branches found")
    elif len(branches_) > 1:
        error_out(
            "More than one branch found.{}".format(
                "\n\t".join([""] + [x.name for x in branches_])
            )
        )

    assert len(branches_) == 1
    branch_name = branches_[0].name
    active_branch = repo.active_branch
    if branch_name == active_branch.name:
        error_out("Can't clean up the current active branch.")
    # branch_name = active_branch.name
    upstream_remote = None
    fork_remote = None

    origin_name = state.get("ORIGIN_NAME", "origin")
    for remote in repo.remotes:
        if remote.name == origin_name:
            # remote.pull()
            upstream_remote = remote
            break
    if not upstream_remote:

        error_out("No remote called {!r} found".format(origin_name))