ngit 3.0.2

nostr plugin for git
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
use std::fs;

use anyhow::{Context, Result};
use ngit::agent_guidance;
use test_harness::Harness;

async fn harness() -> Result<Harness> {
    Harness::builder(
        env!("CARGO_BIN_EXE_ngit"),
        env!("CARGO_BIN_EXE_git-remote-nostr"),
    )
    .build()
    .await
}

#[tokio::test]
async fn status_works_without_a_nostr_remote_or_login() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let output = repo.ngit(["skill", "status", "--json"]).output().await?;

    assert!(
        output.status.success(),
        "skill status failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;
    assert_eq!(json["command_status"], "ok");
    assert_eq!(json["installed"], false);
    assert_eq!(json["is_maintainer"], serde_json::Value::Null);
    assert_eq!(json["reminders_enabled"], true);
    Ok(())
}

#[tokio::test]
async fn install_and_update_work_without_a_nostr_remote_or_login() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let setup = repo.ngit(["skill", "install", "--json"]).output().await?;
    assert!(
        setup.status.success(),
        "skill install failed: {}",
        String::from_utf8_lossy(&setup.stderr)
    );
    let setup_json: serde_json::Value = serde_json::from_slice(&setup.stdout)?;
    assert_eq!(setup_json["command_status"], "ok");
    assert_eq!(setup_json["action"], "installed");
    assert_eq!(setup_json["changes_uncommitted"], true);
    let changed_files = setup_json["changed_files"]
        .as_array()
        .context("skill install changed_files is not an array")?;
    assert_eq!(
        changed_files.len(),
        2 + 2 * agent_guidance::bundled_references().len()
    );
    assert!(
        changed_files
            .iter()
            .any(|path| path == agent_guidance::SKILL_PATH)
    );
    assert!(
        changed_files
            .iter()
            .any(|path| path == agent_guidance::CLAUDE_SKILL_PATH)
    );
    assert!(repo.dir().join(".agents/skills/ngit/SKILL.md").is_file());
    assert!(repo.dir().join(".claude/skills/ngit/SKILL.md").is_file());
    assert!(!repo.dir().join(".agents/ngit-guidance.json").exists());
    assert!(!repo.dir().join("AGENTS.md").exists());
    assert!(!repo.dir().join("CLAUDE.md").exists());
    for (name, content) in agent_guidance::bundled_references() {
        assert_eq!(
            fs::read_to_string(repo.dir().join(".agents/skills/ngit/reference").join(name))?,
            *content
        );
        assert_eq!(
            fs::read_to_string(repo.dir().join(".claude/skills/ngit/reference").join(name))?,
            *content
        );
    }

    let status = repo.ngit(["skill", "status", "--json"]).output().await?;
    assert!(status.status.success());
    let json: serde_json::Value = serde_json::from_slice(&status.stdout)?;
    assert_eq!(json["installed_version"], json["bundled_version"]);
    assert_eq!(json["update_available"], false);
    assert_eq!(
        json["managed_files"],
        serde_json::json!([
            ".agents/skills/ngit/SKILL.md",
            ".claude/skills/ngit/SKILL.md"
        ])
    );

    let custom = "# Maintainer wording\n\nUse ngit for collaboration.\n";
    fs::write(repo.dir().join("AGENTS.md"), custom)?;
    let update = repo.ngit(["skill", "upgrade"]).output().await?;
    assert!(
        update.status.success(),
        "skill update failed: {}",
        String::from_utf8_lossy(&update.stderr)
    );
    assert_eq!(fs::read_to_string(repo.dir().join("AGENTS.md"))?, custom);
    Ok(())
}

#[tokio::test]
async fn ignored_skill_paths_are_not_reported_as_uncommitted() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    fs::write(repo.dir().join(".gitignore"), ".claude/\n")?;
    repo.git_ok(["add", ".gitignore"], "stage the ignore rule")
        .await?;
    repo.git_ok(
        ["commit", "-m", "ignore Claude files"],
        "commit the ignore rule",
    )
    .await?;

    let install = repo.ngit(["skill", "install", "--json"]).output().await?;

    assert!(
        install.status.success(),
        "skill install failed: {}",
        String::from_utf8_lossy(&install.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&install.stdout)?;
    assert_eq!(json["command_status"], "ok");
    assert_eq!(json["changes_uncommitted"], true);
    let changed_files = json["changed_files"]
        .as_array()
        .context("skill install changed_files is not an array")?;
    assert_eq!(
        changed_files.len(),
        1 + agent_guidance::bundled_references().len()
    );
    assert!(changed_files.iter().all(|path| {
        path.as_str()
            .is_some_and(|path| path.starts_with(".agents/skills/ngit/"))
    }));
    assert!(repo.dir().join(".claude/skills/ngit/SKILL.md").is_file());
    Ok(())
}

#[tokio::test]
async fn install_updates_existing_claude_without_creating_agents_files() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    fs::write(repo.dir().join("CLAUDE.md"), "# Existing Claude policy\n")?;

    let install = repo.ngit(["skill", "install"]).output().await?;

    assert!(
        install.status.success(),
        "skill install failed: {}",
        String::from_utf8_lossy(&install.stderr)
    );
    assert!(repo.dir().join(".agents/skills/ngit/SKILL.md").is_file());
    assert!(repo.dir().join(".claude/skills/ngit/SKILL.md").is_file());
    assert!(!repo.dir().join("AGENTS.md").exists());
    assert!(
        fs::read_to_string(repo.dir().join("CLAUDE.md"))?.contains(".claude/skills/ngit/SKILL.md")
    );
    Ok(())
}

/// ngit's own repository keeps single canonical `skills/ngit/SKILL.md` and
/// `skills/ngit/reference/*.md` files that the agent-specific discovery
/// locations symlink to, so the copies cannot drift. Install must follow those
/// symlinks, update the canonical files once, and leave the result uncommitted.
#[cfg(unix)]
#[tokio::test]
async fn install_updates_a_canonical_skill_copy_shared_by_symlinks() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;

    let canonical = repo.dir().join("skills/ngit/SKILL.md");
    fs::create_dir_all(canonical.parent().context("canonical skill has a parent")?)?;
    fs::write(
        &canonical,
        "---\nname: ngit\nversion: \"0.1\"\n---\n\nstale canonical skill body\n",
    )?;
    let canonical_reference = repo.dir().join("skills/ngit/reference");
    fs::create_dir_all(&canonical_reference)?;
    for (name, _) in agent_guidance::bundled_references() {
        fs::write(canonical_reference.join(name), "placeholder\n")?;
    }
    for relative in [
        ".agents/skills/ngit/SKILL.md",
        ".claude/skills/ngit/SKILL.md",
    ] {
        let path = repo.dir().join(relative);
        fs::create_dir_all(path.parent().context("skill path has a parent")?)?;
        std::os::unix::fs::symlink("../../../skills/ngit/SKILL.md", &path)?;
    }
    for dir in [".agents/skills/ngit", ".claude/skills/ngit"] {
        let reference = repo.dir().join(format!("{dir}/reference"));
        fs::create_dir_all(reference.parent().context("skill dir has a parent")?)?;
        std::os::unix::fs::symlink("../../../skills/ngit/reference", &reference)?;
    }
    repo.git_ok(["add", "-A"], "stage the canonical skill layout")
        .await?;
    repo.git_ok(
        ["commit", "-m", "add the canonical skill layout"],
        "commit the canonical skill layout",
    )
    .await?;
    let original_head = repo
        .snapshot()?
        .refs
        .get("refs/heads/main")
        .context("refs/heads/main missing before skill install")?
        .clone();

    let install = repo.ngit(["skill", "install", "--json"]).output().await?;
    assert!(
        install.status.success(),
        "skill install failed: {}",
        String::from_utf8_lossy(&install.stderr)
    );

    assert!(!fs::read_to_string(&canonical)?.contains("stale canonical skill body"));
    assert!(!fs::read_to_string(canonical_reference.join("prs.md"))?.contains("placeholder"));
    for relative in [
        ".agents/skills/ngit/SKILL.md",
        ".claude/skills/ngit/SKILL.md",
    ] {
        assert!(
            fs::symlink_metadata(repo.dir().join(relative))?
                .file_type()
                .is_symlink(),
            "install replaced the `{relative}` symlink"
        );
        assert_eq!(
            fs::read_to_string(repo.dir().join(relative))?,
            fs::read_to_string(&canonical)?
        );
    }
    for (name, content) in agent_guidance::bundled_references() {
        assert_eq!(
            fs::read_to_string(canonical_reference.join(name))?,
            *content
        );
    }

    let status = repo.ngit(["skill", "status", "--json"]).output().await?;
    assert!(status.status.success());
    let json: serde_json::Value = serde_json::from_slice(&status.stdout)?;
    assert_eq!(json["installed"], true);
    assert_eq!(json["installed_version"], json["bundled_version"]);
    assert_eq!(json["update_available"], false);

    let install_json: serde_json::Value = serde_json::from_slice(&install.stdout)?;
    assert_eq!(install_json["command_status"], "ok");
    assert_eq!(install_json["changes_uncommitted"], true);
    assert!(
        install_json["changed_files"]
            .as_array()
            .context("canonical install changed_files is not an array")?
            .iter()
            .any(|path| path == "skills/ngit/SKILL.md")
    );
    assert_eq!(
        repo.snapshot()?
            .refs
            .get("refs/heads/main")
            .context("refs/heads/main missing after skill install")?,
        &original_head,
        "skill install moved HEAD"
    );
    let cached = repo.git(["diff", "--cached", "--quiet"]).output().await?;
    assert!(cached.status.success(), "skill install modified the index");

    // The shared canonical targets are left for the repository's normal
    // validation and commit workflow.
    let changed = repo.git(["diff", "--name-only"]).output().await?;
    let names = String::from_utf8(changed.stdout)?
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .map(str::to_owned)
        .collect::<Vec<_>>();
    assert!(
        names.iter().any(|name| name == "skills/ngit/SKILL.md"),
        "no skill in {names:?}"
    );
    for (name, _) in agent_guidance::bundled_references() {
        let expected = format!("skills/ngit/reference/{name}");
        assert!(
            names.iter().any(|name| name == &expected),
            "missing {expected} in {names:?}"
        );
    }
    Ok(())
}

#[tokio::test]
async fn reinstalling_committed_guidance_reports_no_repository_changes() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let install = repo.ngit(["skill", "install"]).output().await?;
    assert!(install.status.success());
    repo.git_ok(["add", "-A"], "stage installed guidance")
        .await?;
    repo.git_ok(["commit", "-m", "install guidance"], "commit guidance")
        .await?;
    let original_head = repo
        .snapshot()?
        .refs
        .get("refs/heads/main")
        .context("refs/heads/main missing before reinstall")?
        .clone();

    fs::remove_dir_all(repo.dir().join(".agents/skills/ngit"))?;
    fs::remove_dir_all(repo.dir().join(".claude/skills/ngit"))?;
    let reinstall = repo.ngit(["skill", "install", "--json"]).output().await?;

    assert!(
        reinstall.status.success(),
        "skill reinstall failed: {}",
        String::from_utf8_lossy(&reinstall.stderr)
    );
    let json: serde_json::Value = serde_json::from_slice(&reinstall.stdout)?;
    assert_eq!(json["command_status"], "ok");
    assert_eq!(json["action"], "installed");
    assert_eq!(json["changed_files"], serde_json::json!([]));
    assert_eq!(json["changes_uncommitted"], false);
    assert_eq!(
        repo.snapshot()?
            .refs
            .get("refs/heads/main")
            .context("refs/heads/main missing after reinstall")?,
        &original_head,
        "skill reinstall moved HEAD"
    );
    let status = repo.git(["status", "--porcelain"]).output().await?;
    assert!(
        String::from_utf8(status.stdout)?.trim().is_empty(),
        "restored guidance did not match the committed files"
    );
    Ok(())
}

