From bfb16a4c54ae33679b43587a851c37c0f77988be Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 27 Aug 2024 21:34:35 -0700 Subject: [PATCH] cli: get repo path from `WorkspaceCommandHelper`, not from `ReadonlyRepo` I'd like to remove `ReadonlyRepo::repo_path()` since it doesn't make sense when the repo is stored in a database. --- cli/src/cli_util.rs | 4 ++++ cli/src/commands/commit.rs | 2 +- cli/src/commands/describe.rs | 6 +++--- cli/src/commands/git/clone.rs | 2 +- cli/src/commands/git/init.rs | 9 +++++---- cli/src/commands/sparse.rs | 2 +- cli/src/commands/split.rs | 5 +++-- cli/src/commands/squash.rs | 7 ++++++- cli/src/commands/unsquash.rs | 8 ++++++-- cli/src/description_util.rs | 13 ++++++------- 10 files changed, 36 insertions(+), 22 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index cb17ac23c..7cf29a447 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -785,6 +785,10 @@ impl WorkspaceCommandHelper { &self.user_repo.repo } + pub fn repo_path(&self) -> &Path { + self.workspace.repo_path() + } + pub fn working_copy(&self) -> &dyn WorkingCopy { self.workspace.working_copy() } diff --git a/cli/src/commands/commit.rs b/cli/src/commands/commit.rs index 68c20ca1a..e8161fea4 100644 --- a/cli/src/commands/commit.rs +++ b/cli/src/commands/commit.rs @@ -115,7 +115,7 @@ new working-copy commit. } let temp_commit = commit_builder.write_hidden()?; let template = description_template(&tx, "", &temp_commit)?; - edit_description(tx.base_repo(), &template, command.settings())? + edit_description(tx.base_workspace_helper(), &template, command.settings())? }; commit_builder.set_description(description); let new_commit = commit_builder.write(tx.repo_mut())?; diff --git a/cli/src/commands/describe.rs b/cli/src/commands/describe.rs index 1cb888757..b4c1641c8 100644 --- a/cli/src/commands/describe.rs +++ b/cli/src/commands/describe.rs @@ -123,7 +123,6 @@ pub(crate) fn cmd_describe( }) .collect() } else { - let repo = tx.base_repo().clone(); let temp_commits: Vec<(_, _)> = commits .iter() // Edit descriptions in topological order @@ -147,7 +146,8 @@ pub(crate) fn cmd_describe( if let [(_, temp_commit)] = &*temp_commits { let template = description_template(&tx, "", temp_commit)?; - let description = edit_description(&repo, &template, command.settings())?; + let description = + edit_description(tx.base_workspace_helper(), &template, command.settings())?; vec![(&commits[0], description)] } else { @@ -156,7 +156,7 @@ pub(crate) fn cmd_describe( missing, duplicates, unexpected, - } = edit_multiple_descriptions(&mut tx, &repo, &temp_commits, command.settings())?; + } = edit_multiple_descriptions(&mut tx, &temp_commits, command.settings())?; if !missing.is_empty() { return Err(user_error(format!( "The description for the following commits were not found in the edited \ diff --git a/cli/src/commands/git/clone.rs b/cli/src/commands/git/clone.rs index f2b2fb3d5..ee52e35d4 100644 --- a/cli/src/commands/git/clone.rs +++ b/cli/src/commands/git/clone.rs @@ -158,7 +158,7 @@ pub fn cmd_git_clone( let (mut workspace_command, stats) = clone_result?; if let Some(default_branch) = &stats.default_branch { // Set repository level `trunk()` alias to the default remote branch. - let config_path = workspace_command.repo().repo_path().join("config.toml"); + let config_path = workspace_command.repo_path().join("config.toml"); write_config_value_to_file( &ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]), format!("{default_branch}@{remote_name}").into(), diff --git a/cli/src/commands/git/init.rs b/cli/src/commands/git/init.rs index 1af236de5..4b6a6d79a 100644 --- a/cli/src/commands/git/init.rs +++ b/cli/src/commands/git/init.rs @@ -28,6 +28,7 @@ use jj_lib::workspace::Workspace; use crate::cli_util::print_trackable_remote_branches; use crate::cli_util::start_repo_transaction; use crate::cli_util::CommandHelper; +use crate::cli_util::WorkspaceCommandHelper; use crate::command_error::cli_error; use crate::command_error::user_error_with_hint; use crate::command_error::user_error_with_message; @@ -171,7 +172,7 @@ pub fn do_init( let mut workspace_command = command.for_workable_repo(ui, workspace, repo)?; maybe_add_gitignore(&workspace_command)?; workspace_command.maybe_snapshot(ui)?; - maybe_set_repository_level_trunk_alias(ui, workspace_command.repo())?; + maybe_set_repository_level_trunk_alias(ui, &workspace_command)?; if !workspace_command.working_copy_shared_with_git() { let mut tx = workspace_command.start_transaction(); jj_lib::git::import_head(tx.repo_mut())?; @@ -234,9 +235,9 @@ fn init_git_refs( // Set repository level `trunk()` alias to the default branch for "origin". pub fn maybe_set_repository_level_trunk_alias( ui: &Ui, - repo: &Arc, + workspace_command: &WorkspaceCommandHelper, ) -> Result<(), CommandError> { - let git_repo = get_git_repo(repo.store())?; + let git_repo = get_git_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 { @@ -244,7 +245,7 @@ pub fn maybe_set_repository_level_trunk_alias( .. }) = parse_git_ref(reference_name) { - let config_path = repo.repo_path().join("config.toml"); + let config_path = workspace_command.repo_path().join("config.toml"); write_config_value_to_file( &ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]), format!("{default_branch}@origin").into(), diff --git a/cli/src/commands/sparse.rs b/cli/src/commands/sparse.rs index 02d61f317..84c31d1a3 100644 --- a/cli/src/commands/sparse.rs +++ b/cli/src/commands/sparse.rs @@ -154,7 +154,7 @@ fn cmd_sparse_edit( _args: &SparseEditArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let repo_path = workspace_command.repo().repo_path().to_owned(); + let repo_path = workspace_command.repo_path().to_owned(); update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| { let mut new_patterns = edit_sparse(&repo_path, old_patterns, command.settings())?; new_patterns.sort_unstable(); diff --git a/cli/src/commands/split.rs b/cli/src/commands/split.rs index 1f77f0a20..e758646d7 100644 --- a/cli/src/commands/split.rs +++ b/cli/src/commands/split.rs @@ -140,7 +140,8 @@ the operation will be aborted. "Enter a description for the first commit.", &temp_commit, )?; - let description = edit_description(tx.base_repo(), &template, command.settings())?; + let description = + edit_description(tx.base_workspace_helper(), &template, command.settings())?; commit_builder.set_description(description); commit_builder.write(tx.repo_mut())? }; @@ -182,7 +183,7 @@ the operation will be aborted. "Enter a description for the second commit.", &temp_commit, )?; - edit_description(tx.base_repo(), &template, command.settings())? + edit_description(tx.base_workspace_helper(), &template, command.settings())? }; commit_builder.set_description(description); commit_builder.write(tx.repo_mut())? diff --git a/cli/src/commands/squash.rs b/cli/src/commands/squash.rs index 7737af755..86750ecbe 100644 --- a/cli/src/commands/squash.rs +++ b/cli/src/commands/squash.rs @@ -307,7 +307,12 @@ from the source will be moved into the destination. .iter() .filter_map(|source| source.abandon.then_some(source.commit)) .collect_vec(); - combine_messages(tx.base_repo(), &abandoned_commits, destination, settings)? + combine_messages( + tx.base_workspace_helper(), + &abandoned_commits, + destination, + settings, + )? } }; let mut predecessors = vec![destination.id().clone()]; diff --git a/cli/src/commands/unsquash.rs b/cli/src/commands/unsquash.rs index ddb35e251..eb11a7a07 100644 --- a/cli/src/commands/unsquash.rs +++ b/cli/src/commands/unsquash.rs @@ -107,8 +107,12 @@ aborted. // case). if new_parent_tree_id == parent_base_tree.id() { tx.repo_mut().record_abandoned_commit(parent.id().clone()); - let description = - combine_messages(tx.base_repo(), &[&parent], &commit, command.settings())?; + let description = combine_messages( + tx.base_workspace_helper(), + &[&parent], + &commit, + command.settings(), + )?; // Commit the new child on top of the parent's parents. tx.repo_mut() .rewrite_commit(command.settings(), &commit) diff --git a/cli/src/description_util.rs b/cli/src/description_util.rs index ce5199e3e..7b13e3cc1 100644 --- a/cli/src/description_util.rs +++ b/cli/src/description_util.rs @@ -7,12 +7,12 @@ use indoc::indoc; use itertools::Itertools; use jj_lib::backend::CommitId; use jj_lib::commit::Commit; -use jj_lib::repo::ReadonlyRepo; use jj_lib::settings::UserSettings; use thiserror::Error; use crate::cli_util::edit_temp_file; use crate::cli_util::short_commit_hash; +use crate::cli_util::WorkspaceCommandHelper; use crate::cli_util::WorkspaceCommandTransaction; use crate::command_error::CommandError; use crate::formatter::PlainTextFormatter; @@ -33,7 +33,7 @@ where } pub fn edit_description( - repo: &ReadonlyRepo, + workspace_command: &WorkspaceCommandHelper, description: &str, settings: &UserSettings, ) -> Result { @@ -47,7 +47,7 @@ JJ: Lines starting with "JJ: " (like this one) will be removed. let description = edit_temp_file( "description", ".jjdescription", - repo.repo_path(), + workspace_command.repo_path(), &description, settings, )?; @@ -58,7 +58,6 @@ JJ: Lines starting with "JJ: " (like this one) will be removed. /// Edits the descriptions of the given commits in a single editor session. pub fn edit_multiple_descriptions( tx: &mut WorkspaceCommandTransaction, - repo: &ReadonlyRepo, commits: &[(&CommitId, Commit)], settings: &UserSettings, ) -> Result, CommandError> { @@ -87,7 +86,7 @@ pub fn edit_multiple_descriptions( let bulk_message = edit_temp_file( "description", ".jjdescription", - repo.repo_path(), + tx.base_workspace_helper().repo_path(), &bulk_message, settings, )?; @@ -178,7 +177,7 @@ where /// then that one is used. Otherwise we concatenate the messages and ask the /// user to edit the result in their editor. pub fn combine_messages( - repo: &ReadonlyRepo, + workspace_command: &WorkspaceCommandHelper, sources: &[&Commit], destination: &Commit, settings: &UserSettings, @@ -208,7 +207,7 @@ pub fn combine_messages( combined.push_str("\nJJ: Description from source commit:\n"); combined.push_str(commit.description()); } - edit_description(repo, &combined, settings) + edit_description(workspace_command, &combined, settings) } /// Create a description from a list of paragraphs.