Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

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

I'm using this bash & jq script:

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

It will clone the repos into a directory using the REST API parameter name (repo.name). (Default behavior)

I want it to clone the repos into a directory using the REST API parameter full_name (since it consists of repo.owner and repo.name), how do I get around to do that?


This is what is created inside my directory:

repo.name1
repo.name2

This is what I want inside my directory:

repo.owner1
epo.name1
repo.owner2
epo.name2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...