如何使用 REST API 以作者姓名在目录中克隆 repos?

How to clone repos inside a directory with their author name with REST API?

我正在使用 this bash & jq 脚本:

UserName=CHANGEME; \
curl -s https://api.github.com/users/$UserName/repos?per_page=1000 |\
jq -r '.[]|.clone_url' |\
xargs -L1 git clone

它将使用 REST API 参数 name (repo.name) 将存储库克隆到一个目录中。 (默认行为)

我希望它使用 REST API 参数 full_name(因为它由 repo.ownerrepo.name 组成)将存储库克隆到一个目录中,我该怎么做有空去做吗?


这是在我的目录中创建的:

repo.name1
repo.name2

这就是我想要的目录:

repo.owner1\repo.name1
repo.owner2\repo.name2

Answered by Kusalananda:

  1. The individual arguments read by xargs should ideally be quoted.
  2. xargs needs to call git clone with two separate arguments: the repository URL and the destination directory into which to clone it.

jq -r '.[] | .html_url, .full_name' -> jq -r '.[] | [ .html_url, .full_name ]'

  1. Add the @sh operator to output a each such array as a line of shell-quoted words

jq -r '.[] | [ .html_url, .full_name ]' -> jq -r '.[] | [ .html_url, .full_name ] | @sh'


  1. Add -n 2 so that xargs will call the utility with two arguments from its input stream at a time

xargs git clone > xargs -n 2 git clone


  1. Together:
curl -s "https://api.github.com/users/$UserName/repos?per_page=1000" |
jq -r '.[] | [ .html_url, .full_name ] | @sh' |
xargs -n 2 git clone