Search code examples
pythongitsshsubprocessclone

subprocess.check_call: Command '['git', 'clone']' returned non-zero exit status 127


I am working on a microservice for which have written the following script for a user, which wants to ssh into an remote environment and git clone a repo. I have specified the GIT_SSH and GIT_PYTHON_GIT_EXECUTABLE for the environment and then run git clone.

def clone_if_local_repo_absent(local_repo_path, remote_repo_url):
    GIT_SSH = '...'
    GIT_PYTHON_GIT_EXECUTABLE = '/usr/bin/git'
    my_env = os.environ.copy()
    my_env["GIT_SSH"] = GIT_SSH
    my_env["GIT_PYTHON_GIT_EXECUTABLE"] = GIT_PYTHON_GIT_EXECUTABLE
    # Clones to directory specified
    exe_command = shlex.split('git clone {remote_repo_url} {repo_name}'.format(remote_repo_url=remote_repo_url, repo_name=local_repo_path))
    subprocess.check_call(exe_command, env=my_env, shell=True)

On running the script, I get the error: Command '['git', 'clone', remote_repo_url, 'LOCAL']' returned non-zero exit status 127. How can I fix this error?


Solution

  • As mentioned in Git ProBook:

    GIT_SSH, if specified, is a program that is invoked instead of ssh when Git tries to connect to an SSH host.
    It is invoked like $GIT_SSH [username@]host [-p <port>] <command>.

    Note that this isn’t the easiest way to customize how ssh is invoked; it won’t support extra command-line parameters, so you’d have to write a wrapper script and set GIT_SSH to point to it.
    It’s probably easier just to use the ~/.ssh/config file for that.

    SO this is not executed on the remote side at all. It is executed to connect to the remote server.

    If Git is not installed on the remote environment, any git clone executed from the ssh session would not work.