Debian Codegraph @

cli/src/commands/file/chmod.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::ArgValueCompleter;
16 use jj_lib::backend::TreeValue;
17 use jj_lib::merged_tree_builder::MergedTreeBuilder;
18 use jj_lib::object_id::ObjectId as _;
19 use tracing::instrument;
20
21 use crate::cli_util::CommandHelper;
22 use crate::cli_util::RevisionArg;
23 use crate::cli_util::print_unmatched_explicit_paths;
24 use crate::command_error::CommandError;
25 use crate::command_error::user_error;
26 use crate::complete;
27 use crate::ui::Ui;
28
29 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
30 enum ChmodMode {
31 /// Make a path non-executable (alias: normal)
32 // We use short names for enum values so that errors say that the possible values are `n, x`.
33 #[value(name = "n", alias("normal"))]
34 Normal,
35
36 /// Make a path executable (alias: executable)
37 #[value(name = "x", alias("executable"))]
38 Executable,
39 }
40
41 /// Sets or removes the executable bit for paths in the repo
42 ///
43 /// Unlike the POSIX `chmod`, `jj file chmod` also works on Windows, on
44 /// conflicted files, and on arbitrary revisions.
45 #[derive(clap::Args, Clone, Debug)]
46 pub(crate) struct FileChmodArgs {
47 mode: ChmodMode,
48
49 /// The revision to update
50 #[arg(long, short, default_value = "@", value_name = "REVSET")]
51 #[arg(add = ArgValueCompleter::new(complete::revset_expression_mutable))]
52 revision: RevisionArg,
53
54 /// Paths to change the executable bit for
55 #[arg(required = true, 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_chmod(
62 ui: &mut Ui,
63 command: &CommandHelper,
64 args: &FileChmodArgs,
65 ) -> Result<(), CommandError> {
66 let executable_bit = match args.mode {
67 ChmodMode::Executable => true,
68 ChmodMode::Normal => false,
69 };
70
71 let mut workspace_command = command.workspace_helper(ui).await?;
72 let commit = workspace_command
73 .resolve_single_rev(ui, &args.revision)
74 .await?;
75 workspace_command.check_rewritable([commit.id()]).await?;
76 let tree = commit.tree();
77 // TODO: No need to add special case for empty paths when switching to
78 // parse_union_filesets(). paths = [] should be "none()" if supported.
79 let fileset_expression = workspace_command.parse_file_patterns(ui, &args.paths)?;
80 let matcher = fileset_expression.to_matcher();
81 print_unmatched_explicit_paths(ui, &workspace_command, &fileset_expression, [&tree]).await?;
82
83 let mut tx = workspace_command.start_transaction();
84
85 let mut tree_builder = MergedTreeBuilder::new(commit.tree());
86 for (repo_path, result) in tree.entries_matching(matcher.as_ref()) {
87 let mut tree_value = result?;
88 let user_error_with_path = |msg: &str| {
89 user_error(format!(
90 "{msg} at '{}'.",
91 tx.base_workspace_helper().format_file_path(&repo_path)
92 ))
93 };
94 let all_files = tree_value
95 .adds()
96 .flatten()
97 .all(|tree_value| matches!(tree_value, TreeValue::File { .. }));
98 if !all_files {
99 let message = if tree_value.is_resolved() {
100 "Found neither a file nor a conflict"
101 } else {
102 "Some of the sides of the conflict are not files"
103 };
104 return Err(user_error_with_path(message));
105 }
106 for value in tree_value.iter_mut().flatten() {
107 if let TreeValue::File {
108 id: _,
109 executable,
110 copy_id: _,
111 } = value
112 {
113 *executable = executable_bit;
114 }
115 }
116 tree_builder.set_or_remove(repo_path, tree_value);
117 }
118
119 let new_tree = tree_builder.write_tree().await?;
120 tx.repo_mut()
121 .rewrite_commit(&commit)
122 .set_tree(new_tree)
123 .write()
124 .await?;
125 tx.finish(
126 ui,
127 format!(
128 "make paths {} in commit {}",
129 if executable_bit {
130 "executable"
131 } else {
132 "non-executable"
133 },
134 commit.id().hex(),
135 ),
136 )
137 .await
138 }