You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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
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.
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.
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()
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, @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.
Activity
Byron commentedon Sep 9, 2019
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
CaptainJL commentedon Sep 9, 2019
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:
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
CaptainJL commentedon Sep 12, 2019
Just bumping this thread. @Byron
Byron commentedon Sep 12, 2019
Thank, @CaptainJL . Please note that
GIT_PYTHON_TRACEmust be set when running thepythonexecutable to become effective, along with enabling logging within python.main.py
This should yield more information.
CaptainJL commentedon Sep 13, 2019
I have done a little more probing and added additional logging lines:
For the failing case using with with
Git().custom_environment(GIT_SSH_COMMAND='ssh -i gpt_key'):I get log:
But with the success case setting env in clone_from I get:
It is quite clear than clone_from does not get the enviuornment parameters from Git.environment()
CaptainJL commentedon Sep 13, 2019
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.
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.
Thanks for you help though @Byron,
CaptainJL
Byron commentedon Sep 13, 2019
Thanks, @CaptainJL , I should have been more vigilant! The solution you propose, using the
envparameter, 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.