File: log_test.go

package info (click to toggle)
gittuf 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,692 kB
  • sloc: python: 85; makefile: 58; sh: 1
file content (359 lines) | stat: -rw-r--r-- 12,127 bytes parent folder | download | duplicates (2)
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
// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0

package gitinterface

import (
	"fmt"
	"sort"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestGetCommitsBetweenRangeRepository(t *testing.T) {
	tempDir := t.TempDir()
	repo := CreateTestGitRepository(t, tempDir, false)

	refName := "refs/heads/main"
	treeBuilder := NewTreeBuilder(repo)

	// Write empty tree
	emptyTreeID, err := treeBuilder.WriteTreeFromEntries(nil)
	if err != nil {
		t.Fatal(err)
	}

	allCommits := []Hash{}
	for i := 0; i < 5; i++ {
		commitHash, err := repo.Commit(emptyTreeID, refName, "Test commit\n", false)
		require.Nil(t, err)
		allCommits = append(allCommits, commitHash)
	}

	// Git tree structure with their commit trees and their values:
	//
	// Commit1 <- Commit2 <- Commit3 <- Commit4 <- Commit5

	t.Run("Check range between commits 1 and 5", func(t *testing.T) {
		commits, err := repo.GetCommitsBetweenRange(allCommits[4], allCommits[0])
		assert.Nil(t, err)

		expectedCommits := []Hash{allCommits[4], allCommits[3], allCommits[2], allCommits[1]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Pass in wrong order", func(t *testing.T) {
		commits, err := repo.GetCommitsBetweenRange(allCommits[0], allCommits[4])
		assert.Nil(t, err)
		assert.Empty(t, commits)
	})

	t.Run("Check range in separate branches", func(t *testing.T) {
		//     7
		//    ↙ ↘
		//   5   6
		//   ↓   ↓
		//   3   4
		//   ↓   ↓
		//   1   2
		//    ↘ ↙
		//     0

		// If we pass in 7 and 1, we expect to get 7, 6, 5, 4, 3, and 2
		// If we pass in 1 and 7, we should expect nothing since every node that
		// is in the subtree of 1 is also in the subtree of 7

		// Create two new branches for this
		mainBranch := testNameToRefName(t.Name())
		featureBranch := testNameToRefName(t.Name() + " feature branch")

		// Add a common commit for both
		commonCommit, err := repo.Commit(emptyTreeID, mainBranch, "Initial commit\n", false)
		require.Nil(t, err)
		if err := repo.SetReference(featureBranch, commonCommit); err != nil {
			t.Fatal(err)
		}

		mainBranchCommits := []Hash{}
		for i := 0; i < 5; i++ {
			commitHash, err := repo.Commit(emptyTreeID, mainBranch, fmt.Sprintf("Main commit %d\n", i), false)
			require.Nil(t, err)
			mainBranchCommits = append(mainBranchCommits, commitHash)
		}

		featureBranchCommits := []Hash{}
		for i := 0; i < 5; i++ {
			commitHash, err := repo.Commit(emptyTreeID, featureBranch, fmt.Sprintf("Feature commit %d\n", i), false)
			require.Nil(t, err)
			featureBranchCommits = append(featureBranchCommits, commitHash)
		}

		// Add a common merge commit
		mergeCommit := repo.commitWithParents(
			t,
			emptyTreeID,
			[]Hash{
				mainBranchCommits[len(mainBranchCommits)-1],
				featureBranchCommits[len(featureBranchCommits)-1],
			},
			"Merge branches\n",
			false,
		)

		// Check merge to first commit in main branch (not initial common commit)
		expectedCommits := append([]Hash{mergeCommit}, mainBranchCommits[1:]...)
		expectedCommits = append(expectedCommits, featureBranchCommits...)
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})
		commits, err := repo.GetCommitsBetweenRange(mergeCommit, mainBranchCommits[0])
		assert.Nil(t, err)
		assert.Equal(t, expectedCommits, commits)

		// Check merge to first commit in feature branch (not initial common commit)
		expectedCommits = append([]Hash{mergeCommit}, featureBranchCommits[1:]...)
		expectedCommits = append(expectedCommits, mainBranchCommits...)
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})
		commits, err = repo.GetCommitsBetweenRange(mergeCommit, featureBranchCommits[0])
		assert.Nil(t, err)
		assert.Equal(t, expectedCommits, commits)

		// Check merge to initial common commit
		expectedCommits = append([]Hash{mergeCommit}, mainBranchCommits...)
		expectedCommits = append(expectedCommits, featureBranchCommits...)
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})
		commits, err = repo.GetCommitsBetweenRange(mergeCommit, commonCommit)
		assert.Nil(t, err)
		assert.Equal(t, expectedCommits, commits)

		// Set both branches to merge commit, diverge again
		if err := repo.SetReference(mainBranch, mergeCommit); err != nil {
			t.Fatal(err)
		}
		if err := repo.SetReference(featureBranch, mergeCommit); err != nil {
			t.Fatal(err)
		}

		mainBranchCommits = []Hash{}
		for i := 0; i < 5; i++ {
			commitHash, err := repo.Commit(emptyTreeID, mainBranch, fmt.Sprintf("Main commit %d\n", i), false)
			require.Nil(t, err)
			mainBranchCommits = append(mainBranchCommits, commitHash)
		}

		featureBranchCommits = []Hash{}
		for i := 0; i < 5; i++ {
			commitHash, err := repo.Commit(emptyTreeID, featureBranch, fmt.Sprintf("Feature commit %d\n", i), false)
			require.Nil(t, err)
			featureBranchCommits = append(featureBranchCommits, commitHash)
		}

		newMergeCommit := repo.commitWithParents(
			t,
			emptyTreeID,
			[]Hash{
				mainBranchCommits[len(mainBranchCommits)-1],
				featureBranchCommits[len(featureBranchCommits)-1],
			},
			"Merge branches\n",
			false,
		)

		// Check range between two merge commits
		expectedCommits = append([]Hash{newMergeCommit}, mainBranchCommits...)
		expectedCommits = append(expectedCommits, featureBranchCommits...)
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})
		commits, err = repo.GetCommitsBetweenRange(newMergeCommit, mergeCommit)
		assert.Nil(t, err)
		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Get all commits", func(t *testing.T) {
		commits, err := repo.GetCommitsBetweenRange(allCommits[4], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := allCommits
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})
		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Get commits from invalid range", func(t *testing.T) {
		_, err := repo.GetCommitsBetweenRange(ZeroHash, ZeroHash)
		assert.NotNil(t, err)
	})

	t.Run("Get commits from non-existent commit", func(t *testing.T) {
		nonExistentHash, err := repo.WriteBlob([]byte{})
		assert.Nil(t, err)

		commits, err := repo.GetCommitsBetweenRange(nonExistentHash, ZeroHash)
		assert.Nil(t, err)
		assert.Empty(t, commits)
	})
}

