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.

78 lines
2.4 KiB

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 years ago
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 years ago
  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. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/test"
  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. expectedBranchName := "user2-patch-1"
  45. branchName := GetUniquePatchBranchName(ctx)
  46. assert.Equal(t, expectedBranchName, branchName)
  47. }
  48. func TestGetClosestParentWithFiles(t *testing.T) {
  49. models.PrepareTestEnv(t)
  50. ctx := test.MockContext(t, "user2/repo1")
  51. ctx.SetParams(":id", "1")
  52. test.LoadRepo(t, ctx, 1)
  53. test.LoadRepoCommit(t, ctx)
  54. test.LoadUser(t, ctx, 2)
  55. test.LoadGitRepo(t, ctx)
  56. repo := ctx.Repo.Repository
  57. branch := repo.DefaultBranch
  58. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  59. commit, _ := gitRepo.GetBranchCommit(branch)
  60. expectedTreePath := ""
  61. expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
  62. for _, deletedFile := range []string{
  63. "dir1/dir2/dir3/file.txt",
  64. "file.txt",
  65. } {
  66. treePath := GetClosestParentWithFiles(deletedFile, commit)
  67. assert.Equal(t, expectedTreePath, treePath)
  68. }
  69. }