Debian Codegraph @

cli/src/commands/file/track.rs

raw
1 // Copyright 2024 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::io;
16 use std::io::Write as _;
17
18 use jj_lib::ui_path::RepoPathUiConverter;
19 use jj_lib::working_copy::SnapshotStats;
20 use jj_lib::working_copy::UntrackedReason;
21 use tracing::instrument;
22
23 use crate::cli_util::CommandHelper;
24 use crate::cli_util::print_large_file_hint;
25 use crate::cli_util::print_untracked_files;
26 use crate::command_error::CommandError;
27 use crate::ui::Ui;
28
29 /// Start tracking specified paths in the working copy
30 ///
31 /// Without arguments, all paths that are not ignored will be tracked.
32 ///
33 /// By default, new files in the working copy are automatically tracked, so
34 /// this command has no effect.
35 /// You can configure which paths to automatically track by setting
36 /// `snapshot.auto-track` (e.g. to `"none()"` or `"glob:**/*.rs"`). Files that
37 /// don't match the pattern can be manually tracked using this command. The
38 /// default pattern is `all()`.
39 #[derive(clap::Args, Clone, Debug)]
40 pub(crate) struct FileTrackArgs {
41 /// Paths to track
42 #[arg(required = true, value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)]
43 paths: Vec<String>,
44
45 /// Track paths even if they're ignored or too large
46 ///
47 /// By default, `jj file track` will not track files that are ignored by
48 /// .gitignore or exceed the maximum file size. This flag overrides those
49 /// restrictions, explicitly tracking the specified paths.
50 #[arg(long)]
51 include_ignored: bool,
52 }
53
54 #[instrument(skip_all)]
55 pub(crate) async fn cmd_file_track(
56 ui: &mut Ui,
57 command: &CommandHelper,
58 args: &FileTrackArgs,
59 ) -> Result<(), CommandError> {
60 let (mut workspace_command, auto_stats, _) = command.workspace_helper_with_stats(ui).await?;
61 let matcher = workspace_command
62 .parse_file_patterns(ui, &args.paths)?
63 .to_matcher();
64
65 let mut options = workspace_command.snapshot_options_with_start_tracking_matcher(&matcher)?;
66 if args.include_ignored {
67 options.force_tracking_matcher = &matcher;
68 }
69
70 let mut tx = workspace_command.start_transaction().into_inner();
71 let (mut locked_ws, _wc_commit) = workspace_command.start_working_copy_mutation().await?;
72 let (_tree, track_stats) = locked_ws.locked_wc().snapshot(&options).await?;
73 let num_rebased = tx.repo_mut().rebase_descendants().await?;
74 if num_rebased > 0 {
75 writeln!(ui.status(), "Rebased {num_rebased} descendant commits.")?;
76 }
77 let repo = tx.commit("track paths").await?;
78 locked_ws.finish(repo.op_id().clone()).await?;
79 print_track_snapshot_stats(
80 ui,
81 auto_stats,
82 track_stats,
83 workspace_command.env().path_converter(),
84 )?;
85 Ok(())
86 }
87
88 pub fn print_track_snapshot_stats(
89 ui: &Ui,
90 mut auto_stats: SnapshotStats,
91 track_stats: SnapshotStats,
92 path_converter: &RepoPathUiConverter,
93 ) -> io::Result<()> {
94 let mut untracked_paths = track_stats.untracked_paths;
95 for (path, reason) in &mut untracked_paths {
96 if !matches!(reason, UntrackedReason::FileNotAutoTracked) {
97 continue;
98 }
99 if let Some(old_reason) = auto_stats.untracked_paths.remove(path) {
100 *reason = old_reason;
101 }
102 }
103
104 print_untracked_files(ui, &untracked_paths, path_converter)?;
105
106 let (large_files, sizes): (Vec<_>, Vec<_>) = untracked_paths
107 .iter()
108 .filter_map(|(path, reason)| match reason {
109 UntrackedReason::FileTooLarge { size, .. } => Some((path, *size)),
110 UntrackedReason::FileNotAutoTracked => None,
111 })
112 .unzip();
113 if let Some(size) = sizes.iter().max() {
114 let large_files_list: Vec<_> = large_files
115 .iter()
116 .map(|path| path_converter.format_file_path(path))
117 .collect();
118 print_large_file_hint(ui, *size, Some(&large_files_list))?;
119 }
120 Ok(())
121 }