summaryrefslogtreecommitdiff
path: root/main.go
blob: f50704b6ce90531f47a4171acb8322752e60e44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)
	}
}