2021-12-23 01:24:43 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-git/go-git/v5"
|
2021-12-27 15:40:59 +00:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
2021-12-23 01:24:43 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-12-27 17:06:56 +00:00
|
|
|
// Push pushes the latest changes & optionally tags to the default remote
|
|
|
|
func Push(repoDir string, remote string, tags bool, dryRun bool) error {
|
|
|
|
if dryRun {
|
2021-12-27 18:56:27 +00:00
|
|
|
logrus.Debugf("dry run: no git changes pushed in %s", repoDir)
|
2021-12-27 17:06:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
commitRepo, err := git.PlainOpen(repoDir)
|
2021-12-23 01:24:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-27 17:06:56 +00:00
|
|
|
opts := &git.PushOptions{}
|
|
|
|
if remote != "" {
|
|
|
|
opts.RemoteName = remote
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commitRepo.Push(opts); err != nil {
|
2021-12-23 01:24:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-27 17:06:56 +00:00
|
|
|
|
2021-12-27 18:56:27 +00:00
|
|
|
logrus.Debugf("git changes pushed")
|
2021-12-23 01:24:43 +00:00
|
|
|
|
2021-12-27 15:40:59 +00:00
|
|
|
if tags {
|
2021-12-27 17:06:56 +00:00
|
|
|
opts.RefSpecs = append(opts.RefSpecs, config.RefSpec("+refs/tags/*:refs/tags/*"))
|
|
|
|
|
|
|
|
if err := commitRepo.Push(opts); err != nil {
|
2021-12-27 15:40:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-27 17:06:56 +00:00
|
|
|
|
2021-12-27 18:56:27 +00:00
|
|
|
logrus.Debugf("git tags pushed")
|
2021-12-27 15:40:59 +00:00
|
|
|
}
|
2021-12-23 01:24:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|