forked from cosmicpython/book
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupdate-exercise-branch.py
More file actions
executable file
·38 lines (31 loc) · 925 Bytes
/
update-exercise-branch.py
File metadata and controls
executable file
·38 lines (31 loc) · 925 Bytes
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
#!/usr/bin/env python
import sys
import subprocess
from pathlib import Path
def run(cmds):
print(' '.join(cmds))
p = subprocess.run(
cmds,
cwd=Path(__file__).parent / 'code',
capture_output=True,
check=True
)
if p.returncode:
raise Exception(p.stderr.decode())
output = p.stdout.decode()
print(output)
return output
all_branches = run(['git', 'branch', '-a'],)
def main(chapter):
exercise_chapter = f'{chapter}_exercise'
assert exercise_chapter in all_branches
run(['git', 'checkout', exercise_chapter])
commits = list(reversed(run([
'git', 'log', '--pretty=%h',
f'{exercise_chapter}^{{/{chapter}_ends}}..{exercise_chapter}',
]).split()))
run(['git', 'reset', '--hard', chapter])
run(['git', 'cherry-pick', *commits])
run(['git', 'checkout', 'master'])
if __name__ == '__main__':
main(sys.argv[1])