Skip to content

Commit

Permalink
git_backend: rename git_repo_clone() as it isn't just cloning, propag…
Browse files Browse the repository at this point in the history
…ate error

Since git2::Repository::open() will access to the filesystem, it can technically
fail.
  • Loading branch information
yuja committed Oct 3, 2023
1 parent 837dc4f commit 7c96cea
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub struct GitSubmodulePrintGitmodulesArgs {
fn get_git_repo(store: &Store) -> Result<git2::Repository, CommandError> {
match store.backend_impl().downcast_ref::<GitBackend>() {
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()?),
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(),
.backend_impl()
.downcast_ref::<GitBackend>()
.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() {
Expand Down
7 changes: 4 additions & 3 deletions lib/src/git_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<git2::Repository, git2::Error> {
let locked_repo = self.git_repo();
git2::Repository::open(locked_repo.path())
}

/// Git configuration for this repository.
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn get_git_backend(repo: &Arc<ReadonlyRepo>) -> &GitBackend {
}

fn get_git_repo(repo: &Arc<ReadonlyRepo>) -> git2::Repository {
get_git_backend(repo).git_repo_clone()
get_git_backend(repo).open_git_repo().unwrap()
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ fn test_resolve_symbol_change_id(readonly: bool) {
.backend_impl()
.downcast_ref::<GitBackend>()
.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(
Expand Down

0 comments on commit 7c96cea

Please sign in to comment.