mirror of
https://github.com/thegeeklab/wp-git-action.git
synced 2024-11-22 00:00:39 +00:00
fix: use repo.Branch to init new repo (#110)
This commit is contained in:
parent
922b204862
commit
c2608f913f
@ -7,10 +7,13 @@ import (
|
|||||||
|
|
||||||
// Init creates a new Git repository in the specified directory.
|
// Init creates a new Git repository in the specified directory.
|
||||||
func Init(repo Repository) *types.Cmd {
|
func Init(repo Repository) *types.Cmd {
|
||||||
cmd := execabs.Command(
|
args := []string{
|
||||||
gitBin,
|
|
||||||
"init",
|
"init",
|
||||||
)
|
"-b",
|
||||||
|
repo.Branch,
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := execabs.Command(gitBin, args...)
|
||||||
cmd.Dir = repo.WorkDir
|
cmd.Dir = repo.WorkDir
|
||||||
|
|
||||||
return &types.Cmd{
|
return &types.Cmd{
|
||||||
|
@ -7,17 +7,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestInit(t *testing.T) {
|
func TestInit(t *testing.T) {
|
||||||
repo := Repository{
|
tests := []struct {
|
||||||
WorkDir: "/path/to/repo",
|
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)
|
for _, tt := range tests {
|
||||||
require.Equal(t, []string{gitBin, "init"}, cmd.Cmd.Args)
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
require.Equal(t, repo.WorkDir, cmd.Cmd.Dir)
|
cmd := Init(tt.repo)
|
||||||
|
require.Equal(t, tt.expected, cmd.Cmd.Args)
|
||||||
// Test with an empty work directory
|
require.Equal(t, tt.repo.WorkDir, cmd.Cmd.Dir)
|
||||||
repo.WorkDir = ""
|
})
|
||||||
cmd = Init(repo)
|
}
|
||||||
require.Equal(t, []string{gitBin, "init"}, cmd.Cmd.Args)
|
|
||||||
require.Empty(t, cmd.Cmd.Dir)
|
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,7 @@ func TestIsDirty(t *testing.T) {
|
|||||||
name: "dirty repo",
|
name: "dirty repo",
|
||||||
repo: Repository{
|
repo: Repository{
|
||||||
WorkDir: t.TempDir(),
|
WorkDir: t.TempDir(),
|
||||||
|
Branch: "main",
|
||||||
},
|
},
|
||||||
want: true,
|
want: true,
|
||||||
},
|
},
|
||||||
@ -61,6 +62,7 @@ func TestIsDirty(t *testing.T) {
|
|||||||
name: "clean repo",
|
name: "clean repo",
|
||||||
repo: Repository{
|
repo: Repository{
|
||||||
WorkDir: t.TempDir(),
|
WorkDir: t.TempDir(),
|
||||||
|
Branch: "main",
|
||||||
},
|
},
|
||||||
want: false,
|
want: false,
|
||||||
},
|
},
|
||||||
|
@ -46,14 +46,14 @@ func (p *Plugin) run(ctx context.Context) error {
|
|||||||
func (p *Plugin) Validate() error {
|
func (p *Plugin) Validate() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
p.Settings.Repo.Autocorrect = "never"
|
|
||||||
p.Settings.Repo.RemoteName = "origin"
|
|
||||||
p.Settings.Repo.Add = ""
|
|
||||||
|
|
||||||
if p.Settings.Repo.WorkDir == "" {
|
if p.Settings.Repo.WorkDir == "" {
|
||||||
p.Settings.Repo.WorkDir, err = os.Getwd()
|
p.Settings.Repo.WorkDir, err = os.Getwd()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.Settings.Repo.Autocorrect = "never"
|
||||||
|
p.Settings.Repo.RemoteName = "origin"
|
||||||
|
p.Settings.Repo.Add = ""
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get working directory: %w", err)
|
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.
|
// Execute provides the implementation of the plugin.
|
||||||
func (p *Plugin) Execute() error {
|
func (p *Plugin) Execute() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
homeDir := getUserHomeDir()
|
homeDir := getUserHomeDir()
|
||||||
batchCmd := make([]*types.Cmd, 0)
|
batchCmd := make([]*types.Cmd, 0)
|
||||||
gitEnv := []string{
|
gitEnv := []string{
|
||||||
@ -140,13 +142,11 @@ func (p *Plugin) Execute() error {
|
|||||||
return fmt.Errorf("failed to create working directory: %w", err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to check working directory: %w", err)
|
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"))
|
isDir, err := file.IsDir(filepath.Join(p.Settings.Repo.WorkDir, ".git"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to check working directory: %w", err)
|
return fmt.Errorf("failed to check working directory: %w", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user