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"            # 无匹配时静默输出

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

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

@genedna
Copy link
Member

genedna commented Dec 18, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +207 to +212
// Apply grep filtering
if let Some(pattern) = &args.grep {
reachable_commits = reachable_commits
.into_iter()
.filter(|commit| commit.message.contains(pattern))
.collect();

Choose a reason for hiding this comment

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

P2 Badge Clear graph columns when commits are skipped by --grep

The new grep filter is applied before rendering the graph, but GraphState only clears a column when that commit is actually rendered (see GraphState::render in the same file). With libra log --graph --grep <pattern>, if a matching commit’s parent does not match the pattern, the parent hash is pushed into columns but never removed, so later unrelated commits are printed with stray | branches and the column list grows as more ancestors are skipped. This yields incorrect graph output whenever grep hides intermediate commits.

Useful? React with 👍 / 👎.

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 参数,支持按提交信息过滤提交历史

3 participants