Skip to content

Conversation

@lunygithub
Copy link

feat(log): add --grep parameter for commit message filtering
Fixes #47

功能描述

为 log 命令添加 --grep 参数,允许用户根据提交信息中的内容来过滤提交历史,只显示包含指定模式的提交。

实现功能

  • 内容过滤:基于提交消息的内容进行搜索匹配
  • 模式匹配:支持简单的字符串包含匹配
  • 参数组合:可以与其他 log 参数(如 --oneline, -n, --stat 等)组合使用
  • 无结果静默:当没有匹配的提交时,不输出任何内容

实现方案

  1. LogArgs 结构体中添加 Option<String> 类型的 grep 字段表示 --grep 参数
  2. 在获取提交列表后,对每个提交的消息内容进行模式匹配过滤
  3. 通过字符串包含匹配实现基础的内容过滤
  4. 添加完整的单元测试验证功能正确性

测试验证

  • 基础 grep 过滤功能
  • 大小写敏感匹配
  • 无匹配结果静默输出
  • 与其他参数组合使用
  • 边界情况处理

使用示例

libra log --grep "fix"                    # 显示包含 "fix" 的提交
libra log --grep "feature" --oneline      # 单行显示包含 "feature" 的提交
libra log --grep "test" -n 5              # 显示最多5个包含 "test" 的提交
libra log --grep "nonexistent"            # 无匹配时静默输出

@genedna genedna requested a review from Copilot November 20, 2025 14:20
Copilot finished reviewing on behalf of genedna November 20, 2025 14:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a --grep parameter to the log command for filtering commits based on message content, implementing issue #47. The feature uses simple substring matching to filter the commit history before display.

  • Adds --grep option to LogArgs struct for pattern-based commit message filtering
  • Implements filtering logic in the execute function after sorting commits by timestamp
  • Adds unit tests for argument parsing and parameter combinations

@genedna genedna requested a review from Copilot November 21, 2025 01:41
Copilot finished reviewing on behalf of genedna November 21, 2025 01:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment on lines +209 to +212
reachable_commits = reachable_commits
.into_iter()
.filter(|commit| commit.message.contains(pattern))
.collect();
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider handling the edge case where the grep pattern is an empty string. Currently, an empty string will match all commits (since contains("") returns true for any string). While this might be acceptable, it could be clearer to either:

  1. Skip filtering when the pattern is empty
  2. Document this behavior in the help text

Example improvement:

// 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();
    }
}
Suggested change
reachable_commits = reachable_commits
.into_iter()
.filter(|commit| commit.message.contains(pattern))
.collect();
if !pattern.is_empty() {
reachable_commits = reachable_commits
.into_iter()
.filter(|commit| commit.message.contains(pattern))
.collect();
}

Copilot uses AI. Check for mistakes.
#[clap(value_name = "PATHS", num_args = 0..)]
pathspec: Vec<String>,

/// Filter commits by message content
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider clarifying that the grep filter performs case-sensitive substring matching. This helps set user expectations about the behavior. For example:

/// Filter commits by message content (case-sensitive substring match)

or

/// Filter commits by message content. Performs case-sensitive substring matching.
Suggested change
/// Filter commits by message content
/// Filter commits by message content (case-sensitive substring match)

Copilot uses AI. Check for mistakes.
@Ivanbeethoven
Copy link
Collaborator

请参考copilot意见修改代码,并修复fmt和clippy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[r2cn-测试任务] 为 log 命令添加 --grep 参数,支持按提交信息过滤提交历史

2 participants