difit Documentation

repository·main·Indexed 25 days ago

https://github.com/yoshiko-pg/difit

difit is a lightweight CLI tool (version 5.0.8) that spins up a local web server to display Git commit diffs in a GitHub-like Files changed view. It supports reviewing single commits, comparing branches, and viewing uncommitted changes (staged/working). Key features include GitHub PR integration via the --pr flag, the ability to inject initial review comments as JSON, and a dedicated VS Code extension (difit-vscode). It is designed for the AI era, allowing users to copy review comments as structured prompts for AI coding agents.

Tokens
14.9K
Snippets
31
Records
107
Agent score
84%

What's inside difit

  1. Understand the difit testing strategy and tools

    main

    The difit project uses Vitest for its testing framework. The testing strategy is divided into two main categories:

    1. Unit Tests: Run in isolation with all dependencies mocked. They focus on logic and edge cases with fast execution and no side effects.
    2. Integration Tests: Mock only external dependencies (like Git or the file system) to test component interactions and actual HTTP communication.

    Safety Measures during testing:

    • No actual Git operations are performed (they are fully mocked).
    • Random ports are used for server tests to avoid conflicts.
    • All resources are cleaned up in afterEach.
    • No modifications are made to the working directory.
  2. System Architecture of difit

    main

    difit is composed of the following architectural components:

    • CLI: Argument parsing powered by Commander.js with comprehensive validation.
    • Backend: An Express server utilizing simple-git for diff processing.
    • GitHub Integration: Uses GitHub CLI (gh pr diff --patch) to retrieve PR patches.
    • Frontend: Built with React 18, TypeScript, and Vite.
    • Styling: Tailwind CSS v4 with a GitHub-like dark theme.
    • Syntax Highlighting: Prism.js with dynamic language loading.
    • Testing: Vitest unit tests located alongside source files.
    • Quality Control: Uses oxlint, oxfmt, and lefthook pre-commit hooks.
  3. Enable difit for AI Agents

    main

    You can add difit skills to your AI agent to allow it to request code reviews or launch difit with preloaded findings.

    Run the following command to add the skills:

    npx skills add yoshiko-pg/difit

    Available Skills:

    • difit: Asks the user for a review through difit after code changes.
    • difit-review: Reviews a specific diff or PR and launches difit with findings or explanations preloaded as comments.
  4. Integrate GitHub PR reviews with difit

    main

    You can review GitHub Pull Requests directly in the difit web interface using the --pr flag.

    To enable this, difit requires access to the GitHub API via @octokit/rest. You must provide authentication using one of the following methods:

    1. Set the GITHUB_TOKEN environment variable.
    2. Use an existing GitHub CLI session (gh auth token).

    Once authenticated, difit resolves the PR commits locally after fetching the necessary metadata.

  5. Pipe diffs into difit via Stdin

    main
    You can view diffs from any tool by piping unified diffs into difit. If no positional arguments or --pr flags are provided, difit will attempt to auto-detect stdin. You can also explicitly enable stdin mode using -.
  6. Use the difit-review skill for code reviews

    main

    The difit-review skill allows you to launch a git diff in a human-readable viewer while preloading findings or code explanations as comments. This is ideal for reviewing branch diffs, commit diffs, or GitHub PRs.

    Command Selection Rule

    Before running commands, determine the correct command to use:

    1. If command -v difit succeeds, use difit.
    2. Otherwise, use npx difit.

    Note: If using npx difit in a sandboxed environment without network access, you must request escalated permissions and user approval.

  7. Use the difit CLI for Git diff reviews

    main

    difit is a CLI tool that displays Git diffs in a GitHub-like web interface. It consists of a CLI entry point, an Express server, and a React web application.

    Basic Command Syntax

    difit [commit-ish] [compare-with]

    Positional Arguments

    • [commit-ish]: The target commit, branch, or tag to review. Defaults to HEAD.
      • Supports Git references (SHA, branch names, tags).
      • Supports HEAD references (HEAD, HEAD~n, HEAD^).
      • Supports special values: working, staged, or ..
    • [compare-with]: An optional base for comparison. If omitted, it defaults to the parent commit (commit-ish^).

    Special Argument Behaviors

    • working: Shows unstaged changes (working directory vs. staging area). It cannot be used with a compare-with argument unless the base is staged.
    • staged: Shows staged changes against a specified commit. It can only be used as the target, except when the target is working (where it acts as the base).
    • .: Shows all uncommitted changes (both working and staged). It can be compared against any commit.
  8. Requirements for running difit

    main

    To use difit, ensure your environment meets the following requirements:

    • Node.js: version 21.0.0 or higher.
    • Git Repository: A local Git repository containing the commits you wish to review.
    • GitHub CLI: Required if using the --pr mode to fetch Pull Request patches.
  9. Review diffs from standard input (stdin)

    main

    You can pipe unified diff formats from any tool into difit.

    Usage Rules:

    • If - is provided, it always uses stdin mode.
    • If positional arguments (<target> or [compare-with]) or --pr are present, it uses Git/PR mode and will NOT automatically read stdin.
    • If no explicit mode is specified, it automatically switches to stdin mode if stdin is a pipe, file, or socket.

    Examples:

    # View diff from another tool
    diff -u file1.txt file2.txt | difit
    
    # Review a saved patch
    cat changes.patch | difit
    
    # Compare against merge base
    git diff --merge-base main feature | difit
    
    # Explicitly use stdin mode
    git diff --cached | difit -
    git diff --cached | difit -
  10. Use difit comments for AI prompts

    main

    The review system allows you to create comments that can be easily copied as structured prompts for AI coding agents.

    Prompt Formats:

    • Single line: `path/to/file.ext:L<line_number>

    <instruction>`

    • Range selection: `path/to/file.ext:L<start>-L<end>

    <instruction>`

    Users can click "Copy Prompt" on a single comment or "Copy All Prompts" to get all comments in a structured format.

  11. Review single commits or compare branches

    main

    Use the following command patterns to review git changes:

    Single Commit Review

    • difit: Shows the diff of the latest commit (HEAD).
    • difit <commit_hash>: Shows a specific commit.
    • difit <branch_name>: Shows the latest commit of a specific branch.

    Compare Two Commits/Branches

    • difit <target> [compare-with]: Compares two points.
    • difit @ main: Compares HEAD with the main branch (@ is an alias for HEAD).
    • difit feature main: Compares two branches.
    • difit . origin/main: Compares the working directory with a remote branch.
    difit <target> [compare-with]
  12. Review a GitHub Pull Request

    main

    Use the --pr <url> flag to review a GitHub PR. This mode uses the GitHub CLI (gh) to fetch the patch and imports unresolved inline review threads as initial comments in the difit UI.

    Prerequisites:

    1. Authenticate with GitHub CLI: gh auth login.
    2. For Enterprise Server, use gh auth login --hostname YOUR-ENTERPRISE-SERVER or set GH_HOST and GH_TOKEN environment variables.
    difit --pr https://github.com/owner/repo/pull/123