|
1 | 1 | //! Tests for some end-to-end logic about certain operations |
2 | | -use git2::{Error, Repository, RepositoryInitOptions}; |
| 2 | +use git2::{Error, Repository, RepositoryInitOptions, StashFlags}; |
3 | 3 |
|
4 | 4 | use libgit2_sys as raw; |
5 | 5 | use std::ffi::{CString, OsString}; |
| 6 | +use std::fs; |
6 | 7 | use std::ptr; |
7 | 8 |
|
8 | 9 | use tempfile::TempDir; |
@@ -134,3 +135,68 @@ fn non_utf8_branch() { |
134 | 135 | assert_eq!(Some(Ok("refs/heads/main")), names.next()); |
135 | 136 | assert_eq!(None, names.next()); |
136 | 137 | } |
| 138 | + |
| 139 | +#[test] |
| 140 | +fn stash_length() { |
| 141 | + // Test that reflog() and len() allow tracking the number of stashed entries |
| 142 | + let td = TempDir::new().unwrap(); |
| 143 | + let path = td.path(); |
| 144 | + |
| 145 | + let mut opts = RepositoryInitOptions::new(); |
| 146 | + opts.initial_head("main"); |
| 147 | + let mut repo = Repository::init_opts(path, &opts).unwrap(); |
| 148 | + |
| 149 | + let initial_reflog = repo.reflog("refs/stash").expect("Should work"); |
| 150 | + assert_eq!(0, initial_reflog.len()); |
| 151 | + |
| 152 | + let mut config = repo.config().unwrap(); |
| 153 | + config.set_str("user.name", "name").unwrap(); |
| 154 | + config.set_str("user.email", "email").unwrap(); |
| 155 | + let sig = repo.signature().unwrap(); |
| 156 | + |
| 157 | + // Need an initial commit before changes can be stashed |
| 158 | + let mut index = repo.index().unwrap(); |
| 159 | + let id = index.write_tree().unwrap(); |
| 160 | + { |
| 161 | + let tree = repo.find_tree(id).unwrap(); |
| 162 | + repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[]) |
| 163 | + .unwrap(); |
| 164 | + } |
| 165 | + |
| 166 | + fs::write(path.join("README.md"), "README content").unwrap(); |
| 167 | + repo.stash_save(&sig, "Stashed", Some(StashFlags::INCLUDE_UNTRACKED)) |
| 168 | + .unwrap(); |
| 169 | + |
| 170 | + // Existing reflog references are not updated |
| 171 | + assert_eq!(0, initial_reflog.len()); |
| 172 | + |
| 173 | + let after_stash1 = repo.reflog("refs/stash").expect("Should work"); |
| 174 | + assert_eq!(1, after_stash1.len()); |
| 175 | + |
| 176 | + fs::write(path.join("README.md2"), "README content").unwrap(); |
| 177 | + repo.stash_save(&sig, "Stashed2", Some(StashFlags::INCLUDE_UNTRACKED)) |
| 178 | + .unwrap(); |
| 179 | + |
| 180 | + assert_eq!(0, initial_reflog.len()); |
| 181 | + assert_eq!(1, after_stash1.len()); |
| 182 | + |
| 183 | + let after_stash2 = repo.reflog("refs/stash").expect("Should work"); |
| 184 | + assert_eq!(2, after_stash2.len()); |
| 185 | + |
| 186 | + repo.stash_drop(1).expect("Should succeed"); |
| 187 | + |
| 188 | + assert_eq!(0, initial_reflog.len()); |
| 189 | + assert_eq!(1, after_stash1.len()); |
| 190 | + assert_eq!(2, after_stash2.len()); |
| 191 | + let after_drop1 = repo.reflog("refs/stash").expect("Should work"); |
| 192 | + assert_eq!(1, after_drop1.len()); |
| 193 | + |
| 194 | + repo.stash_drop(0).expect("Should succeed"); |
| 195 | + |
| 196 | + assert_eq!(0, initial_reflog.len()); |
| 197 | + assert_eq!(1, after_stash1.len()); |
| 198 | + assert_eq!(2, after_stash2.len()); |
| 199 | + assert_eq!(1, after_drop1.len()); |
| 200 | + let after_drop2 = repo.reflog("refs/stash").expect("Should work"); |
| 201 | + assert_eq!(0, after_drop2.len()); |
| 202 | +} |
0 commit comments