Skip to content

Commit 606a942

Browse files
committed
feature: accept hunks and stage selected changes on git...
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).
1 parent 3e4dc0c commit 606a942

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

lib/app/interactive/interactive.ml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ let handle_patch_errors = function
144144
Lwt_io.print message
145145
>>= fun _input -> Lwt_io.read_line Lwt_io.stdin >>= fun _input -> return `Ok
146146

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

162+
let apply_patch_with_git hunk_patch =
163+
apply_patch_with_cmd "git apply --index" hunk_patch
164+
165+
let apply_patch hunk_patch =
166+
apply_patch_with_cmd "patch -p 0" hunk_patch
167+
162168
let drop_into_editor editor path ~at_line =
163169
let command = Format.sprintf "%s +%d %s" editor at_line path in
164170
Lwt_unix.system command
@@ -173,6 +179,10 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
173179
; "\x1b[1m"
174180
; " [default], "
175181
; "\x1b[0m"
182+
; "\x1b[32m"
183+
; "g = accept as git patch"
184+
; "\x1b[0m"
185+
; ", "
176186
; "\x1b[31m"
177187
; "n = no"
178188
; "\x1b[0m"
@@ -193,6 +203,10 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
193203
; "y = yes"
194204
; "\x1b[0m"
195205
; ", "
206+
; "\x1b[32m"
207+
; "g = accept as git patch"
208+
; "\x1b[0m"
209+
; ", "
196210
; "\x1b[31m"
197211
; "n = no"
198212
; "\x1b[0m"
@@ -218,6 +232,9 @@ let process_input default_is_accept hunk_patch prev_start next_start editor path
218232
>>= fun input ->
219233
match input with
220234
| "y" -> apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
235+
| "" when default_is_accept ->
236+
apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
237+
| "g" -> apply_patch_with_git hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
221238
| "" when default_is_accept ->
222239
apply_patch hunk_patch >>= handle_patch_errors >>= fun _ -> continue ()
223240
| "n" -> continue ()

0 commit comments

Comments
 (0)