diff options
author | Toon Claes <toon@gitlab.com> | 2023-12-13 16:59:38 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2023-12-18 11:54:03 +0300 |
commit | afb5d1fa3a87c69cd7803f7ebf8dfc905310e15f (patch) | |
tree | 28b502f1ccc247eb15c42744982c9f9d4f2cbba3 | |
parent | 95dd9a8271285cf4a7aa4c42ca82319180643f2e (diff) |
gitattributes: Get rid of endOfAttributes
`gitattributes.CheckAttr` appended "end-of-attributes-----" to the names
of the attributes it wants to read. When the output of git-check-attr(1)
is parsed, Gitaly reads until this attribute is found.
Instead, memorize the number of attributes the user requested and keep
reading until that number of attributes is read. This works because the
user should not do concurrent calls to `Check()`
Label: maintenance::refactor
-rw-r--r-- | internal/git/gitattributes/check_attr.go | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/internal/git/gitattributes/check_attr.go b/internal/git/gitattributes/check_attr.go index 35b9c9538..acacf8cc8 100644 --- a/internal/git/gitattributes/check_attr.go +++ b/internal/git/gitattributes/check_attr.go @@ -12,12 +12,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/git" ) -// endOfAttributes is a magic attribute name we use to detect all attributes -// have been processed. It is added to the end of the list of attributes the -// caller requests, when this attribute is seen we know it's the last one and we -// can break the loop. -const endOfAttributes = "end-of-attributes-----" - // CheckAttrCmd can be used to get the gitattributes(5) for a set of files in a // repo. type CheckAttrCmd struct { @@ -25,6 +19,8 @@ type CheckAttrCmd struct { stdout *bufio.Reader stdin *bufio.Writer + count int + m *sync.Mutex } @@ -37,7 +33,7 @@ func CheckAttr(ctx context.Context, repo git.RepositoryExecutor, revision git.Re git.Flag{Name: "-z"}, git.ValueFlag{Name: "--source", Value: revision.String()}, }, - Args: append(names, endOfAttributes), + Args: names, }, git.WithSetupStdin(), git.WithSetupStdout(), @@ -50,6 +46,7 @@ func CheckAttr(ctx context.Context, repo git.RepositoryExecutor, revision git.Re cmd: cmd, stdout: bufio.NewReader(cmd), stdin: bufio.NewWriter(cmd), + count: len(names), m: &sync.Mutex{}, } @@ -77,7 +74,7 @@ func (c CheckAttrCmd) Check(path string) (Attributes, error) { // Using git-check-attr(1) with -z will return data in the format: // <path> NUL <attribute> NUL <info> NUL ... - for { + for i := 0; i < c.count; { word, err := c.stdout.ReadBytes('\000') if err != nil { return nil, fmt.Errorf("read line: %w", err) @@ -92,13 +89,11 @@ func (c CheckAttrCmd) Check(path string) (Attributes, error) { if buf[0] != path { return nil, fmt.Errorf("wrong path name detected, expected %q, got %q", path, buf[0]) } - if buf[1] == endOfAttributes { - break - } if buf[2] != Unspecified { attrs = append(attrs, Attribute{Name: buf[1], State: buf[2]}) } + i++ buf = buf[:0] } |