mirror of
https://github.com/GetOpenScript/GitHub.openscript.git
synced 2026-09-18 08:25:42 +00:00
Remove blobs fallback and report true git repo disk usage including history
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name Display Repo Size for GitHub
|
// @name Display Repo Size for GitHub
|
||||||
// @version 1.2.0
|
// @version 1.3.0
|
||||||
// @description Displays repository size inside the About section on GitHub (supports public & private repos using GH_PAT).
|
// @description Displays total repository size (including full git history) inside the About section on GitHub using GH_PAT.
|
||||||
// @author OpenScript
|
// @author OpenScript
|
||||||
// @match https://github.com/*/*
|
// @match https://github.com/*/*
|
||||||
// @grant none
|
// @grant none
|
||||||
@@ -13,15 +13,15 @@
|
|||||||
const cache = new Map();
|
const cache = new Map();
|
||||||
const ROW_ID = 'openscript-repo-size-about';
|
const ROW_ID = 'openscript-repo-size-about';
|
||||||
|
|
||||||
// Format bytes to human readable format
|
// Format KB to readable size
|
||||||
const formatSize = bytes => {
|
const formatKb = kb => {
|
||||||
const kb = bytes / 1024;
|
if (kb <= 0) return '0 KB (calculating...)';
|
||||||
if (kb < 1024) return `${kb.toFixed(1)} KB`;
|
if (kb < 1024) return `${kb} KB`;
|
||||||
if (kb < 1024 * 1024) return `${(kb / 1024).toFixed(1)} MB`;
|
if (kb < 1024 * 1024) return `${(kb / 1024).toFixed(1)} MB`;
|
||||||
return `${(kb / (1024 * 1024)).toFixed(2)} GB`;
|
return `${(kb / (1024 * 1024)).toFixed(2)} GB`;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extract owner & repo from URL
|
// Extract owner & repo from path
|
||||||
const getRepoInfo = () => {
|
const getRepoInfo = () => {
|
||||||
const [, owner, repo] = location.pathname.split('/');
|
const [, owner, repo] = location.pathname.split('/');
|
||||||
const reserved = new Set([
|
const reserved = new Set([
|
||||||
@@ -52,7 +52,7 @@
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch repository size from GitHub API (fallback to Git Trees when size is 0)
|
// Fetch full repository size from GitHub API (no blobs API fallback)
|
||||||
const fetchRepoSize = async (owner, repo) => {
|
const fetchRepoSize = async (owner, repo) => {
|
||||||
const key = `${owner}/${repo}`;
|
const key = `${owner}/${repo}`;
|
||||||
if (cache.has(key)) return cache.get(key);
|
if (cache.has(key)) return cache.get(key);
|
||||||
@@ -64,29 +64,26 @@
|
|||||||
try {
|
try {
|
||||||
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}`, { headers });
|
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}`, { headers });
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
if (res.status === 404) return pat ? 'repo not found' : 'private (missing GH_PAT)';
|
if (res.status === 404) return { text: 'private (missing GH_PAT)', title: 'Set GH_PAT secret in OpenScript for private repository access' };
|
||||||
if (res.status === 401) return 'invalid GH_PAT';
|
if (res.status === 401) return { text: 'invalid GH_PAT', title: 'GH_PAT token was rejected by GitHub API' };
|
||||||
return `API error (${res.status})`;
|
return { text: `API error (${res.status})`, title: `GitHub API returned ${res.status}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
let bytes = (data.size || 0) * 1024;
|
const kb = data.size ?? 0;
|
||||||
|
const isZero = kb === 0;
|
||||||
|
|
||||||
// GitHub async calculation fallback: if size is 0, sum blob sizes via Git Trees API
|
const result = {
|
||||||
if (bytes <= 0) {
|
text: formatKb(kb),
|
||||||
const branch = data.default_branch || 'main';
|
title: isZero
|
||||||
const treeRes = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees/${branch}?recursive=1`, { headers });
|
? 'GitHub is still calculating disk usage for this new/recent repo. Check back in a few minutes.'
|
||||||
if (treeRes.ok) {
|
: `Total repository disk usage (including full git history): ${kb.toLocaleString()} KB`
|
||||||
const treeData = await treeRes.json();
|
};
|
||||||
bytes = (treeData.tree || []).reduce((acc, item) => acc + (item.size || 0), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const formatted = formatSize(bytes);
|
if (!isZero) cache.set(key, result);
|
||||||
cache.set(key, formatted);
|
return result;
|
||||||
return formatted;
|
|
||||||
} catch {
|
} catch {
|
||||||
return 'failed to load';
|
return { text: 'failed to load', title: 'Network or fetch error' };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -126,9 +123,12 @@
|
|||||||
if (sibling) sibling.insertAdjacentElement('afterend', row);
|
if (sibling) sibling.insertAdjacentElement('afterend', row);
|
||||||
else heading.insertAdjacentElement('afterend', row);
|
else heading.insertAdjacentElement('afterend', row);
|
||||||
|
|
||||||
const size = await fetchRepoSize(info.owner, info.repo);
|
const sizeInfo = await fetchRepoSize(info.owner, info.repo);
|
||||||
const valEl = row.querySelector('.size-val');
|
const valEl = row.querySelector('.size-val');
|
||||||
if (valEl) valEl.textContent = size;
|
if (valEl) {
|
||||||
|
valEl.textContent = sizeInfo.text;
|
||||||
|
if (sizeInfo.title) row.setAttribute('title', sizeInfo.title);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Navigation handlers & periodic check during dynamic React sidebar hydration
|
// Navigation handlers & periodic check during dynamic React sidebar hydration
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -1,16 +1,17 @@
|
|||||||
# Display Repo Size for GitHub (OpenScript Userscript)
|
# Display Repo Size for GitHub (OpenScript Userscript)
|
||||||
|
|
||||||
A lightweight OpenScript userscript that displays the total repository size directly in the **About** section on GitHub repository pages (for both public and private repositories).
|
A lightweight OpenScript userscript that displays the total repository size (including all commit history, branches, tags, and packfiles) directly in the **About** section on GitHub repository pages (for both public and private repositories).
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
- **Integrated in About Section**: Injects cleanly under repository details in the right sidebar (e.g. `📦 673.9 KB repo size`).
|
- **Accurate Git History Size**: Uses GitHub's native `repo.size` disk usage measurement, accurately reflecting the full Git history, delta compression, and packfiles rather than loose blobs.
|
||||||
- **Accurate Size Detection**: Automatically detects when GitHub's API returns `0 KB` on newly created repositories and falls back to calculating the total content size via the Git Trees API.
|
- **New Repo Handling**: If a repository was just pushed and GitHub is still computing initial disk usage (`0 KB`), it provides a friendly indicator (`0 KB (calculating...)`) until GitHub finishes indexing.
|
||||||
- **Private & Public Repositories**: Works on public repositories out-of-the-box and uses `GH_PAT` from OpenScript secrets for private repositories.
|
- **Integrated in About Section**: Injects cleanly under repository details in the right sidebar.
|
||||||
|
- **Private Repositories Supported**: Uses `GH_PAT` from OpenScript secrets for private repositories and increased rate limits.
|
||||||
- **Turbo / SPA Compatible**: Seamlessly persists across GitHub's Turbo and client-side page transitions.
|
- **Turbo / SPA Compatible**: Seamlessly persists across GitHub's Turbo and client-side page transitions.
|
||||||
|
|
||||||
## Installation in OpenScript
|
## Installation in OpenScript
|
||||||
1. Open the **OpenScript** extension popup.
|
1. Open the **OpenScript** extension popup.
|
||||||
2. Click **+ New** in the header.
|
2. Click **+ New** in the header (or click your existing script to edit).
|
||||||
3. Paste the contents of [`DisplayRepoSizeGitHub.user.js`](./DisplayRepoSizeGitHub.user.js).
|
3. Paste the contents of [`DisplayRepoSizeGitHub.user.js`](./DisplayRepoSizeGitHub.user.js).
|
||||||
4. Click **save script**.
|
4. Click **save script**.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user