# 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/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):
# 
# Path: gg/state.py
# def load(configfile, branch_name):
#     # like read() but returning specifically the state for the current repo
#     key = f"{get_repo_name()}:{branch_name}"
#     return read(configfile)[key]
# 
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# def load_config(configfile, key):
#     # like read() but returning specifically the state for the current repo
#     key = f"{get_repo_name()}:_config:{key}"
#     return read(configfile)[key]
# 
# 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 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}")
# 
# def info_out(msg):
#     click.echo(msg)
# 
# def is_bugzilla(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "bugzilla" in urlparse(data["url"]).netloc
#     return False
# 
# def is_github(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "github" in urlparse(data["url"]).netloc
#     return False
# 
# def success_out(msg):
#     click.echo(click.style(msg, fg="green"))


import os
import re
import time
import click
import git
from gg.builtins import github
from gg.main import cli, pass_config
from gg.state import load, read, load_config
from gg.utils import (
    error_out,
    get_default_branch,
    info_out,
    is_bugzilla,
    is_github,
    success_out,
)

    # First group all untracked files by root folder
    all_untracked_files = {}
    for path in repo.untracked_files:
        root = path.split(os.path.sep)[0]
        if root not in all_untracked_files:
            all_untracked_files[root] = {
                "files": [],
                "total_count": count_files_in_directory(root),
            }
        all_untracked_files[root]["files"].append(path)

    # Now filter this based on it being single files or a bunch
    untracked_files = {}
    for root, info in all_untracked_files.items():
        for path in info["files"]:
            age = now - os.stat(path).st_mtime
            # If there's fewer untracked files in its directory, suggest
            # the directory instead.
            if info["total_count"] == 1:
                path = root
            if path in untracked_files:
                if age < untracked_files[path]:
                    # youngest file in that directory
                    untracked_files[path] = age
            else:
                untracked_files[path] = age

    if untracked_files:
        ordered = sorted(untracked_files.items(), key=lambda x: x[1], reverse=True)

# 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/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):
# 
# Path: gg/state.py
# def load(configfile, branch_name):
#     # like read() but returning specifically the state for the current repo
#     key = f"{get_repo_name()}:{branch_name}"
#     return read(configfile)[key]
# 
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# def load_config(configfile, key):
#     # like read() but returning specifically the state for the current repo
#     key = f"{get_repo_name()}:_config:{key}"
#     return read(configfile)[key]
# 
# 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 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}")
# 
# def info_out(msg):
#     click.echo(msg)
# 
# def is_bugzilla(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "bugzilla" in urlparse(data["url"]).netloc
#     return False
# 
# def is_github(data):
#     if data.get("bugnumber") and data.get("url"):
#         return "github" in urlparse(data["url"]).netloc
#     return False
# 
# def success_out(msg):
#     click.echo(click.style(msg, fg="green"))


# Path: gg/builtins/commit/gg_commit.py
import os
import re
import time
import click
import git
from gg.builtins import github
from gg.main import cli, pass_config
from gg.state import load, read, load_config
from gg.utils import (
    error_out,
    get_default_branch,
    info_out,
    is_bugzilla,
    is_github,
    success_out,
)


    # First group all untracked files by root folder
    all_untracked_files = {}
    for path in repo.untracked_files:
        root = path.split(os.path.sep)[0]
        if root not in all_untracked_files:
            all_untracked_files[root] = {
                "files": [],
                "total_count": count_files_in_directory(root),
            }
        all_untracked_files[root]["files"].append(path)

    # Now filter this based on it being single files or a bunch
    untracked_files = {}
    for root, info in all_untracked_files.items():
        for path in info["files"]:
            age = now - os.stat(path).st_mtime
            # If there's fewer untracked files in its directory, suggest
            # the directory instead.
            if info["total_count"] == 1:
                path = root
            if path in untracked_files:
                if age < untracked_files[path]:
                    # youngest file in that directory
                    untracked_files[path] = age
            else:
                untracked_files[path] = age

    if untracked_files:
        ordered = sorted(untracked_files.items(), key=lambda x: x[1], reverse=True)

        info_out("NOTE! There are untracked files:")