Repository navigation
Expand file tree
/
Copy pathresolve_conflicts.py
More file actions
executable file
·206 lines (164 loc) · 6.72 KB
/
Copy pathresolve_conflicts.py
File metadata and controls
executable file
·206 lines (164 loc) · 6.72 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
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
#!/usr/bin/env python3
"""
Script to help resolve merge conflicts in a systematic way.
This script looks for common conflict patterns and applies resolutions.
"""
import os
import re
import subprocess
from typing import List, Tuple
# Get list of files with conflicts
def get_conflicted_files() -> List[str]:
result = subprocess.run(
["git", "diff", "--name-only", "--diff-filter=U"],
capture_output=True,
text=True,
)
return result.stdout.strip().split("\n")
# Read file content
def read_file(file_path: str) -> str:
with open(file_path, "r") as f:
return f.read()
# Write file content
def write_file(file_path: str, content: str) -> None:
with open(file_path, "w") as f:
f.write(content)
# Find conflict markers in content
def find_conflicts(content: str) -> List[Tuple[int, int, int]]:
"""Find conflict markers in content and return (start, middle, end) line indices."""
lines = content.split("\n")
conflicts = []
start_idx = -1
middle_idx = -1
for i, line in enumerate(lines):
if line.startswith("<<<<<<<"):
start_idx = i
elif line.startswith("=======") and start_idx != -1:
middle_idx = i
elif line.startswith(">>>>>>>") and start_idx != -1 and middle_idx != -1:
conflicts.append((start_idx, middle_idx, i))
start_idx = -1
middle_idx = -1
return conflicts
# Resolve a specific conflict
def resolve_conflict(content: str, conflict: Tuple[int, int, int]) -> str:
"""Resolve a specific conflict based on patterns."""
lines = content.split("\n")
start_idx, middle_idx, end_idx = conflict
# Extract the two versions
head_version = "\n".join(lines[start_idx + 1 : middle_idx])
master_version = "\n".join(lines[middle_idx + 1 : end_idx])
# Determine which version to use
# Check for common patterns
# Pattern 1: Simple formatting differences (prefer master's compact format)
if (
head_version.strip().startswith('f"')
and master_version.strip().startswith('f"')
and head_version.count("{") == master_version.count("{")
):
resolved_version = master_version
# Pattern 2: Empty implementation vs actual implementation (prefer implementation)
elif head_version.strip() == "" and master_version.strip() == "pass":
resolved_version = master_version
elif head_version.strip() == "pass" and master_version.strip() == "":
resolved_version = head_version
# Pattern 3: Different error messages (prefer more detailed message)
elif '"message":' in head_version and '"message":' in master_version:
if len(head_version) > len(master_version):
resolved_version = head_version
else:
resolved_version = master_version
# Pattern 4: Print statements with different formatting but same content
elif (
head_version.strip().startswith("print(")
and master_version.strip().startswith("print(")
and head_version.count("print") == master_version.count("print")
):
# Prefer the simpler formatting
if len(master_version) < len(head_version):
resolved_version = master_version
else:
resolved_version = head_version
# Pattern 5: Logger statements with different formatting
elif (
"logger." in head_version
and "logger." in master_version
and head_version.count("logger.") == master_version.count("logger.")
):
# Prefer the more detailed message
if len(head_version) > len(master_version):
resolved_version = head_version
else:
resolved_version = master_version
# Pattern 6: Function definitions with different docstrings
elif (
"def " in head_version
and "def " in master_version
and head_version.split("def ")[1].split("(")[0]
== master_version.split("def ")[1].split("(")[0]
):
# Keep function signature and parameters from master, but docstring from HEAD if it's longer
head_docstring = re.search(r'""".*?"""', head_version, re.DOTALL)
master_docstring = re.search(r'""".*?"""', master_version, re.DOTALL)
if head_docstring and master_docstring:
if len(head_docstring.group()) > len(master_docstring.group()):
# Replace master's docstring with head's docstring
resolved_version = master_version.replace(
master_docstring.group(), head_docstring.group()
)
else:
resolved_version = master_version
else:
resolved_version = master_version
# Pattern 7: Comments with different formatting
elif head_version.strip().startswith("#") and master_version.strip().startswith(
"#"
):
# Prefer the more detailed comment
if len(head_version) > len(master_version):
resolved_version = head_version
else:
resolved_version = master_version
# Default: Use master version for simplicity
# In a real scenario, you might want more sophisticated logic here
else:
resolved_version = master_version
# Replace the conflict with the resolved version
new_lines = lines[:start_idx] + [resolved_version] + lines[end_idx + 1 :]
return "\n".join(new_lines)
# Resolve all conflicts in a file
def resolve_file_conflicts(file_path: str) -> bool:
"""Resolve all conflicts in a file and return True if successful."""
content = read_file(file_path)
conflicts = find_conflicts(content)
if not conflicts:
return False
print(f"Found {len(conflicts)} conflicts in {file_path}")
# Resolve conflicts one by one
for conflict in conflicts:
content = resolve_conflict(content, conflict)
# Write resolved content back to file
write_file(file_path, content)
# Check if all conflicts are resolved
new_conflicts = find_conflicts(content)
if new_conflicts:
print(f"Warning: {len(new_conflicts)} conflicts remain in {file_path}")
return False
return True
# Main function
def main():
conflicted_files = get_conflicted_files()
print(f"Found {len(conflicted_files)} files with conflicts")
resolved_count = 0
for file_path in conflicted_files:
if resolve_file_conflicts(file_path):
subprocess.run(["git", "add", file_path])
resolved_count += 1
print(f"Resolved conflicts in {file_path}")
print(f"Resolved conflicts in {resolved_count}/{len(conflicted_files)} files")
if resolved_count < len(conflicted_files):
print("Some files still have conflicts. Please resolve them manually.")
else:
print("All conflicts resolved!")
if __name__ == "__main__":
main()