use std::path::PathBuf;
use iced::Task;
use crate::message::{Message, StagingPayload};
pub fn stage_file(path: PathBuf, file_path: String) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
gitkraft_core::features::staging::stage_file(&repo, &file_path)
.map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
pub fn unstage_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)
.map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
pub fn stage_all(path: PathBuf) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
gitkraft_core::features::staging::stage_all(&repo).map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
pub fn unstage_all(path: PathBuf) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
gitkraft_core::features::staging::unstage_all(&repo).map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
pub fn discard_file(path: PathBuf, file_path: String) -> Task<Message> {
git_task!(
Message::StagingUpdated,
(|| {
let repo = open_repo!(&path);
gitkraft_core::features::staging::discard_file_changes(&repo, &file_path)
.map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
pub 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)
.map_err(|e| e.to_string())?;
}
refresh_staging_state(&path)
})()
)
}
pub 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)
.map_err(|e| e.to_string())?;
}
refresh_staging_state(&path)
})()
)
}
pub fn discard_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::discard_file_changes(&repo, fp)
.map_err(|e| e.to_string())?;
}
refresh_staging_state(&path)
})()
)
}
pub 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)
.map_err(|e| e.to_string())?;
}
for fp in &staged_paths {
gitkraft_core::features::staging::unstage_file(&repo, fp)
.map_err(|e| e.to_string())?;
gitkraft_core::features::staging::discard_file_changes(&repo, fp)
.map_err(|e| e.to_string())?;
}
refresh_staging_state(&path)
})()
)
}
pub 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)
.map_err(|e| e.to_string())?;
gitkraft_core::features::staging::discard_file_changes(&repo, &file_path)
.map_err(|e| e.to_string())?;
refresh_staging_state(&path)
})()
)
}
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_diff(&repo).map_err(|e| e.to_string())?;
let staged =
gitkraft_core::features::diff::get_staged_diff(&repo).map_err(|e| e.to_string())?;
Ok(StagingPayload { unstaged, staged })
}