diff options
author | John Cai <jcai@gitlab.com> | 2023-08-16 21:38:38 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2023-09-21 23:57:23 +0300 |
commit | 0fce1682378bb898dbe0f87eedf1bd55ef96d42b (patch) | |
tree | de6f7aa2006807dd3ba71bbe792cc35d3e5a61d3 | |
parent | d4e0dd367f4fa3c32b28e0afe37bb26f86731006 (diff) |
git: Add attr-source behind feature flagjc/set-attr
-rw-r--r-- | internal/featureflag/ff_attr_source.go | 9 | ||||
-rw-r--r-- | internal/git/command_factory.go | 32 | ||||
-rw-r--r-- | internal/git/command_factory_test.go | 49 | ||||
-rw-r--r-- | internal/testhelper/testhelper.go | 3 |
4 files changed, 92 insertions, 1 deletions
diff --git a/internal/featureflag/ff_attr_source.go b/internal/featureflag/ff_attr_source.go new file mode 100644 index 000000000..7df6d4f1d --- /dev/null +++ b/internal/featureflag/ff_attr_source.go @@ -0,0 +1,9 @@ +package featureflag + +// AttrSource will set --attr-source=HEAD for every Git command +var AttrSource = NewFeatureFlag( + "attr_source", + "v16.3.0", + "https://gitlab.com/gitlab-org/gitaly/-/issues/5500", + false, +) diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go index d92fc7c78..7c1d5f1d1 100644 --- a/internal/git/command_factory.go +++ b/internal/git/command_factory.go @@ -13,6 +13,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "gitlab.com/gitlab-org/gitaly/v16/internal/cgroups" "gitlab.com/gitlab-org/gitaly/v16/internal/command" + "gitlab.com/gitlab-org/gitaly/v16/internal/featureflag" "gitlab.com/gitlab-org/gitaly/v16/internal/git/alternates" "gitlab.com/gitlab-org/gitaly/v16/internal/git/trace2" "gitlab.com/gitlab-org/gitaly/v16/internal/git/trace2hooks" @@ -439,7 +440,6 @@ func (cf *ExecCommandFactory) newCommand(ctx context.Context, repo storage.Repos if err != nil { return nil, err } - env = append(alternates.Env(repoPath, repo.GetGitObjectDirectory(), repo.GetGitAlternateObjectDirectories()), env...) } @@ -488,6 +488,36 @@ func (cf *ExecCommandFactory) newCommand(ctx context.Context, repo storage.Repos command.WithCgroup(cf.cgroupsManager, cgroupsAddCommandOpts...), command.WithCommandGitVersion(cmdGitVersion.String()), ) + + if featureflag.AttrSource.IsEnabled(ctx) && repoPath != "" { + checkHeadCmdArgs, err := cf.combineArgs( + ctx, + Command{ + Name: "rev-parse", + Args: []string{"HEAD^{commit}"}, + }, + config, + ) + if err != nil { + return nil, err + } + + checkHeadCmdArgs = append([]string{"--git-dir", repoPath}, checkHeadCmdArgs...) + + checkHeadCmd, err := command.New( + ctx, + append([]string{execEnv.BinaryPath}, checkHeadCmdArgs...), + command.WithEnvironment(execEnv.EnvironmentVariables), + ) + if err != nil { + return nil, err + } + + if err := checkHeadCmd.Wait(); err == nil { + args = append([]string{"--attr-source=HEAD"}, args...) + } + } + command, err := command.New(ctx, append([]string{execEnv.BinaryPath}, args...), commandOpts...) if err != nil { return nil, err diff --git a/internal/git/command_factory_test.go b/internal/git/command_factory_test.go index f1dfca0b4..44582ae61 100644 --- a/internal/git/command_factory_test.go +++ b/internal/git/command_factory_test.go @@ -16,6 +16,7 @@ import ( "testing" "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/git/trace2" @@ -1074,6 +1075,54 @@ func TestDefaultTrace2HooksFor(t *testing.T) { } } +func TestExecCommandFactory_globalOptions(t *testing.T) { + t.Parallel() + + testhelper.NewFeatureSets( + featureflag.AttrSource, + ).Run(t, testExecCommandFactoryGlobalOptions) +} + +func testExecCommandFactoryGlobalOptions(t *testing.T, ctx context.Context) { + t.Parallel() + + cfg := testcfg.Build(t) + + repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{ + SkipCreationViaService: true, + }) + + gittest.WriteCommit(t, cfg, repoPath, + gittest.WithTreeEntries( + gittest.TreeEntry{ + Mode: "100644", + Path: ".gitattributes", + Content: "pattern1 attr1=value1\n", + }, + ), gittest.WithBranch("HEAD")) + + commandFactory, cleanup, err := git.NewExecCommandFactory(cfg, testhelper.SharedLogger(t)) + require.NoError(t, err) + defer cleanup() + + var stdout, stderr bytes.Buffer + cmd, err := commandFactory.New(ctx, repo, git.Command{ + Name: "check-attr", + Flags: []git.Option{ + git.Flag{Name: "-a"}, + }, + Args: []string{"pattern1"}, + }, git.WithStdout(&stdout), git.WithStderr(&stderr)) + require.NoError(t, err) + require.NoError(t, cmd.Wait()) + + if featureflag.AttrSource.IsEnabled(ctx) { + require.Equal(t, "pattern1: attr1: value1", text.ChompBytes(stdout.Bytes())) + } else { + require.Equal(t, "", text.ChompBytes(stdout.Bytes())) + } +} + func performPackObjectGit(t *testing.T, ctx context.Context, opts ...git.ExecCommandFactoryOption) { cfg := testcfg.Build(t) repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index 60ea38545..155436e1c 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -258,6 +258,9 @@ func ContextWithoutCancel(opts ...ContextOpt) context.Context { // This feature flag sits in `command.Wait()` and is thus getting executed in a ton of tests. ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.CommandCloseStdout, rand.Int()%2 == 0) + // Randomly enable attr source + ctx = featureflag.ContextWithFeatureFlag(ctx, featureflag.AttrSource, rand.Int()%2 == 0) + for _, opt := range opts { ctx = opt(ctx) } |