nils-git-cli 1.21.9

CLI crate for nils-git-cli in the nils-cli workspace.
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
use crate::common;
use common::{GitCliHarness, git, init_repo};
use nils_test_support::cmd::{CmdOutput, run_with};
use pretty_assertions::assert_eq;
use serde_json::Value;
use std::fs;
use std::path::Path;

fn run_with_agent_home(
    harness: &GitCliHarness,
    cwd: &Path,
    agent_home: &Path,
    args: &[&str],
) -> CmdOutput {
    let agent_home = agent_home.to_string_lossy().to_string();
    let options = harness.cmd_options(cwd).with_env("AGENT_HOME", &agent_home);
    run_with(&harness.git_cli_bin(), args, &options)
}

fn parse_json(output: &CmdOutput) -> Value {
    serde_json::from_str(output.stdout_text().trim()).expect("valid json output")
}

#[test]
fn worktree_add_creates_deterministic_agent_home_path_and_lists_json() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-one",
            "--from",
            "main",
            "--format",
            "json",
        ],
    );

    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    assert_eq!(add.stderr_text(), "");

    let add_json = parse_json(&add);
    assert_eq!(add_json["schema_version"], "cli.git-cli.worktree.add.v1");
    assert_eq!(add_json["ok"], true);
    assert_eq!(add_json["data"]["slug"], "topic-one");
    assert_eq!(
        add_json["data"]["kind"], "feature",
        "default kind is feature"
    );
    assert_eq!(add_json["data"]["branch"], "feat/topic-one");

    let repo_key = add_json["data"]["repo_key"].as_str().expect("repo key");
    let path = add_json["data"]["path"].as_str().expect("path");
    let canonical_agent_home = agent_home
        .path()
        .canonicalize()
        .expect("canonical agent home");
    let canonical_agent_home_text = canonical_agent_home.to_string_lossy().to_string();
    let expected_path = canonical_agent_home
        .join("worktrees")
        .join(repo_key)
        .join("topic-one");
    assert_eq!(
        add_json["data"]["agent_home"].as_str(),
        Some(canonical_agent_home_text.as_str())
    );
    assert_eq!(path, expected_path.to_string_lossy());
    assert!(expected_path.exists(), "worktree path should exist");

    let porcelain = git(repo.path(), &["worktree", "list", "--porcelain"]);
    assert!(porcelain.contains("branch refs/heads/feat/topic-one"));
    assert!(porcelain.contains(path));

    let list = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "list", "--format", "json"],
    );
    assert_eq!(list.code, 0, "stderr: {}", list.stderr_text());
    let list_json = parse_json(&list);
    assert_eq!(list_json["schema_version"], "cli.git-cli.worktree.list.v1");

    let entries = list_json["data"]["entries"]
        .as_array()
        .expect("entries array");
    let managed = entries
        .iter()
        .find(|entry| entry["path"].as_str() == Some(path))
        .expect("managed worktree listed");
    assert_eq!(managed["branch"], "feat/topic-one");
    assert_eq!(managed["managed"], true);
}

#[test]
fn worktree_add_kind_bug_uses_fix_branch_prefix() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-bug",
            "--from",
            "main",
            "--kind",
            "bug",
            "--format",
            "json",
        ],
    );

    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    assert_eq!(add.stderr_text(), "");

    let add_json = parse_json(&add);
    assert_eq!(add_json["ok"], true);
    assert_eq!(add_json["data"]["slug"], "topic-bug");
    assert_eq!(add_json["data"]["kind"], "bug");
    assert_eq!(
        add_json["data"]["branch"], "fix/topic-bug",
        "kind=bug derives the fix/ prefix forge-cli's --kind bug expects"
    );

    let path = add_json["data"]["path"].as_str().expect("path");
    let porcelain = git(repo.path(), &["worktree", "list", "--porcelain"]);
    assert!(porcelain.contains("branch refs/heads/fix/topic-bug"));
    assert!(porcelain.contains(path));
}

#[test]
fn worktree_add_rejects_unknown_kind() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree", "add", "topic-x", "--kind", "nope", "--from", "main", "--format", "json",
        ],
    );

    assert_ne!(add.code, 0, "unknown --kind must fail");
    let add_json = parse_json(&add);
    assert_eq!(add_json["ok"], false);
    assert_eq!(add_json["error"]["code"], "invalid-kind");
}

