cli: extract helper function that constructs SnapshotOptions

It's a bit weird that we have to construct a start-tracking matcher by caller,
but it has to be parameterized anyway.
This commit is contained in:
Yuya Nishihara 2024-12-19 18:10:07 +09:00
parent 1b4e210524
commit 3381dd2a2c
3 changed files with 36 additions and 46 deletions

View file

@ -1305,6 +1305,24 @@ to the current parents may contain changes from multiple commits.
Ok(expression.to_matcher())
}
pub fn snapshot_options_with_start_tracking_matcher<'a>(
&self,
start_tracking_matcher: &'a dyn Matcher,
) -> Result<SnapshotOptions<'a>, CommandError> {
let base_ignores = self.base_ignores()?;
let fsmonitor_settings = self.settings().fsmonitor_settings()?;
let max_new_file_size = self.settings().max_new_file_size()?;
let conflict_marker_style = self.env.conflict_marker_style();
Ok(SnapshotOptions {
base_ignores,
fsmonitor_settings,
progress: None,
start_tracking_matcher,
max_new_file_size,
conflict_marker_style,
})
}
pub(crate) fn path_converter(&self) -> &RepoPathUiConverter {
self.env.path_converter()
}
@ -1751,21 +1769,14 @@ to the current parents may contain changes from multiple commits.
// committing the working copy.
return Ok(());
};
let base_ignores = self.base_ignores().map_err(snapshot_command_error)?;
let auto_tracking_matcher = self
.auto_tracking_matcher(ui)
.map_err(snapshot_command_error)?;
let options = self
.snapshot_options_with_start_tracking_matcher(&auto_tracking_matcher)
.map_err(snapshot_command_error)?;
// Compare working-copy tree and operation with repo's, and reload as needed.
let fsmonitor_settings = self
.settings()
.fsmonitor_settings()
.map_err(snapshot_command_error)?;
let max_new_file_size = self
.settings()
.max_new_file_size()
.map_err(snapshot_command_error)?;
let conflict_marker_style = self.env.conflict_marker_style();
let command = self.env.command.clone();
let mut locked_ws = self
.workspace
@ -1824,19 +1835,15 @@ See https://jj-vcs.github.io/jj/latest/working-copy/#stale-working-copy \
Err(e) => return Err(snapshot_command_error(e)),
};
self.user_repo = ReadonlyUserRepo::new(repo);
let progress = crate::progress::snapshot_progress(ui);
let (new_tree_id, stats) = locked_ws
.locked_wc()
.snapshot(&SnapshotOptions {
base_ignores,
fsmonitor_settings,
progress: progress.as_ref().map(|x| x as _),
start_tracking_matcher: &auto_tracking_matcher,
max_new_file_size,
conflict_marker_style,
})
.map_err(snapshot_command_error)?;
drop(progress);
let (new_tree_id, stats) = {
let mut options = options;
let progress = crate::progress::snapshot_progress(ui);
options.progress = progress.as_ref().map(|x| x as _);
locked_ws
.locked_wc()
.snapshot(&options)
.map_err(snapshot_command_error)?
};
if new_tree_id != *wc_commit.tree_id() {
let mut tx = start_repo_transaction(
&self.user_repo.repo,

View file

@ -14,7 +14,6 @@
use std::io::Write;
use jj_lib::working_copy::SnapshotOptions;
use tracing::instrument;
use crate::cli_util::print_snapshot_stats;
@ -45,22 +44,14 @@ pub(crate) fn cmd_file_track(
args: &FileTrackArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let conflict_marker_style = workspace_command.env().conflict_marker_style();
let matcher = workspace_command
.parse_file_patterns(ui, &args.paths)?
.to_matcher();
let options = workspace_command.snapshot_options_with_start_tracking_matcher(&matcher)?;
let mut tx = workspace_command.start_transaction().into_inner();
let base_ignores = workspace_command.base_ignores()?;
let (mut locked_ws, _wc_commit) = workspace_command.start_working_copy_mutation()?;
let (_tree_id, stats) = locked_ws.locked_wc().snapshot(&SnapshotOptions {
base_ignores,
fsmonitor_settings: command.settings().fsmonitor_settings()?,
progress: None,
start_tracking_matcher: &matcher,
max_new_file_size: command.settings().max_new_file_size()?,
conflict_marker_style,
})?;
let (_tree_id, stats) = locked_ws.locked_wc().snapshot(&options)?;
let num_rebased = tx.repo_mut().rebase_descendants(command.settings())?;
if num_rebased > 0 {
writeln!(ui.status(), "Rebased {num_rebased} descendant commits")?;

View file

@ -19,7 +19,6 @@ use itertools::Itertools;
use jj_lib::merge::Merge;
use jj_lib::merged_tree::MergedTreeBuilder;
use jj_lib::repo::Repo;
use jj_lib::working_copy::SnapshotOptions;
use tracing::instrument;
use crate::cli_util::print_snapshot_stats;
@ -52,15 +51,15 @@ pub(crate) fn cmd_file_untrack(
args: &FileUntrackArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let conflict_marker_style = workspace_command.env().conflict_marker_style();
let store = workspace_command.repo().store().clone();
let matcher = workspace_command
.parse_file_patterns(ui, &args.paths)?
.to_matcher();
let auto_tracking_matcher = workspace_command.auto_tracking_matcher(ui)?;
let options =
workspace_command.snapshot_options_with_start_tracking_matcher(&auto_tracking_matcher)?;
let mut tx = workspace_command.start_transaction().into_inner();
let base_ignores = workspace_command.base_ignores()?;
let auto_tracking_matcher = workspace_command.auto_tracking_matcher(ui)?;
let (mut locked_ws, wc_commit) = workspace_command.start_working_copy_mutation()?;
// Create a new tree without the unwanted files
let mut tree_builder = MergedTreeBuilder::new(wc_commit.tree_id().clone());
@ -78,14 +77,7 @@ pub(crate) fn cmd_file_untrack(
locked_ws.locked_wc().reset(&new_commit)?;
// Commit the working copy again so we can inform the user if paths couldn't be
// untracked because they're not ignored.
let (wc_tree_id, stats) = locked_ws.locked_wc().snapshot(&SnapshotOptions {
base_ignores,
fsmonitor_settings: command.settings().fsmonitor_settings()?,
progress: None,
start_tracking_matcher: &auto_tracking_matcher,
max_new_file_size: command.settings().max_new_file_size()?,
conflict_marker_style,
})?;
let (wc_tree_id, stats) = locked_ws.locked_wc().snapshot(&options)?;
if wc_tree_id != *new_commit.tree_id() {
let wc_tree = store.get_root_tree(&wc_tree_id)?;
let added_back = wc_tree.entries_matching(matcher.as_ref()).collect_vec();