summaryrefslogtreecommitdiff
path: root/filter.go
blob: 68552710234b0cbdffdcb47398dd0062139d6793 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package svn2git2aur

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

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

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:
			// This should only happen once, at the very
			// beginning.  I think I can ignore it.
		case libfastimport.CmdCommit:
			f.curCommitIn = cmdt
			f.curCommitOut = map[string]fullcommit{}
		case libfastimport.CmdCommitEnd:
			for _, fc := range f.curCommitOut {
				// TODO: I think I might need to issue
				// a 'reset' command if
				// f.refs[fc.metadata.Ref] isn't set.
				err = f.backend.Do(fc.metadata)
				if err != nil {
					return err
				}
				f.refs[fc.metadata.Ref] = ":" + strconv.Itoa(fc.metadata.Mark)
				for _, fileaction := range fc.fileactions {
					err = f.backend.Do(fileaction)
					if err != nil {
						return err
					}
				}
			}
			// TODO: synthesize ABS-tree submodule commits
			f.curCommitIn = libfastimport.CmdCommit{}
			f.curCommitOut = nil
		case libfastimport.FileModify:
			branchname := filename2branchname(string(cmdt.Path))
			if branchname == "" {
				continue
			}

			branch_ref := f.toPfx + "/" + branchname
			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 {
					// the file hasn't changed; the action is a no-op; ignore it
					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,
				}}
			}

			filename := strings.TrimPrefix(string(cmdt.Path), branchname+"/")
			outcommit := f.curCommitOut[branch_ref]
			outcommit.fileactions = append(outcommit.fileactions,
				libfastimport.FileModify{
					Mode:    cmdt.Mode,
					Path:    textproto.Path(filename),
					DataRef: cmdt.DataRef,
				})
			if filename == "PKGBUILD" {
				srcinfo, err := f.pkgbuild2srcinfo(cmdt.DataRef)
				if err != nil {
					return err
				}
				outcommit.fileactions = append(outcommit.fileactions,
					libfastimport.FileModify{
						Mode:    cmdt.Mode,
						Path:    textproto.Path(".SRCINFO"),
						DataRef: srcinfo,
					})
			}
			f.curCommitOut[branch_ref] = outcommit
		case libfastimport.FileDelete:
			branchname := filename2branchname(string(cmdt.Path))
			if branchname == "" {
				continue
			}

			branch_ref := f.toPfx + "/" + branchname
			from_dataref, from_dataref_ok := f.refs[branch_ref]
			if !from_dataref_ok {
				return fmt.Errorf("cannot delete file %q from branch %q, because that branch doesn't exist!", cmdt.Path, branchname)
			}
			file_mode, _, _, err := f.backend.Ls(libfastimport.CmdLs{DataRef: from_dataref, Path: cmdt.Path})
			if err != nil {
				return err
			}
			if file_mode == 0 {
				// the file doesn't exist; the action is a no-op; ignore it
				continue
			}

			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,
				}}
			}

			filename := strings.TrimPrefix(string(cmdt.Path), branchname+"/")
			outcommit := f.curCommitOut[branch_ref]
			outcommit.fileactions = append(outcommit.fileactions,
				libfastimport.FileDelete{
					Path: textproto.Path(filename),
				})
			if filename == "PKGBUILD" {
				outcommit.fileactions = append(outcommit.fileactions,
					libfastimport.FileDelete{
						Path: textproto.Path(".SRCINFO"),
					})
			}
			f.curCommitOut[branch_ref] = outcommit
		}
	}
	return nil
}