Created
June 10, 2025 19:04
-
-
Save shole/f8d9ced58531b51478e5dd4366e40e1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import sys | |
from pathlib import Path | |
def human_readable_size(size_bytes): | |
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB']: | |
if size_bytes < 1024: | |
return f"{size_bytes:.2f} {unit}" | |
size_bytes /= 1024 | |
return f"{size_bytes:.2f} PiB" | |
def get_directory_size(path): | |
total = 0 | |
for dirpath, _, filenames in os.walk(path): | |
for f in filenames: | |
try: | |
fp = os.path.join(dirpath, f) | |
total += os.path.getsize(fp) | |
except (FileNotFoundError, PermissionError): | |
continue | |
return total | |
def get_git_root(): | |
try: | |
return Path(subprocess.check_output( | |
["git", "rev-parse", "--show-toplevel"], | |
stderr=subprocess.DEVNULL | |
).decode().strip()) | |
except subprocess.CalledProcessError: | |
print("β Not a Git repository.") | |
sys.exit(1) | |
def get_lfs_objects_size(git_root): | |
lfs_path = git_root / ".git" / "lfs" / "objects" | |
if not lfs_path.exists(): | |
return 0, "β οΈ No LFS objects directory found (no LFS files have been downloaded yet)." | |
size = get_directory_size(lfs_path) | |
return size, None | |
def main(include_working=False): | |
git_root = get_git_root() | |
print(f"π Repository: {git_root.name}") | |
git_dir = git_root / ".git" | |
lfs_size, lfs_warning = get_lfs_objects_size(git_root) | |
git_size = get_directory_size(git_dir) | |
if lfs_warning: | |
print(f"β .git directory size: {human_readable_size(git_size)}") | |
print(lfs_warning) | |
else: | |
print(f"β .git directory size: {human_readable_size(git_size-lfs_size)}") | |
print(f"β All Git LFS objects size (in .git/lfs/objects): {human_readable_size(lfs_size)}") | |
if include_working: | |
print("π Measuring working directory (excluding .git)...") | |
total_size = 0 | |
for item in git_root.iterdir(): | |
if item.name == ".git": | |
continue | |
total_size += get_directory_size(item) | |
print(f"π¦ Working directory size: {human_readable_size(total_size)}") | |
if __name__ == "__main__": | |
include = "--include-working" in sys.argv | |
main(include_working=include) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment