MCPcopy Index your code
hub / github.com/21st-dev/1code / createFileContentsRouter

Function createFileContentsRouter

src/main/lib/git/file-contents.ts:47–222  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

45}
46
47export 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

Callers 1

createGitRouterFunction · 0.90

Calls 6

assertRegisteredWorktreeFunction · 0.90
detectLanguageFunction · 0.90
getFileVersionsFunction · 0.85
isBinaryContentFunction · 0.85
getFileContentMethod · 0.80
setFileContentMethod · 0.80

Tested by

no test coverage detected