在你决定克隆它之前,有没有办法看看GitHub上的Git存储库有多大?
这似乎是一个非常明显/基本的统计数据,但我根本找不到如何在GitHub上看到它。
在你决定克隆它之前,有没有办法看看GitHub上的Git存储库有多大?
这似乎是一个非常明显/基本的统计数据,但我根本找不到如何在GitHub上看到它。
当前回答
在浏览器中,使用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') } )
其他回答
在浏览器中,使用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') } )
正如其他答案所示,可以通过api.github.com获得大小。它在返回的JSON对象的size属性中。
要得到它,只需在你的回购URL中添加一个额外的子域api,并使用/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}
由于它只是URL,您可以使用任何编程语言获取数据。
回复会是这样的:
// 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
}
对我们来说最重要的是尺寸。它现在以Kb为单位,但将来可能会更改(因为它已经被更改了)。
但是… 我测试了很多次,看到回购的实际大小和上面机制显示的大小太不一样了。
让我们给出相同的axios repo:
大小显示在api.github.com -> 4396 Kb -> ~4.29 Mb
如果克隆一个完整的回购:
用克隆回购拉回购。git命令 使用命令du -sh ./axios获取权重 have -> 8.0 Mb 从里面删除。git文件夹 有-> 2.6 Mb
不太好,因为大小~4.29 Mb也不是8或2.6 Mb
如果只克隆最新的提交:
拉—depth 1标志的回购,就像克隆回购—depth 1一样 使用命令du -sh ./axios获取权重 3.2 Mb(接近) 从里面删除。git文件夹 有->相同2.6 Mb
不太好,因为大小~4.29 Mb也不是3.2或2.6 Mb
如果只克隆一个分支:
在上面的JSON中,我们有一个名为default_branch的参数。让我们克隆 用- bv1进行回购。X——单分支标志 使用命令du -sh ./axios获取权重 7.5 Mb(这是接近) 从里面删除。git文件夹会得到同样的2.6 Mb
仍然不太好,因为大小~4.29 Mb也不是7.5或2.6 Mb
因此,size参数显示了一些东西,它接近于最近的提交,但它不是回购的强正确大小。
上面已经展示了它如何使用axios repo,但是使用不同的repo进行的测试显示了相同的结果。
这是我的经验。
@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),几乎是GB。
私有回购需要身份验证。一种方法是使用GitHub个人访问令牌:
curl -u myusername:$PERSONAL_ACCESS_TOKEN https://api.github.com/repos/$2/$3 2> /dev/null | grep size | tr -dc '[:digit:]'
总结一下@larowlan、@VMTrooper和@vahid chakoshy解决方案:
#!/usr/bin/env bash
if [ "$#" -eq 2 ]; then
echo "$(echo "scale=2; $(curl https://api.github.com/repos/$1/$2 2>/dev/null \
| grep size | head -1 | tr -dc '[:digit:]') / 1024" | bc)MB"
elif [ "$#" -eq 3 ] && [ "$1" == "-z" ]; then
# For some reason Content-Length header is returned only on second try
curl -I https://codeload.github.com/$2/$3/zip/master &>/dev/null
echo "$(echo "scale=2; $(curl -I https://codeload.github.com/$2/$3/zip/master \
2>/dev/null | grep Content-Length | cut -d' ' -f2 | tr -d '\r') / 1024 / 1024" \
| bc)MB"
else
printf "Usage: $(basename $0) [-z] OWNER REPO\n\n"
printf "Get github repository size or, optionally [-z], the size of the zipped\n"
printf "master branch (`Download ZIP` link on repo page).\n"
exit 1
fi
我创建了一个bookmarklet脚本来使用NVRM的答案的方法来做到这一点。
要使用它,请创建一个新书签,为其命名,并将此脚本粘贴到URL字段中。在浏览一个回购时点击这个书签会弹出一个提醒,提醒的大小有兆字节和千字节。
javascript:(()=>{let url=new URL(document.location.href);if(url.origin!="https://github.com"){return}if(url.pathname=="/"){return}let p=url.pathname.slice(1,url.pathname.length);let parts=p.split('/');if(parts.length<2){return}let x=[parts[0],parts[1]].join('/');fetch(`https://api.github.com/repos/${x}`).then(r=>r.json()).then((b)=>alert(`${(b['size']/1000).toFixed(2)}mb (${b['size']}kb)`))})()