-
Notifications
You must be signed in to change notification settings - Fork 0
/
safety-mirror.py
51 lines (46 loc) · 1.44 KB
/
safety-mirror.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
import json
import subprocess
def command_output(cmd):
return (
subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
.stdout.read()
.decode("utf-8")
)
remotes = command_output(["git", "remote"]).split()
for repo in json.load(open("sources.json")):
subprocess.run(
[
"git",
"remote",
"set-url" if repo["name"] in remotes else "add",
repo["name"],
repo["url"],
]
)
for branch in repo.get("branches", ["main"]):
subprocess.run(["git", "fetch", repo["name"], branch])
subprocess.run(
[
"git",
"merge",
"--allow-unrelated-histories",
"--no-commit",
f"""{repo["name"]}/{branch}""",
]
)
to_del = []
to_reset = []
for status in command_output(["git", "status", "--porcelain"]).splitlines():
filename = status[3:].strip()
{
"A ": to_del,
"DU ": to_del,
"AA ": to_reset,
"UU ": to_reset,
}.get(status[:3], []).append(filename)
if to_del:
subprocess.run(["git", "rm", "-f"] + to_del)
if to_reset:
subprocess.run(["git", "reset", "--"] + to_reset)
subprocess.run(["git", "checkout", "--"] + to_reset)
subprocess.run(["git", "commit"])