Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions git-credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"path"
"strings"

"github.com/gopasspw/gopass/pkg/ctxutil"
Expand Down Expand Up @@ -170,12 +171,26 @@ func filter(ls []string, prefix string) []string {
}

func composePath(c *cli.Context, cred *gitCredentials) string {
store := c.String("store") + "/"
if store == "/" {
store = ""
var gopassPath string

if c.String("store") != "" {
gopassPath = path.Join(c.String("store"))
}

gopassPath = path.Join(gopassPath, "git", fsutil.CleanFilename(cred.Host))

// If path is supplied due to useHttpPath being set,
// assume the first part of the path is a user or organization.
if cred.Path != "" {
parts := strings.Split(cred.Path, string(os.PathSeparator))
if len(parts) > 0 {
gopassPath = path.Join(gopassPath, parts[0])
}
}

return store + "git/" + fsutil.CleanFilename(cred.Host) + "/" + fsutil.CleanFilename(cred.Username)
gopassPath = path.Join(gopassPath, fsutil.CleanFilename(cred.Username))

return gopassPath
}

// Get returns a credential to git.
Expand Down
96 changes: 96 additions & 0 deletions git-credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,102 @@ func TestGitCredentialHelperWithStoreFlag(t *testing.T) { //nolint:paralleltest
assert.Empty(t, stdout.String())
}

func Test_composePath(t *testing.T) {
t.Parallel()

tests := []struct {
name string
ctx *cli.Context
cred *gitCredentials
want string
}{
{
name: "basic path with host and username",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "github.com",
Username: "alice",
},
want: "git/github.com/alice",
},
{
name: "path with store flag",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{"store": "mystore"}),
cred: &gitCredentials{
Host: "gitlab.com",
Username: "bob",
},
want: "mystore/git/gitlab.com/bob",
},
{
name: "path with useHttpPath - single part",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "github.com",
Path: "myorg",
Username: "charlie",
},
want: "git/github.com/myorg/charlie",
},
{
name: "path with useHttpPath - multiple parts",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "github.com",
Path: "myorg/myrepo",
Username: "dave",
},
want: "git/github.com/myorg/dave",
},
{
name: "path with store and useHttpPath",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{"store": "work"}),
cred: &gitCredentials{
Host: "bitbucket.org",
Path: "company/project",
Username: "eve",
},
want: "work/git/bitbucket.org/company/eve",
},
{
name: "empty username",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "example.com",
Username: "",
},
want: "git/example.com",
},
{
name: "host with special characters gets cleaned",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "my:host.com",
Username: "user",
},
want: "git/my_host.com/user",
},
{
name: "username with special characters",
ctx: gptest.CliCtxWithFlags(t.Context(), t, map[string]string{}),
cred: &gitCredentials{
Host: "github.com",
Username: "user@email.com",
},
want: "git/github.com/user@email.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got := composePath(tt.ctx, tt.cred)
assert.Equal(t, tt.want, got)
})
}
}

func Test_getOptions(t *testing.T) {
t.Parallel()

Expand Down
Loading