在决定克隆一个 Git 存储库之前,有没有办法在 GitHub 上查看它的大小?
这似乎是一个非常明显/基础的统计数据,但我无法在 GitHub 上找到如何查看它。
在决定克隆一个 Git 存储库之前,有没有办法在 GitHub 上查看它的大小?
这似乎是一个非常明显/基础的统计数据,但我无法在 GitHub 上找到如何查看它。
可以通过GitHub API来访问这些信息。
GET /repos/:user/:repo
在检索有关存储库的信息时,名为 size
的属性将包含整个存储库(包括其全部历史记录)的大小,以千字节表示。
例如,Git 存储库大约为 124 MB。返回的 JSON 负载的 size
属性的值为 124283
。
实际上,该大小是以基于服务器端裸仓库的磁盘使用情况表示的千字节。但是,为了避免对具有大型网络的存储库浪费太多空间,GitHub 依赖于Git Alternates。在此配置中,针对裸仓库计算磁盘使用情况不考虑共享对象存储,因此通过 API 调用返回“不完整”的值。
这些信息由 GitHub 支持提供。
$ curl -u "{:username}" https://api.github.com/repos/{:organization}/{:repository}
命令。详情请见 https://developer.github.com/v3/#authentication。 - nulltokensettings/repositories
中查看它。因此,请在代码库设置中添加自己(https://github.com/org-name/repo-name/settings
),才能在https://github.com/settings/repositories
中查看它。
@larowlan 很棒的示例代码。使用新的GitHub API V3,curl语句需要更新。此外,登录不再是必需的:
curl https://api.github.com/repos/$2/$3 2> /dev/null | grep size | tr -dc '[:digit:]'
例如:curl https://api.github.com/repos/dotnet/roslyn 2> /dev/null | grep size | tr -dc '[:digit:]'
返回 931668
(以KB为单位),几乎等于1GB。
私有存储库需要身份验证。一种方法是使用GitHub个人访问令牌:
curl -u myusername:$PERSONAL_ACCESS_TOKEN https://api.github.com/repos/$2/$3 2> /dev/null | grep size | tr -dc '[:digit:]'
通过浏览器,使用JavaScript,因为Github API已启用CORS:
fetch('https://api.github.com/repos/webdev23/source_control_sentry')
.then(v => v.json()).then((v) => {
console.log(v['size'] + 'KB')
}
)
{ "message": "未找到", "documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository" }
- RegarBoy你只需要进入GitHub设置页面,然后打开“repositories”,你便能在浏览器中轻松查看所有大小信息,无需任何额外操作。
使用curl(sudo apt-get curl)和jsonpretty(sudo gem install jsonpretty json)可以实现此目的:
curl -u "YOURGITHUBUSERNAME" http://github.com/api/v2/json/repos/show/OWNER/REPOSITORY |
jsonpretty
用你的GitHub用户名替换YOURGITHUBUSERNAME(想必很清楚)。
将OWNER替换为仓库所有者的Git用户名。 将REPOSITORY替换为仓库名称。
或者,作为一个不错的Bash脚本(将其粘贴到名为gitrepo-info的文件中):
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: gitrepo-info <username> <owner> <repo>"
exit 65
fi
curl -u "$1" http://github.com/api/v2/json/repos/show/$2/$3|jsonpretty
使用方法如下:
gitrepo-info larowlan pisi reel
这将为我提供有关 GitHub 上 pisi/reel 存储库的信息。
如其他答案所示,可以通过 api.github.com
获取大小。它在返回的 JSON 对象的 size
属性中。
要获取它,只需将附加的子域名 api
添加到您的存储库 URL 中,并扩展存储库路径以包括 /repos
:
# For public repos ->
# Repo example: Axios
# Repo URL: https://github.com/axios/axios
⤵ ⤵
curl https://api.github.com/repos/axios/axios
# For private repos ->
# Repo example: My-repo
# Repo URL: https://github.com/my-org/my-repo
curl https://{username}:{api-token}@api.github.com/repos/{orgname}/{reponame}
// Much more props inside
{
"id": 23088740,
"name": "axios",
"full_name": "axios/axios",
"private": false,
"size": 4396,
"default_branch": "v1.x",
"visibility": "public",
"network_count": 9581,
"subscribers_count": 1213
}
size
参数显示了一些接近于最新提交的内容,但它并不是该存储库大小的确切值。
这是我的经验。我已经展示了如何在
axios
存储库中使用它,但是对不同存储库进行测试会得到相同的结果。
gh api repos/<org>/<repo> --jq '.size'
import requests
if __name__ == '__main__':
base_api_url = 'https://api.github.com/repos'
git_repository_url = 'https://github.com/garysieling/wikipedia-categorization.git'
github_username, repository_name = git_repository_url[:-4].split('/')[-2:] # garysieling and wikipedia-categorization
res = requests.get(f'{base_api_url}/{github_username}/{repository_name}')
repository_size = res.json().get('size')
print(repository_size)