diff --git a/internal/commands/project/create/project_create.go b/internal/commands/project/create/project_create.go index ba301fabccebc83b728d253c23f9d5830c082425..b2d11d691e8ea85dcad0590303da089f5c04f354 100644 --- a/internal/commands/project/create/project_create.go +++ b/internal/commands/project/create/project_create.go @@ -109,16 +109,24 @@ func runCreateProject(cmd *cobra.Command, args []string, f cmdutils.Factory) err return err } skipGitInit, _ := cmd.Flags().GetBool("skipGitInit") - if !skipGitInit && f.IO().PromptEnabled() { - doInit := true - err := f.IO().Confirm(cmd.Context(), &doInit, "Directory not Git initialized. Run `git init`?") - if err != nil || !doInit { - return err - } - err = initGit(defaultBranch) - if err != nil { - return err + // Check if directory is already git initialized + gitDir := path.Join(config.GitDir(false)...) + stat, statErr := os.Stat(gitDir) + isGitInitialized := statErr == nil && stat.Mode().IsDir() + + // Early validation: check if we need to init git and if we can + var needsGitInit bool + if !skipGitInit && !isGitInitialized { + if f.IO().PromptEnabled() { + doInit := true + err := f.IO().Confirm(cmd.Context(), &doInit, "Directory not Git initialized. Run `git init`?") + if err != nil { + return err + } + needsGitInit = doInit + } else { + needsGitInit = true } } @@ -139,12 +147,25 @@ func runCreateProject(cmd *cobra.Command, args []string, f cmdutils.Factory) err if user.Username == namespace { namespace = "" } + // When a project name is provided as argument, we won't init git in current directory + // Instead, we'll create a subdirectory and init there (or just add remote if already in git repo) + needsGitInit = false } else { - projectPath, err = git.ToplevelDir() - if err != nil { - return err + // If we're in a git repository, use the repo name + // Otherwise, use the current directory name + if isGitInitialized { + projectPath, err = git.ToplevelDir() + if err != nil { + return err + } + projectPath = path.Base(projectPath) + } else { + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("cannot get current directory: %v", err) + } + projectPath = path.Base(cwd) } - projectPath = path.Base(projectPath) isPath = true c, err := f.ApiClient(f.DefaultHostname()) @@ -215,6 +236,18 @@ func runCreateProject(cmd *cobra.Command, args []string, f cmdutils.Factory) err if err == nil { fmt.Fprintf(f.IO().StdOut, "%s Created repository %s on GitLab: %s\n", greenCheck, project.NameWithNamespace, project.WebURL) + + // Execute git init if needed (we already validated it will work) + if needsGitInit { + err = initGit("") + if err != nil { + // Project exists on GitLab but git init failed + fmt.Fprintf(f.IO().StdErr, "Warning: Project created on GitLab but git init failed: %v\n", err) + fmt.Fprintf(f.IO().StdErr, "You can manually initialize the repository with: git init\n") + // Don't return error since project was created successfully + } + } + if isPath { cfg := f.Config() webURL, _ := url.Parse(project.WebURL) @@ -227,6 +260,16 @@ func runCreateProject(cmd *cobra.Command, args []string, f cmdutils.Factory) err } fmt.Fprintf(f.IO().StdOut, "%s Added remote %s\n", greenCheck, remote) + // Create default branch after remote is added (if specified) + if needsGitInit && defaultBranch != "" { + gitBranch := git.GitCommand("checkout", "-b", defaultBranch) + gitBranch.Stdout = os.Stdout + gitBranch.Stdin = os.Stdin + if err := run.PrepareCmd(gitBranch).Run(); err != nil { + fmt.Fprintf(f.IO().StdErr, "Warning: Failed to create branch %s: %v\n", defaultBranch, err) + } + } + } else if f.IO().PromptEnabled() { doSetup := true err := f.IO().Confirm(cmd.Context(), &doSetup, fmt.Sprintf("Create a local project directory for %s?", project.NameWithNamespace)) diff --git a/internal/commands/project/create/project_create_test.go b/internal/commands/project/create/project_create_test.go index 56f684326cae4cca9f6e71edfe614ed709835092..959e02cea6d8c271ca23f6d629d3ddf81da7c48d 100644 --- a/internal/commands/project/create/project_create_test.go +++ b/internal/commands/project/create/project_create_test.go @@ -63,6 +63,11 @@ func Test_projectCreateCmd(t *testing.T) { Args: []string{"reponame/"}, ExpectedMsg: []string{"✓ Created repository reponame on GitLab: \n"}, }, + { + Name: "Create project with --skipGitInit flag", + Args: []string{"test-repo", "--skipGitInit"}, + ExpectedMsg: []string{"✓ Created repository test-repo on GitLab: \n"}, + }, } cmd := NewCmdCreate(f)