feat(init): set remote during headless git_root scope switch

Combining --git-root <scope> with --remote <url> on an already-initialized
setup now switches the scope and wires the remote onto the new scope's repo
in one command, instead of warning and ignoring --git-root. Reuses the
existing scope-switch and remote-configuration paths back to back.
This commit is contained in:
Willie
2026-05-30 19:23:12 +08:00
parent 9ca77bd12e
commit 5807a4c743
2 changed files with 40 additions and 9 deletions
+9 -9
View File
@@ -337,13 +337,19 @@ func handleExistingInit(opts *initOptions) (bool, error) {
// Headless scope switch: `skillshare init --git-root <scope>` on an existing
// setup initializes a repo at the new scope directory and persists git_root.
// It does not relocate an existing repo (run `mv <old>/.git <new>/.git` for
// that). Combined with --remote we fall through to the remote path below.
if opts.gitRootScope != "" && opts.remoteURL == "" {
// that). With --remote also given, fall through so the remote is configured
// on the just-switched scope's repo.
if opts.gitRootScope != "" {
cfg, err := config.Load()
if err != nil {
return true, err
}
return true, switchGitRootScope(cfg, opts.gitRootScope, opts.dryRun)
if err := switchGitRootScope(cfg, opts.gitRootScope, opts.dryRun); err != nil {
return true, err
}
if opts.remoteURL == "" {
return true, nil
}
}
// If --remote provided, just add the remote to existing setup
@@ -358,12 +364,6 @@ func handleExistingInit(opts *initOptions) (bool, error) {
if scope == "" {
scope = "skills"
}
// Scope changes are not combined with --remote — direct the user to the
// dedicated headless form instead of silently applying it here.
if opts.gitRootScope != "" && opts.gitRootScope != scope {
ui.Warning("--git-root %q ignored when combined with --remote", opts.gitRootScope)
ui.Info(" Change the scope separately: skillshare init --git-root %s", opts.gitRootScope)
}
gitRoot := cfg.EffectiveGitRoot()
if opts.noGit {
ui.Info("Skipped git initialization (--no-git)")
+31
View File
@@ -293,6 +293,37 @@ func TestGitRoot_SwitchScopeAfterInit(t *testing.T) {
}
}
// init --git-root <scope> --remote <url> switches the scope and wires the
// remote on the new scope's repo in one headless command.
func TestGitRoot_SwitchScopeWithRemote(t *testing.T) {
requireWorkingGit(t)
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.RunCLI("init", "--no-copy", "--no-targets", "--no-skill", "--git").AssertSuccess(t)
base := filepath.Dir(sb.ConfigPath)
bareRepo := testutil.SetupBareRemoteRepo(t, t.TempDir())
// Switch to root scope AND wire a remote in one command.
result := sb.RunCLI("init", "--git-root", "root", "--remote", bareRepo)
result.AssertSuccess(t)
cfgBytes, err := os.ReadFile(sb.ConfigPath)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(cfgBytes), "git_root: root") {
t.Errorf("expected git_root: root, got:\n%s", cfgBytes)
}
// The root-scope repo must carry the configured remote.
remote := testutil.RunGit(t, base, "remote", "get-url", "origin")
if remote != bareRepo {
t.Errorf("expected origin %q at root scope, got %q", bareRepo, remote)
}
}
// requireWorkingGit skips the test when the host/container git environment
// cannot init a repo and create a commit (e.g. read-only HOME, an identity that
// cannot be written, or an owner/permission mismatch). Stripped one-shot