* Get file content at a specific treeish
(params: GetFileAtTreeishParams)
| 1923 | * Get file content at a specific treeish |
| 1924 | */ |
| 1925 | async function handleGetFileAtTreeish(params: GetFileAtTreeishParams): Promise<GetFileAtTreeishResponse> { |
| 1926 | const startTime = Date.now() |
| 1927 | logger.info("[Git:getFileAtTreeish] Getting file content", JSON.stringify({ |
| 1928 | workDir: params.workDir, |
| 1929 | treeish: params.treeish, |
| 1930 | filePath: params.filePath, |
| 1931 | })) |
| 1932 | |
| 1933 | try { |
| 1934 | // First check the file size using git cat-file -s |
| 1935 | const sizeResult = await execGit(["cat-file", "-s", `${params.treeish}:${params.filePath}`], params.workDir) |
| 1936 | |
| 1937 | if (!sizeResult.success) { |
| 1938 | // File might not exist at this treeish |
| 1939 | if (sizeResult.stderr.includes("does not exist") || sizeResult.stderr.includes("fatal:")) { |
| 1940 | logger.info("[Git:getFileAtTreeish] File does not exist at treeish", JSON.stringify({ |
| 1941 | filePath: params.filePath, |
| 1942 | treeish: params.treeish, |
| 1943 | })) |
| 1944 | return { content: "", exists: false } |
| 1945 | } |
| 1946 | throw new Error(`Failed to get file size: ${sizeResult.stderr}`) |
| 1947 | } |
| 1948 | |
| 1949 | const fileSize = parseInt(sizeResult.stdout.trim(), 10) |
| 1950 | if (fileSize > MAX_FILE_SIZE_FOR_DIFF) { |
| 1951 | logger.info("[Git:getFileAtTreeish] File too large for diff", JSON.stringify({ |
| 1952 | filePath: params.filePath, |
| 1953 | treeish: params.treeish, |
| 1954 | size: fileSize, |
| 1955 | maxSize: MAX_FILE_SIZE_FOR_DIFF, |
| 1956 | })) |
| 1957 | return { content: "", exists: true, tooLarge: true } |
| 1958 | } |
| 1959 | |
| 1960 | // Use git show to get file content at specific commit |
| 1961 | const result = await execGit(["show", `${params.treeish}:${params.filePath}`], params.workDir) |
| 1962 | |
| 1963 | if (!result.success) { |
| 1964 | // File might not exist at this treeish |
| 1965 | if (result.stderr.includes("does not exist") || result.stderr.includes("fatal: path")) { |
| 1966 | logger.info("[Git:getFileAtTreeish] File does not exist at treeish", JSON.stringify({ |
| 1967 | filePath: params.filePath, |
| 1968 | treeish: params.treeish, |
| 1969 | })) |
| 1970 | return { content: "", exists: false } |
| 1971 | } |
| 1972 | throw new Error(`Failed to get file content: ${result.stderr}`) |
| 1973 | } |
| 1974 | |
| 1975 | logger.info("[Git:getFileAtTreeish] Got file content", JSON.stringify({ |
| 1976 | size: result.stdout.length, |
| 1977 | duration: Date.now() - startTime, |
| 1978 | })) |
| 1979 | |
| 1980 | return { content: result.stdout, exists: true } |
| 1981 | } catch (error: any) { |
| 1982 | logger.error("[Git:getFileAtTreeish] Error:", JSON.stringify({ error: error.message, duration: Date.now() - startTime })) |
no test coverage detected