Skip to content

Commit

Permalink
Parse git.recieve.fsck --strict args configuration
Browse files Browse the repository at this point in the history
options
  • Loading branch information
RidhwaanDev committed Apr 18, 2024
1 parent 98a7ac8 commit b14747f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
16 changes: 16 additions & 0 deletions internal/config/git.go
Expand Up @@ -92,3 +92,19 @@ func (c *Config) GetAll(name string) []string {
}
return res
}
func (c *Config) GetPrefix(prefix string) map[string][]string {
var m = make(map[string][]string)
for _, entry := range c.Entries {
if strings.HasPrefix(entry.Key, prefix) {
trimmedKey:= strings.TrimPrefix(entry.Key, prefix)
m[trimmedKey] = append(m[trimmedKey], entry.Value)
}
}
return m
}

func (c *Config) PrintAll() {
for _, entry := range c.Entries {
fmt.Printf("key: %s , value: %s \n", entry.Key, entry.Value)
}
}
27 changes: 27 additions & 0 deletions internal/config/git_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"testing"
"strings"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -83,6 +84,32 @@ func TestGetConfigEntryMultipleValues(t *testing.T) {
fsckObjects := testGetConfigEntryValue(localRepo, "receive.multivalue")
assert.Equal(t, "c", fsckObjects)
}
func TestGetPrefixParsesArgs(t *testing.T) {
localRepo, err := os.MkdirTemp("", "repo")
defer os.RemoveAll(localRepo)

assert.NoError(t, err, fmt.Sprintf("unable to create the local Git repo: %s", err))

cmd := commandBuilderInDir(localRepo)

// init and config the local Git repo
assert.NoError(t, cmd("git", "init").Run())
assert.NoError(t, cmd("git", "config", "user.email", "spokes-receive-pack@github.com").Run())
assert.NoError(t, cmd("git", "config", "user.name", "spokes-receive-pack").Run())
assert.NoError(t, cmd("git", "config", "receive.fsck.missingEmail", "ignore").Run())
assert.NoError(t, cmd("git", "config", "receive.fsck.badTagName", "ignore").Run())

config, _ := GetConfig(localRepo)
prefix := config.GetPrefix("receive.fsck.")
var result string
for key, value := range prefix {
result += key + "=" + strings.Join(value, ",") + ","
}
result = strings.TrimSuffix(result, ",")
result = "strict=" + result
assert.Equal(t, "strict=missingemail=ignore,badtagname=ignore", result)
}


func commandBuilderInDir(dir string) func(string, ...string) *exec.Cmd {
return func(program string, args ...string) *exec.Cmd {
Expand Down
35 changes: 35 additions & 0 deletions internal/integration/integration_test.go
Expand Up @@ -378,6 +378,41 @@ func (suite *SpokesReceivePackTestSuite) TestSpokesReceivePackWrongObjectSucceed
})
}


func (suite *SpokesReceivePackTestSuite) TestSpokesReceivePackIgnoreArgsSucceed() {
assert.NoError(suite.T(), chdir(suite.T(), suite.remoteRepo), "unable to chdir into our remote Git repo")
// Disable the `receive.fsckObjects option
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsckObjects", "true").Run())
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsck.missingEmail", "ignore").Run())
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsck.badTagName", "ignore").Run())

assert.NoError(suite.T(), chdir(suite.T(), suite.localRepo), "unable to chdir into our local Git repo")

createBogusObjectAndPush(suite, func(suite *SpokesReceivePackTestSuite, err error, _ []byte) {
assert.NoError(
suite.T(),
err,
"unexpected error running the push with the custom spokes-receive-pack program; it should have succeeded since fsck args are ignored")
})
}

func (suite *SpokesReceivePackTestSuite) TestSpokesReceivePackMissingArgsFails() {
assert.NoError(suite.T(), chdir(suite.T(), suite.remoteRepo), "unable to chdir into our remote Git repo")
// Disable the `receive.fsckObjects option
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsckObjects", "true").Run())
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsck.missingEmail", "error").Run())
require.NoError(suite.T(), exec.Command("git", "config", "receive.fsck.badTagName", "error").Run())

assert.NoError(suite.T(), chdir(suite.T(), suite.localRepo), "unable to chdir into our local Git repo")

createBogusObjectAndPush(suite, func(suite *SpokesReceivePackTestSuite, err error, _ []byte) {
assert.Error(
suite.T(),
err,
"unexpected success running the push with the custom spokes-receive-pack program; it should have failed due to missing fsck args")
})
}

func (suite *SpokesReceivePackTestSuite) TestSpokesReceivePackPushFromShallowClone() {
var cmd *exec.Cmd

Expand Down
13 changes: 12 additions & 1 deletion internal/spokes/spokes.go
Expand Up @@ -809,7 +809,18 @@ func (r *spokesReceivePack) readPack(ctx context.Context, commands []command, ca
args = append(args, "--fix-thin")

if r.isFsckConfigEnabled() {
args = append(args, "--strict")
prefix := r.config.GetPrefix("receive.fsck.")
if len(prefix) > 0 {
var result string
for key, value := range prefix {
result += key + "=" + strings.Join(value, ",") + ","
}
result = strings.TrimSuffix(result, ",")
result = "--strict=" + result
args = append(args, result)
} else {
args = append(args, "--strict")
}
}

maxSize, err := r.getMaxInputSize()
Expand Down

0 comments on commit b14747f

Please sign in to comment.