Skip to content

Commit 28e0a69

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 28e0a69

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

lib/app/interactive/interactive.ml

Lines changed: 26 additions & 13 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,24 @@ 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_unix.system command
175+
>>= fun status ->
176+
match status with
177+
| Lwt_unix.WEXITED x -> return (x == 0)
178+
| _ -> return false
179+
172180
let process_input default_is_accept hunk_patch prev_start next_start editor path ~continue =
181+
file_in_git_repo path
182+
>>= fun file_gited ->
183+
let git_option =
184+
if file_gited then
185+
[ "\x1b[32m"
186+
; "g = accept as git patch"
187+
; "\x1b[0m"
188+
; ", "
189+
] else [] in
173190
let prompt =
174191
if default_is_accept then
175192
[ "Accept change ("
@@ -179,11 +196,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
179196
; "\x1b[1m"
180197
; " [default], "
181198
; "\x1b[0m"
182-
; "\x1b[32m"
183-
; "g = accept as git patch"
184-
; "\x1b[0m"
185-
; ", "
186-
; "\x1b[31m"
199+
] @ git_option @ [
200+
"\x1b[31m"
187201
; "n = no"
188202
; "\x1b[0m"
189203
; ", "
@@ -203,11 +217,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
203217
; "y = yes"
204218
; "\x1b[0m"
205219
; ", "
206-
; "\x1b[32m"
207-
; "g = accept as git patch"
208-
; "\x1b[0m"
209-
; ", "
210-
; "\x1b[31m"
220+
] @ git_option @ [
221+
"\x1b[31m"
211222
; "n = no"
212223
; "\x1b[0m"
213224
; "\x1b[1m"
@@ -224,6 +235,7 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
224235
; "q = quit)?"
225236
]
226237
in
238+
let no_command = fun () -> Lwt_io.printl "Uh, I don't know that one. Try again." in
227239
let prompt = String.concat prompt in
228240
Lwt_io.printl prompt
229241
>>= fun () ->
@@ -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 -> no_command () >>= try_again
238251
| "n" -> continue ()
239252
| "" when not default_is_accept -> continue ()
240253
| "e" ->
@@ -249,7 +262,7 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
249262
>>= handle_editor_errors
250263
>>= fun _ -> continue ()
251264
| "q" -> raise Sys.Break
252-
| _ -> Lwt_io.printl "Uh, I don't know that one. Try again." >>= try_again
265+
| _ -> no_command () >>= try_again
253266
in
254267
try_again ()
255268

0 commit comments

Comments
 (0)