Skip to content

Commit

Permalink
cli: add --author argument for commit and describe
Browse files Browse the repository at this point in the history
  • Loading branch information
mati865 committed Sep 14, 2024
1 parent 6e72b1c commit 01074ab
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
allows to set a name for the remote instead of using the default
`origin`.

* `jj commit` and `jj describe` now accept `--author` option allowing to quickly change
author of given commit.

### Fixed bugs

* Fixed panic when parsing invalid conflict markers of a particular form.
Expand Down
15 changes: 15 additions & 0 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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))
})
Expand Down
39 changes: 39 additions & 0 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -251,13 +252,22 @@ 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_user_name_email;
use crate::description_util::ParseBulkEditMessageError;

#[test]
Expand Down Expand Up @@ -410,4 +420,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);
}
}
6 changes: 6 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 01074ab

Please sign in to comment.