gitea源码

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