summaryrefslogtreecommitdiff
path: root/get-commit-dates.py
blob: 53485d5ce4b93520c8de2d64b51456124ff7c87e (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import json
import subprocess

INPUT_FILE = "static/js/data.json"
OUTPUT_FILE = "data_with_dates.json"

def get_git_info(filepath):
    try:
        cmd = [
            "git", "log",
            "--diff-filter=A",
            "--follow",
            "--reverse",
            "--format=%ad|%an",
            "--date=format:%Y-%m-%d",
            "--", filepath
        ]

        output = subprocess.check_output(cmd, text=True).strip()
        if not output:
            return None, None

        first_line = output.splitlines()[0]
        date, author = first_line.split("|", 1)
        return date, author

    except subprocess.CalledProcessError:
        return None, None


with open(INPUT_FILE, "r", encoding="utf-8") as f:
    data = json.load(f)


cache = {}

for key, entry in data.items():
    image = entry.get("image")
    if not image:
        continue

    if image not in cache:
        cache[image] = get_git_info(image)

    date, author = cache[image]

    if date:
        entry["date"] = date
#    if author:
#        entry["committer"] = author


with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2, ensure_ascii=False)


#import subprocess
#import json
#from pathlib import Path
#
#repo_path = Path(".")
#images_path = repo_path / "static/images"
#
#result = {}
#
#for image in images_path.glob("*"):
#    try:
#        cmd = [
#            "git", "log",
#            "--diff-filter=A",
#            "--follow",
#            "--reverse",
#            "--format=%ad|%an",
#            "--date=format:%Y-%m-%d",
#            "--", str(image)
#        ]
#
#        output = subprocess.check_output(cmd, text=True).strip()
#        if not output:
#            continue
#
#        first_line = output.splitlines()[0]
#        date, author = first_line.split("|", 1)
#
#        result[str(image)] = {
#            "date": date,
#            "committer": author
#        }
#
#    except subprocess.CalledProcessError:
#        continue
#
#print(json.dumps(result, indent=2))
#