2021-12-12 19:23:55 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2023-12-01 07:58:53 +01:00
|
|
|
"encoding/json"
|
2023-01-08 20:22:07 +01:00
|
|
|
"fmt"
|
2023-08-15 18:13:21 +02:00
|
|
|
"math/rand"
|
2021-12-12 19:23:55 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-12-01 07:58:53 +01:00
|
|
|
"path/filepath"
|
2023-01-08 20:22:07 +01:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-12-12 19:23:55 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
"github.com/cooperspencer/gickup/gitcmd"
|
2023-10-04 10:55:24 +02:00
|
|
|
"github.com/cooperspencer/gickup/logger"
|
2022-03-20 11:25:11 +01:00
|
|
|
"github.com/cooperspencer/gickup/types"
|
2024-05-10 11:48:48 +02:00
|
|
|
"github.com/cooperspencer/gickup/zip"
|
2021-12-12 19:23:55 +01:00
|
|
|
"github.com/go-git/go-git/v5"
|
2022-02-24 13:00:17 +01:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
2024-02-19 08:42:35 +01:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2021-12-12 19:23:55 +01:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
|
|
|
"github.com/melbahja/goph"
|
2023-10-04 10:55:24 +02:00
|
|
|
"github.com/rs/zerolog"
|
2021-12-12 19:23:55 +01:00
|
|
|
gossh "golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
var (
|
|
|
|
gitc = gitcmd.GitCmd{}
|
2023-10-04 10:55:24 +02:00
|
|
|
sub zerolog.Logger
|
2023-07-18 10:56:43 +02:00
|
|
|
)
|
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
// Locally TODO.
|
2023-02-10 06:07:11 +01:00
|
|
|
func Locally(repo types.Repo, l types.Local, dry bool) bool {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub = logger.CreateSubLogger("stage", "locally", "path", l.Path)
|
2023-07-18 10:56:43 +02:00
|
|
|
if l.LFS {
|
|
|
|
g, err := gitcmd.New()
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Error().
|
2023-07-18 10:56:43 +02:00
|
|
|
Msg(err.Error())
|
|
|
|
}
|
|
|
|
gitc = g
|
|
|
|
}
|
2023-01-08 20:22:07 +01:00
|
|
|
date := time.Now()
|
|
|
|
|
2021-12-16 17:22:44 +01:00
|
|
|
if l.Structured {
|
|
|
|
repo.Name = path.Join(repo.Hoster, repo.Owner, repo.Name)
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2023-01-11 11:00:48 +01:00
|
|
|
if l.Bare {
|
|
|
|
repo.Name += ".git"
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:22:07 +01:00
|
|
|
if l.Keep > 0 {
|
|
|
|
repo.Name = path.Join(repo.Name, fmt.Sprint(date.Unix()))
|
|
|
|
}
|
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
_, err := os.Stat(l.Path)
|
2021-12-12 19:23:55 +01:00
|
|
|
if os.IsNotExist(err) && !dry {
|
2024-04-04 07:31:28 +02:00
|
|
|
if err = os.MkdirAll(l.Path, 0o777); err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Error().
|
2022-03-20 11:25:11 +01:00
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
2023-02-10 06:07:11 +01:00
|
|
|
return false
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
_, err = os.Stat(l.Path)
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
if err != nil {
|
|
|
|
sub.Error().
|
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
return false
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tries := 5
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2021-12-12 19:23:55 +01:00
|
|
|
var auth transport.AuthMethod
|
2022-03-20 11:25:11 +01:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case repo.Origin.SSH:
|
2021-12-12 19:23:55 +01:00
|
|
|
if repo.Origin.SSHKey == "" {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
repo.Origin.SSHKey = path.Join(home, ".ssh", "id_rsa")
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2021-12-12 19:23:55 +01:00
|
|
|
auth, err = ssh.NewPublicKeysFromFile("git", repo.Origin.SSHKey, "")
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Error().
|
2023-02-10 06:07:11 +01:00
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
2023-02-10 06:07:11 +01:00
|
|
|
return false
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
case repo.Token != "":
|
2024-09-02 16:11:52 +02:00
|
|
|
if repo.NoTokenUser {
|
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: repo.Token,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: "xyz",
|
|
|
|
Password: repo.Token,
|
|
|
|
}
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
case repo.Origin.Username != "" && repo.Origin.Password != "":
|
2021-12-12 19:23:55 +01:00
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: repo.Origin.Username,
|
|
|
|
Password: repo.Origin.Password,
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2021-12-12 19:23:55 +01:00
|
|
|
for x := 1; x <= tries; x++ {
|
2024-04-04 07:31:28 +02:00
|
|
|
stat, err := os.Stat(filepath.Join(l.Path, repo.Name))
|
2021-12-12 19:23:55 +01:00
|
|
|
if os.IsNotExist(err) {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2022-03-20 11:25:11 +01:00
|
|
|
Msgf("cloning %s", types.Green(repo.Name))
|
2021-12-12 19:23:55 +01:00
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
err := cloneRepository(repo, auth, dry, l)
|
2021-12-22 05:49:34 +01:00
|
|
|
if err != nil {
|
2022-09-24 18:14:07 +02:00
|
|
|
if err.Error() == "repository not found" {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2022-09-24 18:14:07 +02:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
|
|
|
return false
|
2022-09-24 18:14:07 +02:00
|
|
|
}
|
2021-12-22 05:49:34 +01:00
|
|
|
if x == tries {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2022-03-20 11:25:11 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msg(err.Error())
|
|
|
|
|
2024-09-04 19:22:15 +02:00
|
|
|
return false
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "ERR access denied or repository not exported") {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2022-03-20 11:25:11 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msgf("%s doesn't exist.", repo.Name)
|
|
|
|
|
2024-09-04 19:22:15 +02:00
|
|
|
return false
|
2022-03-20 11:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "remote repository is empty") {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2022-03-20 11:25:11 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msg(err.Error())
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-04-27 11:23:01 +02:00
|
|
|
/*
|
|
|
|
err = os.RemoveAll(filepath.Join(l.Path, repo.Name))
|
|
|
|
if err != nil {
|
|
|
|
dir, _ := filepath.Abs(filepath.Join(l.Path, repo.Name))
|
|
|
|
sub.Warn().
|
|
|
|
Str("repo", repo.Name).Err(err).
|
|
|
|
Msgf("couldn't remove %s", types.Red(dir))
|
|
|
|
}
|
|
|
|
*/
|
2024-01-03 09:15:34 +01:00
|
|
|
|
|
|
|
sub.Warn().Err(err).
|
2022-03-20 11:25:11 +01:00
|
|
|
Msgf("retry %s from %s", types.Red(x), types.Red(tries))
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
continue
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-22 05:49:34 +01:00
|
|
|
if !stat.IsDir() {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2022-03-20 11:25:11 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msgf("%s is a file", types.Red(repo.Name))
|
2021-12-22 05:49:34 +01:00
|
|
|
} else {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2022-03-20 11:25:11 +01:00
|
|
|
Msgf("opening %s locally", types.Green(repo.Name))
|
2021-12-12 19:23:55 +01:00
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
err := updateRepository(repo.Name, auth, dry, l)
|
2021-12-22 05:49:34 +01:00
|
|
|
if err != nil {
|
2023-08-16 14:04:59 +02:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2022-03-20 11:25:11 +01:00
|
|
|
Msg(err.Error())
|
2021-12-22 05:49:34 +01:00
|
|
|
} else {
|
|
|
|
if x == tries {
|
2024-01-03 09:15:34 +01:00
|
|
|
sub.Warn().
|
2022-03-20 11:25:11 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
|
|
|
return false
|
2021-12-12 19:23:55 +01:00
|
|
|
} else {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2024-01-03 09:15:34 +01:00
|
|
|
Str("repo", repo.Name).Err(err).
|
2022-03-20 11:25:11 +01:00
|
|
|
Msgf("retry %s from %s", types.Red(x), types.Red(tries))
|
|
|
|
|
2021-12-22 05:49:34 +01:00
|
|
|
time.Sleep(5 * time.Second)
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2021-12-22 05:49:34 +01:00
|
|
|
continue
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2023-12-01 07:58:53 +01:00
|
|
|
if len(repo.Issues) > 0 {
|
2024-04-04 07:31:28 +02:00
|
|
|
_, err := os.Stat(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)))
|
2023-12-01 07:58:53 +01:00
|
|
|
if os.IsNotExist(err) && !dry {
|
2024-04-27 11:49:50 +02:00
|
|
|
if err := os.MkdirAll(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)), 0o777); err != nil {
|
2023-12-01 07:58:53 +01:00
|
|
|
sub.Error().
|
|
|
|
Msg(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 07:31:28 +02:00
|
|
|
issuesDir, err := filepath.Abs(filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)))
|
2023-12-04 13:24:18 +01:00
|
|
|
if err != nil {
|
|
|
|
sub.Error().
|
|
|
|
Msg(err.Error())
|
|
|
|
} else {
|
|
|
|
sub.Info().Str("repo", repo.Name).Msg("backing up issues")
|
|
|
|
if !dry {
|
|
|
|
for k, v := range repo.Issues {
|
|
|
|
jsonData, err := json.Marshal(v)
|
2023-12-01 07:58:53 +01:00
|
|
|
if err != nil {
|
|
|
|
sub.Error().
|
|
|
|
Msg(err.Error())
|
2023-12-04 13:24:18 +01:00
|
|
|
} else {
|
|
|
|
err = os.WriteFile(filepath.Join(issuesDir, fmt.Sprintf("%s.json", k)), jsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
sub.Error().
|
|
|
|
Msg(err.Error())
|
|
|
|
}
|
2023-12-01 07:58:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 20:22:07 +01:00
|
|
|
if l.Zip {
|
2024-04-04 07:31:28 +02:00
|
|
|
tozip := []string{filepath.Join(l.Path, repo.Name)}
|
2023-12-01 07:58:53 +01:00
|
|
|
|
|
|
|
if len(repo.Issues) > 0 {
|
2024-04-27 16:32:02 +02:00
|
|
|
tozip = append(tozip, filepath.Join(l.Path, fmt.Sprintf("%s.issues", repo.Name)))
|
2023-12-01 07:58:53 +01:00
|
|
|
}
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2023-01-08 20:22:07 +01:00
|
|
|
Msgf("zipping %s", types.Green(repo.Name))
|
2024-05-10 11:48:48 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s.zip", filepath.Join(l.Path, repo.Name))); !os.IsNotExist(err) {
|
|
|
|
sub.Warn().Str("repo", repo.Name).Msgf("will overwrite %s.zip", filepath.Join(l.Path, repo.Name))
|
2023-01-08 20:22:07 +01:00
|
|
|
}
|
2024-05-10 11:48:48 +02:00
|
|
|
|
|
|
|
err := zip.Zip(filepath.Join(l.Path, repo.Name), tozip)
|
|
|
|
if err != nil {
|
|
|
|
sub.Error().
|
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
2024-05-10 11:48:48 +02:00
|
|
|
return false
|
2023-01-08 20:22:07 +01:00
|
|
|
}
|
2024-05-10 11:48:48 +02:00
|
|
|
|
2023-01-08 20:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.Keep > 0 {
|
2024-04-04 07:31:28 +02:00
|
|
|
parentdir := path.Dir(filepath.Join(l.Path, repo.Name))
|
2023-08-15 18:13:21 +02:00
|
|
|
files, err := os.ReadDir(parentdir)
|
2023-01-08 20:22:07 +01:00
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2023-02-20 16:05:01 +01:00
|
|
|
Str("repo", repo.Name).Msg(err.Error())
|
2024-09-04 19:22:15 +02:00
|
|
|
|
|
|
|
return false
|
2023-01-08 20:22:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
keep := []string{}
|
|
|
|
for _, file := range files {
|
|
|
|
fname := file.Name()
|
|
|
|
if l.Zip {
|
|
|
|
fname = strings.TrimSuffix(file.Name(), ".zip")
|
|
|
|
}
|
2024-04-27 16:32:02 +02:00
|
|
|
|
|
|
|
fname = strings.TrimSuffix(fname, ".issues")
|
|
|
|
|
2023-01-08 20:22:07 +01:00
|
|
|
_, err := strconv.ParseInt(fname, 10, 64)
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2023-01-08 20:22:07 +01:00
|
|
|
Str("repo", repo.Name).
|
|
|
|
Msgf("couldn't parse timestamp! %s", types.Red(file.Name()))
|
|
|
|
}
|
|
|
|
if l.Zip && !strings.HasSuffix(file.Name(), ".zip") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keep = append(keep, file.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(keep)))
|
|
|
|
|
|
|
|
if len(keep) > l.Keep {
|
|
|
|
toremove := keep[l.Keep:]
|
|
|
|
for _, file := range toremove {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2023-01-08 20:22:07 +01:00
|
|
|
Msgf("removing %s", types.Red(path.Join(parentdir, file)))
|
|
|
|
err := os.RemoveAll(path.Join(parentdir, file))
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Warn().
|
2023-02-20 16:05:01 +01:00
|
|
|
Str("repo", repo.Name).Msg(err.Error())
|
2023-01-08 20:22:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 19:23:55 +01:00
|
|
|
x = 5
|
|
|
|
}
|
2024-04-04 07:31:28 +02:00
|
|
|
|
2023-02-10 06:07:11 +01:00
|
|
|
return true
|
2021-12-12 19:23:55 +01:00
|
|
|
}
|
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
func updateRepository(reponame string, auth transport.AuthMethod, dry bool, l types.Local) error {
|
|
|
|
r, err := git.PlainOpen(filepath.Join(l.Path, reponame))
|
2021-12-22 05:49:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dry {
|
2023-07-18 10:56:43 +02:00
|
|
|
if l.LFS {
|
2024-04-04 07:31:28 +02:00
|
|
|
_, err = os.Stat(filepath.Join(l.Path, reponame))
|
2023-01-11 11:00:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Info().
|
2024-04-04 07:31:28 +02:00
|
|
|
Msgf("pulling %s", types.Green(reponame))
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2024-04-04 07:31:28 +02:00
|
|
|
err = gitc.Pull(l.Bare, filepath.Join(l.Path, reponame))
|
2023-07-18 10:56:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-27 11:23:01 +02:00
|
|
|
// fetch to see if there are any unpullable commits, for example a force push
|
|
|
|
err = r.Fetch(&git.FetchOptions{Auth: auth, RemoteName: "origin"})
|
|
|
|
if err != nil {
|
2024-02-19 08:42:35 +01:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
err = nil
|
|
|
|
} else {
|
2023-07-18 10:56:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2024-04-27 11:23:01 +02:00
|
|
|
}
|
2024-08-30 12:31:05 +02:00
|
|
|
sub.Info().
|
|
|
|
Msgf("pulling %s", types.Green(reponame))
|
2024-04-27 11:23:01 +02:00
|
|
|
if !l.Bare {
|
|
|
|
w, err := r.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 10:56:43 +02:00
|
|
|
err = w.Pull(&git.PullOptions{Auth: auth, RemoteName: "origin", SingleBranch: false})
|
2024-02-19 08:42:35 +01:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
err = nil
|
|
|
|
} else {
|
2023-10-03 15:53:43 +02:00
|
|
|
return err
|
2024-04-27 11:23:01 +02:00
|
|
|
}
|
2024-08-30 12:31:05 +02:00
|
|
|
}
|
|
|
|
// if everything was ok, fetch everything
|
|
|
|
err = r.Fetch(&git.FetchOptions{Auth: auth, RemoteName: "origin", RefSpecs: []config.RefSpec{"+refs/*:refs/*"}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-18 10:56:43 +02:00
|
|
|
}
|
2023-01-11 11:00:48 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-22 05:49:34 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
func cloneRepository(repo types.Repo, auth transport.AuthMethod, dry bool, l types.Local) error {
|
2022-03-20 11:25:11 +01:00
|
|
|
if dry {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-22 05:49:34 +01:00
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
url := repo.URL
|
|
|
|
if repo.Origin.SSH {
|
|
|
|
url = repo.SSHURL
|
|
|
|
site := types.Site{}
|
2021-12-22 05:49:34 +01:00
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
err := site.GetValues(url)
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Str("repo", repo.Name).Msg(err.Error())
|
2021-12-22 05:49:34 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
sshAuth, err := goph.Key(repo.Origin.SSHKey, "")
|
2022-02-24 13:00:17 +01:00
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Str("repo", repo.Name).Msg(err.Error())
|
2022-03-20 11:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = testSSHConnection(site, sshAuth)
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Str("repo", repo.Name).Msg(err.Error())
|
2022-02-24 13:00:17 +01:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteConfig := config.RemoteConfig{
|
|
|
|
Name: "origin",
|
|
|
|
URLs: []string{url},
|
|
|
|
}
|
2022-02-24 13:00:17 +01:00
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
rem := git.NewRemote(nil, &remoteConfig)
|
2021-12-22 05:49:34 +01:00
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
_, err := rem.List(&git.ListOptions{Auth: auth})
|
|
|
|
if err != nil {
|
2021-12-22 05:49:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
2023-07-18 10:56:43 +02:00
|
|
|
if l.LFS {
|
2024-04-04 07:31:28 +02:00
|
|
|
if repo.Token != "" {
|
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
url = strings.Replace(url, "http://", fmt.Sprintf("http://xyz:%s@", repo.Token), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(url, "https://") {
|
|
|
|
url = strings.Replace(url, "https://", fmt.Sprintf("https://xyz:%s@", repo.Token), -1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if repo.Origin.Username != "" && repo.Origin.Password != "" {
|
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
url = strings.Replace(url, "http://", fmt.Sprintf("http://%s:%s@", repo.Origin.Username, repo.Origin.Password), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(url, "https://") {
|
|
|
|
url = strings.Replace(url, "https://", fmt.Sprintf("https://%s:%s@", repo.Origin.Username, repo.Origin.Password), -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.Clone(url, filepath.Join(l.Path, repo.Name), l.Bare)
|
2023-07-18 10:56:43 +02:00
|
|
|
} else {
|
2024-02-19 08:42:35 +01:00
|
|
|
r := &git.Repository{}
|
2024-04-04 07:31:28 +02:00
|
|
|
r, err = git.PlainClone(filepath.Join(l.Path, repo.Name), l.Bare, &git.CloneOptions{
|
2023-07-18 10:56:43 +02:00
|
|
|
URL: url,
|
|
|
|
Auth: auth,
|
|
|
|
SingleBranch: false,
|
|
|
|
})
|
2024-02-19 08:42:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = r.Fetch(&git.FetchOptions{
|
|
|
|
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
|
|
|
|
Auth: auth,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
err = nil
|
|
|
|
}
|
2023-07-18 10:56:43 +02:00
|
|
|
}
|
2022-03-20 11:25:11 +01:00
|
|
|
|
|
|
|
return err
|
2021-12-22 05:49:34 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
func testSSHConnection(site types.Site, sshAuth goph.Auth) error {
|
2021-12-22 05:49:34 +01:00
|
|
|
_, err := goph.NewConn(&goph.Config{
|
|
|
|
User: site.User,
|
2022-03-20 11:25:11 +01:00
|
|
|
Addr: site.URL,
|
2021-12-22 05:49:34 +01:00
|
|
|
Port: uint(site.Port),
|
|
|
|
Auth: sshAuth,
|
|
|
|
Callback: VerifyHost,
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-20 11:25:11 +01:00
|
|
|
// VerifyHost TODO.
|
2021-12-12 19:23:55 +01:00
|
|
|
func VerifyHost(host string, remote net.Addr, key gossh.PublicKey) error {
|
2022-03-20 11:25:11 +01:00
|
|
|
// Got from the example from
|
|
|
|
// https://github.com/melbahja/goph/blob/master/examples/goph/main.go
|
2021-12-12 19:23:55 +01:00
|
|
|
//
|
|
|
|
// If you want to connect to new hosts.
|
|
|
|
// here your should check new connections public keys
|
|
|
|
// if the key not trusted you shuld return an error
|
|
|
|
//
|
|
|
|
|
|
|
|
// hostFound: is host in known hosts file.
|
2022-03-20 11:25:11 +01:00
|
|
|
// err: error if key not in known hosts file
|
|
|
|
// OR host in known hosts file but key changed!
|
2021-12-12 19:23:55 +01:00
|
|
|
hostFound, err := goph.CheckKnownHost(host, remote, key, "")
|
|
|
|
// Host in known hosts but key mismatch!
|
|
|
|
// Maybe because of MAN IN THE MIDDLE ATTACK!
|
|
|
|
/*
|
|
|
|
if hostFound && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// handshake because public key already exists.
|
|
|
|
if hostFound && err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new host to known hosts file.
|
|
|
|
return goph.AddKnownHost(host, remote, key, "")
|
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
|
|
|
|
func TempClone(repo types.Repo, tempdir string) (*git.Repository, error) {
|
|
|
|
var auth transport.AuthMethod
|
|
|
|
if repo.Token != "" {
|
2024-09-02 17:14:16 +02:00
|
|
|
if repo.NoTokenUser {
|
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: repo.Token,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: "xyz",
|
|
|
|
Password: repo.Token,
|
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-19 08:42:35 +01:00
|
|
|
if repo.Origin.LFS {
|
|
|
|
g, err := gitcmd.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gitc = g
|
|
|
|
|
|
|
|
if strings.HasPrefix(repo.URL, "http://") {
|
|
|
|
repo.URL = strings.Replace(repo.URL, "http://", fmt.Sprintf("http://xyz:%s@", repo.Token), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(repo.URL, "https://") {
|
|
|
|
repo.URL = strings.Replace(repo.URL, "https://", fmt.Sprintf("https://xyz:%s@", repo.Token), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.Clone(repo.URL, tempdir, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := git.PlainOpen(tempdir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = r.Fetch(&git.FetchOptions{
|
|
|
|
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
|
|
|
|
Auth: auth,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the symbolic reference for HEAD
|
|
|
|
headRef, err := r.Head()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the list of branches
|
|
|
|
refs, err := r.Branches()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the names of branches
|
|
|
|
err = refs.ForEach(func(ref *plumbing.Reference) error {
|
|
|
|
if ref.Name().Short() != headRef.Name().Short() {
|
|
|
|
return gitc.Checkout(tempdir, ref.Name().Short())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.Checkout(tempdir, headRef.Name().Short())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.MirrorPull(tempdir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
|
2023-08-16 14:04:59 +02:00
|
|
|
return r, err
|
2024-02-19 08:42:35 +01:00
|
|
|
} else {
|
|
|
|
r, err := git.PlainClone(tempdir, false, &git.CloneOptions{
|
|
|
|
URL: repo.URL,
|
|
|
|
Auth: auth,
|
|
|
|
SingleBranch: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Fetch(&git.FetchOptions{
|
|
|
|
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
|
|
|
|
Auth: auth,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
return r, nil
|
|
|
|
} else {
|
|
|
|
return r, err
|
|
|
|
}
|
2023-08-16 14:04:59 +02:00
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-19 08:42:35 +01:00
|
|
|
func CreateRemotePush(repo *git.Repository, destination types.GenRepo, url string, lfs bool) error {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub = logger.CreateSubLogger("stage", "tempclone", "url", url)
|
2023-08-15 18:13:21 +02:00
|
|
|
token := destination.GetToken()
|
|
|
|
var auth transport.AuthMethod
|
|
|
|
if destination.SSH {
|
|
|
|
if destination.SSHKey == "" {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
destination.SSHKey = path.Join(home, ".ssh", "id_rsa")
|
|
|
|
}
|
|
|
|
site := types.Site{}
|
|
|
|
|
|
|
|
err := site.GetValues(url)
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Msg(err.Error())
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sshAuth, err := goph.Key(destination.SSHKey, "")
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Msg(err.Error())
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = testSSHConnection(site, sshAuth)
|
|
|
|
if err != nil {
|
2023-10-04 10:55:24 +02:00
|
|
|
sub.Fatal().Msg(err.Error())
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
if destination.SSHKey == "" {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
destination.SSHKey = path.Join(home, ".ssh", "id_rsa")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth, err = ssh.NewPublicKeysFromFile("git", destination.SSHKey, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auth = &http.BasicAuth{
|
|
|
|
Username: "xyz",
|
|
|
|
Password: token,
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 08:42:35 +01:00
|
|
|
if lfs {
|
|
|
|
g, err := gitcmd.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gitc = g
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
|
2024-02-19 08:42:35 +01:00
|
|
|
remote := RandomString(8)
|
2023-08-16 14:04:59 +02:00
|
|
|
|
2024-02-19 08:42:35 +01:00
|
|
|
if destination.SSH {
|
|
|
|
err = gitc.NewRemote(remote, url, worktree.Filesystem.Root())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-16 14:04:59 +02:00
|
|
|
|
2024-02-19 08:42:35 +01:00
|
|
|
err = gitc.SSHPush(worktree.Filesystem.Root(), remote, destination.SSHKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(url, "http://") {
|
|
|
|
url = strings.Replace(url, "http://", fmt.Sprintf("http://xyz:%s@", token), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(url, "https://") {
|
|
|
|
url = strings.Replace(url, "https://", fmt.Sprintf("https://xyz:%s@", token), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.NewRemote(remote, url, worktree.Filesystem.Root())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitc.Push(worktree.Filesystem.Root(), remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
|
2024-02-19 08:42:35 +01:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
remoteconfig := config.RemoteConfig{Name: RandomString(8), URLs: []string{url}}
|
|
|
|
remote, err := repo.CreateRemote(&remoteconfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
headref, _ := repo.Head()
|
|
|
|
|
|
|
|
pushoptions := git.PushOptions{Force: destination.Force, Auth: auth, RemoteName: remote.Config().Name, RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf("%s:%s", headref.Name(), headref.Name()))}}
|
|
|
|
|
|
|
|
err = repo.Push(&pushoptions)
|
|
|
|
if err == nil || err == git.NoErrAlreadyUpToDate {
|
|
|
|
pushoptions = git.PushOptions{Force: destination.Force, Auth: auth, RemoteName: remote.Config().Name, RefSpecs: []config.RefSpec{"refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*"}}
|
|
|
|
|
|
|
|
return repo.Push(&pushoptions)
|
|
|
|
}
|
|
|
|
return err
|
2023-08-16 14:04:59 +02:00
|
|
|
}
|
2023-08-15 18:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RandomString(length int) string {
|
|
|
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
b := make([]byte, length)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = charset[seededRand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|