fix: don't block on git push waiting for config reload

Ayman Bagabas created

Change summary

internal/config/config.go |  4 ++++
internal/config/git.go    | 16 +++++++++-------
2 files changed, 13 insertions(+), 7 deletions(-)

Detailed changes

internal/config/config.go 🔗

@@ -4,6 +4,7 @@ import (
 	"os/exec"
 	"path/filepath"
 	"strings"
+	"sync"
 
 	"golang.org/x/crypto/ssh"
 	"gopkg.in/yaml.v2"
@@ -28,6 +29,7 @@ type Config struct {
 	Repos        []Repo `yaml:"repos"`
 	Source       *git.RepoSource
 	Cfg          *config.Config
+	reloadMtx    sync.Mutex
 }
 
 // User contains user-level configuration for a repository.
@@ -107,6 +109,8 @@ func NewConfig(cfg *config.Config) (*Config, error) {
 
 // Reload reloads the configuration.
 func (cfg *Config) Reload() error {
+	cfg.reloadMtx.Lock()
+	defer cfg.reloadMtx.Unlock()
 	err := cfg.Source.LoadRepos()
 	if err != nil {
 		return err

internal/config/git.go 🔗

@@ -10,13 +10,15 @@ import (
 
 // Push registers Git push functionality for the given repo and key.
 func (cfg *Config) Push(repo string, pk ssh.PublicKey) {
-	err := cfg.Reload()
-	if err != nil {
-		log.Printf("error reloading after push: %s", err)
-	}
-	if cfg.Cfg.Callbacks != nil {
-		cfg.Cfg.Callbacks.Push(repo)
-	}
+	go func() {
+		err := cfg.Reload()
+		if err != nil {
+			log.Printf("error reloading after push: %s", err)
+		}
+		if cfg.Cfg.Callbacks != nil {
+			cfg.Cfg.Callbacks.Push(repo)
+		}
+	}()
 }
 
 // Fetch registers Git fetch functionality for the given repo and key.