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 := "" to := ""
excludes := make([]string, 0) excludes := make([]string, 0)
if len(parts) >= 2 { if len(parts) >= 2 {
to = parts[1] to = strings.TrimRight(parts[1], "/")
if len(parts) > 2 { if len(parts) > 2 {
for _, exclude := range parts[2:] { for _, exclude := range parts[2:] {
excludes = append(excludes, exclude) excludes = append(excludes, exclude)
@ -35,14 +35,13 @@ func (p *prefixesFlag) Set(value string) error {
} }
// value must be unique // value must be unique
for _, prefix := range []*splitter.Prefix(*p) { for _, prefix := range *p {
// FIXME: to should be normalized (xxx vs xxx/ for instance)
if prefix.To == to { 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) 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 return nil
} }
@ -79,7 +78,7 @@ func main() {
config := &splitter.Config{ config := &splitter.Config{
Path: path, Path: path,
Origin: origin, Origin: origin,
Prefixes: []*splitter.Prefix(prefixes), Prefixes: prefixes,
Target: target, Target: target,
Commit: commit, Commit: commit,
Debug: debug, Debug: debug,

View file

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