| user | 5a7d60d | 2025-07-27 21:22:04 +0400 | [diff] [blame^] | 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | ) |
| 6 | |
| 7 | // GitMutex provides thread-safe access to Git operations |
| 8 | // Since Git is not thread-safe, we need to serialize all Git operations |
| 9 | // across all agents to prevent repository corruption and race conditions |
| 10 | type GitMutex struct { |
| 11 | mu sync.Mutex |
| 12 | } |
| 13 | |
| 14 | // NewGitMutex creates a new GitMutex instance |
| 15 | func NewGitMutex() *GitMutex { |
| 16 | return &GitMutex{} |
| 17 | } |
| 18 | |
| 19 | // Lock acquires the Git operation lock |
| 20 | // This ensures only one agent can perform Git operations at a time |
| 21 | func (gm *GitMutex) Lock() { |
| 22 | gm.mu.Lock() |
| 23 | } |
| 24 | |
| 25 | // Unlock releases the Git operation lock |
| 26 | func (gm *GitMutex) Unlock() { |
| 27 | gm.mu.Unlock() |
| 28 | } |
| 29 | |
| 30 | // WithLock executes a function while holding the Git lock |
| 31 | // This is a convenience method to ensure proper lock/unlock pattern |
| 32 | func (gm *GitMutex) WithLock(fn func() error) error { |
| 33 | gm.Lock() |
| 34 | defer gm.Unlock() |
| 35 | return fn() |
| 36 | } |
| 37 | |
| 38 | // Global Git mutex instance - shared across all agents |
| 39 | // This ensures no concurrent Git operations across the entire application |
| 40 | var GlobalGitMutex = NewGitMutex() |