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

package main

import (
	"fmt"
	"io"
	"os"
	"sort"

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

func abort(err error) {
	fmt.Fprintln(os.Stderr, "Error:", 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
}

// expects --full-tree
func main() {
	frontend := libfastimport.NewFrontend(os.Stdin, nil, nil)
	backend := libfastimport.NewBackend(os.Stdout, nil, nil)

	replace := map[string]string{}
	fixupMark := func(m string) string {
		if r, ok := replace[m]; ok {
			return r
		}
		return m
	}

	ancestors := map[string]map[string]struct{}{}
	trees := map[string]Tree{}
	addCommit := func(c string, parents []string, tree Tree) {
		cAncestors := map[string]struct{}{}
		for _, parent := range parents {
			parent = fixupMark(parent)
			cAncestors[parent] = struct{}{}
			for ancestor := range ancestors[parent] {
				cAncestors[ancestor] = struct{}{}
			}
		}
		ancestors[c] = cAncestors
		trees[c] = tree
	}
	// subsumedBy(c, commits) determines if commit 'c' is an
	// ancestor of any of the commits given in 'commits'.
	subsumedBy := func(c string, commits []string) bool {
		for _, c2 := range commits {
			c2 = fixupMark(c2)
			if c2 == c {
				continue
			}
			if _, ok := ancestors[c2][c]; ok {
				return true
			}
		}
		return false
	}
	pruneParents := func(commits []string) []string {
		var ret []string
		for _, c := range commits {
			c = fixupMark(c)
			if !subsumedBy(c, commits) {
				ret = append(ret, c)
			}
		}
		return ret
	}

	commitMeta := libfastimport.CmdCommit{}
	commitFile := Tree{}
	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 = Tree{}
		case libfastimport.CmdCommitEnd:
			mark := fmt.Sprintf(":%d", commitMeta.Mark)
			parents := pruneParents(append([]string{commitMeta.From}, commitMeta.Merge...))
			sort.Stable(commitFile)

			// decide whether to skip this commit
			if len(parents) == 1 && TreesEqual(trees[parents[0]], commitFile) {
				replace[mark] = parents[0]
				continue
			}

			// remember the commit
			addCommit(mark, parents, commitFile)

			// emit the commit
			commitMeta.From = parents[0]
			commitMeta.Merge = parents[1:]
			if err := backend.Do(commitMeta); err != nil {
				abort(err)
			}
			if err := backend.Do(libfastimport.FileDeleteAll{}); err != nil {
				abort(err)
			}
			for _, file := range commitFile {
				if err := backend.Do(file); err != nil {
					abort(err)
				}
			}
		case libfastimport.FileDeleteAll:
			// do nothing
		case libfastimport.FileModify:
			commitFile = append(commitFile, cmdt)

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