* Initialize a git repository with a default .gitignore
(params: InitGitParams)
| 2187 | * Initialize a git repository with a default .gitignore |
| 2188 | */ |
| 2189 | async function handleInitGit(params: InitGitParams): Promise<InitGitResponse> { |
| 2190 | const startTime = Date.now() |
| 2191 | logger.info("[Git:initGit] Initializing git repository", JSON.stringify({ directory: params.directory })) |
| 2192 | |
| 2193 | try { |
| 2194 | // Validate directory exists |
| 2195 | if (!fs.existsSync(params.directory)) { |
| 2196 | return { success: false, error: `Directory does not exist: ${params.directory}` } |
| 2197 | } |
| 2198 | |
| 2199 | if (!fs.statSync(params.directory).isDirectory()) { |
| 2200 | return { success: false, error: `Path is not a directory: ${params.directory}` } |
| 2201 | } |
| 2202 | |
| 2203 | // Check if already a git repository |
| 2204 | const gitDir = path.join(params.directory, ".git") |
| 2205 | if (fs.existsSync(gitDir)) { |
| 2206 | return { success: false, error: "Directory is already a git repository" } |
| 2207 | } |
| 2208 | |
| 2209 | // Initialize git repository |
| 2210 | const initResult = await execGit(["init"], params.directory) |
| 2211 | if (!initResult.success) { |
| 2212 | return { success: false, error: `Failed to initialize git: ${initResult.stderr}` } |
| 2213 | } |
| 2214 | |
| 2215 | // Create .gitignore if it doesn't exist |
| 2216 | const gitignorePath = path.join(params.directory, ".gitignore") |
| 2217 | if (!fs.existsSync(gitignorePath)) { |
| 2218 | fs.writeFileSync(gitignorePath, DEFAULT_GITIGNORE, "utf8") |
| 2219 | logger.info("[Git:initGit] Created .gitignore") |
| 2220 | } |
| 2221 | |
| 2222 | // Stage and commit the .gitignore |
| 2223 | await execGit(["add", ".gitignore"], params.directory) |
| 2224 | await execGit(["commit", "-m", "Initial commit: Add .gitignore"], params.directory) |
| 2225 | |
| 2226 | logger.info("[Git:initGit] Git repository initialized successfully", JSON.stringify({ directory: params.directory, duration: Date.now() - startTime })) |
| 2227 | return { success: true } |
| 2228 | } catch (error: any) { |
| 2229 | logger.error("[Git:initGit] Error:", JSON.stringify({ error: error.message, stack: error.stack, duration: Date.now() - startTime })) |
| 2230 | return { success: false, error: error.message } |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | // ============================================================================ |
| 2235 | // Hot Files Cache (for search ranking) |
no test coverage detected