gitgrip 0.18.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
//! Operational integration tests for griptree commands.
//!
//! Tests `gr tree add/list/remove/lock/unlock` against real git worktrees
//! in temporary workspaces. All tests run offline.

mod common;

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

use assert_cmd::Command;
use predicates::str::contains;
use serde_yaml::Value;

use common::assertions;
use common::fixtures::{write_griptree_config, WorkspaceBuilder};
use common::git_helpers;

fn set_default_branch(manifest_path: &Path, repo: &str, branch: &str) {
    let content = fs::read_to_string(manifest_path).unwrap();
    let mut doc: Value = serde_yaml::from_str(&content).unwrap();
    assert!(
        !doc["repos"][repo].is_null(),
        "repo {} missing from manifest",
        repo
    );
    doc["repos"][repo]["default_branch"] = Value::String(branch.to_string());
    fs::write(manifest_path, serde_yaml::to_string(&doc).unwrap()).unwrap();
}

// ── Tree Add ──────────────────────────────────────────────────────

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

    let result = gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "feat/new-feature",
    );

    assert!(
        result.is_ok(),
        "tree add should succeed: {:?}",
        result.err()
    );

    // Griptree directory should be created as sibling to workspace
    let tree_path = ws.workspace_root.parent().unwrap().join("feat-new-feature");
    assertions::assert_file_exists(&tree_path);

    // .griptree pointer file should exist
    let pointer_path = tree_path.join(".griptree");
    assertions::assert_file_exists(&pointer_path);

    // Verify pointer contents
    let pointer: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&pointer_path).unwrap()).unwrap();
    assert_eq!(pointer["branch"], "feat/new-feature");
    assert_eq!(
        pointer["mainWorkspace"],
        ws.workspace_root.to_string_lossy().as_ref()
    );
    assert!(!pointer["locked"].as_bool().unwrap());

    // griptrees.json registry should be updated
    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    assertions::assert_file_exists(&registry_path);
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(
        registry["griptrees"]["feat/new-feature"].is_object(),
        "registry should contain the new griptree"
    );

    // Each repo should have a worktree directory in the griptree
    assertions::assert_file_exists(&tree_path.join("app"));
    assertions::assert_file_exists(&tree_path.join("lib"));

    // Worktrees should be on the new branch
    assertions::assert_on_branch(&tree_path.join("app"), "feat/new-feature");
    assertions::assert_on_branch(&tree_path.join("lib"), "feat/new-feature");
}

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

    git_helpers::create_branch(&ws.repo_path("lib"), "dev");
    git_helpers::commit_file(&ws.repo_path("lib"), "dev.txt", "dev", "Add dev");
    git_helpers::push_branch(&ws.repo_path("lib"), "origin", "dev");
    git_helpers::checkout(&ws.repo_path("lib"), "main");

    let manifest_path =
        gitgrip::core::manifest_paths::resolve_gripspace_manifest_path(&ws.workspace_root)
            .expect("workspace manifest path should resolve");
    set_default_branch(&manifest_path, "lib", "dev");
    let manifest = ws.load_manifest();

    let result =
        gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/upstream");
    assert!(
        result.is_ok(),
        "tree add should succeed: {:?}",
        result.err()
    );

    let tree_path = ws.workspace_root.parent().unwrap().join("feat-upstream");
    let config_path = tree_path.join(".gitgrip").join("griptree.json");
    let config: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(&config_path).unwrap()).unwrap();

    assert_eq!(config["repoUpstreams"]["app"], "origin/main");
    assert_eq!(config["repoUpstreams"]["lib"], "origin/dev");

    assert_eq!(
        git_helpers::branch_upstream(&tree_path.join("app"), "feat/upstream"),
        Some("origin/main".to_string())
    );
    assert_eq!(
        git_helpers::branch_upstream(&tree_path.join("lib"), "feat/upstream"),
        Some("origin/dev".to_string())
    );
}

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

    let result = gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "feat/with-manifest",
    );

    assert!(
        result.is_ok(),
        "tree add with manifest repo should succeed: {:?}",
        result.err()
    );

    let tree_path = ws
        .workspace_root
        .parent()
        .unwrap()
        .join("feat-with-manifest");

    // Manifest worktree should be created in griptree
    let tree_manifest_dir = tree_path.join(".gitgrip").join("spaces").join("main");
    assertions::assert_file_exists(&tree_manifest_dir);

    // A supported manifest filename should exist in the griptree manifest dir.
    assert!(
        gitgrip::core::manifest_paths::resolve_manifest_file_in_dir(&tree_manifest_dir).is_some()
    );

    // Pointer should reference the manifest worktree
    let pointer_path = tree_path.join(".griptree");
    let pointer: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&pointer_path).unwrap()).unwrap();
    assert!(
        pointer["manifestBranch"].is_string(),
        "pointer should have manifestBranch"
    );
}

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

    // Create first griptree
    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/dup")
        .expect("first tree add should succeed");

    // Attempt to create same griptree again
    let result =
        gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/dup");

    assert!(result.is_err(), "duplicate tree add should fail");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("already exists"),
        "error should mention 'already exists': {}",
        err
    );
}

