@@ -2,13 +2,13 @@ use super::{CommandBlocking, DrawableComponent, ScrollType};
22use crate :: {
33 components:: { CommandInfo , Component } ,
44 keys,
5- queue:: { Action , InternalEvent , Queue , ResetItem } ,
5+ queue:: { Action , InternalEvent , NeedsUpdate , Queue , ResetItem } ,
66 strings,
77 ui:: { calc_scroll_top, style:: Theme } ,
88} ;
9- use asyncgit:: { hash, DiffLine , DiffLineType , FileDiff } ;
9+ use asyncgit:: { hash, sync , DiffLine , DiffLineType , FileDiff , CWD } ;
1010use crossterm:: event:: Event ;
11- use std:: { borrow:: Cow , cmp} ;
11+ use std:: { borrow:: Cow , cmp, path :: Path } ;
1212use strings:: commands;
1313use tui:: {
1414 backend:: Backend ,
@@ -271,19 +271,40 @@ impl DiffComponent {
271271 false
272272 }
273273
274- fn add_hunk ( & self ) -> Result < ( ) > {
274+ fn unstage_hunk ( & mut self ) -> Result < ( ) > {
275275 if let Some ( hunk) = self . selected_hunk {
276276 let hash = self . diff . hunks [ hunk] . header_hash ;
277- self . queue
278- . as_ref ( )
279- . expect ( "try using queue in immutable diff" )
280- . borrow_mut ( )
281- . push_back ( InternalEvent :: StageHunk ( hash) ) ;
277+ sync:: unstage_hunk ( CWD , self . current . path . clone ( ) , hash) ?;
278+ self . queue_update ( ) ;
282279 }
283280
284281 Ok ( ( ) )
285282 }
286283
284+ fn stage_hunk ( & mut self ) -> Result < ( ) > {
285+ if let Some ( hunk) = self . selected_hunk {
286+ let path = self . current . path . clone ( ) ;
287+ if self . diff . untracked {
288+ sync:: stage_add_file ( CWD , Path :: new ( & path) ) ?;
289+ } else {
290+ let hash = self . diff . hunks [ hunk] . header_hash ;
291+ sync:: stage_hunk ( CWD , path, hash) ?;
292+ }
293+
294+ self . queue_update ( ) ;
295+ }
296+
297+ Ok ( ( ) )
298+ }
299+
300+ fn queue_update ( & mut self ) {
301+ self . queue
302+ . as_ref ( )
303+ . expect ( "try using queue in immutable diff" )
304+ . borrow_mut ( )
305+ . push_back ( InternalEvent :: Update ( NeedsUpdate :: ALL ) ) ;
306+ }
307+
287308 fn reset_hunk ( & self ) -> Result < ( ) > {
288309 if let Some ( hunk) = self . selected_hunk {
289310 let hash = self . diff . hunks [ hunk] . header_hash ;
@@ -434,7 +455,11 @@ impl Component for DiffComponent {
434455 Ok ( true )
435456 }
436457 keys:: ENTER if !self . is_immutable ( ) => {
437- self . add_hunk ( ) ?;
458+ if self . current . is_stage {
459+ self . unstage_hunk ( ) ?;
460+ } else {
461+ self . stage_hunk ( ) ?;
462+ }
438463 Ok ( true )
439464 }
440465 keys:: DIFF_RESET_HUNK
0 commit comments