aboutsummaryrefslogtreecommitdiffstats
path: root/tree_diff.go
blob: 1c613bdd400745675776735c60a0e8daaa5bee49 (plain) (blame)
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
package git

import (
	"bytes"
	"fmt"
	"io"
	"sort"
	"strings"

	"gopkg.in/src-d/go-git.v4/core"
)

type Action int

func (a Action) String() string {
	switch a {
	case Insert:
		return "Insert"
	case Delete:
		return "Delete"
	case Modify:
		return "Modify"
	default:
		panic(fmt.Sprintf("unsupported action: %d", a))
	}
}

const (
	Insert Action = iota
	Delete
	Modify
)

type Change struct {
	Action
	From ChangeEntry
	To   ChangeEntry
}

type ChangeEntry struct {
	Name      string
	Tree      *Tree
	TreeEntry TreeEntry
}

func (c *Change) Files() (from *File, to *File, err error) {
	if c.Action == Insert || c.Action == Modify {
		to, err = newFileFromTreeEntry(c.To.Tree, &c.To.TreeEntry)
		if err != nil {
			return
		}

	}

	if c.Action == Delete || c.Action == Modify {
		from, err = newFileFromTreeEntry(c.From.Tree, &c.From.TreeEntry)
		if err != nil {
			return
		}
	}

	return
}

func newFileFromTreeEntry(t *Tree, e *TreeEntry) (*File, error) {
	blob, err := t.r.Blob(e.Hash)
	if err != nil {
		return nil, err
	}

	return NewFile(e.Name, e.Mode, blob), nil
}

func (c *Change) String() string {
	return fmt.Sprintf("<Action: %s, Path: %s>", c.Action, c.name())
}

func (c *Change) name() string {
	if c.From.Name != "" {
		return c.From.Name
	}

	return c.To.Name
}

type Changes []*Change

func newEmpty() Changes {
	return make([]*Change, 0, 0)
}

func DiffTree(a, b *Tree) ([]*Change, error) {
	if a == b {
		return newEmpty(), nil
	}

	if a == nil || b == nil {
		return newWithEmpty(a, b)
	}

	return newDiffTree(a, b)
}

func (c Changes) Len() int {
	return len(c)
}

func (c Changes) Swap(i, j int) {
	c[i], c[j] = c[j], c[i]
}

func (c Changes) Less(i, j int) bool {
	return strings.Compare(c[i].name(), c[j].name()) < 0
}

func (c Changes) String() string {
	var buffer bytes.Buffer
	buffer.WriteString("[")
	comma := ""
	for _, v := range c {
		buffer.WriteString(comma)
		buffer.WriteString(v.String())
		comma = ", "
	}
	buffer.WriteString("]")

	return buffer.String()
}

func newWithEmpty(a, b *Tree) (Changes, error) {
	changes := newEmpty()

	var action Action
	var tree *Tree
	if a == nil {
		action = Insert
		tree = b
	} else {
		action = Delete
		tree = a
	}

	w := NewTreeWalker(tree.r, tree)
	defer w.Close()

	for {
		path, entry, err := w.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, fmt.Errorf("cannot get next file: %s", err)
		}

		if entry.Mode.IsDir() {
			continue
		}

		c := &Change{Action: action}

		if action == Insert {
			c.To.Name = path
			c.To.TreeEntry = entry
			c.To.Tree = tree
		} else {
			c.From.Name = path
			c.From.TreeEntry = entry
			c.From.Tree = tree
		}

		changes = append(changes, c)
	}

	return changes, nil
}

// FIXME: this is very inefficient, but correct.
// The proper way to do this is to implement a diff-tree algorithm,
// while taking advantage of the tree hashes to avoid traversing
// subtrees when the hash is equal in both inputs.
func newDiffTree(a, b *Tree) ([]*Change, error) {
	result := make([]*Change, 0)

	aChanges, err := newWithEmpty(a, nil)
	if err != nil {
		return nil, fmt.Errorf("cannot create nil-diff of source tree: %s", err)
	}
	sort.Sort(aChanges)

	bChanges, err := newWithEmpty(nil, b)
	if err != nil {
		return nil, fmt.Errorf("cannot create nil-diff of destination tree: %s", err)
	}
	sort.Sort(bChanges)

	for len(aChanges) > 0 && len(bChanges) > 0 {
		switch comp := strings.Compare(aChanges[0].name(), bChanges[0].name()); {
		case comp == 0: // append as "Modify" or ignore if not changed
			modified, err := hasChange(a, b, aChanges[0].name())
			if err != nil {
				return nil, err
			}

			if modified {
				c := mergeInsertAndDeleteIntoModify(aChanges[0], bChanges[0])
				result = append(result, c)
			}

			aChanges = aChanges[1:]
			bChanges = bChanges[1:]
		case comp < 0: // delete first a change
			result = append(result, aChanges[0])
			aChanges = aChanges[1:]
		case comp > 0: // insert first b change
			result = append(result, bChanges[0])
			bChanges = bChanges[1:]
		}
	}

	// append all remaining changes in aChanges, if any, as deletes
	// append all remaining changes in bChanges, if any, as inserts
	result = append(result, aChanges...)
	result = append(result, bChanges...)

	return result, nil
}

func mergeInsertAndDeleteIntoModify(a, b *Change) *Change {
	c := &Change{Action: Modify}
	c.From.Name = a.From.Name
	c.From.Tree = a.From.Tree
	c.From.TreeEntry = a.From.TreeEntry
	c.To.Name = b.To.Name
	c.To.Tree = b.To.Tree
	c.To.TreeEntry = b.To.TreeEntry

	return c
}

func hasChange(a, b *Tree, path string) (bool, error) {
	ha, err := hash(a, path)
	if err != nil {
		return false, err
	}

	hb, err := hash(b, path)
	if err != nil {
		return false, err
	}

	return ha != hb, nil
}

func hash(tree *Tree, path string) (core.Hash, error) {
	file, err := tree.File(path)
	if err != nil {
		var empty core.Hash
		return empty, fmt.Errorf("cannot find file %s in tree: %s", path, err)
	}

	return file.Hash, nil
}