-
Notifications
You must be signed in to change notification settings - Fork 79
feat(log): add --grep parameter for commit message filtering #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -96,6 +96,10 @@ pub struct LogArgs { | |||||||||||||||||||||||||||||||||||||||||||||||
| /// Files to limit diff output (used with -p, --name-only, or --stat) | ||||||||||||||||||||||||||||||||||||||||||||||||
| #[clap(value_name = "PATHS", num_args = 0..)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| pathspec: Vec<String>, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// Filter commits by message content (case-sensitive substring match) | ||||||||||||||||||||||||||||||||||||||||||||||||
| #[clap(long)] | ||||||||||||||||||||||||||||||||||||||||||||||||
| pub grep: Option<String>, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(PartialEq, Debug)] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -382,6 +386,16 @@ pub async fn execute_safe(args: LogArgs, output: &OutputConfig) -> CliResult<()> | |||||||||||||||||||||||||||||||||||||||||||||||
| // default sort with signature time | ||||||||||||||||||||||||||||||||||||||||||||||||
| reachable_commits.sort_by_key(|b| std::cmp::Reverse(b.committer.timestamp)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Apply grep filtering | ||||||||||||||||||||||||||||||||||||||||||||||||
| if let Some(pattern) = &args.grep { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if !pattern.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| reachable_commits = reachable_commits | ||||||||||||||||||||||||||||||||||||||||||||||||
| .into_iter() | ||||||||||||||||||||||||||||||||||||||||||||||||
| .filter(|commit| commit.message.contains(pattern)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
386
to
+398
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // default sort with signature time | |
| reachable_commits.sort_by_key(|b| std::cmp::Reverse(b.committer.timestamp)); | |
| // Apply grep filtering | |
| if let Some(pattern) = &args.grep { | |
| if !pattern.is_empty() { | |
| reachable_commits = reachable_commits | |
| .into_iter() | |
| .filter(|commit| commit.message.contains(pattern)) | |
| .collect(); | |
| } | |
| } | |
| // Apply grep filtering before sorting to avoid sorting commits that will be discarded. | |
| if let Some(pattern) = &args.grep { | |
| if !pattern.is_empty() { | |
| reachable_commits | |
| .retain(|commit| commit.message.contains(pattern)); | |
| } | |
| } | |
| // default sort with signature time | |
| reachable_commits.sort_by_key(|b| std::cmp::Reverse(b.committer.timestamp)); |
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue as the integration test file: LogArgs::parse_from(["libra", "log", ...]) treats the "log" token as a positional pathspec entry, not a subcommand name. That makes these tests misleading and can mask bugs. Please parse LogArgs without the extra "log" token and consider asserting pathspec is empty.
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new parsing-only tests are duplicated in both src/command/log.rs and tests/command/log_test.rs, but neither validates the actual --grep filtering behavior in execute_safe. Prefer keeping a single parsing test (if needed) and add/keep integration tests that execute libra log --grep ... and assert stdout matches expected commits.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1093,3 +1093,45 @@ async fn test_log_short_number_flag_with_double_dash_before_subcommand() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(status.success(), "libra -- log -2 failed: {err}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(count_commit_lines(&out), 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test grep parameter parsing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_log_args_grep() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log", "--grep", "fix"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, Some("fix".to_string())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1099
to
+1101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The added tests only verify Clap argument parsing and never execute Useful? React with 👍 / 👎. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, None); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1097
to
+1105
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test grep combined with other arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_grep_with_other_args() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LogArgs::parse_from(["libra", "log", "--grep", "feature", "--oneline", "-n", "5"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, Some("feature".to_string())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(args.oneline); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.number, Some(5)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test case-sensitive matching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_grep_case_sensitive() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log", "--grep", "FIX"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, Some("FIX".to_string())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test empty string grep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_grep_empty_string() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log", "--grep", ""]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, Some("".to_string())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test graph with grep combination | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn test_graph_with_grep() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log", "--graph", "--grep", "fix"]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert!(args.graph); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(args.grep, Some("fix".to_string())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1100
to
+1136
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let args = LogArgs::parse_from(["libra", "log", "--grep", "fix"]); | |
| assert_eq!(args.grep, Some("fix".to_string())); | |
| let args = LogArgs::parse_from(["libra", "log"]); | |
| assert_eq!(args.grep, None); | |
| } | |
| // Test grep combined with other arguments | |
| #[test] | |
| fn test_grep_with_other_args() { | |
| let args = | |
| LogArgs::parse_from(["libra", "log", "--grep", "feature", "--oneline", "-n", "5"]); | |
| assert_eq!(args.grep, Some("feature".to_string())); | |
| assert!(args.oneline); | |
| assert_eq!(args.number, Some(5)); | |
| } | |
| // Test case-sensitive matching | |
| #[test] | |
| fn test_grep_case_sensitive() { | |
| let args = LogArgs::parse_from(["libra", "log", "--grep", "FIX"]); | |
| assert_eq!(args.grep, Some("FIX".to_string())); | |
| } | |
| // Test empty string grep | |
| #[test] | |
| fn test_grep_empty_string() { | |
| let args = LogArgs::parse_from(["libra", "log", "--grep", ""]); | |
| assert_eq!(args.grep, Some("".to_string())); | |
| } | |
| // Test graph with grep combination | |
| #[test] | |
| fn test_graph_with_grep() { | |
| let args = LogArgs::parse_from(["libra", "log", "--graph", "--grep", "fix"]); | |
| assert!(args.graph); | |
| assert_eq!(args.grep, Some("fix".to_string())); | |
| let args = LogArgs::parse_from(["libra", "--grep", "fix"]); | |
| assert_eq!(args.grep, Some("fix".to_string())); | |
| assert!(args.pathspec.is_empty()); | |
| let args = LogArgs::parse_from(["libra"]); | |
| assert_eq!(args.grep, None); | |
| assert!(args.pathspec.is_empty()); | |
| } | |
| // Test grep combined with other arguments | |
| #[test] | |
| fn test_grep_with_other_args() { | |
| let args = | |
| LogArgs::parse_from(["libra", "--grep", "feature", "--oneline", "-n", "5"]); | |
| assert_eq!(args.grep, Some("feature".to_string())); | |
| assert!(args.oneline); | |
| assert_eq!(args.number, Some(5)); | |
| assert!(args.pathspec.is_empty()); | |
| } | |
| // Test case-sensitive matching | |
| #[test] | |
| fn test_grep_case_sensitive() { | |
| let args = LogArgs::parse_from(["libra", "--grep", "FIX"]); | |
| assert_eq!(args.grep, Some("FIX".to_string())); | |
| assert!(args.pathspec.is_empty()); | |
| } | |
| // Test empty string grep | |
| #[test] | |
| fn test_grep_empty_string() { | |
| let args = LogArgs::parse_from(["libra", "--grep", ""]); | |
| assert_eq!(args.grep, Some("".to_string())); | |
| assert!(args.pathspec.is_empty()); | |
| } | |
| // Test graph with grep combination | |
| #[test] | |
| fn test_graph_with_grep() { | |
| let args = LogArgs::parse_from(["libra", "--graph", "--grep", "fix"]); | |
| assert!(args.graph); | |
| assert_eq!(args.grep, Some("fix".to_string())); | |
| assert!(args.pathspec.is_empty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
execute_safefiltersreachable_commitsby--grepbeforedefault_abbrevis computed, so abbreviation length is now derived from only the matched subset instead of full reachable history. In repositories where a 7-character prefix is unique only within the subset,--oneline/--abbrev-commitoutput can become ambiguous when users reuse that hash in later commands. Keep abbreviation-length calculation based on the unfiltered reachable set (or retain a pre-filter copy) and apply grep only to the displayed iteration set.Useful? React with 👍 / 👎.