#[test]
fn worktree_add_existing_slug_fails_with_json_error() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let first = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "add", "topic-one", "--from", "main"],
    );
    assert_eq!(first.code, 0, "stderr: {}", first.stderr_text());

    let second = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-one",
            "--from",
            "main",
            "--format",
            "json",
        ],
    );

    assert_ne!(second.code, 0);
    assert_eq!(second.stderr_text(), "");
    let json = parse_json(&second);
    assert_eq!(json["schema_version"], "cli.git-cli.worktree.add.v1");
    assert_eq!(json["ok"], false);
    assert_eq!(json["error"]["code"], "branch-exists");
    assert!(
        json["error"]["message"]
            .as_str()
            .expect("message")
            .contains("feat/topic-one")
    );
}

#[test]
fn worktree_remove_parse_error_respects_json_format() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let output = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "remove", "--format", "json"],
    );

    assert_eq!(output.code, 64);
    assert_eq!(output.stderr_text(), "");
    let json = parse_json(&output);
    assert_eq!(json["schema_version"], "cli.git-cli.worktree.remove.v1");
    assert_eq!(json["ok"], false);
    assert_eq!(json["error"]["code"], "invalid-target-count");
}

#[test]
fn worktree_remove_with_branch_name_hints_slug_in_text_and_json() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    // A docs-kind worktree: branch `docs/topic-docs`, slug `topic-docs`.
    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-docs",
            "--from",
            "main",
            "--kind",
            "docs",
            "--format",
            "json",
        ],
    );
    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    let add_json = parse_json(&add);
    assert_eq!(add_json["data"]["branch"], "docs/topic-docs");

    // Passing the branch name (text mode) fails but points at the slug + path.
    let text = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "remove", "docs/topic-docs"],
    );
    assert_ne!(text.code, 0);
    let stderr = text.stderr_text();
    assert!(stderr.contains("hint:"), "stderr: {stderr}");
    assert!(stderr.contains("branch name"), "stderr: {stderr}");
    assert!(stderr.contains("slug 'topic-docs'"), "stderr: {stderr}");

    // The same mistake in JSON mode carries the hint on the error envelope.
    let json = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "remove", "docs/topic-docs", "--format", "json"],
    );
    assert_ne!(json.code, 0);
    let json = parse_json(&json);
    assert_eq!(json["error"]["code"], "worktree-not-found");
    let hint = json["error"]["hint"].as_str().expect("hint");
    assert!(hint.contains("slug 'topic-docs'"), "hint: {hint}");
}

#[test]
fn worktree_list_from_linked_worktree_resolves_primary_repo_root() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-one",
            "--from",
            "main",
            "--format",
            "json",
        ],
    );
    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    let add_json = parse_json(&add);
    let linked_path = add_json["data"]["path"].as_str().expect("path").to_string();
    let expected_repo_root = repo
        .path()
        .canonicalize()
        .expect("canonical repo")
        .to_string_lossy()
        .to_string();

    // Run `worktree list` from INSIDE the linked worktree. The managed layout
    // (repo_root / repo_key / managed flag) must reflect the PRIMARY worktree,
    // not the linked one we happen to stand in.
    let list = run_with_agent_home(
        &harness,
        Path::new(&linked_path),
        agent_home.path(),
        &["worktree", "list", "--format", "json"],
    );
    assert_eq!(list.code, 0, "stderr: {}", list.stderr_text());
    let list_json = parse_json(&list);
    assert_eq!(
        list_json["data"]["repo_root"].as_str(),
        Some(expected_repo_root.as_str()),
        "repo_root should resolve to the primary worktree even from inside a linked worktree"
    );

    let entries = list_json["data"]["entries"]
        .as_array()
        .expect("entries array");
    let managed = entries
        .iter()
        .find(|entry| entry["path"].as_str() == Some(linked_path.as_str()))
        .expect("managed worktree listed");
    assert_eq!(
        managed["managed"], true,
        "managed worktree must stay classified managed from inside a linked worktree"
    );
}

