A couple of days ago, I posted an one-liner for GitHub, now it’s Bitbucket’s turn.


BB="bitbucket.org"; curl -s -u $USER:SECRET_HERE -k https://api.bitbucket.org/1.0/users/$USER/ | sed -n '/^.\{13\}\(scm\|slug\)/ {s/.*"\([^"]\+\)",/\1/;p}' | paste -s -d ' \n' | while read SCM SLUG; do git_URL="$SCM@$BB:$USER/$SLUG.git"; hg_URL="ssh://$SCM@$BB/$USER/$SLUG"; URL=${SCM}_URL; $SCM clone ${!URL}; done

The breakdown version:


BB="bitbucket.org"
curl -s -u $USER:SECRET_HERE -k https://api.bitbucket.org/1.0/users/$USER/ |
sed -n '/^.\{13\}\(scm\|slug\)/ {s/.*"\([^"]\+\)",/\1/;p}' |
paste -s -d ' \n' |
while read SCM SLUG; do
git_URL="$SCM@$BB:$USER/$SLUG.git"
hg_URL="ssh://$SCM@$BB/$USER/$SLUG"
URL=${SCM}_URL
$SCM clone ${!URL}
done

Same here, you may need to change $USER. You can also remove the -u part if you don’t have private repositories, then you don’t need to use Basic Authentication.

This one-liner should work for both Git and Mercurial (Hg). It uses sed to filter out necessary data, the SCM type and the slug, which is the repository’s name. paste is very useful to join every two lines, so the data can be read into two variables.

In the loop, I use indirect expansion to get the proper URL for the certain SCM type, there is no need for using switch or if, and this way looks cleaner. Too bad that API doesn’t include the clone URL.