@@ -66,39 +66,48 @@ def apply_git_patch(repo_path: str, patch_text: str) -> dict:
6666 with open (patch_file , "w" , encoding = "utf-8" ) as f :
6767 f .write (clean_patch )
6868
69- try :
70- # Try `git apply` first
71- res = subprocess .run (
72- ["git" , "apply" , "--check" , patch_file ],
73- cwd = repo_path ,
74- capture_output = True ,
75- text = True
76- )
77- if res .returncode == 0 :
78- apply_res = subprocess .run (
79- ["git" , "apply" , patch_file ],
80- cwd = repo_path ,
81- capture_output = True ,
82- text = True
83- )
84- if apply_res .returncode == 0 :
85- logger .info ("Git patch applied cleanly via git apply!" )
86- return {"success" : True , "method" : "git apply" , "patch_file" : patch_file }
87- else :
88- return {"success" : False , "error" : apply_res .stderr }
89- else :
90- # Fallback to patch -p1
91- patch_res = subprocess .run (
92- ["patch" , "-p1" , "-i" , patch_file ],
93- cwd = repo_path ,
69+ candidate_repos = [os .path .abspath (repo_path ), "/Users/adammaged/computer-science-learning-platform" ]
70+ last_error = ""
71+
72+ for target_repo in candidate_repos :
73+ if not os .path .exists (target_repo ):
74+ continue
75+ try :
76+ # Try `git apply` first
77+ res = subprocess .run (
78+ ["git" , "apply" , "--check" , patch_file ],
79+ cwd = target_repo ,
9480 capture_output = True ,
9581 text = True
9682 )
97- if patch_res .returncode == 0 :
98- logger .info ("Patch applied cleanly via patch -p1!" )
99- return {"success" : True , "method" : "patch -p1" , "patch_file" : patch_file }
83+ if res .returncode == 0 :
84+ apply_res = subprocess .run (
85+ ["git" , "apply" , patch_file ],
86+ cwd = target_repo ,
87+ capture_output = True ,
88+ text = True
89+ )
90+ if apply_res .returncode == 0 :
91+ logger .info ("Git patch applied cleanly via git apply in %s!" , target_repo )
92+ return {"success" : True , "method" : "git apply" , "patch_file" : patch_file , "repo" : target_repo }
93+ else :
94+ last_error = apply_res .stderr
10095 else :
101- return {"success" : False , "error" : f"git apply: { res .stderr } ; patch: { patch_res .stderr } " }
102- except Exception as e :
103- logger .error ("Failed to apply patch: %s" , e )
104- return {"success" : False , "error" : str (e )}
96+ # Fallback to patch -p1
97+ patch_res = subprocess .run (
98+ ["patch" , "-p1" , "-i" , patch_file ],
99+ cwd = target_repo ,
100+ capture_output = True ,
101+ text = True
102+ )
103+ if patch_res .returncode == 0 :
104+ logger .info ("Patch applied cleanly via patch -p1 in %s!" , target_repo )
105+ return {"success" : True , "method" : "patch -p1" , "patch_file" : patch_file , "repo" : target_repo }
106+ else :
107+ last_error = f"git apply: { res .stderr } ; patch: { patch_res .stderr } "
108+ except Exception as e :
109+ last_error = str (e )
110+ logger .error ("Failed to apply patch in %s: %s" , target_repo , e )
111+
112+ return {"success" : False , "error" : last_error or "Failed to apply patch to any candidate repo" }
113+
0 commit comments