Skip to content

Commit

Permalink
fix: allow pipes to be present in git username (argoproj#20040)
Browse files Browse the repository at this point in the history
* fix: [argoproj#15949] split git revision metadata by newline not pipe symbol

pipe symbol '|' could appear in username or commit message body

Signed-off-by: Petr Studeny <[email protected]>

* test: add test

Signed-off-by: Blake Pettersson <[email protected]>

* Update util/git/client_test.go

Co-authored-by: Petr Studeny <[email protected]>
Signed-off-by: Blake Pettersson <[email protected]>

* test: fix test

Signed-off-by: Blake Pettersson <[email protected]>

---------

Signed-off-by: Petr Studeny <[email protected]>
Signed-off-by: Blake Pettersson <[email protected]>
Co-authored-by: Petr Studeny <[email protected]>
  • Loading branch information
blakepettersson and dosmanak authored Oct 8, 2024
1 parent a288b4d commit 6002c7d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,11 @@ func (m *nativeGitClient) CommitSHA() (string, error) {

// returns the meta-data for the commit
func (m *nativeGitClient) RevisionMetadata(revision string) (*RevisionMetadata, error) {
out, err := m.runCmd("show", "-s", "--format=%an <%ae>|%at|%B", revision)
out, err := m.runCmd("show", "-s", "--format=%an <%ae>%n%at%n%B", revision)
if err != nil {
return nil, err
}
segments := strings.SplitN(out, "|", 3)
segments := strings.SplitN(out, "\n", 3)
if len(segments) != 3 {
return nil, fmt.Errorf("expected 3 segments, got %v", segments)
}
Expand Down
41 changes: 41 additions & 0 deletions util/git/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -438,3 +439,43 @@ func Test_IsRevisionPresent(t *testing.T) {
revisionPresent = client.IsRevisionPresent("invalid-revision")
assert.False(t, revisionPresent)
}

func Test_nativeGitClient_RevisionMetadata(t *testing.T) {
tempDir := t.TempDir()
client, err := NewClient(fmt.Sprintf("file://%s", tempDir), NopCreds{}, true, false, "", "")
require.NoError(t, err)

err = client.Init()
require.NoError(t, err)

p := path.Join(client.Root(), "README")
f, err := os.Create(p)
require.NoError(t, err)
_, err = f.WriteString("Hello.")
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)

err = runCmd(client.Root(), "git", "config", "user.name", "FooBar ||| something\nelse")
require.NoError(t, err)
err = runCmd(client.Root(), "git", "config", "user.email", "[email protected]")
require.NoError(t, err)

err = runCmd(client.Root(), "git", "add", "README")
require.NoError(t, err)
err = runCmd(client.Root(), "git", "commit", "--date=\"Sat Jun 5 20:00:00 2021 +0000 UTC\"", "-m", `| Initial commit |
(╯°□°)╯︵ ┻━┻
`, "-a")
require.NoError(t, err)

metadata, err := client.RevisionMetadata("HEAD")
require.NoError(t, err)
require.Equal(t, &RevisionMetadata{
Author: `FooBar ||| somethingelse <[email protected]>`,
Date: time.Date(2021, time.June, 5, 20, 0, 0, 0, time.UTC).Local(),
Tags: []string{},
Message: "| Initial commit |\n\n(╯°□°)╯︵ ┻━┻",
}, metadata)
}

0 comments on commit 6002c7d

Please sign in to comment.