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
2 changes: 2 additions & 0 deletions aria/contents/docs/libra/command/log/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ List commits that are reachable by current local branch. The order is from the l

- `-n`, `--number <NUMBER>`
Limit the number of output
- `--oneline`
Shorthand for `--pretty=oneline --abbrev-commit`. Display each commit as a single line with abbreviated commit hash (7 characters) and commit message first line only.
- `-h`, `--help`
Print help

Expand Down
19 changes: 14 additions & 5 deletions libra/src/command/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct LogArgs {
/// Limit the number of output
#[clap(short, long)]
pub number: Option<usize>,
/// Shorthand for --pretty=oneline --abbrev-commit
#[clap(long)]
pub oneline: bool,
}

/// Get all reachable commits from the given commit hash
Expand Down Expand Up @@ -83,7 +86,13 @@ pub async fn execute(args: LogArgs) {
break;
}
output_number += 1;
let mut message = {
let message = if args.oneline {
// Oneline format: <short_hash> <commit_message_first_line>
let short_hash = &commit.id.to_string()[..7];
let (msg, _) = parse_commit_msg(&commit.message);
format!("{} {}", short_hash.yellow(), msg)
Comment on lines +90 to +93

Copilot AI Jul 15, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the oneline formatting logic into a separate helper function (e.g., fn format_oneline(commit: &Commit) -> String) to keep execute concise and improve readability.

Suggested change
// Oneline format: <short_hash> <commit_message_first_line>
let short_hash = &commit.id.to_string()[..7];
let (msg, _) = parse_commit_msg(&commit.message);
format!("{} {}", short_hash.yellow(), msg)
format_oneline(&commit)

Copilot uses AI. Check for mistakes.
} else {
// Default detailed format
let mut message = format!("{} {}", "commit".yellow(), &commit.id.to_string().yellow());

// TODO other branch's head should shown branch name
Expand All @@ -96,11 +105,11 @@ pub async fn execute(args: LogArgs) {
}
message = format!("{}{}", message, ")".yellow());
}
message.push_str(&format!("\nAuthor: {}", commit.author));
let (msg, _) = parse_commit_msg(&commit.message);
message.push_str(&format!("\n{msg}\n"));
message
};
message.push_str(&format!("\nAuthor: {}", commit.author));
let (msg, _) = parse_commit_msg(&commit.message);
message.push_str(&format!("\n{msg}\n"));

#[cfg(unix)]
{
Expand All @@ -112,7 +121,7 @@ pub async fn execute(args: LogArgs) {
}
#[cfg(not(unix))]
{
println!("{}", message);
println!("{message}");
}
}
#[cfg(unix)]
Expand Down
39 changes: 39 additions & 0 deletions libra/tests/command/log_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,42 @@ async fn create_test_commit_tree() -> String {

commit_6.id.to_string()
}

#[tokio::test]
#[serial]
/// Tests log command with --oneline parameter
async fn test_log_oneline() {
let temp_path = tempdir().unwrap();
test::setup_with_new_libra_in(temp_path.path()).await;
let _guard = ChangeDirGuard::new(temp_path.path());

// Create test commits
let commit_id = create_test_commit_tree().await;
let reachable_commits = get_reachable_commits(commit_id).await;

// Test oneline format
let args = LogArgs {
number: Some(3),
oneline: true
};

// Since execute function writes to stdout, we'll test the logic directly
let mut sorted_commits = reachable_commits.clone();
sorted_commits.sort_by(|a, b| b.committer.timestamp.cmp(&a.committer.timestamp));

let max_commits = std::cmp::min(args.number.unwrap_or(usize::MAX), sorted_commits.len());

for (i, commit) in sorted_commits.iter().take(max_commits).enumerate() {
// Test short hash format (should be 7 characters)
let short_hash = &commit.id.to_string()[..7];
assert_eq!(short_hash.len(), 7);

// Test that commit message parsing works
let (msg, _) = common::utils::parse_commit_msg(&commit.message);
assert!(!msg.is_empty());

// For our test commits, verify the expected format
let expected_number = 6 - i; // commits are numbered 6, 5, 4, 3, 2, 1
assert_eq!(msg.trim(), format!("Commit_{expected_number}"));
}
}
2 changes: 1 addition & 1 deletion libra/tests/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use libra::command::branch::BranchArgs;
use libra::command::get_target_commit;
use libra::command::init::init;
use libra::command::init::InitArgs;
use libra::command::log::get_reachable_commits;
use libra::command::log::{get_reachable_commits, LogArgs};
use libra::command::save_object;
use libra::command::status::changes_to_be_staged;
use libra::command::switch::{self, check_status, SwitchArgs};
Expand Down
Loading