splitsh-lite/splitter/config.go

100 lines
2 KiB
Go
Raw Normal View History

2016-06-06 18:12:57 +02:00
package splitter
import (
"fmt"
"log"
2024-03-08 10:57:23 +01:00
"strings"
2016-06-06 18:12:57 +02:00
"sync"
git "github.com/libgit2/git2go/v34"
bolt "go.etcd.io/bbolt"
2016-06-06 18:12:57 +02:00
)
// Prefix represents which paths to split
type Prefix struct {
2024-03-08 08:28:46 +01:00
From string
To string
Excludes []string
2016-06-06 18:12:57 +02:00
}
2024-03-08 10:57:23 +01:00
// NewPrefix returns a new prefix, sanitizing the input
func NewPrefix(from, to string, excludes []string) *Prefix {
// remove the trailing slash (to avoid duplicating cache)
from = strings.TrimRight(from, "/")
to = strings.TrimRight(to, "/")
// remove trailing slashes from excludes (as it does not mean anything)
for i, exclude := range excludes {
excludes[i] = strings.TrimRight(exclude, "/")
}
return &Prefix{
From: from,
To: to,
Excludes: excludes,
}
}
2016-06-06 18:12:57 +02:00
// Config represents a split configuration
type Config struct {
2016-07-31 16:35:17 +02:00
Prefixes []*Prefix
Path string
Origin string
Commit string
Target string
GitVersion string
Debug bool
Scratch bool
2016-06-06 18:12:57 +02:00
// for advanced usage only
// naming and types subject to change anytime!
Logger *log.Logger
DB *bolt.DB
RepoMu *sync.Mutex
Repo *git.Repository
2016-07-31 16:35:17 +02:00
Git int
}
var supportedGitVersions = map[string]int{
"<1.8.2": 1,
"<2.8.0": 2,
"latest": 3,
2016-06-06 18:12:57 +02:00
}
// Split splits a configuration
func Split(config *Config, result *Result) error {
state, err := newState(config, result)
if err != nil {
return err
}
defer state.close()
return state.split()
}
// Validate validates the configuration
func (config *Config) Validate() error {
2024-03-07 15:26:05 +01:00
ok, err := git.ReferenceNameIsValid(config.Origin)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("the origin is not a valid Git reference")
2016-06-06 18:12:57 +02:00
}
2024-03-07 15:26:05 +01:00
ok, err = git.ReferenceNameIsValid(config.Target)
if err != nil {
return err
}
if config.Target != "" && !ok {
return fmt.Errorf("the target is not a valid Git reference")
2016-06-06 18:12:57 +02:00
}
2016-07-31 16:35:17 +02:00
git, ok := supportedGitVersions[config.GitVersion]
if !ok {
2024-03-07 15:26:05 +01:00
return fmt.Errorf(`the git version can only be one of "<1.8.2", "<2.8.0", or "latest"`)
2016-07-31 16:35:17 +02:00
}
config.Git = git
2016-06-06 18:12:57 +02:00
return nil
}