You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_compare_test.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "bytes"
  6. "io"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestGetFormatPatch(t *testing.T) {
  12. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  13. clonedPath, err := cloneRepo(t, bareRepo1Path)
  14. if err != nil {
  15. assert.NoError(t, err)
  16. return
  17. }
  18. repo, err := openRepositoryWithDefaultContext(clonedPath)
  19. if err != nil {
  20. assert.NoError(t, err)
  21. return
  22. }
  23. defer repo.Close()
  24. rd := &bytes.Buffer{}
  25. err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
  26. if err != nil {
  27. assert.NoError(t, err)
  28. return
  29. }
  30. patchb, err := io.ReadAll(rd)
  31. if err != nil {
  32. assert.NoError(t, err)
  33. return
  34. }
  35. patch := string(patchb)
  36. assert.Regexp(t, "^From 8d92fc95", patch)
  37. assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
  38. }
  39. func TestReadPatch(t *testing.T) {
  40. // Ensure we can read the patch files
  41. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  42. repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
  43. if err != nil {
  44. assert.NoError(t, err)
  45. return
  46. }
  47. defer repo.Close()
  48. // This patch doesn't exist
  49. noFile, err := repo.ReadPatchCommit(0)
  50. assert.Error(t, err)
  51. // This patch is an empty one (sometimes it's a 404)
  52. noCommit, err := repo.ReadPatchCommit(1)
  53. assert.Error(t, err)
  54. // This patch is legit and should return a commit
  55. oldCommit, err := repo.ReadPatchCommit(2)
  56. if err != nil {
  57. assert.NoError(t, err)
  58. return
  59. }
  60. assert.Empty(t, noFile)
  61. assert.Empty(t, noCommit)
  62. assert.Len(t, oldCommit, 40)
  63. assert.True(t, oldCommit == "6e8e2a6f9efd71dbe6917816343ed8415ad696c3")
  64. }
  65. func TestReadWritePullHead(t *testing.T) {
  66. // Ensure we can write SHA1 head corresponding to PR and open them
  67. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  68. // As we are writing we should clone the repository first
  69. clonedPath, err := cloneRepo(t, bareRepo1Path)
  70. if err != nil {
  71. assert.NoError(t, err)
  72. return
  73. }
  74. repo, err := openRepositoryWithDefaultContext(clonedPath)
  75. if err != nil {
  76. assert.NoError(t, err)
  77. return
  78. }
  79. defer repo.Close()
  80. // Try to open non-existing Pull
  81. _, err = repo.GetRefCommitID(PullPrefix + "0/head")
  82. assert.Error(t, err)
  83. // Write a fake sha1 with only 40 zeros
  84. newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
  85. err = repo.SetReference(PullPrefix+"1/head", newCommit)
  86. if err != nil {
  87. assert.NoError(t, err)
  88. return
  89. }
  90. // Read the file created
  91. headContents, err := repo.GetRefCommitID(PullPrefix + "1/head")
  92. if err != nil {
  93. assert.NoError(t, err)
  94. return
  95. }
  96. assert.Len(t, headContents, 40)
  97. assert.True(t, headContents == newCommit)
  98. // Remove file after the test
  99. err = repo.RemoveReference(PullPrefix + "1/head")
  100. assert.NoError(t, err)
  101. }
  102. func TestGetCommitFilesChanged(t *testing.T) {
  103. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  104. repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
  105. assert.NoError(t, err)
  106. defer repo.Close()
  107. testCases := []struct {
  108. base, head string
  109. files []string
  110. }{
  111. {
  112. EmptySHA,
  113. "95bb4d39648ee7e325106df01a621c530863a653",
  114. []string{"file1.txt"},
  115. },
  116. {
  117. EmptySHA,
  118. "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  119. []string{"file2.txt"},
  120. },
  121. {
  122. "95bb4d39648ee7e325106df01a621c530863a653",
  123. "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  124. []string{"file2.txt"},
  125. },
  126. {
  127. EmptyTreeSHA,
  128. "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
  129. []string{"file1.txt", "file2.txt"},
  130. },
  131. }
  132. for _, tc := range testCases {
  133. changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head)
  134. assert.NoError(t, err)
  135. assert.ElementsMatch(t, tc.files, changedFiles)
  136. }
  137. }