Move repofiles from modules/repofiles to services/repository/files (#17774)
* Move repofiles from modules to services * rename services/repository/repofiles -> services/repository/files * Fix test Co-authored-by: 6543 <6543@obermui.de>tokarchuk/v1.17
parent
754fdd8f9c
commit
c97d66d23c
@ -1,41 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"net/url" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
api "code.gitea.io/gitea/modules/structs" |
||||
) |
||||
|
||||
// GetBlobBySHA get the GitBlobResponse of a repository using a sha hash.
|
||||
func GetBlobBySHA(repo *models.Repository, sha string) (*api.GitBlobResponse, error) { |
||||
gitRepo, err := git.OpenRepository(repo.RepoPath()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer gitRepo.Close() |
||||
gitBlob, err := gitRepo.GetBlob(sha) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
content := "" |
||||
if gitBlob.Size() <= setting.API.DefaultMaxBlobSize { |
||||
content, err = gitBlob.GetBlobContentBase64() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
return &api.GitBlobResponse{ |
||||
SHA: gitBlob.ID.String(), |
||||
URL: repo.APIURL() + "/git/blobs/" + url.PathEscape(gitBlob.ID.String()), |
||||
Size: gitBlob.Size(), |
||||
Encoding: "base64", |
||||
Content: content, |
||||
}, nil |
||||
} |
@ -1,40 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models/unittest" |
||||
api "code.gitea.io/gitea/modules/structs" |
||||
"code.gitea.io/gitea/modules/test" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestGetBlobBySHA(t *testing.T) { |
||||
unittest.PrepareTestEnv(t) |
||||
ctx := test.MockContext(t, "user2/repo1") |
||||
test.LoadRepo(t, ctx, 1) |
||||
test.LoadRepoCommit(t, ctx) |
||||
test.LoadUser(t, ctx, 2) |
||||
test.LoadGitRepo(t, ctx) |
||||
defer ctx.Repo.GitRepo.Close() |
||||
|
||||
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" |
||||
ctx.SetParams(":id", "1") |
||||
ctx.SetParams(":sha", sha) |
||||
|
||||
gbr, err := GetBlobBySHA(ctx.Repo.Repository, ctx.Params(":sha")) |
||||
expectedGBR := &api.GitBlobResponse{ |
||||
Content: "dHJlZSAyYTJmMWQ0NjcwNzI4YTJlMTAwNDllMzQ1YmQ3YTI3NjQ2OGJlYWI2CmF1dGhvciB1c2VyMSA8YWRkcmVzczFAZXhhbXBsZS5jb20+IDE0ODk5NTY0NzkgLTA0MDAKY29tbWl0dGVyIEV0aGFuIEtvZW5pZyA8ZXRoYW50a29lbmlnQGdtYWlsLmNvbT4gMTQ4OTk1NjQ3OSAtMDQwMAoKSW5pdGlhbCBjb21taXQK", |
||||
Encoding: "base64", |
||||
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/65f1bf27bc3bf70f64657658635e66094edbcb4d", |
||||
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d", |
||||
Size: 180, |
||||
} |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, expectedGBR, gbr) |
||||
} |
@ -1,19 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
) |
||||
|
||||
// CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch
|
||||
func CountDivergingCommits(repo *models.Repository, branch string) (*git.DivergeObject, error) { |
||||
divergence, err := git.GetDivergingCommits(repo.RepoPath(), repo.DefaultBranch, branch) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &divergence, nil |
||||
} |
@ -1,41 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
) |
||||
|
||||
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
|
||||
// NOTE: All text-values will be trimmed from whitespaces.
|
||||
// Requires: Repo, Creator, SHA
|
||||
func CreateCommitStatus(repo *models.Repository, creator *models.User, sha string, status *models.CommitStatus) error { |
||||
repoPath := repo.RepoPath() |
||||
|
||||
// confirm that commit is exist
|
||||
gitRepo, err := git.OpenRepository(repoPath) |
||||
if err != nil { |
||||
return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) |
||||
} |
||||
if _, err := gitRepo.GetCommit(sha); err != nil { |
||||
gitRepo.Close() |
||||
return fmt.Errorf("GetCommit[%s]: %v", sha, err) |
||||
} |
||||
gitRepo.Close() |
||||
|
||||
if err := models.NewCommitStatus(models.NewCommitStatusOptions{ |
||||
Repo: repo, |
||||
Creator: creator, |
||||
SHA: sha, |
||||
CommitStatus: status, |
||||
}); err != nil { |
||||
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err) |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -1,23 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.package repofiles
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"path" |
||||
"strings" |
||||
) |
||||
|
||||
// CleanUploadFileName Trims a filename and returns empty string if it is a .git directory
|
||||
func CleanUploadFileName(name string) string { |
||||
// Rebase the filename
|
||||
name = strings.Trim(path.Clean("/"+name), " /") |
||||
// Git disallows any filenames to have a .git directory in them.
|
||||
for _, part := range strings.Split(name, "/") { |
||||
if strings.ToLower(part) == ".git" { |
||||
return "" |
||||
} |
||||
} |
||||
return name |
||||
} |
@ -1,27 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestCleanUploadFileName(t *testing.T) { |
||||
t.Run("Clean regular file", func(t *testing.T) { |
||||
name := "this/is/test" |
||||
cleanName := CleanUploadFileName(name) |
||||
expectedCleanName := name |
||||
assert.EqualValues(t, expectedCleanName, cleanName) |
||||
}) |
||||
|
||||
t.Run("Clean a .git path", func(t *testing.T) { |
||||
name := "this/is/test/.git" |
||||
cleanName := CleanUploadFileName(name) |
||||
expectedCleanName := "" |
||||
assert.EqualValues(t, expectedCleanName, cleanName) |
||||
}) |
||||
} |
@ -1,33 +0,0 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repofiles |
||||
|
||||
import ( |
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/structs" |
||||
) |
||||
|
||||
// GetPayloadCommitVerification returns the verification information of a commit
|
||||
func GetPayloadCommitVerification(commit *git.Commit) *structs.PayloadCommitVerification { |
||||
verification := &structs.PayloadCommitVerification{} |
||||
commitVerification := models.ParseCommitWithSignature(commit) |
||||
if commit.Signature != nil { |
||||
verification.Signature = commit.Signature.Signature |
||||
verification.Payload = commit.Signature.Payload |
||||
} |
||||
if commitVerification.SigningUser != nil { |
||||
verification.Signer = &structs.PayloadUser{ |
||||
Name: commitVerification.SigningUser.Name, |
||||
Email: commitVerification.SigningUser.Email, |
||||
} |
||||
} |
||||
verification.Verified = commitVerification.Verified |
||||
verification.Reason = commitVerification.Reason |
||||
if verification.Reason == "" && !verification.Verified { |
||||
verification.Reason = "gpg.error.not_signed_commit" |
||||
} |
||||
return verification |
||||
} |
@ -1,26 +0,0 @@ |
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package repository |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
) |
||||
|
||||
// GetBranch returns a branch by its name
|
||||
func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) { |
||||
if len(branch) == 0 { |
||||
return nil, fmt.Errorf("GetBranch: empty string for branch") |
||||
} |
||||
gitRepo, err := git.OpenRepository(repo.RepoPath()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer gitRepo.Close() |
||||
|
||||
return gitRepo.GetBranch(branch) |
||||
} |
@ -0,0 +1,73 @@ |
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package files |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"code.gitea.io/gitea/models" |
||||
"code.gitea.io/gitea/modules/git" |
||||
"code.gitea.io/gitea/modules/structs" |
||||
) |
||||
|
||||
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
|
||||
// NOTE: All text-values will be trimmed from whitespaces.
|
||||
// Requires: Repo, Creator, SHA
|
||||
func CreateCommitStatus(repo *models.Repository, creator *models.User, sha string, status *models.CommitStatus) error { |
||||
repoPath := repo.RepoPath() |
||||
|
||||
// confirm that commit is exist
|
||||
gitRepo, err := git.OpenRepository(repoPath) |
||||
if err != nil { |
||||
return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err) |
||||
} |
||||
if _, err := gitRepo.GetCommit(sha); err != nil { |
||||
gitRepo.Close() |
||||
return fmt.Errorf("GetCommit[%s]: %v", sha, err) |
||||
} |
||||
gitRepo.Close() |
||||
|
||||
if err := models.NewCommitStatus(models.NewCommitStatusOptions{ |
||||
Repo: repo, |
||||
Creator: creator, |
||||
SHA: sha, |
||||
CommitStatus: status, |
||||
}); err != nil { |
||||
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err) |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch
|
||||
func CountDivergingCommits(repo *models.Repository, branch string) (*git.DivergeObject, error) { |
||||
divergence, err := git.GetDivergingCommits(repo.RepoPath(), repo.DefaultBranch, branch) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &divergence, nil |
||||
} |
||||
|
||||
// GetPayloadCommitVerification returns the verification information of a commit
|
||||
func GetPayloadCommitVerification(commit *git.Commit) *structs.PayloadCommitVerification { |
||||
verification := &structs.PayloadCommitVerification{} |
||||
commitVerification := models.ParseCommitWithSignature(commit) |
||||
if commit.Signature != nil { |
||||
verification.Signature = commit.Signature.Signature |
||||
verification.Payload = commit.Signature.Payload |
||||
} |
||||
if commitVerification.SigningUser != nil { |
||||
verification.Signer = &structs.PayloadUser{ |
||||
Name: commitVerification.SigningUser.Name, |
||||
Email: commitVerification.SigningUser.Email, |
||||
} |
||||
} |
||||
verification.Verified = commitVerification.Verified |
||||
verification.Reason = commitVerification.Reason |
||||
if verification.Reason == "" && !verification.Verified { |
||||
verification.Reason = "gpg.error.not_signed_commit" |
||||
} |
||||
return verification |
||||
} |
Loading…
Reference in new issue