♻️ refactor: update git.ts
swadhinbiswas committed 3 months ago
c00e371
1 parent 2816e0e
commit c00e371
1 file changed
+41 -13
Showing 1 changed file+41-13
▼src/lib/git.ts
+41-13
| @@ -145,18 +145,6 @@ export async function initRepository( | ||
145 | 145 | throw err; |
146 | 146 | } |
147 | 147 | |
148 | - // Install hooks | |
149 | - if (!options.skipHooks) { | |
150 | - try { | |
151 | - logger.info(`[initRepository] Installing hooks`); | |
152 | - await installHooks(localRepoPath); | |
153 | - logger.info(`[initRepository] Hooks installed`); | |
154 | - } catch (err) { | |
155 | - logger.error(`[initRepository] Hook installation failed:`, err); | |
156 | - throw err; | |
157 | - } | |
158 | - } | |
159 | - | |
160 | 148 | // If we need initial content, create a temporary working copy |
161 | 149 | if (readme || gitignoreTemplate || licenseType) { |
162 | 150 | const tempPath = `${localRepoPath}.tmp`; |
| @@ -211,8 +199,10 @@ export async function initRepository( | ||
211 | 199 | } |
212 | 200 | |
213 | 201 | // Push to bare repository |
202 | + // Use --no-verify to skip hooks: either they aren't installed yet (normal case) | |
203 | + // or they're leftover from a previous failed init (repo dir already existed) | |
214 | 204 | await workingGit.addRemote("origin", localRepoPath); |
215 | - await workingGit.push("origin", defaultBranch); | |
205 | + await workingGit.push(["origin", defaultBranch, "--no-verify"]); | |
216 | 206 | logger.info(`[initRepository] Pushed to bare repo`); |
217 | 207 | |
218 | 208 | // Clean up temp directory |
| @@ -228,6 +218,19 @@ export async function initRepository( | ||
228 | 218 | } |
229 | 219 | } |
230 | 220 | |
221 | + // Install hooks AFTER the initial push to avoid pre-receive hook callback | |
222 | + // during initialization (which would fail if server isn't reachable yet) | |
223 | + if (!options.skipHooks) { | |
224 | + try { | |
225 | + logger.info(`[initRepository] Installing hooks`); | |
226 | + await installHooks(localRepoPath); | |
227 | + logger.info(`[initRepository] Hooks installed`); | |
228 | + } catch (err) { | |
229 | + logger.error(`[initRepository] Hook installation failed:`, err); | |
230 | + throw err; | |
231 | + } | |
232 | + } | |
233 | + | |
231 | 234 | // Set default branch in bare repo |
232 | 235 | try { |
233 | 236 | const bareGit = createSimpleGit({ baseDir: localRepoPath }); |
| @@ -991,6 +994,31 @@ export async function getCommitDiff( | ||
991 | 994 | } |
992 | 995 | } |
993 | 996 | |
997 | +/** | |
998 | + * Get the raw unified diff (patch) for a single commit | |
999 | + */ | |
1000 | +export async function getCommitPatchDiff( | |
1001 | + repoPath: string, | |
1002 | + sha: string, | |
1003 | +): Promise<string> { | |
1004 | + const git = getGit(repoPath); | |
1005 | + try { | |
1006 | + const output = await git.raw([ | |
1007 | + "diff-tree", | |
1008 | + "-p", | |
1009 | + "--no-commit-id", | |
1010 | + "-r", | |
1011 | + "-M", | |
1012 | + sha, | |
1013 | + ]); | |
1014 | + return output; | |
1015 | + } catch (error) { | |
1016 | + if (!isExpectedGitError(error)) { | |
1017 | + logger.error({ err: error }, "Error getting commit patch diff"); | |
1018 | + } | |
1019 | + return ""; | |
1020 | + } | |
1021 | +} | |
994 | 1022 | /** |
995 | 1023 | * Get blame for a file |
996 | 1024 | */ |
997 | 1025 | |