-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_date.py
102 lines (87 loc) · 3.48 KB
/
update_date.py
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
96
97
98
99
100
101
102
import os
import subprocess
from datetime import datetime
import yaml
def get_last_content_change_date(file_path):
"""
Retrieves the date of the last commit that changed the content of a file,
ignoring changes to the front matter.
"""
try:
# Get the commit hashes that changed the file
commits = subprocess.check_output(
['git', 'log', '--pretty=format:%H', '--', file_path],
text=True,
stderr=subprocess.DEVNULL
).strip().splitlines()
if not commits:
return None
for commit_hash in commits:
# Get the diff for this commit
diff_output = subprocess.check_output(
['git', 'show', '--pretty=format:', '--unified=0', commit_hash, file_path],
text=True,
stderr=subprocess.DEVNULL
)
# Split the diff into lines and check for content changes
diff_lines = diff_output.splitlines()
content_change = False
in_content = False
for line in diff_lines:
if line.startswith('---'):
in_content = not in_content
continue
if in_content and (line.startswith('+') or line.startswith('-')):
if not line.startswith('+++') and not line.startswith('---'):
content_change = True
break
if content_change:
# Get the commit date
date_str = subprocess.check_output(
['git', 'show', '-s', '--format=%cd', '--date=iso', commit_hash],
text=True,
stderr=subprocess.DEVNULL
).strip()
return datetime.fromisoformat(date_str)
return None
except subprocess.CalledProcessError:
return None
def update_frontmatter_date(file_path):
"""
Updates the 'updated' field in the front matter of a markdown file
with the last content change date.
"""
try:
with open(file_path, 'r') as f:
content = f.read()
parts = content.split('---', 2)
if len(parts) < 3:
print(f"Skipped: {file_path} (no frontmatter)")
return
frontmatter_str = parts[1]
body = parts[2]
frontmatter = yaml.safe_load(frontmatter_str)
if not frontmatter:
print(f"Skipped: {file_path} (empty frontmatter)")
return
last_change_date = get_last_content_change_date(file_path)
if last_change_date:
frontmatter['updated'] = last_change_date.strftime('%Y-%m-%d')
modified_frontmatter_str = yaml.dump(frontmatter, sort_keys=False)
modified_content = f"---\n{modified_frontmatter_str}---{body}"
with open(file_path, 'w') as f:
f.write(modified_content)
print(f"Updated date in {file_path} to {last_change_date.strftime('%Y-%m-%d')}")
else:
print(f"No content changes found for {file_path}")
except yaml.YAMLError as e:
print(f"Skipped: {file_path} (YAML error: {e})")
except Exception as e:
print(f"Skipped: {file_path} (Error processing file: {e})")
if __name__ == "__main__":
target_dir = "original"
for root, _, files in os.walk(target_dir):
for file in files:
if file.endswith(".md"):
file_path = os.path.join(root, file)
update_frontmatter_date(file_path)