use std::path::Path;
use crate::error::Result;
use crate::objects::ObjectId;
use crate::porcelain::hunk_select::HunkSelection;
use crate::porcelain::stash::{
apply_stash_entry, create_stash, drop_stash, list_stashes, pop_stash, push_stash,
StashApplyOptions as StashEntryApplyOptions, StashCreateOptions, StashEntry, StashMode,
};
use crate::repo::Repository;
#[derive(Debug, Clone)]
pub struct StashPushOptions {
pub message: Option<String>,
pub include_untracked: bool,
pub identity: String,
pub selection: Option<HunkSelection>,
pub pathspecs: Vec<String>,
pub dry_run: bool,
pub mode: StashMode,
}
#[derive(Debug, Clone)]
pub struct StashApplyOptions {
pub index: usize,
pub restore_index: bool,
pub identity: String,
}
#[derive(Debug, Copy, Clone)]
pub struct RepositoryStash<'repo> {
repo: &'repo Repository,
}
impl<'repo> RepositoryStash<'repo> {
pub(crate) fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
pub fn list(&self) -> Result<Vec<StashEntry>> {
list_stashes(self.repo)
}
pub fn push(&self, options: &StashPushOptions) -> Result<Option<ObjectId>> {
let inner = StashCreateOptions {
message: options.message.clone(),
include_untracked: options.include_untracked,
identity: options.identity.clone(),
selection: options.selection.clone(),
pathspecs: options.pathspecs.clone(),
dry_run: options.dry_run,
mode: options.mode,
};
push_stash(self.repo, &inner).map(|o| o.map(|r| r.oid))
}
pub fn create(&self, options: &StashPushOptions) -> Result<Option<ObjectId>> {
let inner = StashCreateOptions {
message: options.message.clone(),
include_untracked: options.include_untracked,
identity: options.identity.clone(),
selection: options.selection.clone(),
pathspecs: options.pathspecs.clone(),
dry_run: options.dry_run,
mode: options.mode,
};
create_stash(self.repo, &inner)
}
pub fn apply(&self, work_tree: &Path, options: &StashApplyOptions) -> Result<bool> {
Ok(apply_stash_entry(
self.repo,
work_tree,
options.index,
&StashEntryApplyOptions {
restore_index: options.restore_index,
},
)?
.has_conflicts())
}
pub fn pop(&self, work_tree: &Path, options: &StashApplyOptions) -> Result<bool> {
pop_stash(
self.repo,
work_tree,
options.index,
options.restore_index,
&options.identity,
)
}
pub fn drop(&self, index: usize, identity: &str) -> Result<()> {
drop_stash(self.repo, index, identity)
}
}