Skip to content

Commit

Permalink
feature: accept hunks and stage selected changes on git...
Browse files Browse the repository at this point in the history
using the -review we can interactively select
which changes we will accept.

a new option is added to allow users to accept
the patch and also automatically stage the changes
(currently only git is supported).
  • Loading branch information
diasbruno committed Nov 17, 2022
1 parent 3e4dc0c commit 9aa0ceb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/app/interactive/interactive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ let handle_patch_errors = function
Lwt_io.print message
>>= fun _input -> Lwt_io.read_line Lwt_io.stdin >>= fun _input -> return `Ok

let apply_patch hunk_patch =
let cmd = Lwt_process.shell "patch -p 0" in
let apply_patch_with_cmd cmd hunk_patch =
let cmd = Lwt_process.shell cmd in
return (Lwt_process.open_process_full cmd)
>>= fun process ->
Lwt_io.write_line process#stdin hunk_patch
Expand All @@ -159,6 +159,12 @@ let apply_patch hunk_patch =
(if debug then Lwt_io.printf "[debug] %s,%s\n" stdout stderr else return ())
>>= fun () -> process#close

let apply_patch_with_git hunk_patch =
apply_patch_with_cmd "git apply --index" hunk_patch

let apply_patch hunk_patch =
apply_patch_with_cmd "patch -p 0" hunk_patch

let drop_into_editor editor path ~at_line =
let command = Format.sprintf "%s +%d %s" editor at_line path in
Lwt_unix.system command
Expand All @@ -173,6 +179,10 @@ 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"
; "n = no"
; "\x1b[0m"
Expand All @@ -193,6 +203,10 @@ 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"
; "n = no"
; "\x1b[0m"
Expand Down Expand Up @@ -220,6 +234,7 @@ 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 ()
| "n" -> continue ()
| "" when not default_is_accept -> continue ()
| "e" ->
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test('a', 1)

0 comments on commit 9aa0ceb

Please sign in to comment.