# Path: rebasehelper/constants.py
# ENCODING: str = locale.getpreferredencoding()
# 
# Path: rebasehelper/helpers/git_helper.py
# class GitHelper:
# 
#     """Class which operates with git repositories"""
# 
#     # provide fallback values if system is not configured
#     GIT_USER_NAME: str = 'rebase-helper'
#     GIT_USER_EMAIL: str = 'rebase-helper@localhost.local'
# 
#     @classmethod
#     def get_user(cls):
#         try:
#             return git.cmd.Git().config('user.name', get=True, stdout_as_string=True)
#         except git.GitCommandError:
#             logger.warning("Failed to get configured git user name, using '%s'", cls.GIT_USER_NAME)
#             return cls.GIT_USER_NAME
# 
#     @classmethod
#     def get_email(cls):
#         try:
#             return git.cmd.Git().config('user.email', get=True, stdout_as_string=True)
#         except git.GitCommandError:
#             logger.warning("Failed to get configured git user email, using '%s'", cls.GIT_USER_EMAIL)
#             return cls.GIT_USER_EMAIL
# 
#     @classmethod
#     def run_mergetool(cls, repo):
#         # we can't use GitPython here, as it doesn't allow
#         # for the command to attach to stdout directly
#         cwd = os.getcwd()
#         try:
#             os.chdir(repo.working_tree_dir)
#             ProcessHelper.run_subprocess(['git', 'mergetool'])
#         finally:
#             os.chdir(cwd)


import os
import git  # type: ignore
import pytest  # type: ignore
from rebasehelper.constants import ENCODING
from rebasehelper.helpers.git_helper import GitHelper
                work_git_path = os.path.join(workdir, 'git')
                os.makedirs(work_git_path)

                config_file = os.path.join(work_git_path, 'config')
                self.write_config_file(config_file, name, email)
                os.environ['HOME'] = workdir
                os.environ['XDG_CONFIG_HOME'] = workdir
            elif config == 'global_include':
                work_git_path = os.path.join(workdir, 'git')
                os.makedirs(work_git_path)

                config_file = os.path.join(work_git_path, 'config')
                with open(config_file, 'w', encoding=ENCODING) as f:
                    f.write('[include]\n'
                            '    path = included_config\n')
                included_config_file = os.path.join(work_git_path, 'included_config')
                self.write_config_file(included_config_file, name, email)
                os.environ['HOME'] = workdir
                os.environ['XDG_CONFIG_HOME'] = workdir
            elif config == 'local':
                repo = git.Repo.init(workdir)
                repo.git.config('user.name', name, local=True)
                repo.git.config('user.email', email, local=True)
            elif config == 'env':
                config_file = os.path.join(workdir, 'git_config')
                os.environ['GIT_CONFIG'] = config_file
                self.write_config_file(config_file, name, email)
            else:
                raise RuntimeError()


# Path: rebasehelper/constants.py
# ENCODING: str = locale.getpreferredencoding()
# 
# Path: rebasehelper/helpers/git_helper.py
# class GitHelper:
# 
#     """Class which operates with git repositories"""
# 
#     # provide fallback values if system is not configured
#     GIT_USER_NAME: str = 'rebase-helper'
#     GIT_USER_EMAIL: str = 'rebase-helper@localhost.local'
# 
#     @classmethod
#     def get_user(cls):
#         try:
#             return git.cmd.Git().config('user.name', get=True, stdout_as_string=True)
#         except git.GitCommandError:
#             logger.warning("Failed to get configured git user name, using '%s'", cls.GIT_USER_NAME)
#             return cls.GIT_USER_NAME
# 
#     @classmethod
#     def get_email(cls):
#         try:
#             return git.cmd.Git().config('user.email', get=True, stdout_as_string=True)
#         except git.GitCommandError:
#             logger.warning("Failed to get configured git user email, using '%s'", cls.GIT_USER_EMAIL)
#             return cls.GIT_USER_EMAIL
# 
#     @classmethod
#     def run_mergetool(cls, repo):
#         # we can't use GitPython here, as it doesn't allow
#         # for the command to attach to stdout directly
#         cwd = os.getcwd()
#         try:
#             os.chdir(repo.working_tree_dir)
#             ProcessHelper.run_subprocess(['git', 'mergetool'])
#         finally:
#             os.chdir(cwd)


# Path: rebasehelper/tests/helpers/test_git_helper.py
import os
import git  # type: ignore
import pytest  # type: ignore
from rebasehelper.constants import ENCODING
from rebasehelper.helpers.git_helper import GitHelper

                work_git_path = os.path.join(workdir, 'git')
                os.makedirs(work_git_path)

                config_file = os.path.join(work_git_path, 'config')
                self.write_config_file(config_file, name, email)
                os.environ['HOME'] = workdir
                os.environ['XDG_CONFIG_HOME'] = workdir
            elif config == 'global_include':
                work_git_path = os.path.join(workdir, 'git')
                os.makedirs(work_git_path)

                config_file = os.path.join(work_git_path, 'config')
                with open(config_file, 'w', encoding=ENCODING) as f:
                    f.write('[include]\n'
                            '    path = included_config\n')
                included_config_file = os.path.join(work_git_path, 'included_config')
                self.write_config_file(included_config_file, name, email)
                os.environ['HOME'] = workdir
                os.environ['XDG_CONFIG_HOME'] = workdir
            elif config == 'local':
                repo = git.Repo.init(workdir)
                repo.git.config('user.name', name, local=True)
                repo.git.config('user.email', email, local=True)
            elif config == 'env':
                config_file = os.path.join(workdir, 'git_config')
                os.environ['GIT_CONFIG'] = config_file
                self.write_config_file(config_file, name, email)
            else:
                raise RuntimeError()


            assert name == GitHelper.get_user()