File size: 1,216 Bytes
62896ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import subprocess
import sys


def get_staged_files():
    result = subprocess.run(
        ["git", "diff", "--cached", "--name-only", "--diff-filter=AM"],
        check=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    return [line.strip() for line in result.stdout.splitlines() if line.strip()]


def main():
    max_mb = float(os.environ.get("MAX_FILE_SIZE_MB", "100"))
    max_bytes = int(max_mb * 1024 * 1024)

    oversized = []
    for path in get_staged_files():
        if not os.path.isfile(path):
            continue
        try:
            size = os.path.getsize(path)
        except OSError:
            continue
        if size > max_bytes:
            oversized.append((path, size))

    if oversized:
        print("ERROR: fichiers trop volumineux dans le commit.")
        print(f"Limite: {max_mb:.0f} MB")
        for path, size in oversized:
            size_mb = size / (1024 * 1024)
            print(f"- {path} ({size_mb:.2f} MB)")
        print("Supprime-les du commit ou utilise Git LFS si nécessaire.")
        return 1

    return 0


if __name__ == "__main__":
    sys.exit(main())