|
| 1 | +use crate::domain::StashInfo; |
| 2 | +use crate::error::{AppError, Result}; |
| 3 | +use git2::{Repository, StashApplyOptions, StashFlags}; |
| 4 | + |
| 5 | +#[tauri::command] |
| 6 | +pub async fn get_stash_list(path: String) -> std::result::Result<Vec<StashInfo>, String> { |
| 7 | + get_stash_list_impl(&path).map_err(|e: AppError| e.to_string()) |
| 8 | +} |
| 9 | + |
| 10 | +fn get_stash_list_impl(path: &str) -> Result<Vec<StashInfo>> { |
| 11 | + let mut repo = Repository::open(path)?; |
| 12 | + let mut stashes = Vec::new(); |
| 13 | + |
| 14 | + repo.stash_foreach(|index, message, id| { |
| 15 | + stashes.push(StashInfo { |
| 16 | + index, |
| 17 | + message: message.to_string(), |
| 18 | + id: id.to_string(), |
| 19 | + }); |
| 20 | + true |
| 21 | + })?; |
| 22 | + |
| 23 | + Ok(stashes) |
| 24 | +} |
| 25 | + |
| 26 | +#[tauri::command] |
| 27 | +pub async fn stash_save( |
| 28 | + path: String, |
| 29 | + message: Option<String>, |
| 30 | + include_untracked: bool, |
| 31 | +) -> std::result::Result<(), String> { |
| 32 | + stash_save_impl(&path, message.as_deref(), include_untracked).map_err(|e: AppError| e.to_string()) |
| 33 | +} |
| 34 | + |
| 35 | +fn stash_save_impl(path: &str, message: Option<&str>, include_untracked: bool) -> Result<()> { |
| 36 | + let mut repo = Repository::open(path)?; |
| 37 | + let signature = repo.signature()?; |
| 38 | + |
| 39 | + let mut flags = StashFlags::DEFAULT; |
| 40 | + if include_untracked { |
| 41 | + flags |= StashFlags::INCLUDE_UNTRACKED; |
| 42 | + } |
| 43 | + |
| 44 | + repo.stash_save(&signature, message.unwrap_or(""), Some(flags))?; |
| 45 | + Ok(()) |
| 46 | +} |
| 47 | + |
| 48 | +#[tauri::command] |
| 49 | +pub async fn stash_apply(path: String, index: usize) -> std::result::Result<(), String> { |
| 50 | + stash_apply_impl(&path, index).map_err(|e: AppError| e.to_string()) |
| 51 | +} |
| 52 | + |
| 53 | +fn stash_apply_impl(path: &str, index: usize) -> Result<()> { |
| 54 | + let mut repo = Repository::open(path)?; |
| 55 | + let mut opts = StashApplyOptions::new(); |
| 56 | + repo.stash_apply(index, Some(&mut opts))?; |
| 57 | + Ok(()) |
| 58 | +} |
| 59 | + |
| 60 | +#[tauri::command] |
| 61 | +pub async fn stash_pop(path: String, index: usize) -> std::result::Result<(), String> { |
| 62 | + stash_pop_impl(&path, index).map_err(|e: AppError| e.to_string()) |
| 63 | +} |
| 64 | + |
| 65 | +fn stash_pop_impl(path: &str, index: usize) -> Result<()> { |
| 66 | + let mut repo = Repository::open(path)?; |
| 67 | + let mut opts = StashApplyOptions::new(); |
| 68 | + repo.stash_pop(index, Some(&mut opts))?; |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +#[tauri::command] |
| 73 | +pub async fn stash_drop(path: String, index: usize) -> std::result::Result<(), String> { |
| 74 | + stash_drop_impl(&path, index).map_err(|e: AppError| e.to_string()) |
| 75 | +} |
| 76 | + |
| 77 | +fn stash_drop_impl(path: &str, index: usize) -> Result<()> { |
| 78 | + let mut repo = Repository::open(path)?; |
| 79 | + repo.stash_drop(index)?; |
| 80 | + Ok(()) |
| 81 | +} |
0 commit comments