Skip to content

Commit 86f27c5

Browse files
committed
modify spark git init to support add submodule
1 parent d76d909 commit 86f27c5

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

cmd/git/init.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ var initCmd = &cobra.Command{
2424
1. Run 'git init'
2525
2. Configure git user (name/email)
2626
3. Add child GitHub repos as git submodules
27-
4. Create a GitHub remote repository via 'gh repo create'
28-
5. Generate a .gitignore with common patterns
27+
4. Generate a .gitignore with common patterns
28+
5. Create an initial commit
29+
6. Create a GitHub remote repository via 'gh repo create --push'
2930
3031
The repo name defaults to the current directory name.
3132
Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
@@ -52,12 +53,12 @@ Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
5253
fmt.Printf("Initializing git repository: %s/%s\n", owner, repoName)
5354
fmt.Println()
5455

55-
fmt.Println("Step 1/5: Running git init...")
56+
fmt.Println("Step 1/6: Running git init...")
5657
if err := git.InitRepo(repoPath); err != nil {
5758
return fmt.Errorf("git init failed: %w", err)
5859
}
5960

60-
fmt.Println("Step 2/5: Configuring git user...")
61+
fmt.Println("Step 2/6: Configuring git user...")
6162
username := viper.GetString("git.username")
6263
email := viper.GetString("git.email")
6364
if username != "" && email != "" {
@@ -71,25 +72,30 @@ Owner defaults to the 'github-owner' config value in ~/.spark.yaml.`,
7172
fmt.Println(" No git user configured in ~/.spark.yaml. Skipping.")
7273
}
7374

74-
fmt.Println("Step 3/5: Scanning for child GitHub repos to add as submodules...")
75+
fmt.Println("Step 3/6: Scanning for child GitHub repos to add as submodules...")
7576
if err := git.AddChildReposAsSubmodules(repoPath); err != nil {
7677
fmt.Printf("Warning: %v\n", err)
7778
}
7879

80+
fmt.Println("Step 4/6: Generating .gitignore...")
81+
if err := git.WriteGitIgnore(repoPath); err != nil {
82+
fmt.Printf("Warning: failed to write .gitignore: %v\n", err)
83+
} else {
84+
fmt.Println(" .gitignore created")
85+
}
86+
87+
fmt.Println("Step 5/6: Creating initial commit...")
88+
if err := git.InitialCommit(repoPath, "chore: initial commit via spark git init"); err != nil {
89+
fmt.Printf("Warning: initial commit failed: %v\n", err)
90+
}
91+
7992
if !initSkipGh {
80-
fmt.Printf("Step 4/5: Creating GitHub remote repository (%s/%s)...\n", owner, repoName)
93+
fmt.Printf("Step 6/6: Creating GitHub remote repository (%s/%s)...\n", owner, repoName)
8194
if err := git.CreateGitHubRepo(repoPath, owner, repoName, initPrivate); err != nil {
8295
return fmt.Errorf("creating GitHub repository failed: %w", err)
8396
}
8497
} else {
85-
fmt.Println("Step 4/5: Skipping GitHub remote creation (--skip-gh)")
86-
}
87-
88-
fmt.Println("Step 5/5: Generating .gitignore...")
89-
if err := git.WriteGitIgnore(repoPath); err != nil {
90-
fmt.Printf("Warning: failed to write .gitignore: %v\n", err)
91-
} else {
92-
fmt.Println(" .gitignore created")
98+
fmt.Println("Step 6/6: Skipping GitHub remote creation (--skip-gh)")
9399
}
94100

95101
fmt.Println()

internal/git/init.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ coverage/
9696
return os.WriteFile(gitignorePath, []byte(content), 0644)
9797
}
9898

99+
func InitialCommit(repoPath, message string) error {
100+
addCmd := exec.Command("git", "add", ".")
101+
addCmd.Dir = repoPath
102+
addCmd.Stdout = os.Stdout
103+
addCmd.Stderr = os.Stderr
104+
if err := addCmd.Run(); err != nil {
105+
return fmt.Errorf("failed to stage files: %w", err)
106+
}
107+
108+
commitCmd := exec.Command("git", "commit", "-m", message)
109+
commitCmd.Dir = repoPath
110+
commitCmd.Stdout = os.Stdout
111+
commitCmd.Stderr = os.Stderr
112+
if err := commitCmd.Run(); err != nil {
113+
return fmt.Errorf("failed to commit: %w", err)
114+
}
115+
116+
return nil
117+
}
118+
99119
func AddChildReposAsSubmodules(rootPath string) error {
100120
entries, err := os.ReadDir(rootPath)
101121
if err != nil {

0 commit comments

Comments
 (0)