Skip to content

Commit

Permalink
Sanitize prefix values
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 8, 2024
1 parent 5dbf39b commit bd864e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions main.go
Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions splitter/config.go
Expand Up @@ -3,6 +3,7 @@ package splitter
import (
"fmt"
"log"
"strings"
"sync"

git "github.com/libgit2/git2go/v34"
Expand All @@ -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
Expand Down

0 comments on commit bd864e2

Please sign in to comment.