sparse: make "sparse set --edit" accept only workspace-relative paths

I don't think it needs to convert absolute file paths to workspace paths.
This commit is contained in:
Yuya Nishihara 2023-11-20 17:37:17 +09:00
parent 8ad0a703d4
commit 5c22164a26

View file

@ -19,13 +19,12 @@ use std::path::Path;
use clap::Subcommand; use clap::Subcommand;
use itertools::Itertools; use itertools::Itertools;
use jj_lib::file_util;
use jj_lib::repo_path::RepoPathBuf; use jj_lib::repo_path::RepoPathBuf;
use jj_lib::settings::UserSettings; use jj_lib::settings::UserSettings;
use tracing::instrument; use tracing::instrument;
use crate::cli_util::{edit_temp_file, print_checkout_stats, CommandHelper}; use crate::cli_util::{edit_temp_file, print_checkout_stats, CommandHelper};
use crate::command_error::{internal_error_with_message, CommandError}; use crate::command_error::{internal_error_with_message, user_error_with_message, CommandError};
use crate::ui::Ui; use crate::ui::Ui;
/// Manage which paths from the working-copy commit are present in the working /// Manage which paths from the working-copy commit are present in the working
@ -111,14 +110,7 @@ fn cmd_sparse_set(
.iter() .iter()
.map(|v| workspace_command.parse_file_path(v)) .map(|v| workspace_command.parse_file_path(v))
.try_collect()?; .try_collect()?;
// Determine inputs of `edit` operation now, since `workspace_command` is let repo_path = workspace_command.repo().repo_path().to_owned();
// inaccessible while the working copy is locked.
let edit_inputs = args.edit.then(|| {
(
workspace_command.repo().clone(),
workspace_command.workspace_root().clone(),
)
});
let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?; let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?;
let mut new_patterns = HashSet::new(); let mut new_patterns = HashSet::new();
if args.reset { if args.reset {
@ -136,13 +128,8 @@ fn cmd_sparse_set(
} }
let mut new_patterns = new_patterns.into_iter().collect_vec(); let mut new_patterns = new_patterns.into_iter().collect_vec();
new_patterns.sort(); new_patterns.sort();
if let Some((repo, workspace_root)) = edit_inputs { if args.edit {
new_patterns = edit_sparse( new_patterns = edit_sparse(&repo_path, &new_patterns, command.settings())?;
&workspace_root,
repo.repo_path(),
&new_patterns,
command.settings(),
)?;
new_patterns.sort(); new_patterns.sort();
} }
let stats = locked_ws let stats = locked_ws
@ -157,15 +144,13 @@ fn cmd_sparse_set(
} }
fn edit_sparse( fn edit_sparse(
workspace_root: &Path,
repo_path: &Path, repo_path: &Path,
sparse: &[RepoPathBuf], sparse: &[RepoPathBuf],
settings: &UserSettings, settings: &UserSettings,
) -> Result<Vec<RepoPathBuf>, CommandError> { ) -> Result<Vec<RepoPathBuf>, CommandError> {
let mut content = String::new(); let mut content = String::new();
for sparse_path in sparse { for sparse_path in sparse {
let workspace_relative_sparse_path = let workspace_relative_sparse_path = sparse_path.to_fs_path(Path::new(""));
file_util::relative_path(workspace_root, &sparse_path.to_fs_path(workspace_root));
let path_string = workspace_relative_sparse_path.to_str().ok_or_else(|| { let path_string = workspace_relative_sparse_path.to_str().ok_or_else(|| {
io::Error::new( io::Error::new(
io::ErrorKind::InvalidData, io::ErrorKind::InvalidData,
@ -188,13 +173,13 @@ fn edit_sparse(
content content
.lines() .lines()
.filter(|line| !line.starts_with("JJ: ") && !line.trim().is_empty()) .filter(|line| !line.starts_with("JJ: "))
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.map(|line| { .map(|line| {
Ok::<_, CommandError>(RepoPathBuf::parse_fs_path( RepoPathBuf::from_relative_path(line).map_err(|err| {
workspace_root, user_error_with_message(format!("Failed to parse sparse pattern: {line}"), err)
workspace_root, })
line.trim(),
)?)
}) })
.try_collect() .try_collect()
} }