-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChronoCommits.py
88 lines (72 loc) · 2.83 KB
/
ChronoCommits.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
import logging
import subprocess
from datetime import date, datetime, timedelta
from pathlib import Path
from tempfile import TemporaryDirectory
from rich.logging import RichHandler
from rich.progress import track
from rich.prompt import Prompt, IntPrompt
logger = logging.getLogger("ChronoCommits")
cwd = Path(__file__).parent.resolve()
def run_from_path_silent(args, cwd: str):
return subprocess.run(args, cwd=cwd, stdout=subprocess.DEVNULL)
def iter_dates(start: date, end: date):
delta = end - start
for i in range(delta.days + 1):
day = start + timedelta(days=i)
yield day
def create_empty_past_commit(d: date, directory: str) -> None:
dt = datetime.combine(d, datetime.min.time())
iso_string = dt.isoformat()
subprocess.run(
[
"git",
"commit",
"--allow-empty",
"--message",
"Testing ChronoCommits",
"--date",
iso_string,
],
env={
"GIT_AUTHOR_DATE": iso_string,
"GIT_COMMITTER_DATE": iso_string,
},
cwd=directory,
check=True,
stdout=subprocess.DEVNULL,
)
logger.info("Created empty commit at %s", iso_string)
def setup_git_dir(email: str, path: str | Path) -> None:
run_from_path_silent(["git", "init"], cwd=path)
run_from_path_silent(["git", "config", "user.email", email], cwd=path)
logger.info("Created git repository at %s", path)
run_from_path_silent(["git", "config", "user.name", "ChronoCommits"], cwd=path)
logger.info("Set github credentials for repository.")
def push_to_remote(remote: str, path: str | Path) -> None:
run_from_path_silent(["git", "remote", "add", "origin", remote], cwd=path)
run_from_path_silent(["git", "branch", "-M", "main"], cwd=path)
run_from_path_silent(["git", "push", "-u", "origin", "main"], cwd=path)
logger.info("Pushed to remote repository: %s", remote)
def main() -> None:
with TemporaryDirectory(prefix="ChronoCommits-", delete=True) as git_dir:
logger.info("Created temporary directory at %s", git_dir)
git_email = Prompt.ask(
"An email linked to your GitHub account (can be private)"
)
setup_git_dir(git_email, git_dir)
start_year = IntPrompt.ask(
"What year would you like your commits to start from?", default=2025
)
for fake_date in track(
iter_dates(date(start_year, 1, 1), date.today()),
description="Creating fake commits in repository..",
):
create_empty_past_commit(fake_date, git_dir)
remote_url = Prompt.ask("Whats your GitHub repository URL?")
push_to_remote(remote_url, git_dir)
if __name__ == "__main__":
logging.basicConfig(
level="INFO", datefmt="[%X]", handlers=[RichHandler(show_path=False)]
)
main()