summaryrefslogtreecommitdiff
path: root/fi-pacmanlog/main.go
blob: d8d9a52dd97ec11a1fe74c6e800e4bbd86ecb24f (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"strings"

	"git.lukeshu.com/go/libfastimport"
	"git.parabola.nu/fiutil"
	"github.com/pkg/errors"
)

func abort(err error) {
	fmt.Fprintln(os.Stderr, "Error:", err)
	os.Exit(1)
}

func main() {
	inref := os.Args[1]
	outref := os.Args[2]

	frontend, err := fiutil.GitFastExport("--no-data", "--full-tree", "--use-done-feature", inref)
	if err != nil {
		abort(err)
	}
	backend, err := fiutil.GitFastImport("--done")

	tags := map[string]bool{}

	commitMeta := libfastimport.CmdCommit{}
	commitFile := []libfastimport.Cmd{}
	for {
		cmd, err := frontend.ReadCmd()
		if err != nil {
			if err == io.EOF {
				break
			}
			abort(err)
		}
		switch cmdt := cmd.(type) {

		case libfastimport.CmdCommit:
			commitMeta = cmdt
			commitFile = []libfastimport.Cmd{}
		case libfastimport.CmdCommitEnd:
			ver := ""
			for _, file := range commitFile {
				if m, ok := file.(libfastimport.FileModify); ok && m.Path == "PKGBUILD" {
					_, pkgbuild, err := backend.CatBlob(libfastimport.CmdCatBlob{DataRef: m.DataRef})
					if err != nil {
						abort(err)
					}
					script := string(pkgbuild) + "\n" + `. "$(librelib messages)" && get_full_version` + "\n"
					cmd := exec.Command("bash")
					cmd.Stdin = strings.NewReader(script)
					out, err := cmd.Output()
					if err != nil {
						abort(err)
					}
					ver = strings.TrimSpace(string(out))
				}
			}
			if ver == "" || tags[ver] {
				break
			}
			tags[ver] = true
			commitMeta.Msg = "core-x86_64 pacman " + ver + " : " + commitMeta.Msg
			commitMeta.From = ""
			commitMeta.Ref = outref
			err = backend.Do(commitMeta)
			if err != nil {
				abort(err)
			}
			for _, file := range commitFile {
				err = backend.Do(file)
				if err != nil {
					abort(err)
				}
			}
		case libfastimport.FileModify:
			if !strings.HasPrefix(string(cmdt.Path), "repos/core-x86_64/") {
				break
			}
			cmdt.Path = libfastimport.Path(strings.TrimPrefix(string(cmdt.Path), "repos/core-x86_64/"))
			commitFile = append(commitFile, cmdt)
		case libfastimport.FileDeleteAll:
			commitFile = append(commitFile, cmd)

		case libfastimport.CmdReset:
			cmdt.RefName = outref
			err := backend.Do(cmdt)
			if err != nil {
				abort(err)
			}
		case libfastimport.CmdFeature:
			err := backend.Do(cmdt)
			if err != nil {
				abort(err)
			}
		case libfastimport.CmdOption:
			err := backend.Do(cmdt)
			if err != nil {
				abort(err)
			}
		case libfastimport.CmdDone:
			err := backend.Do(cmdt)
			if err != nil {
				abort(err)
			}
		case libfastimport.CmdComment:
		default:
			abort(errors.Errorf("Unexpected command: %[1]T(%#[1]v)", cmd))
		}
	}
}