Bitbucket克隆所有团队存储库

Imr*_*ran 3 git macos bitbucket

我想使用bash脚本和http克隆我的所有bitbucket团队存储库。我发现了一些使用Bitbucket API的示例,但是它们似乎都未返回任何存储库。有任何想法吗?使用Mac。

小智 15

这是我的简单解决方案。制作文件downloader.sh

#!/bin/bash

USER=${1}
TEAM=${2}

rm -rf "$TEAM" && mkdir "$TEAM" && cd $TEAM

NEXT_URL="https://api.bitbucket.org/2.0/repositories/${TEAM}?pagelen=100"

while [ ! -z $NEXT_URL ]
do
    curl -u $USER $NEXT_URL > repoinfo.json
    jq -r '.values[] | .links.clone[1].href' repoinfo.json > ../repos.txt
    NEXT_URL=`jq -r '.next' repoinfo.json`

    for repo in `cat ../repos.txt`
    do
        echo "Cloning" $repo
        if echo "$repo" | grep -q ".git"; then
            command="git"
        else
            command="hg"
        fi
        $command clone $repo
    done
done

cd ..
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式运行它:

sh downloader.sh username teamname # or username instead team name
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的剧本。小附录:bitbucket 停止使用用户名+密码作为身份验证方法。为了使您的脚本正常工作,您需要创建一个应用程序密码并在命令行中使用“username:apppassword”作为“username”。用户名可在帐户设置中找到。使用带有应用程序密码的电子邮件地址将导致错误消息。 (2认同)

Eri*_*ord 8

确保在bitbucket中设置了ssh密钥,并通过手动克隆一个存储库来安装git:

这将克隆您拥有的所有存储库:

USER=bitbucket_username; curl --user ${USER} https://api.bitbucket.org/2.0/repositories/${USER} | grep -o '"ssh:[^ ,]\+' | xargs -L1 git clone
Run Code Online (Sandbox Code Playgroud)

要备份您的团队存储库,请使用相同的脚本,但对您的位桶团队名称进行硬编码,如下所示:

USER=bitbucket_username; curl --user ${USER} https://api.bitbucket.org/2.0/repositories/TEAMNAME | grep -o '"ssh:[^ ,]\+' | xargs -L1 git clone
Run Code Online (Sandbox Code Playgroud)

这是一个更好的方法

curl -u ${1} https://api.bitbucket.org/1.0/users/TEAMNAME > repoinfo

for repo_name in `cat repoinfo | sed -r 's/("name": )/\n\1/g' | sed -r 's/"name": "(.*)"/\1/' | sed -e 's/{//' | cut -f1 -d\" | tr '\n' ' '`
do
    echo "Cloning " $repo_name
    git clone git@bitbucket.org:TEAMNAME/$repo_name.git
    echo "---"
done
Run Code Online (Sandbox Code Playgroud)

  • 仅适用于可能正在尝试使用此功能的用户。确保ssh:是您存储库的正确签名。对我来说是`git:`。像这样:`USER = myUserName; curl --user $ {USER} https://api.bitbucket.org/2.0/repositories/THETEAM | grep -o'“ git @ [^,] \ +'| xargs -L1 git clone` (2认同)

Edu*_*lly 7

另一种选择是使用jq

#!/bin/bash

user=username:password

curl -u $user 'https://api.bitbucket.org/2.0/user/permissions/teams?pagelen=100' > teams.json

jq -r '.values[] | .team.username' teams.json > teams.txt

for team in `cat teams.txt`
do
  echo $team

  rm -rf "${team}"

  mkdir "${team}"

  cd "${team}"

  url="https://api.bitbucket.org/2.0/repositories/${team}?pagelen=100"

  echo $url

  curl -u $user $url > repoinfo.json

  jq -r '.values[] | .links.clone[0].href' repoinfo.json > repos.txt

  for repo in `cat repos.txt`
  do
    echo "Cloning" $repo
    git clone $repo
  done

  cd ..

done
Run Code Online (Sandbox Code Playgroud)