sync: Improve git sync reliability
Some checks are pending
continuous-integration/drone/push Build is running

This commit is contained in:
nemunaire 2022-01-22 07:51:02 +01:00
parent 01b05aaed0
commit d4a81aa660

View file

@ -5,9 +5,12 @@ package sync
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
)
@ -77,20 +80,68 @@ func (i GitImporter) Init() error {
func (i GitImporter) Sync() error {
log.Println("Synchronizing local git repository...")
cmdremote := exec.Command("git", "-C", i.li.Base, "pull", "--rebase", "--force", "-X", "theirs", "--recurse-submodules=yes", "origin")
stdout, err := cmdremote.CombinedOutput()
cmdfetch := exec.Command("git", "-C", i.li.Base, "fetch", "origin")
stdout, err := cmdfetch.CombinedOutput()
if err != nil {
log.Printf("Local git repository synchronization failed: %s\n%s", err, stdout)
log.Printf("Git repository fetch failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
cmdsubmodule := exec.Command("git", "-C", i.li.Base, "submodule", "foreach", "--recursive", "git", "lfs", "pull")
stdout, err = cmdsubmodule.CombinedOutput()
cmdclean := exec.Command("git", "-C", i.li.Base, "clean", "-xfde", "*_MERGED")
stdout, err = cmdclean.CombinedOutput()
if err != nil {
log.Printf("Local LFS synchronization failed: %s\n%s", err, stdout)
log.Printf("Local git repository clean failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
if _, err := os.Stat(path.Join(i.li.Base, ".gitmodules")); !errors.Is(err, os.ErrNotExist) {
// We have submodules, clean it
cmdsubclean := exec.Command("git", "-C", i.li.Base, "submodule", "foreach", "--recursive", "git", "clean", "-xfde", "*_MERGED")
stdout, err = cmdsubclean.CombinedOutput()
if err != nil {
log.Printf("Local git repository submodules clean failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
}
cmdreset := exec.Command("git", "-C", i.li.Base, "reset", "--hard", "origin/master")
stdout, err = cmdreset.CombinedOutput()
if err != nil {
log.Printf("Local git repository reset failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
if _, err := os.Stat(path.Join(i.li.Base, ".gitmodules")); !errors.Is(err, os.ErrNotExist) {
// Treat submodules
cmdsubreset := exec.Command("git", "-C", i.li.Base, "submodule", "foreach", "--recursive", "git", "reset", "--hard")
stdout, err = cmdsubreset.CombinedOutput()
if err != nil {
log.Printf("Local git repository submodule reset failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
cmdsubupdate := exec.Command("git", "-C", i.li.Base, "submodule", "update", "--init", "--recursive")
stdout, err = cmdsubupdate.CombinedOutput()
if err != nil {
log.Printf("Local git repository submodule update failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
cmdsublfs := exec.Command("git", "-C", i.li.Base, "submodule", "foreach", "--recursive", "git", "lfs", "pull")
stdout, err = cmdsublfs.CombinedOutput()
if err != nil {
log.Printf("Local LFS synchronization failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
} else {
cmdlfs := exec.Command("git", "-C", i.li.Base, "lfs", "pull")
stdout, err = cmdlfs.CombinedOutput()
if err != nil {
log.Printf("Local LFS synchronization failed: %s\n%s", err, stdout)
return fmt.Errorf("%w:\n%s", err, stdout)
}
}
log.Println("Local git repository synchronized successfully")
return nil
}