-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupstream.py
More file actions
109 lines (92 loc) · 3.97 KB
/
upstream.py
File metadata and controls
109 lines (92 loc) · 3.97 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
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
import os
import subprocess
from dotenv import load_dotenv
load_dotenv()
TOKEN_GITHUB = os.getenv('TOKEN_GITHUB', '')
USER = "MoeKernel"
REPO = "android_kernel_xiaomi_ginkgo"
def run_command(command):
"""Executes a shell command and returns the output."""
result = subprocess.run(command, shell=True, text=True, capture_output=True)
if result.stdout:
return result.stdout.strip()
if result.stderr:
return result.stderr.strip()
return None
def clone_repo():
"""Clones the kernel repository."""
remote_url = f'https://{USER}:{TOKEN_GITHUB}@github.com/{USER}/{REPO}.git'
if not os.path.exists(REPO):
result = run_command(f"git clone {remote_url}")
print(result)
os.chdir(REPO)
else:
print(f"The repository {REPO} has already been cloned.")
def fetch_and_reset_upstream(upstream_repo, branch):
"""Fetches updates from the upstream repository and applies them to the current branch."""
run_command(f"git remote add upstream {upstream_repo} || true")
run_command(f"git fetch upstream {branch}")
run_command(f"git reset --hard upstream/linux-4.14.y")
def merge_with_strategy(branch):
"""Merges the upstream branch with 'ours' strategy."""
result = run_command(f"git merge -X ours upstream/{branch}")
if result and "CONFLICT" in result:
print("Conflicts detected but preserved local changes with 'ours' strategy.")
run_command("git commit -m 'Resolved conflicts: preserved local changes using ours'")
else:
print(f"Merge of upstream changes from {branch} completed successfully.")
def clean_and_commit_makefile():
"""Removes the '-openela' part from the EXTRAVERSION variable in the Makefile and commits the changes."""
try:
with open('Makefile', 'r') as file:
lines = file.readlines()
modified = False
with open('Makefile', 'w') as file:
for line in lines:
if line.startswith("EXTRAVERSION"):
if '-openela' in line:
line = "EXTRAVERSION =\n"
modified = True
file.write(line)
if modified:
print("Makefile updated. Committing changes...")
run_command("git add Makefile")
run_command('git commit -m "Remove -openela from EXTRAVERSION in Makefile"')
print("Changes to the Makefile committed successfully.")
else:
print("No changes needed in the Makefile.")
except FileNotFoundError:
print("Makefile not found. Please check if the file is present.")
def push_changes(branch):
"""Pushes the changes to the original repository."""
remote_url = f'https://{USER}:{TOKEN_GITHUB}@github.com/{USER}/{REPO}.git'
subprocess.run(["git", "remote", "set-url", "origin", remote_url])
subprocess.run(["git", "config", "--global", "user.email", "akariondev@gmail.com"])
subprocess.run(["git", "config", "--global", "user.name", "Akari Shoiya"])
result = run_command(f"git push origin {branch} --force")
print(result)
if result:
print(f"Pushed changes to branch {branch} successfully.")
else:
print(f"Failed to push changes to branch {branch}.")
def process_branches(upstream_repo, upstream_branch, branches):
"""Processes a list of branches to update and push changes."""
fetch_and_reset_upstream(upstream_repo, upstream_branch)
for branch in branches:
print(f"Processing branch {branch}...")
run_command(f"git checkout {branch}")
merge_with_strategy(upstream_branch)
clean_and_commit_makefile()
push_changes(branch)
if __name__ == "__main__":
upstream_repo = "https://github.com/openela/kernel-lts.git"
upstream_branch = "linux-4.14.y"
branches = [
"fourteen_tests"
# "fourteen_dynamic_noksu",
# "fourteen_dynamic",
# "without-ksu",
# "fourteen",
]
#clone_repo()
process_branches(upstream_repo, upstream_branch, branches)