sephera_core 0.5.0

Shared analysis engine behind the Sephera CLI for LOC metrics, context building, and Tree-sitter AST compression.
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
616
617
618
619
620
621
622
623
624
625
626
use std::{
    ffi::OsString,
    path::{Path, PathBuf},
};

use anyhow::{Context, Result, bail};
use tempfile::TempDir;
use url::Url;

use super::git::{git_stdout_string, run_git};

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SourceRequest {
    pub path: Option<PathBuf>,
    pub url: Option<String>,
    pub git_ref: Option<String>,
}

#[derive(Debug)]
pub struct ResolvedSource {
    pub analysis_path: PathBuf,
    pub repo_root: PathBuf,
    pub display_path: Option<String>,
    pub display_repo_root: Option<String>,
    pub(crate) checkout_guard: Option<TempDir>,
}

impl ResolvedSource {
    #[must_use]
    pub const fn is_remote(&self) -> bool {
        self.checkout_guard.is_some()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreeHostingStyle {
    GitHub,
    GitLab,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ParsedRemoteSource {
    Repo {
        clone_url: String,
        display_repo_root: String,
    },
    Tree {
        clone_url: String,
        display_repo_root: String,
        style: TreeHostingStyle,
        tail_segments: Vec<String>,
    },
}

/// Resolves a local path or remote repository URL into a concrete analysis
/// source on disk.
///
/// # Errors
///
/// Returns an error when the request is invalid, the URL cannot be parsed,
/// cloning or checkout fails, or a tree URL cannot be resolved to a valid
/// directory in the temporary checkout.
pub fn resolve_source(request: &SourceRequest) -> Result<ResolvedSource> {
    match (&request.path, &request.url) {
        (Some(_), Some(_)) => {
            bail!("`path` and `url` are mutually exclusive");
        }
        (None, None) => {
            if request.git_ref.is_some() {
                bail!("`ref` requires `url`");
            }

            Ok(ResolvedSource {
                analysis_path: PathBuf::from("."),
                repo_root: PathBuf::from("."),
                display_path: None,
                display_repo_root: None,
                checkout_guard: None,
            })
        }
        (Some(path), None) => {
            if request.git_ref.is_some() {
                bail!("`ref` requires `url`");
            }

            Ok(ResolvedSource {
                analysis_path: path.clone(),
                repo_root: path.clone(),
                display_path: None,
                display_repo_root: None,
                checkout_guard: None,
            })
        }
        (None, Some(raw_url)) => {
            let parsed_source = parse_remote_source(raw_url)?;
            if matches!(parsed_source, ParsedRemoteSource::Tree { .. })
                && request.git_ref.is_some()
            {
                bail!("`ref` cannot be combined with a tree URL");
            }

            resolve_remote_source(parsed_source, request.git_ref.as_deref())
        }
    }
}

fn resolve_remote_source(
    parsed_source: ParsedRemoteSource,
    git_ref: Option<&str>,
) -> Result<ResolvedSource> {
    let checkout_root =
        TempDir::new().context("failed to create temp checkout")?;
    let repo_root = checkout_root.path().join("repo");

    let clone_url = match &parsed_source {
        ParsedRemoteSource::Repo { clone_url, .. }
        | ParsedRemoteSource::Tree { clone_url, .. } => clone_url,
    };

    run_git(
        None,
        [
            OsString::from("clone"),
            OsString::from(clone_url),
            repo_root.as_os_str().to_os_string(),
        ],
        &format!("clone `{clone_url}`"),
    )?;

    let (analysis_path, display_path, display_repo_root) = match parsed_source {
        ParsedRemoteSource::Repo {
            display_repo_root, ..
        } => {
            let resolved_ref = git_ref
                .map(|raw_ref| resolve_checkout_ref(&repo_root, raw_ref))
                .transpose()?;
            let display_repo_root = resolved_ref.map_or_else(
                || display_repo_root.clone(),
                |raw_ref| format!("{display_repo_root}@{raw_ref}"),
            );
            (
                repo_root.clone(),
                Some(display_repo_root.clone()),
                Some(display_repo_root),
            )
        }
        ParsedRemoteSource::Tree {
            display_repo_root,
            style,
            tail_segments,
            ..
        } => {
            let (resolved_ref, sub_path) =
                resolve_tree_checkout(&repo_root, &tail_segments)?;
            let display_repo_root_with_ref =
                format!("{display_repo_root}@{resolved_ref}");
            let display_path = render_tree_display_url(
                style,
                &display_repo_root,
                &resolved_ref,
                sub_path.as_deref(),
            );
            let analysis_path = sub_path.as_ref().map_or_else(
                || repo_root.clone(),
                |sub_path| repo_root.join(sub_path),
            );
            (
                analysis_path,
                Some(display_path),
                Some(display_repo_root_with_ref),
            )
        }
    };

    if !analysis_path.exists() {
        bail!(
            "resolved analysis path `{}` does not exist in the checkout",
            analysis_path.display()
        );
    }
    if !analysis_path.is_dir() {
        bail!(
            "resolved analysis path `{}` is not a directory",
            analysis_path.display()
        );
    }

    Ok(ResolvedSource {
        analysis_path,
        repo_root,
        display_path,
        display_repo_root,
        checkout_guard: Some(checkout_root),
    })
}

fn resolve_tree_checkout(
    repo_root: &Path,
    tail_segments: &[String],
) -> Result<(String, Option<String>)> {
    if tail_segments.is_empty() {
        bail!("tree URL is missing the ref segment");
    }

    for prefix_len in (1..=tail_segments.len()).rev() {
        let raw_ref = tail_segments[..prefix_len].join("/");
        if let Ok(resolved_ref) = resolve_checkout_ref(repo_root, &raw_ref) {
            let sub_path = if prefix_len == tail_segments.len() {
                None
            } else {
                Some(tail_segments[prefix_len..].join("/"))
            };
            return Ok((resolved_ref, sub_path));
        }
    }

    bail!(
        "failed to resolve a git ref from tree URL segments `{}`",
        tail_segments.join("/")
    )
}

fn resolve_checkout_ref(repo_root: &Path, raw_ref: &str) -> Result<String> {
    let trimmed_ref = raw_ref.trim();
    if trimmed_ref.is_empty() {
        bail!("git ref must not be empty");
    }

    let resolved_ref =
        resolve_commitish(repo_root, trimmed_ref).or_else(|_| {
            let remote_ref = format!("origin/{trimmed_ref}");
            resolve_commitish(repo_root, &remote_ref)
        })?;

    run_git(
        Some(repo_root),
        [
            OsString::from("checkout"),
            OsString::from("--detach"),
            OsString::from(&resolved_ref),
        ],
        &format!("checkout `{trimmed_ref}`"),
    )?;

    Ok(trimmed_ref.to_owned())
}

fn resolve_commitish(repo_root: &Path, candidate: &str) -> Result<String> {
    git_stdout_string(
        repo_root,
        [
            OsString::from("rev-parse"),
            OsString::from("--verify"),
            OsString::from(format!("{candidate}^{{commit}}")),
        ],
        &format!("resolve git ref `{candidate}`"),
    )
}

fn parse_remote_source(raw_url: &str) -> Result<ParsedRemoteSource> {
    let trimmed_url = raw_url.trim();
    if trimmed_url.is_empty() {
        bail!("URL must not be empty");
    }

    if is_scp_style_git_url(trimmed_url) {
        return Ok(ParsedRemoteSource::Repo {
            clone_url: trimmed_url.to_owned(),
            display_repo_root: strip_trailing_git_suffix(trimmed_url),
        });
    }

    let parsed_url = Url::parse(trimmed_url)
        .with_context(|| format!("failed to parse URL `{trimmed_url}`"))?;

    match parsed_url.scheme() {
        "http" | "https" => parse_http_remote_source(&parsed_url),
        "ssh" | "file" => Ok(ParsedRemoteSource::Repo {
            clone_url: strip_trailing_slash(trimmed_url),
            display_repo_root: strip_trailing_git_suffix(trimmed_url),
        }),
        scheme => bail!("unsupported URL scheme `{scheme}`"),
    }
}

fn parse_http_remote_source(parsed_url: &Url) -> Result<ParsedRemoteSource> {
    let path_segments = parsed_url
        .path_segments()
        .map(|segments| {
            segments
                .filter(|segment| !segment.is_empty())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();

    if path_segments.len() < 2 {
        bail!("URL must include a repository path");
    }

    if path_segments
        .get(2)
        .is_some_and(|segment| *segment == "blob")
    {
        bail!("blob URLs are not supported");
    }

    if let Some(marker_index) =
        path_segments.iter().position(|segment| *segment == "-")
        && path_segments
            .get(marker_index + 1)
            .is_some_and(|segment| *segment == "tree")
    {
        if marker_index < 2 {
            bail!("GitLab tree URL must include a repository path");
        }

        if path_segments.get(marker_index + 2).is_none() {
            bail!("tree URL is missing the ref segment");
        }

        let repo_segments = &path_segments[..marker_index];
        let tail_segments = path_segments[(marker_index + 2)..]
            .iter()
            .map(|segment| (*segment).to_owned())
            .collect::<Vec<_>>();
        let repo_root = build_http_repo_root(parsed_url, repo_segments);

        return Ok(ParsedRemoteSource::Tree {
            clone_url: repo_root.clone(),
            display_repo_root: repo_root,
            style: TreeHostingStyle::GitLab,
            tail_segments,
        });
    }

    if path_segments
        .get(2)
        .is_some_and(|segment| *segment == "tree")
    {
        if path_segments.get(3).is_none() {
            bail!("tree URL is missing the ref segment");
        }

        let repo_segments = &path_segments[..2];
        let tail_segments = path_segments[3..]
            .iter()
            .map(|segment| (*segment).to_owned())
            .collect::<Vec<_>>();
        let repo_root = build_http_repo_root(parsed_url, repo_segments);

        return Ok(ParsedRemoteSource::Tree {
            clone_url: repo_root.clone(),
            display_repo_root: repo_root,
            style: TreeHostingStyle::GitHub,
            tail_segments,
        });
    }

    if path_segments.contains(&"blob") {
        bail!("blob URLs are not supported");
    }

    Ok(ParsedRemoteSource::Repo {
        clone_url: strip_trailing_slash(parsed_url.as_str()),
        display_repo_root: strip_trailing_git_suffix(parsed_url.as_str()),
    })
}

fn build_http_repo_root(parsed_url: &Url, repo_segments: &[&str]) -> String {
    let mut repo_root = format!(
        "{}://{}",
        parsed_url.scheme(),
        parsed_url.host_str().expect("HTTP URLs must have a host"),
    );
    if let Some(port) = parsed_url.port() {
        repo_root.push(':');
        repo_root.push_str(&port.to_string());
    }
    repo_root.push('/');
    repo_root.push_str(&repo_segments.join("/"));
    repo_root
}

fn render_tree_display_url(
    style: TreeHostingStyle,
    repo_root: &str,
    git_ref: &str,
    sub_path: Option<&str>,
) -> String {
    let mut rendered = match style {
        TreeHostingStyle::GitHub => format!("{repo_root}/tree/{git_ref}"),
        TreeHostingStyle::GitLab => format!("{repo_root}/-/tree/{git_ref}"),
    };

    if let Some(sub_path) = sub_path
        && !sub_path.is_empty()
    {
        rendered.push('/');
        rendered.push_str(sub_path);
    }

    rendered
}

fn is_scp_style_git_url(raw_url: &str) -> bool {
    raw_url.contains('@')
        && raw_url.contains(':')
        && !raw_url.contains("://")
        && raw_url
            .split_once(':')
            .is_some_and(|(left, right)| !left.is_empty() && !right.is_empty())
}

fn strip_trailing_slash(raw_url: &str) -> String {
    raw_url.trim_end_matches('/').to_owned()
}

fn strip_trailing_git_suffix(raw_url: &str) -> String {
    strip_trailing_slash(raw_url)
        .trim_end_matches(".git")
        .to_owned()
}

#[cfg(test)]
mod tests {
    use std::{fs, process::Command};

    use tempfile::tempdir;

    use super::*;

    fn run_git(repo_root: &Path, args: &[&str]) {
        let output = Command::new("git")
            .current_dir(repo_root)
            .args(args)
            .output()
            .unwrap_or_else(|error| {
                panic!("failed to run git {args:?}: {error}")
            });
        assert!(
            output.status.success(),
            "git {:?} failed\nstdout:\n{}\nstderr:\n{}",
            args,
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr),
        );
    }

    fn init_repo(repo_root: &Path) {
        run_git(repo_root, &["init"]);
        run_git(repo_root, &["config", "user.name", "Sephera Tests"]);
        run_git(repo_root, &["config", "user.email", "tests@example.com"]);
    }

    fn commit_all(repo_root: &Path, message: &str) {
        run_git(repo_root, &["add", "-A"]);
        run_git(repo_root, &["commit", "-m", message]);
    }

    fn write_file(repo_root: &Path, relative_path: &str, contents: &str) {
        let absolute_path = repo_root.join(relative_path);
        if let Some(parent) = absolute_path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(absolute_path, contents).unwrap();
    }

    #[test]
    fn parses_github_tree_url() {
        let parsed_source = parse_remote_source(
            "https://github.com/reim/sephera/tree/main/docs/src",
        )
        .unwrap();

        assert_eq!(
            parsed_source,
            ParsedRemoteSource::Tree {
                clone_url: String::from("https://github.com/reim/sephera"),
                display_repo_root: String::from(
                    "https://github.com/reim/sephera"
                ),
                style: TreeHostingStyle::GitHub,
                tail_segments: vec![
                    String::from("main"),
                    String::from("docs"),
                    String::from("src"),
                ],
            }
        );
    }

    #[test]
    fn parses_gitlab_tree_url() {
        let parsed_source = parse_remote_source(
            "https://gitlab.com/group/subgroup/repo/-/tree/main/docs",
        )
        .unwrap();

        assert_eq!(
            parsed_source,
            ParsedRemoteSource::Tree {
                clone_url: String::from(
                    "https://gitlab.com/group/subgroup/repo"
                ),
                display_repo_root: String::from(
                    "https://gitlab.com/group/subgroup/repo"
                ),
                style: TreeHostingStyle::GitLab,
                tail_segments: vec![String::from("main"), String::from("docs")],
            }
        );
    }

    #[test]
    fn rejects_blob_urls() {
        let error = parse_remote_source(
            "https://github.com/reim/sephera/blob/main/README.md",
        )
        .unwrap_err();

        assert!(error.to_string().contains("blob URLs are not supported"));
    }

    #[test]
    fn parses_scp_style_repo_url() {
        let parsed_source =
            parse_remote_source("git@github.com:reim/sephera.git").unwrap();

        assert_eq!(
            parsed_source,
            ParsedRemoteSource::Repo {
                clone_url: String::from("git@github.com:reim/sephera.git"),
                display_repo_root: String::from("git@github.com:reim/sephera"),
            }
        );
    }

    #[test]
    fn resolves_file_repo_url_and_selected_ref() {
        let temp_dir = tempdir().unwrap();
        init_repo(temp_dir.path());
        write_file(temp_dir.path(), "README.md", "# main\n");
        commit_all(temp_dir.path(), "main");
        run_git(temp_dir.path(), &["tag", "v1.0.0"]);

        let source = resolve_source(&SourceRequest {
            path: None,
            url: Some(format!("file://{}", temp_dir.path().display())),
            git_ref: Some(String::from("v1.0.0")),
        })
        .unwrap();

        assert!(source.is_remote());
        assert!(source.analysis_path.join("README.md").is_file());
        let expected_display =
            format!("file://{}@v1.0.0", temp_dir.path().display());
        assert_eq!(
            source.display_path.as_deref(),
            Some(expected_display.as_str())
        );
    }

    #[test]
    fn resolves_tree_url_with_branch_names_that_contain_slashes() {
        let temp_dir = tempdir().unwrap();
        init_repo(temp_dir.path());
        write_file(temp_dir.path(), "src/lib.rs", "pub fn main() {}\n");
        commit_all(temp_dir.path(), "initial");
        run_git(temp_dir.path(), &["checkout", "-b", "feature/docs"]);
        write_file(temp_dir.path(), "docs/guide.md", "# guide\n");
        commit_all(temp_dir.path(), "docs");

        let source = resolve_source(&SourceRequest {
            path: None,
            url: Some(
                "https://github.com/reim/sephera/tree/feature/docs/docs"
                    .to_string(),
            ),
            git_ref: None,
        });

        assert!(source.is_err());

        let source = resolve_remote_source(
            ParsedRemoteSource::Tree {
                clone_url: format!("file://{}", temp_dir.path().display()),
                display_repo_root: String::from(
                    "https://github.com/reim/sephera",
                ),
                style: TreeHostingStyle::GitHub,
                tail_segments: vec![
                    String::from("feature"),
                    String::from("docs"),
                    String::from("docs"),
                ],
            },
            None,
        )
        .unwrap();

        assert!(
            source.analysis_path.join("guide.md").is_file(),
            "expected tree URL to resolve to the docs subdirectory"
        );
        assert_eq!(
            source.display_path.as_deref(),
            Some("https://github.com/reim/sephera/tree/feature/docs/docs")
        );
        assert_eq!(
            source.display_repo_root.as_deref(),
            Some("https://github.com/reim/sephera@feature/docs")
        );
    }

    #[test]
    fn rejects_ref_without_url() {
        let error = resolve_source(&SourceRequest {
            path: Some(PathBuf::from(".")),
            url: None,
            git_ref: Some(String::from("main")),
        })
        .unwrap_err();

        assert!(error.to_string().contains("`ref` requires `url`"));
    }
}