mirror of
https://github.com/thegeeklab/wp-git-action.git
synced 2024-11-21 13:50: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.
|
||||
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{
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user