All files / src/cli context.ts

77.77% Statements 21/27
74.07% Branches 20/27
66.66% Functions 2/3
77.77% Lines 21/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                        2x                                                       2x 2x 2x 2x     2x 2x 2x 2x 2x   2x 2x         2x 1x 1x 1x 1x 1x 1x           2x           2x                                                                                                                                  
import { basename } from 'node:path';
import type { EventContext, FileChange } from '../types/index.js';
import { pluralize } from './output/index.js';
import { expandAndCreateFileChanges } from './files.js';
import {
  getChangedFilesWithPatches,
  getCurrentBranch,
  getHeadSha,
  getDefaultBranch,
  getRepoRoot,
  getRepoName,
  getCommitMessage,
  resolveRef,
  type GitFileChange,
} from './git.js';
 
/**
 * Convert git file change to EventContext FileChange format.
 */
function toFileChange(file: GitFileChange): FileChange {
  return {
    filename: file.filename,
    status: file.status,
    additions: file.additions,
    deletions: file.deletions,
    patch: file.patch,
    chunks: file.chunks,
  };
}
 
export interface LocalContextOptions {
  base?: string;
  head?: string;
  cwd?: string;
  /** Override auto-detected default branch (from config) */
  defaultBranch?: string;
  /** Analyze only staged changes (git diff --cached) */
  staged?: boolean;
}
 
/**
 * Build an EventContext from local git repository state.
 * Creates a synthetic pull_request event from git diff.
 *
 * When analyzing a specific commit (head is set), uses the actual commit
 * message as title/body to provide intent context to the LLM.
 */
export function buildLocalEventContext(options: LocalContextOptions = {}): EventContext {
  const cwd = options.cwd ?? process.cwd();
  const repoPath = getRepoRoot(cwd);
  const { owner, name } = getRepoName(cwd);
  const defaultBranch = options.defaultBranch ?? getDefaultBranch(cwd);
 
  // When staged, always diff against HEAD (index vs HEAD)
  const staged = options.staged ?? false;
  const base = staged ? 'HEAD' : (options.base ?? defaultBranch);
  const head = options.head; // undefined means working tree
  const currentBranch = getCurrentBranch(cwd);
  const headSha = head ? resolveRef(head, cwd) : getHeadSha(cwd);
 
  const changedFiles = getChangedFilesWithPatches(base, head, cwd, { staged });
  const files = changedFiles.map(toFileChange);
 
  // Use actual commit message when analyzing a specific commit
  let title: string;
  let body: string;
  if (head) {
    const commitMsg = getCommitMessage(head, cwd);
    title = commitMsg.subject || `Commit ${head}`;
    body = commitMsg.body || `Analyzing changes in ${head}`;
  } else if (staged) {
    title = `Staged changes: ${currentBranch}`;
    body = `Analyzing staged changes`;
  } else E{
    title = `Local changes: ${currentBranch}`;
    body = `Analyzing local changes from ${base} to working tree`;
  }
 
  const diffContextSource = staged
    ? { type: 'git-index' as const }
    : head
      ? { type: 'git-ref' as const, ref: headSha }
      : { type: 'working-tree' as const };
 
  return {
    eventType: 'pull_request',
    action: 'opened',
    repository: {
      owner,
      name,
      fullName: `${owner}/${name}`,
      defaultBranch,
    },
    pullRequest: {
      number: 0, // Local run, no real PR number
      title,
      body,
      author: 'local',
      baseBranch: base,
      headBranch: currentBranch,
      headSha,
      baseSha: resolveRef(base, cwd),
      files,
    },
    diffContextSource,
    repoPath,
  };
}
 
export interface FileContextOptions {
  patterns: string[];
  cwd?: string;
}
 
/**
 * Build an EventContext from a list of files or glob patterns.
 * Creates a synthetic pull_request event treating files as newly added.
 * This allows analysis without requiring git or a warden.toml config.
 */
export async function buildFileEventContext(options: FileContextOptions): Promise<EventContext> {
  const cwd = options.cwd ?? process.cwd();
  const dirName = basename(cwd);
 
  const files = await expandAndCreateFileChanges(options.patterns, cwd);
 
  return {
    eventType: 'pull_request',
    action: 'opened',
    repository: {
      owner: 'local',
      name: dirName,
      fullName: `local/${dirName}`,
      defaultBranch: 'main',
    },
    pullRequest: {
      number: 0,
      title: 'File analysis',
      body: `Analyzing ${files.length} ${pluralize(files.length, 'file')}`,
      author: 'local',
      baseBranch: 'main',
      headBranch: 'file-analysis',
      headSha: 'file-analysis',
      baseSha: 'file-analysis',
      files,
    },
    diffContextSource: { type: 'working-tree' },
    repoPath: cwd,
  };
}