diff options
author | Toon Claes <toon@gitlab.com> | 2023-09-21 17:55:47 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2023-10-02 09:15:29 +0300 |
commit | a97c037db0987d747fd8727d247f1179c8457f72 (patch) | |
tree | 84ba06ae20866ee9708df9ae266b57ebd62f2c6e | |
parent | a19743506c1d63fc6959cb6d022f0c5bec9f9ef3 (diff) |
gitattributes: Implement the use of --source
Since Git 2.40 git-check-attr(1) learned to accept a --source argument
to specify the tree-ish to read the file from. This makes it unnecessary
to copy the contents of that file to info/attributes.
This change makes CheckAttr() accept a git revision to read the
.gitattributes straight from the given revision.
Label: maintenance::refactor
-rw-r--r-- | internal/git/gitattributes/check_attr.go | 3 | ||||
-rw-r--r-- | internal/git/gitattributes/check_attr_test.go | 38 | ||||
-rw-r--r-- | internal/git/gittest/tree.go | 4 | ||||
-rw-r--r-- | internal/gitaly/linguist/linguist.go | 2 | ||||
-rw-r--r-- | internal/gitaly/linguist/linguist_test.go | 9 |
5 files changed, 29 insertions, 27 deletions
diff --git a/internal/git/gitattributes/check_attr.go b/internal/git/gitattributes/check_attr.go index 474bd3bc5..35b9c9538 100644 --- a/internal/git/gitattributes/check_attr.go +++ b/internal/git/gitattributes/check_attr.go @@ -29,12 +29,13 @@ type CheckAttrCmd struct { } // CheckAttr creates a CheckAttrCmd that checks the given list of attribute names. -func CheckAttr(ctx context.Context, repo git.RepositoryExecutor, names []string) (*CheckAttrCmd, func(), error) { +func CheckAttr(ctx context.Context, repo git.RepositoryExecutor, revision git.Revision, names []string) (*CheckAttrCmd, func(), error) { cmd, err := repo.Exec(ctx, git.Command{ Name: "check-attr", Flags: []git.Option{ git.Flag{Name: "--stdin"}, git.Flag{Name: "-z"}, + git.ValueFlag{Name: "--source", Value: revision.String()}, }, Args: append(names, endOfAttributes), }, diff --git a/internal/git/gitattributes/check_attr_test.go b/internal/git/gitattributes/check_attr_test.go index 98ef1bb2b..dcd7e4e0a 100644 --- a/internal/git/gitattributes/check_attr_test.go +++ b/internal/git/gitattributes/check_attr_test.go @@ -1,21 +1,17 @@ package gitattributes import ( - "os" - "path/filepath" "testing" "github.com/stretchr/testify/require" + "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/helper/perm" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg" ) func TestCheckAttrCmd_Check(t *testing.T) { - t.Parallel() - ctx := testhelper.Context(t) cfg := testcfg.Build(t) @@ -24,14 +20,9 @@ func TestCheckAttrCmd_Check(t *testing.T) { }) repo := localrepo.NewTestRepo(t, cfg, repoProto) - // Until https://gitlab.com/groups/gitlab-org/-/epics/9006 is completed - // we rely on info/attributes. - infoPath := filepath.Join(repoPath, "info") - require.NoError(t, os.MkdirAll(infoPath, perm.SharedDir)) - attrPath := filepath.Join(infoPath, "attributes") - for _, tc := range []struct { desc string + treeOID git.ObjectID attrContent string path string expectedAttr Attributes @@ -39,6 +30,12 @@ func TestCheckAttrCmd_Check(t *testing.T) { { desc: "no attributes", path: "README.md", + treeOID: gittest.DefaultObjectHash.EmptyTreeOID, + expectedAttr: Attributes{}, + }, + { + desc: "empty attributes", + path: "README.md", expectedAttr: Attributes{}, }, { @@ -138,10 +135,25 @@ func TestCheckAttrCmd_Check(t *testing.T) { expectedAttr: Attributes{Attribute{Name: "foo", State: "bar"}}, }, } { + tc := tc + t.Run(tc.desc, func(t *testing.T) { - require.NoError(t, os.WriteFile(attrPath, []byte(tc.attrContent), perm.SharedFile)) + t.Parallel() + + treeOID := tc.treeOID + if treeOID == "" { + treeOID = gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + {Path: ".gitattributes", Mode: "100644", Content: tc.attrContent}, + }) + } else { + require.Empty(t, tc.attrContent, "cannot specify both attrContent & treeOID") + } + + commitID := gittest.WriteCommit(t, cfg, repoPath, + gittest.WithMessage(tc.desc), + gittest.WithTree(treeOID)) - checkCmd, finish, err := CheckAttr(ctx, repo, []string{"foo", "bar"}) + checkCmd, finish, err := CheckAttr(ctx, repo, commitID.Revision(), []string{"foo", "bar"}) require.NoError(t, err) t.Cleanup(finish) diff --git a/internal/git/gittest/tree.go b/internal/git/gittest/tree.go index 58db5e888..7237d32b7 100644 --- a/internal/git/gittest/tree.go +++ b/internal/git/gittest/tree.go @@ -98,11 +98,9 @@ func WriteTree(tb testing.TB, cfg config.Cfg, repoPath string, entries []TreeEnt require.False(tb, len(entry.OID) > 0 && len(entry.Content) > 0, "entry cannot have both OID and content") - require.False(tb, len(entry.OID) == 0 && len(entry.Content) == 0, - "entry must have either an OID or content") oid := entry.OID - if len(entry.Content) > 0 { + if oid == "" { oid = WriteBlob(tb, cfg, repoPath, []byte(entry.Content)) } diff --git a/internal/gitaly/linguist/linguist.go b/internal/gitaly/linguist/linguist.go index db2847b67..32c8b06e0 100644 --- a/internal/gitaly/linguist/linguist.go +++ b/internal/gitaly/linguist/linguist.go @@ -63,7 +63,7 @@ func (inst *Instance) Stats(ctx context.Context, commitID git.ObjectID) (ByteCou } defer cancel() - checkAttr, finishAttr, err := gitattributes.CheckAttr(ctx, inst.repo, linguistAttrs) + checkAttr, finishAttr, err := gitattributes.CheckAttr(ctx, inst.repo, commitID.Revision(), linguistAttrs) if err != nil { return nil, fmt.Errorf("linguist create check attr: %w", err) } diff --git a/internal/gitaly/linguist/linguist_test.go b/internal/gitaly/linguist/linguist_test.go index fe3d6c2b1..cd8df0dfd 100644 --- a/internal/gitaly/linguist/linguist_test.go +++ b/internal/gitaly/linguist/linguist_test.go @@ -523,15 +523,6 @@ func TestInstance_Stats(t *testing.T) { t.Run(tc.desc, func(t *testing.T) { repoProto, repoPath, objectID := tc.setup(t) - // Apply the gitattributes - // We should get rid of this with https://gitlab.com/groups/gitlab-org/-/epics/9006 - infoPath := filepath.Join(repoPath, "info") - require.NoError(t, os.MkdirAll(infoPath, perm.SharedDir)) - attrData, err := gittest.NewCommand(t, cfg, "-C", repoPath, "cat-file", "blob", objectID.String()+":.gitattributes").Output() - if err == nil { - require.NoError(t, os.WriteFile(filepath.Join(infoPath, "attributes"), attrData, perm.SharedFile)) - } - repo := localrepo.NewTestRepo(t, cfg, repoProto) linguist := New(cfg, catfileCache, repo) stats, err := linguist.Stats(ctx, objectID) |