0

I'm trying to clone a set of repositories from GitHub. It works but I can't work out how to add a destination folder with xargs. This is what I currently have

curl "https://api.github.com/orgs/$org/repos?access_token=$authToken" |
  grep -e 'clone_url*' | 
  grep -w clone_url | 
  grep -o '[^"]\+://.\+.git' |
  xargs -L1 git clone

Except I want the cloned repositories to go into a another directory ./my_folder. I've tried changing the last line:

xargs -L1 git clone "./my_folder"

But this does not work. I can't work out how to add on the extra argument using xargs. The normal way would be to just add the destination folder on to the end of the command like in this answer but I can't quite work out how to do this with my code.

Inian
  • 80,270
  • 14
  • 142
  • 161
Muon
  • 1,294
  • 1
  • 9
  • 31

1 Answers1

1

The problem here is that the destination folder for each git clone operation needs to be a separate subdirectory of my_folder, whose name would probably be something like the basename part of the git URL. This is probably hard to achieve using xargs, so I suggest in place of the xargs an explicit loop:

... | while read repo_url ; do git clone $repo_url my_folder/$(basename $repo_url) ; done

This to go at the end of the pipeline after the curl and grep commands.

You could of course strip off the .git if preferred, by replacing $(basename $repo_url) with

$(basename $repo_url | sed 's/\.git$//')`

Putting it all together for convenience:

curl "https://api.github.com/orgs/$org/repos?access_token=$authToken" | \
  grep -e 'clone_url*' | \
  grep -w clone_url | \
  grep -o '[^"]\+://.\+.git' | \
  while read repo_url
  do 
      git clone $repo_url my_folder/$(basename $repo_url | sed 's/\.git$//')
  done
alani
  • 12,573
  • 2
  • 13
  • 23