Skip to content

Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd) issue #916

Closed

Description

@CaptainJL

Hey all,

I am having an issue with using the custom_environment parameter.
This is on a Windows 10 machine.

Basically this will not work, returning the error (see under code)

import git
from git import Repo
from git import Git
git_ssh_cmd = 'ssh -i gpt_key'
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
     Repo.clone_from('git@bitbucket.org:<private git>.git', 'python_test_cloning')

git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

However setting the env variable in the Repo.clone_from command works fine:
Repo.clone_from('git@bitbucket.org:<private git>.git', 'python_test_cloning', env={"GIT_SSH_COMMAND": 'ssh -i gpt_key'})

I need to be able to use the ssh key for operation other than cloning, so I need to be able to set up the local environment purely via the python interface. This also fails for reference (when I did sucessfully clone).

repo = git.Repo("python_test_cloning")
with repo.git.custom_environment(GIT_SSH_COMMAND='ssh -i gpt_key'):
    for remote in repo.remotes:
        remote.fetch()

with error:

git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
cmdline: git fetch -v origin
stderr: 'fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.'

Any ideas of why this is not working and how to fix it?
Remember that this is Windows 10.

Thanks,
CaptainJL

Activity

  1. Byron commented on Sep 9, 2019

    @Byron
    Member

    Thanks for the detailed information! Something I would do to verify the custom environment information is actually passed by enabling debug output in the logs.

    Something you could do beforehand though is to use a fully qualified, absolute path to the key file, such as in with repo.git.custom_environment(GIT_SSH_COMMAND='ssh -i /path/to/gpt_key'):... .

    Please let me know if this worked, or when there are interesting logs to look at. Thanks

  2. CaptainJL commented on Sep 9, 2019

    @CaptainJL
    Author

    Hey @Byron,
    I had done the full string multiple times and it did exactly the same.

    I have attached the debug-level log from the command:

    with Git().custom_environment(GIT_SSH_COMMAND='ssh -i C:\\Users\\jstev\\Documents\\testing\\python_git_testing\\gpt_key', GIT_PYTHON_TRACE="full"):
        git.Repo.clone_from('git@bitbucket.org:<private git>.git', 'python_test_cloning', branch='master')
        logging.debug(os.environ)
    

    From this it appears pretty clear the environment variable wasnt set correctly, considering I can't find 'GIT_PYTHON_TRACE' in the log when I logged 'os.environ'.
    Although I don't know gitpython well enough to really make any judgement on its operations.

    log_debug_failed.log

    Thanks,
    CaptainJL

  3. CaptainJL commented on Sep 12, 2019

    @CaptainJL
    Author

    Just bumping this thread. @Byron

  4. Byron commented on Sep 12, 2019

    @Byron
    Member

    Thank, @CaptainJL . Please note that GIT_PYTHON_TRACE must be set when running the python executable to become effective, along with enabling logging within python.

    GIT_PYTHON_TRACE=full python main.py

    main.py

    import logging
    logging.basicConfig(level=logging.INFO)
    
    code_causing_the_issue()

    This should yield more information.

  5. CaptainJL commented on Sep 13, 2019

    @CaptainJL
    Author

    I have done a little more probing and added additional logging lines:

        def update_environment(self, **kwargs):
            ...
            logging.debug("env in update_environment:" + str(self.environment()))
            return old_env
    
        def clone_from(cls, url, to_path, progress=None, env=None, multi_options=None, **kwargs):
            ...
            logging.debug("env in clone_from:", git.environment())
            return cls._clone(git, url, to_path, GitCmdObjectDB, progress, multi_options, **kwargs)
    

    For the failing case using with with Git().custom_environment(GIT_SSH_COMMAND='ssh -i gpt_key'):
    I get log:

    DEBUG:root:env in update_environment:{'GIT_SSH_COMMAND': 'ssh -i gpt_key'}
    DEBUG:root:env in clone_from: {}
    DEBUG:git.util:Failed checking if running in CYGWIN due to: FileNotFoundError(2, 'The system cannot find the file specified', None, 2, None)
    INFO:git.cmd:git clone -v git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git python_test_cloning
    DEBUG:git.cmd:Popen(['git', 'clone', '-v', 'git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git', 'python_test_cloning'], cwd=C:\Users\jstev\Documents\testing\python_git_testing, universal_newlines=True, shell=None, istream=None)
    DEBUG:git.repo.base:Cmd(['git', 'clone', '-v', 'git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git', 'python_test_cloning'])'s unused stdout:
    DEBUG:git.cmd:AutoInterrupt wait stderr: b"Cloning into 'python_test_cloning'...\ngit@bitbucket.org: Permission denied (publickey).\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n"
    DEBUG:root:env in update_environment:{}

    But with the success case setting env in clone_from I get:

    DEBUG:root:env in update_environment:{'GIT_SSH_COMMAND': 'ssh -i gpt_key'}
    DEBUG:root:env in clone_from: {'GIT_SSH_COMMAND': 'ssh -i gpt_key'}
    DEBUG:git.util:Failed checking if running in CYGWIN due to: FileNotFoundError(2, 'The system cannot find the file specified', None, 2, None)
    INFO:git.cmd:git clone -v git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git python_test_cloning
    DEBUG:git.cmd:Popen(['git', 'clone', '-v', 'git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git', 'python_test_cloning'], cwd=C:\Users\jstev\Documents\testing\python_git_testing, universal_newlines=True, shell=None, istream=None)
    DEBUG:git.repo.base:Cmd(['git', 'clone', '-v', 'git@bitbucket.org:Jean-Luc_BluePrintLab/python_test_cloning.git', 'python_test_cloning'])'s unused stdout:
    DEBUG:root:env in update_environment:{'GIT_SSH_COMMAND': 'ssh -i gpt_key'}

    It is quite clear than clone_from does not get the enviuornment parameters from Git.environment()

  6. CaptainJL commented on Sep 13, 2019

    @CaptainJL
    Author

    Hey @Byron , so I have actually found a solution to this based on this thread: #306

    It turns out that this code below creates 2 different instances of Git() that don't share git instances, which appears to be why it does not work.

    import git
    from git import Git
    from git import Repo
    with Git().custom_environment(GIT_SSH_COMMAND='ssh -i <path/to/sshkey>'):
         Repo.clone_from('git@bitbucket.org:<private git>.git', 'python_test_cloning')
    

    As such, you guys created the 'env' parameter to deal with it.
    However, if you use its own instance, as such by the code below, it works fine, although this can only be used after the repository has been cloned as "repo = git.Repo()" runs into an exception.

    repo = git.Repo("python_test_cloning")
    with repo.git.custom_environment(GIT_SSH_COMMAND=git_ssh_command):
        # Work with pre-cloned git repository
        ......
    

    Thanks for you help though @Byron,
    CaptainJL

  7. Byron commented on Sep 13, 2019

    @Byron
    Member

    Thanks, @CaptainJL , I should have been more vigilant! The solution you propose, using the env parameter, is indeed what is intended.
    Hopefully this issue will help others running into a similar problem.

    As the problem seems solved, I am closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions