Skip to content

Commit 9e5c1d4

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 9e5c1d4

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/app/interactive/interactive.ml

Lines changed: 24 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,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"
@@ -234,7 +245,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
234245
| "y" -> apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
235246
| "" when default_is_accept ->
236247
apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
237-
| "g" -> apply_patch_with_git hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
248+
| "g" when file_gited -> apply_patch_with_git hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
249+
| "g" when not file_gited -> Lwt_io.printl "File is not on repository. Try again." >>= try_again
238250
| "n" -> continue ()
239251
| "" when not default_is_accept -> continue ()
240252
| "e" ->

0 commit comments

Comments
 (0)