@@ -160,7 +160,7 @@ let apply_patch_with_cmd cmd hunk_patch =
160160 >> = fun () -> process#close
161161
162162let apply_patch_with_git hunk_patch =
163- apply_patch_with_cmd " git apply --index" hunk_patch
163+ apply_patch_with_cmd " git apply --index --intent-to-add " hunk_patch
164164
165165let apply_patch hunk_patch =
166166 apply_patch_with_cmd " patch -p 0" hunk_patch
@@ -169,7 +169,25 @@ let drop_into_editor editor path ~at_line =
169169 let command = Format. sprintf " %s +%d %s" editor at_line path in
170170 Lwt_unix. system command
171171
172+ let file_in_git_repo path =
173+ let command = Format. sprintf " test ! -z \" $(git ls-files -- %s)\" " path in
174+ Lwt_io. printf " [debug] %s\n " command |> ignore;
175+ Lwt_unix. system command
176+ >> = fun status ->
177+ match status with
178+ | Lwt_unix. WEXITED x -> Lwt_io. printf " [debug] %d\n " x |> ignore; return (x == 0 )
179+ | _ -> return false
180+
172181let process_input default_is_accept hunk_patch prev_start next_start editor path ~continue =
182+ file_in_git_repo path
183+ >> = fun file_gited ->
184+ let git_option =
185+ if file_gited then
186+ [ " \x1b [32m"
187+ ; " g = accept as git patch"
188+ ; " \x1b [0m"
189+ ; " , "
190+ ] else [] in
173191 let prompt =
174192 if default_is_accept then
175193 [ " Accept change ("
@@ -179,11 +197,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
179197 ; " \x1b [1m"
180198 ; " [default], "
181199 ; " \x1b [0m"
182- ; " \x1b [32m"
183- ; " g = accept as git patch"
184- ; " \x1b [0m"
185- ; " , "
186- ; " \x1b [31m"
200+ ] @ git_option @ [
201+ " \x1b [31m"
187202 ; " n = no"
188203 ; " \x1b [0m"
189204 ; " , "
@@ -203,11 +218,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
203218 ; " y = yes"
204219 ; " \x1b [0m"
205220 ; " , "
206- ; " \x1b [32m"
207- ; " g = accept as git patch"
208- ; " \x1b [0m"
209- ; " , "
210- ; " \x1b [31m"
221+ ] @ git_option @ [
222+ " \x1b [31m"
211223 ; " n = no"
212224 ; " \x1b [0m"
213225 ; " \x1b [1m"
@@ -234,7 +246,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
234246 | "y" -> apply_patch hunk_patch >> = handle_patch_errors >> = fun _ -> continue ()
235247 | "" when default_is_accept ->
236248 apply_patch hunk_patch >> = handle_patch_errors >> = fun _ -> continue ()
237- | "g" -> apply_patch_with_git hunk_patch >> = handle_patch_errors >> = fun _ -> continue ()
249+ | "g" when file_gited -> apply_patch_with_git hunk_patch >> = handle_patch_errors >> = fun _ -> continue ()
250+ | "g" when not file_gited -> Lwt_io. printl " File is not on repository. Try again." >> = try_again
238251 | "n" -> continue ()
239252 | "" when not default_is_accept -> continue ()
240253 | "e" ->
0 commit comments