GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

pulling from private repo

Open rogelioamancisidor opened this issue 3 years ago • 2 comments

I can clone from a private repo by specifying the path to he ssh key, like this:

git_dir = "[email protected]:user_id/repo_name.git"
save_at = './'
Repo.clone_from(git_dir, save_at, env={"GIT_SSH_COMMAND": 'ssh -o StrictHostKeyChecking=no  -i ./my_path/mykey'})

However, I this code cannot pull from the same private repo

repo = Repo(save_at)
ssh_cmd =  'ssh -o StrictHostKeyChecking=no  -i ./my_path/mykey'}
with repo.git.custom_environment(GIT_SSH_COMMAND=ssh_cmd):
    repo.remotes.origin.pull('master')

and throws the error

File "/usr/local/lib/python3.10/site-packages/git/remote.py", line 910, in pull
    res = self._get_fetch_info_from_stderr(proc, progress,
  File "/usr/local/lib/python3.10/site-packages/git/remote.py", line 750, in _get_fetch_info_from_stderr
    proc.wait(stderr=stderr_text)
  File "/usr/local/lib/python3.10/site-packages/git/cmd.py", line 502, in wait
    raise GitCommandError(remove_password_if_present(self.args), status, errstr)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
  cmdline: git pull -v origin master
  stderr: 'fatal: Could not read from remote repository.'

What am I missing here?

rogelioamancisidor avatar Sep 07 '22 08:09 rogelioamancisidor

With the GIT_PYTHON_TRACE=1 and GIT_TRACE=1 environment variable set, you should see which commands are actually invoked and maybe get to the bottom of it.

Generally the approach of using GIT_SSH_COMMAND should work with custom_environment.

Byron avatar Sep 07 '22 09:09 Byron

Can I set the environmental variables in the python code? My code runs in a kubernetes container so it will be to much of a trouble to declare those env variables in a secret so I can hopefully debug what is going on.

I updated the error message. Do you know what does GitCommandError(remove_password_if_present(self.args), status, errstr) mean?

rogelioamancisidor avatar Sep 07 '22 17:09 rogelioamancisidor