Skip to content

Instantly share code, notes, and snippets.

@vikiboss
Last active April 2, 2026 06:33
Show Gist options
  • Select an option

  • Save vikiboss/16efac49015739c3273f68e52ce21570 to your computer and use it in GitHub Desktop.

Select an option

Save vikiboss/16efac49015739c3273f68e52ce21570 to your computer and use it in GitHub Desktop.
repo.sh
# 公开仓库的 PAT 即可,无任何敏感权限,仅用于提高 GitHub API 速率限制
# 将本文件内容放到 ~/.bashrc 或者 ~/.zshrc 即可通过 `repo vikiboss/60s` 快速查询仓库大小等信息
# Public-repo-only PAT for higher API rate limits.
export GITHUB_RATE_LIMIT_PAT="github_pat_xxx"
# size(KB)转可读字符串
_repo_size() {
local kb=$1
if (( kb < 1024 )); then
echo "${kb} KB"
elif (( kb < 1048576 )); then
awk -v s="$kb" 'BEGIN{ printf "%.2f MB", s/1024 }'
else
awk -v s="$kb" 'BEGIN{ printf "%.2f GB", s/1048576 }'
fi
}
# 查询 GitHub 仓库信息,用法:repo <owner/repo>
repo() {
local repo="$1"
if [ -z "$repo" ]; then
echo "用法: repo <owner/repo>"
return 1
fi
local pat="${GITHUB_RATE_LIMIT_PAT:-}"
[ -z "$pat" ] && { echo "❌ 未找到 GITHUB_RATE_LIMIT_PAT 环境变量"; return 1; }
local response
response=$(curl -sf \
-H "Authorization: Bearer $pat" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$repo") || {
echo "❌ 请求失败:仓库不存在、无权限或 PAT 已失效"
return 1
}
local -a f
f=("${(@f)$(jq -r '
.full_name,
.stargazers_count,
.forks_count,
.open_issues_count,
.size,
(.language // "—"),
.visibility,
.default_branch,
(.created_at | split("T")[0]),
(.updated_at | split("T")[0]),
(.license.spdx_id // .license.name // ""),
(.homepage // ""),
(if .archived then "1" else "" end),
(if .fork then "1" else "" end)
' <<< "$response")}")
local full_name="${f[1]}"
local stars="${f[2]}" forks="${f[3]}"
local open_issues="${f[4]}" size="${f[5]}"
local language="${f[6]}" visibility="${f[7]}"
local default_branch="${f[8]}"
local created_at="${f[9]}" updated_at="${f[10]}"
local license="${f[11]}" homepage="${f[12]}"
local archived="${f[13]}" is_fork="${f[14]}"
[[ "$license" == "NOASSERTION" || "$license" == "NONE" ]] && license=""
local size_human && size_human=$(_repo_size "$size")
local name_suffix=""
[[ "$visibility" != "public" ]] && name_suffix=" 🔒"
[[ -n "$archived" ]] && name_suffix+=" 🗄 已归档"
[[ -n "$is_fork" ]] && name_suffix+=" 🍴 Fork"
local line3="💻 语言: ${language} | 🌿 分支: ${default_branch}"
[[ -n "$license" ]] && line3+=" | 📜 协议: ${license}"
printf "\n"
printf "📦 %s%s\n" "$full_name" "$name_suffix"
[[ -n "$homepage" ]] && printf "🏠 主页: %s\n" "$homepage"
printf "%s\n" "$line3"
printf "⭐ Stars: %s | 🍴 Forks: %s | 🔖 Issues: %s\n" "$stars" "$forks" "$open_issues"
printf "💾 大小: %s | 📅 创建: %s | 🔁 更新: %s" "$size_human" "$created_at" "$updated_at"
printf "\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment