From eed715dc51e2b5433c6b4911b7e98758d87077e2 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 22 Sep 2021 11:19:41 -0700 Subject: [PATCH] git: try to fix flaky (?) fetch tests by keeping connection open longer It seems it wasn't Windows that behaved differently when it comes getting the remote's default branch; the test failed on Ubuntu too. The documentation for `Remote::default_branch()` says that it can be called even after the connection has been closed, but let's see if calling it while the connection is open helps anyway. To do that, we have to replicate what `Remote::fetch()` does. --- lib/src/git.rs | 10 +++++++++- lib/tests/test_git.rs | 4 ---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/src/git.rs b/lib/src/git.rs index ee437ae4d..bf8643d8a 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -148,7 +148,14 @@ pub fn fetch( fetch_options.remote_callbacks(callbacks); fetch_options.prune(FetchPrune::On); let refspec: &[&str] = &[]; - remote.fetch(refspec, Some(&mut fetch_options), None)?; + remote.download(refspec, Some(&mut fetch_options))?; + // The FetchOptions above ate our RemoteCallbacks so it seems we need to create + // a new instance. + let mut callbacks = git2::RemoteCallbacks::new(); + callbacks.credentials(|_url, username_from_url, _allowed_types| { + git2::Cred::ssh_key_from_agent(username_from_url.unwrap()) + }); + remote.update_tips(Some(&mut callbacks), false, git2::AutotagOption::All, None)?; // TODO: We could make it optional to get the default branch since we only care // about it on clone. let mut default_branch = None; @@ -161,6 +168,7 @@ pub fn fetch( } } } + remote.disconnect()?; import_refs(mut_repo, git_repo).map_err(|err| match err { GitImportError::InternalGitError(source) => GitFetchError::InternalGitError(source), })?; diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index f54c1c9e5..7e80e794b 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -303,11 +303,7 @@ fn test_fetch_success() { let mut tx = jj_repo.start_transaction("test"); let default_branch = git::fetch(tx.mut_repo(), &clone_git_repo, "origin").unwrap(); // The default branch is "main" - #[cfg(unix)] assert_eq!(default_branch, Some("main".to_string())); - // TODO: Figure out why we don't find the remote's default branch on Windows. - #[cfg(windows)] - assert_eq!(default_branch, None); let repo = tx.commit(); // The new commit is visible after git::fetch(). assert!(repo.view().heads().contains(&commit_id(&new_git_commit)));