Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active March 7, 2023 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/36e61f291b2904c4d8ec53c710ca6ae6 to your computer and use it in GitHub Desktop.
Save scivision/36e61f291b2904c4d8ec53c710ca6ae6 to your computer and use it in GitHub Desktop.
Git clone without prompting for HTTPS or SSH password (print message instead)
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()
#!/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