mars-schema 0.27.3

Pure schema reader and editor for Mars agent packages
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
/// A bare `host/path` URL (`github.com/org/repo`) spelled with `https://`; any other URL is
/// returned as written. Reading an entry applies it, so a consumer's `mars.toml` and a dependency's
/// manifest fetch the same repository from the same text.
pub fn with_default_scheme(url: &str) -> String {
    if !url.contains("://")
        && !is_local_path(url)
        && !is_ssh_shorthand(url)
        && url.contains('/')
        && url.contains('.')
    {
        format!("https://{url}")
    } else {
        url.to_string()
    }
}

/// A filesystem path or `file:` URL, which never takes a scheme. Git's own rule: with a colon, it
/// is local only if a `/` comes before the first one (`./a:b`, `~/x:y`) or it opens with a drive
/// letter (`C:/x`, `C:\x`); so `~user@host:repo` is SSH. Without a colon, a leading `/`, `.` or `~`.
fn is_local_path(url: &str) -> bool {
    let bytes = url.as_bytes();
    if url.starts_with("file:")
        || (bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':')
    {
        return true;
    }
    match url.split_once(':') {
        Some((before_colon, _)) => before_colon.contains('/'),
        None => url.starts_with(['/', '.', '~']),
    }
}

/// Git's scp-like SSH form: `user@host:path` whatever the path (`git@host:/srv/repo.git` included),
/// or `host:path` without a user unless it is `host:<port>/…`. A `scheme://` URL or a local path is
/// not one.
pub fn is_ssh_shorthand(url: &str) -> bool {
    if url.contains("://") || is_local_path(url) {
        return false;
    }
    let Some((authority, path)) = url.split_once(':') else {
        return false;
    };
    if path.is_empty() || authority.contains(['/', '\\']) {
        return false;
    }
    match authority.split_once('@') {
        Some((user, host)) => !user.is_empty() && !host.is_empty(),
        None => {
            let (first, rest) = path.split_once('/').unwrap_or((path, ""));
            let is_port =
                !rest.is_empty() && !first.is_empty() && first.bytes().all(|b| b.is_ascii_digit());
            !authority.is_empty() && !is_port
        }
    }
}

