2024-04-04 20:53:35 +02:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
2024-05-22 02:59:08 +02:00
|
|
|
"strings"
|
2024-04-04 20:53:35 +02:00
|
|
|
|
2024-05-22 03:07:33 +02:00
|
|
|
"git.gnous.eu/ada/spiegel/internal/utils"
|
2024-04-04 20:53:35 +02:00
|
|
|
goGit "github.com/go-git/go-git/v5"
|
2024-05-26 22:00:55 +02:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/serverinfo"
|
2024-05-22 03:07:33 +02:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
2024-05-26 22:00:55 +02:00
|
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
2024-04-04 20:53:35 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-05-26 22:00:55 +02:00
|
|
|
func StartClone(repoList []Config) {
|
2024-04-04 20:53:35 +02:00
|
|
|
logrus.Debug("Start first repository clone")
|
|
|
|
for _, content := range repoList {
|
|
|
|
_, err := os.Stat(content.FullPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
content.fullClone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:00:55 +02:00
|
|
|
func (c Config) fullClone() {
|
2024-05-22 01:45:19 +02:00
|
|
|
logrus.Debug("Clone ", c.Name, "...")
|
2024-04-04 20:53:35 +02:00
|
|
|
logger := logrus.New()
|
|
|
|
w := logger.Writer()
|
|
|
|
defer func(w *io.PipeWriter) {
|
|
|
|
err := w.Close()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
2024-05-22 02:59:08 +02:00
|
|
|
|
|
|
|
return
|
2024-04-04 20:53:35 +02:00
|
|
|
}
|
|
|
|
}(w)
|
|
|
|
|
2024-05-22 01:45:19 +02:00
|
|
|
repoConfig := &goGit.CloneOptions{
|
|
|
|
URL: c.URL,
|
2024-04-04 20:53:35 +02:00
|
|
|
Progress: w,
|
|
|
|
Mirror: true,
|
2024-05-22 01:45:19 +02:00
|
|
|
}
|
|
|
|
|
2024-05-22 03:07:33 +02:00
|
|
|
if !utils.IsHTTPRepo(c.URL) {
|
2024-05-22 02:59:08 +02:00
|
|
|
key, err := os.ReadFile(c.SSHKey)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := strings.Split(c.URL, "@")[0]
|
|
|
|
url := strings.Split(c.URL, "@")[1]
|
|
|
|
|
|
|
|
sshAuth, err := ssh.NewPublicKeys(user, key, "")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
repoConfig.Auth = sshAuth
|
|
|
|
repoConfig.URL = url
|
|
|
|
}
|
|
|
|
|
2024-05-22 01:45:19 +02:00
|
|
|
_, err := goGit.PlainClone(c.FullPath, true, repoConfig)
|
2024-04-04 20:53:35 +02:00
|
|
|
if err != nil {
|
2024-05-22 01:45:19 +02:00
|
|
|
logrus.Error(err)
|
2024-04-04 20:53:35 +02:00
|
|
|
}
|
2024-05-26 22:00:55 +02:00
|
|
|
|
|
|
|
err = c.UpdateInfo()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
2024-04-04 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 22:00:55 +02:00
|
|
|
func (c Config) Update() {
|
2024-05-22 01:45:19 +02:00
|
|
|
repo, err := goGit.PlainOpen(c.FullPath)
|
2024-04-04 20:53:35 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-22 01:45:19 +02:00
|
|
|
logrus.Debug("Clone ", c.Name, "...")
|
2024-04-04 20:53:35 +02:00
|
|
|
logger := logrus.New()
|
|
|
|
w := logger.Writer()
|
|
|
|
defer func(w *io.PipeWriter) {
|
|
|
|
err := w.Close()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}(w)
|
|
|
|
|
2024-05-22 02:59:08 +02:00
|
|
|
fetchConfig := &goGit.FetchOptions{
|
2024-04-04 20:53:35 +02:00
|
|
|
Progress: w,
|
2024-05-22 02:59:08 +02:00
|
|
|
}
|
|
|
|
|
2024-05-22 03:07:33 +02:00
|
|
|
if !utils.IsHTTPRepo(c.URL) {
|
2024-05-22 02:59:08 +02:00
|
|
|
key, err := os.ReadFile(c.SSHKey)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := strings.Split(c.URL, "@")[0]
|
|
|
|
|
|
|
|
sshAuth, err := ssh.NewPublicKeys(user, key, "")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fetchConfig.Auth = sshAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.Fetch(fetchConfig)
|
2024-04-04 20:53:35 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, goGit.NoErrAlreadyUpToDate) {
|
2024-05-22 01:45:19 +02:00
|
|
|
logrus.Info(c.Name, " is already up-to-date")
|
2024-04-04 20:53:35 +02:00
|
|
|
} else {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2024-05-26 22:00:55 +02:00
|
|
|
|
|
|
|
err = c.UpdateInfo()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) UpdateInfo() error {
|
|
|
|
r, err := goGit.PlainOpen(c.FullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = serverinfo.UpdateServerInfo(r.Storer, r.Storer.(*filesystem.Storage).Filesystem()) //nolint: forcetypeassert
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-04-04 20:53:35 +02:00
|
|
|
}
|