Repository navigation
Expand file tree
/
Copy pathcopy-from-ox
More file actions
executable file
·83 lines (64 loc) · 3 KB
/
Copy pathcopy-from-ox
File metadata and controls
executable file
·83 lines (64 loc) · 3 KB
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
#!/usr/bin/env python
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
# Copy any new commits from the same branch in the kitty source tree on the ox
# machine onto the current branch of this tree. The copied commits are signed
# and have any Co-authored-by trailers removed from their messages. Since this
# rewrites the commits, commits on ox that have already been copied are
# detected by their patch ids and skipped.
import os
import re
import shlex
import subprocess
import sys
HOST = 'ox'
REMOTE_PATH = '~/kitty-src'
base = os.path.dirname(os.path.abspath(__file__))
os.chdir(base)
def run(what: str) -> None:
ret = subprocess.run(shlex.split(what))
if ret.returncode != 0:
raise SystemExit(ret.returncode)
def output(what: str) -> str:
return subprocess.check_output(shlex.split(what)).decode('utf-8').strip()
def clean_message(msg: str) -> str:
lines = [x for x in msg.splitlines() if not re.match(r'\s*co-authored-by\s*:', x, flags=re.IGNORECASE)]
return '\n'.join(lines).rstrip() + '\n'
class CopyFailed(Exception):
pass
def copy_commit(commit: str) -> None:
fmt = '%an%x00%ae%x00%ad%x00%B'
name, email, date, msg = subprocess.check_output(
['git', 'log', '-1', '--date=raw', f'--format={fmt}', commit]).decode('utf-8').split('\0', 3)
if subprocess.run(['git', 'cherry-pick', '--no-commit', commit]).returncode != 0:
raise CopyFailed(f'Failed to cherry-pick {commit}')
env = dict(os.environ, GIT_AUTHOR_NAME=name, GIT_AUTHOR_EMAIL=email, GIT_AUTHOR_DATE=date)
if subprocess.run(
['git', 'commit', '--quiet', '--no-verify', '--allow-empty', '-S', '--cleanup=verbatim', '-F', '-'],
input=clean_message(msg).encode('utf-8'), env=env,
).returncode != 0:
raise CopyFailed(f'Failed to commit {commit}')
branch = sys.argv[1] if len(sys.argv) > 1 else output('git branch --show-current')
if not branch:
raise SystemExit('Not on a branch and no branch specified')
if output('git status --porcelain --untracked-files=no'):
raise SystemExit('Working tree has uncommitted changes, aborting')
if output('git branch --show-current') != branch:
run(f'git checkout {branch}')
before = output('git rev-parse HEAD')
run(f'git fetch ssh://{HOST}/{REMOTE_PATH} {branch}')
remote_head = output('git rev-parse FETCH_HEAD')
# Commits reachable from FETCH_HEAD but not HEAD whose changes are not
# already present in HEAD, oldest first
new_commits = [x[2:] for x in output(f'git cherry HEAD {remote_head}').splitlines() if x.startswith('+ ')]
if not new_commits:
print('No new commits to copy')
raise SystemExit(0)
for commit in new_commits:
try:
copy_commit(commit)
except (CopyFailed, subprocess.CalledProcessError) as err:
subprocess.run(['git', 'cherry-pick', '--quit'])
subprocess.run(['git', 'reset', '--quiet', '--hard', before])
raise SystemExit(f'{err}, branch reset to its original state')
after = output('git rev-parse HEAD')
run(f'git log --oneline {before}..{after}')