-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutor.py
More file actions
304 lines (251 loc) Β· 12.2 KB
/
executor.py
File metadata and controls
304 lines (251 loc) Β· 12.2 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
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
"""
Executor - Command execution with confirmation and dry-run support
"""
import subprocess
import shlex
from typing import List
import os
class CommandExecutor:
"""Handles execution of Git commands with safety features"""
def __init__(self, dry_run: bool = False, no_confirm: bool = False, working_directory=None):
"""
Initialize the command executor
Args:
dry_run: If True, only show commands without executing
no_confirm: If True, skip confirmation prompts
"""
self.dry_run = dry_run
self.no_confirm = no_confirm
self.working_directory = working_directory or os.getcwd() # Use provided directory or current
def execute_commands(self, commands: List[str]) -> None:
"""
Execute a list of Git commands
Args:
commands: List of Git commands to execute
"""
if not commands:
print("β No commands to execute")
return
# Display commands
print("Generated commands:")
for i, cmd in enumerate(commands, 1):
print(f" {i}. {cmd}")
print()
if self.dry_run:
print("Dry run mode - commands not executed")
return
# Get confirmation unless --no-confirm is used
if not self.no_confirm:
if not self._get_confirmation():
print("β Execution cancelled")
return
# Execute commands
print("π Executing commands...")
for i, cmd in enumerate(commands, 1):
print(f"\n[{i}/{len(commands)}] {cmd}")
try:
result = subprocess.run(
shlex.split(cmd),
capture_output=True,
text=True,
cwd=self.working_directory,
timeout=30 # 30 second timeout
)
if result.returncode == 0:
print("β
Success")
if result.stdout.strip():
print(f"π€ Output:\n{result.stdout}")
else:
print(f"β Error (exit code {result.returncode})")
if result.stderr.strip():
print(f"π₯ Error output:\n{result.stderr}")
# try alternatives
error_msg = result.stderr.lower()
retry_attempted = False
if "pathspec" in error_msg and "did not match" in error_msg:
retry_attempted = self._try_branch_alternatives(cmd, i, len(commands))
if not retry_attempted:
# Show suggestions if no auto-retry was possible
if "pathspec" in error_msg and "did not match" in error_msg:
print("\nπ‘ Suggestion: The reference doesn't exist (branch, file, or commit).")
print("Try these commands to check what's available:")
print(" git branch -a # See all branches")
print(" git log --oneline # See recent commits")
print(" git status # See current state")
elif "did not match any file" in error_msg:
print("\nπ‘ Suggestion: The reference doesn't exist.")
print("Check available options with: git branch -a")
# Ask if user wants to continue with remaining commands (if no successful retry)
if not retry_attempted and i < len(commands) and not self.no_confirm:
if not self._get_continue_confirmation():
print("π Execution stopped")
break
else:
# No stderr but still failed
if i < len(commands) and not self.no_confirm:
if not self._get_continue_confirmation():
print("π Execution stopped")
break
except subprocess.TimeoutExpired:
print("Command timed out after 30 seconds")
if i < len(commands) and not self.no_confirm:
if not self._get_continue_confirmation():
print("π Execution stopped")
break
except Exception as e:
print(f"β Unexpected error: {e}")
if i < len(commands) and not self.no_confirm:
if not self._get_continue_confirmation():
print("π Execution stopped")
break
print("\n⨠Command execution completed")
def _try_branch_alternatives(self, original_cmd: str, cmd_index: int, total_commands: int) -> bool:
"""
Try alternative commands when branch operations fail
Returns True if a successful retry was made, False otherwise
"""
cmd_lower = original_cmd.lower()
if not ("checkout" in cmd_lower or "switch" in cmd_lower):
return False
# Extract branch name from checkout/switch commands
parts = shlex.split(original_cmd)
if len(parts) < 2:
return False
target_branch = parts[-1] # Last argument is usually the branch
# Get all available branches
available_branches = self._get_available_branches()
if not available_branches:
return False
print(f"\nπ Looking for alternatives to '{target_branch}'...")
print(f"π Available branches: {', '.join(available_branches[:5])}{'...' if len(available_branches) > 5 else ''}")
retry_commands = []
# Strategy 1: Exact case-insensitive matches
exact_matches = [b for b in available_branches if b.lower() == target_branch.lower() and b != target_branch]
for exact_match in exact_matches[:1]: # Only try the first exact match
if "checkout" in cmd_lower:
retry_commands.append(f"git checkout {exact_match}")
elif "switch" in cmd_lower:
retry_commands.append(f"git switch {exact_match}")
# Strategy 2: Create the exact branch the user asked for
if "checkout" in cmd_lower:
retry_commands.append(f"git checkout -b {target_branch}")
elif "switch" in cmd_lower:
retry_commands.append(f"git switch -c {target_branch}")
# Strategy 3: Fuzzy matches as suggestions only (don't auto-execute)
similar_branches = self._find_similar_branches(target_branch, available_branches)
if similar_branches:
print(f"π‘ Also found similar branches: {', '.join(similar_branches[:2])}")
# Don't auto-add fuzzy matches - they should be manual suggestions
# Try each alternative
for retry_cmd in retry_commands:
print(f"\nπ Trying alternative: {retry_cmd}")
try:
retry_result = subprocess.run(
shlex.split(retry_cmd),
capture_output=True,
text=True,
cwd=self.working_directory,
timeout=30
)
if retry_result.returncode == 0:
print("β
Alternative succeeded!")
if retry_result.stdout.strip():
print(f"π€ Output:\n{retry_result.stdout}")
return True
else:
print(f"β Alternative failed: {retry_result.stderr.strip()}")
except Exception as e:
print(f"β Alternative failed with exception: {e}")
# If all retries failed, suggest fuzzy matches
if similar_branches:
print(f"\nπ‘ Did you mean one of these existing branches?")
for branch in similar_branches[:3]:
print(f" giti 'switch to {branch}'")
return False
def _get_available_branches(self) -> list:
"""Get list of all available branches (local and remote)"""
try:
result = subprocess.run(
["git", "branch", "-a"],
capture_output=True,
text=True,
cwd=self.working_directory,
timeout=10
)
if result.returncode != 0:
return []
branches = []
for line in result.stdout.strip().split('\n'):
if line.strip():
# Clean up branch names (remove *, whitespace, remotes/origin/)
branch = line.strip().replace('*', '').strip()
if branch.startswith('remotes/origin/'):
branch = branch.replace('remotes/origin/', '')
if branch and branch != 'HEAD' and '->' not in branch:
branches.append(branch)
# Remove duplicates and return
return list(set(branches))
except Exception:
return []
def _find_similar_branches(self, target: str, available: list) -> list:
"""Find branches similar to target using fuzzy matching"""
if not available:
return []
similar = []
target_lower = target.lower()
# Exact matches first (case insensitive)
for branch in available:
if branch.lower() == target_lower:
similar.append(branch)
# Partial matches
for branch in available:
branch_lower = branch.lower()
if (target_lower in branch_lower or branch_lower in target_lower) and branch not in similar:
similar.append(branch)
# Fuzzy matches (common patterns)
fuzzy_matches = []
for branch in available:
branch_lower = branch.lower()
if branch in similar:
continue
# Check for common variations
if any(pattern in branch_lower for pattern in [target_lower[:4], target_lower[-4:]]) and len(target) > 3:
fuzzy_matches.append(branch)
similar.extend(fuzzy_matches[:2]) # Add top 2 fuzzy matches
return similar[:3] # Return top 3 overall matches
def _get_confirmation(self) -> bool:
"""
Get user confirmation to execute commands
Returns:
True if user confirms, False otherwise
"""
while True:
try:
response = input("Execute these commands? [y/N]: ").strip().lower()
if response in ['y', 'yes']:
return True
elif response in ['n', 'no', '']:
return False
else:
print("Please enter 'y' for yes or 'n' for no")
except (KeyboardInterrupt, EOFError):
print("\nβ Cancelled by user")
return False
def _get_continue_confirmation(self) -> bool:
"""
Get user confirmation to continue after an error
Returns:
True if user wants to continue, False otherwise
"""
while True:
try:
response = input("Continue with remaining commands? [y/N]: ").strip().lower()
if response in ['y', 'yes']:
return True
elif response in ['n', 'no', '']:
return False
else:
print("Please enter 'y' for yes or 'n' for no")
except (KeyboardInterrupt, EOFError):
print("\nβ Cancelled by user")
return False