# 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
    upstream_remote.pull(repo.heads[default_branch])

    # Is this one of the merged branches?!
    # XXX I don't know how to do this "nativly" with GitPython.
    merged_branches = [
        x.strip()
        for x in repo.git.branch("--merged").splitlines()
        if x.strip() and not x.strip().startswith("*")
    ]
    was_merged = branch_name in merged_branches
    certain = was_merged or force
    if not certain:
        # Need to ask the user.
        # XXX This is where we could get smart and compare this branch
        # with the default branch.
        certain = (
            input("Are you certain {} is actually merged? [Y/n] ".format(branch_name))
            .lower()
            .strip()
            != "n"
        )
    if not certain:
        return 1

    if was_merged:
        repo.git.branch("-d", branch_name)
    else:
        repo.git.branch("-D", branch_name)

    fork_remote = None

# 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

    upstream_remote.pull(repo.heads[default_branch])

    # Is this one of the merged branches?!
    # XXX I don't know how to do this "nativly" with GitPython.
    merged_branches = [
        x.strip()
        for x in repo.git.branch("--merged").splitlines()
        if x.strip() and not x.strip().startswith("*")
    ]
    was_merged = branch_name in merged_branches
    certain = was_merged or force
    if not certain:
        # Need to ask the user.
        # XXX This is where we could get smart and compare this branch
        # with the default branch.
        certain = (
            input("Are you certain {} is actually merged? [Y/n] ".format(branch_name))
            .lower()
            .strip()
            != "n"
        )
    if not certain:
        return 1

    if was_merged:
        repo.git.branch("-d", branch_name)
    else:
        repo.git.branch("-D", branch_name)

    fork_remote = None

    state = read(config.configfile)