@@ -2,13 +2,13 @@ use super::{CommandBlocking, DrawableComponent, ScrollType};
2
2
use crate :: {
3
3
components:: { CommandInfo , Component } ,
4
4
keys,
5
- queue:: { Action , InternalEvent , Queue , ResetItem } ,
5
+ queue:: { Action , InternalEvent , NeedsUpdate , Queue , ResetItem } ,
6
6
strings,
7
7
ui:: { calc_scroll_top, style:: Theme } ,
8
8
} ;
9
- use asyncgit:: { hash, DiffLine , DiffLineType , FileDiff } ;
9
+ use asyncgit:: { hash, sync , DiffLine , DiffLineType , FileDiff , CWD } ;
10
10
use crossterm:: event:: Event ;
11
- use std:: { borrow:: Cow , cmp} ;
11
+ use std:: { borrow:: Cow , cmp, path :: Path } ;
12
12
use strings:: commands;
13
13
use tui:: {
14
14
backend:: Backend ,
@@ -271,19 +271,40 @@ impl DiffComponent {
271
271
false
272
272
}
273
273
274
- fn add_hunk ( & self ) -> Result < ( ) > {
274
+ fn unstage_hunk ( & mut self ) -> Result < ( ) > {
275
275
if let Some ( hunk) = self . selected_hunk {
276
276
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 ( ) ;
282
279
}
283
280
284
281
Ok ( ( ) )
285
282
}
286
283
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
+
287
308
fn reset_hunk ( & self ) -> Result < ( ) > {
288
309
if let Some ( hunk) = self . selected_hunk {
289
310
let hash = self . diff . hunks [ hunk] . header_hash ;
@@ -434,7 +455,11 @@ impl Component for DiffComponent {
434
455
Ok ( true )
435
456
}
436
457
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
+ }
438
463
Ok ( true )
439
464
}
440
465
keys:: DIFF_RESET_HUNK
0 commit comments