MCPcopy
hub / github.com/21st-dev/1code / withGitLock

Function withGitLock

src/main/lib/git/git-factory.ts:70–96  ·  view source on GitHub ↗
(
	worktreePath: string,
	operation: () => Promise<T>
)

Source from the content-addressed store, hash-verified

68 * @returns Result of the operation
69 */
70export 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

Callers 2

createBranchesRouterFunction · 0.90

Calls 3

getMethod · 0.45
setMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected