0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-06-02 18:29:41 +02:00

fix: use repo.Branch to init new repo

This commit is contained in:
Robert Kaussow 2024-05-05 22:24:31 +02:00
parent 179014e52e
commit fbd0ab69ae
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
4 changed files with 38 additions and 21 deletions

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

@ -46,14 +46,20 @@ 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 = ""
// This default cannot be set in the cli flag, as the CI_* environment variables
// can be set empty, resulting in an empty default value.
if p.Settings.Repo.Branch == "" {
p.Settings.Repo.Branch = "main"
}
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 +108,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 +148,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)

View File

@ -141,7 +141,6 @@ func Flags(settings *Settings, category string) []cli.Flag {
Usage: "name of the git source branch",
EnvVars: []string{"PLUGIN_BRANCH"},
Destination: &settings.Repo.Branch,
Value: "main",
Category: category,
},
&cli.StringFlag{