# 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 is_github(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "github" in urlparse(data["url"]).netloc
#     return False
# 
# Path: gg/state.py
# def save(configfile, description, branch_name, **extra):
#     repo_name = get_repo_name()
#     key = f"{repo_name}:{branch_name}"
#     new = {key: extra}
#     new[key].update(
#         {"description": description, "date": datetime.datetime.now().isoformat()}
#     )
#     update(configfile, new)
# 
# 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/bugzilla.py
# @cli.group()
# @click.option(
#     "-u",
#     "--bugzilla-url",
#     default=BUGZILLA_URL,
#     help=f"URL to Bugzilla instance (default: {BUGZILLA_URL})",
# )
# @pass_config
# def bugzilla(config, bugzilla_url):
#     """General tool for connecting to Bugzilla. The default URL
#     is that for bugzilla.mozilla.org but you can override that.
#     Once you're signed in you can use those credentials to automatically
#     fetch bug summaries even of private bugs.
#     """
#     config.bugzilla_url = bugzilla_url
# 
# Path: gg/builtins/github.py
# @cli.group()
# @click.option(
#     "-u",
#     "--github-url",
#     default=GITHUB_URL,
#     help=f"URL to GitHub instance (default: {GITHUB_URL})",
# )
# @pass_config
# def github(config, github_url):
#     """For setting up a GitHub API token."""
#     config.github_url = github_url
# 
# 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 re
import getpass
import urllib
import click
from gg.utils import error_out, info_out, is_github
from gg.state import save, read
from gg.main import cli, pass_config
from gg.builtins import bugzilla
from gg.builtins import github
from gg.builtins.branches.gg_branches import find
        # Looping over the remotes, let's figure out which one
        # is the one that has issues. Let's try every one that isn't
        # your fork remote.
        for origin in repo.remotes:
            if origin.name == fork_name:
                continue
            url = origin.url
            org, repo = parse_remote_url(origin.url)
            github_title, github_url = github.get_title(
                config, org, repo, int(bugnumber)
            )
            if github_title:
                candidates.append((github_title, int(bugnumber), github_url))

        bugzilla_summary, bugzilla_url = bugzilla.get_summary(config, bugnumber)
        if bugzilla_summary:
            candidates.append((bugzilla_summary, int(bugnumber), bugzilla_url))

        if len(candidates) > 1:
            info_out(
                "Input is ambiguous. Multiple possibilities found. "
                "Please re-run with the full URL:"
            )
            for title, _, url in candidates:
                info_out("\t{}".format(url))
                info_out("\t{}\n".format(title))
            error_out("Awaiting your choice")
        elif len(candidates) == 1:
            return candidates[0]
        else:

# 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 is_github(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "github" in urlparse(data["url"]).netloc
#     return False
# 
# Path: gg/state.py
# def save(configfile, description, branch_name, **extra):
#     repo_name = get_repo_name()
#     key = f"{repo_name}:{branch_name}"
#     new = {key: extra}
#     new[key].update(
#         {"description": description, "date": datetime.datetime.now().isoformat()}
#     )
#     update(configfile, new)
# 
# 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/bugzilla.py
# @cli.group()
# @click.option(
#     "-u",
#     "--bugzilla-url",
#     default=BUGZILLA_URL,
#     help=f"URL to Bugzilla instance (default: {BUGZILLA_URL})",
# )
# @pass_config
# def bugzilla(config, bugzilla_url):
#     """General tool for connecting to Bugzilla. The default URL
#     is that for bugzilla.mozilla.org but you can override that.
#     Once you're signed in you can use those credentials to automatically
#     fetch bug summaries even of private bugs.
#     """
#     config.bugzilla_url = bugzilla_url
# 
# Path: gg/builtins/github.py
# @cli.group()
# @click.option(
#     "-u",
#     "--github-url",
#     default=GITHUB_URL,
#     help=f"URL to GitHub instance (default: {GITHUB_URL})",
# )
# @pass_config
# def github(config, github_url):
#     """For setting up a GitHub API token."""
#     config.github_url = github_url
# 
# 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/start/gg_start.py
import re
import getpass
import urllib
import click
from gg.utils import error_out, info_out, is_github
from gg.state import save, read
from gg.main import cli, pass_config
from gg.builtins import bugzilla
from gg.builtins import github
from gg.builtins.branches.gg_branches import find

        # Looping over the remotes, let's figure out which one
        # is the one that has issues. Let's try every one that isn't
        # your fork remote.
        for origin in repo.remotes:
            if origin.name == fork_name:
                continue
            url = origin.url
            org, repo = parse_remote_url(origin.url)
            github_title, github_url = github.get_title(
                config, org, repo, int(bugnumber)
            )
            if github_title:
                candidates.append((github_title, int(bugnumber), github_url))

        bugzilla_summary, bugzilla_url = bugzilla.get_summary(config, bugnumber)
        if bugzilla_summary:
            candidates.append((bugzilla_summary, int(bugnumber), bugzilla_url))

        if len(candidates) > 1:
            info_out(
                "Input is ambiguous. Multiple possibilities found. "
                "Please re-run with the full URL:"
            )
            for title, _, url in candidates:
                info_out("\t{}".format(url))
                info_out("\t{}\n".format(title))
            error_out("Awaiting your choice")
        elif len(candidates) == 1:
            return candidates[0]
        else:

            error_out("ID could not be found on GitHub or Bugzilla")