Skip to content

Commit 2dbd0de

Browse files
committed
✅ Standardize default branch name in test setup
Ensure test repository uses 'main' as default branch name for consistency across different Git environments. - Add branch name check and standardization in setup_git_repo() - Create or update 'main' branch if current branch differs - Set HEAD to point to 'main' branch and checkout - Use force checkout to handle any working directory conflicts - Maintain compatibility with environments using different defaults
1 parent 52ef008 commit 2dbd0de

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/git_tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ fn setup_git_repo() -> (TempDir, GitRepo) {
4545
)
4646
.expect("Failed to commit");
4747

48+
// Ensure the default branch is named 'main' for consistency across environments
49+
{
50+
let head_commit = repo
51+
.head()
52+
.expect("Failed to get HEAD")
53+
.peel_to_commit()
54+
.expect("Failed to peel HEAD to commit");
55+
let current_branch = repo
56+
.head()
57+
.ok()
58+
.and_then(|h| h.shorthand().map(|s| s.to_string()))
59+
.unwrap_or_default();
60+
if current_branch != "main" {
61+
// Create or update the 'main' branch pointing to the current HEAD commit
62+
repo.branch("main", &head_commit, true)
63+
.expect("Failed to create 'main' branch");
64+
repo.set_head("refs/heads/main")
65+
.expect("Failed to set HEAD to 'main' branch");
66+
repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force()))
67+
.expect("Failed to checkout 'main' branch");
68+
}
69+
}
70+
4871
let git_repo = GitRepo::new(temp_dir.path()).expect("Failed to create GitRepo");
4972
(temp_dir, git_repo)
5073
}

0 commit comments

Comments
 (0)