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.
This commit is contained in:
Martin von Zweigbergk 2021-09-22 11:19:41 -07:00
parent 2086d1a84d
commit eed715dc51
2 changed files with 9 additions and 5 deletions

View file

@ -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),
})?;

View file

@ -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)));