Skip to content

Commit

Permalink
Merge pull request #73 from github/fix-cmdline
Browse files Browse the repository at this point in the history
Parse git.recieve.fsck --strict args configuration options
  • Loading branch information
RidhwaanDev committed May 13, 2024
2 parents 0c7ac7c + a7f1e35 commit 2348e81
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
10 changes: 10 additions & 0 deletions internal/config/git.go
Expand Up @@ -92,3 +92,13 @@ 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
}
24 changes: 24 additions & 0 deletions internal/config/git_test.go
Expand Up @@ -83,6 +83,30 @@ 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())
assert.NoError(t, cmd("git", "config","--add", "receive.fsck.badTagName", "error").Run())

config, _ := GetConfig(localRepo)
prefix := config.GetPrefix("receive.fsck.")

assert.Equal(t, prefix["missingemail"][0], "ignore")
assert.Equal(t, prefix["badtagname"][0], "ignore")
assert.Equal(t, prefix["badtagname"][1], "error")
}


func commandBuilderInDir(dir string) func(string, ...string) *exec.Cmd {
return func(program string, args ...string) *exec.Cmd {
Expand Down
33 changes: 33 additions & 0 deletions internal/integration/integration_test.go
Expand Up @@ -378,6 +378,39 @@ 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")
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")
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
15 changes: 14 additions & 1 deletion internal/spokes/spokes.go
Expand Up @@ -823,7 +823,20 @@ 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, values := range prefix {
for _, value := range values {
result += key + "=" + 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 2348e81

Please sign in to comment.