diff options
author | Stan Hu <stanhu@gmail.com> | 2018-12-20 08:43:33 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-12-21 00:12:35 +0300 |
commit | 1d5ae61515b70d694499ebaaefb588d655a83ec5 (patch) | |
tree | 10c9b14c5be0d57e88d7e74bf93286d57410ed6f | |
parent | 606874698c0f184e3bd7f4beae9af1b3258828ce (diff) |
Fix incorrect tree entries retrieved with directories that have curly braces
In a `git cat-file` request in the form of `ref^{tree}:{{folder}}`, a
trailing `}` causes Git to give up early and assume the intended tree is
the root tree. This appears to be a bug in Git:
https://marc.info/?l=git&m=154533633212043&w=2
Until that Git bug is resolved, we work around the problem by omitting
the type restriction. This restriction is checked in the Gitaly service
code in any case.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/46261
-rw-r--r-- | changelogs/unreleased/sh-fix-filenames-with-curly-braces.yml | 5 | ||||
-rw-r--r-- | internal/service/commit/tree_entries_helper.go | 2 | ||||
-rw-r--r-- | internal/service/commit/tree_entries_test.go | 68 |
3 files changed, 74 insertions, 1 deletions
diff --git a/changelogs/unreleased/sh-fix-filenames-with-curly-braces.yml b/changelogs/unreleased/sh-fix-filenames-with-curly-braces.yml new file mode 100644 index 000000000..45ee9f6f6 --- /dev/null +++ b/changelogs/unreleased/sh-fix-filenames-with-curly-braces.yml @@ -0,0 +1,5 @@ +--- +title: Fix incorrect tree entries retrieved with directories that have curly braces +merge_request: 1013 +author: +type: fixed diff --git a/internal/service/commit/tree_entries_helper.go b/internal/service/commit/tree_entries_helper.go index 1edb74fa2..369b1b2a1 100644 --- a/internal/service/commit/tree_entries_helper.go +++ b/internal/service/commit/tree_entries_helper.go @@ -74,7 +74,7 @@ func treeEntries(c *catfile.Batch, revision, path string, rootOid string, recurs rootOid = rootTreeInfo.Oid } - treeEntryInfo, err := c.Info(fmt.Sprintf("%s^{tree}:%s", revision, path)) + treeEntryInfo, err := c.Info(fmt.Sprintf("%s:%s", revision, path)) if err != nil { if catfile.IsNotFound(err) { return nil, nil diff --git a/internal/service/commit/tree_entries_test.go b/internal/service/commit/tree_entries_test.go index 48a81a484..f496e49d2 100644 --- a/internal/service/commit/tree_entries_test.go +++ b/internal/service/commit/tree_entries_test.go @@ -3,6 +3,8 @@ package commit import ( "fmt" "io" + "os" + "path" "testing" "github.com/stretchr/testify/require" @@ -12,6 +14,72 @@ import ( "google.golang.org/grpc/codes" ) +func TestSuccessfulGetTreeEntriesWithCurlyBraces(t *testing.T) { + server, serverSocketPath := startTestServices(t) + defer server.Stop() + + client, conn := newCommitServiceClient(t, serverSocketPath) + defer conn.Close() + + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepoWithWorktree(t) + defer cleanupFn() + + normalFolderName := "issue-46261/folder" + curlyFolderName := "issue-46261/{{curly}}" + normalFolder := path.Join(testRepoPath, normalFolderName) + curlyFolder := path.Join(testRepoPath, curlyFolderName) + + os.MkdirAll(normalFolder, 0755) + os.MkdirAll(curlyFolder, 0755) + + testhelper.MustRunCommand(t, nil, "touch", path.Join(normalFolder, "/test1.txt")) + testhelper.MustRunCommand(t, nil, "touch", path.Join(curlyFolder, "/test2.txt")) + + testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "add", "--all") + testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "commit", "-m", "Test commit") + + testCases := []struct { + description string + revision []byte + path []byte + recursive bool + filename []byte + }{ + { + description: "with a normal folder", + revision: []byte("master"), + path: []byte(normalFolderName), + filename: []byte("issue-46261/folder/test1.txt"), + }, + { + description: "with a folder with curly braces", + revision: []byte("master"), + path: []byte(curlyFolderName), + filename: []byte("issue-46261/{{curly}}/test2.txt"), + }, + } + + for _, testCase := range testCases { + request := &gitalypb.GetTreeEntriesRequest{ + Repository: testRepo, + Revision: []byte("HEAD"), + Path: testCase.path, + Recursive: testCase.recursive, + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + c, err := client.GetTreeEntries(ctx, request) + if err != nil { + t.Fatal(err) + } + + fetchedEntries := getTreeEntriesFromTreeEntryClient(t, c) + require.Equal(t, 1, len(fetchedEntries)) + require.Equal(t, testCase.filename, fetchedEntries[0].FlatPath) + } +} + func TestSuccessfulGetTreeEntries(t *testing.T) { // Force entries to be sliced to test that behaviour oldMaxTreeEntries := maxTreeEntries |