summaryrefslogtreecommitdiff
path: root/fi-prune-empty/main.go
blob: 7c1ea5dd4293f1eb11bb72e634f563c70dd63dd3 (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
// Copyright 2019  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/>.

// Command fi-prune-empty prunes a fast-import stream, removing empty
// commits and empty merges.
package main

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

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

	"git.parabola.nu/~lukeshu/fastimport-go-utils/fiutil"
)

func usage() {
	fmt.Printf("Usage: git fast-export --full-tree | %s | git fast-import\n", os.Args[0])
	fmt.Printf("Prunes a fast-import stream, removing empty commits and empty merges\n")
	os.Exit(0)
}

func main() {
	if len(os.Args) == 2 && os.Args[1] == "--help" {
		usage()
	}
	if len(os.Args) != 1 {
		fiutil.ErrUsage(fmt.Sprintf("expected 0 arguments, got %d", len(os.Args)-1))
	}

	frontend := libfastimport.NewFrontend(os.Stdin, os.Stdin, nil)
	backend := libfastimport.NewBackend(os.Stdout, os.Stdout, nil)

	filter := NewPruneEmpty(backend)
	if err := fiutil.RunHandler(frontend, filter); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}
}

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 PruneEmpty struct {
	backend *libfastimport.Backend

	// history
	replace map[string]string

	// history
	ancestors map[string]map[string]struct{}
	trees     map[string]Tree
	refs      map[string]string

	// current commit
	commitMeta libfastimport.CmdCommit
	commitFile Tree

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

func NewPruneEmpty(backend *libfastimport.Backend) *PruneEmpty {
	return &PruneEmpty{
		backend: backend,

		// history
		replace: map[string]string{},

		// history
		ancestors: map[string]map[string]struct{}{},
		trees:     map[string]Tree{},
		refs:      map[string]string{},

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

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

func (h *PruneEmpty) fixupMark(m string) string {
	if r, ok := h.replace[m]; ok {
		return r
	}
	return m
}

func (h *PruneEmpty) addCommit(c string, parents []string, tree Tree) {
	cAncestors := map[string]struct{}{}
	for _, parent := range parents {
		parent = h.fixupMark(parent)
		cAncestors[parent] = struct{}{}
		for ancestor := range h.ancestors[parent] {
			cAncestors[ancestor] = struct{}{}
		}
	}
	h.ancestors[c] = cAncestors
	h.trees[c] = tree
}

// subsumedBy(c, commits) determines if commit 'c' is an
// ancestor of any of the commits given in 'commits'.
func (h *PruneEmpty) subsumedBy(c string, commits []string) bool {
	for _, c2 := range commits {
		c2 = h.fixupMark(c2)
		if c2 == c {
			continue
		}
		if _, ok := h.ancestors[c2][c]; ok {
			return true
		}
	}
	return false
}
func (h *PruneEmpty) pruneParents(commits []string) []string {
	var ret []string
	for _, c := range commits {
		c = h.fixupMark(c)
		if c != "" && !h.subsumedBy(c, commits) {
			ret = append(ret, c)
		}
	}
	return ret
}

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

// commit //////////////////////////////////////////////////////////////////////
func (h *PruneEmpty) CmdCommit(cmd libfastimport.CmdCommit) error {
	if cmd.Mark < 0 {
		return errors.Errorf("refusing to process commit to %q without mark", cmd.Ref)
	}
	h.commitMeta = cmd
	h.commitFile = Tree{}
	return nil
}
func (h *PruneEmpty) 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()),
		})
	}()

	mark := fmt.Sprintf(":%d", h.commitMeta.Mark)
	from := h.commitMeta.From
	if from == "" {
		from = h.refs[h.commitMeta.Ref]
	}
	parents := h.pruneParents(append([]string{from}, h.commitMeta.Merge...))
	sort.Stable(h.commitFile)

	// decide whether to skip this commit
	switch len(parents) {
	case 0:
		if len(h.commitFile) == 0 {
			h.replace[mark] = ""
			return nil
		}
		h.commitMeta.From = ""
		h.commitMeta.Merge = nil
	case 1:
		if TreesEqual(h.trees[parents[0]], h.commitFile) {
			h.replace[mark] = parents[0]
			h.refs[h.commitMeta.Ref] = parents[0]
			return nil
		}
		fallthrough
	default:
		h.commitMeta.From = parents[0]
		h.commitMeta.Merge = parents[1:]
	}
	h.commitsOut++

	// remember the commit
	h.addCommit(mark, parents, h.commitFile)
	if h.commitMeta.From == "" && h.refs[h.commitMeta.Ref] != "" {
		return errors.Errorf("refusing to reset ref %q from %q", h.commitMeta.Ref, h.refs[h.commitMeta.Ref])
	}
	h.refs[h.commitMeta.Ref] = mark

	// emit the commit
	if err := h.backend.Do(h.commitMeta); err != nil {
		return err
	}
	if err := h.backend.Do(libfastimport.FileDeleteAll{}); err != nil {
		return err
	}
	for _, file := range h.commitFile {
		if err := h.backend.Do(file); err != nil {
			return err
		}
	}

	return nil
}

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

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

// other commands //////////////////////////////////////////////////////////////
func (h *PruneEmpty) CmdBlob(cmd libfastimport.CmdBlob) error             { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdCheckpoint(cmd libfastimport.CmdCheckpoint) error { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdComment(cmd libfastimport.CmdComment) error       { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdDone(cmd libfastimport.CmdDone) error             { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdOption(cmd libfastimport.CmdOption) error         { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdProgress(cmd libfastimport.CmdProgress) error     { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdReset(cmd libfastimport.CmdReset) error           { return h.backend.Do(cmd) }
func (h *PruneEmpty) CmdTag(cmd libfastimport.CmdTag) error               { return h.backend.Do(cmd) }

func (h *PruneEmpty) CmdCatBlob(cmd libfastimport.CmdCatBlob) (sha1 string, data string, err error) {
	if strings.HasPrefix(cmd.DataRef, ":") {
		cmd.DataRef = h.fixupMark(cmd.DataRef)
	}
	return h.backend.CatBlob(cmd)
}
func (h *PruneEmpty) CmdGetMark(cmd libfastimport.CmdGetMark) (sha1 string, err error) {
	cmd.Mark, _ = strconv.Atoi(h.fixupMark(fmt.Sprintf(":%d", cmd.Mark))[1:])
	return h.backend.GetMark(cmd)
}
func (h *PruneEmpty) CmdLs(cmd libfastimport.CmdLs) (mode libfastimport.Mode, dataref string, path libfastimport.Path, err error) {
	if strings.HasPrefix(cmd.DataRef, ":") {
		cmd.DataRef = h.fixupMark(cmd.DataRef)
	}
	return h.backend.Ls(cmd)
}

func (h *PruneEmpty) 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)
	}
}