I'm trying to integrate gitpython with an IDE, but I'm having some problems with push.
remote_repo = self.repo.remotes[remote]
remote_repo.push(self.repo.active_branch.name)
When I run this command, or just
git push --porcelain origin master
the prompt asks my ssh password.
Enter passphrase for key '/home/user/.ssh/id_rsa':
My problem is:
How can I workaround it and provide an interface to identify if a password is necessary, and if so be able to provide it?
In case you would like to have full control over how an ssh connection is made, and if you use git 2.3 or newer, you may instantiate git with the GIT_SSH_COMMAND
environment variable set. It points to a script which is called in place of ssh. Therefore you are enabled to figure out whether or not a password is required, and to launch additional GUI to obtain the desired input.
In code, it would look something like this:
remote_repo = self.repo.remotes[remote]
# here is where you could choose an executable based on the platform.
# Shell scripts might just not work plainly on windows.
ssh_executable = os.path.join(rw_dir, 'my_ssh_executable.sh')
# This permanently changes all future calls to git to have the given environment variables set
# You can pass additional information in your own environment variables as well.
self.repo.git.update_environment(GIT_SSH_COMMAND=ssh_executable)
# now all calls to git which require SSH interaction call your executable
remote_repo.push(self.repo.active_branch.name)
Please note that this only works for accessing resources via SSH. In case the protocol is HTTPS for example, a password prompt might be given anyway.
Prior to git 2.3, you can use the GIT_SSH
environment variable. It works differently as it is expected to contain only the path to the ssh
program, to which additional arguments will be passed. Of course, this could be your script as well similar to what was shown above. I'd like to point out the differences between these two environment variables more precisely, but am lacking the personal experience to do so.