[PR #429] fix: correct operator precedence bug causing false 'Committers are not using git-ai' message #463

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

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

State: closed
Merged: Yes


Summary

Fixes a critical bug where git-ai stats incorrectly displays "Committers are not using git-ai" even when valid authorship data exists.

Root Cause Analysis

This bug was introduced in commit c9303a0 (PR #288) while fixing issue #287. The problematic code at src/authorship/range_authorship.rs:438:

if !stats.authorship_stats.commits_with_authorship > stats.authorship_stats.total_commits {
    println!("Committers are not using git-ai");
    return;
}

The Problem

Due to Rust's operator precedence rules, the unary ! (NOT) operator has higher precedence than the > (comparison) operator. This means the expression is parsed as:

(!commits_with_authorship) > total_commits

NOT as the author likely intended:

!(commits_with_authorship > total_commits)

Why This is Wrong for Integers

In Rust, when ! is applied to an integer (like usize), it performs bitwise NOT, not boolean NOT:

Expression Result
!0usize 18446744073709551615 (usize::MAX)
!1usize 18446744073709551614 (usize::MAX - 1)
!n for any small n A very large number

So the condition becomes:

(HUGE_NUMBER) > total_commits

Which is almost always true, causing the error message to display incorrectly.

The Fix

Replace the broken condition with the straightforward check that was clearly intended:

if stats.authorship_stats.commits_with_authorship == 0 {
    println!("Committers are not using git-ai");
    return;
}

This correctly identifies when no commits in the range have authorship logs.

Reproduction

Before the fix:

git-ai stats HEAD~2..HEAD
# Shows: Committers are not using git-ai (even with valid data)

git-ai stats HEAD~2..HEAD --json
# Shows valid data: {"human_additions":1,"ai_additions":3,...}

After the fix:

git-ai stats HEAD~2..HEAD
# Correctly shows the authorship statistics bar graph

Testing

  • cargo build --release - compiles successfully
  • cargo test - all tests pass
  • Manual testing confirms the fix works correctly
  • Regression introduced in PR #288 (commit c9303a0)
  • Original issue that PR #288 was fixing: #287
**Original Pull Request:** https://github.com/git-ai-project/git-ai/pull/429 **State:** closed **Merged:** Yes --- ## Summary Fixes a critical bug where `git-ai stats` incorrectly displays "Committers are not using git-ai" even when valid authorship data exists. ## Root Cause Analysis This bug was introduced in commit c9303a0 (PR #288) while fixing issue #287. The problematic code at `src/authorship/range_authorship.rs:438`: ```rust if !stats.authorship_stats.commits_with_authorship > stats.authorship_stats.total_commits { println!("Committers are not using git-ai"); return; } ``` ### The Problem Due to Rust's **operator precedence rules**, the unary `!` (NOT) operator has **higher precedence** than the `>` (comparison) operator. This means the expression is parsed as: ```rust (!commits_with_authorship) > total_commits ``` **NOT** as the author likely intended: ```rust !(commits_with_authorship > total_commits) ``` ### Why This is Wrong for Integers In Rust, when `!` is applied to an integer (like `usize`), it performs **bitwise NOT**, not boolean NOT: | Expression | Result | |------------|--------| | `!0usize` | `18446744073709551615` (usize::MAX) | | `!1usize` | `18446744073709551614` (usize::MAX - 1) | | `!n` for any small n | A very large number | So the condition becomes: ``` (HUGE_NUMBER) > total_commits ``` Which is **almost always true**, causing the error message to display incorrectly. ## The Fix Replace the broken condition with the straightforward check that was clearly intended: ```rust if stats.authorship_stats.commits_with_authorship == 0 { println!("Committers are not using git-ai"); return; } ``` This correctly identifies when no commits in the range have authorship logs. ## Reproduction Before the fix: ```bash git-ai stats HEAD~2..HEAD # Shows: Committers are not using git-ai (even with valid data) git-ai stats HEAD~2..HEAD --json # Shows valid data: {"human_additions":1,"ai_additions":3,...} ``` After the fix: ```bash git-ai stats HEAD~2..HEAD # Correctly shows the authorship statistics bar graph ``` ## Testing - [x] `cargo build --release` - compiles successfully - [x] `cargo test` - all tests pass - [x] Manual testing confirms the fix works correctly ## Related - Regression introduced in PR #288 (commit c9303a0) - Original issue that PR #288 was fixing: #287
kerem 2026-03-02 04:13:42 +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#463
No description provided.