fix reset hunk in untracked file

This commit is contained in:
Stephan Dilly 2020-06-14 23:34:54 +02:00
parent dfe284a45b
commit d67a240bc7
3 changed files with 70 additions and 5 deletions

View File

@ -73,6 +73,8 @@ pub struct FileDiff {
pub hunks: Vec<Hunk>,
/// lines total summed up over hunks
pub lines: usize,
///
pub untracked: bool,
}
pub(crate) fn get_diff_raw<'a>(
@ -150,8 +152,6 @@ fn raw_diff_to_file_diff<'a>(
diff: &'a Diff,
work_dir: &Path,
) -> Result<FileDiff> {
// scope_time!("raw_diff_to_file_diff");
let mut res: FileDiff = FileDiff::default();
let mut current_lines = Vec::new();
let mut current_hunk: Option<HunkHeader> = None;
@ -251,6 +251,10 @@ fn raw_diff_to_file_diff<'a>(
adder(&current_hunk.unwrap(), &current_lines);
}
if new_file_diff {
res.untracked = true;
}
Ok(res)
}

View File

@ -32,7 +32,7 @@ pub fn stage_hunk(
Ok(())
}
///
/// this will fail for an all untracked file
pub fn reset_hunk(
repo_path: &str,
file_path: String,
@ -134,3 +134,45 @@ pub fn unstage_hunk(
Ok(count == 1)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
error::Result,
sync::{diff::get_diff, tests::repo_init_empty},
};
use std::{
fs::{self, File},
io::Write,
path::Path,
};
#[test]
fn reset_untracked_file_which_will_not_find_hunk() -> Result<()> {
let file_path = Path::new("foo/foo.txt");
let (_td, repo) = repo_init_empty()?;
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
let sub_path = root.join("foo/");
fs::create_dir_all(&sub_path)?;
File::create(&root.join(file_path))?.write_all(b"test")?;
let diff = get_diff(
sub_path.to_str().unwrap(),
String::from(file_path.to_str().unwrap()),
false,
)?;
assert!(reset_hunk(
repo_path,
String::from(file_path.to_str().unwrap()),
diff.hunks[0].header_hash,
)
.is_err());
Ok(())
}
}

View File

@ -2,7 +2,7 @@ use super::{CommandBlocking, DrawableComponent, ScrollType};
use crate::{
components::{CommandInfo, Component},
keys,
queue::{Action, InternalEvent, Queue},
queue::{Action, InternalEvent, Queue, ResetItem},
strings,
ui::{calc_scroll_top, style::Theme},
};
@ -303,6 +303,21 @@ impl DiffComponent {
Ok(())
}
fn reset_untracked(&self) -> Result<()> {
self.queue
.as_ref()
.expect("try using queue in immutable diff")
.borrow_mut()
.push_back(InternalEvent::ConfirmAction(Action::Reset(
ResetItem {
path: self.current.path.clone(),
is_folder: false,
},
)));
Ok(())
}
fn is_immutable(&self) -> bool {
self.queue.is_none()
}
@ -426,7 +441,11 @@ impl Component for DiffComponent {
if !self.is_immutable()
&& !self.is_stage() =>
{
self.reset_hunk()?;
if self.diff.untracked {
self.reset_untracked()?;
} else {
self.reset_hunk()?;
}
Ok(true)
}
_ => Ok(false),