Git clone without prompting for HTTPS or SSH password (print message instead)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.5) | |
set(urls | |
"https://github.com/scivision/not-a-repo.git" | |
"ssh://github.com/scivision/not-a-repo.git" | |
) | |
find_package(Git REQUIRED) | |
set(ENV{GIT_TERMINAL_PROMPT} 0) | |
set(ENV{GIT_SSH_COMMAND} "ssh -oBatchMode=yes") | |
foreach(url IN LISTS urls) | |
execute_process( | |
COMMAND ${GIT_EXECUTABLE} clone ${url} | |
RESULT_VARIABLE ret | |
OUTPUT_VARIABLE out OUTPUT_STRIP_TRAILING_WHITESPACE | |
ERROR_VARIABLE err ERROR_STRIP_TRAILING_WHITESPACE | |
) | |
message(STATUS "url: ${url} | |
returncode: ${ret} | |
stdout: ${out} | |
stderr: ${err} | |
") | |
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Without this technique, the clone operation would prompt for a password and hang | |
endlessly or until timemout. | |
""" | |
import os | |
import subprocess | |
import shutil | |
urls = ( | |
"https://github.com/scivision/not-a-repo.git", | |
"https://github.com/scivision/gitMC.git", | |
) | |
if not (git := shutil.which("git")): | |
raise FileNotFoundError("git not found") | |
env = os.environ.copy() | |
env["GIT_TERMINAL_PROMPT"] = "0" | |
env["GIT_SSH_COMMAND"] = "ssh -oBatchMode=yes" | |
for url in urls: | |
ret = subprocess.run([git, "clone", url], env=env, text=True, capture_output=True) | |
print("url:", url) | |
print("returncode:", ret.returncode) | |
print("stdout:", ret.stdout) | |
print("stderr:", ret.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment