diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-26 15:43:01 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-29 15:57:49 +0300 |
commit | 9411cc748c5a48b6711ab99ea35dd8c34da4ce44 (patch) | |
tree | 04a327fce426a7ba2393cea58abac985b3715e23 | |
parent | 6d9a2edad295dcb694c065e4b12b970462ce0911 (diff) |
git: Rename `gitCommand` to `commandDescription`
The `gitCommands` array hosts descriptions about all the git commands
we're running in Gitaly's codebase. Its naming is somewhat awkward
though because it clashes with the `Cmd` interface and its `SubCmd`
respectively `SubSubCmd` implementations. By only taking a look at their
names, it's not obvious what the scope of each of these is and how they
interact with each other.
Rename the `gitCommands` array and its `gitCommand` structure to
`commandDescriptions` respectively `commandDescription`. Because this is
what it is: it describes git commands, how we need to invoke them and
how they behave.
-rw-r--r-- | internal/git/command.go | 12 | ||||
-rw-r--r-- | internal/git/command_description.go (renamed from internal/git/subcommand.go) | 110 | ||||
-rw-r--r-- | internal/git/command_description_test.go | 17 | ||||
-rw-r--r-- | internal/git/command_factory.go | 10 | ||||
-rw-r--r-- | internal/git/subcommand_test.go | 17 |
5 files changed, 83 insertions, 83 deletions
diff --git a/internal/git/command.go b/internal/git/command.go index a487836e9..80f07c257 100644 --- a/internal/git/command.go +++ b/internal/git/command.go @@ -54,13 +54,13 @@ func (sc SubCmd) Subcommand() string { return sc.Name } func (sc SubCmd) CommandArgs() ([]string, error) { var safeArgs []string - gitCommand, ok := gitCommands[sc.Name] + commandDescription, ok := commandDescriptions[sc.Name] if !ok { return nil, fmt.Errorf("invalid sub command name %q: %w", sc.Name, ErrInvalidArg) } safeArgs = append(safeArgs, sc.Name) - commandArgs, err := assembleCommandArgs(gitCommand, sc.Flags, sc.Args, sc.PostSepArgs) + commandArgs, err := assembleCommandArgs(commandDescription, sc.Flags, sc.Args, sc.PostSepArgs) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func (sc SubCmd) CommandArgs() ([]string, error) { return safeArgs, nil } -func assembleCommandArgs(gitCommand gitCommand, flags []Option, args []string, postSepArgs []string) ([]string, error) { +func assembleCommandArgs(commandDescription commandDescription, flags []Option, args []string, postSepArgs []string) ([]string, error) { var commandArgs []string for _, o := range flags { @@ -87,7 +87,7 @@ func assembleCommandArgs(gitCommand gitCommand, flags []Option, args []string, p commandArgs = append(commandArgs, a) } - if gitCommand.supportsEndOfOptions() { + if commandDescription.supportsEndOfOptions() { commandArgs = append(commandArgs, "--end-of-options") } @@ -128,7 +128,7 @@ var actionRegex = regexp.MustCompile(`^[[:alnum:]]+[-[:alnum:]]*$`) func (sc SubSubCmd) CommandArgs() ([]string, error) { var safeArgs []string - gitCommand, ok := gitCommands[sc.Name] + commandDescription, ok := commandDescriptions[sc.Name] if !ok { return nil, fmt.Errorf("invalid sub command name %q: %w", sc.Name, ErrInvalidArg) } @@ -139,7 +139,7 @@ func (sc SubSubCmd) CommandArgs() ([]string, error) { } safeArgs = append(safeArgs, sc.Action) - commandArgs, err := assembleCommandArgs(gitCommand, sc.Flags, sc.Args, sc.PostSepArgs) + commandArgs, err := assembleCommandArgs(commandDescription, sc.Flags, sc.Args, sc.PostSepArgs) if err != nil { return nil, err } diff --git a/internal/git/subcommand.go b/internal/git/command_description.go index d68e1a988..0601141b7 100644 --- a/internal/git/subcommand.go +++ b/internal/git/command_description.go @@ -9,111 +9,111 @@ const ( scGeneratesPackfiles ) -type gitCommand struct { +type commandDescription struct { flags uint opts []GlobalOption } -// gitCommands is a curated list of Git command names for special git.ExecCommandFactory -// validation logic -var gitCommands = map[string]gitCommand{ - "apply": gitCommand{ +// commandDescriptions is a curated list of Git command descriptions for special +// git.ExecCommandFactory validation logic +var commandDescriptions = map[string]commandDescription{ + "apply": { flags: scNoRefUpdates, }, - "archive": gitCommand{ + "archive": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "blame": gitCommand{ + "blame": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "bundle": gitCommand{ + "bundle": { flags: scNoRefUpdates | scGeneratesPackfiles, }, - "cat-file": gitCommand{ + "cat-file": { flags: scNoRefUpdates, }, - "check-ref-format": gitCommand{ + "check-ref-format": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "checkout": gitCommand{ + "checkout": { flags: scNoEndOfOptions, }, - "clone": gitCommand{ + "clone": { flags: scNoEndOfOptions | scGeneratesPackfiles, }, - "commit": gitCommand{ + "commit": { flags: 0, }, - "commit-graph": gitCommand{ + "commit-graph": { flags: scNoRefUpdates, }, - "config": gitCommand{ + "config": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "count-objects": gitCommand{ + "count-objects": { flags: scNoRefUpdates, }, - "diff": gitCommand{ + "diff": { flags: scNoRefUpdates, }, - "diff-tree": gitCommand{ + "diff-tree": { flags: scNoRefUpdates, }, - "fetch": gitCommand{ + "fetch": { flags: 0, }, - "for-each-ref": gitCommand{ + "for-each-ref": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "format-patch": gitCommand{ + "format-patch": { flags: scNoRefUpdates, }, - "fsck": gitCommand{ + "fsck": { flags: scNoRefUpdates, }, - "gc": gitCommand{ + "gc": { flags: scNoRefUpdates | scGeneratesPackfiles, }, - "grep": gitCommand{ + "grep": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "hash-object": gitCommand{ + "hash-object": { flags: scNoRefUpdates, }, - "init": gitCommand{ + "init": { flags: scNoRefUpdates, }, - "linguist": gitCommand{ + "linguist": { flags: scNoEndOfOptions, }, - "log": gitCommand{ + "log": { flags: scNoRefUpdates, }, - "ls-remote": gitCommand{ + "ls-remote": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "ls-tree": gitCommand{ + "ls-tree": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "merge-base": gitCommand{ + "merge-base": { flags: scNoRefUpdates, }, - "mktag": gitCommand{ + "mktag": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "multi-pack-index": gitCommand{ + "multi-pack-index": { flags: scNoRefUpdates, }, - "pack-refs": gitCommand{ + "pack-refs": { flags: scNoRefUpdates, }, - "pack-objects": gitCommand{ + "pack-objects": { flags: scNoRefUpdates | scGeneratesPackfiles, }, - "push": gitCommand{ + "push": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "receive-pack": gitCommand{ + "receive-pack": { flags: 0, opts: []GlobalOption{ // In case the repository belongs to an object pool, we want to prevent @@ -136,10 +136,10 @@ var gitCommands = map[string]gitCommand{ ConfigPair{Key: "receive.advertisePushOptions", Value: "true"}, }, }, - "remote": gitCommand{ + "remote": { flags: scNoEndOfOptions, }, - "repack": gitCommand{ + "repack": { flags: scNoRefUpdates | scGeneratesPackfiles, opts: []GlobalOption{ // Write bitmap indices when packing objects, which @@ -147,31 +147,31 @@ var gitCommands = map[string]gitCommand{ ConfigPair{Key: "repack.writeBitmaps", Value: "true"}, }, }, - "rev-list": gitCommand{ + "rev-list": { flags: scNoRefUpdates, }, - "rev-parse": gitCommand{ + "rev-parse": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "show": gitCommand{ + "show": { flags: scNoRefUpdates, }, - "show-ref": gitCommand{ + "show-ref": { flags: scNoRefUpdates, }, - "symbolic-ref": gitCommand{ + "symbolic-ref": { flags: 0, }, - "tag": gitCommand{ + "tag": { flags: 0, }, - "update-ref": gitCommand{ + "update-ref": { flags: 0, }, - "upload-archive": gitCommand{ + "upload-archive": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "upload-pack": gitCommand{ + "upload-pack": { flags: scNoRefUpdates | scGeneratesPackfiles, opts: []GlobalOption{ ConfigPair{Key: "uploadpack.allowFilter", Value: "true"}, @@ -180,31 +180,31 @@ var gitCommands = map[string]gitCommand{ ConfigPair{Key: "uploadpack.allowAnySHA1InWant", Value: "true"}, }, }, - "version": gitCommand{ + "version": { flags: scNoRefUpdates | scNoEndOfOptions, }, - "worktree": gitCommand{ + "worktree": { flags: 0, }, } -// mayUpdateRef indicates if a gitCommand is known to update references. +// mayUpdateRef indicates if a command is known to update references. // This is useful to determine if a command requires reference hook // configuration. A non-exhaustive list of commands is consulted to determine if // refs are updated. When unknown, true is returned to err on the side of // caution. -func (c gitCommand) mayUpdateRef() bool { +func (c commandDescription) mayUpdateRef() bool { return c.flags&scNoRefUpdates == 0 } -// mayGeneratePackfiles indicates if a gitCommand is known to generate +// mayGeneratePackfiles indicates if a command is known to generate // packfiles. This is used in order to inject packfile configuration. -func (c gitCommand) mayGeneratePackfiles() bool { +func (c commandDescription) mayGeneratePackfiles() bool { return c.flags&scGeneratesPackfiles != 0 } // supportsEndOfOptions indicates whether a command can handle the // `--end-of-options` option. -func (c gitCommand) supportsEndOfOptions() bool { +func (c commandDescription) supportsEndOfOptions() bool { return c.flags&scNoEndOfOptions == 0 } diff --git a/internal/git/command_description_test.go b/internal/git/command_description_test.go new file mode 100644 index 000000000..eae4820e4 --- /dev/null +++ b/internal/git/command_description_test.go @@ -0,0 +1,17 @@ +package git + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCommandDescriptions_mayGeneratePackfiles(t *testing.T) { + gcDescription, ok := commandDescriptions["gc"] + require.True(t, ok) + require.True(t, gcDescription.mayGeneratePackfiles()) + + applyDescription, ok := commandDescriptions["apply"] + require.True(t, ok) + require.False(t, applyDescription.mayGeneratePackfiles()) +} diff --git a/internal/git/command_factory.go b/internal/git/command_factory.go index f27308788..5497053ab 100644 --- a/internal/git/command_factory.go +++ b/internal/git/command_factory.go @@ -129,7 +129,7 @@ func (cf *ExecCommandFactory) newCommand(ctx context.Context, repo repository.Gi } func handleOpts(ctx context.Context, sc Cmd, cc *cmdCfg, opts []CmdOpt) error { - gitCommand, ok := gitCommands[sc.Subcommand()] + commandDescription, ok := commandDescriptions[sc.Subcommand()] if !ok { return fmt.Errorf("invalid sub command name %q: %w", sc.Subcommand(), ErrInvalidArg) } @@ -140,10 +140,10 @@ func handleOpts(ctx context.Context, sc Cmd, cc *cmdCfg, opts []CmdOpt) error { } } - if !cc.hooksConfigured && gitCommand.mayUpdateRef() { + if !cc.hooksConfigured && commandDescription.mayUpdateRef() { return fmt.Errorf("subcommand %q: %w", sc.Subcommand(), ErrHookPayloadRequired) } - if gitCommand.mayGeneratePackfiles() { + if commandDescription.mayGeneratePackfiles() { cc.globals = append(cc.globals, ConfigPair{ Key: "pack.windowMemory", Value: "100m", }) @@ -161,7 +161,7 @@ func combineArgs(gitConfig []config.GitConfig, sc Cmd, cc *cmdCfg) (_ []string, } }() - gitCommand, ok := gitCommands[sc.Subcommand()] + commandDescription, ok := commandDescriptions[sc.Subcommand()] if !ok { return nil, fmt.Errorf("invalid sub command name %q: %w", sc.Subcommand(), ErrInvalidArg) } @@ -185,7 +185,7 @@ func combineArgs(gitConfig []config.GitConfig, sc Cmd, cc *cmdCfg) (_ []string, }) } combinedGlobals = append(combinedGlobals, globalOptions...) - combinedGlobals = append(combinedGlobals, gitCommand.opts...) + combinedGlobals = append(combinedGlobals, commandDescription.opts...) combinedGlobals = append(combinedGlobals, cc.globals...) for _, global := range combinedGlobals { diff --git a/internal/git/subcommand_test.go b/internal/git/subcommand_test.go deleted file mode 100644 index e874e817b..000000000 --- a/internal/git/subcommand_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package git - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestSubcommand_mayGeneratePackfiles(t *testing.T) { - gcCmd, ok := gitCommands["gc"] - require.True(t, ok) - require.True(t, gcCmd.mayGeneratePackfiles()) - - applyCmd, ok := gitCommands["apply"] - require.True(t, ok) - require.False(t, applyCmd.mayGeneratePackfiles()) -} |