diff options
author | Karthik Nayak <knayak@gitlab.com> | 2023-08-28 13:30:59 +0300 |
---|---|---|
committer | Karthik Nayak <knayak@gitlab.com> | 2023-09-08 17:17:55 +0300 |
commit | 00dcb3fd854129d6aedfd47ffce5645cd2dbf40c (patch) | |
tree | f40934214836681064e7bfc06f12223ffa7d9208 | |
parent | 704f12ccd332c086dc06c5bd4658e7fe7bc6a70c (diff) |
catfile: Add mailmap support to new ObjectReader
In 0b032626, we have introduced mailmap support in our catfile package.
This change seems to have crossed with 950ca530, where we have
introduced a new catfile process type that uses `--batch-command` in
order to combine the `ObjectContentReader` and the `ObjectInfoReaders`
into a single process via `ObjectReader`. The new object reader does not
support mailmaps.
Add mailmap support for the new object reader, similar to the others.
-rw-r--r-- | internal/git/catfile/object_reader.go | 19 | ||||
-rw-r--r-- | internal/git/catfile/object_reader_test.go | 44 |
2 files changed, 56 insertions, 7 deletions
diff --git a/internal/git/catfile/object_reader.go b/internal/git/catfile/object_reader.go index d431b0576..d217ebae0 100644 --- a/internal/git/catfile/object_reader.go +++ b/internal/git/catfile/object_reader.go @@ -8,6 +8,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "gitlab.com/gitlab-org/gitaly/v16/internal/command" + "gitlab.com/gitlab-org/gitaly/v16/internal/featureflag" "gitlab.com/gitlab-org/gitaly/v16/internal/git" ) @@ -67,14 +68,20 @@ func newObjectReader( repo git.RepositoryExecutor, counter *prometheus.CounterVec, ) (*objectReader, error) { + flags := []git.Option{ + git.Flag{Name: "-Z"}, + git.Flag{Name: "--batch-command"}, + git.Flag{Name: "--buffer"}, + } + + if featureflag.MailmapOptions.IsEnabled(ctx) { + flags = append([]git.Option{git.Flag{Name: "--use-mailmap"}}, flags...) + } + batchCmd, err := repo.Exec(ctx, git.Command{ - Name: "cat-file", - Flags: []git.Option{ - git.Flag{Name: "-Z"}, - git.Flag{Name: "--batch-command"}, - git.Flag{Name: "--buffer"}, - }, + Name: "cat-file", + Flags: flags, }, git.WithSetupStdin(), ) diff --git a/internal/git/catfile/object_reader_test.go b/internal/git/catfile/object_reader_test.go index e171bff31..e86f9f0fe 100644 --- a/internal/git/catfile/object_reader_test.go +++ b/internal/git/catfile/object_reader_test.go @@ -1,6 +1,7 @@ package catfile import ( + "context" "errors" "fmt" "io" @@ -10,6 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v16/internal/featureflag" "gitlab.com/gitlab-org/gitaly/v16/internal/git" "gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v16/internal/helper/text" @@ -165,7 +167,10 @@ func TestObjectReader_reader(t *testing.T) { func TestObjectReader_object(t *testing.T) { t.Parallel() - ctx := testhelper.Context(t) + testhelper.NewFeatureSets(featureflag.MailmapOptions).Run(t, testObjectReaderObject) +} + +func testObjectReaderObject(t *testing.T, ctx context.Context) { cfg := testcfg.Build(t) repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{ SkipCreationViaService: true, @@ -237,6 +242,43 @@ func TestObjectReader_object(t *testing.T) { require.NoError(t, err) } }) + + t.Run("read existing object with mailmap", func(t *testing.T) { + mailmapContents := "A U Thor <author@example.com> Scrooge McDuck <scrooge@mcduck.com>" + + commitID := gittest.WriteCommit(t, cfg, repoPath, + gittest.WithTreeEntries( + gittest.TreeEntry{Path: ".mailmap", Mode: "100644", Content: mailmapContents}, + ), + gittest.WithBranch("main"), + ) + + var commitContents []byte + + if featureflag.MailmapOptions.IsEnabled(ctx) { + commitContents = gittest.Exec(t, cfg, "-C", repoPath, "cat-file", + "--use-mailmap", "-p", commitID.String()) + } else { + commitContents = gittest.Exec(t, cfg, "-C", repoPath, "cat-file", + "-p", commitID.String()) + } + + reader, err := newObjectContentReader(ctx, newRepoExecutor(t, cfg, repoProto), nil) + require.NoError(t, err) + + object, err := reader.Object(ctx, "refs/heads/main") + require.NoError(t, err) + + data, err := io.ReadAll(object) + require.NoError(t, err) + require.Equal(t, commitContents, data) + + if featureflag.MailmapOptions.IsEnabled(ctx) { + require.Contains(t, string(data), "A U Thor <author@example.com>") + } else { + require.Contains(t, string(data), "Scrooge McDuck <scrooge@mcduck.com>") + } + }) } func TestObjectReader_queue(t *testing.T) { |