From 593bcabf2e96ac369bb0e43d70cfdfa9d1c8a2b1 Mon Sep 17 00:00:00 2001 From: Willie Date: Sun, 22 Feb 2026 06:50:17 +0800 Subject: [PATCH] fix: defer git initial commit to after skill installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit doGitInit() previously committed immediately (only .gitignore), but installSkillIfNeeded() runs later — leaving skill files uncommitted. This caused "Local changes detected" on first pull. Move commit to a single commitSourceFiles() call at the end of init, after all files are in place. Covers all scenarios: performFreshInit (first-time init): - No remote, no skill → 1 commit: "Initial commit" (.gitignore) - No remote, with skill → 1 commit: "Initial skills" (.gitignore + skill) - Remote empty → same as above, remote fetch is no-op - Remote has skills → reset --hard to remote; commitSourceFiles is no-op - Remote unreachable → same as no-remote (fetch fails gracefully) handleExistingInit (init --remote on existing config): - Git exists → commitSourceFiles is no-op (already committed) - Git not exists, remote reachable → commit before setupGitRemote so HEAD exists for reset --hard - Git not exists, remote unreachable → commit .gitignore so push/pull don't see "Local changes detected" --- cmd/skillshare/init.go | 55 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/cmd/skillshare/init.go b/cmd/skillshare/init.go index aa91d62df..c2380572d 100644 --- a/cmd/skillshare/init.go +++ b/cmd/skillshare/init.go @@ -197,6 +197,10 @@ func handleExistingInit(opts *initOptions) (bool, error) { // Ensure git is initialized before setting up remote // (--remote implies --git, so opts.initGit is already true) initGitIfNeeded(cfg.Source, opts.dryRun, opts.initGit, opts.noGit) + // Commit any uncommitted source files so push/pull work cleanly + if !opts.dryRun { + commitSourceFiles(cfg.Source) + } setupGitRemote(cfg.Source, opts.remoteURL, opts.dryRun) return true, nil } @@ -283,6 +287,11 @@ func performFreshInit(opts *initOptions, home string) error { // Install built-in skillshare skill (opt-in) installSkillIfNeeded(sourcePath, opts.dryRun, opts.initSkill, opts.noSkill) + // Single initial commit with all source files (.gitignore + skills) + if !opts.dryRun && !opts.noGit { + commitSourceFiles(sourcePath) + } + // Print completion message skillInstalled := false if _, err := os.Stat(filepath.Join(sourcePath, "skillshare", "SKILL.md")); err == nil { @@ -771,40 +780,42 @@ func doGitInit(sourcePath string) { os.WriteFile(gitignore, []byte(".DS_Store\n"), 0644) } - // Initial commit if there are files - entries, _ := os.ReadDir(sourcePath) - hasFiles := false - for _, e := range entries { - if e.Name() != ".git" && e.Name() != ".gitignore" { - hasFiles = true - break - } - } - // Ensure git identity is configured (needed for commit). ensureGitIdentity(sourcePath) - // Always create initial commit (at least .gitignore) so that - // git stash / git pull work immediately after init. + ui.Success("Git initialized") +} + +// commitSourceFiles creates a single initial commit with all source files +// (.gitignore, copied skills, installed skills). Called once at the end of +// init so that git stash / git pull work immediately. +func commitSourceFiles(sourcePath string) { + gitDir := filepath.Join(sourcePath, ".git") + if _, err := os.Stat(gitDir); os.IsNotExist(err) { + return + } + addCmd := exec.Command("git", "add", ".") addCmd.Dir = sourcePath addCmd.Run() + // Determine commit message based on content + entries, _ := os.ReadDir(sourcePath) + hasSkills := false + for _, e := range entries { + if e.Name() != ".git" && e.Name() != ".gitignore" { + hasSkills = true + break + } + } + msg := "Initial commit" - if hasFiles { + if hasSkills { msg = "Initial skills" } commitCmd := exec.Command("git", "commit", "-m", msg) commitCmd.Dir = sourcePath - if out, err := commitCmd.CombinedOutput(); err != nil { - ui.Warning("Failed to create initial commit: %s", strings.TrimSpace(string(out))) - } - - if hasFiles { - ui.Success("Git initialized with initial commit") - } else { - ui.Success("Git initialized") - } + commitCmd.Run() // best-effort } func setupGitRemote(sourcePath, remoteURL string, dryRun bool) {