# Path: git_trac/git_error.py
# class GitError(RuntimeError):
#     r"""
#     Error raised when git exits with a non-zero exit code.
# 
#     EXAMPLES::
# 
#         sage: from git_trac.git_error import GitError
#         sage: raise GitError({'exit_code':128, 'stdout':'', 'stderr':'', 'cmd':'command'})
#         Traceback (most recent call last):
#         ...
#         GitError: git returned with 
#         non-zero exit code (128) when executing "command"
#     """
#     def __init__(self, result, explain=None, advice=None):
#         r"""
#         Initialization.
# 
#         TESTS::
# 
#             sage: from git_trac.git_error import GitError
#             sage: type(GitError({'exit_code':128, 'stdout':'', 'stderr':'', 'cmd':'command'}))
#             <class 'git_trac.git_error.GitError'>
#         """
#         self.exit_code = result['exit_code']
#         self.cmd = result['cmd'].strip()
#         def prefix(string, prefix):
#             return '\n'.join([prefix + ': ' + line.rstrip() for line in string.splitlines()])
#         self.stdout = prefix(result['stdout'], '    STDOUT')
#         self.stderr = prefix(result['stderr'], '    STDERR')
#         self.explain = explain
#         self.advice = advice
#         template = 'git returned with non-zero exit code ({0}) when executing "{1}"'
#         msg = template.format(self.exit_code, self.cmd)
#         if len(self.stdout) != 0:
#             msg += '\n' + self.stdout
#         if len(self.stderr) != 0:
#             msg += '\n' + self.stderr
#         RuntimeError.__init__(self, msg)
# 
# Path: git_trac/logger.py
# def make_logger(name, doctest_mode=False):
# def enable_doctest_mode():


import os
import subprocess
from .git_error import GitError
from .logger import logger
                k = '-' + k
            else:
                k = '--' + k.replace('_', '-')
            if v is True:
                s.append(k)
            elif v is not False:
                s.append(k+'='+str(v))
        if args:
            s.extend(a for a in args if a is not None)
        s = [str(arg) for arg in s]
        complete_cmd = ' '.join(s)
        self._log('cmd', complete_cmd)

        env.update(os.environ)
        process = subprocess.Popen(s, stdout=popen_stdout, stderr=popen_stderr, env=env)
        stdout, stderr = process.communicate()
        retcode = process.poll()
        if stdout is not None and popen_stdout is subprocess.PIPE:
            stdout = stdout.decode('utf-8')
            self._log('stdout', stdout)
        if stderr is not None and popen_stderr is subprocess.PIPE:
            stderr = stderr.decode('utf-8')
            self._log('stderr', stderr)
        return {'exit_code':retcode, 'stdout':stdout, 'stderr':stderr, 'cmd':complete_cmd}

    def _run(self, cmd, args, kwds={}, popen_stdout=None, popen_stderr=None, exit_code_to_exception=True):
        result = self._run_unsafe(cmd, args, kwds,
                                  popen_stdout=popen_stdout,
                                  popen_stderr=popen_stderr)
        if exit_code_to_exception and result['exit_code']:

# Path: git_trac/git_error.py
# class GitError(RuntimeError):
#     r"""
#     Error raised when git exits with a non-zero exit code.
# 
#     EXAMPLES::
# 
#         sage: from git_trac.git_error import GitError
#         sage: raise GitError({'exit_code':128, 'stdout':'', 'stderr':'', 'cmd':'command'})
#         Traceback (most recent call last):
#         ...
#         GitError: git returned with 
#         non-zero exit code (128) when executing "command"
#     """
#     def __init__(self, result, explain=None, advice=None):
#         r"""
#         Initialization.
# 
#         TESTS::
# 
#             sage: from git_trac.git_error import GitError
#             sage: type(GitError({'exit_code':128, 'stdout':'', 'stderr':'', 'cmd':'command'}))
#             <class 'git_trac.git_error.GitError'>
#         """
#         self.exit_code = result['exit_code']
#         self.cmd = result['cmd'].strip()
#         def prefix(string, prefix):
#             return '\n'.join([prefix + ': ' + line.rstrip() for line in string.splitlines()])
#         self.stdout = prefix(result['stdout'], '    STDOUT')
#         self.stderr = prefix(result['stderr'], '    STDERR')
#         self.explain = explain
#         self.advice = advice
#         template = 'git returned with non-zero exit code ({0}) when executing "{1}"'
#         msg = template.format(self.exit_code, self.cmd)
#         if len(self.stdout) != 0:
#             msg += '\n' + self.stdout
#         if len(self.stderr) != 0:
#             msg += '\n' + self.stderr
#         RuntimeError.__init__(self, msg)
# 
# Path: git_trac/logger.py
# def make_logger(name, doctest_mode=False):
# def enable_doctest_mode():


# Path: git_trac/git_interface.py
import os
import subprocess
from .git_error import GitError
from .logger import logger

                k = '-' + k
            else:
                k = '--' + k.replace('_', '-')
            if v is True:
                s.append(k)
            elif v is not False:
                s.append(k+'='+str(v))
        if args:
            s.extend(a for a in args if a is not None)
        s = [str(arg) for arg in s]
        complete_cmd = ' '.join(s)
        self._log('cmd', complete_cmd)

        env.update(os.environ)
        process = subprocess.Popen(s, stdout=popen_stdout, stderr=popen_stderr, env=env)
        stdout, stderr = process.communicate()
        retcode = process.poll()
        if stdout is not None and popen_stdout is subprocess.PIPE:
            stdout = stdout.decode('utf-8')
            self._log('stdout', stdout)
        if stderr is not None and popen_stderr is subprocess.PIPE:
            stderr = stderr.decode('utf-8')
            self._log('stderr', stderr)
        return {'exit_code':retcode, 'stdout':stdout, 'stderr':stderr, 'cmd':complete_cmd}

    def _run(self, cmd, args, kwds={}, popen_stdout=None, popen_stderr=None, exit_code_to_exception=True):
        result = self._run_unsafe(cmd, args, kwds,
                                  popen_stdout=popen_stdout,
                                  popen_stderr=popen_stderr)
        if exit_code_to_exception and result['exit_code']:

            raise GitError(result)