// ── Tree List ──────────────────────────────────────────────────────

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

    let result = gitgrip::cli::commands::tree::run_tree_list(&ws.workspace_root);

    assert!(
        result.is_ok(),
        "tree list with no griptrees should succeed: {:?}",
        result.err()
    );
}

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

    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/listed")
        .expect("tree add should succeed");

    let result = gitgrip::cli::commands::tree::run_tree_list(&ws.workspace_root);

    assert!(
        result.is_ok(),
        "tree list should succeed: {:?}",
        result.err()
    );

    // Verify registry has the entry
    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(registry["griptrees"]["feat/listed"].is_object());
}

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

    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/listed")
        .expect("tree add should succeed");

    let tree_path = ws.workspace_root.parent().unwrap().join("feat-listed");

    let mut cmd = Command::cargo_bin("gr").expect("gr binary should build");
    cmd.current_dir(&tree_path)
        .args(["tree", "list"])
        .assert()
        .success()
        .stdout(contains("feat/listed"));
}

// ── Tree Remove ──────────────────────────────────────────────────

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

    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/removeme")
        .expect("tree add should succeed");

    let tree_path = ws.workspace_root.parent().unwrap().join("feat-removeme");
    assert!(
        tree_path.exists(),
        "griptree dir should exist before remove"
    );

    let result =
        gitgrip::cli::commands::tree::run_tree_remove(&ws.workspace_root, "feat/removeme", false);

    assert!(
        result.is_ok(),
        "tree remove should succeed: {:?}",
        result.err()
    );

    // Directory should be gone
    assert!(!tree_path.exists(), "griptree directory should be removed");

    // Registry should no longer contain the entry
    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(
        registry["griptrees"]["feat/removeme"].is_null(),
        "registry should not contain removed griptree"
    );
}

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

    gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "feat/remove-from-tree",
    )
    .expect("tree add should succeed");

    let tree_path = ws
        .workspace_root
        .parent()
        .unwrap()
        .join("feat-remove-from-tree");
    let result =
        gitgrip::cli::commands::tree::run_tree_remove(&tree_path, "feat/remove-from-tree", false);

    assert!(
        result.is_ok(),
        "tree remove from griptree workspace should succeed: {:?}",
        result.err()
    );
    assert!(!tree_path.exists(), "griptree directory should be removed");
}

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

    // Create the griptrees.json so remove has something to read
    let config_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    std::fs::write(&config_path, r#"{"griptrees":{}}"#).unwrap();

    let result =
        gitgrip::cli::commands::tree::run_tree_remove(&ws.workspace_root, "feat/ghost", false);

    assert!(result.is_err(), "removing nonexistent griptree should fail");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("not found"),
        "error should mention 'not found': {}",
        err
    );
}

