Skip to content

Commit

Permalink
feature: check if file is already on git repository...
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diasbruno committed Nov 18, 2022
1 parent c8e9929 commit 9e5c1d4
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/app/interactive/interactive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ let apply_patch_with_cmd cmd hunk_patch =
>>= fun () -> process#close

let apply_patch_with_git hunk_patch =
apply_patch_with_cmd "git apply --index" hunk_patch
apply_patch_with_cmd "git apply --index --intent-to-add" hunk_patch

let apply_patch hunk_patch =
apply_patch_with_cmd "patch -p 0" hunk_patch
Expand All @@ -169,7 +169,24 @@ let drop_into_editor editor path ~at_line =
let command = Format.sprintf "%s +%d %s" editor at_line path in
Lwt_unix.system command

let file_in_git_repo path =
let command = Format.sprintf "test ! -z \"$(git ls-files -- %s)\"" path in
Lwt_unix.system command
>>= fun status ->
match status with
| Lwt_unix.WEXITED x -> return (x == 0)
| _ -> return false

let process_input default_is_accept hunk_patch prev_start next_start editor path ~continue =
file_in_git_repo path
>>= fun file_gited ->
let git_option =
if file_gited then
[ "\x1b[32m"
; "g = accept as git patch"
; "\x1b[0m"
; ", "
] else [] in
let prompt =
if default_is_accept then
[ "Accept change ("
Expand All @@ -179,11 +196,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
; "\x1b[1m"
; " [default], "
; "\x1b[0m"
; "\x1b[32m"
; "g = accept as git patch"
; "\x1b[0m"
; ", "
; "\x1b[31m"
] @ git_option @ [
"\x1b[31m"
; "n = no"
; "\x1b[0m"
; ", "
Expand All @@ -203,11 +217,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
; "y = yes"
; "\x1b[0m"
; ", "
; "\x1b[32m"
; "g = accept as git patch"
; "\x1b[0m"
; ", "
; "\x1b[31m"
] @ git_option @ [
"\x1b[31m"
; "n = no"
; "\x1b[0m"
; "\x1b[1m"
Expand All @@ -234,7 +245,8 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
| "y" -> apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
| "" when default_is_accept ->
apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
| "g" -> apply_patch_with_git hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
| "g" when file_gited -> apply_patch_with_git hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
| "g" when not file_gited -> Lwt_io.printl "File is not on repository. Try again." >>= try_again
| "n" -> continue ()
| "" when not default_is_accept -> continue ()
| "e" ->
Expand Down

0 comments on commit 9e5c1d4

Please sign in to comment.