diff options
author | Toon Claes <toon@gitlab.com> | 2023-12-15 12:03:16 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2023-12-18 11:54:03 +0300 |
commit | 509f32f183fcfac6bdf8b59e8d1042e95795cf4d (patch) | |
tree | e401a5635a00ef863a9d1a6737d629652023fa0c | |
parent | afb5d1fa3a87c69cd7803f7ebf8dfc905310e15f (diff) |
gitattributes: Disallow empty list of attribute names
When the user passes an empty list of attributes, we cannot properly
parse the output from git-check-attr(1). To protect against that, return
an error when the list of names passed to `gitattributes.CheckAttr()` is
empty.
-rw-r--r-- | internal/git/gitattributes/check_attr.go | 5 | ||||
-rw-r--r-- | internal/git/gitattributes/check_attr_test.go | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/internal/git/gitattributes/check_attr.go b/internal/git/gitattributes/check_attr.go index acacf8cc8..8596efaa3 100644 --- a/internal/git/gitattributes/check_attr.go +++ b/internal/git/gitattributes/check_attr.go @@ -10,6 +10,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/command" "gitlab.com/gitlab-org/gitaly/v16/internal/git" + "gitlab.com/gitlab-org/gitaly/v16/internal/structerr" ) // CheckAttrCmd can be used to get the gitattributes(5) for a set of files in a @@ -26,6 +27,10 @@ type CheckAttrCmd struct { // CheckAttr creates a CheckAttrCmd that checks the given list of attribute names. func CheckAttr(ctx context.Context, repo git.RepositoryExecutor, revision git.Revision, names []string) (*CheckAttrCmd, func(), error) { + if len(names) == 0 { + return nil, nil, structerr.NewInvalidArgument("empty list of attribute names") + } + cmd, err := repo.Exec(ctx, git.Command{ Name: "check-attr", Flags: []git.Option{ diff --git a/internal/git/gitattributes/check_attr_test.go b/internal/git/gitattributes/check_attr_test.go index dcd7e4e0a..1e65b17f9 100644 --- a/internal/git/gitattributes/check_attr_test.go +++ b/internal/git/gitattributes/check_attr_test.go @@ -7,10 +7,20 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/git" "gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v16/internal/git/localrepo" + "gitlab.com/gitlab-org/gitaly/v16/internal/structerr" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg" ) +func TestCheckAttr(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + _, finish, err := CheckAttr(ctx, nil, "HEAD", nil) + require.Equal(t, structerr.NewInvalidArgument("empty list of attribute names"), err) + require.Nil(t, finish) +} + func TestCheckAttrCmd_Check(t *testing.T) { ctx := testhelper.Context(t) cfg := testcfg.Build(t) |