gitgrip 0.20.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
//! Agent command tests for gitgrip.
//!
//! Tests for the gr agent subcommands:
//! - context (markdown and JSON output)
//! - build
//! - test
//! - verify

mod common;

use std::fs;
use std::path::Path;

use common::fixtures::WorkspaceBuilder;

/// Helper to add per-repo agent config by inserting it into the repo's YAML block.
///
/// `repo_name` — the repo to add agent config to
/// `agent_yaml` — the agent block (indented 4 spaces under the repo key), e.g.:
///   `"      description: Test app\n      build: cargo build\n"`
fn add_repo_agent_config(workspace_root: &Path, repo_name: &str, agent_yaml: &str) {
    let manifest_path = workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main")
        .join("gripspace.yml");
    let content = fs::read_to_string(&manifest_path).unwrap();

    // Find the repo block and insert agent config after default_branch line
    let search = format!("  {}:\n", repo_name);
    let repo_start = content
        .find(&search)
        .unwrap_or_else(|| panic!("repo '{}' not found in manifest", repo_name));

    // Find the end of the default_branch line for this repo
    let after_repo = &content[repo_start..];
    let db_needle = "    default_branch: main\n";
    let db_offset = after_repo
        .find(db_needle)
        .expect("default_branch line not found");
    let insert_pos = repo_start + db_offset + db_needle.len();

    let mut new_content = String::with_capacity(content.len() + agent_yaml.len() + 20);
    new_content.push_str(&content[..insert_pos]);
    new_content.push_str("    agent:\n");
    new_content.push_str(agent_yaml);
    new_content.push_str(&content[insert_pos..]);

    fs::write(&manifest_path, new_content).unwrap();
}

/// Helper to append workspace-level agent config to the manifest.
fn add_workspace_agent_config(workspace_root: &Path, workspace_yaml: &str) {
    let manifest_path = workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main")
        .join("gripspace.yml");
    let mut content = fs::read_to_string(&manifest_path).unwrap();
    content.push_str(workspace_yaml);
    fs::write(&manifest_path, content).unwrap();
}

// ── Context Tests ────────────────────────────────────────────────

#[test]
fn test_agent_context_no_agent_config() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();
    let manifest = ws.load_manifest();

    // Should succeed even without any agent config
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        None,
        false,
    );
    assert!(
        result.is_ok(),
        "context should succeed without agent config: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_context_with_workspace_agent_config() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    description: "Test workspace"
    conventions:
      - "Use conventional commits"
      - "Never push to main"
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        None,
        false,
    );
    assert!(
        result.is_ok(),
        "context should succeed with workspace agent config: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_context_with_repo_agent_config() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_repo_agent_config(
        &ws.workspace_root,
        "app",
        "      description: \"Test application\"\n      language: rust\n      build: cargo build\n      test: cargo test\n",
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        None,
        false,
    );
    assert!(
        result.is_ok(),
        "context should succeed with repo agent config: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_context_json_output() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    description: "Test workspace"
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        None,
        true, // json mode
    );
    assert!(
        result.is_ok(),
        "context JSON should succeed: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_context_repo_filter() {
    let ws = WorkspaceBuilder::new()
        .add_repo("app")
        .add_repo("lib")
        .build();

    let manifest = ws.load_manifest();

    // Filter to a specific repo
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        Some("app"),
        false,
    );
    assert!(
        result.is_ok(),
        "context with repo filter should succeed: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_context_repo_filter_not_found() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();
    let manifest = ws.load_manifest();

    // Filter to nonexistent repo should fail
    let result = gitgrip::cli::commands::agent::run_agent_context(
        &ws.workspace_root,
        &manifest,
        Some("nonexistent"),
        false,
    );
    assert!(result.is_err(), "should fail for nonexistent repo");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("not found"),
        "error should mention not found: {}",
        err
    );
}

// ── Generate Context Tests ───────────────────────────────────────

#[test]
fn test_agent_generate_context_no_targets() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();
    let manifest = ws.load_manifest();

    // No targets configured — should succeed silently
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "generate-context should succeed with no targets: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_generate_context_raw_format() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    // Write a source file in the manifest content dir
    let manifests_dir = ws
        .workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main");
    fs::write(
        manifests_dir.join("CONTEXT.md"),
        "# Workspace Rules\nRule 1\nRule 2",
    )
    .unwrap();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    context_source: CONTEXT.md
    targets:
      - format: raw
        dest: AGENTS.md
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "raw format generation should succeed: {:?}",
        result.err()
    );

    let output = fs::read_to_string(ws.workspace_root.join("AGENTS.md")).unwrap();
    assert_eq!(output, "# Workspace Rules\nRule 1\nRule 2");
}

#[test]
fn test_agent_generate_context_per_repo() {
    let ws = WorkspaceBuilder::new()
        .add_repo("app")
        .add_repo("lib")
        .build();

    // Add agent config to both repos
    add_repo_agent_config(
        &ws.workspace_root,
        "app",
        "      description: \"Main application\"\n      language: rust\n      build: cargo build\n",
    );
    add_repo_agent_config(
        &ws.workspace_root,
        "lib",
        "      description: \"Shared library\"\n      language: rust\n      test: cargo test\n",
    );

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    targets:
      - format: opencode
        dest: ".opencode/skill/{repo}/SKILL.md"
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "per-repo generation should succeed: {:?}",
        result.err()
    );

    // Check that files were generated for both repos
    let app_skill = ws.workspace_root.join(".opencode/skill/app/SKILL.md");
    let lib_skill = ws.workspace_root.join(".opencode/skill/lib/SKILL.md");

    assert!(app_skill.exists(), "app skill file should exist");
    assert!(lib_skill.exists(), "lib skill file should exist");

    let app_content = fs::read_to_string(&app_skill).unwrap();
    assert!(app_content.contains("name: app"), "should have frontmatter");
    assert!(
        app_content.contains("Main application"),
        "should have description"
    );
    assert!(
        app_content.contains("Language: rust"),
        "should have language"
    );

    let lib_content = fs::read_to_string(&lib_skill).unwrap();
    assert!(lib_content.contains("name: lib"), "should have frontmatter");
    assert!(
        lib_content.contains("Shared library"),
        "should have description"
    );
}

#[test]
fn test_agent_generate_context_compose_with() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    let manifests_dir = ws
        .workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main");
    fs::write(manifests_dir.join("CONTEXT.md"), "# Base Context").unwrap();

    // Write a compose_with file in the workspace (workspace-relative path)
    fs::write(
        ws.workspace_root.join("PRIVATE_RULES.md"),
        "# Private Rules\nDo not share.",
    )
    .unwrap();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    context_source: CONTEXT.md
    targets:
      - format: raw
        dest: CLAUDE.md
        compose_with:
          - PRIVATE_RULES.md
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "compose_with should succeed: {:?}",
        result.err()
    );

    let output = fs::read_to_string(ws.workspace_root.join("CLAUDE.md")).unwrap();
    assert!(
        output.contains("# Base Context"),
        "should contain base content"
    );
    assert!(
        output.contains("# Private Rules"),
        "should contain composed content"
    );
}

#[test]
fn test_agent_generate_context_dry_run() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    let manifests_dir = ws
        .workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main");
    fs::write(manifests_dir.join("CONTEXT.md"), "content").unwrap();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    context_source: CONTEXT.md
    targets:
      - format: raw
        dest: DRY_RUN_OUTPUT.md
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        true, // dry_run
        false,
    );
    assert!(result.is_ok(), "dry-run should succeed: {:?}", result.err());

    // File should NOT exist in dry-run mode
    assert!(
        !ws.workspace_root.join("DRY_RUN_OUTPUT.md").exists(),
        "dry-run should not write files"
    );
}

#[test]
fn test_agent_generate_context_claude_format() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_repo_agent_config(
        &ws.workspace_root,
        "app",
        "      description: \"Test app\"\n      language: typescript\n      build: pnpm build\n      test: pnpm test\n",
    );

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    targets:
      - format: claude
        dest: ".claude/skills/{repo}/SKILL.md"
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "claude format should succeed: {:?}",
        result.err()
    );

    let skill_path = ws.workspace_root.join(".claude/skills/app/SKILL.md");
    assert!(skill_path.exists(), "claude skill file should exist");

    let content = fs::read_to_string(&skill_path).unwrap();
    assert!(
        content.starts_with("---\n"),
        "should start with frontmatter"
    );
    assert!(
        content.contains("name: app"),
        "should have name in frontmatter"
    );
    assert!(content.contains("Test app"), "should have description");
    assert!(
        content.contains("Language: typescript"),
        "should have language"
    );
}

#[test]
fn test_agent_generate_context_cursor_format() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    let manifests_dir = ws
        .workspace_root
        .join(".gitgrip")
        .join("spaces")
        .join("main");
    fs::write(
        manifests_dir.join("CONTEXT.md"),
        "# Project Rules\n## Code Style\nUse 2-space indent\n",
    )
    .unwrap();

    add_workspace_agent_config(
        &ws.workspace_root,
        r#"workspace:
  agent:
    context_source: CONTEXT.md
    targets:
      - format: cursor
        dest: .cursorrules
"#,
    );

    let manifest = ws.load_manifest();
    let result = gitgrip::cli::commands::agent::run_agent_generate_context(
        &ws.workspace_root,
        &manifest,
        false,
        true,
    );
    assert!(
        result.is_ok(),
        "cursor format should succeed: {:?}",
        result.err()
    );

    let content = fs::read_to_string(ws.workspace_root.join(".cursorrules")).unwrap();
    assert!(
        !content.contains("# "),
        "heading markers should be stripped"
    );
    assert!(
        content.contains("Project Rules"),
        "heading text should remain"
    );
    assert!(
        content.contains("Code Style"),
        "subheading text should remain"
    );
}

// ── Build Tests ──────────────────────────────────────────────────

#[test]
fn test_agent_build_runs_command() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    let marker = ws.workspace_root.join("app").join("build-marker.txt");
    let agent_yaml = format!("      build: echo built > \"{}\"\n", marker.display());
    add_repo_agent_config(&ws.workspace_root, "app", &agent_yaml);

    let manifest = ws.load_manifest();
    let result =
        gitgrip::cli::commands::agent::run_agent_build(&ws.workspace_root, &manifest, Some("app"));
    assert!(result.is_ok(), "build should succeed: {:?}", result.err());
    assert!(
        marker.exists(),
        "build command should have created marker file"
    );
}

#[test]
fn test_agent_build_fails_on_error() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_repo_agent_config(&ws.workspace_root, "app", "      build: exit 1\n");

    let manifest = ws.load_manifest();
    let result =
        gitgrip::cli::commands::agent::run_agent_build(&ws.workspace_root, &manifest, Some("app"));
    assert!(result.is_err(), "build should fail when command fails");
}

#[test]
fn test_agent_build_no_config_skips() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();
    let manifest = ws.load_manifest();

    // No agent config — should succeed silently (no repos to build)
    let result =
        gitgrip::cli::commands::agent::run_agent_build(&ws.workspace_root, &manifest, None);
    assert!(
        result.is_ok(),
        "build with no config should succeed: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_build_specific_repo_no_config_errors() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();
    let manifest = ws.load_manifest();

    // Naming a specific repo with no agent.build should error
    let result =
        gitgrip::cli::commands::agent::run_agent_build(&ws.workspace_root, &manifest, Some("app"));
    assert!(
        result.is_err(),
        "build should error when named repo has no build command"
    );
}

// ── Test Tests ───────────────────────────────────────────────────

#[test]
fn test_agent_test_runs_command() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    let marker = ws.workspace_root.join("app").join("test-marker.txt");
    let agent_yaml = format!("      test: echo tested > \"{}\"\n", marker.display());
    add_repo_agent_config(&ws.workspace_root, "app", &agent_yaml);

    let manifest = ws.load_manifest();
    let result =
        gitgrip::cli::commands::agent::run_agent_test(&ws.workspace_root, &manifest, Some("app"));
    assert!(result.is_ok(), "test should succeed: {:?}", result.err());
    assert!(
        marker.exists(),
        "test command should have created marker file"
    );
}

// ── Verify Tests ─────────────────────────────────────────────────

#[test]
fn test_agent_verify_runs_all_checks() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_repo_agent_config(
        &ws.workspace_root,
        "app",
        "      build: \"true\"\n      test: \"true\"\n      lint: \"true\"\n",
    );

    let manifest = ws.load_manifest();
    let result =
        gitgrip::cli::commands::agent::run_agent_verify(&ws.workspace_root, &manifest, Some("app"));
    assert!(
        result.is_ok(),
        "verify should succeed when all checks pass: {:?}",
        result.err()
    );
}

#[test]
fn test_agent_verify_reports_failures() {
    let ws = WorkspaceBuilder::new().add_repo("app").build();

    add_repo_agent_config(
        &ws.workspace_root,
        "app",
        "      build: \"true\"\n      test: exit 1\n      lint: \"true\"\n",
    );

    let manifest = ws.load_manifest();
    let result =
        gitgrip::cli::commands::agent::run_agent_verify(&ws.workspace_root, &manifest, Some("app"));
    assert!(result.is_err(), "verify should fail when a check fails");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("1 verification"),
        "should report 1 failure: {}",
        err
    );
}