( worktreePath: string, operation: () => Promise<T> )
| 68 | * @returns Result of the operation |
| 69 | */ |
| 70 | export async function withGitLock<T>( |
| 71 | worktreePath: string, |
| 72 | operation: () => Promise<T> |
| 73 | ): Promise<T> { |
| 74 | // Wait for any existing operation to complete |
| 75 | const existing = operationLocks.get(worktreePath); |
| 76 | if (existing) { |
| 77 | await existing.catch(() => {}); // Ignore errors from previous operation |
| 78 | } |
| 79 | |
| 80 | // Create a new lock for this operation |
| 81 | let resolveLock: () => void; |
| 82 | const lock = new Promise<void>((resolve) => { |
| 83 | resolveLock = resolve; |
| 84 | }); |
| 85 | operationLocks.set(worktreePath, lock); |
| 86 | |
| 87 | try { |
| 88 | return await operation(); |
| 89 | } finally { |
| 90 | resolveLock!(); |
| 91 | // Clean up the lock if it's still ours |
| 92 | if (operationLocks.get(worktreePath) === lock) { |
| 93 | operationLocks.delete(worktreePath); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Known lock files that git creates during operations |
no test coverage detected