Compare commits

...

2 Commits

5 changed files with 36 additions and 22 deletions

View File

@ -1,7 +1,7 @@
# renovate: datasource=github-releases depName=mvdan/gofumpt
GOFUMPT_PACKAGE_VERSION := v0.6.0
# renovate: datasource=github-releases depName=golangci/golangci-lint
GOLANGCI_LINT_PACKAGE_VERSION := v1.57.2
GOLANGCI_LINT_PACKAGE_VERSION := v1.58.0
EXECUTABLE := wp-git-action

View File

@ -7,10 +7,13 @@ import (
// Init creates a new Git repository in the specified directory.
func Init(repo Repository) *types.Cmd {
cmd := execabs.Command(
gitBin,
args := []string{
"init",
)
"-b",
repo.Branch,
}
cmd := execabs.Command(gitBin, args...)
cmd.Dir = repo.WorkDir
return &types.Cmd{

View File

@ -7,17 +7,26 @@ import (
)
func TestInit(t *testing.T) {
repo := Repository{
WorkDir: "/path/to/repo",
tests := []struct {
name string
repo Repository
expected []string
}{
{
name: "init repo",
repo: Repository{
WorkDir: "/path/to/repo",
Branch: "main",
},
expected: []string{gitBin, "init", "-b", "main"},
},
}
cmd := Init(repo)
require.Equal(t, []string{gitBin, "init"}, cmd.Cmd.Args)
require.Equal(t, repo.WorkDir, cmd.Cmd.Dir)
// Test with an empty work directory
repo.WorkDir = ""
cmd = Init(repo)
require.Equal(t, []string{gitBin, "init"}, cmd.Cmd.Args)
require.Empty(t, cmd.Cmd.Dir)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := Init(tt.repo)
require.Equal(t, tt.expected, cmd.Cmd.Args)
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
})
}
}

View File

@ -54,6 +54,7 @@ func TestIsDirty(t *testing.T) {
name: "dirty repo",
repo: Repository{
WorkDir: t.TempDir(),
Branch: "main",
},
want: true,
},
@ -61,6 +62,7 @@ func TestIsDirty(t *testing.T) {
name: "clean repo",
repo: Repository{
WorkDir: t.TempDir(),
Branch: "main",
},
want: false,
},

View File

@ -46,14 +46,14 @@ func (p *Plugin) run(ctx context.Context) error {
func (p *Plugin) Validate() error {
var err error
p.Settings.Repo.Autocorrect = "never"
p.Settings.Repo.RemoteName = "origin"
p.Settings.Repo.Add = ""
if p.Settings.Repo.WorkDir == "" {
p.Settings.Repo.WorkDir, err = os.Getwd()
}
p.Settings.Repo.Autocorrect = "never"
p.Settings.Repo.RemoteName = "origin"
p.Settings.Repo.Add = ""
if err != nil {
return fmt.Errorf("failed to get working directory: %w", err)
}
@ -102,6 +102,8 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
var err error
homeDir := getUserHomeDir()
batchCmd := make([]*types.Cmd, 0)
gitEnv := []string{
@ -140,13 +142,11 @@ func (p *Plugin) Execute() error {
return fmt.Errorf("failed to create working directory: %w", err)
}
isEmpty, err := file.IsDirEmpty(p.Settings.Repo.WorkDir)
p.Settings.Repo.IsEmpty, err = file.IsDirEmpty(p.Settings.Repo.WorkDir)
if err != nil {
return fmt.Errorf("failed to check working directory: %w", err)
}
p.Settings.Repo.IsEmpty = isEmpty
isDir, err := file.IsDir(filepath.Join(p.Settings.Repo.WorkDir, ".git"))
if err != nil {
return fmt.Errorf("failed to check working directory: %w", err)