#[tokio::test]
async fn opt_out_is_reported_by_status() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let missing_scope = repo.ngit(["skill", "opt-out"]).output().await?;
    assert!(
        !missing_scope.status.success(),
        "skill opt-out unexpectedly accepted no scope"
    );

    let opt_out = repo.ngit(["skill", "opt-out", "--local"]).output().await?;
    assert!(
        opt_out.status.success(),
        "skill opt-out failed: {}",
        String::from_utf8_lossy(&opt_out.stderr)
    );
    assert_eq!(
        repo.config("nostr.skill-reminders").await?.as_deref(),
        Some("false")
    );

    let status = repo.ngit(["skill", "status", "--json"]).output().await?;
    assert!(status.status.success());
    let json: serde_json::Value = serde_json::from_slice(&status.stdout)?;
    assert_eq!(json["reminders_enabled"], false);
    Ok(())
}

#[tokio::test]
async fn global_opt_out_works_without_a_git_worktree() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let global_config = repo.dir().join(".gitconfig");
    let outside = repo.dir().join("outside");
    fs::create_dir(&outside)?;

    // Point every global-config resolution ngit could use at the same file:
    // the `GIT_CONFIG_GLOBAL` override, and libgit2's `$HOME` / XDG search
    // for when the override is absent.
    let mut command = repo.ngit(["skill", "opt-out", "--global"]);
    let output = command
        .current_dir(outside)
        .env("GIT_CONFIG_GLOBAL", &global_config)
        .env("HOME", repo.dir())
        .env_remove("XDG_CONFIG_HOME")
        .output()
        .await
        .context("failed to run global skill opt-out")?;
    assert!(
        output.status.success(),
        "global skill opt-out failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!git2::Config::open(&global_config)?.get_bool("nostr.skill-reminders")?);
    Ok(())
}

/// Release candidates bundled `reference/repo-settings.md`, which was later
/// merged into `repositories.md`. Upgrading such an install must delete the
/// stale copy and leave the deletion for the repository to commit.
#[tokio::test]
async fn upgrade_removes_a_retired_reference_installed_by_a_release_candidate() -> Result<()> {
    let harness = harness().await?;
    let repo = harness.fresh_repo()?;
    let install = repo.ngit(["skill", "install"]).output().await?;
    assert!(
        install.status.success(),
        "skill install failed: {}",
        String::from_utf8_lossy(&install.stderr)
    );
    let retired = [
        ".agents/skills/ngit/reference/repo-settings.md",
        ".claude/skills/ngit/reference/repo-settings.md",
    ];
    for relative in retired {
        fs::write(repo.dir().join(relative), "superseded reference\n")?;
    }
    repo.git_ok(["add", "-A"], "stage the retired reference")
        .await?;
    repo.git_ok(
        ["commit", "-m", "add a release-candidate reference"],
        "commit the retired reference",
    )
    .await?;
    let original_head = repo
        .snapshot()?
        .refs
        .get("refs/heads/main")
        .context("refs/heads/main missing before skill upgrade")?
        .clone();

    let upgrade = repo.ngit(["skill", "upgrade", "--json"]).output().await?;
    assert!(
        upgrade.status.success(),
        "skill upgrade failed: {}",
        String::from_utf8_lossy(&upgrade.stderr)
    );

    for relative in retired {
        assert!(
            !repo.dir().join(relative).exists(),
            "{relative} survived the upgrade"
        );
    }
    let json: serde_json::Value = serde_json::from_slice(&upgrade.stdout)?;
    assert_eq!(json["command_status"], "ok");
    assert_eq!(json["changes_uncommitted"], true);
    let changed_files = json["changed_files"]
        .as_array()
        .context("skill upgrade changed_files is not an array")?;
    for relative in retired {
        assert!(
            changed_files.iter().any(|path| path == relative),
            "{relative} missing from {changed_files:?}"
        );
    }
    let changed = repo.git(["diff", "--name-status"]).output().await?;
    let lines = String::from_utf8(changed.stdout)?;
    for relative in retired {
        assert!(
            lines
                .lines()
                .any(|line| line.starts_with('D') && line.ends_with(relative)),
            "{relative} uncommitted deletion missing from {lines}"
        );
    }
    assert_eq!(
        repo.snapshot()?
            .refs
            .get("refs/heads/main")
            .context("refs/heads/main missing after skill upgrade")?,
        &original_head,
        "skill upgrade moved HEAD"
    );
    let cached = repo.git(["diff", "--cached", "--quiet"]).output().await?;
    assert!(cached.status.success(), "skill upgrade modified the index");
    Ok(())
}