mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-17 21:00:38 +02:00
Use modern "git update-index --cacheinfo" syntax to support more file names (#37338)
Modern syntax was added in git 2.0 And add more tests
This commit is contained in:
parent
aee6628bf5
commit
38d337c94a
@ -178,8 +178,9 @@ func (t *TemporaryUploadRepository) HashObjectAndWrite(ctx context.Context, cont
|
|||||||
|
|
||||||
// AddObjectToIndex adds the provided object hash to the index with the provided mode and path
|
// AddObjectToIndex adds the provided object hash to the index with the provided mode and path
|
||||||
func (t *TemporaryUploadRepository) AddObjectToIndex(ctx context.Context, mode, objectHash, objectPath string) error {
|
func (t *TemporaryUploadRepository) AddObjectToIndex(ctx context.Context, mode, objectHash, objectPath string) error {
|
||||||
if err := gitcmd.NewCommand("update-index", "--add", "--replace", "--cacheinfo").
|
cmd := gitcmd.NewCommand("update-index", "--add", "--replace", "--cacheinfo").
|
||||||
AddDynamicArguments(mode, objectHash, objectPath).WithDir(t.basePath).RunWithStderr(ctx); err != nil {
|
AddDynamicArguments(mode + "," + objectHash + "," + objectPath).WithDir(t.basePath)
|
||||||
|
if err := cmd.RunWithStderr(ctx); err != nil {
|
||||||
if matched, _ := regexp.MatchString(".*Invalid path '.*", err.Stderr()); matched {
|
if matched, _ := regexp.MatchString(".*Invalid path '.*", err.Stderr()); matched {
|
||||||
return ErrFilePathInvalid{
|
return ErrFilePathInvalid{
|
||||||
Message: objectPath,
|
Message: objectPath,
|
||||||
|
|||||||
45
services/repository/files/temp_repo_test.go
Normal file
45
services/repository/files/temp_repo_test.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTemporaryUploadRepository(t *testing.T) {
|
||||||
|
mockedRepo := &repo_model.Repository{Name: "mocked-repo-name", OwnerName: "mocked-owner-name"}
|
||||||
|
|
||||||
|
doTest := func(t *testing.T, objectFormatName string) {
|
||||||
|
tmpGitRepo, err := NewTemporaryUploadRepository(mockedRepo)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer tmpGitRepo.Close()
|
||||||
|
|
||||||
|
require.NoError(t, tmpGitRepo.Init(t.Context(), objectFormatName))
|
||||||
|
|
||||||
|
require.NoError(t, tmpGitRepo.RemoveFilesFromIndex(t.Context(), "any-file-name"))
|
||||||
|
require.NoError(t, tmpGitRepo.RemoveFilesFromIndex(t.Context(), "--any-file-name"))
|
||||||
|
|
||||||
|
objID, err := tmpGitRepo.HashObjectAndWrite(t.Context(), bytes.NewReader(nil))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, tmpGitRepo.AddObjectToIndex(t.Context(), "100644", objID, "any-file-name"))
|
||||||
|
require.NoError(t, tmpGitRepo.AddObjectToIndex(t.Context(), "100644", objID, "--any-file-name"))
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("sha1", func(t *testing.T) {
|
||||||
|
doTest(t, git.Sha1ObjectFormat.Name())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("sha256", func(t *testing.T) {
|
||||||
|
if !git.DefaultFeatures().SupportHashSha256 {
|
||||||
|
t.Skip("sha256 is not supported")
|
||||||
|
}
|
||||||
|
doTest(t, git.Sha256ObjectFormat.Name())
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user