|
1 | 1 | //! Tests for some end-to-end logic about certain operations |
2 | | -use git2::{Error, Repository, RepositoryInitOptions, StashFlags}; |
| 2 | +use git2::{Error, ReferenceType, Repository, RepositoryInitOptions, StashFlags}; |
3 | 3 |
|
4 | 4 | use libgit2_sys as raw; |
5 | 5 | use std::ffi::{CString, OsString}; |
@@ -200,3 +200,41 @@ fn stash_length() { |
200 | 200 | let after_drop2 = repo.reflog("refs/stash").expect("Should work"); |
201 | 201 | assert_eq!(0, after_drop2.len()); |
202 | 202 | } |
| 203 | + |
| 204 | +#[test] |
| 205 | +fn branch_name_on_init() { |
| 206 | + // Confirm that the branch name is available via find_reference() even when |
| 207 | + // no commits are made yet and the branch doesn't exist |
| 208 | + // Test with "main" |
| 209 | + { |
| 210 | + let td = TempDir::new().unwrap(); |
| 211 | + let path = td.path(); |
| 212 | + |
| 213 | + let mut opts = RepositoryInitOptions::new(); |
| 214 | + opts.initial_head("main"); |
| 215 | + let repo = Repository::init_opts(path, &opts).unwrap(); |
| 216 | + |
| 217 | + let head_ref = repo.find_reference("HEAD").unwrap(); |
| 218 | + assert_eq!(Some(ReferenceType::Symbolic), head_ref.kind()); |
| 219 | + assert_eq!(Some("HEAD"), head_ref.name()); |
| 220 | + |
| 221 | + let target = head_ref.symbolic_target(); |
| 222 | + assert_eq!(Some("refs/heads/main"), target); |
| 223 | + } |
| 224 | + // Test with "somerandombranchnamehere" |
| 225 | + { |
| 226 | + let td = TempDir::new().unwrap(); |
| 227 | + let path = td.path(); |
| 228 | + |
| 229 | + let mut opts = RepositoryInitOptions::new(); |
| 230 | + opts.initial_head("somerandombranchnamehere"); |
| 231 | + let repo = Repository::init_opts(path, &opts).unwrap(); |
| 232 | + |
| 233 | + let head_ref = repo.find_reference("HEAD").unwrap(); |
| 234 | + assert_eq!(Some(ReferenceType::Symbolic), head_ref.kind()); |
| 235 | + assert_eq!(Some("HEAD"), head_ref.name()); |
| 236 | + |
| 237 | + let target = head_ref.symbolic_target(); |
| 238 | + assert_eq!(Some("refs/heads/somerandombranchnamehere"), target); |
| 239 | + } |
| 240 | +} |
0 commit comments