func TestGetCommitsBetweenRangeForMergeCommits(t *testing.T) {
	// Creating a tree with merge commits
	tmpDir := t.TempDir()
	repo := CreateTestGitRepository(t, tmpDir, false)

	commitIDs := make([]Hash, 0, 6)

	emptyBlobHash, err := repo.WriteBlob(nil)
	if err != nil {
		t.Fatal(err)
	}

	treeHashes := createTestTrees(t, repo, emptyBlobHash, 6)
	if err != nil {
		t.Fatal(err)
	}

	// creating the first commit
	commitID := repo.commitWithParents(t, treeHashes[0], nil, fmt.Sprintf("Test commit %v", 1), false)
	commitIDs = append(commitIDs, commitID)

	// creating two children from the first commit
	// in the visual, these will be commit 2 and commit 3
	children := createChildrenCommits(t, repo, treeHashes, commitID, 2)
	commitIDs = append(commitIDs, children...)

	// creating a child for commit 2, which in the visual will be commit 4
	commitID = repo.commitWithParents(t, treeHashes[3], []Hash{children[0]}, fmt.Sprintf("Test commit %v", 4), false)
	commitIDs = append(commitIDs, commitID)

	// creating a merge commit from the two children, which in the visual will be commit 5
	commitID = repo.commitWithParents(t, treeHashes[4], children, fmt.Sprintf("Test commit %v", 5), false)
	commitIDs = append(commitIDs, commitID)

	// creating a child for commit 3, which in the visual will be commit 6
	commitID = repo.commitWithParents(t, treeHashes[5], []Hash{children[1]}, fmt.Sprintf("Test commit %v", 6), false)
	commitIDs = append(commitIDs, commitID)

	// Git tree with merge commit structure without its commit trees and its values:
	//
	//  commit 4       commit 5         commit 6
	//    │              │  │              │
	//    └─► commit 2 ◄─┘  └─► commit 3 ◄─┘
	//            │              │
	//            └─► commit 1 ◄─┘

	t.Run("Test commit 1", func(t *testing.T) {
		// commit 1 is the first commit, so it should be the only commit returned
		commits, err := repo.GetCommitsBetweenRange(commitIDs[0], ZeroHash)
		assert.Nil(t, err)
		expectedCommits := []Hash{commitIDs[0]}
		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Test commit 2", func(t *testing.T) {
		// commit 2 is the first child of commit 1, so only it and commit 1 should be returned
		commits, err := repo.GetCommitsBetweenRange(commitIDs[1], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := []Hash{commitIDs[1], commitIDs[0]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Test commit 3", func(t *testing.T) {
		// commit 3 is the second child of commit 1, so only it and commit 1 should be returned
		commits, err := repo.GetCommitsBetweenRange(commitIDs[2], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := []Hash{commitIDs[0], commitIDs[2]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Test commit 4", func(t *testing.T) {
		// commit 4 is the child of commit 2, so only it, commit 2, and commit 2's parent commit 1 should be returned
		commits, err := repo.GetCommitsBetweenRange(commitIDs[3], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := []Hash{commitIDs[1], commitIDs[0], commitIDs[3]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Test commit 5, the merge commit", func(t *testing.T) {
		// commit 5 is the merge commit of commit 2 and commit 3, so it should return commit 5, commit 2, commit 3, and commit 1 (the parent of commit 2 and commit 3)
		commits, err := repo.GetCommitsBetweenRange(commitIDs[4], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := []Hash{commitIDs[4], commitIDs[1], commitIDs[0], commitIDs[2]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})

	t.Run("Test commit 6", func(t *testing.T) {
		// commit 6 is the child of commit 3, so it should return commit 6, commit 3, and commit 1 (the parent of commit 3)
		commits, err := repo.GetCommitsBetweenRange(commitIDs[5], ZeroHash)
		assert.Nil(t, err)

		expectedCommits := []Hash{commitIDs[0], commitIDs[5], commitIDs[2]}
		sort.Slice(expectedCommits, func(i, j int) bool {
			return expectedCommits[i].String() < expectedCommits[j].String()
		})

		assert.Equal(t, expectedCommits, commits)
	})
}

func createTestTrees(t *testing.T, repo *Repository, emptyBlobHash Hash, num int) []Hash {
	t.Helper()
	treeBuilder := NewTreeBuilder(repo)
	treeHashes := make([]Hash, 0, num)
	for i := 1; i <= num; i++ {
		objects := []TreeEntry{}
		for j := 0; j < i; j++ {
			objects = append(objects, NewEntryBlob(fmt.Sprintf("%d", j+1), emptyBlobHash))
		}

		treeHash, err := treeBuilder.WriteTreeFromEntries(objects)
		if err != nil {
			t.Fatal(err)
		}

		treeHashes = append(treeHashes, treeHash)
	}
	return treeHashes
}

func createChildrenCommits(t *testing.T, repo *Repository, treeHashes []Hash, parentHash Hash, numChildren int) []Hash {
	t.Helper()

	children := make([]Hash, 0, numChildren)

	for i := 1; i <= numChildren; i++ {
		commitID := repo.commitWithParents(t, treeHashes[i], []Hash{parentHash}, fmt.Sprintf("Test commit %v", i+1), false)
		children = append(children, commitID)
	}
	return children
}