Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Created May 26, 2024 15:40
Show Gist options
  • Save jonathanagustin/9a3ebd91eaa8b39706a9a49364ed6ace to your computer and use it in GitHub Desktop.
Save jonathanagustin/9a3ebd91eaa8b39706a9a49364ed6ace to your computer and use it in GitHub Desktop.
Git LFS Track Files Over a Threshold (Default is 100MB)
#!/usr/bin/env python3
import subprocess
from pathlib import Path
SKIP_LIST = [
".git",
"build",
"venv",
".venv",
"node_modules",
]
SIZE_THRESHOLD_IN_MB = 100
CALCULATED_THRESHOLD = SIZE_THRESHOLD_IN_MB * 1024 * 1024
def track_large_files(directory):
try:
for item in directory.iterdir():
if item.name in SKIP_LIST or item.is_symlink():
continue
if item.is_dir():
track_large_files(item)
elif item.is_file():
try:
file_size = item.stat().st_size
except FileNotFoundError:
print(f"File not found: {item}")
continue
except PermissionError:
print(f"Permission denied: {item}")
continue
if file_size > CALCULATED_THRESHOLD:
try:
subprocess.run(["git", "lfs", "track", str(item)], check=True)
print(f"Tracked file: {item}")
except subprocess.CalledProcessError as e:
print(f"Error tracking file: {item}")
print(f"Error message: {e}")
except PermissionError:
print(f"Permission denied: {directory}")
except FileNotFoundError:
print(f"Directory not found: {directory}")
def install_git_lfs():
try:
subprocess.run(["git", "lfs", "install"], check=True)
except subprocess.CalledProcessError as e:
print("Error installing Git LFS")
print(f"Error message: {e}")
exit(1)
directory = Path(".")
install_git_lfs()
track_large_files(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment