From bfb5e55cfd550ad14d460a3e9453c6bf72c1ed42 Mon Sep 17 00:00:00 2001 From: Waleed Khan Date: Wed, 22 Sep 2021 16:17:01 -0700 Subject: [PATCH] git: properly detect default branch The default branch relies on checking the value of `HEAD`. The `empty_git_commit` function updates the ref `refs/heads/main`, but since `HEAD` was never updated to point to that ref, the default branch can't be determined. The fix is to explicitly set `HEAD`. Personally, this test failed reliably for me on macOS. I don't know why this behavior would be non-deterministic on other platforms. --- lib/tests/test_git.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index 373b7b3c8..e18798c86 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -285,6 +285,7 @@ fn test_fetch_success() { let jj_repo_dir = temp_dir.path().join("jj"); let source_git_repo = git2::Repository::init_bare(&source_repo_dir).unwrap(); let initial_git_commit = empty_git_commit(&source_git_repo, "refs/heads/main", &[]); + source_git_repo.set_head("refs/heads/main").unwrap(); let clone_git_repo = git2::Repository::clone(source_repo_dir.to_str().unwrap(), &clone_repo_dir).unwrap(); std::fs::create_dir(&jj_repo_dir).unwrap(); @@ -301,10 +302,9 @@ fn test_fetch_success() { .contains(&commit_id(&new_git_commit))); let mut tx = jj_repo.start_transaction("test"); - let _default_branch = git::fetch(tx.mut_repo(), &clone_git_repo, "origin").unwrap(); + let default_branch = git::fetch(tx.mut_repo(), &clone_git_repo, "origin").unwrap(); // The default branch is "main" - // TODO: Figure out why this is unreliable. Issue #30. - //assert_eq!(default_branch, Some("main".to_string())); + assert_eq!(default_branch, Some("main".to_string())); let repo = tx.commit(); // The new commit is visible after git::fetch(). assert!(repo.view().heads().contains(&commit_id(&new_git_commit)));