summaryrefslogtreecommitdiff
path: root/fi-prune-empty2/stream.go
blob: cadaf8b2a2a553c465a7e840cbdb434156b95086 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright 2019-2021  Luke Shumaker <lukeshu@parabola.nu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

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

type Tree []libfastimport.FileModify

func (t Tree) Len() int           { return len(t) }
func (t Tree) Less(i, j int) bool { return t[i].Path < t[j].Path }
func (t Tree) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }

func TreesEqual(a, b Tree) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

type Mark int

func AsMark(str string) (Mark, error) {
	if str == "" {
		return -1, nil
	}
	if !strings.HasPrefix(str, ":") {
		return -1, errors.Errorf("does not look like a mark: %q", str)
	}
	n, err := strconv.Atoi(str[1:])
	if err != nil {
		return -1, errors.Wrapf(err, "does not look like a mark: %q", str)
	}
	return Mark(n), nil
}

func MarkInSlice(needle Mark, haystack []Mark) bool {
	for _, straw := range haystack {
		if needle == straw {
			return true
		}
	}
	return false
}

type Commit struct {
	Mark        Mark
	OriginalOID Hash
	Parents     []Mark
	Tree        Tree

	ExcludedParents bool
}

type Driver interface {
	// ProcessCommit takes an input Commit, and
	//
	//  1. Records any information about that Commit that the
	//     Driver will need for future operations.
	//
	//  2. Decide whether the output stream should contain that
	//     Commit at all; return (nil, nil) if the output stream
	//     should omit this Commit.
	//
	//  3. Potentially mutate the list of commit.Parents, which it
	//     returns.
	ProcessCommit(Commit) (*Commit, error)

	// GotMark is called to inform the Driver what output-repo
	// Hash a given output-stream Mark refers to.  The Driver
	// returns true if this is a new Hash that has never been
	// passed to GotMark before; the return value is just used
	// progress statistics.
	GotMark(Mark, Hash) bool

	// FixMark takes an input-stream Mark, and returns the
	// equivalent output-stream Mark.  An error is signaled by
	// returning a negative Mark.
	FixMark(Mark) Mark

	// FixCommitIsh takes an input-stream commitish, and returns
	// the equivalent output-stream commitish.  An error is
	// signaled by returning an empty string.
	//
	// As a point of interest, the actual implementation (in
	// prune.go) requires that the input commitish be a Mark;
	// errors otherwise.
	FixCommitIsh(string) string

	// HandleDone gets called before the "done" command is
	// emitted, but after any other commands that would be emitted
	// by the Handler.  It is called with a map of every ref that
	// has been defined in the output-repo.
	HandleDone(refs map[string]Mark) error
}

type Handler struct {
	backend *libfastimport.Backend
	driver  Driver

	allowExcludedParents bool

	// current refs
	refs map[string]Mark

	// current commit
	commitMeta libfastimport.CmdCommit
	commitFile Tree

	// statistics
	commitsIn  int
	commitsOut int
	beg        time.Time
}

func NewHandler(backend *libfastimport.Backend, driver Driver, allowExcludedParents bool) *Handler {
	return &Handler{
		backend: backend,
		driver:  driver,

		allowExcludedParents: allowExcludedParents,

		// current refs
		refs: map[string]Mark{},

		// current commit
		commitMeta: libfastimport.CmdCommit{},
		commitFile: Tree{},

		// statistics
		beg: time.Now(),
	}
}

////////////////////////////////////////////////////////////////////////////////

// commit //////////////////////////////////////////////////////////////////////
func (h *Handler) CmdCommit(cmd libfastimport.CmdCommit) error {
	if cmd.Mark < 0 {
		return errors.Errorf("refusing to process commit to %q without mark", cmd.Ref)
	}
	if !strings.HasPrefix(cmd.Ref, "refs/") {
		return errors.Errorf("refusing to process commit :%d to invalid refname %q", cmd.Mark, cmd.Ref)
	}
	h.commitMeta = cmd
	h.commitFile = nil
	return nil
}

