splitsh-lite/main.go

116 lines
2.9 KiB
Go
Raw Permalink Normal View History

2016-06-06 18:12:57 +02:00
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/splitsh/lite/splitter"
)
2017-02-23 07:38:57 +01:00
var (
version = "dev"
)
2016-06-06 18:12:57 +02:00
type prefixesFlag []*splitter.Prefix
func (p *prefixesFlag) String() string {
return fmt.Sprint(*p)
}
func (p *prefixesFlag) Set(value string) error {
parts := strings.Split(value, ":")
from := parts[0]
to := ""
2024-03-08 08:28:46 +01:00
excludes := make([]string, 0)
if len(parts) >= 2 {
2024-03-08 10:57:23 +01:00
to = strings.TrimRight(parts[1], "/")
2024-03-08 08:28:46 +01:00
if len(parts) > 2 {
for _, exclude := range parts[2:] {
excludes = append(excludes, exclude)
}
}
2016-06-06 18:12:57 +02:00
}
// value must be unique
2024-03-08 10:57:23 +01:00
for _, prefix := range *p {
2016-06-06 18:12:57 +02:00
if prefix.To == to {
2024-03-07 15:26:05 +01:00
return fmt.Errorf("cannot have two prefix splits under the same directory: %s -> %s vs %s -> %s", prefix.From, prefix.To, from, to)
2016-06-06 18:12:57 +02:00
}
}
2024-03-08 10:57:23 +01:00
*p = append(*p, splitter.NewPrefix(from, to, excludes))
2016-06-06 18:12:57 +02:00
return nil
}
var prefixes prefixesFlag
2016-07-31 16:35:17 +02:00
var origin, target, commit, path, gitVersion string
var scratch, debug, progress, v bool
2016-06-06 18:12:57 +02:00
func init() {
flag.Var(&prefixes, "prefix", "The directory(ies) to split")
flag.StringVar(&origin, "origin", "HEAD", "The branch to split (optional, defaults to the current one)")
flag.StringVar(&target, "target", "", "The branch to create when split is finished (optional)")
flag.StringVar(&commit, "commit", "", "The commit at which to start the split (optional)")
flag.StringVar(&path, "path", ".", "The repository path (optional, current directory by default)")
flag.BoolVar(&scratch, "scratch", false, "Flush the cache (optional)")
flag.BoolVar(&debug, "debug", false, "Enable the debug mode (optional)")
2016-07-31 16:35:17 +02:00
flag.StringVar(&gitVersion, "git", "latest", "Simulate a given version of Git (optional)")
2016-06-06 18:12:57 +02:00
flag.BoolVar(&progress, "progress", false, "Show progress bar (optional, cannot be enabled when debug is enabled)")
2017-02-23 07:38:57 +01:00
flag.BoolVar(&v, "version", false, "Show version")
2016-06-06 18:12:57 +02:00
}
func main() {
flag.Parse()
2017-02-23 07:38:57 +01:00
if v {
fmt.Fprintf(os.Stderr, "splitsh-lite version %s\n", version)
2017-02-23 07:38:57 +01:00
os.Exit(0)
}
2016-06-06 18:12:57 +02:00
if len(prefixes) == 0 {
fmt.Fprintln(os.Stderr, "You must provide the directory to split via the --prefix flag")
2016-06-06 18:12:57 +02:00
os.Exit(1)
}
config := &splitter.Config{
2016-07-31 16:35:17 +02:00
Path: path,
Origin: origin,
2024-03-08 10:57:23 +01:00
Prefixes: prefixes,
2016-07-31 16:35:17 +02:00
Target: target,
Commit: commit,
Debug: debug,
2016-07-31 16:35:17 +02:00
Scratch: scratch,
GitVersion: gitVersion,
2016-06-06 18:12:57 +02:00
}
result := &splitter.Result{}
var ticker *time.Ticker
if progress && !debug {
2016-06-06 18:12:57 +02:00
ticker = time.NewTicker(time.Millisecond * 50)
go func() {
for range ticker.C {
fmt.Fprintf(os.Stderr, "%d commits created, %d commits traversed\r", result.Created(), result.Traversed())
}
}()
}
if err := splitter.Split(config, result); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
2016-06-06 18:12:57 +02:00
os.Exit(1)
}
if ticker != nil {
ticker.Stop()
}
fmt.Fprintf(os.Stderr, "%d commits created, %d commits traversed, in %s\n", result.Created(), result.Traversed(), result.Duration(time.Millisecond))
2016-06-06 18:12:57 +02:00
if result.Head() != nil {
fmt.Println(result.Head().String())
}
}