package mirror
import (
"context"
"fmt"
"time"
"nugitea/internal/gitcmd"
"nugitea/internal/repo"
)
func AddPush(ctx context.Context, repos *repo.Store, mirrors *Store, repoName, remoteURL string, interval time.Duration) error {
repoPath, err := repos.Path(repoName)
if err != nil {
return err
}
if !repos.Exists(repoName) {
return fmt.Errorf("repo %q does not exist", repoName)
}
_ = gitcmd.New("remote", "remove", remoteName).WithDir(repoPath).Run(ctx)
if err := gitcmd.New("remote", "add", "--mirror=push", remoteName, remoteURL).WithDir(repoPath).Run(ctx); err != nil {
return fmt.Errorf("git remote add: %w", err)
}
return mirrors.Add(Entry{
Repo: repoName,
RemoteURL: remoteURL,
Direction: Push,
IntervalSeconds: int64(interval.Seconds()),
})
}
func SyncPush(ctx context.Context, repos *repo.Store, e Entry) error {
repoPath, err := repos.Path(e.Repo)
if err != nil {
return err
}
return gitcmd.New("push", "--mirror", "-f", remoteName).WithDir(repoPath).Run(ctx)
}