func (h *Handler) CmdCommitEnd(cmd libfastimport.CmdCommitEnd) error {
	h.commitsIn++
	defer func() {
		h.backend.Do(libfastimport.CmdProgress{
			Str: fmt.Sprintf("[%s] %d commits => %d commits (%.2f commit/s)",
				os.Args[0],
				h.commitsIn, h.commitsOut,
				float64(h.commitsIn)/time.Since(h.beg).Seconds()),
		})
	}()

	// Build the aggregated object for ProcessCommit.
	var cmt Commit
	var err error
	cmt.Mark = Mark(h.commitMeta.Mark)
	cmt.OriginalOID, err = AsHash(h.commitMeta.OriginalOID)
	if err != nil {
		fmt.Errorf("%w: commit: %q", err, "original-oid "+h.commitMeta.OriginalOID)
	}
	cmt.Parents, err = func() ([]Mark, error) {
		var parents []Mark
		if h.commitMeta.From == "" {
			if from, ok := h.refs[h.commitMeta.Ref]; ok {
				parents = append(parents, from)
			}
		} else {
			mark, err := AsMark(h.driver.FixCommitIsh(h.commitMeta.From))
			if err != nil {
				return nil, fmt.Errorf("%w: commit: %q", err, "from "+h.commitMeta.From)
			}
			if mark >= 0 {
				parents = append(parents, mark)
			}
		}
		for _, merge := range h.commitMeta.Merge {
			mark, err := AsMark(h.driver.FixCommitIsh(merge))
			if err != nil {
				return nil, fmt.Errorf("%w: commit: %q", err, "merge "+merge)
			}
			if mark >= 0 && !MarkInSlice(mark, parents) {
				parents = append(parents, mark)
			}
		}
		return parents, nil
	}()
	if err != nil {
		if h.allowExcludedParents {
			cmt.ExcludedParents = true
		} else {
			return err
		}
	}

	sort.Stable(h.commitFile)
	cmt.Tree = h.commitFile

	// Remember this.
	defer func() {
		h.refs[h.commitMeta.Ref] = Mark(h.commitMeta.Mark)
	}()

	// Call ProcessCommit.
	cmtptr, err := h.driver.ProcessCommit(cmt)
	if err != nil {
		return err
	}

	// Do something with the result.
	if cmtptr == nil {
		// Drop the commit.

		if _, refExists := h.refs[h.commitMeta.Ref]; !refExists {
			mark := h.driver.FixMark(Mark(h.commitMeta.Mark))
			commitIsh := fmt.Sprintf(":%d", mark)
			if mark < 0 {
				commitIsh = EmptyHash
			}
			err := h.backend.Do(libfastimport.CmdReset{
				RefName:   h.commitMeta.Ref,
				CommitIsh: commitIsh,
			})
			if err != nil {
				return err
			}
		}
		h.refs[h.commitMeta.Ref] = Mark(h.commitMeta.Mark)
	} else {
		// Emit the commit.

		// apply the mutations from ProcessCommit to
		// h.commitMeta
		if cmt.ExcludedParents {
			h.commitMeta.From = h.driver.FixCommitIsh(h.commitMeta.From)
			merge := make([]string, 0, len(h.commitMeta.Merge))
			for i := range h.commitMeta.Merge {
				fixed := h.driver.FixCommitIsh(h.commitMeta.Merge[i])
				if fixed != "" {
					merge = append(merge, fixed)
				}
			}
			h.commitMeta.Merge = merge
		} else {
			if len(cmtptr.Parents) == 0 {
				h.commitMeta.From = EmptyHash
				h.commitMeta.Merge = nil
			} else {
				h.commitMeta.From = fmt.Sprintf(":%d", cmtptr.Parents[0])
				h.commitMeta.Merge = nil
				for _, merge := range cmtptr.Parents[1:] {
					h.commitMeta.Merge = append(h.commitMeta.Merge, fmt.Sprintf(":%d", merge))
				}
			}
		}

		// actually emit the commit
		if err := h.backend.Do(h.commitMeta); err != nil {
			return errors.Wrapf(err, "processing commit :%d", h.commitMeta.Mark)
		}
		if err := h.backend.Do(libfastimport.FileDeleteAll{}); err != nil {
			return errors.Wrapf(err, "processing commit :%d", h.commitMeta.Mark)
		}
		for _, file := range h.commitFile {
			if err := h.backend.Do(file); err != nil {
				return errors.Wrapf(err, "processing commit :%d", h.commitMeta.Mark)
			}
		}

		// tell the driver about the emitted commit
		sha1, err := h.backend.GetMark(libfastimport.CmdGetMark{
			Mark: h.commitMeta.Mark,
		})
		if err != nil {
			return err
		}
		hash, err := AsHash(sha1)
		if err != nil {
			return err
		}
		if h.driver.GotMark(Mark(h.commitMeta.Mark), hash) {
			h.commitsOut++
		}
	}

	return nil
}

// file commands ///////////////////////////////////////////////////////////////
func (h *Handler) FileModify(cmd libfastimport.FileModify) error {
	h.commitFile = append(h.commitFile, cmd)
	return nil
}
func (h *Handler) FileModifyInline(cmd libfastimport.FileModifyInline) error {
	return errors.New("unexpected inline \"filemodify\" command; must use a mark")
}
func (h *Handler) FileCopy(cmd libfastimport.FileCopy) error {
	return errors.New("unexpected \"filecopy\" command: this filter requires --full-tree")
}
func (h *Handler) FileRename(cmd libfastimport.FileRename) error {
	return errors.New("unexpected \"filerename\" command: this filter requires --full-tree")
}
func (h *Handler) FileDelete(cmd libfastimport.FileDelete) error {
	return errors.New("unexpected \"filedelete\" command: this filter requires --full-tree")
}
func (h *Handler) FileDeleteAll(cmd libfastimport.FileDeleteAll) error {
	h.commitFile = nil
	return nil
}

// note commands ///////////////////////////////////////////////////////////////
func (h *Handler) NoteModify(cmd libfastimport.NoteModify) error {
	return errors.Errorf("unsupported (but known) command %T", cmd)
}
func (h *Handler) NoteModifyInline(cmd libfastimport.NoteModifyInline) error {
	return errors.Errorf("unsupported (but known) command %T", cmd)
}

// other commands //////////////////////////////////////////////////////////////
func (h *Handler) CmdBlob(cmd libfastimport.CmdBlob) error { return h.backend.Do(cmd) }
func (h *Handler) CmdAlias(cmd libfastimport.CmdAlias) error {
	return errors.Errorf("unsupported (but known) command %T", cmd)
}
func (h *Handler) CmdCheckpoint(cmd libfastimport.CmdCheckpoint) error { return h.backend.Do(cmd) }
func (h *Handler) CmdComment(cmd libfastimport.CmdComment) error       { return h.backend.Do(cmd) }
func (h *Handler) CmdDone(cmd libfastimport.CmdDone) error             { return h.backend.Do(cmd) }
func (h *Handler) CmdOption(cmd libfastimport.CmdOption) error         { return h.backend.Do(cmd) }
func (h *Handler) CmdProgress(cmd libfastimport.CmdProgress) error     { return h.backend.Do(cmd) }
func (h *Handler) CmdReset(cmd libfastimport.CmdReset) error {
	if cmd.CommitIsh == "" || cmd.CommitIsh == EmptyHash {
		delete(h.refs, cmd.RefName)
	} else {
		cmd.CommitIsh = h.driver.FixCommitIsh(cmd.CommitIsh)
		if cmd.CommitIsh == "" || cmd.CommitIsh == EmptyHash {
			delete(h.refs, cmd.RefName)
		} else {
			mark, err := AsMark(cmd.CommitIsh)
			if err != nil {
				return fmt.Errorf("%w: reset: %q", err, "merge "+cmd.CommitIsh)
			}
			h.refs[cmd.RefName] = mark
		}
	}
	return h.backend.Do(cmd)
}
func (h *Handler) CmdTag(cmd libfastimport.CmdTag) error {
	cmd.CommitIsh = h.driver.FixCommitIsh(cmd.CommitIsh)

	// emit the tag
	if err := h.backend.Do(cmd); err != nil {
		return err
	}

	// tell the driver about the emitted tag
	sha1, err := h.backend.GetMark(libfastimport.CmdGetMark{
		Mark: cmd.Mark,
	})
	if err != nil {
		return err
	}
	hash, err := AsHash(sha1)
	if err != nil {
		return err
	}
	h.driver.GotMark(Mark(cmd.Mark), hash)

	// remember the tag
	h.refs[cmd.RefName] = Mark(cmd.Mark)

	return nil
}

func (h *Handler) CmdCatBlob(cmd libfastimport.CmdCatBlob) (sha1 string, data string, err error) {
	cmd.DataRef = h.driver.FixCommitIsh(cmd.DataRef)
	return h.backend.CatBlob(cmd)
}
func (h *Handler) CmdGetMark(cmd libfastimport.CmdGetMark) (sha1 string, err error) {
	cmd.Mark = int(h.driver.FixMark(Mark(cmd.Mark)))
	if cmd.Mark < 0 {
		return "", errors.New("mark no longer exists")
	}
	sha1, err = h.backend.GetMark(cmd)
	if err == nil {
		hash, err := AsHash(sha1)
		if err != nil {
			return "", err
		}
		h.driver.GotMark(Mark(cmd.Mark), hash)
	}
	return sha1, err
}
func (h *Handler) CmdLs(cmd libfastimport.CmdLs) (mode libfastimport.Mode, dataref string, path libfastimport.Path, err error) {
	cmd.DataRef = h.driver.FixCommitIsh(cmd.DataRef)
	return h.backend.Ls(cmd)
}

func (h *Handler) CmdFeature(cmd libfastimport.CmdFeature) error {
	switch cmd.Feature {
	case "date-format":
		if cmd.Argument != "raw" {
			return errors.Errorf("date-format=%q: only supports the %q format", cmd.Argument, "raw")
		}
		return h.backend.Do(cmd)
	case "export-marks", "relative-marks", "no-relative-marks", "force", "import-marks", "import-marks-if-exists", "get-mark", "cat-blob", "ls":
		return h.backend.Do(cmd)
	case "notes":
		return errors.Errorf("unsupported (but known) command %T", cmd)
	case "done":
		return h.backend.Do(cmd)
	default:
		return errors.Errorf("unknown feature %q", cmd.Feature)
	}
}

func (h *Handler) HookEndOfStream() error {
	return h.driver.HandleDone(h.refs)
}