Skip to content

Commit 28722c5

Browse files
Add end-to-end test showing branch name on initialization
Closes #570
1 parent e4cf075 commit 28722c5

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

tests/end_to_end.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! 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};
33

44
use libgit2_sys as raw;
55
use std::ffi::{CString, OsString};
@@ -200,3 +200,41 @@ fn stash_length() {
200200
let after_drop2 = repo.reflog("refs/stash").expect("Should work");
201201
assert_eq!(0, after_drop2.len());
202202
}
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

Comments
 (0)