-
Notifications
You must be signed in to change notification settings - Fork 1
/
new-subs-migrate.py
executable file
·371 lines (333 loc) · 8.43 KB
/
new-subs-migrate.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env -S python3 -u
# migrate: move files away from main branch
# TODO atomic adding of files
import sys
import os
import re
import subprocess
import shlex
import shutil
# verbose
debug_print = print
# quiet
debug_print = lambda _: None
src_dir = "new-subs"
src_dir = "new-subs-repo"
new_subs_dir = "new-subs-repo"
remote_url = os.environ.get("NEW_SUBS_REPO_URL")
assert remote_url
if not os.path.exists(f"{new_subs_dir}/.git"):
print("git init")
args = [
"git",
"-c", "init.defaultBranch=main",
"-C", new_subs_dir,
"init",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
print("git remote add")
args = [
"git",
"-C", new_subs_dir,
"remote",
"add",
"origin",
remote_url,
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
print("getting list of existing files")
print("git pull origin main")
args = [
"git",
"-C", new_subs_dir,
"pull",
#"--force", # untracked working tree files will be overwritten by checkout
"origin",
"main",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=9999,
)
print("git checkout main")
args = [
"git",
"-C", new_subs_dir,
"checkout",
"--quiet",
#"--force", # untracked working tree files will be overwritten by checkout
"main",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
done_nums = []
files_txt_path = f"{new_subs_dir}/files.txt"
if os.path.exists(files_txt_path):
with open(files_txt_path) as f:
for line in f:
filename = line.strip()
if filename.startswith(".git"):
continue
if filename in {"files.txt", "nums"}:
continue
try:
num = int(filename.split(".", 1)[0])
except ValueError:
print("skipping file", filename)
continue
done_nums.append(num)
print(f"found {len(done_nums)} done nums")
done_nums_set = set(done_nums)
nums_todo = None
if len(sys.argv) > 2 and sys.argv[1] == "--nums":
print(f"processing nums from argv:", sys.argv[2:])
nums_todo = set(map(int, sys.argv[2:]))
else:
print(f"processing files in {src_dir} ...")
for filename in os.listdir(src_dir):
if filename.startswith(".git"):
continue
if filename in {"files.txt", "nums"}:
continue
try:
num = int(filename.split(".", 1)[0])
except ValueError:
print("skipping file", filename)
continue
if num in done_nums_set:
continue
if nums_todo and not num in nums_todo:
continue
if filename.endswith(".zip"):
# add to branch f"nums/{num}"
print("adding file", filename)
# https://stackoverflow.com/questions/53005845/checking-out-orphan-branch-in-new-work-tree
# d=subdir; n=some-branch; git worktree add --detach --no-checkout $d; git -C $d checkout --orphan $n; git reset; git clean -fdq; echo hello >$d/test.txt; git -C $d add test.txt; git -C $d commit -m init
worktree_path = f"{new_subs_dir}/nums/{num}"
if os.path.exists(worktree_path):
# remove old worktree
args = [
"git",
"-C", new_subs_dir,
"worktree",
"remove",
"--force",
f"nums/{num}", # worktree path
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", new_subs_dir,
"worktree",
"add",
"--quiet",
"--detach",
"--no-checkout",
f"nums/{num}", # worktree path
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
# FIXME fatal: a branch named 'nums/9539188' already exists
args = [
"git",
"-C", worktree_path,
"checkout",
"--quiet",
"--orphan",
f"nums/{num}", # branch name
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", worktree_path,
"reset",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", worktree_path,
"clean",
"-fdq",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
# copy file to worktree
shutil.copyfile(
f"{src_dir}/{filename}",
f"{new_subs_dir}/nums/{num}/{filename}",
)
args = [
"git",
"-C", worktree_path,
"add",
filename
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
# disable compression for zip files
gitattributes_path = f"{new_subs_dir}/nums/{num}/.gitattributes"
# https://stackoverflow.com/questions/7102053/git-pull-without-remotely-compressing-objects
with open(gitattributes_path, "w") as f:
f.write("*.zip -delta\n")
args = [
"git",
"-C", worktree_path,
"add",
os.path.basename(gitattributes_path),
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", worktree_path,
"commit",
"--quiet",
"-m", f"add {num}",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", new_subs_dir,
"worktree",
"remove",
f"nums/{num}", # worktree path
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
# add to files.txt in main branch
with open(files_txt_path, "a") as f:
f.write(f"{filename}\n")
args = [
"git",
"-C", new_subs_dir,
"add",
# note: actually: relative path to new_subs_dir
os.path.basename(files_txt_path),
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
args = [
"git",
"-C", new_subs_dir,
"commit",
"--quiet",
"-m", f"files.txt: add {num}",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
# cleanup: rm file from main branch
try:
args = [
"git",
"-C", new_subs_dir,
"rm",
#"--quiet",
filename,
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
args = [
"git",
"-C", new_subs_dir,
"commit",
"--quiet",
"-m", f"rm {num}",
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=10,
)
except subprocess.CalledProcessError as error:
debug_print(f"rm {new_subs_dir}/{filename}")
#os.unlink(f"{new_subs_dir}/{filename}")
os.unlink(f"{src_dir}/{filename}",)
# TODO git rebase
# TODO git push --force
print("git push")
args = [
"git",
"-C", new_subs_dir,
"push",
"--all", # push all branches
remote_url,
]
debug_print(shlex.join(args))
proc = subprocess.run(
args,
check=True,
timeout=9999,
#capture_output=True,
#encoding="utf8",
)