Skip to content

Conversation

@ab22593k
Copy link

Summary

This PR introduces significant refactoring across Git operations, UI responsiveness, and error handling. It abstracts Git operations using a GitBackend trait, enables non-blocking UI for long operations with spinners, optimizes syntax highlighting, and refines error reporting for better context and user feedback. The core Git operations are now handled by the git2 library for improved reliability and consistency.

Description

Core Capabilities

  • Git Operation Abstraction: Introduces a GitBackend trait to abstract Git operations, allowing for interchangeable backends (currently Git2Backend for git2 library integration).
  • Non-Blocking UI for Long Operations: Implements a non-blocking UI for operations like fetch and pull. A spinner is displayed in the command log during execution, and upon completion, a "completed" message along with the full command output is shown.
  • Syntax Highlighting Optimizations: Enhances syntax highlighting performance by increasing cache size and implementing caching for parsed syntax trees. A safeguard is added to disable highlighting for very large files.
  • Structured Error Handling: Refactors error handling to provide more detailed and contextual error messages, including specific operation failures and underlying error details.

Notes

The removal of the pest dependency for parsing Git status output simplifies the codebase. The new GitBackend trait provides a clean abstraction for Git interactions, paving the way for future backend implementations.

The core Git operations for generating diffs (unstaged, staged) and showing
commit/stash details have been refactored to use the `git2` Rust library
instead of shelling out to external `git` commands.

This change offers several benefits:
*   **Improved Reliability:** Direct API calls are generally more robust than
parsing command-line output.
*   **Error Handling:** Specific `git2::Error` types can be captured,
replacing generic `io::Error` for Git-related failures.
*   **Performance:** Eliminates the overhead of spawning external processes
for each operation.

Affected areas include:
*   `diff_unstaged` and `diff_staged`
*   `show` (for commit details)
*   `stash_show`

Error types `GitDiff` and `GitShow` in `src/error.rs` have been updated to
`git2::Error` to reflect the new error sources. Consequently, many snapshot
tests were updated to match the slightly altered output format produced by the
`git2` diff generation.
The `git status` operation has been refactored to use the `git2` Rust library
instead of shelling out to `git status --porcelain`.

This change offers several benefits:
*   **Improved Reliability:** Direct API calls are generally more robust than
parsing command-line output.
*   **Error Handling:** Specific `git2::Error` types can be captured,
replacing generic `io::Error` for Git-related failures.
*   **Performance:** Eliminates the overhead of spawning external processes
for each operation.

The previous parsing logic, including the `pest` and `pest_derive`
dependencies and the `src/git/parse/status` module, has been removed.

Consequently, many snapshot tests were updated to match the slightly altered
output format produced by `git2`'s status generation.
Updated string formatting throughout the codebase to adhere to clippy's
recommendations.

Specifically, this commit addresses instances of redundant or less idiomatic
string
formatting in macros like `format_args!`, `eprintln!`, `log::trace!`, and
`log::error!`.

These changes improve code readability and consistency, making the codebase
happier according to `cargo clippy`.
The Git operations for detecting rebase, merge, and revert statuses have been
refactored to use the `git2` Rust library.

This change offers several benefits:
*   **Improved Reliability:** Direct API calls to `git2` for `RepositoryState`
and OID parsing are more robust than manually reading and parsing `.git`
status files.
*   **Enhanced Error Handling:** Replaces generic `io::Error` with
more specific `git2::Error` types for Git-related failures and adds
`GitRebaseStatus` to `src/error.rs`.
*   **Consistency:** Further aligns Git status detection with the `git2`-based
implementation for `diff`, `show`, and general `status` operations.

Specifically, the `rebase_status`, `merge_status`, and `revert_status`
functions now leverage `repo.state()`, `git2::Oid`, and `repo.open_rebase()`
for more accurate and robust status detection. Unnecessary `log::warn!` calls
for file read errors have been replaced with proper error propagation.

Minor formatting adjustments were also included.
This change introduces a non-blocking user interface for long-running Git
operations such as `fetch` and `pull`. Previously, the UI would freeze
while these commands executed, providing no visual feedback until
completion.

