| 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::fmt::Write as _; |
| 16 |
use std::path::Path; |
| 17 |
|
| 18 |
use itertools::Itertools as _; |
| 19 |
use jj_lib::repo_path::RepoPathBuf; |
| 20 |
use tracing::instrument; |
| 21 |
|
| 22 |
use super::update_sparse_patterns_with; |
| 23 |
use crate::cli_util::CommandHelper; |
| 24 |
use crate::command_error::CommandError; |
| 25 |
use crate::command_error::internal_error; |
| 26 |
use crate::command_error::user_error_with_message; |
| 27 |
use crate::description_util::TextEditor; |
| 28 |
use crate::ui::Ui; |
| 29 |
|
| 30 |
/// Start an editor to update the patterns that are present in the working copy |
| 31 |
#[derive(clap::Args, Clone, Debug)] |
| 32 |
pub struct SparseEditArgs {} |
| 33 |
|
| 34 |
#[instrument(skip_all)] |
| 35 |
pub async fn cmd_sparse_edit( |
| 36 |
ui: &mut Ui, |
| 37 |
command: &CommandHelper, |
| 38 |
_args: &SparseEditArgs, |
| 39 |
) -> Result<(), CommandError> { |
| 40 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 41 |
let editor = workspace_command.text_editor()?; |
| 42 |
update_sparse_patterns_with(ui, &mut workspace_command, |_ui, old_patterns| { |
| 43 |
let mut new_patterns = edit_sparse(&editor, old_patterns)?; |
| 44 |
new_patterns.sort_unstable(); |
| 45 |
new_patterns.dedup(); |
| 46 |
Ok(new_patterns) |
| 47 |
}) |
| 48 |
.await |
| 49 |
} |
| 50 |
|
| 51 |
fn edit_sparse( |
| 52 |
editor: &TextEditor, |
| 53 |
sparse: &[RepoPathBuf], |
| 54 |
) -> Result<Vec<RepoPathBuf>, CommandError> { |
| 55 |
let mut content = String::new(); |
| 56 |
for sparse_path in sparse { |
| 57 |
// Invalid path shouldn't block editing. Edited paths will be validated. |
| 58 |
let workspace_relative_sparse_path = sparse_path.to_fs_path_unchecked(Path::new("")); |
| 59 |
let path_string = workspace_relative_sparse_path.to_str().ok_or_else(|| { |
| 60 |
internal_error(format!( |
| 61 |
"Stored sparse path is not valid utf-8: {}", |
| 62 |
workspace_relative_sparse_path.display() |
| 63 |
)) |
| 64 |
})?; |
| 65 |
writeln!(&mut content, "{path_string}").unwrap(); |
| 66 |
} |
| 67 |
|
| 68 |
let content = editor |
| 69 |
.edit_str(content, Some(".jjsparse")) |
| 70 |
.map_err(|err| err.with_name("sparse patterns"))?; |
| 71 |
|
| 72 |
content |
| 73 |
.lines() |
| 74 |
.filter(|line| !line.starts_with("JJ:")) |
| 75 |
.map(|line| line.trim()) |
| 76 |
.filter(|line| !line.is_empty()) |
| 77 |
.map(|line| { |
| 78 |
RepoPathBuf::from_relative_path(line).map_err(|err| { |
| 79 |
user_error_with_message(format!("Failed to parse sparse pattern: {line}"), err) |
| 80 |
}) |
| 81 |
}) |
| 82 |
.try_collect() |
| 83 |
} |