Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aria/contents/docs/libra/command/commit/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ If the `--allow-empty` option is not given, the commit will be aborted if the in
- `--conventional`
Check if the commit message is conventional format, if not, the commit will be aborted.
- `-h`, `--help` Print help
- `-s`,`--signoff` Add sign-off-by trailer by the committer at the end of the commit message.

<Note type="note" title="Note">
The commit message should align with Git’s behavior as closely as possible, excpet the `--conventional` option, which is not supported in Git. Refs https://git-scm.com/docs/git-commit for more information.
Expand Down
46 changes: 43 additions & 3 deletions libra/src/command/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashSet, path::PathBuf};

use crate::command::load_object;
use crate::internal::branch::Branch;
use crate::internal::config::Config as UserConfig;
use crate::internal::head::Head;
use crate::utils::client_storage::ClientStorage;
use crate::utils::path;
Expand All @@ -17,7 +18,7 @@ use mercury::internal::object::ObjectTrait;

use super::save_object;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Default)]
pub struct CommitArgs {
#[arg(short, long)]
pub message: String,
Expand All @@ -33,6 +34,10 @@ pub struct CommitArgs {
/// amend the last commit
#[arg(long)]
pub amend: bool,

/// add signed-off-by line at the end of the commit message
#[arg(short = 's', long)]
pub signoff: bool,
}

pub async fn execute(args: CommitArgs) {
Expand All @@ -43,7 +48,26 @@ pub async fn execute(args: CommitArgs) {
if tracked_entries.is_empty() && !args.allow_empty {
panic!("fatal: no changes added to commit, use --allow-empty to override");
}
if args.conventional && !check_conventional_commits_message(&args.message) {

//Prepare commit message
let commit_message = if args.signoff {

Copilot AI Jul 12, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You compute commit_message with the signoff, but later calls to format_commit_msg(&args.message, ...) still use args.message instead of commit_message. You need to pass commit_message into your commit creation functions so the signoff actually appears in the saved commit object.

Copilot uses AI. Check for mistakes.
// get user
let user_name = UserConfig::get("user", None, "name")
.await
.unwrap_or_else(|| "unknown".to_string());
let user_email = UserConfig::get("user", None, "email")
.await
.unwrap_or_else(|| "unknown".to_string());

// get sign line
let signoff_line = format!("Signed-off-by: {user_name} <{user_email}>");
format!("{}\n\n{signoff_line}", args.message)
} else {
args.message.clone()
};

// check format(if needed)
if args.conventional && !check_conventional_commits_message(&commit_message) {
Comment thread
genedna marked this conversation as resolved.
panic!("fatal: commit message does not follow conventional commits");
}

Expand Down Expand Up @@ -208,6 +232,7 @@ mod test {
use crate::utils::test::*;

use super::*;

#[test]
///Testing basic parameter parsing functionality.
fn test_parse_args() {
Expand All @@ -231,10 +256,25 @@ mod test {

let args = CommitArgs::try_parse_from(["commit", "-m", "init", "--allow-empty", "--amend"]);
assert!(args.is_ok());

let args = CommitArgs::try_parse_from(["commit", "-m", "init", "-s"]);
assert!(args.is_ok());
assert!(args.unwrap().signoff);

let args = CommitArgs::try_parse_from(["commit", "-m", "init", "--signoff"]);
assert!(args.is_ok());
assert!(args.unwrap().signoff);

let args = CommitArgs::try_parse_from(["commit", "-m", "init", "--amend", "--signoff"]);
assert!(args.is_ok());
let args = args.unwrap();
assert!(args.amend);
assert!(args.signoff);

}

#[tokio::test]
#[serial]
#[serial]
/// Tests the recursive tree creation from index entries.
/// Verifies that tree objects are correctly created, saved to storage, and properly organized in a hierarchical structure.
async fn test_create_tree() {
Expand Down
4 changes: 4 additions & 0 deletions libra/tests/command/branch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn test_branch() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(commit_args).await;
let first_commit_id = Branch::find_branch("master", None).await.unwrap().commit;
Expand All @@ -25,6 +26,7 @@ async fn test_branch() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(commit_args).await;
let second_commit_id = Branch::find_branch("master", None).await.unwrap().commit;
Expand Down Expand Up @@ -109,6 +111,7 @@ async fn test_create_branch_from_remote() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;
let hash = Head::current_commit().await.unwrap();
Expand Down Expand Up @@ -146,6 +149,7 @@ async fn test_invalid_branch_name() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;

Expand Down
1 change: 1 addition & 0 deletions libra/tests/command/checkout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async fn test_checkout_module_functions() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(commit_args).await;

Expand Down
5 changes: 5 additions & 0 deletions libra/tests/command/commit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async fn test_execute_commit_with_empty_index_fail() {
allow_empty: false,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;
}
Expand All @@ -39,6 +40,7 @@ async fn test_execute_commit() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;

Expand All @@ -63,6 +65,7 @@ async fn test_execute_commit() {
allow_empty: true,
conventional: false,
amend: true,
signoff: false,
};
commit::execute(args).await;

Expand Down Expand Up @@ -103,6 +106,7 @@ async fn test_execute_commit() {
allow_empty: false,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;

Expand Down Expand Up @@ -130,6 +134,7 @@ async fn test_execute_commit() {
allow_empty: true,
conventional: false,
amend: true,
signoff: false,
};
commit::execute(args).await;

Expand Down
4 changes: 4 additions & 0 deletions libra/tests/command/switch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn test_switch_function() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;
}
Expand Down Expand Up @@ -82,6 +83,7 @@ async fn test_switch_function() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;

Expand Down Expand Up @@ -157,6 +159,7 @@ async fn test_detach_head_basic() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;
}
Expand Down Expand Up @@ -195,6 +198,7 @@ async fn test_detach_head_basic() {
allow_empty: true,
conventional: false,
amend: false,
signoff: false,
};
commit::execute(args).await;
}
Expand Down
Loading