-
Notifications
You must be signed in to change notification settings - Fork 2
/
regen-git.py
executable file
·120 lines (98 loc) · 3.8 KB
/
regen-git.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import datetime
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Optional
import regen # should live alongside this script
sys.path.insert(0, os.path.dirname(__file__))
SKEL_BRANCH = "skel"
def find_git_dir(path: Optional[Path] = None) -> Path:
if path is None:
path = Path(".").absolute()
dev = path.stat().st_dev
while path != Path("/"):
trial = path / ".git"
if trial.exists():
return trial
path = path.parent
if path.stat().st_dev != dev:
raise Exception("No git dir found on same device")
raise Exception("No git dir found before root")
def main():
skel_rev = subprocess.check_output(
["git", "describe", "--always", "--dirty"],
cwd=os.path.dirname(__file__),
encoding="utf-8",
).strip()
git_branches = subprocess.check_output(
["git", "branch", "-a"], encoding="utf-8"
).splitlines(False)
print("Branches", git_branches)
if f"* {SKEL_BRANCH}" in git_branches:
raise Exception(f"Branch {SKEL_BRANCH!r} appears to be checked out")
existing_branch = f" {SKEL_BRANCH}" in git_branches
try:
git_user = subprocess.check_output(
["git", "config", "--get", "--local", "user.name"]
).strip()
except subprocess.CalledProcessError:
git_user = None
try:
git_email = subprocess.check_output(
["git", "config", "--get", "--local", "user.email"]
).strip()
except subprocess.CalledProcessError:
git_email = None
git_dir = find_git_dir()
print(f"Using git_dir {git_dir!r}")
with tempfile.TemporaryDirectory() as d:
os.chdir(d)
if not existing_branch:
subprocess.check_call(["git", "init"])
if git_user:
subprocess.check_call(["git", "config", "user.name", git_user])
if git_email:
subprocess.check_call(["git", "config", "user.email", git_email])
subprocess.check_call(["git", "remote", "add", "origin", git_dir])
subprocess.check_call(["git", "checkout", "--orphan", SKEL_BRANCH])
regen.main()
subprocess.check_call(["git", "add", "-A"])
try:
subprocess.check_call(
["git", "commit", "-m", f"Initialize skel\n\nrev: {skel_rev}"]
)
except subprocess.CalledProcessError as e:
print("\x1b[33mNo changes?\x1b[0m")
raise
subprocess.check_call(["git", "push", "origin", SKEL_BRANCH])
print(
"Good luck, the first "
f"'\x1b[32mgit merge --allow-unrelated-histories {SKEL_BRANCH}\x1b[0m' "
"is typically full of conflicts."
)
else:
subprocess.check_call(["git", "clone", "-b", SKEL_BRANCH, git_dir, "t"])
os.chdir("t")
if git_user:
subprocess.check_call(["git", "config", "user.name", git_user])
if git_email:
subprocess.check_call(["git", "config", "user.email", git_email])
regen.main()
subprocess.check_call(["git", "add", "-A"])
date = datetime.datetime.now().strftime("%Y-%m-%d")
try:
subprocess.check_call(
["git", "commit", "-m", f"Update skel {date}\n\nrev: {skel_rev}"]
)
except subprocess.CalledProcessError as e:
print("\x1b[33mNo changes?\x1b[0m")
return
subprocess.check_call(["git", "push", "origin", SKEL_BRANCH])
print(
f"Completed; use '\x1b[32mgit merge {SKEL_BRANCH}\x1b[0m' to pull in changes."
)
if __name__ == "__main__":
main()