summaryrefslogtreecommitdiff
path: root/filter.go
blob: e263988a143ce96b41843507dd6808f52d6126f9 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package svn2git2aur

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"strings"

	"git.lukeshu.com/go/libfastimport"
)

type fullcommit struct {
	metadata libfastimport.CmdCommit
	fileactions []libfastimport.Cmd
}

type Filter struct {
	fromRef string
	toPfx   string

	frontend *libfastimport.Frontend
	backend  *libfastimport.Backend

	refs map[string]string
	mark int

	curCommitIn libfastimport.CmdCommit
	curCommitOut map[string]fullcommit
}

func NewFilter(fromRef string, toPfx string) (*Filter, error) {
	var err error
	ret := &Filter{
		fromRef: fromRef,
		toPfx:   toPfx,
	}

	ret.refs, err = gitRefs()
	if err != nil {
		return nil, err
	}

	ret.frontend, err = gitFastExport(
		"--use-done-feature",
		"--no-data",
		"--", fromRef)
	if err != nil {
		return nil, err
	}

	ret.backend, err = gitFastImport("--done")
	if err != nil {
		return nil, err
	}

	return ret, nil
}

func (f *Filter) newmark() int {
	f.mark++
	return f.mark
}

func (f *Filter) infofile(name string) string {
	return "info/svn2git2aur/" + name
}

func (f *Filter) pkgbuild2srcinfo(pkgbuildId string) (string, error) {
	cachefilename := f.infofile("pkgbuild2srcinfo/" + pkgbuildId)
	for {
		// Try to get a cached result
		b, err := ioutil.ReadFile(cachefilename)
		if err == nil && len(bytes.TrimSpace(b)) == 40 {
			return string(bytes.TrimSpace(b)), nil
		}

		// Read the PKGBUILD
		sha1, data, err := f.backend.CatBlob(libfastimport.CmdCatBlob{DataRef: pkgbuildId})
		if sha1 != pkgbuildId {
			return "", fmt.Errorf("PKGBUILD sha1 mismatch: %q != %q", pkgbuildId, sha1)
		}
		// Write the PKGBUILD to a temporary file to pass to makepkg
		file, err := os.OpenFile(f.infofile("tmp/PKGBUILD"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
		if err != nil {
			return "", err
		}
		_, err = io.WriteString(file, data)
		file.Close()
		if err != nil {
			return "", err
		}
		// Run makepkg on the PKGBUILD to get a .SRCINFO
		cmd := exec.Command("makepkg", "--printsrcinfo")
		cmd.Dir = f.infofile("tmp")
		srcinfoBody, err := cmd.Output()
		if err != nil {
			return "", err
		}
		// Write the .SRCINFO back in to git
		mark := f.newmark()
		err = f.backend.Do(libfastimport.CmdBlob{
			Mark: mark,
			Data: string(srcinfoBody),
		})
		if err != nil {
			return "", err
		}
		// Now get the new ID of the .SRCINFO from git
		srcinfoId, err := f.backend.GetMark(libfastimport.CmdGetMark{
			Mark: mark,
		})
		if err != nil {
			return "", err
		}
		// Write the srcinfoId in to the cache
		file, err = os.OpenFile(cachefilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
		_, err = io.WriteString(file, srcinfoId)
		file.Close()
		if err != nil {
			return "", err
		}
	}
}

func gitsvnid2commit(gitsvnid string, searchrange string) (string, error) {
	cmd := exec.Command("git", "log",
		"-n1",
		"--format=%H",
		"--grep=^git-svn-id: "+gitsvnid+"$",
		searchrange)
	out, err := cmd.Output()
	return strings.TrimSpace(string(out)), err
}

func (f *Filter) Run() error {
	for {
		cmd, err := f.frontend.ReadCmd()
		if err != nil {
			return err
		}
		switch cmdt := cmd.(type) {
		case libfastimport.CmdFeature, libfastimport.CmdOption, libfastimport.CmdCheckpoint, libfastimport.CmdProgress, libfastimport.CmdComment:
			err = f.backend.Do(cmd)
			if err != nil {
				return err
			}
		case libfastimport.CmdDone:
			err = f.backend.Do(cmd)
			if err != nil {
				return err
			}
			cmd, err := f.frontend.ReadCmd()
			if cmd != nil || err != io.EOF {
				return fmt.Errorf("git fast-export kept going after 'done': cmd=%v err=%v", cmd, err)
			}
			return nil
		case libfastimport.CmdReset:
			// TODO
		case libfastimport.CmdCommit:
			// TODO
		case libfastimport.CmdCommitEnd:
			// TODO
		case libfastimport.FileModify:
			branchname := filename2branchname(string(cmdt.Path))
			if branchname == "" {
				continue
			}
			branch_ref := f.toPfx + "/" + path.Dir(string(cmdt.Path))
			from_dataref, from_dataref_ok := f.refs[branch_ref]
			if from_dataref_ok {
				file_mode, file_dataref, _, err := f.backend.Ls(libfastimport.CmdLs{DataRef: from_dataref, Path: cmdt.Path})
				if err != nil {
					return err
				}
				if file_mode == cmdt.Mode && file_dataref == cmdt.DataRef {
					continue
				}
			} else {
				from_dataref = branch_ref + "^0"
			}

			if _, ok := f.curCommitOut[branch_ref]; !ok {
				f.curCommitOut[branch_ref] = fullcommit{metadata: libfastimport.CmdCommit{
					Ref:       branch_ref,
					Mark:      f.newmark(),
					Author:    f.curCommitIn.Author,
					Committer: f.curCommitIn.Committer,
					Msg:       f.curCommitIn.Msg,
					From:      from_dataref,
					Merge:     nil,
				}}
			}
			//outcommit := f.curCommitOut[branch_ref]
			// TODO
		case libfastimport.FileDelete:
			//ref := f.toPfx + "/" + path.Dir(string(cmdt.Path))
			// TODO
		}
	}
	return nil
}