// ── Tree Lock / Unlock ──────────────────────────────────────────

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

    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/locked")
        .expect("tree add should succeed");

    // Lock the griptree
    let lock_result = gitgrip::cli::commands::tree::run_tree_lock(
        &ws.workspace_root,
        "feat/locked",
        Some("important work"),
    );
    assert!(
        lock_result.is_ok(),
        "tree lock should succeed: {:?}",
        lock_result.err()
    );

    // Verify registry shows locked
    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(
        registry["griptrees"]["feat/locked"]["locked"]
            .as_bool()
            .unwrap(),
        "registry should show locked"
    );
    assert_eq!(
        registry["griptrees"]["feat/locked"]["lock_reason"],
        "important work"
    );

    // Verify .griptree pointer also shows locked
    let tree_path = ws.workspace_root.parent().unwrap().join("feat-locked");
    let pointer: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(tree_path.join(".griptree")).unwrap())
            .unwrap();
    assert!(
        pointer["locked"].as_bool().unwrap(),
        "pointer should show locked"
    );

    // Try to remove without force - should fail
    let remove_result =
        gitgrip::cli::commands::tree::run_tree_remove(&ws.workspace_root, "feat/locked", false);
    assert!(
        remove_result.is_err(),
        "removing locked griptree should fail"
    );
    let err = remove_result.unwrap_err().to_string();
    assert!(
        err.contains("locked"),
        "error should mention 'locked': {}",
        err
    );

    // Force remove should succeed
    let force_result =
        gitgrip::cli::commands::tree::run_tree_remove(&ws.workspace_root, "feat/locked", true);
    assert!(
        force_result.is_ok(),
        "force remove should succeed: {:?}",
        force_result.err()
    );
    assert!(
        !tree_path.exists(),
        "griptree dir should be removed after force"
    );
}

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

    gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "feat/lock-from-tree",
    )
    .expect("tree add should succeed");

    let tree_path = ws
        .workspace_root
        .parent()
        .unwrap()
        .join("feat-lock-from-tree");
    let lock_result =
        gitgrip::cli::commands::tree::run_tree_lock(&tree_path, "feat/lock-from-tree", None);
    assert!(
        lock_result.is_ok(),
        "tree lock from griptree workspace should succeed: {:?}",
        lock_result.err()
    );

    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(
        registry["griptrees"]["feat/lock-from-tree"]["locked"]
            .as_bool()
            .unwrap(),
        "registry should show locked after lock from griptree workspace"
    );
}

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

    gitgrip::cli::commands::tree::run_tree_add(&ws.workspace_root, &manifest, "feat/unlock-me")
        .expect("tree add should succeed");

    // Lock it
    gitgrip::cli::commands::tree::run_tree_lock(&ws.workspace_root, "feat/unlock-me", None)
        .expect("lock should succeed");

    // Unlock it
    let unlock_result =
        gitgrip::cli::commands::tree::run_tree_unlock(&ws.workspace_root, "feat/unlock-me");
    assert!(
        unlock_result.is_ok(),
        "unlock should succeed: {:?}",
        unlock_result.err()
    );

    // Verify registry shows unlocked
    let registry_path = ws.workspace_root.join(".gitgrip").join("griptrees.json");
    let registry: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&registry_path).unwrap()).unwrap();
    assert!(
        !registry["griptrees"]["feat/unlock-me"]["locked"]
            .as_bool()
            .unwrap(),
        "registry should show unlocked"
    );

    // Remove should now succeed
    let remove_result =
        gitgrip::cli::commands::tree::run_tree_remove(&ws.workspace_root, "feat/unlock-me", false);
    assert!(
        remove_result.is_ok(),
        "remove after unlock should succeed: {:?}",
        remove_result.err()
    );
}

// ── Tree Edge Cases ──────────────────────────────────────────────

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

    let result = gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "fix/bug/critical",
    );

    assert!(
        result.is_ok(),
        "tree add with nested slashes should succeed: {:?}",
        result.err()
    );

    // Branch slashes should become dashes in directory name
    let tree_path = ws.workspace_root.parent().unwrap().join("fix-bug-critical");
    assertions::assert_file_exists(&tree_path);
    assertions::assert_on_branch(&tree_path.join("app"), "fix/bug/critical");
}

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

    // Verify repos start on main
    assertions::assert_on_branch(&ws.repo_path("app"), "main");
    assertions::assert_on_branch(&ws.repo_path("lib"), "main");

    gitgrip::cli::commands::tree::run_tree_add(
        &ws.workspace_root,
        &manifest,
        "feat/no-side-effects",
    )
    .expect("tree add should succeed");

    // Original repos should still be on main
    assertions::assert_on_branch(&ws.repo_path("app"), "main");
    assertions::assert_on_branch(&ws.repo_path("lib"), "main");
}

// ── Tree Return ────────────────────────────────────────────────

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

    git_helpers::create_branch(&ws.repo_path("app"), "griptree-base");
    git_helpers::create_branch(&ws.repo_path("app"), "feat/return");

    write_griptree_config(&ws.workspace_root, "griptree-base", "app", "origin/main");

    let result = gitgrip::cli::commands::tree::run_tree_return(
        &ws.workspace_root,
        &manifest,
        &gitgrip::cli::commands::tree::TreeReturnOptions {
            base_override: None,
            no_sync: true,
            autostash: false,
            prune_branch: None,
            prune_current: false,
            prune_remote: false,
            force: false,
        },
    )
    .await;

    assert!(
        result.is_ok(),
        "tree return should succeed: {:?}",
        result.err()
    );
    assertions::assert_on_branch(&ws.repo_path("app"), "griptree-base");
}

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

    git_helpers::create_branch(&ws.repo_path("app"), "griptree-base");
    git_helpers::create_branch(&ws.repo_path("app"), "feat/prune");

    write_griptree_config(&ws.workspace_root, "griptree-base", "app", "origin/main");

    let result = gitgrip::cli::commands::tree::run_tree_return(
        &ws.workspace_root,
        &manifest,
        &gitgrip::cli::commands::tree::TreeReturnOptions {
            base_override: None,
            no_sync: true,
            autostash: false,
            prune_branch: None,
            prune_current: true,
            prune_remote: false,
            force: true,
        },
    )
    .await;

    assert!(
        result.is_ok(),
        "tree return should succeed: {:?}",
        result.err()
    );
    assert!(!git_helpers::branch_exists(
        &ws.repo_path("app"),
        "feat/prune"
    ));
    assertions::assert_on_branch(&ws.repo_path("app"), "griptree-base");
}