From 0234894629ee090fb1db0855f9ebbf0db91776ba Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Thu, 12 Sep 2024 02:43:17 +1000 Subject: [PATCH] git: get_git_repo -> get_git_backend_repo, distinguish it from current worktree Now that colocated workspaces exist, it is necessary to distinguish the two kinds of git repository `jj` might open. There's the git backend (which is either a bare repository or a colocated one) and the worktrees for all the colocated workspaces. Both of these are a `gix/git2::Repository`, the difference is just which path you opened. So you have to distinguish them with names. This rename mostly serves as an opportunity to review at least some of the usages of explicit usage of the git backend repo. --- cli/src/commands/git/clone.rs | 4 ++-- cli/src/commands/git/fetch.rs | 4 ++-- cli/src/commands/git/init.rs | 4 ++-- cli/src/commands/git/push.rs | 4 ++-- cli/src/commands/git/remote/add.rs | 4 ++-- cli/src/commands/git/remote/list.rs | 4 ++-- cli/src/commands/git/remote/remove.rs | 4 ++-- cli/src/commands/git/remote/rename.rs | 4 ++-- cli/src/commands/git/remote/set_url.rs | 4 ++-- cli/src/git_util.rs | 11 ++++++++++- 10 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cli/src/commands/git/clone.rs b/cli/src/commands/git/clone.rs index 4c3e1cc1355..08f784e840b 100644 --- a/cli/src/commands/git/clone.rs +++ b/cli/src/commands/git/clone.rs @@ -35,7 +35,7 @@ use crate::commands::git::map_git_error; use crate::commands::git::maybe_add_gitignore; use crate::config::write_config_value_to_file; use crate::config::ConfigNamePathBuf; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::git_util::print_git_import_stats; use crate::git_util::with_remote_git_callbacks; use crate::ui::Ui; @@ -204,7 +204,7 @@ fn do_git_clone( } else { Workspace::init_internal_git(command.settings(), wc_path)? }; - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; writeln!( ui.status(), r#"Fetching into new repo in "{}""#, diff --git a/cli/src/commands/git/fetch.rs b/cli/src/commands/git/fetch.rs index 7b667b8287c..d1716d4d259 100644 --- a/cli/src/commands/git/fetch.rs +++ b/cli/src/commands/git/fetch.rs @@ -27,7 +27,7 @@ use crate::command_error::user_error_with_hint; use crate::command_error::CommandError; use crate::commands::git::get_single_remote; use crate::commands::git::map_git_error; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::git_util::print_git_import_stats; use crate::git_util::with_remote_git_callbacks; use crate::ui::Ui; @@ -60,7 +60,7 @@ pub fn cmd_git_fetch( args: &GitFetchArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let git_repo = get_git_repo(workspace_command.repo().store())?; + let git_repo = get_git_backend_repo(workspace_command.repo().store())?; let remotes = if args.all_remotes { get_all_remotes(&git_repo)? } else if args.remotes.is_empty() { diff --git a/cli/src/commands/git/init.rs b/cli/src/commands/git/init.rs index e13f25ded95..da83734a2c5 100644 --- a/cli/src/commands/git/init.rs +++ b/cli/src/commands/git/init.rs @@ -36,7 +36,7 @@ use crate::command_error::CommandError; use crate::commands::git::maybe_add_gitignore; use crate::config::write_config_value_to_file; use crate::config::ConfigNamePathBuf; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::git_util::is_colocated_git_workspace; use crate::git_util::print_failed_git_export; use crate::git_util::print_git_import_stats; @@ -242,7 +242,7 @@ pub fn maybe_set_repository_level_trunk_alias( ui: &Ui, workspace_command: &WorkspaceCommandHelper, ) -> Result<(), CommandError> { - let git_repo = get_git_repo(workspace_command.repo().store())?; + let git_repo = get_git_backend_repo(workspace_command.repo().store())?; if let Ok(reference) = git_repo.find_reference("refs/remotes/origin/HEAD") { if let Some(reference_name) = reference.symbolic_target() { if let Some(RefName::RemoteBranch { diff --git a/cli/src/commands/git/push.rs b/cli/src/commands/git/push.rs index 429d02b22d4..4e6288b10d7 100644 --- a/cli/src/commands/git/push.rs +++ b/cli/src/commands/git/push.rs @@ -48,7 +48,7 @@ use crate::command_error::CommandError; use crate::commands::git::get_single_remote; use crate::commands::git::map_git_error; use crate::formatter::Formatter; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::git_util::with_remote_git_callbacks; use crate::git_util::GitSidebandProgressMessageWriter; use crate::ui::Ui; @@ -143,7 +143,7 @@ pub fn cmd_git_push( args: &GitPushArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let git_repo = get_git_repo(workspace_command.repo().store())?; + let git_repo = get_git_backend_repo(workspace_command.repo().store())?; let remote = if let Some(name) = &args.remote { name.clone() diff --git a/cli/src/commands/git/remote/add.rs b/cli/src/commands/git/remote/add.rs index a66a7faac89..5669f903bc6 100644 --- a/cli/src/commands/git/remote/add.rs +++ b/cli/src/commands/git/remote/add.rs @@ -17,7 +17,7 @@ use jj_lib::repo::Repo; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::ui::Ui; /// Add a Git remote @@ -36,7 +36,7 @@ pub fn cmd_git_remote_add( ) -> Result<(), CommandError> { let workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; git::add_remote(&git_repo, &args.remote, &args.url)?; Ok(()) } diff --git a/cli/src/commands/git/remote/list.rs b/cli/src/commands/git/remote/list.rs index fee2d32f478..02fc6887883 100644 --- a/cli/src/commands/git/remote/list.rs +++ b/cli/src/commands/git/remote/list.rs @@ -18,7 +18,7 @@ use jj_lib::repo::Repo; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::ui::Ui; /// List Git remotes @@ -32,7 +32,7 @@ pub fn cmd_git_remote_list( ) -> Result<(), CommandError> { let workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; for remote_name in git_repo.remotes()?.iter().flatten() { let remote = git_repo.find_remote(remote_name)?; writeln!( diff --git a/cli/src/commands/git/remote/remove.rs b/cli/src/commands/git/remote/remove.rs index f2dca800dbd..dcd306131b1 100644 --- a/cli/src/commands/git/remote/remove.rs +++ b/cli/src/commands/git/remote/remove.rs @@ -17,7 +17,7 @@ use jj_lib::repo::Repo; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::ui::Ui; /// Remove a Git remote and forget its bookmarks @@ -34,7 +34,7 @@ pub fn cmd_git_remote_remove( ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; let mut tx = workspace_command.start_transaction(); git::remove_remote(tx.repo_mut(), &git_repo, &args.remote)?; if tx.repo().has_changes() { diff --git a/cli/src/commands/git/remote/rename.rs b/cli/src/commands/git/remote/rename.rs index 94f966825d3..4bbe6e86eca 100644 --- a/cli/src/commands/git/remote/rename.rs +++ b/cli/src/commands/git/remote/rename.rs @@ -17,7 +17,7 @@ use jj_lib::repo::Repo; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::ui::Ui; /// Rename a Git remote @@ -36,7 +36,7 @@ pub fn cmd_git_remote_rename( ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; let mut tx = workspace_command.start_transaction(); git::rename_remote(tx.repo_mut(), &git_repo, &args.old, &args.new)?; if tx.repo().has_changes() { diff --git a/cli/src/commands/git/remote/set_url.rs b/cli/src/commands/git/remote/set_url.rs index cee2da0559f..c80ee97be8c 100644 --- a/cli/src/commands/git/remote/set_url.rs +++ b/cli/src/commands/git/remote/set_url.rs @@ -17,7 +17,7 @@ use jj_lib::repo::Repo; use crate::cli_util::CommandHelper; use crate::command_error::CommandError; -use crate::git_util::get_git_repo; +use crate::git_util::get_git_backend_repo; use crate::ui::Ui; /// Set the URL of a Git remote @@ -36,7 +36,7 @@ pub fn cmd_git_remote_set_url( ) -> Result<(), CommandError> { let workspace_command = command.workspace_helper(ui)?; let repo = workspace_command.repo(); - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_backend_repo(repo.store())?; git::set_remote_url(&git_repo, &args.remote, &args.url)?; Ok(()) } diff --git a/cli/src/git_util.rs b/cli/src/git_util.rs index 137693996e2..24118017c59 100644 --- a/cli/src/git_util.rs +++ b/cli/src/git_util.rs @@ -44,7 +44,16 @@ use crate::formatter::Formatter; use crate::progress::Progress; use crate::ui::Ui; -pub fn get_git_repo(store: &Store) -> Result { +/// This opens a [git2::Repository] for the underlying [GitBackend], assuming +/// this store is git-backed. +/// +/// If the JJ repo is colocated with the git repo, then this will often not be +/// what you want, because its HEAD will always be for the default workspace. +/// Most JJ commands that access the HEAD need the one for the current +/// workspace. +/// +/// However, sometimes you don't care. For example, when you run `jj git fetch`. +pub fn get_git_backend_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.open_git_repo()?),