{"version":3,"sources":["../../src/listGitFiles.ts"],"sourcesContent":["import { getAppLogger } from '@intlayer/config';\nimport configuration from '@intlayer/config/built';\nimport { readFileSync } from 'fs';\nimport { join } from 'path';\nimport simpleGit from 'simple-git';\n\nexport type DiffMode = 'gitDiff' | 'uncommitted' | 'unpushed' | 'untracked';\n\nconst getGitRootDir = async (): Promise<string | null> => {\n  try {\n    const git = simpleGit();\n    const rootDir = await git.revparse(['--show-toplevel']);\n    return rootDir.trim();\n  } catch (error) {\n    const appLogger = getAppLogger(configuration);\n    appLogger('Error getting git root directory:' + error, {\n      level: 'error',\n    });\n    return null;\n  }\n};\n\nexport type ListGitFilesOptions = {\n  mode: DiffMode[];\n  baseRef?: string;\n  currentRef?: string;\n  absolute?: boolean;\n};\n\nexport const listGitFiles = async ({\n  mode,\n  baseRef = 'origin/main',\n  currentRef = 'HEAD', // HEAD points to the current branch's latest commit\n  absolute = true,\n}: ListGitFilesOptions) => {\n  try {\n    const git = simpleGit();\n    const diff: Set<string> = new Set();\n\n    if (mode.includes('untracked')) {\n      const status = await git.status();\n      status.not_added.forEach((f) => diff.add(f));\n    }\n\n    if (mode.includes('uncommitted')) {\n      // Get uncommitted changes\n      const uncommittedDiff = await git.diff(['--name-only', 'HEAD']);\n\n      const uncommittedFiles = uncommittedDiff.split('\\n').filter(Boolean);\n\n      uncommittedFiles.forEach((file) => diff.add(file));\n    }\n\n    if (mode.includes('unpushed')) {\n      // Get unpushed commits\n      const unpushedDiff = await git.diff(['--name-only', '@{push}...HEAD']);\n\n      const unpushedFiles = unpushedDiff.split('\\n').filter(Boolean);\n\n      unpushedFiles.forEach((file) => diff.add(file));\n    }\n\n    if (mode.includes('gitDiff')) {\n      // Get the base branch (usually main/master) from CI environment\n\n      await git.fetch(baseRef);\n\n      const diffBranch = await git.diff([\n        '--name-only',\n        `${baseRef}...${currentRef}`,\n      ]);\n\n      const gitDiffFiles = diffBranch.split('\\n').filter(Boolean);\n\n      gitDiffFiles.forEach((file) => diff.add(file));\n    }\n\n    if (absolute) {\n      const gitRootDir = await getGitRootDir();\n      if (!gitRootDir) {\n        return [];\n      }\n      return Array.from(diff).map((file) => join(gitRootDir, file));\n    }\n\n    return Array.from(diff);\n  } catch (error) {\n    console.warn('Failed to get changes list:', error);\n  }\n};\n\nexport type ListGitLinesOptions = {\n  mode: DiffMode[];\n  baseRef?: string;\n  currentRef?: string;\n};\n\nexport const listGitLines = async (\n  filePath: string,\n  {\n    mode,\n    baseRef = 'origin/main',\n    currentRef = 'HEAD', // HEAD points to the current branch's latest commit\n  }: ListGitLinesOptions\n): Promise<number[]> => {\n  const git = simpleGit();\n  // We collect **line numbers** (1-based) that were modified/added by the diff.\n  // Using a Set ensures uniqueness when the same line is reported by several modes.\n  const changedLines: Set<number> = new Set();\n\n  /**\n   * Extracts line numbers from a diff generated with `--unified=0`.\n   * Each hunk header looks like: @@ -<oldStart>,<oldCount> +<newStart>,<newCount> @@\n   * We consider both the \"+\" (new) side for additions and the \"-\" (old) side for deletions.\n   * For deletions, we add the line before and after the deletion point in the current file.\n   */\n  const collectLinesFromDiff = (diffOutput: string) => {\n    const hunkRegex = /@@ -(\\d+)(?:,(\\d+))? \\+(\\d+)(?:,(\\d+))? @@/g;\n    let match: RegExpExecArray | null;\n\n    while ((match = hunkRegex.exec(diffOutput)) !== null) {\n      const oldCount = match[2] ? Number(match[2]) : 1;\n      const newStart = Number(match[3]);\n      const newCount = match[4] ? Number(match[4]) : 1;\n\n      // Handle additions/modifications (+ side)\n      if (newCount > 0) {\n        for (let i = 0; i < newCount; i++) {\n          changedLines.add(newStart + i);\n        }\n      }\n\n      // Handle deletions (- side)\n      if (oldCount > 0 && newCount === 0) {\n        // For deletions, add the line before and after the deletion point\n        // The deletion point in the new file is at newStart\n        if (newStart > 1) {\n          changedLines.add(newStart - 1); // Line before deletion\n        }\n        changedLines.add(newStart); // Line after deletion (if it exists)\n      }\n    }\n  };\n\n  // 1. Handle untracked files – when a file is untracked its entire content is new.\n  if (mode.includes('untracked')) {\n    const status = await git.status();\n    const isUntracked = status.not_added.includes(filePath);\n    if (isUntracked) {\n      try {\n        const content = readFileSync(filePath, 'utf-8');\n        content.split('\\n').forEach((_, idx) => changedLines.add(idx + 1));\n      } catch {\n        // ignore read errors – file may have been deleted, etc.\n      }\n    }\n  }\n\n  // 2. Uncommitted changes (working tree vs HEAD)\n  if (mode.includes('uncommitted')) {\n    const diffOutput = await git.diff(['--unified=0', 'HEAD', '--', filePath]);\n    collectLinesFromDiff(diffOutput);\n  }\n\n  // 3. Unpushed commits – compare local branch to its upstream\n  if (mode.includes('unpushed')) {\n    const diffOutput = await git.diff([\n      '--unified=0',\n      '@{push}...HEAD',\n      '--',\n      filePath,\n    ]);\n    collectLinesFromDiff(diffOutput);\n  }\n\n  // 4. Regular git diff between baseRef and currentRef (e.g., CI pull-request diff)\n  if (mode.includes('gitDiff')) {\n    await git.fetch(baseRef);\n    const diffOutput = await git.diff([\n      '--unified=0',\n      `${baseRef}...${currentRef}`,\n      '--',\n      filePath,\n    ]);\n    collectLinesFromDiff(diffOutput);\n  }\n\n  // Return the list sorted for convenience\n  return Array.from(changedLines).sort((a, b) => a - b);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;AAC7B,mBAA0B;AAC1B,gBAA6B;AAC7B,kBAAqB;AACrB,wBAAsB;AAItB,MAAM,gBAAgB,YAAoC;AACxD,MAAI;AACF,UAAM,UAAM,kBAAAA,SAAU;AACtB,UAAM,UAAU,MAAM,IAAI,SAAS,CAAC,iBAAiB,CAAC;AACtD,WAAO,QAAQ,KAAK;AAAA,EACtB,SAAS,OAAO;AACd,UAAM,gBAAY,4BAAa,aAAAC,OAAa;AAC5C,cAAU,sCAAsC,OAAO;AAAA,MACrD,OAAO;AAAA,IACT,CAAC;AACD,WAAO;AAAA,EACT;AACF;AASO,MAAM,eAAe,OAAO;AAAA,EACjC;AAAA,EACA,UAAU;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