()
| 45 | } |
| 46 | |
| 47 | export const createFileContentsRouter = () => { |
| 48 | return router({ |
| 49 | getFileContents: publicProcedure |
| 50 | .input( |
| 51 | z.object({ |
| 52 | worktreePath: z.string(), |
| 53 | filePath: z.string(), |
| 54 | oldPath: z.string().optional(), |
| 55 | category: z.enum(["against-base", "committed", "staged", "unstaged"]), |
| 56 | commitHash: z.string().optional(), |
| 57 | defaultBranch: z.string().optional(), |
| 58 | }), |
| 59 | ) |
| 60 | .query(async ({ input }): Promise<FileContents> => { |
| 61 | assertRegisteredWorktree(input.worktreePath); |
| 62 | |
| 63 | // Build cache key from all relevant parameters |
| 64 | const cacheKey = `${input.filePath}:${input.category}:${input.commitHash || "working"}:${input.oldPath || ""}`; |
| 65 | |
| 66 | // Check cache first |
| 67 | const cached = gitCache.getFileContent(input.worktreePath, cacheKey); |
| 68 | if (cached) { |
| 69 | try { |
| 70 | const parsed = JSON.parse(cached) as FileContents; |
| 71 | console.log("[getFileContents] Cache hit for:", input.filePath); |
| 72 | return parsed; |
| 73 | } catch { |
| 74 | // Invalid cache entry, continue to fetch |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | console.log("[getFileContents] Cache miss, fetching:", input.filePath); |
| 79 | const git = simpleGit(input.worktreePath); |
| 80 | const defaultBranch = input.defaultBranch || "main"; |
| 81 | const originalPath = input.oldPath || input.filePath; |
| 82 | |
| 83 | const { original, modified } = await getFileVersions( |
| 84 | git, |
| 85 | input.worktreePath, |
| 86 | input.filePath, |
| 87 | originalPath, |
| 88 | input.category, |
| 89 | defaultBranch, |
| 90 | input.commitHash, |
| 91 | ); |
| 92 | |
| 93 | const result: FileContents = { |
| 94 | original, |
| 95 | modified, |
| 96 | language: detectLanguage(input.filePath), |
| 97 | }; |
| 98 | |
| 99 | // Store in cache |
| 100 | gitCache.setFileContent(input.worktreePath, cacheKey, JSON.stringify(result)); |
| 101 | |
| 102 | return result; |
| 103 | }), |
| 104 |
no test coverage detected