Sanitize prefix values

This commit is contained in:
Fabien Potencier 2024-03-08 10:57:23 +01:00
parent 5dbf39b5b4
commit bd864e2ca6
No known key found for this signature in database
2 changed files with 23 additions and 5 deletions

View file

@ -26,7 +26,7 @@ func (p *prefixesFlag) Set(value string) error {
to := ""
excludes := make([]string, 0)
if len(parts) >= 2 {
to = parts[1]
to = strings.TrimRight(parts[1], "/")
if len(parts) > 2 {
for _, exclude := range parts[2:] {
excludes = append(excludes, exclude)
@ -35,14 +35,13 @@ func (p *prefixesFlag) Set(value string) error {
}
// value must be unique
for _, prefix := range []*splitter.Prefix(*p) {
// FIXME: to should be normalized (xxx vs xxx/ for instance)
for _, prefix := range *p {
if prefix.To == to {
return fmt.Errorf("cannot have two prefix splits under the same directory: %s -> %s vs %s -> %s", prefix.From, prefix.To, from, to)
}
}
*p = append(*p, &splitter.Prefix{From: from, To: to, Excludes: excludes})
*p = append(*p, splitter.NewPrefix(from, to, excludes))
return nil
}
@ -79,7 +78,7 @@ func main() {
config := &splitter.Config{
Path: path,
Origin: origin,
Prefixes: []*splitter.Prefix(prefixes),
Prefixes: prefixes,
Target: target,
Commit: commit,
Debug: debug,

View file

@ -3,6 +3,7 @@ package splitter
import (
"fmt"
"log"
"strings"
"sync"
git "github.com/libgit2/git2go/v34"
@ -16,6 +17,24 @@ type Prefix struct {
Excludes []string
}
// 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,
}
}
// Config represents a split configuration
type Config struct {
Prefixes []*Prefix