git-repo-updater/internal/repositories/update.go

37 lines
737 B
Go

package repositories
import (
"fmt"
"os"
"os/exec"
"strings"
)
func Update(repositoryPath string) error {
repositoryPath = strings.TrimSuffix(repositoryPath, "/.git")
err := os.Chdir(repositoryPath)
if err != nil {
return fmt.Errorf("failed to change directory to %s: %w", repositoryPath, err)
}
fmt.Printf("Updating %s\n", repositoryPath)
commands := [][]string{
{"git", "fetch", "--all", "--jobs=4", "--progress", "--prune"},
{"git", "pull", "--rebase"},
}
for _, args := range commands {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s failed: %w", strings.Join(args, " "), err)
}
}
return nil
}