package main import ( "fmt" "io" "os" "strings" "time" ) func fmtDuration(d time.Duration) string { h := d / time.Hour d -= h * time.Hour m := d / time.Minute d -= m * time.Minute s := d / time.Second return fmt.Sprintf("%02d:%02d:%02d", h, m, s) } func usage(w io.Writer) { fmt.Fprintf(w, "Usage: %s refs/FROM_REFNAME refs/TO_PREFIX\n", os.Args[0]) fmt.Fprintf(w, " or: %s -h|--help\n", os.Args[0]) fmt.Fprintf(w, "Read an Arch Linux svntogit branch, and turn it into AUR-style branches\n") } func main() { for _, arg := range os.Args[1:] { switch arg { case "-h", "--help": usage(os.Stdout) os.Exit(0) } } if len(os.Args) != 3 { usage(os.Stderr) os.Exit(2) } fromRef := os.Args[1] toPfx := os.Args[2] if !strings.HasPrefix(fromRef, "refs/") || !strings.HasPrefix(fromRef, "refs/") { fmt.Fprintln(os.Stderr, "ref names and prefixes must start with \"refs/\"") usage(os.Stderr) os.Exit(2) } commits := 0 beg := time.Now() maxline := 0 status := func() { duration := time.Since(beg) line := fmt.Sprintf("[%s] %d commits (%.2f commit/s)", fmtDuration(duration), commits, float64(commits)/duration.Seconds()) if len(line) > maxline { maxline = len(line) } fmt.Fprintf(os.Stderr, "\r%-[1]*[2]s", maxline, line) } filter, err := NewFilter(fromRef, toPfx) if err != nil { fmt.Fprintln(os.Stderr, "Could not initialize filter:", err) os.Exit(1) } filter.OnCommit = func() { commits++ status() } err = filter.Run() fmt.Fprintln(os.Stderr) if err != nil { fmt.Fprintln(os.Stderr, "Error:", err) os.Exit(1) } }