MCPcopy Create free account
hub / github.com/libgit2/git2go / TestApplyDiffAddfile

Function TestApplyDiffAddfile

diff_test.go:243–464  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

241}
242
243func TestApplyDiffAddfile(t *testing.T) {
244 repo := createTestRepo(t)
245 defer cleanupTestRepo(t, repo)
246
247 seedTestRepo(t, repo)
248
249 addFirstFileCommit, addFirstFileTree := addAndGetTree(t, repo, "file1", `hello`)
250 defer addFirstFileCommit.Free()
251 defer addFirstFileTree.Free()
252 addSecondFileCommit, addSecondFileTree := addAndGetTree(t, repo, "file2", `hello2`)
253 defer addSecondFileCommit.Free()
254 defer addSecondFileTree.Free()
255
256 diff, err := repo.DiffTreeToTree(addFirstFileTree, addSecondFileTree, nil)
257 checkFatal(t, err)
258 defer diff.Free()
259
260 t.Run("check does not apply to current tree because file exists", func(t *testing.T) {
261 err = repo.ResetToCommit(addSecondFileCommit, ResetHard, &CheckoutOptions{})
262 checkFatal(t, err)
263
264 err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
265 if err == nil {
266 t.Error("expecting applying patch to current repo to fail")
267 }
268 })
269
270 t.Run("check apply to correct commit", func(t *testing.T) {
271 err = repo.ResetToCommit(addFirstFileCommit, ResetHard, &CheckoutOptions{})
272 checkFatal(t, err)
273
274 err = repo.ApplyDiff(diff, ApplyLocationBoth, nil)
275 checkFatal(t, err)
276
277 t.Run("Check that diff only changed one file", func(t *testing.T) {
278 checkSecondFileStaged(t, repo)
279
280 index, err := repo.Index()
281 checkFatal(t, err)
282 defer index.Free()
283
284 newTreeOID, err := index.WriteTreeTo(repo)
285 checkFatal(t, err)
286
287 newTree, err := repo.LookupTree(newTreeOID)
288 checkFatal(t, err)
289 defer newTree.Free()
290
291 _, err = repo.CreateCommit("HEAD", signature(), signature(), fmt.Sprintf("patch apply"), newTree, addFirstFileCommit)
292 checkFatal(t, err)
293 })
294
295 t.Run("test applying patch produced the same diff", func(t *testing.T) {
296 head, err := repo.Head()
297 checkFatal(t, err)
298
299 commit, err := repo.LookupCommit(head.Target())
300 checkFatal(t, err)

Callers

nothing calls this directly

Calls 15

createTestRepoFunction · 0.85
cleanupTestRepoFunction · 0.85
seedTestRepoFunction · 0.85
addAndGetTreeFunction · 0.85
checkFatalFunction · 0.85
checkSecondFileStagedFunction · 0.85
signatureFunction · 0.85
DiffFromBufferFunction · 0.85
DefaultApplyOptionsFunction · 0.85
checkNoFilesStagedFunction · 0.85
DiffTreeToTreeMethod · 0.80
ResetToCommitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…