-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: move description utils to description_util.rs
- Loading branch information
1 parent
f1898a3
commit afc2382
Showing
9 changed files
with
126 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::io::Write; | ||
use std::{fs, io}; | ||
|
||
use itertools::Itertools; | ||
use jj_lib::commit::Commit; | ||
use jj_lib::matchers::EverythingMatcher; | ||
use jj_lib::repo::ReadonlyRepo; | ||
use jj_lib::settings::UserSettings; | ||
|
||
use crate::cli_util::{run_ui_editor, user_error, CommandError, WorkspaceCommandHelper}; | ||
use crate::diff_util::{self, DiffFormat}; | ||
use crate::formatter::PlainTextFormatter; | ||
use crate::text_util; | ||
use crate::ui::Ui; | ||
|
||
pub fn edit_description( | ||
repo: &ReadonlyRepo, | ||
description: &str, | ||
settings: &UserSettings, | ||
) -> Result<String, CommandError> { | ||
let description_file_path = (|| -> Result<_, io::Error> { | ||
let mut file = tempfile::Builder::new() | ||
.prefix("editor-") | ||
.suffix(".jjdescription") | ||
.tempfile_in(repo.repo_path())?; | ||
file.write_all(description.as_bytes())?; | ||
file.write_all(b"\nJJ: Lines starting with \"JJ: \" (like this one) will be removed.\n")?; | ||
let (_, path) = file.keep().map_err(|e| e.error)?; | ||
Ok(path) | ||
})() | ||
.map_err(|e| { | ||
user_error(format!( | ||
r#"Failed to create description file in "{path}": {e}"#, | ||
path = repo.repo_path().display() | ||
)) | ||
})?; | ||
|
||
run_ui_editor(settings, &description_file_path)?; | ||
|
||
let description = fs::read_to_string(&description_file_path).map_err(|e| { | ||
user_error(format!( | ||
r#"Failed to read description file "{path}": {e}"#, | ||
path = description_file_path.display() | ||
)) | ||
})?; | ||
// Delete the file only if everything went well. | ||
// TODO: Tell the user the name of the file we left behind. | ||
std::fs::remove_file(description_file_path).ok(); | ||
// Normalize line ending, remove leading and trailing blank lines. | ||
let description = description | ||
.lines() | ||
.filter(|line| !line.starts_with("JJ: ")) | ||
.join("\n"); | ||
Ok(text_util::complete_newline(description.trim_matches('\n'))) | ||
} | ||
|
||
pub fn combine_messages( | ||
repo: &ReadonlyRepo, | ||
source: &Commit, | ||
destination: &Commit, | ||
settings: &UserSettings, | ||
abandon_source: bool, | ||
) -> Result<String, CommandError> { | ||
let description = if abandon_source { | ||
if source.description().is_empty() { | ||
destination.description().to_string() | ||
} else if destination.description().is_empty() { | ||
source.description().to_string() | ||
} else { | ||
let combined = "JJ: Enter a description for the combined commit.\n".to_string() | ||
+ "JJ: Description from the destination commit:\n" | ||
+ destination.description() | ||
+ "\nJJ: Description from the source commit:\n" | ||
+ source.description(); | ||
edit_description(repo, &combined, settings)? | ||
} | ||
} else { | ||
destination.description().to_string() | ||
}; | ||
Ok(description) | ||
} | ||
|
||
pub fn description_template_for_commit( | ||
ui: &Ui, | ||
settings: &UserSettings, | ||
workspace_command: &WorkspaceCommandHelper, | ||
commit: &Commit, | ||
) -> Result<String, CommandError> { | ||
let mut diff_summary_bytes = Vec::new(); | ||
diff_util::show_patch( | ||
ui, | ||
&mut PlainTextFormatter::new(&mut diff_summary_bytes), | ||
workspace_command, | ||
commit, | ||
&EverythingMatcher, | ||
&[DiffFormat::Summary], | ||
)?; | ||
let description = if commit.description().is_empty() { | ||
settings.default_description() | ||
} else { | ||
commit.description().to_owned() | ||
}; | ||
if diff_summary_bytes.is_empty() { | ||
Ok(description) | ||
} else { | ||
Ok(description + "\n" + &diff_summary_to_description(&diff_summary_bytes)) | ||
} | ||
} | ||
|
||
pub fn diff_summary_to_description(bytes: &[u8]) -> String { | ||
let text = std::str::from_utf8(bytes).expect( | ||
"Summary diffs and repo paths must always be valid UTF8.", | ||
// Double-check this assumption for diffs that include file content. | ||
); | ||
"JJ: This commit contains the following changes:\n".to_owned() | ||
+ &textwrap::indent(text, "JJ: ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters