gitgrip 1.0.0

Multi-repo workflow tool - manage multiple git repositories as one
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
//! `gr migrate` — convert existing repos into a gripspace (#424).
//!
//! Two subcommands:
//!   - `from-repos`: Generate a new gripspace from GitHub repo list
//!   - `in-place`:   Convert an existing git repo dir into a gripspace

use crate::cli::output::Output;
use crate::core::griptree::{GriptreeConfig, GriptreePointer, GriptreeRepoInfo};
use crate::core::manifest::{CloneStrategy, Manifest, ManifestSettings, MergeStrategy, RepoConfig};
use crate::core::manifest_paths;
use chrono::Utc;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
use std::collections::{HashMap, HashSet};
use std::io::IsTerminal;
use std::path::{Path, PathBuf};
use std::process::Output as ProcessOutput;

/// Agent configuration collected during interactive setup.
struct AgentSpec {
    name: String,
    role: String,
    model: String,
    tool: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ExistingWorktree {
    root: PathBuf,
    branch: Option<String>,
    is_main: bool,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
struct MigratedGriptreesList {
    griptrees: HashMap<String, MigratedGriptreeEntry>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct MigratedGriptreeEntry {
    path: String,
    branch: String,
    locked: bool,
    lock_reason: Option<String>,
}

/// Run `gr migrate from-repos` — generate gripspace from GitHub repos.
pub async fn run_migrate_from_repos(
    repos: &[String],
    org: Option<&str>,
    prefix: Option<&str>,
    path: Option<&str>,
    json: bool,
) -> anyhow::Result<()> {
    if repos.is_empty() {
        anyhow::bail!("At least one --repo is required");
    }

    if !json {
        Output::header("Migrating repos to gripspace...");
        println!();
    }

    // Parse repo specs (owner/name)
    let mut parsed_repos: Vec<(String, String)> = Vec::new();
    for spec in repos {
        let parts: Vec<&str> = spec.splitn(2, '/').collect();
        if parts.len() != 2 {
            anyhow::bail!(
                "Invalid repo format '{}': expected owner/repo (e.g. GetConversa/conversa-app)",
                spec
            );
        }
        parsed_repos.push((parts[0].to_string(), parts[1].to_string()));
    }

    // Determine target directory
    let target_dir = match path {
        Some(p) => PathBuf::from(p),
        None => {
            let name = prefix.unwrap_or("gripspace");
            std::env::current_dir()?.join(name)
        }
    };

    let prefix_str = prefix.unwrap_or("workspace");
    let org_str = org.unwrap_or_else(|| parsed_repos[0].0.as_str());

    if !json {
        Output::info(&format!("Target directory: {}", target_dir.display()));
        Output::info(&format!("Repos to migrate: {}", repos.len()));
        println!();
    }

    // Interactive agent team configuration
    let interactive = !json && std::io::stdin().is_terminal();
    let agents = if interactive {
        configure_agents_interactive()?
    } else {
        Vec::new()
    };

    // Generate all files
    let manifest_yaml = generate_manifest_yaml(&parsed_repos, org_str, prefix_str);
    let claude_md = generate_claude_md(&parsed_repos, prefix_str, &agents);
    let agents_toml = generate_agents_toml(prefix_str, &agents);
    let prompts: Vec<(String, String)> = agents
        .iter()
        .map(|a| {
            (
                a.name.clone(),
                generate_agent_prompt(a, &parsed_repos, prefix_str),
            )
        })
        .collect();

    // Create directory structure
    create_gripspace_dirs(
        &target_dir,
        &manifest_yaml,
        &claude_md,
        &agents_toml,
        &prompts,
    )?;

    if !json {
        Output::success("Gripspace structure created:");
        println!("  {}/", target_dir.display());
        println!("    .gitgrip/spaces/main/gripspace.yml");
        println!("    config/CLAUDE.md");
        println!("    config/agents.toml");
        for (name, _) in &prompts {
            println!("    config/prompts/{}.md", name);
        }
        println!();
        Output::info("Next steps:");
        println!("  1. cd {}", target_dir.display());
        println!("  2. Review and edit .gitgrip/spaces/main/gripspace.yml");
        println!("  3. gr sync   # Clone all repos");
        println!("  4. gr status # Verify everything works");
        if !agents.is_empty() {
            println!("  5. gr spawn up  # Launch agent team");
        }
    }

    if json {
        let result = serde_json::json!({
            "target_dir": target_dir.display().to_string(),
            "repos": repos,
            "agents": agents.iter().map(|a| &a.name).collect::<Vec<_>>(),
            "manifest": ".gitgrip/spaces/main/gripspace.yml",
            "config": "config/",
        });
        println!("{}", serde_json::to_string_pretty(&result)?);
    }

    Ok(())
}

/// Interactive agent team configuration using dialoguer.
fn configure_agents_interactive() -> anyhow::Result<Vec<AgentSpec>> {
    let theme = ColorfulTheme::default();
    let mut agents = Vec::new();

    let add_agents = Confirm::with_theme(&theme)
        .with_prompt("Configure AI agent team?")
        .default(true)
        .interact()?;

    if !add_agents {
        return Ok(agents);
    }

    println!();
    Output::info("Add agents one at a time. Press Enter with empty name to finish.");
    println!();

    let models = ["claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5"];
    let tools = ["claude", "codex"];

    loop {
        let name: String = Input::with_theme(&theme)
            .with_prompt("Agent name (empty to finish)")
            .allow_empty(true)
            .interact_text()?;

        if name.is_empty() {
            break;
        }

        let role: String = Input::with_theme(&theme)
            .with_prompt("Role")
            .default("implementation".to_string())
            .interact_text()?;

        let model_idx = Select::with_theme(&theme)
            .with_prompt("Model")
            .items(&models)
            .default(1) // sonnet
            .interact()?;

        let tool_idx = Select::with_theme(&theme)
            .with_prompt("Tool")
            .items(&tools)
            .default(0) // claude
            .interact()?;

        agents.push(AgentSpec {
            name,
            role,
            model: models[model_idx].to_string(),
            tool: tools[tool_idx].to_string(),
        });

        println!();
    }

    Ok(agents)
}

/// Generate gripspace.yml content.
fn generate_manifest_yaml(repos: &[(String, String)], org: &str, prefix: &str) -> String {
    let mut yaml = String::new();
    yaml.push_str("# Generated by gr migrate from-repos\n");
    yaml.push_str("version: 2\n\n");

    // Manifest self-reference
    yaml.push_str("manifest:\n");
    yaml.push_str(&format!(
        "  url: https://github.com/{}/{}-gripspace.git\n",
        org, prefix
    ));
    yaml.push_str("  revision: main\n\n");

    // Repos
    yaml.push_str("repos:\n");
    for (owner, name) in repos {
        yaml.push_str(&format!("  {}:\n", name));
        yaml.push_str(&format!(
            "    url: https://github.com/{}/{}.git\n",
            owner, name
        ));
        yaml.push_str(&format!("    path: ./{}\n", name));
        yaml.push_str("    revision: main\n\n");
    }

    // Config repo — linkfiles source CLAUDE.md and agents.toml into workspace root
    yaml.push_str(&format!("  {}-config:\n", prefix));
    yaml.push_str(&format!(
        "    url: https://github.com/{}/{}-config.git\n",
        org, prefix
    ));
    yaml.push_str("    path: ./config\n");
    yaml.push_str("    revision: main\n");
    yaml.push_str("    linkfile:\n");
    yaml.push_str("      - src: CLAUDE.md\n");
    yaml.push_str("        dest: CLAUDE.md\n");
    yaml.push_str("      - src: agents.toml\n");
    yaml.push_str("        dest: .gitgrip/agents.toml\n\n");

    // Settings
    yaml.push_str("settings:\n");
    yaml.push_str("  revision: main\n");
    yaml.push_str("  clone_strategy: clone\n");

    yaml
}

/// Generate CLAUDE.md with repo table and agent context.
fn generate_claude_md(repos: &[(String, String)], prefix: &str, agents: &[AgentSpec]) -> String {
    let mut md = String::new();
    md.push_str(&format!("# {}\n\n", prefix));
    md.push_str("Multi-repo workspace managed by **gitgrip** (`gr`). Always use `gr` — never raw `git` or `gh`.\n\n");

    md.push_str("## Repos\n\n");
    md.push_str("| Repo | Path | Description |\n");
    md.push_str("|------|------|-------------|\n");
    for (_owner, name) in repos {
        md.push_str(&format!("| **{}** | `{}/` | |\n", name, name));
    }

    if !agents.is_empty() {
        md.push_str("\n## Agent Team\n\n");
        md.push_str("| Agent | Role | Model |\n");
        md.push_str("|-------|------|-------|\n");
        for agent in agents {
            md.push_str(&format!(
                "| **{}** | {} | {} |\n",
                agent.name, agent.role, agent.model
            ));
        }
    }

    md.push_str("\n## Git Workflow\n\n");
    md.push_str("Always use `gr` instead of `git` or `gh`.\n\n");
    md.push_str("```bash\n");
    md.push_str("gr sync                  # Pull all repos\n");
    md.push_str("gr status                # Check state\n");
    md.push_str("gr branch feat/my-feat   # Branch across repos\n");
    md.push_str("gr add . && gr commit -m \"message\" && gr push -u\n");
    md.push_str("gr pr create -t \"feat: title\" --push\n");
    md.push_str("gr pr merge              # Merge linked PRs\n");
    md.push_str("```\n");

    md
}

/// Generate agents.toml with full team configuration.
fn generate_agents_toml(prefix: &str, agents: &[AgentSpec]) -> String {
    let mut toml = String::new();
    toml.push_str(&format!("# Agent configuration for {}\n\n", prefix));
    toml.push_str("[spawn]\n");
    toml.push_str(&format!("session_name = \"{}\"\n", prefix));
    toml.push_str("channel = \"dev\"\n");
    toml.push_str("auto_journal = true\n\n");

    if agents.is_empty() {
        toml.push_str("# Add agents here:\n");
        toml.push_str("# [agents.my-agent]\n");
        toml.push_str("# role = \"implementation\"\n");
        toml.push_str("# model = \"claude-sonnet-4-6\"\n");
        toml.push_str("# tool = \"claude\"\n");
        toml.push_str("# worktree = \"main\"\n");
    } else {
        for agent in agents {
            toml.push_str(&format!("[agents.{}]\n", agent.name));
            toml.push_str(&format!("role = \"{}\"\n", agent.role));
            toml.push_str(&format!("model = \"{}\"\n", agent.model));
            toml.push_str(&format!("tool = \"{}\"\n", agent.tool));
            toml.push_str("worktree = \"main\"\n");
            toml.push_str(&format!(
                "startup_prompt = \"config/prompts/{}.md\"\n",
                agent.name
            ));
            toml.push_str("channel = \"dev\"\n");
            toml.push_str("loop_interval = \"2m\"\n\n");
        }
    }

    toml
}

/// Generate a per-agent prompt file.
fn generate_agent_prompt(agent: &AgentSpec, repos: &[(String, String)], prefix: &str) -> String {
    let mut prompt = String::new();
    prompt.push_str(&format!("# {} — {}\n\n", agent.name, agent.role));
    prompt.push_str(&format!(
        "You are **{}**, an AI agent working on the **{}** project.\n\n",
        agent.name, prefix
    ));
    prompt.push_str(&format!("## Your Role\n\n{}\n\n", agent.role));
    prompt.push_str("## Workspace\n\n");
    prompt.push_str("This is a multi-repo workspace managed by `gr` (gitgrip). Repos:\n\n");
    for (_owner, name) in repos {
        prompt.push_str(&format!("- `{}/`\n", name));
    }
    prompt.push_str("\n## Rules\n\n");
    prompt.push_str("- Always use `gr` for git operations, never raw `git` or `gh`\n");
    prompt.push_str("- Check #dev channel for assignments before starting work\n");
    prompt.push_str("- Claim tasks before building\n");
    prompt.push_str("- Post intent before coding\n");
    prompt.push_str("- Tests on first submission\n");
    prompt
}

/// Create the gripspace directory structure on disk.
fn create_gripspace_dirs(
    target: &Path,
    manifest_yaml: &str,
    claude_md: &str,
    agents_toml: &str,
    prompts: &[(String, String)],
) -> anyhow::Result<()> {
    let manifest_dir = target.join(".gitgrip/spaces/main");
    let config_dir = target.join("config");
    let prompts_dir = config_dir.join("prompts");
    std::fs::create_dir_all(&manifest_dir)?;
    std::fs::create_dir_all(&prompts_dir)?;

    // Write files
    std::fs::write(manifest_dir.join("gripspace.yml"), manifest_yaml)?;
    std::fs::write(config_dir.join("CLAUDE.md"), claude_md)?;
    std::fs::write(config_dir.join("agents.toml"), agents_toml)?;

    // Write per-agent prompts
    for (name, content) in prompts {
        std::fs::write(prompts_dir.join(format!("{}.md", name)), content)?;
    }

    // Initialize manifest directory as a git repo so gr init can use it
    let git_init = std::process::Command::new("git")
        .args(["init", "-b", "main"])
        .current_dir(&manifest_dir)
        .output();
    if let Err(e) = git_init {
        Output::warning(&format!("git init failed: {}", e));
    }
    let git_add = std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&manifest_dir)
        .output();
    if let Err(e) = git_add {
        Output::warning(&format!("git add failed: {}", e));
    }
    let git_commit = std::process::Command::new("git")
        .args(["commit", "-m", "Initial gripspace manifest"])
        .current_dir(&manifest_dir)
        .output();
    if let Err(e) = git_commit {
        Output::warning(&format!("git commit failed: {}", e));
    }

    Ok(())
}

/// Run `gr migrate in-place` — convert a git repo dir into a gripspace.
///
/// Algorithm (v3.2):
/// 1. Derive repo name from `git remote get-url origin` (basename, strip .git)
///    Falls back to directory name if no remote or command fails.
/// 2. Enumerate the main repo plus linked worktrees via `git worktree list --porcelain`.
/// 3. For each worktree root, move everything into `<worktree-root>/<repo-name>` EXCEPT:
///    .synapt/, .claude/, .env, _migrate_tmp/, and the child dir name.
/// 4. Run `git worktree repair` after all paths move so linked worktree metadata points at
///    the new `<worktree-root>/<repo-name>` locations.
/// 5. Create `.gitgrip/griptrees.json` at the main root and `.griptree` /
///    `.gitgrip/griptree.json` in each linked worktree root.
pub async fn run_migrate_in_place(
    path: Option<&str>,
    dry_run: bool,
    json: bool,
) -> anyhow::Result<()> {
    let root = match path {
        Some(p) => PathBuf::from(p).canonicalize()?,
        None => std::env::current_dir()?,
    };

    // Guard: must be a git repo (has .git at root)
    if !root.join(".git").exists() {
        if root.join(".gitgrip").exists() {
            anyhow::bail!(
                "Already a gripspace (has .gitgrip/). Run `gr status` to check the workspace."
            );
        }
        anyhow::bail!("Not a git repository: no .git found at {}", root.display());
    }

    // Guard: git 2.30+ required for `git worktree repair`
    check_git_version_for_repair()?;

    // Derive repo name
    let repo_name = derive_repo_name_from_remote(&root);
    let worktrees = list_existing_worktrees(&root)?;
    let linked_worktrees: Vec<ExistingWorktree> =
        worktrees.iter().filter(|wt| !wt.is_main).cloned().collect();
    let main_revision = worktrees
        .iter()
        .find(|wt| wt.is_main)
        .and_then(|wt| wt.branch.clone())
        .unwrap_or_else(|| "main".to_string());

    validate_migration_targets(&worktrees, &repo_name)?;

    if !json {
        Output::header("gr migrate in-place");
        println!();
        Output::info(&format!("Gripspace root: {}", root.display()));
        Output::info(&format!("Repo child:     {}/{}", root.display(), repo_name));
        if !linked_worktrees.is_empty() {
            Output::info(&format!("Linked worktrees: {}", linked_worktrees.len()));
            for wt in &linked_worktrees {
                let branch = wt.branch.as_deref().unwrap_or("(detached)");
                println!("  {} -> {}/{}", branch, wt.root.display(), repo_name);
            }
        }
        println!();
        if dry_run {
            Output::warning("DRY RUN — no changes will be made");
            println!();
        }
    }

    if !json && dry_run {
        for wt in &worktrees {
            let to_move = collect_paths_to_move(&wt.root, &repo_name)?;
            let label = if wt.is_main {
                "main repo".to_string()
            } else {
                format!(
                    "linked worktree ({})",
                    wt.branch.as_deref().unwrap_or("detached")
                )
            };
            println!(
                "  Would move {} into {}/{}:",
                label,
                wt.root.display(),
                repo_name
            );
            for p in &to_move {
                println!(
                    "    {}",
                    p.file_name().unwrap_or_default().to_string_lossy()
                );
            }
            println!();
            println!("  Would keep at {}:", wt.root.display());
            for path in [".synapt", ".claude", ".env"] {
                if wt.root.join(path).exists() {
                    if wt.root.join(path).is_dir() {
                        println!("    {}/", path);
                    } else {
                        println!("    {}", path);
                    }
                }
            }
            println!();
        }
        println!("  Would run: git worktree repair");
        println!("  Would create: {}/.gitgrip/griptrees.json", root.display());
        println!(
            "  Would create: {}/.gitgrip/spaces/main/gripspace.yml",
            root.display()
        );
        for wt in &linked_worktrees {
            println!("  Would create: {}/.griptree", wt.root.display());
            println!(
                "  Would create: {}/.gitgrip/griptree.json",
                wt.root.display()
            );
        }
        return Ok(());
    }

    if dry_run {
        let result = serde_json::json!({
            "root": root.display().to_string(),
            "repo_name": repo_name,
            "worktrees": worktrees.iter().map(|wt| serde_json::json!({
                "root": wt.root.display().to_string(),
                "branch": wt.branch,
                "is_main": wt.is_main,
                "to_move": collect_paths_to_move(&wt.root, &repo_name).unwrap_or_default().iter()
                    .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
                    .collect::<Vec<_>>(),
            })).collect::<Vec<_>>(),
            "dry_run": true,
        });
        println!("{}", serde_json::to_string_pretty(&result)?);
        return Ok(());
    }

    for wt in &worktrees {
        migrate_single_worktree_root(&wt.root, &repo_name)?;
    }

    let repair_outputs = run_worktree_repairs(&worktrees, &repo_name)?;
    for repair in &repair_outputs {
        if !json {
            let repair_out = String::from_utf8_lossy(&repair.stdout);
            let repair_err = String::from_utf8_lossy(&repair.stderr);
            if !repair_out.trim().is_empty() || !repair_err.trim().is_empty() {
                Output::info("git worktree repair output:");
                for line in repair_out.lines().chain(repair_err.lines()) {
                    println!("  {}", line);
                }
            }
        }
        worktree_repair_must_succeed(repair)?;
    }

    create_root_gripspace_metadata(&root, &linked_worktrees)?;
    create_root_gripspace_manifest(&root, &repo_name, &main_revision)?;
    for wt in &linked_worktrees {
        create_linked_griptree_metadata(&root, wt, &repo_name)?;
    }

    if json {
        let result = serde_json::json!({
            "root": root.display().to_string(),
            "repo_name": repo_name,
            "main_child": root.join(&repo_name).display().to_string(),
            "linked_worktrees": linked_worktrees.iter().map(|wt| serde_json::json!({
                "root": wt.root.display().to_string(),
                "child": wt.root.join(&repo_name).display().to_string(),
                "branch": wt.branch,
            })).collect::<Vec<_>>(),
            "worktree_repair": true,
        });
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        Output::success("Migration complete!");
        println!();
        println!("  Main repo moved to: {}/{}/", root.display(), repo_name);
        for wt in &linked_worktrees {
            println!(
                "  Linked worktree:   {}/{} ({})",
                wt.root.display(),
                repo_name,
                wt.branch.as_deref().unwrap_or("detached")
            );
        }
        if root.join(".synapt").exists() {
            println!("  .synapt/           stays at gripspace root");
        }
        if root.join(".claude").exists() {
            println!("  .claude/           stays at gripspace root");
        }
        if root.join(".env").exists() {
            println!("  .env               stays at gripspace root");
        }
        println!("  .gitgrip/          created (gripspace marker)");
        println!();
        Output::info("Next steps:");
        println!("  cd {}", root.display());
        println!("  gr tree list    # verify linked worktrees became griptrees");
        println!("  gr status       # verify repos are visible");
    }

    Ok(())
}

fn migration_keep_names() -> HashSet<&'static str> {
    [".synapt", ".claude", ".env", "_migrate_tmp"]
        .iter()
        .copied()
        .collect()
}

fn collect_paths_to_move(root: &Path, repo_name: &str) -> anyhow::Result<Vec<PathBuf>> {
    let keep = migration_keep_names();
    let mut to_move: Vec<PathBuf> = Vec::new();
    for entry in std::fs::read_dir(root)? {
        let entry = entry?;
        let name = entry.file_name();
        let name_str = name.to_string_lossy().to_string();
        if keep.contains(name_str.as_str()) || name_str == repo_name {
            continue;
        }
        to_move.push(entry.path());
    }
    Ok(to_move)
}

fn list_existing_worktrees(root: &Path) -> anyhow::Result<Vec<ExistingWorktree>> {
    let output = std::process::Command::new("git")
        .args(["worktree", "list", "--porcelain"])
        .current_dir(root)
        .output()
        .map_err(|e| anyhow::anyhow!("Failed to run git worktree list --porcelain: {}", e))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git worktree list --porcelain failed: {}", stderr.trim());
    }
    parse_worktree_list_porcelain(&String::from_utf8_lossy(&output.stdout), root)
}

fn parse_worktree_list_porcelain(
    stdout: &str,
    main_root: &Path,
) -> anyhow::Result<Vec<ExistingWorktree>> {
    let mut worktrees = Vec::new();
    let mut current_path: Option<PathBuf> = None;
    let mut current_branch: Option<String> = None;

    let mut flush_current =
        |path: &mut Option<PathBuf>, branch: &mut Option<String>| -> anyhow::Result<()> {
            if let Some(root) = path.take() {
                let canonical = root.canonicalize().unwrap_or(root.clone());
                worktrees.push(ExistingWorktree {
                    is_main: canonical == main_root,
                    root,
                    branch: branch.take(),
                });
            }
            Ok(())
        };

    for line in stdout.lines() {
        if line.trim().is_empty() {
            flush_current(&mut current_path, &mut current_branch)?;
            continue;
        }
        if let Some(path) = line.strip_prefix("worktree ") {
            flush_current(&mut current_path, &mut current_branch)?;
            current_path = Some(PathBuf::from(path.trim()));
            continue;
        }
        if let Some(branch) = line.strip_prefix("branch ") {
            current_branch = Some(normalize_branch_name(branch.trim()));
        }
    }

    flush_current(&mut current_path, &mut current_branch)?;

    if !worktrees.iter().any(|wt| wt.is_main) {
        anyhow::bail!(
            "git worktree list did not include the main repo path {}",
            main_root.display()
        );
    }

    Ok(worktrees)
}

fn normalize_branch_name(branch: &str) -> String {
    branch
        .strip_prefix("refs/heads/")
        .unwrap_or(branch)
        .to_string()
}

fn validate_migration_targets(
    worktrees: &[ExistingWorktree],
    repo_name: &str,
) -> anyhow::Result<()> {
    for wt in worktrees {
        if !wt.is_main && wt.branch.is_none() {
            anyhow::bail!(
                "Linked worktree at {} is detached; migrate in-place requires branch-backed worktrees",
                wt.root.display()
            );
        }
        let child = wt.root.join(repo_name);
        if child.exists() {
            anyhow::bail!(
                "Child directory already exists: {}. Migration may have already run.",
                child.display()
            );
        }
    }
    Ok(())
}

fn migrate_single_worktree_root(root: &Path, repo_name: &str) -> anyhow::Result<()> {
    let to_move = collect_paths_to_move(root, repo_name)?;
    let tmp = root.join("_migrate_tmp");
    std::fs::create_dir_all(&tmp)?;

    for src in &to_move {
        let dest = tmp.join(src.file_name().unwrap());
        std::fs::rename(src, dest)
            .map_err(|e| anyhow::anyhow!("Failed to move {}: {}", src.display(), e))?;
    }

    let child = root.join(repo_name);
    std::fs::rename(&tmp, &child).map_err(|e| {
        anyhow::anyhow!(
            "Failed to rename _migrate_tmp to {}: {}",
            child.display(),
            e
        )
    })?;
    Ok(())
}

fn run_worktree_repairs(
    worktrees: &[ExistingWorktree],
    repo_name: &str,
) -> anyhow::Result<Vec<ProcessOutput>> {
    let main = worktrees
        .iter()
        .find(|wt| wt.is_main)
        .ok_or_else(|| anyhow::anyhow!("Missing main worktree entry"))?;
    let main_child = main.root.join(repo_name);
    let mut cmd = std::process::Command::new("git");
    cmd.args(["worktree", "repair"]).current_dir(&main_child);
    for wt in worktrees.iter().filter(|wt| !wt.is_main) {
        cmd.arg(wt.root.join(repo_name));
    }

    let output = cmd.output().map_err(|e| {
        anyhow::anyhow!(
            "Failed to run git worktree repair in {}: {}",
            main_child.display(),
            e
        )
    })?;

    Ok(vec![output])
}

fn create_root_gripspace_metadata(
    root: &Path,
    linked_worktrees: &[ExistingWorktree],
) -> anyhow::Result<()> {
    let gitgrip_dir = root.join(".gitgrip");
    std::fs::create_dir_all(&gitgrip_dir)?;
    let stale_child_marker = gitgrip_dir.join("griptree.json");
    if stale_child_marker.exists() {
        let _ = std::fs::remove_file(&stale_child_marker);
    }

    let mut griptrees = MigratedGriptreesList::default();
    for wt in linked_worktrees {
        let branch = wt.branch.as_ref().ok_or_else(|| {
            anyhow::anyhow!("Missing branch for linked worktree {}", wt.root.display())
        })?;
        griptrees.griptrees.insert(
            branch.clone(),
            MigratedGriptreeEntry {
                path: wt.root.to_string_lossy().to_string(),
                branch: branch.clone(),
                locked: false,
                lock_reason: None,
            },
        );
    }

    let griptrees_json = serde_json::to_string_pretty(&griptrees)?;
    std::fs::write(gitgrip_dir.join("griptrees.json"), griptrees_json)?;
    Ok(())
}

fn create_root_gripspace_manifest(
    root: &Path,
    repo_name: &str,
    revision: &str,
) -> anyhow::Result<()> {
    let repo_child = root.join(repo_name);
    let remote_url = std::process::Command::new("git")
        .args(["remote", "get-url", "origin"])
        .current_dir(&repo_child)
        .output()
        .ok()
        .filter(|out| out.status.success())
        .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string())
        .filter(|s| !s.is_empty());

    let manifest_dir = manifest_paths::main_space_dir(root);
    std::fs::create_dir_all(&manifest_dir)?;

    let repo = RepoConfig {
        url: remote_url,
        remote: None,
        path: format!("./{}", repo_name),
        revision: Some(revision.to_string()),
        target: None,
        sync_remote: None,
        push_remote: None,
        copyfile: None,
        linkfile: None,
        platform: None,
        reference: false,
        groups: vec![],
        agent: None,
        clone_strategy: None,
    };

    let manifest = Manifest {
        version: 2,
        remotes: None,
        gripspaces: None,
        manifest: None,
        repos: HashMap::from([(repo_name.to_string(), repo)]),
        settings: ManifestSettings {
            pr_prefix: "[cross-repo]".to_string(),
            merge_strategy: MergeStrategy::default(),
            revision: Some(revision.to_string()),
            target: None,
            sync_remote: None,
            push_remote: None,
            clone_strategy: CloneStrategy::default(),
        },
        workspace: None,
    };

    let yaml = format!(
        "# Generated by gr migrate in-place\n{}",
        serde_yaml::to_string(&manifest)?
    );
    std::fs::write(manifest_dir.join("gripspace.yml"), yaml)?;
    Ok(())
}

fn create_linked_griptree_metadata(
    main_root: &Path,
    worktree: &ExistingWorktree,
    repo_name: &str,
) -> anyhow::Result<()> {
    let branch = worktree.branch.as_ref().ok_or_else(|| {
        anyhow::anyhow!(
            "Missing branch for linked worktree {}",
            worktree.root.display()
        )
    })?;
    let gitgrip_dir = worktree.root.join(".gitgrip");
    std::fs::create_dir_all(&gitgrip_dir)?;
    std::fs::write(gitgrip_dir.join("state.json"), "{}")?;

    let mut config = GriptreeConfig::new(branch, &worktree.root.to_string_lossy());
    config
        .repo_upstreams
        .insert(repo_name.to_string(), format!("origin/{}", branch));
    config.save(&gitgrip_dir.join("griptree.json"))?;

    let child = worktree.root.join(repo_name);
    let main_child = main_root.join(repo_name);
    let pointer = GriptreePointer {
        main_workspace: main_root.to_string_lossy().to_string(),
        branch: branch.clone(),
        locked: false,
        created_at: Some(Utc::now()),
        repos: vec![GriptreeRepoInfo {
            name: repo_name.to_string(),
            original_branch: branch.clone(),
            is_reference: false,
            worktree_name: None,
            worktree_path: Some(child.to_string_lossy().to_string()),
            main_repo_path: Some(main_child.to_string_lossy().to_string()),
        }],
        manifest_branch: None,
        manifest_worktree_name: None,
    };
    let pointer_json = serde_json::to_string_pretty(&pointer)?;
    std::fs::write(worktree.root.join(".griptree"), pointer_json)?;
    Ok(())
}

fn worktree_repair_must_succeed(repair: &ProcessOutput) -> anyhow::Result<()> {
    if repair.status.success() {
        return Ok(());
    }
    let stderr = String::from_utf8_lossy(&repair.stderr);
    anyhow::bail!("git worktree repair exited non-zero: {}", stderr.trim());
}

