@@ -21,13 +21,16 @@ var initCmd = &cobra.Command{
2121 Use : "init" ,
2222 Short : "Initialize a git repository and create a GitHub remote" ,
2323 Long : `Initialize the current directory as a git repository:
24- 1. Run 'git init'
24+ 1. Run 'git init' (skipped if already a git repo with GitHub remote)
25252. Configure git user (name/email)
26263. Add child GitHub repos as git submodules
27274. Generate a .gitignore with common patterns
28285. Create an initial commit
29296. Create a GitHub remote repository via 'gh repo create --push'
3030
31+ If the current directory is already a git repository with a GitHub remote,
32+ steps 1, 4, 5, and 6 are skipped — only submodules are added and committed.
33+
3134The repo name defaults to the current directory name.
3235Owner defaults to the 'github-owner' config value in ~/.spark.yaml.` ,
3336 RunE : func (cmd * cobra.Command , args []string ) error {
@@ -37,9 +40,6 @@ Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
3740 if owner == "" {
3841 owner = viper .GetString ("github-owner" )
3942 }
40- if owner == "" {
41- return fmt .Errorf ("GitHub owner is required. Set --owner flag or 'github-owner' in ~/.spark.yaml" )
42- }
4343
4444 repoName := initRepo
4545 if repoName == "" {
@@ -50,15 +50,24 @@ Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
5050 }
5151 }
5252
53- fmt . Printf ( "Initializing git repository: %s/%s \n " , owner , repoName )
54- fmt . Println ( )
53+ alreadyGitRepo := git . IsGitRepository ( repoPath )
54+ hasGitHub , _ := git . IsGitHubRepo ( repoPath )
5555
56- fmt .Println ("Step 1/6: Running git init..." )
57- if err := git .InitRepo (repoPath ); err != nil {
58- return fmt .Errorf ("git init failed: %w" , err )
56+ if alreadyGitRepo && hasGitHub {
57+ fmt .Printf ("Existing GitHub repository detected: %s\n \n " , repoPath )
58+ fmt .Println ("Step 1/3: Configuring git user..." )
59+ } else {
60+ if owner == "" {
61+ return fmt .Errorf ("GitHub owner is required. Set --owner flag or 'github-owner' in ~/.spark.yaml" )
62+ }
63+ fmt .Printf ("Initializing git repository: %s/%s\n \n " , owner , repoName )
64+ fmt .Println ("Step 1/6: Running git init..." )
65+ if err := git .InitRepo (repoPath ); err != nil {
66+ return fmt .Errorf ("git init failed: %w" , err )
67+ }
68+ fmt .Println ("Step 2/6: Configuring git user..." )
5969 }
6070
61- fmt .Println ("Step 2/6: Configuring git user..." )
6271 username := viper .GetString ("git.username" )
6372 email := viper .GetString ("git.email" )
6473 if username != "" && email != "" {
@@ -72,6 +81,20 @@ Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
7281 fmt .Println (" No git user configured in ~/.spark.yaml. Skipping." )
7382 }
7483
84+ if alreadyGitRepo && hasGitHub {
85+ fmt .Println ("Step 2/3: Scanning for child GitHub repos to add as submodules..." )
86+ if err := git .AddChildReposAsSubmodules (repoPath ); err != nil {
87+ fmt .Printf ("Warning: %v\n " , err )
88+ }
89+ fmt .Println ("Step 3/3: Committing submodule changes..." )
90+ if err := git .InitialCommit (repoPath , "chore: add existing repos as submodules" ); err != nil {
91+ fmt .Printf ("Warning: commit failed: %v\n " , err )
92+ }
93+ fmt .Println ()
94+ fmt .Println ("Done! Submodules added to existing repository." )
95+ return nil
96+ }
97+
7598 fmt .Println ("Step 3/6: Scanning for child GitHub repos to add as submodules..." )
7699 if err := git .AddChildReposAsSubmodules (repoPath ); err != nil {
77100 fmt .Printf ("Warning: %v\n " , err )
0 commit comments