nomograph-kit 0.7.0

Verified tool registry manager -- manages developer toolchains from git-based registries
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
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

use crate::config::{Config, Pin, Registry};
use crate::tool::{load_registry_tools, ToolDef};

/// A tool definition resolved to its source registry.
#[derive(Debug, Clone)]
pub struct ResolvedTool {
    pub def: ToolDef,
    pub registry: String,
}

// -- Git operations (shell out to git for credential helper support) --

/// Clone a registry with shallow depth.
fn clone(url: &str, dest: &Path, branch: &str) -> Result<()> {
    let status = std::process::Command::new("git")
        .args([
            "clone",
            "--quiet",
            "--depth",
            "1",
            "--branch",
            branch,
            "--single-branch",
            url,
        ])
        .arg(dest)
        .status()
        .context("Failed to run git clone")?;
    if !status.success() {
        anyhow::bail!("git clone failed for {url}");
    }
    Ok(())
}

/// Fast-forward pull an existing clone.
fn pull(repo_dir: &Path, branch: &str) -> Result<()> {
    let status = std::process::Command::new("git")
        .args(["pull", "--quiet", "--ff-only", "origin", branch])
        .current_dir(repo_dir)
        .status()
        .context("Failed to run git pull")?;
    if !status.success() {
        anyhow::bail!("git pull failed in {}", repo_dir.display());
    }
    Ok(())
}

// -- Public API --

/// Ensure a registry is cloned and up to date. Returns the local path.
///
/// Clones the registry if it doesn't exist locally, otherwise pulls the
/// latest changes. Uses `--depth 1 --single-branch` for clones and
/// `--ff-only` for pulls to keep the local copy minimal and safe.
pub fn ensure_registry(config: &Config, reg: &Registry) -> Result<PathBuf> {
    let registry_dir = config.registry_dir()?;
    std::fs::create_dir_all(&registry_dir)
        .with_context(|| format!("failed to create {}", registry_dir.display()))?;

    let dest = registry_dir.join(&reg.name);

    if dest.join(".git").exists() {
        eprintln!("  pulling {}", reg.name);
        // F19: if --ff-only pull fails (e.g. force-pushed remote), re-clone from scratch
        if let Err(e) = pull(&dest, &reg.branch) {
            eprintln!("  pull failed ({e:#}), re-cloning from scratch");
            std::fs::remove_dir_all(&dest)
                .with_context(|| format!("failed to remove {}", dest.display()))?;
            clone(&reg.url, &dest, &reg.branch)
                .with_context(|| format!("failed to re-clone registry '{}'", reg.name))?;
        }
    } else {
        // Remove the directory if it exists but isn't a valid git repo
        // (e.g. partial clone that was interrupted).
        if dest.exists() {
            std::fs::remove_dir_all(&dest).with_context(|| {
                format!(
                    "failed to remove stale directory {}",
                    dest.display()
                )
            })?;
        }
        eprintln!("  cloning {}", reg.name);
        clone(&reg.url, &dest, &reg.branch)
            .with_context(|| format!("failed to clone registry '{}'", reg.name))?;
    }

    Ok(dest)
}

/// Resolve tools across all registries in priority order.
///
/// Registries are iterated in the order they appear in the config (index 0
/// is highest priority). When multiple registries define the same tool,
/// the first-seen definition wins and later ones are logged as shadowed.
pub fn resolve_tools(config: &Config) -> Result<Vec<ResolvedTool>> {
    let registry_base = config.registry_dir()?;
    let mut seen: HashMap<String, String> = HashMap::new();
    let mut resolved: Vec<ResolvedTool> = Vec::new();

    for reg in &config.registry {
        let reg_path = registry_base.join(&reg.name);
        if !reg_path.exists() {
            eprintln!(
                "  warning: registry '{}' not cloned yet, skipping",
                reg.name
            );
            continue;
        }

        let tools = load_registry_tools(&reg_path)
            .with_context(|| format!("failed to load tools from registry '{}'", reg.name))?;

        for def in tools {
            if let Some(winner) = seen.get(&def.name) {
                eprintln!(
                    "  {} shadowed: '{}' already provided by '{}'",
                    def.name, reg.name, winner
                );
            } else {
                seen.insert(def.name.clone(), reg.name.clone());
                resolved.push(ResolvedTool {
                    def,
                    registry: reg.name.clone(),
                });
            }
        }
    }

    Ok(resolved)
}

