From bd864e2ca6a8156840f7cc129e0274d1eaf401c4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 8 Mar 2024 10:57:23 +0100 Subject: [PATCH] Sanitize prefix values --- main.go | 9 ++++----- splitter/config.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ea79764..a67b430 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/splitter/config.go b/splitter/config.go index 7a4a0ef..b5aefdb 100644 --- a/splitter/config.go +++ b/splitter/config.go @@ -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