[PR #310] feat: add commit message statistics feature (closes #309) #394

Closed
opened 2026-03-02 04:13:30 +03:00 by kerem · 0 comments
Owner

Original Pull Request: https://github.com/git-ai-project/git-ai/pull/310

State: closed
Merged: No


Summary

This PR implements automatic AI code statistics in commit messages, addressing issue #309.

What This Feature Does

When enabled, git-ai will automatically append AI code statistics to commit messages after a commit is created. The feature:

  • Only adds stats when AI code is present - human-only commits remain unchanged
  • Supports multiple formats: Text (with progress bars) or Markdown (with emoji)
  • Fully configurable via git config
  • Preserves commit SHA for human-only commits (no history rewriting)
  • Synchronizes with Git Notes when SHA changes occur during amend operations

Example Output

Text Format (default)

Add user authentication

Stats: ████████░░░░░░░░░░░░ | 60% you, 20% mixed, 20% ai
AI: 3 accepted, 5 generated, waited 1m 5s

Markdown Format

Add user authentication

🧠 you    ████████      60%
🤖 ai     ░░░░░░██      20%
Mixed     ░░░░░░░░      20%

AI: 3 accepted, 5 generated, waited 1m 5s

Configuration

Enable the feature:

git-ai config set --add feature_flags.commit_message_stats true

Customize format:

git config ai.commit-message-stats.format markdown
git config ai.commit-message-stats.include-progress-bar true
git config ai.commit-message-stats.template "{original_message}\n\n{stats}"

Or use environment variable:

GIT_AI_COMMIT_MESSAGE_STATS=true git commit -m "message"

Implementation Details

New Files

  • src/authorship/commit_message.rs - Core formatting logic (565 lines)
  • docs/commit-message-stats.md - Comprehensive documentation
  • tests/commit_message_stats.rs - Rust integration tests
  • tests/commit/README.md - Test documentation
  • tests/commit/run-message-stats-tests.sh - Shell integration tests (10 scenarios)

Modified Files

  • src/authorship/post_commit.rs - Integrates formatting into post-commit hook
  • src/feature_flags.rs - Adds commit_message_stats feature flag
  • src/git/repository.rs - Adds amend_commit_message() and message() methods
  • src/git/refs.rs - Adds notes_remove() for Git Notes synchronization
  • src/authorship/mod.rs - Exports new module
  • README.md - Updated with feature information

How It Works

  1. Post-commit hook runs after git creates a commit
  2. Checks for AI code in the commit via Git Notes
  3. If AI code exists:
    • Formats statistics based on config
    • Amends commit message (changes SHA)
    • Updates Git Notes to new SHA
  4. If no AI code: Original commit is unchanged

Testing

Run Integration Tests

./tests/commit/run-message-stats-tests.sh

Run Rust Tests

cargo test --test commit_message_stats
cargo test commit_message
cargo test post_commit

Manual Testing

mkdir /tmp/test && cd /tmp/test
git init
git config user.name "Test"
git config user.email "test@test.com"
git config ai.commit-message-stats.enabled true

# Test with AI code
echo "// AI" > file.rs
git add file.rs
git-ai checkpoint mock_ai file.rs
GIT_AI=git git commit -m "Add AI code"
# Verify: git log -1 --format='%B' shows stats

# Test without AI code
echo "human" > human.txt
git add human.txt
GIT_AI=git git commit -m "Human only"
# Verify: git log -1 --format='%B' shows no stats

Edge Cases Handled

  • No AI code → No stats added
  • Pure AI code → 100% AI stats
  • Mixed code → Correct percentages
  • Delete-only commits → Shows "(no additions)"
  • Markdown format → Emoji visualization
  • Disabled feature → No modification
  • Custom templates → User-defined format
  • SHA changes → Git Notes synchronized
  • Large commits → Performance optimized

Feature Flag

The feature is controlled by:

  • feature_flags.rs: commit_message_stats (debug: true, release: false)
  • Git config: ai.commit-message-stats.enabled
  • Environment: GIT_AI_COMMIT_MESSAGE_STATS=true

Priority: Feature flag > git config > environment > default (disabled)

  • Closes #309
  • Depends on existing Git Notes infrastructure (refs/notes/ai)
  • Compatible with all existing git-ai workflows (rebase, merge, cherry-pick, etc.)
**Original Pull Request:** https://github.com/git-ai-project/git-ai/pull/310 **State:** closed **Merged:** No --- ### Summary This PR implements automatic AI code statistics in commit messages, addressing issue #309. ### What This Feature Does When enabled, git-ai will automatically append AI code statistics to commit messages after a commit is created. The feature: - **Only adds stats when AI code is present** - human-only commits remain unchanged - **Supports multiple formats**: Text (with progress bars) or Markdown (with emoji) - **Fully configurable** via git config - **Preserves commit SHA** for human-only commits (no history rewriting) - **Synchronizes with Git Notes** when SHA changes occur during amend operations ### Example Output #### Text Format (default) Add user authentication Stats: ████████░░░░░░░░░░░░ | 60% you, 20% mixed, 20% ai AI: 3 accepted, 5 generated, waited 1m 5s #### Markdown Format ```markdown Add user authentication 🧠 you ████████ 60% 🤖 ai ░░░░░░██ 20% Mixed ░░░░░░░░ 20% AI: 3 accepted, 5 generated, waited 1m 5s ``` ### Configuration Enable the feature: ```bash git-ai config set --add feature_flags.commit_message_stats true ``` Customize format: ```bash git config ai.commit-message-stats.format markdown git config ai.commit-message-stats.include-progress-bar true git config ai.commit-message-stats.template "{original_message}\n\n{stats}" ``` Or use environment variable: ```bash GIT_AI_COMMIT_MESSAGE_STATS=true git commit -m "message" ``` ### Implementation Details #### New Files - `src/authorship/commit_message.rs` - Core formatting logic (565 lines) - `docs/commit-message-stats.md` - Comprehensive documentation - `tests/commit_message_stats.rs` - Rust integration tests - `tests/commit/README.md` - Test documentation - `tests/commit/run-message-stats-tests.sh` - Shell integration tests (10 scenarios) #### Modified Files - `src/authorship/post_commit.rs` - Integrates formatting into post-commit hook - `src/feature_flags.rs` - Adds `commit_message_stats` feature flag - `src/git/repository.rs` - Adds `amend_commit_message()` and `message()` methods - `src/git/refs.rs` - Adds `notes_remove()` for Git Notes synchronization - `src/authorship/mod.rs` - Exports new module - `README.md` - Updated with feature information ### How It Works 1. **Post-commit hook** runs after git creates a commit 2. **Checks for AI code** in the commit via Git Notes 3. **If AI code exists**: - Formats statistics based on config - Amends commit message (changes SHA) - Updates Git Notes to new SHA 4. **If no AI code**: Original commit is unchanged ### Testing #### Run Integration Tests ```bash ./tests/commit/run-message-stats-tests.sh ``` #### Run Rust Tests ```bash cargo test --test commit_message_stats cargo test commit_message cargo test post_commit ``` #### Manual Testing ```bash mkdir /tmp/test && cd /tmp/test git init git config user.name "Test" git config user.email "test@test.com" git config ai.commit-message-stats.enabled true # Test with AI code echo "// AI" > file.rs git add file.rs git-ai checkpoint mock_ai file.rs GIT_AI=git git commit -m "Add AI code" # Verify: git log -1 --format='%B' shows stats # Test without AI code echo "human" > human.txt git add human.txt GIT_AI=git git commit -m "Human only" # Verify: git log -1 --format='%B' shows no stats ``` ### Edge Cases Handled - ✅ No AI code → No stats added - ✅ Pure AI code → 100% AI stats - ✅ Mixed code → Correct percentages - ✅ Delete-only commits → Shows "(no additions)" - ✅ Markdown format → Emoji visualization - ✅ Disabled feature → No modification - ✅ Custom templates → User-defined format - ✅ SHA changes → Git Notes synchronized - ✅ Large commits → Performance optimized ### Feature Flag The feature is controlled by: - `feature_flags.rs`: `commit_message_stats` (debug: true, release: false) - Git config: `ai.commit-message-stats.enabled` - Environment: `GIT_AI_COMMIT_MESSAGE_STATS=true` Priority: Feature flag > git config > environment > default (disabled) ### Related - Closes #309 - Depends on existing Git Notes infrastructure (`refs/notes/ai`) - Compatible with all existing git-ai workflows (rebase, merge, cherry-pick, etc.)
kerem 2026-03-02 04:13:30 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/git-ai#394
No description provided.