-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add
--author
argument for commit
and describe
- Loading branch information
Showing
5 changed files
with
78 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ use crate::command_error::CommandError; | |
use crate::description_util::description_template; | ||
use crate::description_util::edit_description; | ||
use crate::description_util::join_message_paragraphs; | ||
use crate::description_util::parse_user_name_email; | ||
use crate::ui::Ui; | ||
|
||
/// Update the description and create a new change on top. | ||
|
@@ -50,6 +51,11 @@ pub(crate) struct CommitArgs { | |
/// $ JJ_USER='Foo Bar' [email protected] jj commit --reset-author | ||
#[arg(long)] | ||
reset_author: bool, | ||
/// Set author to the provided string | ||
/// | ||
/// This changes author name and email while retaining timestamp. | ||
#[arg(long, conflicts_with = "reset_author")] | ||
author: Option<String>, | ||
} | ||
|
||
#[instrument(skip_all)] | ||
|
@@ -106,6 +112,15 @@ new working-copy commit. | |
if args.reset_author { | ||
commit_builder.set_author(commit_builder.committer().clone()); | ||
} | ||
if let Some(author) = &args.author { | ||
let (name, email) = parse_user_name_email(author)?; | ||
let new_author = jj_lib::backend::Signature { | ||
name, | ||
email, | ||
timestamp: commit_builder.author().timestamp.clone(), | ||
}; | ||
commit_builder.set_author(new_author); | ||
} | ||
|
||
let description = if !args.message_paragraphs.is_empty() { | ||
join_message_paragraphs(&args.message_paragraphs) | ||
|
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ use crate::description_util::description_template; | |
use crate::description_util::edit_description; | ||
use crate::description_util::edit_multiple_descriptions; | ||
use crate::description_util::join_message_paragraphs; | ||
use crate::description_util::parse_user_name_email; | ||
use crate::description_util::ParsedBulkEditMessage; | ||
use crate::ui::Ui; | ||
|
||
|
@@ -72,6 +73,11 @@ pub(crate) struct DescribeArgs { | |
/// $ JJ_USER='Foo Bar' [email protected] jj describe --reset-author | ||
#[arg(long)] | ||
reset_author: bool, | ||
/// Set author to the provided string | ||
/// | ||
/// This changes author name and email while retaining timestamp. | ||
#[arg(long, conflicts_with = "reset_author")] | ||
author: Option<String>, | ||
} | ||
|
||
#[instrument(skip_all)] | ||
|
@@ -139,6 +145,15 @@ pub(crate) fn cmd_describe( | |
let new_author = commit_builder.committer().clone(); | ||
commit_builder.set_author(new_author); | ||
} | ||
if let Some(author) = &args.author { | ||
let (name, email) = parse_user_name_email(author)?; | ||
let new_author = jj_lib::backend::Signature { | ||
name, | ||
email, | ||
timestamp: commit_builder.author().timestamp.clone(), | ||
}; | ||
commit_builder.set_author(new_author); | ||
} | ||
let temp_commit = commit_builder.write_hidden()?; | ||
Ok((commit.id(), temp_commit)) | ||
}) | ||
|
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ use crate::cli_util::edit_temp_file; | |
use crate::cli_util::short_commit_hash; | ||
use crate::cli_util::WorkspaceCommandHelper; | ||
use crate::cli_util::WorkspaceCommandTransaction; | ||
use crate::command_error::cli_error; | ||
use crate::command_error::CommandError; | ||
use crate::formatter::PlainTextFormatter; | ||
use crate::text_util; | ||
|
@@ -251,13 +252,21 @@ pub fn description_template( | |
Ok(output.into_string_lossy()) | ||
} | ||
|
||
pub fn parse_user_name_email(author: &str) -> Result<(String, String), CommandError> { | ||
let re = regex::Regex::new(r"(?<name>.*?)\s*<(?<email>.+)>").unwrap(); | ||
let captures = re | ||
.captures(author) | ||
.ok_or_else(|| cli_error("Invalid author string"))?; | ||
Ok((captures["name"].to_string(), captures["email"].to_string())) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use indexmap::indexmap; | ||
use indoc::indoc; | ||
use maplit::hashmap; | ||
|
||
use super::parse_bulk_edit_message; | ||
use super::{parse_bulk_edit_message, parse_user_name_email}; | ||
use crate::description_util::ParseBulkEditMessageError; | ||
|
||
#[test] | ||
|
@@ -410,4 +419,33 @@ mod tests { | |
assert!(result.duplicates.is_empty()); | ||
assert!(result.unexpected.is_empty()); | ||
} | ||
|
||
#[test] | ||
fn test_parse_author() { | ||
let expected_name = "Example"; | ||
let expected_email = "[email protected]"; | ||
let parsed = parse_user_name_email(&format!("{expected_name} <{expected_email}>")).unwrap(); | ||
assert_eq!( | ||
(expected_name.to_string(), expected_email.to_string()), | ||
parsed | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_author_with_utf8() { | ||
let expected_name = "Ąćęłńóśżź"; | ||
let expected_email = "[email protected]"; | ||
let parsed = parse_user_name_email(&format!("{expected_name} <{expected_email}>")).unwrap(); | ||
assert_eq!( | ||
(expected_name.to_string(), expected_email.to_string()), | ||
parsed | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_author_without_name() { | ||
let expected_email = "[email protected]"; | ||
let parsed = parse_user_name_email(&format!("<{expected_email}>")).unwrap(); | ||
assert_eq!(("".to_string(), expected_email.to_string()), parsed); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -446,6 +446,9 @@ Update the description and create a new change on top | |
You can use it in combination with the JJ_USER and JJ_EMAIL environment variables to set a different author: | ||
$ JJ_USER='Foo Bar' [email protected] jj commit --reset-author | ||
* `--author <AUTHOR>` — Set author to the provided string | ||
This changes author name and email while retaining timestamp. | ||
|
@@ -599,6 +602,9 @@ Starts an editor to let you edit the description of changes. The editor will be | |
You can use it in combination with the JJ_USER and JJ_EMAIL environment variables to set a different author: | ||
$ JJ_USER='Foo Bar' [email protected] jj describe --reset-author | ||
* `--author <AUTHOR>` — Set author to the provided string | ||
This changes author name and email while retaining timestamp. | ||
|