/// Apply local pins to override versions or registry sources.
///
/// If a pin specifies only a version, the version is updated in place.
/// If a pin specifies a registry, the tool is re-resolved from that
/// registry only (replacing the existing entry if present), and then
/// the pinned version is applied if one was given.
pub fn apply_pins(
    resolved: &mut Vec<ResolvedTool>,
    pins: &HashMap<String, Pin>,
    config: &Config,
) -> Result<()> {
    let registry_base = config.registry_dir()?;
    let version_re = regex::Regex::new(crate::tool::VERSION_PATTERN).unwrap();

    for (tool_name, pin) in pins {
        if let Some(ref pin_registry) = pin.registry {
            // Registry pin: re-resolve from the specified registry only.
            let reg = config.registry(pin_registry).ok_or_else(|| {
                anyhow::anyhow!(
                    "pin for '{}' references unknown registry '{}'",
                    tool_name,
                    pin_registry
                )
            })?;

            let reg_path = registry_base.join(&reg.name);
            if !reg_path.exists() {
                anyhow::bail!(
                    "pin for '{}' references registry '{}' which is not cloned",
                    tool_name,
                    pin_registry
                );
            }

            let tools = load_registry_tools(&reg_path).with_context(|| {
                format!(
                    "failed to load tools from registry '{}' for pin",
                    pin_registry
                )
            })?;

            let mut new_def = tools
                .into_iter()
                .find(|t| t.name == *tool_name)
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "pin for '{}' references registry '{}' which does not contain that tool",
                        tool_name,
                        pin_registry
                    )
                })?;

            // Apply version override if specified.
            if let Some(ref version) = pin.version {
                new_def.version = version.clone();
            }

            let new_entry = ResolvedTool {
                def: new_def,
                registry: pin_registry.clone(),
            };

            // Replace existing entry or append.
            if let Some(existing) = resolved.iter_mut().find(|r| r.def.name == *tool_name) {
                *existing = new_entry;
            } else {
                resolved.push(new_entry);
            }
        } else if let Some(ref version) = pin.version {
            // Finding 15: validate pinned version before applying
            if !version_re.is_match(version) {
                anyhow::bail!(
                    "invalid pin version '{}' for '{}'",
                    version,
                    tool_name
                );
            }
            // Version-only pin: update in place.
            if let Some(existing) = resolved.iter_mut().find(|r| r.def.name == *tool_name) {
                existing.def.version = version.clone();
            } else {
                eprintln!(
                    "  warning: pin for '{}' has no matching tool to override",
                    tool_name
                );
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Config, Pin, Registry, Settings};
    use crate::tool::{Source, Tier, ToolDef};
    use std::collections::HashMap;

    /// Build a minimal ToolDef for testing.
    fn make_tool(name: &str, version: &str) -> ToolDef {
        ToolDef {
            name: name.to_string(),
            description: None,
            source: Source::Github,
            version: version.to_string(),
            tag_prefix: "v".to_string(),
            bin: None,
            tier: Tier::Low,
            repo: Some("owner/repo".to_string()),
            project_id: None,
            package: None,
            crate_name: None,
            aqua: None,
            assets: HashMap::new(),
            checksum: None,
            checksums: HashMap::new(),
            signature: None,
        }
    }

    /// Build a Config with a tmpdir-based cache so tests don't touch ~/.cache.
    fn make_config(registries: Vec<Registry>, pins: HashMap<String, Pin>, dir: &Path) -> Config {
        Config {
            settings: Settings {
                cache_dir: dir.to_string_lossy().to_string(),
                ..Settings::default()
            },
            registry: registries,
            pins,
        }
    }

    /// Populate a fake registry on disk: create tools/<name>.toml files.
    fn write_tool_file(registry_dir: &Path, def: &ToolDef) {
        let tools_dir = registry_dir.join("tools");
        std::fs::create_dir_all(&tools_dir).unwrap();
        let content = format!(
            r#"[tool]
name = "{name}"
source = "{source}"
version = "{version}"
tag_prefix = "v"
tier = "{tier}"
repo = "{repo}"
"#,
            name = def.name,
            source = match def.source {
                Source::Github => "github",
                Source::Gitlab => "gitlab",
                Source::Npm => "npm",
                Source::Crates => "crates",
                Source::Direct => "direct",
                Source::Rustup => "rustup",
            },
            version = def.version,
            tier = def.tier,
            repo = def.repo.as_deref().unwrap_or("owner/repo"),
        );
        let path = tools_dir.join(format!("{}.toml", def.name));
        std::fs::write(path, content).unwrap();
    }

    #[test]
    fn resolve_first_registry_wins() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");

        // Create two registries with overlapping tools.
        let alpha_dir = reg_dir.join("alpha");
        let beta_dir = reg_dir.join("beta");

        // Both registries define "gh", but alpha is higher priority.
        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));
        write_tool_file(&alpha_dir, &make_tool("jq", "1.7.1"));
        write_tool_file(&beta_dir, &make_tool("gh", "2.88.0"));
        write_tool_file(&beta_dir, &make_tool("yq", "4.44.0"));

        let config = make_config(
            vec![
                Registry {
                    name: "alpha".to_string(),
                    url: "https://example.com/alpha.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
                Registry {
                    name: "beta".to_string(),
                    url: "https://example.com/beta.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
            ],
            HashMap::new(),
            cache,
        );

        let resolved = resolve_tools(&config).unwrap();

        // gh should come from alpha with version 2.89.0
        let gh = resolved.iter().find(|r| r.def.name == "gh").unwrap();
        assert_eq!(gh.def.version, "2.89.0");
        assert_eq!(gh.registry, "alpha");

        // jq from alpha
        let jq = resolved.iter().find(|r| r.def.name == "jq").unwrap();
        assert_eq!(jq.registry, "alpha");

        // yq from beta (no conflict)
        let yq = resolved.iter().find(|r| r.def.name == "yq").unwrap();
        assert_eq!(yq.registry, "beta");

        // Total: gh + jq + yq = 3 (beta's gh is shadowed)
        assert_eq!(resolved.len(), 3);
    }

    #[test]
    fn pin_overrides_version() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");

        let alpha_dir = reg_dir.join("alpha");
        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));

        let config = make_config(
            vec![Registry {
                name: "alpha".to_string(),
                url: "https://example.com/alpha.git".to_string(),
                branch: "main".to_string(),
                readonly: false,
            }],
            HashMap::from([(
                "gh".to_string(),
                Pin {
                    version: Some("2.90.0".to_string()),
                    registry: None,
                },
            )]),
            cache,
        );

        let mut resolved = resolve_tools(&config).unwrap();
        apply_pins(&mut resolved, &config.pins, &config).unwrap();

        let gh = resolved.iter().find(|r| r.def.name == "gh").unwrap();
        assert_eq!(gh.def.version, "2.90.0");
        assert_eq!(gh.registry, "alpha");
    }

    #[test]
    fn pin_overrides_registry() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");

        let alpha_dir = reg_dir.join("alpha");
        let beta_dir = reg_dir.join("beta");

        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));
        write_tool_file(&beta_dir, &make_tool("gh", "2.88.0"));

        let config = make_config(
            vec![
                Registry {
                    name: "alpha".to_string(),
                    url: "https://example.com/alpha.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
                Registry {
                    name: "beta".to_string(),
                    url: "https://example.com/beta.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
            ],
            // Pin gh to beta with a specific version.
            HashMap::from([(
                "gh".to_string(),
                Pin {
                    version: Some("2.91.0".to_string()),
                    registry: Some("beta".to_string()),
                },
            )]),
            cache,
        );

        let mut resolved = resolve_tools(&config).unwrap();
        // Before pin: gh comes from alpha at 2.89.0
        assert_eq!(
            resolved.iter().find(|r| r.def.name == "gh").unwrap().registry,
            "alpha"
        );

        apply_pins(&mut resolved, &config.pins, &config).unwrap();

        // After pin: gh comes from beta at 2.91.0
        let gh = resolved.iter().find(|r| r.def.name == "gh").unwrap();
        assert_eq!(gh.def.version, "2.91.0");
        assert_eq!(gh.registry, "beta");
    }

    #[test]
    fn pin_registry_only_keeps_original_version() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");

        let alpha_dir = reg_dir.join("alpha");
        let beta_dir = reg_dir.join("beta");

        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));
        write_tool_file(&beta_dir, &make_tool("gh", "2.88.0"));

        let config = make_config(
            vec![
                Registry {
                    name: "alpha".to_string(),
                    url: "https://example.com/alpha.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
                Registry {
                    name: "beta".to_string(),
                    url: "https://example.com/beta.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
            ],
            // Pin gh to beta but no version override.
            HashMap::from([(
                "gh".to_string(),
                Pin {
                    version: None,
                    registry: Some("beta".to_string()),
                },
            )]),
            cache,
        );

        let mut resolved = resolve_tools(&config).unwrap();
        apply_pins(&mut resolved, &config.pins, &config).unwrap();

        // gh should come from beta with beta's version (2.88.0)
        let gh = resolved.iter().find(|r| r.def.name == "gh").unwrap();
        assert_eq!(gh.def.version, "2.88.0");
        assert_eq!(gh.registry, "beta");
    }

    #[test]
    fn pin_unknown_registry_errors() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");
        let alpha_dir = reg_dir.join("alpha");
        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));

        let config = make_config(
            vec![Registry {
                name: "alpha".to_string(),
                url: "https://example.com/alpha.git".to_string(),
                branch: "main".to_string(),
                readonly: false,
            }],
            HashMap::from([(
                "gh".to_string(),
                Pin {
                    version: None,
                    registry: Some("nonexistent".to_string()),
                },
            )]),
            cache,
        );

        let mut resolved = resolve_tools(&config).unwrap();
        let result = apply_pins(&mut resolved, &config.pins, &config);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("unknown registry")
        );
    }

    #[test]
    fn pin_adds_tool_not_in_resolved() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries");

        let alpha_dir = reg_dir.join("alpha");
        let beta_dir = reg_dir.join("beta");

        write_tool_file(&alpha_dir, &make_tool("gh", "2.89.0"));
        // yq only in beta, but alpha is the only configured registry
        // so resolve_tools won't find it. A registry pin can pull it in.
        write_tool_file(&beta_dir, &make_tool("yq", "4.44.0"));

        let config = make_config(
            vec![
                Registry {
                    name: "alpha".to_string(),
                    url: "https://example.com/alpha.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
                Registry {
                    name: "beta".to_string(),
                    url: "https://example.com/beta.git".to_string(),
                    branch: "main".to_string(),
                    readonly: false,
                },
            ],
            HashMap::from([(
                "yq".to_string(),
                Pin {
                    version: Some("4.45.0".to_string()),
                    registry: Some("beta".to_string()),
                },
            )]),
            cache,
        );

        // Only alpha is populated in resolve, so yq won't be there yet.
        // But beta is on disk, so the registry pin can pull it in.
        let mut resolved = resolve_tools(&config).unwrap();
        let yq_before = resolved.iter().find(|r| r.def.name == "yq");
        // yq is present from beta via normal resolution
        assert!(yq_before.is_some());

        apply_pins(&mut resolved, &config.pins, &config).unwrap();

        let yq = resolved.iter().find(|r| r.def.name == "yq").unwrap();
        assert_eq!(yq.def.version, "4.45.0");
        assert_eq!(yq.registry, "beta");
    }

    #[test]
    fn resolve_skips_missing_registry() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();

        // Don't create any registry dirs on disk.
        let config = make_config(
            vec![Registry {
                name: "missing".to_string(),
                url: "https://example.com/missing.git".to_string(),
                branch: "main".to_string(),
                readonly: false,
            }],
            HashMap::new(),
            cache,
        );

        let resolved = resolve_tools(&config).unwrap();
        assert!(resolved.is_empty());
    }

    #[test]
    fn resolve_empty_registry() {
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path();
        let reg_dir = cache.join("registries").join("empty");
        std::fs::create_dir_all(&reg_dir).unwrap();

        let config = make_config(
            vec![Registry {
                name: "empty".to_string(),
                url: "https://example.com/empty.git".to_string(),
                branch: "main".to_string(),
                readonly: false,
            }],
            HashMap::new(),
            cache,
        );

        let resolved = resolve_tools(&config).unwrap();
        assert!(resolved.is_empty());
    }
}