|
| 1 | +// Copyright 2025 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::path::PathBuf; |
| 16 | + |
| 17 | +use tracing::instrument; |
| 18 | + |
| 19 | +use crate::cli_util::CommandHelper; |
| 20 | +use crate::command_error::CommandError; |
| 21 | +use crate::command_error::internal_error_with_message; |
| 22 | +use crate::command_error::user_error; |
| 23 | +use crate::command_error::user_error_with_message; |
| 24 | +use crate::config::maybe_read; |
| 25 | +use crate::merge_tools::make_diff_sections; |
| 26 | +use crate::ui::Ui; |
| 27 | + |
| 28 | +/// Reviews and updates configuration stored in version control. |
| 29 | +/// You should never need to run this command unless jj tells you to. |
| 30 | +/// This command needs to be run when the config checked in to the repo is |
| 31 | +/// changed, and allows you to approve or reject said changes on a line-by-line |
| 32 | +/// basis. |
| 33 | +#[derive(clap::Args, Clone, Debug)] |
| 34 | +pub struct ConfigReviewManagedArgs { |
| 35 | + /// Trust the repository's config and skip review of it. |
| 36 | + /// Use this when you absolutely trust the repo config (eg. you're the only |
| 37 | + /// contributor). |
| 38 | + #[arg(long)] |
| 39 | + trust: bool, |
| 40 | +} |
| 41 | + |
| 42 | +#[instrument(skip_all)] |
| 43 | +pub fn cmd_review_managed( |
| 44 | + ui: &mut Ui, |
| 45 | + command: &CommandHelper, |
| 46 | + args: &ConfigReviewManagedArgs, |
| 47 | +) -> Result<(), CommandError> { |
| 48 | + // It'd be nice if we were able to just error out here. |
| 49 | + // But in the event that a user disables their repo-managed-config in their |
| 50 | + // repo-managed-config, that would leave no way to re-enable it easily. |
| 51 | + if !command |
| 52 | + .raw_config() |
| 53 | + .as_ref() |
| 54 | + .get("repo-managed-config.enabled")? |
| 55 | + { |
| 56 | + writeln!(ui.warning_default(), "repo-managed-config is disabled.")?; |
| 57 | + writeln!( |
| 58 | + ui.hint_default(), |
| 59 | + "Enable it with `jj config set <--user|--repo> repo-managed-config.enabled true`" |
| 60 | + )?; |
| 61 | + } |
| 62 | + if let Some(paths) = command.config_env().repo_managed_config_paths() { |
| 63 | + let workspace_command = command.workspace_helper(ui)?; |
| 64 | + let path_converter = workspace_command.path_converter(); |
| 65 | + let vcs = maybe_read( |
| 66 | + &paths |
| 67 | + .managed |
| 68 | + .to_fs_path(workspace_command.workspace_root()) |
| 69 | + .map_err(|e| { |
| 70 | + internal_error_with_message("Managed path is not a valid FS path", e) |
| 71 | + })?, |
| 72 | + )? |
| 73 | + .unwrap_or_default(); |
| 74 | + let config = maybe_read(&paths.config)?.unwrap_or_default(); |
| 75 | + |
| 76 | + if vcs.is_empty() { |
| 77 | + // We don't need the user to review this since it's not a security issue. |
| 78 | + writeln!( |
| 79 | + ui.status(), |
| 80 | + "The config file has been removed from the VCS, so we have removed the local copy \ |
| 81 | + too." |
| 82 | + )?; |
| 83 | + std::fs::remove_file(paths.config)?; |
| 84 | + std::fs::remove_file(paths.last_reviewed)?; |
| 85 | + return Ok(()); |
| 86 | + } |
| 87 | + |
| 88 | + if config == vcs { |
| 89 | + writeln!(ui.status(), "Your config file is already up to date")?; |
| 90 | + return Ok(()); |
| 91 | + } |
| 92 | + |
| 93 | + let new_config = if args.trust { |
| 94 | + vcs.clone() |
| 95 | + } else { |
| 96 | + let sections = make_diff_sections( |
| 97 | + &String::from_utf8(config).map_err(|e| { |
| 98 | + user_error_with_message("Currently applied config was not utf-8", e) |
| 99 | + })?, |
| 100 | + &String::from_utf8(vcs.clone()).map_err(|e| { |
| 101 | + user_error_with_message("Config stored in VCS was not utf-8", e) |
| 102 | + })?, |
| 103 | + ) |
| 104 | + .map_err(|e| internal_error_with_message("Failed to create diff sections", e))?; |
| 105 | + // Ideally we'd use the user's chosen diff selector, but that |
| 106 | + // heavily relies on jj's objects such as Tree and Store. |
| 107 | + let managed_path = PathBuf::from(path_converter.format_file_path(&paths.managed)); |
| 108 | + let recorded = scm_record::Recorder::new( |
| 109 | + scm_record::RecordState { |
| 110 | + is_read_only: false, |
| 111 | + commits: vec![], |
| 112 | + files: vec![scm_record::File { |
| 113 | + old_path: None, |
| 114 | + path: std::borrow::Cow::Borrowed(&managed_path), |
| 115 | + // This doesn't do anything. |
| 116 | + file_mode: scm_record::FileMode::Unix(0o777), |
| 117 | + sections, |
| 118 | + }], |
| 119 | + }, |
| 120 | + &mut scm_record::helpers::CrosstermInput, |
| 121 | + ) |
| 122 | + .run() |
| 123 | + .map_err(|_| user_error("Failed to select changes"))?; |
| 124 | + |
| 125 | + // There's always precisely one file. |
| 126 | + reconstruct(&recorded.files[0].sections).into_bytes() |
| 127 | + }; |
| 128 | + std::fs::write(paths.config, new_config)?; |
| 129 | + std::fs::write(paths.last_reviewed, vcs)?; |
| 130 | + writeln!(ui.status(), "Updated repo config file")?; |
| 131 | + Ok(()) |
| 132 | + } else { |
| 133 | + Err(user_error( |
| 134 | + "Unable to detect location of config files. Are you in a repo?", |
| 135 | + )) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +fn reconstruct(sections: &[scm_record::Section]) -> String { |
| 140 | + let mut out: Vec<&str> = Default::default(); |
| 141 | + for section in sections { |
| 142 | + match section { |
| 143 | + scm_record::Section::Unchanged { lines } => out.extend(lines.iter().map(AsRef::as_ref)), |
| 144 | + scm_record::Section::Changed { lines } => { |
| 145 | + for line in lines { |
| 146 | + if line.is_checked == (line.change_type == scm_record::ChangeType::Added) { |
| 147 | + out.push(&line.line); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + _ => {} |
| 152 | + } |
| 153 | + } |
| 154 | + out.join("") |
| 155 | +} |
0 commit comments