| 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 std::io; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use jj_lib::commit::Commit; |
| 19 |
use jj_lib::config::ConfigNamePathBuf; |
| 20 |
use jj_lib::config::ConfigValue; |
| 21 |
use jj_lib::repo::Repo as _; |
| 22 |
use tracing::instrument; |
| 23 |
|
| 24 |
use super::ConfigTargetArgs; |
| 25 |
use crate::cli_util::CommandHelper; |
| 26 |
use crate::cli_util::WorkspaceCommandHelper; |
| 27 |
use crate::command_error::CommandError; |
| 28 |
use crate::command_error::user_error_with_message; |
| 29 |
use crate::complete; |
| 30 |
use crate::config::parse_value_or_bare_string; |
| 31 |
use crate::ui::Ui; |
| 32 |
|
| 33 |
/// Update a config file to set the given option to a given value. |
| 34 |
#[derive(clap::Args, Clone, Debug)] |
| 35 |
pub struct ConfigSetArgs { |
| 36 |
#[arg(required = true)] |
| 37 |
#[arg(add = ArgValueCandidates::new(complete::leaf_config_keys))] |
| 38 |
name: ConfigNamePathBuf, |
| 39 |
|
| 40 |
/// New value to set |
| 41 |
/// |
| 42 |
/// The value should be specified as a TOML expression. If string value |
| 43 |
/// isn't enclosed by any TOML constructs (such as apostrophes or array |
| 44 |
/// notation), quotes can be omitted. Note that the value may also need |
| 45 |
/// shell quoting. TOML multi-line strings can be useful if the value |
| 46 |
/// contains apostrophes. For example, to set `foo.bar` to the string |
| 47 |
/// "{don't}" use `jj config set --user foo.bar "'''{don't}'''"`. This is |
| 48 |
/// valid in both Bash and Fish. |
| 49 |
/// |
| 50 |
/// Alternative, e.g. to avoid dealing with shell quoting, use `jj config |
| 51 |
/// edit` to edit the TOML file directly. |
| 52 |
#[arg(required = true, value_parser = parse_value_or_bare_string)] |
| 53 |
value: ConfigValue, |
| 54 |
|
| 55 |
#[command(flatten)] |
| 56 |
target: ConfigTargetArgs, |
| 57 |
} |
| 58 |
|
| 59 |
/// Denotes a type of author change |
| 60 |
enum AuthorChange { |
| 61 |
Name, |
| 62 |
Email, |
| 63 |
} |
| 64 |
|
| 65 |
#[instrument(skip_all)] |
| 66 |
pub async fn cmd_config_set( |
| 67 |
ui: &mut Ui, |
| 68 |
command: &CommandHelper, |
| 69 |
args: &ConfigSetArgs, |
| 70 |
) -> Result<(), CommandError> { |
| 71 |
let mut file = args.target.edit_config_file(ui, command)?; |
| 72 |
|
| 73 |
// If the user is trying to change the author config, we should warn them |
| 74 |
// that it won't affect the working copy author |
| 75 |
if args.name == ConfigNamePathBuf::from_iter(vec!["user", "name"]) { |
| 76 |
check_wc_author(ui, command, &args.value, AuthorChange::Name).await?; |
| 77 |
} else if args.name == ConfigNamePathBuf::from_iter(vec!["user", "email"]) { |
| 78 |
check_wc_author(ui, command, &args.value, AuthorChange::Email).await?; |
| 79 |
} |
| 80 |
|
| 81 |
file.set_value(&args.name, &args.value) |
| 82 |
.map_err(|err| user_error_with_message(format!("Failed to set {}", args.name), err))?; |
| 83 |
file.save()?; |
| 84 |
Ok(()) |
| 85 |
} |
| 86 |
|
| 87 |
/// Returns the commit of the working copy if it exists. |
| 88 |
async fn maybe_wc_commit(helper: &WorkspaceCommandHelper) -> Option<Commit> { |
| 89 |
let repo = helper.repo(); |
| 90 |
let id = helper.get_wc_commit_id()?; |
| 91 |
repo.store().get_commit_async(id).await.ok() |
| 92 |
} |
| 93 |
|
| 94 |
/// Check if the working copy author name matches the user's config value |
| 95 |
/// If it doesn't, print a warning message |
| 96 |
async fn check_wc_author( |
| 97 |
ui: &mut Ui, |
| 98 |
command: &CommandHelper, |
| 99 |
new_value: &toml_edit::Value, |
| 100 |
author_change: AuthorChange, |
| 101 |
) -> io::Result<()> { |
| 102 |
let Ok(helper) = command.workspace_helper(ui).await else { |
| 103 |
// config set should work even if cwd isn't a jj repo |
| 104 |
return Ok(()); |
| 105 |
}; |
| 106 |
if let Some(wc_commit) = maybe_wc_commit(&helper).await { |
| 107 |
let author = wc_commit.author(); |
| 108 |
let orig_value = match author_change { |
| 109 |
AuthorChange::Name => &author.name, |
| 110 |
AuthorChange::Email => &author.email, |
| 111 |
}; |
| 112 |
if new_value.as_str() != Some(orig_value) { |
| 113 |
warn_wc_author(ui, &author.name, &author.email)?; |
| 114 |
} |
| 115 |
} |
| 116 |
Ok(()) |
| 117 |
} |
| 118 |
|
| 119 |
/// Prints a warning message about the working copy to the user |
| 120 |
fn warn_wc_author(ui: &Ui, user_name: &str, user_email: &str) -> io::Result<()> { |
| 121 |
Ok(writeln!( |
| 122 |
ui.warning_default(), |
| 123 |
"This setting will only impact future commits.\nThe author of the working copy will stay \ |
| 124 |
\"{user_name} <{user_email}>\".\nTo change the working copy author, use \"jj metaedit \ |
| 125 |
--update-author\"." |
| 126 |
)?) |
| 127 |
} |