| 1 |
//! Tests for some end-to-end logic about certain operations |
| 2 |
use git2::{ |
| 3 |
Error, ReferenceType, Repository, RepositoryInitOptions, StashFlags, Status, StatusOptions, |
| 4 |
}; |
| 5 |
|
| 6 |
use libgit2_sys as raw; |
| 7 |
use std::ffi::{CString, OsString}; |
| 8 |
use std::fs; |
| 9 |
use std::path::Path; |
| 10 |
use std::ptr; |
| 11 |
|
| 12 |
use tempfile::TempDir; |
| 13 |
|
| 14 |
// Skip on MacOS, where git cannot even create a branch with a non-UTF8 name |
| 15 |
// Same on Windows |
| 16 |
|
| 17 |
#[test] |
| 18 |
#[cfg_attr(any(windows, target_os = "macos"), ignore)] |
| 19 |
fn non_utf8_branch() { |
| 20 |
let td = TempDir::new().unwrap(); |
| 21 |
let path = td.path(); |
| 22 |
{ |
| 23 |
let mut opts = RepositoryInitOptions::new(); |
| 24 |
opts.initial_head("main"); |
| 25 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 26 |
|
| 27 |
let mut config = repo.config().unwrap(); |
| 28 |
config.set_str("user.name", "name").unwrap(); |
| 29 |
config.set_str("user.email", "email").unwrap(); |
| 30 |
let mut index = repo.index().unwrap(); |
| 31 |
let id = index.write_tree().unwrap(); |
| 32 |
|
| 33 |
let tree = repo.find_tree(id).unwrap(); |
| 34 |
let sig = repo.signature().unwrap(); |
| 35 |
repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[]) |
| 36 |
.unwrap(); |
| 37 |
} |
| 38 |
|
| 39 |
// Create a branch with a non-UTF8 name |
| 40 |
// Since that cannot be done via the git2-rs interface, call the raw |
| 41 |
// underlying binding |
| 42 |
// For that we also have to recreate the underlying `git_repository` pointer |
| 43 |
{ |
| 44 |
macro_rules! check_result { |
| 45 |
($result:ident) => { |
| 46 |
if $result != 0 { |
| 47 |
let e = Error::last_error($result); |
| 48 |
// Show the error details in the assertion failure |
| 49 |
assert_eq!("", format!("{:?}", e)); |
| 50 |
} |
| 51 |
}; |
| 52 |
} |
| 53 |
// based on util.rs IntoCString for OsString |
| 54 |
// Need cfg() guards since the file is compiled on Windows even when |
| 55 |
// the test is skipped |
| 56 |
#[cfg(unix)] |
| 57 |
fn ostr_to_cstr(s: OsString) -> CString { |
| 58 |
use std::ffi::OsStr; |
| 59 |
use std::os::unix::prelude::*; |
| 60 |
let s: &OsStr = s.as_ref(); |
| 61 |
CString::new(s.as_bytes()).unwrap() |
| 62 |
} |
| 63 |
#[cfg(windows)] |
| 64 |
fn ostr_to_cstr(s: OsString) -> CString { |
| 65 |
panic!("Test is skipped on Windows"); |
| 66 |
} |
| 67 |
|
| 68 |
let path = ostr_to_cstr(path.into()); |
| 69 |
let mut repo = ptr::null_mut(); |
| 70 |
unsafe { |
| 71 |
let result = raw::git_repository_open(&mut repo, path.as_ptr()); |
| 72 |
assert_eq!(0, result); |
| 73 |
} |
| 74 |
// We now have a git_repository pointer in `repo` |
| 75 |
// Get a reference to the latest commit |
| 76 |
|
| 77 |
// Repo::head() |
| 78 |
let mut head_reference = ptr::null_mut(); |
| 79 |
unsafe { |
| 80 |
let result = raw::git_repository_head(&mut head_reference, repo); |
| 81 |
check_result!(result); |
| 82 |
} |
| 83 |
|
| 84 |
// Reference::peel() |
| 85 |
let mut peeled = ptr::null_mut(); |
| 86 |
unsafe { |
| 87 |
let result = |
| 88 |
raw::git_reference_peel(&mut peeled, head_reference, raw::GIT_OBJECT_COMMIT); |
| 89 |
check_result!(result); |
| 90 |
assert_eq!(raw::GIT_OBJECT_COMMIT, raw::git_object_type(&*peeled)); |
| 91 |
} |
| 92 |
|
| 93 |
// Object::cast_or_panic(), already confirmed to be a commit |
| 94 |
let as_commit = peeled as *mut raw::git_commit; |
| 95 |
|
| 96 |
let branch_name = CString::new(vec![b'f', 0xff, 0xC0, b'o', b'o']).unwrap(); |
| 97 |
let mut branch_reference = ptr::null_mut(); |
| 98 |
unsafe { |
| 99 |
let result = raw::git_branch_create( |
| 100 |
&mut branch_reference, |
| 101 |
repo, |
| 102 |
branch_name.as_ptr(), |
| 103 |
as_commit, |
| 104 |
0, |
| 105 |
); |
| 106 |
check_result!(result); |
| 107 |
} |
| 108 |
|
| 109 |
unsafe { |
| 110 |
// impl Drop for Reference |
| 111 |
raw::git_reference_free(branch_reference); |
| 112 |
|
| 113 |
// impl Drop for Commit |
| 114 |
raw::git_commit_free(as_commit); |
| 115 |
|
| 116 |
// impl Drop for Object |
| 117 |
raw::git_object_free(peeled); |
| 118 |
|
| 119 |
// impl Drop for Reference |
| 120 |
raw::git_reference_free(head_reference); |
| 121 |
|
| 122 |
// impl Drop for Repository |
| 123 |
raw::git_repository_free(repo); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Now, get the repo again |
| 128 |
let repo = Repository::open(path).expect("created above"); |
| 129 |
let mut refs = repo.references().expect("references"); |
| 130 |
let mut names = refs.names(); |
| 131 |
|
| 132 |
assert_eq!( |
| 133 |
Some(Err(Error::from_str( |
| 134 |
"invalid utf-8 sequence of 1 bytes from index 12" |
| 135 |
))), |
| 136 |
names.next() |
| 137 |
); |
| 138 |
assert_eq!(Some(Ok("refs/heads/main")), names.next()); |
| 139 |
assert_eq!(None, names.next()); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn stash_length() { |
| 144 |
// Test that reflog() and len() allow tracking the number of stashed entries |
| 145 |
let td = TempDir::new().unwrap(); |
| 146 |
let path = td.path(); |
| 147 |
|
| 148 |
let mut opts = RepositoryInitOptions::new(); |
| 149 |
opts.initial_head("main"); |
| 150 |
let mut repo = Repository::init_opts(path, &opts).unwrap(); |
| 151 |
|
| 152 |
let initial_reflog = repo.reflog("refs/stash").expect("Should work"); |
| 153 |
assert_eq!(0, initial_reflog.len()); |
| 154 |
|
| 155 |
let mut config = repo.config().unwrap(); |
| 156 |
config.set_str("user.name", "name").unwrap(); |
| 157 |
config.set_str("user.email", "email").unwrap(); |
| 158 |
let sig = repo.signature().unwrap(); |
| 159 |
|
| 160 |
// Need an initial commit before changes can be stashed |
| 161 |
let mut index = repo.index().unwrap(); |
| 162 |
let id = index.write_tree().unwrap(); |
| 163 |
{ |
| 164 |
let tree = repo.find_tree(id).unwrap(); |
| 165 |
repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[]) |
| 166 |
.unwrap(); |
| 167 |
} |
| 168 |
|
| 169 |
fs::write(path.join("README.md"), "README content").unwrap(); |
| 170 |
repo.stash_save(&sig, "Stashed", Some(StashFlags::INCLUDE_UNTRACKED)) |
| 171 |
.unwrap(); |
| 172 |
|
| 173 |
// Existing reflog references are not updated |
| 174 |
assert_eq!(0, initial_reflog.len()); |
| 175 |
|
| 176 |
let after_stash1 = repo.reflog("refs/stash").expect("Should work"); |
| 177 |
assert_eq!(1, after_stash1.len()); |
| 178 |
|
| 179 |
fs::write(path.join("README.md2"), "README content").unwrap(); |
| 180 |
repo.stash_save(&sig, "Stashed2", Some(StashFlags::INCLUDE_UNTRACKED)) |
| 181 |
.unwrap(); |
| 182 |
|
| 183 |
assert_eq!(0, initial_reflog.len()); |
| 184 |
assert_eq!(1, after_stash1.len()); |
| 185 |
|
| 186 |
let after_stash2 = repo.reflog("refs/stash").expect("Should work"); |
| 187 |
assert_eq!(2, after_stash2.len()); |
| 188 |
|
| 189 |
repo.stash_drop(1).expect("Should succeed"); |
| 190 |
|
| 191 |
assert_eq!(0, initial_reflog.len()); |
| 192 |
assert_eq!(1, after_stash1.len()); |
| 193 |
assert_eq!(2, after_stash2.len()); |
| 194 |
let after_drop1 = repo.reflog("refs/stash").expect("Should work"); |
| 195 |
assert_eq!(1, after_drop1.len()); |
| 196 |
|
| 197 |
repo.stash_drop(0).expect("Should succeed"); |
| 198 |
|
| 199 |
assert_eq!(0, initial_reflog.len()); |
| 200 |
assert_eq!(1, after_stash1.len()); |
| 201 |
assert_eq!(2, after_stash2.len()); |
| 202 |
assert_eq!(1, after_drop1.len()); |
| 203 |
let after_drop2 = repo.reflog("refs/stash").expect("Should work"); |
| 204 |
assert_eq!(0, after_drop2.len()); |
| 205 |
} |
| 206 |
|
| 207 |
#[test] |
| 208 |
fn branch_name_on_init() { |
| 209 |
// Confirm that the branch name is available via find_reference() even when |
| 210 |
// no commits are made yet and the branch doesn't exist |
| 211 |
// Test with "main" |
| 212 |
{ |
| 213 |
let td = TempDir::new().unwrap(); |
| 214 |
let path = td.path(); |
| 215 |
|
| 216 |
let mut opts = RepositoryInitOptions::new(); |
| 217 |
opts.initial_head("main"); |
| 218 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 219 |
|
| 220 |
let head_ref = repo.find_reference("HEAD").unwrap(); |
| 221 |
assert_eq!(Some(ReferenceType::Symbolic), head_ref.kind()); |
| 222 |
assert_eq!(Ok("HEAD"), head_ref.name()); |
| 223 |
|
| 224 |
let target = head_ref.symbolic_target(); |
| 225 |
assert_eq!(Ok(Some("refs/heads/main")), target); |
| 226 |
} |
| 227 |
// Test with "somerandombranchnamehere" |
| 228 |
{ |
| 229 |
let td = TempDir::new().unwrap(); |
| 230 |
let path = td.path(); |
| 231 |
|
| 232 |
let mut opts = RepositoryInitOptions::new(); |
| 233 |
opts.initial_head("somerandombranchnamehere"); |
| 234 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 235 |
|
| 236 |
let head_ref = repo.find_reference("HEAD").unwrap(); |
| 237 |
assert_eq!(Some(ReferenceType::Symbolic), head_ref.kind()); |
| 238 |
assert_eq!(Ok("HEAD"), head_ref.name()); |
| 239 |
|
| 240 |
let target = head_ref.symbolic_target(); |
| 241 |
assert_eq!(Ok(Some("refs/heads/somerandombranchnamehere")), target); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn repo_status() { |
| 247 |
let td = TempDir::new().unwrap(); |
| 248 |
let path = td.path(); |
| 249 |
|
| 250 |
let mut opts = RepositoryInitOptions::new(); |
| 251 |
opts.initial_head("main"); |
| 252 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 253 |
|
| 254 |
let mut config = repo.config().unwrap(); |
| 255 |
config.set_str("user.name", "name").unwrap(); |
| 256 |
config.set_str("user.email", "email").unwrap(); |
| 257 |
|
| 258 |
// Create some files |
| 259 |
fs::write(path.join("BothModified"), "BothModified").unwrap(); |
| 260 |
fs::write(path.join("IndexModified"), "IndexModified").unwrap(); |
| 261 |
fs::write(path.join("IndexDeleted"), "IndexDeleted").unwrap(); |
| 262 |
fs::write(path.join("IndexRenamed"), "IndexRenamed").unwrap(); |
| 263 |
fs::write(path.join("IndexTypechange"), "IndexTypechange").unwrap(); |
| 264 |
fs::write(path.join("WorktreeDeleted"), "WorktreeDeleted").unwrap(); |
| 265 |
fs::write(path.join("WorktreeModified"), "WorktreeModified").unwrap(); |
| 266 |
fs::write(path.join("WorktreeTypechange"), "WorktreeTypechange").unwrap(); |
| 267 |
fs::write(path.join("WorktreeRenamed"), "WorktreeRenamed").unwrap(); |
| 268 |
fs::write(path.join("Unchanged"), "Unchanged").unwrap(); |
| 269 |
fs::write(path.join(".gitignore"), "ignored-*").unwrap(); |
| 270 |
|
| 271 |
let mut index = repo.index().unwrap(); |
| 272 |
index.add_path(&Path::new("BothModified")).unwrap(); |
| 273 |
index.add_path(&Path::new("IndexModified")).unwrap(); |
| 274 |
index.add_path(&Path::new("IndexDeleted")).unwrap(); |
| 275 |
index.add_path(&Path::new("IndexRenamed")).unwrap(); |
| 276 |
index.add_path(&Path::new("IndexTypechange")).unwrap(); |
| 277 |
index.add_path(&Path::new("Unchanged")).unwrap(); |
| 278 |
index.add_path(&Path::new("WorktreeDeleted")).unwrap(); |
| 279 |
index.add_path(&Path::new("WorktreeModified")).unwrap(); |
| 280 |
index.add_path(&Path::new("WorktreeRenamed")).unwrap(); |
| 281 |
index.add_path(&Path::new("WorktreeTypechange")).unwrap(); |
| 282 |
index.add_path(&Path::new(".gitignore")).unwrap(); |
| 283 |
|
| 284 |
let id = index.write_tree().unwrap(); |
| 285 |
|
| 286 |
let tree = repo.find_tree(id).unwrap(); |
| 287 |
let sig = repo.signature().unwrap(); |
| 288 |
repo.commit(Some("HEAD"), &sig, &sig, "Initial files", &tree, &[]) |
| 289 |
.unwrap(); |
| 290 |
|
| 291 |
// Modify some files that will differ between HEAD and index |
| 292 |
fs::write(path.join("BothModified"), "Modified in index").unwrap(); |
| 293 |
fs::write(path.join("IndexModified"), "IndexModified-content2").unwrap(); |
| 294 |
fs::write(path.join("IndexNew"), "IndexNew").unwrap(); |
| 295 |
fs::remove_file(path.join("IndexDeleted")).unwrap(); |
| 296 |
fs::remove_file(path.join("IndexTypechange")).unwrap(); |
| 297 |
|
| 298 |
fn create_symlink(to: &Path, from: &Path) { |
| 299 |
#[cfg(unix)] |
| 300 |
std::os::unix::fs::symlink(to, from).unwrap(); |
| 301 |
|
| 302 |
#[cfg(windows)] |
| 303 |
std::os::windows::fs::symlink_file(to, from).unwrap(); |
| 304 |
} |
| 305 |
create_symlink(&path.join("Unchanged"), &path.join("IndexTypechange")); |
| 306 |
|
| 307 |
fs::rename(path.join("IndexRenamed"), path.join("IndexRenamed-new")).unwrap(); |
| 308 |
|
| 309 |
index.add_path(&Path::new("BothModified")).unwrap(); |
| 310 |
index.remove_path(&Path::new("IndexDeleted")).unwrap(); |
| 311 |
index.add_path(&Path::new("IndexModified")).unwrap(); |
| 312 |
index.add_path(&Path::new("IndexNew")).unwrap(); |
| 313 |
index.remove_path(&Path::new("IndexRenamed")).unwrap(); |
| 314 |
index.add_path(&Path::new("IndexRenamed-new")).unwrap(); |
| 315 |
index.add_path(&Path::new("IndexTypechange")).unwrap(); |
| 316 |
|
| 317 |
// And between index and worktree |
| 318 |
fs::write(path.join("ignored-random"), "ignored-random").unwrap(); |
| 319 |
fs::write(path.join("BothModified"), "Modified in worktree").unwrap(); |
| 320 |
fs::remove_file(path.join("WorktreeDeleted")).unwrap(); |
| 321 |
fs::write(path.join("WorktreeModified"), "New content").unwrap(); |
| 322 |
fs::write(path.join("WorktreeNew"), "WorktreeNew").unwrap(); |
| 323 |
fs::remove_file(path.join("WorktreeTypechange")).unwrap(); |
| 324 |
fs::rename( |
| 325 |
path.join("WorktreeRenamed"), |
| 326 |
path.join("WorktreeRenamed-new"), |
| 327 |
) |
| 328 |
.unwrap(); |
| 329 |
|
| 330 |
create_symlink(&path.join("Unchanged"), &path.join("WorktreeTypechange")); |
| 331 |
|
| 332 |
let mut opts = StatusOptions::new(); |
| 333 |
opts.renames_head_to_index(true); |
| 334 |
opts.renames_index_to_workdir(true); |
| 335 |
opts.include_untracked(true); |
| 336 |
opts.include_unmodified(true); |
| 337 |
opts.include_ignored(true); |
| 338 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 339 |
|
| 340 |
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] |
| 341 |
struct SimpleEntry { |
| 342 |
path: String, |
| 343 |
status: String, |
| 344 |
} |
| 345 |
let mut entries: Vec<SimpleEntry> = vec![]; |
| 346 |
for entry in status.iter() { |
| 347 |
entries.push({ |
| 348 |
SimpleEntry { |
| 349 |
path: entry.path().unwrap().to_string(), |
| 350 |
status: format!("{:?}", entry.status()), |
| 351 |
} |
| 352 |
}); |
| 353 |
} |
| 354 |
entries.sort(); |
| 355 |
macro_rules! expected { |
| 356 |
($($path:literal -> $status:expr,)+) => { |
| 357 |
vec![ |
| 358 |
$(SimpleEntry { path: $path.to_string(), status: concat!("Status(", stringify!($status), ")").to_string() }),+ |
| 359 |
] |
| 360 |
} |
| 361 |
} |
| 362 |
// Does not cover WT_UNREADABLE which is rare, or CONFLICTED which is for |
| 363 |
// in-progress conflicts |
| 364 |
// Doesn't show all combinations of index and worktree changes, just a few |
| 365 |
assert_eq!( |
| 366 |
expected!( |
| 367 |
// Status::CURRENT |
| 368 |
".gitignore" -> 0x0, |
| 369 |
"BothModified" -> INDEX_MODIFIED | WT_MODIFIED, |
| 370 |
"IndexDeleted" -> INDEX_DELETED, |
| 371 |
"IndexModified" -> INDEX_MODIFIED, |
| 372 |
"IndexNew" -> INDEX_NEW, |
| 373 |
"IndexRenamed" -> INDEX_RENAMED, |
| 374 |
"IndexTypechange" -> INDEX_TYPECHANGE, |
| 375 |
// Status::CURRENT |
| 376 |
"Unchanged" -> 0x0, |
| 377 |
"WorktreeDeleted" -> WT_DELETED, |
| 378 |
"WorktreeModified" -> WT_MODIFIED, |
| 379 |
"WorktreeNew" -> WT_NEW, |
| 380 |
"WorktreeRenamed" -> WT_RENAMED, |
| 381 |
"WorktreeTypechange" -> WT_TYPECHANGE, |
| 382 |
"ignored-random" -> IGNORED, |
| 383 |
), |
| 384 |
entries |
| 385 |
); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn repo_status_clean() { |
| 390 |
let td = TempDir::new().unwrap(); |
| 391 |
let path = td.path(); |
| 392 |
|
| 393 |
let mut opts = RepositoryInitOptions::new(); |
| 394 |
opts.initial_head("main"); |
| 395 |
let repo = Repository::init_opts(path, &opts).unwrap(); |
| 396 |
|
| 397 |
let mut config = repo.config().unwrap(); |
| 398 |
config.set_str("user.name", "name").unwrap(); |
| 399 |
config.set_str("user.email", "email").unwrap(); |
| 400 |
|
| 401 |
// Create some files |
| 402 |
fs::write(path.join("MyFile"), "content").unwrap(); |
| 403 |
|
| 404 |
let mut index = repo.index().unwrap(); |
| 405 |
index.add_path(&Path::new("MyFile")).unwrap(); |
| 406 |
|
| 407 |
let id = index.write_tree().unwrap(); |
| 408 |
|
| 409 |
let tree = repo.find_tree(id).unwrap(); |
| 410 |
let sig = repo.signature().unwrap(); |
| 411 |
repo.commit(Some("HEAD"), &sig, &sig, "Initial files", &tree, &[]) |
| 412 |
.unwrap(); |
| 413 |
|
| 414 |
// A repo status is "clean" if |
| 415 |
// - there are no untracked files |
| 416 |
// - there are no modified files, in either the index or worktree |
| 417 |
let mut opts = StatusOptions::new(); |
| 418 |
opts.include_untracked(true); |
| 419 |
|
| 420 |
// Repo is clean: |
| 421 |
{ |
| 422 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 423 |
assert_eq!(0, status.len()); |
| 424 |
} |
| 425 |
|
| 426 |
fs::write(path.join("OtherFile"), "content").unwrap(); |
| 427 |
|
| 428 |
// Repo is dirty due to the untracked file |
| 429 |
{ |
| 430 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 431 |
assert_eq!(1, status.len()); |
| 432 |
let entry = status.get(0).unwrap(); |
| 433 |
assert_eq!(Ok("OtherFile"), entry.path()); |
| 434 |
assert_eq!(Status::WT_NEW, entry.status()); |
| 435 |
} |
| 436 |
|
| 437 |
// Add it to the index |
| 438 |
index.add_path(&Path::new("OtherFile")).unwrap(); |
| 439 |
|
| 440 |
// Still dirty |
| 441 |
{ |
| 442 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 443 |
assert_eq!(1, status.len()); |
| 444 |
let entry = status.get(0).unwrap(); |
| 445 |
assert_eq!(Ok("OtherFile"), entry.path()); |
| 446 |
assert_eq!(Status::INDEX_NEW, entry.status()); |
| 447 |
} |
| 448 |
|
| 449 |
// Remove the file, |
| 450 |
fs::remove_file(path.join("OtherFile")).unwrap(); |
| 451 |
|
| 452 |
// Still dirty because it is in the index |
| 453 |
{ |
| 454 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 455 |
assert_eq!(1, status.len()); |
| 456 |
let entry = status.get(0).unwrap(); |
| 457 |
assert_eq!(Ok("OtherFile"), entry.path()); |
| 458 |
assert_eq!(Status::INDEX_NEW | Status::WT_DELETED, entry.status()); |
| 459 |
} |
| 460 |
|
| 461 |
// After removing from the index, it should be clean again |
| 462 |
index.remove_path(&Path::new("OtherFile")).unwrap(); |
| 463 |
|
| 464 |
{ |
| 465 |
let status = repo.statuses(Some(&mut opts)).unwrap(); |
| 466 |
assert_eq!(0, status.len()); |
| 467 |
} |
| 468 |
} |