Skip to content

Commit 4f29e70

Browse files
committed
feature: check if file is already on git repository...
when applying to the stage area, if the file is not already commited on the repository the patch will fail. we show the option only if the file is already commited.
1 parent c8e9929 commit 4f29e70

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

lib/app/interactive/interactive.ml

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let apply_patch_with_cmd cmd hunk_patch =
160160
>>= fun () -> process#close
161161

162162
let 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

165165
let 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+
172181
let 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

Comments
 (0)