GitHub Repository Size Checker (Excluding .git)

Displays the total size of files in a GitHub repository (excluding .git directory) next to the repo name.

当前为 2025-04-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

作者
John Gray
评分
0 0 0
版本
1.0
创建于
2025-04-13
更新于
2025-04-13
大小
18.5 KB
许可证
MIT
适用于

## GitHub Repository Size Checker (Excluding .git)

Displays the total size of repository files (excluding the `.git` directory) directly on GitHub repository pages, next to the Public/Private label.

**Why?** GitHub doesn't show the total size of the *checked-out code* easily. This script calculates it using the GitHub API, giving you a quick understanding of a project's codebase size *before* you clone it.

---

### How it Works

1. **Detects Repo Pages:** Activates automatically when you visit a GitHub repository's main page.
2. **Identifies Repo:** Extracts the owner and repository name from the URL.
3. **Requires GitHub PAT:** Needs a Personal Access Token (PAT) with `repo` (or `public_repo`) scope to interact with the GitHub API respectfully and avoid rate limits. It will prompt you if one isn't found and store it securely using your userscript manager (`GM_setValue`). You can also set/update it via the script's menu command.
4. **Smart Caching:**
* Checks a local cache for the repository's size first.
* If the cached data is less than 24 hours old, it's displayed instantly (no API calls!).
* If older than 24 hours, it checks the latest commit SHA of the default branch. If the SHA hasn't changed, the cached size is reused and the timestamp updated.
5. **Fetches Data (Only When Needed):** If the cache is invalid (too old and SHA changed, or no cache exists), it fetches the list of all files and their sizes from the GitHub API.
6. **Calculates Size:** Sums the sizes of all files (`blobs`) reported by the API. It handles potentially truncated results from the API for very large repositories.
7. **Displays Result:** Shows the calculated size (e.g., `14.3 MB`, `2.1 GB`) next to the repo's Public/Private label.
* Shows `(loading...)` or `(validating...)` during checks.
* Shows `PAT Required` or other errors if issues occur.
* Adds a `~` prefix (e.g., `~1.5 GB`) if the API indicated the file list was truncated (size is approximate).

---

### Features

* Displays repository size clearly on the page.
* Excludes the `.git` directory for a practical code size estimate.
* Securely stores your GitHub PAT locally via the userscript manager.
* Menu command available (via Tampermonkey/Greasemonkey icon) to update the PAT.
* Efficient caching (24 hours) with SHA validation reduces API calls significantly.
* Human-readable size formatting (Bytes, KB, MB, GB).
* Visual indicators for loading, errors, and truncated results.
* Designed to work with GitHub's dynamic page loading.

---

### Requirements

1. **Userscript Manager:** A browser extension like Tampermonkey (recommended), Greasemonkey, Violentmonkey, etc.
2. **GitHub Personal Access Token (PAT):**
* Needed to make GitHub API requests.
* Requires the **`repo`** scope (or at least **`public_repo`** if you only browse public repositories).
* You can generate one here: [https://github.com/settings/tokens](https://github.com/settings/tokens) -> Personal access tokens -> Tokens (classic) -> Generate new token (classic).

---

### Setup

1. **Install a Userscript Manager** (if you haven't already).
2. **Click the "Install" button** on this Greasy Fork page.
3. **Generate a GitHub PAT:**
* Go to [GitHub Tokens Settings](https://github.com/settings/tokens).
* Click "Generate new token" -> "Generate new token (classic)".
* Add a note (e.g., "Repo Size Checker").
* Set an expiration date (recommended!).
* Select the scope: check **`repo`** (or just `public_repo` for public repos only).
* Click "Generate token".
* **Important:** Copy the token immediately. You won't see it again.
4. **Provide the PAT to the Script:**
* The *first time* you visit a GitHub repository after installing the script, it should prompt you to enter the PAT. Paste the token you copied.
* *Alternatively*, click the Tampermonkey/Greasemonkey icon in your browser toolbar, find "GitHub Repository Size Checker", and select the "Set/Update GitHub PAT..." command to enter or change the token.

---

### Notes

* **PAT Security:** Your PAT is stored locally by your userscript manager. Treat it like a password. Use tokens with the minimum necessary scope and consider setting expiration dates.
* **GitHub UI Changes:** GitHub sometimes updates its website layout. If the script stops placing the size correctly, the CSS selectors in the script might need updating. Please report an issue if this happens.
* **Rate Limits:** Using a PAT gives you a much higher API rate limit, making errors unlikely.
* **Large Repositories:** For extremely large repositories, GitHub's API might truncate the file list. The script will show a `~` symbol before the size in these cases, indicating it's a minimum estimate based on the data received.