abra/pkg/git/push.go
decentral1se 4d7c812fe2
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
WIP: operator collaboration MVP
See toolshed/organising#467
2024-12-30 00:46:25 +01:00

44 lines
829 B
Go

package git
import (
"coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)
// Push pushes the latest changes & optionally tags to the default remote
func Push(repoDir string, remote string, tags bool, dryRun bool) error {
if dryRun {
log.Debugf("dry run: no git changes pushed in %s", repoDir)
return nil
}
commitRepo, err := git.PlainOpen(repoDir)
if err != nil {
return err
}
opts := &git.PushOptions{}
if remote != "" {
opts.RemoteName = remote
}
if err := commitRepo.Push(opts); err != nil {
return err
}
log.Debug("git changes pushed")
if tags {
opts.RefSpecs = append(opts.RefSpecs, config.RefSpec("+refs/tags/*:refs/tags/*"))
if err := commitRepo.Push(opts); err != nil {
return err
}
log.Debug("git tags pushed")
}
return nil
}