/// Derive the repo name from `git remote get-url origin`, falling back to dir name.
/// "git@github.com:GetConversa/conversa-app.git" → "conversa-app"
fn derive_repo_name_from_remote(repo: &Path) -> String {
    let result = std::process::Command::new("git")
        .args(["remote", "get-url", "origin"])
        .current_dir(repo)
        .output();

    if let Ok(out) = result {
        if out.status.success() {
            let url = String::from_utf8_lossy(&out.stdout).trim().to_string();
            if !url.is_empty() {
                // SSH: git@github.com:org/repo.git  → after last '/' or ':'
                // HTTPS: https://github.com/org/repo.git → after last '/'
                let base = url.rsplit(['/', ':']).next().unwrap_or(&url).to_string();
                // Strip .git suffix
                return base.strip_suffix(".git").unwrap_or(&base).to_string();
            }
        }
    }

    // Fallback: use the directory name
    repo.file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string()
}

/// Check that git >= 2.30 is available (required for `git worktree repair`).
fn check_git_version_for_repair() -> anyhow::Result<()> {
    let out = std::process::Command::new("git")
        .arg("--version")
        .output()
        .map_err(|_| anyhow::anyhow!("git not found — is git installed?"))?;

    let version_str = String::from_utf8_lossy(&out.stdout);
    // "git version 2.43.0 (Apple Git-115)" → parse major.minor
    let parts: Vec<u32> = version_str
        .split_whitespace()
        .find(|s| s.contains('.'))
        .unwrap_or("0.0")
        .split('.')
        .take(2)
        .filter_map(|s| s.parse().ok())
        .collect();

    let (major, minor) = (
        parts.first().copied().unwrap_or(0),
        parts.get(1).copied().unwrap_or(0),
    );
    if major < 2 || (major == 2 && minor < 30) {
        anyhow::bail!(
            "git 2.30+ required for `git worktree repair` (found git {}.{}). \
             Please upgrade git.",
            major,
            minor
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn run_git(dir: &Path, args: &[&str]) -> ProcessOutput {
        std::process::Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .unwrap_or_else(|e| panic!("failed to run git {:?}: {}", args, e))
    }

    fn assert_git_ok(dir: &Path, args: &[&str]) -> ProcessOutput {
        let output = run_git(dir, args);
        assert!(
            output.status.success(),
            "git {:?} failed\nstdout:\n{}\nstderr:\n{}",
            args,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        output
    }

    fn normalize_git_path(path: &Path) -> String {
        path.canonicalize()
            .unwrap()
            .to_string_lossy()
            .replace('\\', "/")
            .replace("//?/", "")
    }

    #[test]
    fn test_generate_manifest_yaml() {
        let repos = vec![
            ("GetConversa".to_string(), "conversa-app".to_string()),
            ("GetConversa".to_string(), "blessed-sound".to_string()),
        ];
        let yaml = generate_manifest_yaml(&repos, "synapt-dev", "consult-conversa");
        assert!(yaml.contains("version: 2"));
        assert!(yaml.contains("conversa-app:"));
        assert!(yaml.contains("blessed-sound:"));
        assert!(yaml.contains("GetConversa/conversa-app.git"));
        assert!(yaml.contains("consult-conversa-config:"));
        assert!(yaml.contains("clone_strategy: clone"));
    }

    #[test]
    fn test_generate_claude_md() {
        let repos = vec![("org".to_string(), "myrepo".to_string())];
        let agents = vec![AgentSpec {
            name: "atlas".to_string(),
            role: "research".to_string(),
            model: "claude-opus-4-6".to_string(),
            tool: "claude".to_string(),
        }];
        let md = generate_claude_md(&repos, "myproject", &agents);
        assert!(md.contains("# myproject"));
        assert!(md.contains("| **myrepo**"));
        assert!(md.contains("| **atlas**"));
        assert!(!md.contains("Premium Features"));
        assert!(!md.contains("recall_identity"));
    }

    #[test]
    fn test_generate_agents_toml_with_agents() {
        let agents = vec![AgentSpec {
            name: "apollo".to_string(),
            role: "implementation".to_string(),
            model: "claude-sonnet-4-6".to_string(),
            tool: "claude".to_string(),
        }];
        let toml = generate_agents_toml("myproject", &agents);
        assert!(toml.contains("[agents.apollo]"));
        assert!(toml.contains("role = \"implementation\""));
        assert!(toml.contains("model = \"claude-sonnet-4-6\""));
        assert!(toml.contains("config/prompts/apollo.md"));
    }

    #[test]
    fn test_generate_agents_toml_empty() {
        let toml = generate_agents_toml("myproject", &[]);
        assert!(toml.contains("session_name = \"myproject\""));
        assert!(toml.contains("# Add agents here:"));
    }

    #[test]
    fn test_migration_keep_names_includes_root_env() {
        let keep = migration_keep_names();
        assert!(keep.contains(".synapt"));
        assert!(keep.contains(".claude"));
        assert!(keep.contains(".env"));
        assert!(keep.contains("_migrate_tmp"));
    }

    #[test]
    fn test_worktree_repair_failure_is_error() {
        let output = std::process::Command::new("git")
            .args(["worktree", "definitely-not-a-subcommand"])
            .output()
            .expect("git should be runnable in test environment");
        assert!(worktree_repair_must_succeed(&output).is_err());
    }

    #[test]
    fn test_parse_worktree_list_marks_main_and_branch_names() {
        let main = PathBuf::from("/tmp/conversa")
            .canonicalize()
            .unwrap_or_else(|_| PathBuf::from("/tmp/conversa"));
        let porcelain = "\
worktree /tmp/conversa
HEAD deadbeef
branch refs/heads/main

worktree /tmp/conversa-dev
HEAD feedface
branch refs/heads/feat/dev
";

        let worktrees = parse_worktree_list_porcelain(porcelain, &main).unwrap();
        assert_eq!(worktrees.len(), 2);
        assert!(worktrees[0].is_main);
        assert_eq!(worktrees[0].branch.as_deref(), Some("main"));
        assert!(!worktrees[1].is_main);
        assert_eq!(worktrees[1].branch.as_deref(), Some("feat/dev"));
    }

    #[tokio::test]
    async fn test_migrate_in_place_converts_linked_worktrees_into_griptrees() {
        let temp = TempDir::new().unwrap();
        let main_root = temp.path().join("conversa");
        let linked_root = temp.path().join("conversa-dev");
        std::fs::create_dir_all(&main_root).unwrap();

        assert_git_ok(&main_root, &["init", "-b", "main"]);
        assert_git_ok(&main_root, &["config", "user.name", "Test User"]);
        assert_git_ok(&main_root, &["config", "user.email", "test@example.com"]);
        std::fs::write(main_root.join("README.md"), "hello\n").unwrap();
        std::fs::write(main_root.join(".env"), "FOO=bar\n").unwrap();
        assert_git_ok(&main_root, &["add", "."]);
        assert_git_ok(&main_root, &["commit", "-m", "init"]);
        assert_git_ok(
            &main_root,
            &[
                "remote",
                "add",
                "origin",
                "git@github.com:GetConversa/conversa-app.git",
            ],
        );

        assert_git_ok(
            &main_root,
            &[
                "worktree",
                "add",
                linked_root.to_str().unwrap(),
                "-b",
                "feat/dev",
            ],
        );
        std::fs::create_dir_all(linked_root.join(".claude")).unwrap();

        run_migrate_in_place(Some(main_root.to_str().unwrap()), false, false)
            .await
            .unwrap();

        let repo_name = "conversa-app";
        let main_child = main_root.join(repo_name);
        let linked_child = linked_root.join(repo_name);

        assert!(main_child.exists(), "main child repo should exist");
        assert!(linked_child.exists(), "linked child repo should exist");
        assert!(
            main_root.join(".env").exists(),
            ".env should stay at main root"
        );
        assert!(
            linked_root.join(".claude").exists(),
            ".claude should stay at linked worktree root"
        );

        let griptrees: MigratedGriptreesList = serde_json::from_str(
            &std::fs::read_to_string(main_root.join(".gitgrip").join("griptrees.json")).unwrap(),
        )
        .unwrap();
        let entry = griptrees.griptrees.get("feat/dev").unwrap();
        assert_eq!(
            PathBuf::from(&entry.path).canonicalize().unwrap(),
            linked_root.canonicalize().unwrap()
        );

        let griptree_cfg = GriptreeConfig::load_from_workspace(&linked_root)
            .unwrap()
            .unwrap();
        assert_eq!(griptree_cfg.branch, "feat/dev");

        let manifest_path = manifest_paths::main_space_dir(&main_root).join("gripspace.yml");
        assert!(
            manifest_path.exists(),
            "root manifest should exist after migration"
        );
        let manifest = crate::core::manifest::Manifest::load(&manifest_path).unwrap();
        let repo = manifest.repos.get(repo_name).unwrap();
        assert_eq!(repo.path, format!("./{}", repo_name));
        assert_eq!(repo.revision.as_deref(), Some("main"));
        assert_eq!(
            repo.url.as_deref(),
            Some("git@github.com:GetConversa/conversa-app.git")
        );

        let pointer = GriptreePointer::load(&linked_root.join(".griptree")).unwrap();
        assert_eq!(
            PathBuf::from(pointer.main_workspace)
                .canonicalize()
                .unwrap(),
            main_root.canonicalize().unwrap()
        );
        assert_eq!(pointer.branch, "feat/dev");
        assert_eq!(pointer.repos.len(), 1);
        assert_eq!(pointer.repos[0].name, repo_name);
        assert_eq!(
            PathBuf::from(pointer.repos[0].worktree_path.as_ref().unwrap())
                .canonicalize()
                .unwrap(),
            linked_child.canonicalize().unwrap()
        );
        assert_eq!(
            PathBuf::from(pointer.repos[0].main_repo_path.as_ref().unwrap())
                .canonicalize()
                .unwrap(),
            main_child.canonicalize().unwrap()
        );

        assert_git_ok(&main_child, &["status", "--short"]);
        let linked_branch = assert_git_ok(&linked_child, &["rev-parse", "--abbrev-ref", "HEAD"]);
        assert_eq!(
            String::from_utf8_lossy(&linked_branch.stdout).trim(),
            "feat/dev"
        );

        let worktree_list = assert_git_ok(&main_child, &["worktree", "list", "--porcelain"]);
        let worktree_stdout = String::from_utf8_lossy(&worktree_list.stdout).replace('\\', "/");
        assert!(worktree_stdout.contains(&normalize_git_path(&linked_child)));
    }
}