/// Canonicalize a git URL for identity comparison and cache keying.
///
/// SSH and HTTPS forms of the same repo produce the same canonical form.
///
/// Canonical form: `host/path` (lowercase host, no protocol, no `.git`,
/// no trailing slash, no userinfo, SCP colon converted to slash).
///
/// Port handling: Explicit ports in URL-style forms (`host:port/path`) are
/// preserved. SCP-style colon (`host:path`) is distinguished by checking
/// whether the text between the colon and the next `/` (or end of string) is
/// **entirely digits** (a real port) vs. a path segment (convert to slash).
/// This correctly handles digit-leading path segments like `123team/repo`.
///
/// # Examples
///
/// ```text
/// canonicalize_git_url("https://github.com/foo/bar")   → "github.com/foo/bar"
/// canonicalize_git_url("git@github.com:foo/bar.git")   → "github.com/foo/bar"
/// canonicalize_git_url("ssh://git@github.com/foo/bar") → "github.com/foo/bar"
/// canonicalize_git_url("GITHUB.COM/Foo/Bar")           → "github.com/Foo/Bar"
/// ```
pub fn canonicalize_git_url(url: &str) -> String {
    let mut s = url.to_string();

    // 1. Strip protocol prefixes
    for prefix in &["https://", "http://", "ssh://", "git://"] {
        if let Some(rest) = s.strip_prefix(prefix) {
            s = rest.to_string();
            break;
        }
    }

    // 2. Strip userinfo (git@, user:pass@, etc.)
    //    Only strip if the '@' comes before the first '/' (it's a userinfo,
    //    not part of the path).
    if let Some(at_pos) = s.find('@') {
        let slash_pos = s.find('/').unwrap_or(s.len());
        if at_pos < slash_pos {
            s = s[at_pos + 1..].to_string();
        }
    }

    // 3. Handle SCP-style colon vs port colon.
    //    After stripping userinfo we may have:
    //      - `github.com:foo/bar`    (SCP – colon is a separator, convert to /)
    //      - `github.com:123team/r`  (SCP – digit-leading path, convert to /)
    //      - `github.com:1234/path`  (URL with port – keep the colon)
    //      - `github.com/foo/bar`    (already URL-style – no colon)
    if let Some(colon_pos) = s.find(':') {
        let before_colon = &s[..colon_pos];
        let after_colon = &s[colon_pos + 1..];
        // Only treat as a port when all characters up to the next '/' (or end of
        // string) are ASCII digits.  A digit-leading path segment like `123team`
        // must NOT be treated as a port.
        if !before_colon.contains('/') && !after_colon.starts_with("//") {
            let port_candidate = after_colon.split('/').next().unwrap_or("");
            let is_port =
                !port_candidate.is_empty() && port_candidate.chars().all(|c| c.is_ascii_digit());
            if !is_port {
                // SCP-style colon: convert to slash
                s.replace_range(colon_pos..colon_pos + 1, "/");
            }
        }
    }

    // 4. Strip trailing `.git` suffix
    if let Some(rest) = s.strip_suffix(".git") {
        s = rest.to_string();
    }

    // 5. Strip trailing slash
    if let Some(rest) = s.strip_suffix('/') {
        s = rest.to_string();
    }

    // 6. Lowercase the host portion only (everything before the first `/`).
    //    Path segments are case-sensitive on most git hosts.
    if let Some(slash_pos) = s.find('/') {
        let host = s[..slash_pos].to_ascii_lowercase();
        s = format!("{host}{}", &s[slash_pos..]);
    } else {
        // No slash means the whole string is a host (bare domain URL).
        s = s.to_ascii_lowercase();
    }

    s
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Protocol forms ──────────────────────────────────────────────────────

    #[test]
    fn https_form() {
        assert_eq!(
            canonicalize_git_url("https://github.com/foo/bar"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn http_form() {
        assert_eq!(
            canonicalize_git_url("http://gitlab.com/org/repo"),
            "gitlab.com/org/repo"
        );
    }

    #[test]
    fn ssh_url_form() {
        assert_eq!(
            canonicalize_git_url("ssh://git@github.com/foo/bar"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn git_protocol_form() {
        assert_eq!(
            canonicalize_git_url("git://github.com/foo/bar"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn scp_form_git_at() {
        assert_eq!(
            canonicalize_git_url("git@github.com:foo/bar.git"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn bare_domain_form() {
        assert_eq!(
            canonicalize_git_url("github.com/meridian-flow/meridian-base"),
            "github.com/meridian-flow/meridian-base"
        );
    }

    // ── All three protocol forms for the same repo converge ────────────────

    #[test]
    fn ssh_and_https_converge() {
        let https = canonicalize_git_url("https://github.com/foo/bar");
        let ssh = canonicalize_git_url("git@github.com:foo/bar.git");
        let ssh_url = canonicalize_git_url("ssh://git@github.com/foo/bar");
        assert_eq!(https, ssh);
        assert_eq!(https, ssh_url);
    }

    // ── `.git` suffix tolerance ─────────────────────────────────────────────

    #[test]
    fn strips_git_suffix_https() {
        assert_eq!(
            canonicalize_git_url("https://github.com/foo/bar.git"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn strips_git_suffix_bare() {
        assert_eq!(
            canonicalize_git_url("github.com/foo/bar.git"),
            "github.com/foo/bar"
        );
    }

    // ── Trailing slash tolerance ────────────────────────────────────────────

    #[test]
    fn strips_trailing_slash() {
        assert_eq!(
            canonicalize_git_url("https://github.com/foo/bar/"),
            "github.com/foo/bar"
        );
    }

    // ── Lowercase host, case-preserved path ────────────────────────────────

    #[test]
    fn lowercases_host_only() {
        assert_eq!(
            canonicalize_git_url("GITHUB.COM/Foo/Bar"),
            "github.com/Foo/Bar"
        );
    }

    #[test]
    fn lowercases_host_in_https_url() {
        assert_eq!(
            canonicalize_git_url("https://GITHUB.COM/Foo/Bar"),
            "github.com/Foo/Bar"
        );
    }

    // ── Userinfo stripped ──────────────────────────────────────────────────

    #[test]
    fn strips_userinfo_user_password() {
        assert_eq!(
            canonicalize_git_url("https://user:pass@github.com/foo/bar"),
            "github.com/foo/bar"
        );
    }

    #[test]
    fn strips_userinfo_bare_user() {
        assert_eq!(
            canonicalize_git_url("https://user@github.com/foo/bar"),
            "github.com/foo/bar"
        );
    }

    // ── Port preservation ──────────────────────────────────────────────────

    #[test]
    fn preserves_explicit_port_in_url_form() {
        // A URL-style port (digit after colon) is kept as-is.
        assert_eq!(
            canonicalize_git_url("git://gitlab.localtest.me:19424/group/pkg.git"),
            "gitlab.localtest.me:19424/group/pkg"
        );
    }

    #[test]
    fn preserves_explicit_port_in_https() {
        assert_eq!(
            canonicalize_git_url("https://git.example.com:8443/org/repo"),
            "git.example.com:8443/org/repo"
        );
    }

    // ── SCP colon becomes slash, port digit is preserved ───────────────────

    #[test]
    fn scp_colon_converted_to_slash() {
        assert_eq!(
            canonicalize_git_url("git@github.com:org/repo.git"),
            "github.com/org/repo"
        );
    }

    // ── SCP path with digit-leading segment (regression: was mis-detected as port)

    #[test]
    fn scp_digit_leading_path_segment() {
        // "123team" starts with a digit but is NOT a port — must become a slash.
        assert_eq!(
            canonicalize_git_url("git@example.com:123team/repo.git"),
            "example.com/123team/repo"
        );
    }

    #[test]
    fn scp_digit_leading_path_segment_no_git_suffix() {
        assert_eq!(
            canonicalize_git_url("git@example.com:9front/repo"),
            "example.com/9front/repo"
        );
    }

    // ── Real ports are preserved even when path follows ────────────────────

    #[test]
    fn port_only_digits_is_preserved_https() {
        assert_eq!(
            canonicalize_git_url("https://gitlab.com:8443/org/repo"),
            "gitlab.com:8443/org/repo"
        );
    }

    #[test]
    fn port_only_digits_is_preserved_git_protocol() {
        assert_eq!(
            canonicalize_git_url("git://custom.host:19424/group/pkg.git"),
            "custom.host:19424/group/pkg"
        );
    }

    // ── Subgroup / nested paths preserved ──────────────────────────────────

    #[test]
    fn subgroup_path_preserved() {
        assert_eq!(
            canonicalize_git_url("https://gitlab.com/group/subgroup/repo"),
            "gitlab.com/group/subgroup/repo"
        );
    }

    #[test]
    fn subgroup_path_ssh_preserved() {
        assert_eq!(
            canonicalize_git_url("git@gitlab.com:group/subgroup/repo.git"),
            "gitlab.com/group/subgroup/repo"
        );
    }

    // ── No-slash bare host ─────────────────────────────────────────────────

    #[test]
    fn bare_host_no_path_lowercased() {
        assert_eq!(canonicalize_git_url("GITHUB.COM"), "github.com");
    }

    // ── Idempotence ────────────────────────────────────────────────────────

    #[test]
    fn idempotent_on_already_canonical_form() {
        let canonical = "github.com/foo/bar";
        assert_eq!(canonicalize_git_url(canonical), canonical);
    }

    // ── Classifier: SSH shorthand, local paths, default scheme ─────────────

    #[test]
    fn default_scheme_table() {
        let unchanged = [
            "git@host:/srv/repo.git",
            "git@host.example:org/repo.git",
            "git@host:org/repo",
            "github.com:org/repo",
            "example.com:123team/repo",
            "host:~user/repo",
            "~user@host:repo",
            "./a:b",
            "~/x:y",
            "[::1]:8080/org/repo.git",
            "localhost:8080/repo",
            "C:/x/repo.d",
            "C:\\x\\repo",
            "D:repo.git/x",
            "./rel/x.git",
            "../up/x.git",
            "/abs/x.y/repo",
            "~/x.y/repo",
            "file:/x.y/r",
            "file:///x.y/r",
            "ssh://git@github.com/org/repo",
            "https://github.com/org/repo",
            "org/repo",
        ];
        for url in unchanged {
            assert_eq!(with_default_scheme(url), url, "{url}");
        }
        assert_eq!(
            with_default_scheme("github.com/org/repo"),
            "https://github.com/org/repo"
        );
        assert_eq!(
            with_default_scheme("example.com:8080/org/repo"),
            "https://example.com:8080/org/repo"
        );
    }

    #[test]
    fn ssh_shorthand_table() {
        for url in [
            "git@host:/srv/repo.git",
            "git@host:org/repo",
            "git@host:8080/repo",
            "github.com:org/repo",
            "example.com:123team/repo",
            "host:/srv/repo.git",
            "host:~user/repo",
            "~user@host:repo",
        ] {
            assert!(is_ssh_shorthand(url), "{url}");
        }
        for url in [
            "example.com:8080/org/repo",
            "git@host:",
            "@host:org/repo",
            "ssh://git@host/org/repo",
            "/srv/repo:x",
            "./a:b",
            "~/x:y",
            "C:/x",
            "\\\\?\\C:\\x",
            "file:/x",
            "org/repo",
        ] {
            assert!(!is_ssh_shorthand(url), "{url}");
        }
    }
}