Git ssh with std::process::Command

Hello everyone,

I'm writing a rust script to execute some code on different git repositories. As I want the experiments to be reproducible the tools must automatically clone the repositories. I though that the best way to keep control access is to generate SSH key for the users. The idea is to grant access for collaborators only in a first time and after publish the private key when the code is released.

I view on a forum that a SSH key can be given to git with the command : GIT_SSH_COMMAND="ssh -i /path/to/private_id" git clone

My first question is : How I indicate to std::process::Command that I want to prefix git clone with GIT_SSH_COMMAND ?

My second question is : When I run the command line, git ask :

The authenticity of host ' (' can't be established.
RSA key fingerprint is SHA256:foobarbazxxxxxxxx....
Are you sure you want to continue connecting (yes/no/[fingerprint])? 

I want to the user to answer the question, but I don't know how to pipe the user input into the subprocess.

Thank you to help ! :grinning:

First question: You can use Command::env method to set an environment variable (GIT_SSH_COMMAND is passed to git as an environment variable).

Second question: You can use Command::stdin with Stdio::inherit() to transparently pass user input to the child process.

See the standard library documentation for Command for more information.

Thank you very, clear and very nice answer !

I highly recommend using the git2 crate for this to avoid problems just like the one you encountered.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.