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.

editor_test.go 2.5KB

Refactor editor upload, update and delete to use git plumbing and add LFS support (#5702) * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile * Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview * Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles * Move branch checkout functions out of repo_editor.go as they are no longer used there * BUGFIX: The default permissions should be 100644 This is a change from the previous code but is more in keeping with the default behaviour of git. Signed-off-by: Andrew Thornton <art27@cantab.net> * Standardise cleanUploadFilename to more closely match git See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton <art27@cantab.net> * Redirect on bad paths Signed-off-by: Andrew Thornton <art27@cantab.net> * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton <art27@cantab.net> * Add LFS support Signed-off-by: Andrew Thornton <art27@cantab.net> * Update upload.go attribution header Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution. * Delete upload files after session committed * Ensure that GIT_AUTHOR_NAME etc. are valid for git see #5774 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton <art27@cantab.net> * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton <art27@cantab.net> * fix formatting Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton <art27@cantab.net> * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton <art27@cantab.net>
5 年之前
Refactor editor upload, update and delete to use git plumbing and add LFS support (#5702) * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFile * Use git plumbing for upload: #5621 repo_editor.go: GetDiffPreview * Use git plumbing for upload: #5621 repo_editor.go: DeleteRepoFile * Use git plumbing for upload: #5621 repo_editor.go: UploadRepoFiles * Move branch checkout functions out of repo_editor.go as they are no longer used there * BUGFIX: The default permissions should be 100644 This is a change from the previous code but is more in keeping with the default behaviour of git. Signed-off-by: Andrew Thornton <art27@cantab.net> * Standardise cleanUploadFilename to more closely match git See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton <art27@cantab.net> * Redirect on bad paths Signed-off-by: Andrew Thornton <art27@cantab.net> * Refactor to move the uploading functions out to a module Signed-off-by: Andrew Thornton <art27@cantab.net> * Add LFS support Signed-off-by: Andrew Thornton <art27@cantab.net> * Update upload.go attribution header Upload.go is essentially the remnants of repo_editor.go. The remaining code is essentially unchanged from the Gogs code, hence the Gogs attribution. * Delete upload files after session committed * Ensure that GIT_AUTHOR_NAME etc. are valid for git see #5774 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add in test cases per @lafriks comment * Add space between gitea and github imports Signed-off-by: Andrew Thornton <art27@cantab.net> * more examples in TestCleanUploadName Signed-off-by: Andrew Thornton <art27@cantab.net> * fix formatting Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the SSH_ORIGINAL_COMMAND to ensure hooks are run Signed-off-by: Andrew Thornton <art27@cantab.net> * Switch off SSH_ORIGINAL_COMMAND Signed-off-by: Andrew Thornton <art27@cantab.net>
5 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "code.gitea.io/gitea/modules/git"
  7. "code.gitea.io/gitea/modules/test"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCleanUploadName(t *testing.T) {
  13. models.PrepareTestEnv(t)
  14. var kases = map[string]string{
  15. ".git/refs/master": "",
  16. "/root/abc": "root/abc",
  17. "./../../abc": "abc",
  18. "a/../.git": "",
  19. "a/../../../abc": "abc",
  20. "../../../acd": "acd",
  21. "../../.git/abc": "",
  22. "..\\..\\.git/abc": "..\\..\\.git/abc",
  23. "..\\../.git/abc": "",
  24. "..\\../.git": "",
  25. "abc/../def": "def",
  26. ".drone.yml": ".drone.yml",
  27. ".abc/def/.drone.yml": ".abc/def/.drone.yml",
  28. "..drone.yml.": "..drone.yml.",
  29. "..a.dotty...name...": "..a.dotty...name...",
  30. "..a.dotty../.folder../.name...": "..a.dotty../.folder../.name...",
  31. }
  32. for k, v := range kases {
  33. assert.EqualValues(t, cleanUploadFileName(k), v)
  34. }
  35. }
  36. func TestGetUniquePatchBranchName(t *testing.T) {
  37. models.PrepareTestEnv(t)
  38. ctx := test.MockContext(t, "user2/repo1")
  39. ctx.SetParams(":id", "1")
  40. test.LoadRepo(t, ctx, 1)
  41. test.LoadRepoCommit(t, ctx)
  42. test.LoadUser(t, ctx, 2)
  43. test.LoadGitRepo(t, ctx)
  44. defer ctx.Repo.GitRepo.Close()
  45. expectedBranchName := "user2-patch-1"
  46. branchName := GetUniquePatchBranchName(ctx)
  47. assert.Equal(t, expectedBranchName, branchName)
  48. }
  49. func TestGetClosestParentWithFiles(t *testing.T) {
  50. models.PrepareTestEnv(t)
  51. ctx := test.MockContext(t, "user2/repo1")
  52. ctx.SetParams(":id", "1")
  53. test.LoadRepo(t, ctx, 1)
  54. test.LoadRepoCommit(t, ctx)
  55. test.LoadUser(t, ctx, 2)
  56. test.LoadGitRepo(t, ctx)
  57. defer ctx.Repo.GitRepo.Close()
  58. repo := ctx.Repo.Repository
  59. branch := repo.DefaultBranch
  60. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  61. defer gitRepo.Close()
  62. commit, _ := gitRepo.GetBranchCommit(branch)
  63. expectedTreePath := ""
  64. expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
  65. for _, deletedFile := range []string{
  66. "dir1/dir2/dir3/file.txt",
  67. "file.txt",
  68. } {
  69. treePath := GetClosestParentWithFiles(deletedFile, commit)
  70. assert.Equal(t, expectedTreePath, treePath)
  71. }
  72. }