#[test]
fn worktree_go_resolves_slug_and_emits_path_shell_and_json() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree", "add", "topic-go", "--from", "main", "--format", "json",
        ],
    );
    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    let path = parse_json(&add)["data"]["path"]
        .as_str()
        .expect("path")
        .to_string();

    // Default text mode prints the bare resolved path (composable with `cd`).
    let go = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "go", "topic-go"],
    );
    assert_eq!(go.code, 0, "stderr: {}", go.stderr_text());
    assert_eq!(go.stdout_text().trim(), path);

    // Shell mode prints an evaluable `cd -- <path>` command.
    let go_shell = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "go", "topic-go", "--shell"],
    );
    assert_eq!(go_shell.code, 0, "stderr: {}", go_shell.stderr_text());
    let shell_out = go_shell.stdout_text();
    assert!(
        shell_out.trim_start().starts_with("cd -- "),
        "stdout: {shell_out}"
    );
    assert!(shell_out.contains(&path), "stdout: {shell_out}");

    // JSON mode carries the resolved metadata under a versioned envelope.
    let go_json = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "go", "topic-go", "--format", "json"],
    );
    assert_eq!(go_json.code, 0, "stderr: {}", go_json.stderr_text());
    let json = parse_json(&go_json);
    assert_eq!(json["schema_version"], "cli.git-cli.worktree.go.v1");
    assert_eq!(json["ok"], true);
    assert_eq!(json["data"]["path"].as_str(), Some(path.as_str()));
    assert_eq!(json["data"]["branch"], "feat/topic-go");
    assert_eq!(json["data"]["managed"], true);
}

#[test]
fn worktree_go_resolves_branch_name_from_a_linked_worktree() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let alpha = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree", "add", "alpha", "--from", "main", "--format", "json",
        ],
    );
    assert_eq!(alpha.code, 0, "stderr: {}", alpha.stderr_text());
    let alpha_path = parse_json(&alpha)["data"]["path"]
        .as_str()
        .expect("alpha path")
        .to_string();

    let beta = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree", "add", "beta", "--from", "main", "--format", "json",
        ],
    );
    assert_eq!(beta.code, 0, "stderr: {}", beta.stderr_text());
    let beta_path = parse_json(&beta)["data"]["path"]
        .as_str()
        .expect("beta path")
        .to_string();

    // From inside alpha, jump to beta by its full branch name.
    let go = run_with_agent_home(
        &harness,
        Path::new(&alpha_path),
        agent_home.path(),
        &["worktree", "go", "feat/beta", "--format", "json"],
    );
    assert_eq!(go.code, 0, "stderr: {}", go.stderr_text());
    let json = parse_json(&go);
    assert_eq!(json["data"]["path"].as_str(), Some(beta_path.as_str()));
}

#[test]
fn worktree_go_unknown_target_errors_in_json() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let go = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "go", "does-not-exist", "--format", "json"],
    );
    assert_ne!(go.code, 0);
    assert_eq!(go.stderr_text(), "");
    let json = parse_json(&go);
    assert_eq!(json["schema_version"], "cli.git-cli.worktree.go.v1");
    assert_eq!(json["ok"], false);
    assert_eq!(json["error"]["code"], "worktree-not-found");
}

#[test]
fn worktree_remove_refuses_primary_and_removes_managed_slug() {
    let harness = GitCliHarness::new();
    let repo = init_repo();
    let agent_home = tempfile::TempDir::new().expect("agent home");

    let add = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "add",
            "topic-two",
            "--from",
            "main",
            "--format",
            "json",
        ],
    );
    assert_eq!(add.code, 0, "stderr: {}", add.stderr_text());
    let add_json = parse_json(&add);
    let path = add_json["data"]["path"].as_str().expect("path");
    assert!(Path::new(path).exists());
    fs::create_dir(repo.path().join("topic-two")).expect("local shadow dir");

    let primary = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &[
            "worktree",
            "remove",
            repo.path().to_str().expect("utf8 repo path"),
            "--format",
            "json",
        ],
    );
    assert_ne!(primary.code, 0);
    let primary_json = parse_json(&primary);
    assert_eq!(primary_json["error"]["code"], "refuse-primary-worktree");

    let remove = run_with_agent_home(
        &harness,
        repo.path(),
        agent_home.path(),
        &["worktree", "remove", "topic-two", "--format", "json"],
    );
    assert_eq!(remove.code, 0, "stderr: {}", remove.stderr_text());
    assert_eq!(remove.stderr_text(), "");

    let remove_json = parse_json(&remove);
    assert_eq!(
        remove_json["schema_version"],
        "cli.git-cli.worktree.remove.v1"
    );
    assert_eq!(remove_json["ok"], true);
    assert_eq!(remove_json["data"]["removed_path"], path);
    assert!(!Path::new(path).exists(), "worktree path should be removed");
}