Skip to content

Commit 602257f

Browse files
author
Stephan Dilly
committed
simplification and fix staging hunks in untracked file
1 parent b3045b3 commit 602257f

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

src/app.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,21 +343,6 @@ impl App {
343343
self.reset.open(action)?;
344344
flags.insert(NeedsUpdate::COMMANDS);
345345
}
346-
InternalEvent::StageHunk(hash) => {
347-
if let Some((path, is_stage)) =
348-
self.status_tab.selected_path()
349-
{
350-
if is_stage {
351-
if sync::unstage_hunk(CWD, path, hash)? {
352-
flags.insert(NeedsUpdate::ALL);
353-
}
354-
} else if sync::stage_hunk(CWD, path, hash)
355-
.is_ok()
356-
{
357-
flags.insert(NeedsUpdate::ALL);
358-
}
359-
}
360-
}
361346
InternalEvent::ShowErrorMsg(msg) => {
362347
self.msg.show_msg(msg.as_str())?;
363348
flags

src/components/diff.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use super::{CommandBlocking, DrawableComponent, ScrollType};
22
use 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};
1010
use crossterm::event::Event;
11-
use std::{borrow::Cow, cmp};
11+
use std::{borrow::Cow, cmp, path::Path};
1212
use strings::commands;
1313
use 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

src/queue.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ pub enum InternalEvent {
3737
///
3838
ConfirmedAction(Action),
3939
///
40-
StageHunk(u64),
41-
///
4240
ShowErrorMsg(String),
4341
///
4442
Update(NeedsUpdate),

0 commit comments

Comments
 (0)