简体   繁体   English

用于克隆 GitHub 存储库的 Shell 脚本

[英]Shell script to clone a GitHub Repo

I am trying to automate a process that contains a series of git commands.我正在尝试自动化一个包含一系列 git 命令的过程。

I want the shell script to deal with some interactive commands, like passing the username and password to git clone url -v .我希望 shell 脚本处理一些交互式命令,比如将用户名和密码传递给git clone url -v I verified that if I just run git clone url -v it will show the following in order:我确认如果我只运行git clone url -v它将按顺序显示以下内容:

  1. cloning into someRepo克隆到 someRepo
  2. asking for username要求用户名
  3. asking for password要求密码

I've tried:我试过了:

  1. echo -e 'username\\n' | git clone url -v
  2. echo -e 'username\\npassword\\n' | git clone url -v
  3. git clone url -v <<< username\\npassword\\n
  4. (sleep 5;echo -e 'username\\n' | git clone url -v)

I thought that the first message cloning into repo will take some time.我认为cloning into repo的第一条消息需要一些时间。 None of them is working, but all of them are showing the same message that Username for url:它们都没有工作,但它们都显示了与Username for url:相同的消息Username for url:

Having spent lots of time in this, I know that在这方面花了很多时间,我知道

git clone https://$username:$password@enterpriseGithub.com/org/repo

is working, but it is UNSAFE to use since the log show the username and password explicitly.正在工作,但使用它是不安全的,因为日志明确显示了用户名和密码。

Better practice would be to avoid user/password authentication at all (as by configuring agent-based auth, ideally backed by private keys stored on physical tokens), or set up credential storage in a keystore provided (and hopefully secured) by your operating system -- but if you just want to keep credentials off the command line, that can be done:更好的做法是避免用户名/密码认证在所有(如通过配置基于代理的身份验证,最好通过存储在物理令牌私有密钥的支持),或者你的操作系统设置凭据存储中提供的密钥库(希望担保) -- 但是,如果您只想将凭据保留在命令行之外,则可以这样做:

# Assume that we already know the credentials we want to store...
gitUsername="some"; gitPassword="values"

# Create a file containing the credentials readable only to the current user
mkdir -p "$HOME/.git-creds/https"
chmod 700 "$HOME/.git-creds"
cat >"$HOME/.git-creds/https/enterprise-github.com" <<EOF
username=$gitUsername
password=$gitPassword
EOF

# Generate a script that can retrieve stored credentials
mkdir -p -- "$HOME/bin"
cat >"$HOME/bin/git-retrieve-creds" <<'EOF'
#!/usr/bin/env bash
declare -A args=( )
while IFS= read -r line; do
  case $line in
    *..*) echo "ERROR: Invalid request" >&2; exit 1;;
    *=*)  args[${line%%=*}]=${line#*=} ;;
    '')   break ;;
  esac
done

[[ ${args[protocol]} && ${args[host]} ]] || {
  echo "Did not retrieve protocol and host" >&2; exit 1;
}
f="$HOME/.git-creds/${args[protocol]}/${args[host]}"
[[ -s $f ]] && cat -- "$f"
EOF
chmod +x "$HOME/bin/git-retrieve-creds"

# And configure git to use that 
git config --global credential.helper "$HOME/bin/git-retrieve-creds"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM