diff --git a/lib/src/git.rs b/lib/src/git.rs index d05b18e0ef..f77722e77f 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -245,14 +245,17 @@ pub fn export_refs( upgrade_old_export_state(repo); let last_export_path = repo.repo_path().join("git_export_view"); - if let Ok(last_export_file) = OpenOptions::new().read(true).open(&last_export_path) { - let mut protocol = TCompactInputProtocol::new(&last_export_file); - let thrift_view = simple_op_store_model::View::read_from_in_protocol(&mut protocol) - .map_err(|err| GitExportError::ReadStateError(err.to_string()))?; - let last_export_store_view = simple_op_store::view_from_thrift(&thrift_view); - let last_export_view = View::new(last_export_store_view); - export_changes(&last_export_view, repo.view(), git_repo)?; - } + let last_export_store_view = + if let Ok(last_export_file) = OpenOptions::new().read(true).open(&last_export_path) { + let mut protocol = TCompactInputProtocol::new(&last_export_file); + let thrift_view = simple_op_store_model::View::read_from_in_protocol(&mut protocol) + .map_err(|err| GitExportError::ReadStateError(err.to_string()))?; + simple_op_store::view_from_thrift(&thrift_view) + } else { + crate::op_store::View::default() + }; + let last_export_view = View::new(last_export_store_view); + export_changes(&last_export_view, repo.view(), git_repo)?; if let Ok(mut last_export_file) = OpenOptions::new() .write(true) .create(true) diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index 88f997cad2..e2f5ea0084 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -403,8 +403,9 @@ fn test_import_refs_detached_head() { } #[test] -fn test_export_refs_initial() { - // The first export doesn't do anything +fn test_export_refs_no_detach() { + // When exporting the branch that's current checked out, don't detach HEAD if + // the target already matches let mut test_data = GitRepoData::create(); let git_repo = test_data.git_repo; let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]); @@ -416,7 +417,7 @@ fn test_export_refs_initial() { .unwrap(); test_data.repo = tx.commit(); - // The first export shouldn't do anything + // Do an initial export to make sure `main` is considered assert_eq!(git::export_refs(&test_data.repo, &git_repo), Ok(())); assert_eq!(git_repo.head().unwrap().name(), Some("refs/heads/main")); assert_eq!(