GitHub API,提取提交为本地文件

时间:2017-02-21 01:29:39

标签: github github-api

我是GitHub API的新手,我一直在寻找一种自动方式,我可以将GitHub中特定存储库的所有提交作为“本地文件”下载到我的机器中,但没有成功。

非常感谢任何建议和意见。

2 个答案:

答案 0 :(得分:2)

你可以:

  • 只需git克隆GitHub repo:你将获得默认分支检出的文件,以及.git子文件夹中的所有提交
  • get the archive link of a repo然后curl说存档(但你只会得到回购内容,而不是"所有提交"
  • list the commits on a repo,使用?path=a/file/path参数过滤所述提交,只获取给定file.cpp
  • 的提交

答案 1 :(得分:1)

如果你想通过Github API在一个repo上list the commits,你可以使用以下bash脚本迭代所有提交并将它们写入根JSON数组中的单个文件。

如果您想要选择特定字段或过滤其他内容,则可以添加自己的jq过滤器:

#!/bin/bash

# change those vars :
GITHUB_USER=google
GITHUB_REPO=gson
OUTPUT_FILE=commit_list.json
GITHUB_ACCESS_TOKEN=1234567890123445678

loop=0
index=1
TMP_FILE=tmpfile.txt
PER_PAGE=100


rm -f $TMP_FILE
echo "[]" > $OUTPUT_FILE

while [ "$loop" -ne 1 ]
do
    URI="https://api.github.com/repos/$GITHUB_USER/$GITHUB_REPO/commits?page=$index&per_page=$PER_PAGE&access_token=$GITHUB_ACCESS_TOKEN"
    data=`curl -s $URI`

    # add some filter here if needed (for instance `echo "$data" | jq '[ .[].commit ]`)
    filtered=`echo "$data"`

    check=`echo "$filtered" | jq 'if (type=="array" and length>0) then "continue" else "stop" end'`

    if [ "$check" == '"stop"' ]; then
        loop=1
    else
        echo "$filtered" > $TMP_FILE
        concat=`jq -s add $TMP_FILE $OUTPUT_FILE`
        echo "$concat" > $OUTPUT_FILE
        size=`jq '. | length' $OUTPUT_FILE`
        echo "computed $index page - fetched total commit count of : $size"
        index=$((index+1))
    fi
done

此脚本需要jqcurl

输出位于输出文件commit_list.json中,该文件是所有commit JSON object的数组:

# jq 'length' commit_list.json
1329
相关问题