Debian Codegraph @

cli/src/commands/file/list.rs

raw
1 // Copyright 2020 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 clap_complete::ArgValueCandidates;
16 use clap_complete::ArgValueCompleter;
17 use tracing::instrument;
18
19 use crate::cli_util::CommandHelper;
20 use crate::cli_util::RevisionArg;
21 use crate::cli_util::print_unmatched_explicit_paths;
22 use crate::command_error::CommandError;
23 use crate::commit_templater::TreeEntry;
24 use crate::complete;
25 use crate::templater::TemplateRenderer;
26 use crate::ui::Ui;
27
28 /// List files in a revision
29 #[derive(clap::Args, Clone, Debug)]
30 pub(crate) struct FileListArgs {
31 /// The revision to list files in
32 #[arg(long, short, default_value = "@", value_name = "REVSET")]
33 #[arg(add = ArgValueCompleter::new(complete::revset_expression_all))]
34 revision: RevisionArg,
35
36 /// Render each file entry using the given template
37 ///
38 /// All 0-argument methods of the [`TreeEntry` type] are available as
39 /// keywords in the template expression. See [`jj help -k templates`] for
40 /// more information.
41 ///
42 /// The default template can be set by the `templates.file_list`
43 /// setting.
44 ///
45 /// [`TreeEntry` type]:
46 /// https://docs.jj-vcs.dev/latest/templates/#treeentry-type
47 ///
48 /// [`jj help -k templates`]:
49 /// https://docs.jj-vcs.dev/latest/templates/
50 #[arg(long, short = 'T')]
51 #[arg(add = ArgValueCandidates::new(complete::template_aliases))]
52 template: Option<String>,
53
54 /// Only list files matching these prefixes (instead of all files)
55 #[arg(value_name = "FILESETS", value_hint = clap::ValueHint::AnyPath)]
56 #[arg(add = ArgValueCompleter::new(complete::all_revision_files))]
57 paths: Vec<String>,
58 }
59
60 #[instrument(skip_all)]
61 pub(crate) async fn cmd_file_list(
62 ui: &mut Ui,
63 command: &CommandHelper,
64 args: &FileListArgs,
65 ) -> Result<(), CommandError> {
66 let workspace_command = command.workspace_helper(ui).await?;
67 let commit = workspace_command
68 .resolve_single_rev(ui, &args.revision)
69 .await?;
70 let tree = commit.tree();
71 let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?;
72 let matcher = fileset_expression.to_matcher();
73 let template: TemplateRenderer<TreeEntry> = {
74 let language = workspace_command.commit_template_language();
75 let text = match &args.template {
76 Some(value) => value.to_owned(),
77 None => workspace_command.settings().get("templates.file_list")?,
78 };
79 workspace_command
80 .parse_template(ui, &language, &text)?
81 .labeled(["file_list"])
82 };
83
84 ui.request_pager();
85 let mut formatter = ui.stdout_formatter();
86 for (path, value) in tree.entries_matching(matcher.as_ref()) {
87 let entry = TreeEntry {
88 path,
89 value: value?,
90 };
91 template.format(&entry, formatter.as_mut())?;
92 }
93 print_unmatched_explicit_paths(ui, &workspace_command, &fileset_expression, [&tree]).await?;
94 Ok(())
95 }