diff options
author | Vasilii Iakliushin <viakliushin@gitlab.com> | 2023-03-01 20:12:30 +0300 |
---|---|---|
committer | Vasilii Iakliushin <viakliushin@gitlab.com> | 2023-03-01 20:12:30 +0300 |
commit | 0ab47cf61488ed6c40630cf0564852d9b5a0cf75 (patch) | |
tree | e6472262c7ca8e85c4e2b405f6b62764f8301e50 | |
parent | 6f6f25a04766bd7f136732e94f3df67481470108 (diff) |
Fix positional argument error when file starts with `-`
Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/393553
**Problem**
We provide file path in Args list, but it does not support files that
start with dash at the beginning.
```
git ls-tree HEAD -test
error: unknown switch `e'
```
**Solution**
Provide path in PostSepArgs list.
```
git ls-tree HEAD -- -test
```
This command supports files that start with dash.
Changelog: fixed
-rw-r--r-- | internal/gitaly/service/commit/list_last_commits_for_tree.go | 7 | ||||
-rw-r--r-- | internal/gitaly/service/commit/list_last_commits_for_tree_test.go | 22 |
2 files changed, 26 insertions, 3 deletions
diff --git a/internal/gitaly/service/commit/list_last_commits_for_tree.go b/internal/gitaly/service/commit/list_last_commits_for_tree.go index e520104cb..20409fb93 100644 --- a/internal/gitaly/service/commit/list_last_commits_for_tree.go +++ b/internal/gitaly/service/commit/list_last_commits_for_tree.go @@ -116,9 +116,10 @@ func (s *server) newLSTreeParser(in *gitalypb.ListLastCommitsForTreeRequest, str opts := git.ConvertGlobalOptions(in.GetGlobalOptions()) cmd, err := s.gitCmdFactory.New(stream.Context(), in.GetRepository(), git.Command{ - Name: "ls-tree", - Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--full-name"}}, - Args: []string{in.GetRevision(), path}, + Name: "ls-tree", + Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--full-name"}}, + Args: []string{in.GetRevision()}, + PostSepArgs: []string{path}, }, opts...) if err != nil { return nil, nil, err diff --git a/internal/gitaly/service/commit/list_last_commits_for_tree_test.go b/internal/gitaly/service/commit/list_last_commits_for_tree_test.go index e60b92096..f4a73f465 100644 --- a/internal/gitaly/service/commit/list_last_commits_for_tree_test.go +++ b/internal/gitaly/service/commit/list_last_commits_for_tree_test.go @@ -382,6 +382,28 @@ func TestSuccessfulListLastCommitsForTreeRequestWithGlobCharacters(t *testing.T) }) } +func TestSuccessfulListLastCommitsForTreeRequestWithDashAtTheBeginning(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(t, ctx) + + commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries(gittest.TreeEntry{ + Path: "-test", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + {Path: "README.md", Mode: "100644", Content: "something"}, + }), + })) + + stream, err := client.ListLastCommitsForTree(ctx, &gitalypb.ListLastCommitsForTreeRequest{ + Repository: repo, + Revision: commitID.String(), + Path: []byte("-test"), + Limit: 100, + }) + require.NoError(t, err) + require.Equal(t, []string{"-test"}, fetchCommitPaths(t, stream)) +} + func fileExistsInCommits(t *testing.T, stream gitalypb.CommitService_ListLastCommitsForTreeClient, path string) bool { t.Helper() |