Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.2 KB
Raw Blame
import type { RenderResult, CommandDetectionResult } from "./types.ts";
import { NO_RENDER } from "./types.ts";

/**
 * RTK semantic renderer for `git diff` / `git show` output.
 *
 * Keeps only:
 *  - `diff --git a/... b/...` file header lines
 *  - `@@ ... @@` hunk headers
 *  - change lines starting with `+` or `-` (but NOT `+++`/`---`)
 *
 * Everything else (context lines, index lines, mode lines, etc.) is dropped.
 * If no hunk header is found, returns no-op (input is not a real diff).
 */
export function renderGitDiff(text: string, _detection: CommandDetectionResult): RenderResult {
  // Guard: must have at least one hunk header
  if (!text.includes("@@ ")) {
    return NO_RENDER(text);
  }

  const kept: string[] = [];
  for (const line of text.split("\n")) {
    if (
      line.startsWith("diff --git ") ||
      line.startsWith("@@ ")
    ) {
      kept.push(line);
    } else if (/^[+-](?![+-])/.test(line)) {
      // change line (+foo or -foo) but NOT +++ or ---
      kept.push(line);
    }
    // drop: context lines (space), index lines, --- a/, +++ b/, mode lines, etc.
  }

  const out = kept.join("\n");
  if (out === text) return NO_RENDER(text);

  return { text: out, changed: true, renderer: "git-diff" };
}