Skip to content

Instantly share code, notes, and snippets.

@tan9
Last active August 6, 2022 16:13
Show Gist options
  • Save tan9/82f014f6a1cff3744a76a1d1c86aaf5c to your computer and use it in GitHub Desktop.
Save tan9/82f014f6a1cff3744a76a1d1c86aaf5c to your computer and use it in GitHub Desktop.
Clone all Azure DevOps repos within a organzation.
#!/bin/bash
set -e
export AZURE_PAT=<YOUR_AZURE_PRIVATE_ACCESS_TOKEN>
export organization=<YOUR_ORGANIZATION>
# limit the number of spawned processes: https://stackoverflow.com/a/14387296/3440376
function max5 {
while [ `jobs -p | wc -l` -ge 5 ]
do
sleep 0.1
done
}
# iterator over all projects
for project in $(curl --silent \
--user $organization:$AZURE_PAT \
--request GET \
https://dev.azure.com/$organization/_apis/projects\?api-version\=7.0 \
| jq -r ".value[].name"); do
echo "Porject: $project"
# mkdir if project directory doesn't exist
if [ ! -d "$project" ]
then
mkdir $project
fi
# clone or update all repos in project
for repo in $(curl --silent \
--user $organization:$AZURE_PAT \
--request GET \
https://dev.azure.com/$organization/$project/_apis/git/repositories\?api-version\=7.0 \
| jq -r ".value[].name"); do
echo " Repo: $repo"
if [ ! -d "$project/$repo.git" ]
then
# clone if repo directory doesn't exist
max5; git clone --mirror https://$organization@dev.azure.com/$organization/$project/_git/$repo $project/$repo.git &
else
# update from remote if already cloned
max5; git -C "$project/$repo.git" remote update &
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment