-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackdate_commits_real_dates.py
More file actions
64 lines (51 loc) · 2.16 KB
/
backdate_commits_real_dates.py
File metadata and controls
64 lines (51 loc) · 2.16 KB
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
import os
import subprocess
import random
from datetime import datetime, timedelta
# === CONFIGURATION ===
REPO_DIR = "." # Use current directory
COMMIT_FILE = "commit_messages.txt"
NUM_COMMITS = 200
START_DATE = datetime(2021, 11, 20)
def load_commit_messages(path):
with open(path, "r", encoding="utf-8") as f:
lines = [line.strip() for line in f if line.strip()]
if len(lines) < NUM_COMMITS:
raise ValueError(f"Expected at least {NUM_COMMITS} unique commit messages.")
random.shuffle(lines)
return lines[:NUM_COMMITS]
def generate_random_dates(n, start_date, end_date):
delta = (end_date - start_date).days
dates = set()
while len(dates) < n:
rand_day = start_date + timedelta(days=random.randint(0, delta))
rand_time = timedelta(hours=random.randint(0, 23), minutes=random.randint(0, 59))
full_datetime = rand_day + rand_time
dates.add(full_datetime.strftime("%Y-%m-%dT%H:%M:%S"))
return sorted(dates)
def setup_repo(path):
if not os.path.exists(os.path.join(path, ".git")):
subprocess.run(["git", "init"], cwd=path)
def make_commit(repo_path, message, date_iso):
dummy_file = os.path.join(repo_path, "log.txt")
with open(dummy_file, "a", encoding="utf-8") as f:
f.write(f"{message}\n")
env = os.environ.copy()
env["GIT_AUTHOR_DATE"] = date_iso
env["GIT_COMMITTER_DATE"] = date_iso
subprocess.run(["git", "add", "."], cwd=repo_path, env=env)
subprocess.run(["git", "commit", "-m", message], cwd=repo_path, env=env)
def main():
print("📄 Loading commit messages...")
messages = load_commit_messages(COMMIT_FILE)
print("📅 Generating random commit dates from June 14, 2016...")
dates = generate_random_dates(NUM_COMMITS, START_DATE, datetime.now())
print(f"📁 Setting up Git repo at: {REPO_DIR}")
setup_repo(REPO_DIR)
print("⏳ Creating commits...")
for i in range(NUM_COMMITS):
print(f"📦 Commit {i+1}/{NUM_COMMITS} → {dates[i]} | {messages[i]}")
make_commit(REPO_DIR, messages[i], dates[i])
print("\n✅ Done! All commits backdated randomly since June 14, 2016.")
if __name__ == "__main__":
main()