diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 48db93833c..08350ac7dd 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -776,7 +776,7 @@ impl WorkspaceCommandHelper { pub fn snapshot(&mut self, ui: &mut Ui) -> Result<(), CommandError> { if self.may_update_working_copy { if self.working_copy_shared_with_git { - let git_repo = self.git_backend().unwrap().git_repo_clone(); + let git_repo = self.git_backend().unwrap().open_git_repo()?; self.import_git_refs_and_head(ui, &git_repo)?; } self.snapshot_working_copy(ui)?; @@ -1391,7 +1391,7 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin } if self.working_copy_shared_with_git { - let git_repo = self.user_repo.git_backend().unwrap().git_repo_clone(); + let git_repo = self.user_repo.git_backend().unwrap().open_git_repo()?; let failed_branches = git::export_refs(mut_repo, &git_repo)?; print_failed_git_export(ui, &failed_branches)?; } @@ -1458,7 +1458,7 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin writeln!(ui, "Rebased {num_rebased} descendant commits")?; } if self.working_copy_shared_with_git { - let git_repo = self.git_backend().unwrap().git_repo_clone(); + let git_repo = self.git_backend().unwrap().open_git_repo()?; self.export_head_to_git(mut_repo, &git_repo)?; let failed_branches = git::export_refs(mut_repo, &git_repo)?; print_failed_git_export(ui, &failed_branches)?; diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 4f9b5228e8..1d818ef678 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -187,7 +187,7 @@ pub struct GitSubmodulePrintGitmodulesArgs { fn get_git_repo(store: &Store) -> Result { match store.backend_impl().downcast_ref::() { None => Err(user_error("The repo is not backed by a git repo")), - Some(git_backend) => Ok(git_backend.git_repo_clone()), + Some(git_backend) => Ok(git_backend.open_git_repo()?), } } diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 3836ddb038..897731b380 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1206,7 +1206,7 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(), .backend_impl() .downcast_ref::() .unwrap() - .git_repo_clone(); + .open_git_repo()?; let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?; workspace_command.snapshot(ui)?; if workspace_command.working_copy_shared_with_git() { diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 198746f040..54bdc01ebd 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -158,9 +158,10 @@ impl GitBackend { self.repo.lock().unwrap() } - pub fn git_repo_clone(&self) -> git2::Repository { - let path = self.repo.lock().unwrap().path().to_owned(); - git2::Repository::open(path).unwrap() + /// Creates new owned git repository instance. + pub fn open_git_repo(&self) -> Result { + let locked_repo = self.git_repo(); + git2::Repository::open(locked_repo.path()) } /// Git configuration for this repository. diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index 2035dcc7b4..dcab266b20 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -79,7 +79,7 @@ fn get_git_backend(repo: &Arc) -> &GitBackend { } fn get_git_repo(repo: &Arc) -> git2::Repository { - get_git_backend(repo).git_repo_clone() + get_git_backend(repo).open_git_repo().unwrap() } #[test] diff --git a/lib/tests/test_revset.rs b/lib/tests/test_revset.rs index 8e5bcf64b3..95c0244603 100644 --- a/lib/tests/test_revset.rs +++ b/lib/tests/test_revset.rs @@ -201,7 +201,8 @@ fn test_resolve_symbol_change_id(readonly: bool) { .backend_impl() .downcast_ref::() .unwrap() - .git_repo_clone(); + .open_git_repo() + .unwrap(); // Add some commits that will end up having change ids with common prefixes let empty_tree_id = git_repo.treebuilder(None).unwrap().write().unwrap(); let git_author = git2::Signature::new(