Skip to content

Instantly share code, notes, and snippets.

@erdincay
Last active March 14, 2024 21:00
Show Gist options
  • Save erdincay/4f1d2e092c50e78ae1ffa39d13fa404e to your computer and use it in GitHub Desktop.
Save erdincay/4f1d2e092c50e78ae1ffa39d13fa404e to your computer and use it in GitHub Desktop.
su GitHub (downloading all repositories from a given user)
#!/bin/bash
if [ -z "$1" ]; then
echo "waiting for the following arguments: username + max-page-number"
exit 1
else
name=$1
fi
if [ -z "$2" ]; then
max=2
else
max=$2
fi
cntx="users"
page=1
echo $name
echo $max
echo $cntx
echo $page
until (( $page -lt $max ))
do
curl "https://api.github.com/$cntx/$name/repos?page=$page&per_page=100" | grep -e 'clone_url*' | cut -d \" -f 4 | xargs -L1 git clone
$page=$page+1
done
exit 0
@willmac321
Copy link

hey if yall want to make this work for gitlab
install jq [json bash parser]
and replace curl line with:
curl "https://gitlab.com/api/v4/$cntx/$name/projects?page=1&per_page=100" | jq '.[]' | jq .'ssh_url_to_repo' | xargs -L1 git clone

@erdincay
Copy link
Author

hey if yall want to make this work for gitlab
install jq [json bash parser]
and replace curl line with:
curl "https://gitlab.com/api/v4/$cntx/$name/projects?page=1&per_page=100" | jq '.[]' | jq .'ssh_url_to_repo' | xargs -L1 git clone

thanks @willmac321

@excitedbox
Copy link

Should line 11 have a $ before the 2 to make it a variable?

@willmac321
Copy link

hey if yall want to make this work for gitlab
install jq [json bash parser]
and replace curl line with:
curl "https://gitlab.com/api/v4/$cntx/$name/projects?page=1&per_page=100" | jq '.[]' | jq .'ssh_url_to_repo' | xargs -L1 git clone

thanks @willmac321

No Problem, happy to help!

@drattek
Copy link

drattek commented Feb 15, 2021

how to make it work with private repos?

@codysnider
Copy link

how to make it work with private repos?

You can tack on an env var with your github token to the URL: &access_token=$GITHUB_TOKEN

@askfriends
Copy link

How to run this on Windows10?
btw i want to clone some private repos too

@erdincay
Copy link
Author

erdincay commented Jul 6, 2021

How to run this on Windows10?
btw i want to clone some private repos too

please have a look at the security aspects on the comments here: https://stackoverflow.com/questions/19576742/how-to-clone-all-repos-at-once-from-github/32833411#32833411

@erdincay
Copy link
Author

Should line 11 have a $ before the 2 to make it a variable?

nope, will run only for the first page (first 100 entries) if used without max-page argument

@peelyKoodoo
Copy link

Thank you!

Only thing that tripped me up was that my terminal is authenticated with ssh, so I needed to switch clone_url for ssh_url. And also I got a syntax error on until (( $page -lt $max ) and $page=$page+1, I had to change them to until [ $page -gt $max ] and let "page=page+1". And, finally, I was cloneing the repos in my org, not my user, so I set cntx=orgs

Whole script being

#!/bin/zsh

if [ -z "$1" ]; then
    echo "waiting for the following arguments: username + max-page-number"
    exit 1
else
    name=$1
fi

if [ -z "$2" ]; then 
    max=2
else
    max=$2
fi

cntx="orgs"
page=1

echo $name
echo $max
echo $cntx
echo $page

until [ $page -gt $max ]
do 
    curl -i -u $name:$NPM_TOKEN "https://api.github.com/$cntx/$name/repos?page=$page&per_page=100" | grep -e 'ssh_url*' | cut -d \" -f 4 | xargs -L1 git clone
    let "page=page+1"
done

exit 0

@dkgndianko
Copy link

I did a similar thing with gh command.
You can see it there.

@Ricecr
Copy link

Ricecr commented Jul 2, 2023

"This is another solution":
page=$((page+1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment