diff options
Diffstat (limited to 'asyncgit/src/sync/stash.rs')
-rw-r--r-- | asyncgit/src/sync/stash.rs | 474 |
1 files changed, 237 insertions, 237 deletions
diff --git a/asyncgit/src/sync/stash.rs b/asyncgit/src/sync/stash.rs index e19079d9..f98679a2 100644 --- a/asyncgit/src/sync/stash.rs +++ b/asyncgit/src/sync/stash.rs @@ -1,370 +1,370 @@ use super::{utils::repo, CommitId}; use crate::error::{Error, Result}; use git2::{ - build::CheckoutBuilder, Oid, Repository, StashApplyOptions, - StashFlags, + build::CheckoutBuilder, Oid, Repository, StashApplyOptions, + StashFlags, }; use scopetime::scope_time; /// pub fn get_stashes(repo_path: &str) -> Result<Vec<CommitId>> { - scope_time!("get_stashes"); + scope_time!("get_stashes"); - let mut repo = repo(repo_path)?; + let mut repo = repo(repo_path)?; - let mut list = Vec::new(); + let mut list = Vec::new(); - repo.stash_foreach(|_index, _msg, id| { - list.push((*id).into()); - true - })?; + repo.stash_foreach(|_index, _msg, id| { + list.push((*id).into()); + true + })?; - Ok(list) + Ok(list) } /// checks whether a given commit is a stash commit. pub fn is_stash_commit( - repo_path: &str, - id: &CommitId, + repo_path: &str, + id: &CommitId, ) -> Result<bool> { - let stashes = get_stashes(repo_path)?; - Ok(stashes.contains(id)) + let stashes = get_stashes(repo_path)?; + Ok(stashes.contains(id)) } /// pub fn stash_drop(repo_path: &str, stash_id: CommitId) -> Result<()> { - scope_time!("stash_drop"); + scope_time!("stash_drop"); - let mut repo = repo(repo_path)?; + let mut repo = repo(repo_path)?; - let index = get_stash_index(&mut repo, stash_id.into())?; + let index = get_stash_index(&mut repo, stash_id.into())?; - repo.stash_drop(index)?; + repo.stash_drop(index)?; - Ok(()) + Ok(()) } /// pub fn stash_pop(repo_path: &str, stash_id: CommitId) -> Result<()> { - scope_time!("stash_pop"); + scope_time!("stash_pop"); - let mut repo = repo(repo_path)?; + let mut repo = repo(repo_path)?; - let index = get_stash_index(&mut repo, stash_id.into())?; + let index = get_stash_index(&mut repo, stash_id.into())?; - repo.stash_pop(index, None)?; + repo.stash_pop(index, None)?; - Ok(()) + Ok(()) } /// pub fn stash_apply( - repo_path: &str, - stash_id: CommitId, - allow_conflicts: bool, + repo_path: &str, + stash_id: CommitId, + allow_conflicts: bool, ) -> Result<()> { - scope_time!("stash_apply"); + scope_time!("stash_apply"); - let mut repo = repo(repo_path)?; + let mut repo = repo(repo_path)?; - let index = get_stash_index(&mut repo, stash_id.get_oid())?; + let index = get_stash_index(&mut repo, stash_id.get_oid())?; - let mut checkout = CheckoutBuilder::new(); - checkout.allow_conflicts(allow_conflicts); + let mut checkout = CheckoutBuilder::new(); + checkout.allow_conflicts(allow_conflicts); - let mut opt = StashApplyOptions::default(); - opt.checkout_options(checkout); - repo.stash_apply(index, Some(&mut opt))?; + let mut opt = StashApplyOptions::default(); + opt.checkout_options(checkout); + repo.stash_apply(index, Some(&mut opt))?; - Ok(()) + Ok(()) } fn get_stash_index( - repo: &mut Repository, - stash_id: Oid, + repo: &mut Repository, + stash_id: Oid, ) -> Result<usize> { - let mut idx = None; - - repo.stash_foreach(|index, _msg, id| { - if *id == stash_id { - idx = Some(index); - false - } else { - true - } - })?; - - idx.ok_or_else(|| { - Error::Generic("stash commit not found".to_string()) - }) + let mut idx = None; + + repo.stash_foreach(|index, _msg, id| { + if *id == stash_id { + idx = Some(index); + false + } else { + true + } + })?; + + idx.ok_or_else(|| { + Error::Generic("stash commit not found".to_string()) + }) } /// pub fn stash_save( - repo_path: &str, - message: Option<&str>, - include_untracked: bool, - keep_index: bool, + repo_path: &str, + message: Option<&str>, + include_untracked: bool, + keep_index: bool, ) -> Result<CommitId> { - scope_time!("stash_save"); + scope_time!("stash_save"); - let mut repo = repo(repo_path)?; + let mut repo = repo(repo_path)?; - let sig = repo.signature()?; + let sig = repo.signature()?; - let mut options = StashFlags::DEFAULT; + let mut options = StashFlags::DEFAULT; - if include_untracked { - options.insert(StashFlags::INCLUDE_UNTRACKED); - } - if keep_index { - options.insert(StashFlags::KEEP_INDEX); - } + if include_untracked { + options.insert(StashFlags::INCLUDE_UNTRACKED); + } + if keep_index { + options.insert(StashFlags::KEEP_INDEX); + } - let id = repo.stash_save2(&sig, message, Some(options))?; + let id = repo.stash_save2(&sig, message, Some(options))?; - Ok(CommitId::new(id)) + Ok(CommitId::new(id)) } #[cfg(test)] mod tests { - use super::*; - use crate::sync::{ - commit, get_commit_files, get_commits_info, stage_add_file, - tests::{ - debug_cmd_print, get_statuses, repo_init, - write_commit_file, - }, - utils::{repo_read_file, repo_write_file}, - }; - use std::{fs::File, io::Write, path::Path}; + use super::*; + use crate::sync::{ + commit, get_commit_files, get_commits_info, stage_add_file, + tests::{ + debug_cmd_print, get_statuses, repo_init, + write_commit_file, + }, + utils::{repo_read_file, repo_write_file}, + }; + use std::{fs::File, io::Write, path::Path}; - #[test] - fn test_smoke() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_smoke() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - assert_eq!( - stash_save(repo_path, None, true, false).is_ok(), - false - ); + assert_eq!( + stash_save(repo_path, None, true, false).is_ok(), + false + ); - assert_eq!(get_stashes(repo_path).unwrap().is_empty(), true); - } + assert_eq!(get_stashes(repo_path).unwrap().is_empty(), true); + } - #[test] - fn test_stashing() -> Result<()> { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stashing() -> Result<()> { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - File::create(&root.join("foo.txt"))? - .write_all(b"test\nfoo")?; + File::create(&root.join("foo.txt"))? + .write_all(b"test\nfoo")?; - assert_eq!(get_statuses(repo_path), (1, 0)); + assert_eq!(get_statuses(repo_path), (1, 0)); - stash_save(repo_path, None, true, false)?; + stash_save(repo_path, None, true, false)?; - assert_eq!(get_statuses(repo_path), (0, 0)); + assert_eq!(get_statuses(repo_path), (0, 0)); - Ok(()) - } + Ok(()) + } - #[test] - fn test_stashes() -> Result<()> { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stashes() -> Result<()> { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - File::create(&root.join("foo.txt"))? - .write_all(b"test\nfoo")?; + File::create(&root.join("foo.txt"))? + .write_all(b"test\nfoo")?; - stash_save(repo_path, Some("foo"), true, false)?; + stash_save(repo_path, Some("foo"), true, false)?; - let res = get_stashes(repo_path)?; + let res = get_stashes(repo_path)?; - assert_eq!(res.len(), 1); + assert_eq!(res.len(), 1); - let infos = - get_commits_info(repo_path, &[res[0]], 100).unwrap(); + let infos = + get_commits_info(repo_path, &[res[0]], 100).unwrap(); - assert_eq!(infos[0].message, "On master: foo"); + assert_eq!(infos[0].message, "On master: foo"); - Ok(()) - } + Ok(()) + } - #[test] - fn test_stash_nothing_untracked() -> Result<()> { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_nothing_untracked() -> Result<()> { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - File::create(&root.join("foo.txt"))? - .write_all(b"test\nfoo")?; + File::create(&root.join("foo.txt"))? + .write_all(b"test\nfoo")?; - assert!( - stash_save(repo_path, Some("foo"), false, false).is_err() - ); + assert!( + stash_save(repo_path, Some("foo"), false, false).is_err() + ); - Ok(()) - } + Ok(()) + } - #[test] - fn test_stash_without_2nd_parent() -> Result<()> { - let file_path1 = Path::new("file1.txt"); - let (_td, repo) = repo_init()?; - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_without_2nd_parent() -> Result<()> { + let file_path1 = Path::new("file1.txt"); + let (_td, repo) = repo_init()?; + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - File::create(&root.join(file_path1))?.write_all(b"test")?; - stage_add_file(repo_path, file_path1)?; - commit(repo_path, "c1")?; + File::create(&root.join(file_path1))?.write_all(b"test")?; + stage_add_file(repo_path, file_path1)?; + commit(repo_path, "c1")?; - File::create(&root.join(file_path1))? - .write_all(b"modified")?; + File::create(&root.join(file_path1))? + .write_all(b"modified")?; - //NOTE: apparently `libgit2` works differently to git stash in - //always creating the third parent for untracked files while the - //cli skips that step when no new files exist - debug_cmd_print(repo_path, "git stash"); + //NOTE: apparently `libgit2` works differently to git stash in + //always creating the third parent for untracked files while the + //cli skips that step when no new files exist + debug_cmd_print(repo_path, "git stash"); - let stash = get_stashes(repo_path)?[0]; + let stash = get_stashes(repo_path)?[0]; - let diff = get_commit_files(repo_path, stash)?; + let diff = get_commit_files(repo_path, stash)?; - assert_eq!(diff.len(), 1); + assert_eq!(diff.len(), 1); - Ok(()) - } + Ok(()) + } - #[test] - fn test_stash_apply_conflict() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_apply_conflict() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - repo_write_file(&repo, "test.txt", "test").unwrap(); + repo_write_file(&repo, "test.txt", "test").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - repo_write_file(&repo, "test.txt", "foo").unwrap(); + repo_write_file(&repo, "test.txt", "foo").unwrap(); - let res = stash_apply(repo_path, id, false); + let res = stash_apply(repo_path, id, false); - assert!(res.is_err()); - } + assert!(res.is_err()); + } - #[test] - fn test_stash_apply_conflict2() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_apply_conflict2() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - write_commit_file(&repo, "test.txt", "test", "c1"); + write_commit_file(&repo, "test.txt", "test", "c1"); - repo_write_file(&repo, "test.txt", "test2").unwrap(); + repo_write_file(&repo, "test.txt", "test2").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - repo_write_file(&repo, "test.txt", "test3").unwrap(); + repo_write_file(&repo, "test.txt", "test3").unwrap(); - let res = stash_apply(repo_path, id, false); + let res = stash_apply(repo_path, id, false); - assert!(res.is_err()); - } + assert!(res.is_err()); + } - #[test] - fn test_stash_apply_creating_conflict() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_apply_creating_conflict() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - write_commit_file(&repo, "test.txt", "test", "c1"); + write_commit_file(&repo, "test.txt", "test", "c1"); - repo_write_file(&repo, "test.txt", "test2").unwrap(); + repo_write_file(&repo, "test.txt", "test2").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - repo_write_file(&repo, "test.txt", "test3").unwrap(); + repo_write_file(&repo, "test.txt", "test3").unwrap(); - let res = stash_apply(repo_path, id, false); + let res = stash_apply(repo_path, id, false); - assert!(res.is_err()); + assert!(res.is_err()); - let res = stash_apply(repo_path, id, true); + let res = stash_apply(repo_path, id, true); - assert!(res.is_ok()); - } + assert!(res.is_ok()); + } - #[test] - fn test_stash_pop_no_conflict() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_pop_no_conflict() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - write_commit_file(&repo, "test.txt", "test", "c1"); + write_commit_file(&repo, "test.txt", "test", "c1"); - repo_write_file(&repo, "test.txt", "test2").unwrap(); + repo_write_file(&repo, "test.txt", "test2").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - let res = stash_pop(repo_path, id); + let res = stash_pop(repo_path, id); - assert!(res.is_ok()); - assert_eq!( - repo_read_file(&repo, "test.txt").unwrap(), - "test2" - ); - } + assert!(res.is_ok()); + assert_eq!( + repo_read_file(&repo, "test.txt").unwrap(), + "test2" + ); + } - #[test] - fn test_stash_pop_conflict() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_pop_conflict() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - repo_write_file(&repo, "test.txt", "test").unwrap(); + repo_write_file(&repo, "test.txt", "test").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - repo_write_file(&repo, "test.txt", "test2").unwrap(); + repo_write_file(&repo, "test.txt", "test2").unwrap(); - let res = stash_pop(repo_path, id); + let res = stash_pop(repo_path, id); - assert!(res.is_err()); - assert_eq!( - repo_read_file(&repo, "test.txt").unwrap(), - "test2" - ); - } + assert!(res.is_err()); + assert_eq!( + repo_read_file(&repo, "test.txt").unwrap(), + "test2" + ); + } - #[test] - fn test_stash_pop_conflict_after_commit() { - let (_td, repo) = repo_init().unwrap(); - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); + #[test] + fn test_stash_pop_conflict_after_commit() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); - write_commit_file(&repo, "test.txt", "test", "c1"); + write_commit_file(&repo, "test.txt", "test", "c1"); - repo_write_file(&repo, "test.txt", "test2").unwrap(); + repo_write_file(&repo, "test.txt", "test2").unwrap(); - let id = - stash_save(repo_path, Some("foo"), true, false).unwrap(); + let id = + stash_save(repo_path, Some("foo"), true, false).unwrap(); - repo_write_file(&repo, "test.txt", "test3").unwrap(); + repo_write_file(&repo, "test.txt", "test3").unwrap(); - let res = stash_pop(repo_path, id); + let res = stash_pop(repo_path, id); - assert!(res.is_err()); - assert_eq!( - repo_read_file(&repo, "test.txt").unwrap(), - "test3" - ); - } + assert!(res.is_err()); + assert_eq!( + repo_read_file(&repo, "test.txt").unwrap(), + "test3" + ); + } } |