如何获取Linux上GitHub存储库的所有分支的列表? [英] How to get a list of all forks of a GitHub repo on Linux?

查看:170
本文介绍了如何获取Linux上GitHub存储库的所有分支的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以一种简单,非交互的方式获取GitHub存储库中的派生列表.

I would like to have a simple, non-interactive way to get a list of forks of a GitHub repo.

就我个人而言,它必须至少在Linux上运行.

For me personally, it has to run on at least Linux.

推荐答案

使用bURL脚本通过cURL使用GraphQL(GiHub API v4):

Using GraphQL (GiHub API v4) from a bash script, using cURL:

#!/bin/bash
# Returns a list of all forks of a github repo.
# See the output of "$0 -h" for more details.

set -e

# initial default values
access_token=""
repo_owner=$USER
repo_name=$(basename $(pwd))
res_file=res.json


function print_help() {

    echo "Returns a list of all forks of a github repo."
    echo
    echo "Usage:"
    echo "         `basename $0` [OPTIONS]"
    echo "Options:"
    echo "          -h, --help              Show this help message"
    echo "          -o, --owner <string>    Name of the GitHub user or organization of the original repo"
    echo "          -r, --repo  <string>    Name of the GitHub original repo"
    echo "          -t, --token <string>    GitHub personal access token, required for authentication"
    echo "                                  To get one, see: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line"
}

# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
    arg="$1"
    shift

    case "${arg}" in
        -h|--help)
            shift # past argument
            print_help
            exit 0
            ;;
        -o|--owner)
            repo_owner="$1"
            shift # past argument
            ;;
        -r|--repo)
            repo_name="$1"
            shift # past argument
            ;;
        -t|--token)
            access_token="$1"
            shift # past argument
            ;;
        *) # non-/unknown option
            POSITIONAL+=("${arg}") # save it in an array for later
            shift # past argument
            ;;
    esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

if [ -z "$access_token" ]
then
    >&2 echo "WARNING: Access token not specified, though it is required!"
    print_help
    exit 1
fi

curl \
    'https://api.github.com/graphql' \
    -H 'Accept-Encoding: gzip, deflate, br' \
    -H 'Content-Type: application/json' \
    -H 'Accept: application/json' \
    -H 'Connection: keep-alive' \
    -H 'Origin: altair://-' \
    -H "Authorization: Bearer $access_token" \
    --data-binary '{"query":"query { repository(owner: \"'$repo_owner'\", name: \"'$repo_name'\") { forks(first:100) { edges { node { nameWithOwner } } } } }","variables":{}}' \
    --compressed \
    > "$res_file"

cat "$res_file" \
    | sed -e 's/nameWithOwner/\nXXX/g' \
    | grep XXX \
    | awk -e 'BEGIN { FS="\""; } { print $3; }'

您需要创建访问令牌.

上述脚本的示例调用:

git-hub-list-forks -o hoijui -r JavaOSC -t 1234567890abcdef1234567890abcdef

注意:GitHub将一次最多可提取100个分支的最大数量限制

NOTE: GitHub limits the maximum amount of forks to fetch to 100 at a time

这篇关于如何获取Linux上GitHub存储库的所有分支的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