Skip to content

Commit 55fd5a5

Browse files
Require repo init before adopting root
Co-authored-by: Andrew <andrewxhill@gmail.com>
1 parent f60aca1 commit 55fd5a5

2 files changed

Lines changed: 41 additions & 21 deletions

File tree

internal/app/repo.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ Flags:
414414
}
415415
repoRoot := layout.RepositoryRoot
416416

417-
cfg, _, err := policy.LoadOrDefault(repoRoot)
417+
cfg, present, err := policy.LoadOrDefault(repoRoot)
418418
if err != nil {
419419
return err
420420
}
@@ -424,7 +424,7 @@ Flags:
424424

425425
fixesApplied := []string{}
426426
if adoptRoot {
427-
if err := adoptRepositoryRoot(repoRoot, layout, &cfg); err != nil {
427+
if err := adoptRepositoryRoot(repoRoot, layout, &cfg, present); err != nil {
428428
return err
429429
}
430430
fixesApplied = append(fixesApplied, fmt.Sprintf("set canonical main worktree to repository root %s", filepath.Clean(layout.RepositoryRoot)))
@@ -463,7 +463,16 @@ Flags:
463463
return nil
464464
}
465465

466-
func adoptRepositoryRoot(repoRoot string, layout git.RepositoryLayout, cfg *policy.File) error {
466+
func adoptRepositoryRoot(repoRoot string, layout git.RepositoryLayout, cfg *policy.File, configPresent bool) error {
467+
if !configPresent {
468+
return fmt.Errorf("repository is not initialized; run `mq repo init --repo %s` before adopting the root checkout", repoRoot)
469+
}
470+
471+
store := state.NewStore(state.DefaultPath(layout.GitDir))
472+
if !store.Exists() {
473+
return fmt.Errorf("repository is not initialized; run `mq repo init --repo %s` before adopting the root checkout", repoRoot)
474+
}
475+
467476
result := buildRepoRootResult(repoRoot, *cfg, layout)
468477
if !result.CanAdoptRoot {
469478
return fmt.Errorf("cannot adopt repository root as canonical main worktree yet; run `mq repo root --repo %s --json` and complete the recommended actions first", repoRoot)
@@ -474,24 +483,21 @@ func adoptRepositoryRoot(repoRoot string, layout git.RepositoryLayout, cfg *poli
474483
return err
475484
}
476485

477-
store := state.NewStore(state.DefaultPath(layout.GitDir))
478-
if store.Exists() {
479-
ctx := context.Background()
480-
if err := store.EnsureSchema(ctx); err != nil {
481-
return err
482-
}
483-
if _, _, err := ensureRepositoryRecord(ctx, store, repoRoot, *cfg); err != nil {
484-
return err
485-
}
486-
if _, err := store.UpsertRepository(ctx, state.RepositoryRecord{
487-
CanonicalPath: repoRoot,
488-
ProtectedBranch: cfg.Repo.ProtectedBranch,
489-
RemoteName: cfg.Repo.RemoteName,
490-
MainWorktree: cfg.Repo.MainWorktree,
491-
PolicyVersion: "v1",
492-
}); err != nil {
493-
return err
494-
}
486+
ctx := context.Background()
487+
if err := store.EnsureSchema(ctx); err != nil {
488+
return err
489+
}
490+
if _, _, err := ensureRepositoryRecord(ctx, store, repoRoot, *cfg); err != nil {
491+
return err
492+
}
493+
if _, err := store.UpsertRepository(ctx, state.RepositoryRecord{
494+
CanonicalPath: repoRoot,
495+
ProtectedBranch: cfg.Repo.ProtectedBranch,
496+
RemoteName: cfg.Repo.RemoteName,
497+
MainWorktree: cfg.Repo.MainWorktree,
498+
PolicyVersion: "v1",
499+
}); err != nil {
500+
return err
495501
}
496502
return registerRepo(cfg.Repo.MainWorktree, repoRoot, state.DefaultPath(layout.GitDir))
497503
}

internal/app/repo_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,20 @@ func TestRepoRootRejectsAdoptWhenRootCheckoutIsDirty(t *testing.T) {
201201
}
202202
}
203203

204+
func TestRepoRootAdoptRequiresInitializedRepo(t *testing.T) {
205+
repoRoot, _ := createTestRepo(t)
206+
207+
var rootOut bytes.Buffer
208+
var rootErr bytes.Buffer
209+
err := runRepoRoot([]string{"--repo", repoRoot, "--adopt-root", "--json"}, &rootOut, &rootErr)
210+
if err == nil {
211+
t.Fatalf("expected adopt-root to require repo init first")
212+
}
213+
if !strings.Contains(err.Error(), "run `mq repo init") {
214+
t.Fatalf("expected repo init guidance, got %v", err)
215+
}
216+
}
217+
204218
func TestRepoInitSupportsJSONOutput(t *testing.T) {
205219
repoRoot, worktreePath := createTestRepo(t)
206220
registryPath := filepath.Join(t.TempDir(), "registry.json")

0 commit comments

Comments
 (0)