Now, when a long-running operation is initiated, a spinner appears in the
command log, indicating that the operation is in progress. Upon
completion, the spinner is replaced with a "completed" message, and the
full output of the command is displayed.

This refactor offers several benefits:
*   **Improved User Experience:** The application remains responsive
    during potentially time-consuming Git operations, enhancing perceived
    performance.
*   **Clearer Feedback:** Users now have immediate visual confirmation that
    a command is being executed, reducing uncertainty about application
    status.
*   **Enhanced Information:** The complete output of fetch and pull
    operations is now captured and displayed in the UI, providing more
    context than before.

Key changes include:
*   Introduction of `OperationStatus` enum and `ongoing_operations`
    HashMap in `src/app.rs` to manage and track asynchronous command states.
*   New `run_cmd_with_spinner` function in `src/app.rs` to initiate
    commands with visual feedback.
*   `fetch` and `pull` operations in `src/ops/fetch.rs` and
    `src/ops/pull.rs` are updated to use the new non-blocking mechanism.
*   The UI rendering logic in `src/ui.rs` is adapted to display animated
    spinners for ongoing operations and their results upon completion.
*   Numerous snapshot tests were updated to reflect the new output format
    of completed operations.
This change introduces optimizations that provide better responsiveness
for syntax highlighting, especially for large repositories, while maintaining
full backward compatibility. Users can now disable syntax highlighting
entirely if performance is a concern.

Previously, syntax highlighting could lead to noticeable slowdowns and
unresponsiveness when dealing with extensive file contents.

Specific changes include:
*   Increased the cache size for `HunkHighlights` from 200 to 500 entries.
*   Introduced a new cached function, `cached_syntax_parse`, which leverages
    `DefaultHasher` to store and reuse parsed syntax trees, significantly
    reducing redundant computation.
*   Implemented a performance safeguard in `iter_syntax_highlights` that
    automatically disables highlighting for files exceeding 100,000 characters
    in length, or if the feature is explicitly disabled in the configuration.
*   Added `highlight` benchmarks in `benches/highlight.rs` to measure and
    track the performance of syntax parsing and diff computation.
*   Adjusted module visibility in `src/lib.rs` and `src/syntax_parser.rs`
    to support the new benchmarking infrastructure.
This change refactors the application's error handling to provide more
structured, contextual, and user-friendly messages.

Previously, errors often lacked specific details about the failing operation
or provided less actionable feedback, making debugging and user comprehension
more difficult.

This refactor offers several benefits:
*   **Enhanced Clarity:** New error variants (`GitOperationFailed`,
`IoOperationFailed`, `ParsingFailed`) capture the specific operation that
failed, along with the underlying error, providing richer context.
*   **Improved User Guidance:** Error display messages for critical operations
(e.g., Git status, command failures) are now more descriptive and suggest
potential remedies or checks.
*   **Consistency:** Helper functions (`Error::git_operation`,
`Error::io_operation`, `Error::parsing`) ensure a standardized approach to
error construction and propagation.
@ab22593k ab22593k marked this pull request as ready for review October 22, 2025 16:00
@altsem
Copy link
Owner

altsem commented Oct 26, 2025

This has a lot of changes, so I suggest splitting it up into smaller PRs, so I can review one thing at a time.

Git Operation Abstraction: Introduces a GitBackend trait to abstract Git operations, allowing for interchangeable backends (currently Git2Backend for git2 library integration).

Looks like a hallucination..
However, I'm moving more and more things away from libgit2, so the changes don't make a lot of sense. See 79aaaee

Non-Blocking UI for Long Operations: Implements a non-blocking UI for operations like fetch and pull. A spinner is displayed in the command log during execution, and upon completion, a "completed" message along with the full command output is shown.

It is actually already non-blocking for those ops. I guess a spinner is nice.

Syntax Highlighting Optimizations: Enhances syntax highlighting performance by increasing cache size and implementing caching for parsed syntax trees. A safeguard is added to disable highlighting for very large files.

Isn't it already cached?

Structured Error Handling: Refactors error handling to provide more detailed and contextual error messages, including specific operation failures and underlying error details.

This feels like a good thing to do.

@altsem altsem closed this Oct 26, 2025
@ab22593k
Copy link
Author

@altsem, thanks for your time, i will try

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.

2 participants