Files
skillshare/cmd/skillshare/gitroot.go
T
Willie 314e2b2df4 feat(init): switch git_root scope headlessly on an existing setup
Previously 'skillshare init --git-root <scope>' on an already-initialized
setup either errored with 'already initialized' (no --remote) or warned and
ignored the flag (with --remote), so there was no non-interactive way to
change the scope after init — only the web UI or a manual config edit.

handleExistingInit now treats 'init --git-root <scope>' (without --remote) as
a headless scope switch via switchGitRootScope: it inits a repo at the new
scope directory (reusing one already there, sharing doGitInitIfAbsent with the
fresh-init and server paths) and persists git_root. It does not relocate an
existing repo — switching scope means 'version a different directory', not
'move history'. Combined with --remote the flag still falls through to the
remote path, now pointing users at the standalone form instead of a bare
'ignored' warning.

Update the mismatch guidance to offer 'skillshare init --git-root <scope>' as
the first fix, and revise the git_root docs (configuration + init) to document
the headless switch, superseding the earlier 'must re-run init / not supported'
wording. Add TestGitRoot_SwitchScopeAfterInit.
2026-05-30 19:23:12 +08:00

44 lines
1.6 KiB
Go

package main
import (
"os"
"path/filepath"
"skillshare/internal/config"
"skillshare/internal/ui"
)
// hasGitDir reports whether dir contains a .git entry directly (not via a parent).
func hasGitDir(dir string) bool {
_, err := os.Stat(filepath.Join(dir, ".git"))
return err == nil
}
// resolveGitRoot returns the directory commit/push/pull should operate on.
//
// Option (a) mismatch handling: if the configured scope has no repo directly at
// its directory but another scope's directory does, the user changed git_root
// without relocating the repo. We print guidance and return ok=false so the
// caller aborts. When no stray repo is found we return the resolved directory and
// let the caller's existing repo check emit the standard "not a git repository"
// guidance (this also preserves the legacy case of a skills source nested inside
// an unrelated parent git repo).
func resolveGitRoot(cfg *config.Config, spinner *ui.Spinner) (string, bool) {
root := cfg.EffectiveGitRoot()
if scope, dir, mismatch := cfg.GitRootMismatch(); mismatch {
configured := cfg.GitRoot
if configured == "" {
configured = "skills"
}
spinner.Fail("Git root mismatch")
ui.Info(" git_root operates on: %s", root)
ui.Info(" but the git repo lives at: %s (%s)", dir, scope)
ui.Info(" Fix it with one of:")
ui.Info(" - skillshare init --git-root %s (start a fresh repo at the configured scope)", configured)
ui.Info(" - mv %s/.git %s/.git (move the existing repo over, keeps history)", dir, root)
ui.Info(" - set 'git_root: %s' in %s (keep using the existing repo)", scope, config.ConfigPath())
return "", false
}
return root, true
}