Semaphore/db_lib/GitRepository.go

76 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-09-23 17:12:35 +02:00
package db_lib
import (
"github.com/semaphoreui/semaphore/util"
"os"
"path"
2023-02-26 07:22:47 +01:00
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/pkg/task_logger"
)
type GitRepositoryDirType int
const (
GitRepositoryTmpPath GitRepositoryDirType = iota
GitRepositoryFullPath
)
2023-02-26 07:22:47 +01:00
type GitClient interface {
Clone(r GitRepository) error
Pull(r GitRepository) error
Checkout(r GitRepository, target string) error
CanBePulled(r GitRepository) bool
GetLastCommitMessage(r GitRepository) (msg string, err error)
GetLastCommitHash(r GitRepository) (hash string, err error)
GetLastRemoteCommitHash(r GitRepository) (hash string, err error)
}
type GitRepository struct {
TmpDirName string
TemplateID int
Repository db.Repository
Logger task_logger.Logger
2023-02-26 07:22:47 +01:00
Client GitClient
}
func (r GitRepository) GetFullPath() string {
if r.TmpDirName != "" {
return path.Join(util.Config.TmpPath, r.TmpDirName)
}
return r.Repository.GetFullPath(r.TemplateID)
}
2023-02-26 07:22:47 +01:00
func (r GitRepository) ValidateRepo() error {
_, err := os.Stat(r.GetFullPath())
return err
}
func (r GitRepository) Clone() error {
2023-02-26 07:22:47 +01:00
return r.Client.Clone(r)
}
func (r GitRepository) Pull() error {
2023-02-26 07:22:47 +01:00
return r.Client.Pull(r)
}
func (r GitRepository) Checkout(target string) error {
2023-02-26 07:22:47 +01:00
return r.Client.Checkout(r, target)
}
func (r GitRepository) CanBePulled() bool {
2023-02-26 07:22:47 +01:00
return r.Client.CanBePulled(r)
}
func (r GitRepository) GetLastCommitMessage() (msg string, err error) {
2023-02-26 07:22:47 +01:00
return r.Client.GetLastCommitMessage(r)
}
func (r GitRepository) GetLastCommitHash() (hash string, err error) {
2023-02-26 07:22:47 +01:00
return r.Client.GetLastCommitHash(r)
}
2022-01-30 18:43:15 +01:00
func (r GitRepository) GetLastRemoteCommitHash() (hash string, err error) {
2023-02-26 07:22:47 +01:00
return r.Client.GetLastRemoteCommitHash(r)
2022-01-30 18:43:15 +01:00
}