LoginSignup
0

More than 5 years have passed since last update.

bitbucketのすべてのRepo(権限を持っているRepo)を一気にCloneするスクリプト・コマンド

Last updated at Posted at 2018-06-28

背景

たくさんRepoがあるときに、新しいパソコンを購入しすべてのRepoを一個ずつCloneしてくるのがめんどくさいのですべて一気にCloneしたい!

スクリプト

コマンドでやりたかったが、一回に返ってくる量に制限があるので、Paginateしてる分をLoopしてあげるスクリプトを書いた。Repoがそんなにない場合は、コマンドでおっけー

get_all_repo.sh
#!/bin/bash

USER=$1
PASS=$2
URL=https://api.bitbucket.org/2.0/repositories/$USER

if [ $# -lt 2 ];then
    echo "usage ./get_all_repos.sh <user> <pass> <team optional>"
    exit 1
elif [ $# -eq 3 ];then
    TEAM=$3
    URL=https://api.bitbucket.org/2.0/repositories/$TEAM
fi

TMP_FILE=tmp_res.json

while true
do
    curl --user $USER:$PASS $URL > $TMP_FILE
    cat $TMP_FILE | jq -r '.values[].links.clone[].href' | grep -v https | xargs -L1 git clone
    next=$(cat $TMP_FILE | jq -r '.next')
    if [ -z $next ];then
        echo no more next
        break;
    else
        echo next $next
        URL=$next
    fi
done

実行コマンド

User(自分)の管理するRepoをすべてCloneする

./get_all_repos.sh <user> <pass>

Teamの管理するRepoをすべてCloneする

./get_all_repos.sh <user> <pass> <team>

コマンド

Repo数が多くない場合は、一行のコマンドで十分。実行するとパスワードが聞かれるので、パスワードを入力すれば、Cloneが始まる。

User(自分)の管理するRepoをすべてCloneする

USER=bitbucket_username; curl --user $USER https://api.bitbucket.org/2.0/repositories/$USER | jq '.values[].links.clone[].href' | grep -v https | xargs -L1 git clone

Teamの管理するRepoをすべてCloneする

USER=bitbucket_username;TEAM=bitbucket_teamname curl --user $USER https://api.bitbucket.org/2.0/repositories/$TEAM | jq '.values[].links.clone[].href' | grep -v https | xargs -L1 git clone

説明

bitbucket API

これで、すべてのRepoの情報がJSON形式で帰ってくる

curl --user $USER https://api.bitbucket.org/2.0/repositories/$USER | jq '.values[].links.clone[].href' | grep -v https | xargs -L1 git clone

jqコマンド

jqコマンドでJSONをパースして自分の必要な情報だけを集める。以下のもので、Cloneの中に、sshとhttpsのCloneアドレスが列挙されるようになる


jq '.values[].links.clone[].href'

grep

今回は、不精して、単にhttpsを除外しただけ。本来はjqコマンドでnameがsshのものに限定すると一番よい!


grep -v https

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0