use std::path::PathBuf;
use iced::Task;
use crate::macros::StringErr;
use crate::message::{Message, StagingPayload};
pub(crate) fn stage_file(path: PathBuf, file_path: String) -> Task<Message> {
staging_op!(path, |repo| gitkraft_core::features::staging::stage_file(
&repo, &file_path
))
}
pub(crate) fn unstage_file(path: PathBuf, file_path: String) -> Task<Message> {
staging_op!(path, |repo| gitkraft_core::features::staging::unstage_file(
&repo, &file_path
))
}
pub(crate) fn stage_all(path: PathBuf) -> Task<Message> {
staging_op!(path, |repo| gitkraft_core::features::staging::stage_all(
&repo
))
}
pub(crate) fn unstage_all(path: PathBuf) -> Task<Message> {
staging_op!(path, |repo| gitkraft_core::features::staging::unstage_all(
&repo
))
}
pub(crate) fn discard_file(path: PathBuf, file_path: String) -> Task<Message> {
staging_op!(path, |repo| {
gitkraft_core::features::staging::discard_file_changes(&repo, &file_path)
})
}
pub(crate) fn stage_files(path: PathBuf, file_paths: Vec<String>) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
for fp in &file_paths {
gitkraft_core::features::staging::stage_file(&repo, fp).str_err()?;
}
refresh_staging_state(&path)
})()
)
}
pub(crate) fn unstage_files(path: PathBuf, file_paths: Vec<String>) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
for fp in &file_paths {
gitkraft_core::features::staging::unstage_file(&repo, fp).str_err()?;
}
refresh_staging_state(&path)
})()
)
}
pub(crate) fn discard_all_selected(
path: PathBuf,
unstaged_paths: Vec<String>,
staged_paths: Vec<String>,
) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
for fp in &unstaged_paths {
gitkraft_core::features::staging::discard_file_changes(&repo, fp).str_err()?;
}
for fp in &staged_paths {
gitkraft_core::features::staging::unstage_file(&repo, fp).str_err()?;
gitkraft_core::features::staging::discard_file_changes(&repo, fp).str_err()?;
}
refresh_staging_state(&path)
})()
)
}
pub(crate) fn discard_staged_file(path: PathBuf, file_path: String) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
gitkraft_core::features::staging::unstage_file(&repo, &file_path).str_err()?;
gitkraft_core::features::staging::discard_file_changes(&repo, &file_path).str_err()?;
refresh_staging_state(&path)
})()
)
}
pub(crate) fn refresh_staging_state(path: &std::path::Path) -> Result<StagingPayload, String> {
let repo = open_repo!(path);
let unstaged = gitkraft_core::features::diff::get_working_dir_file_list(&repo).str_err()?;
let staged = gitkraft_core::features::diff::get_staged_file_list(&repo).str_err()?;
Ok(StagingPayload { unstaged, staged })
}
pub(crate) fn load_staging_file_diff(
path: PathBuf,
file_path: String,
staged: bool,
) -> Task<Message> {
git_task!(
Message::StagingFileDiffLoaded,
(|| {
let repo = open_repo!(&path);
if staged {
gitkraft_core::features::diff::get_staged_single_file_diff(&repo, &file_path)
.str_err()
} else {
gitkraft_core::features::diff::get_working_dir_single_file_diff(&repo, &file_path)
.str_err()
}
})()
)
}