Skip to main content

relay_knowledge/code/
resolution.rs

1use std::path::Path;
2
3use super::{
4    CodeIndexError,
5    git::{resolve_ref, resolve_tree},
6    scope::scoped_source_snapshot_for_filters,
7    source::{source_commit_is_filesystem, source_kind},
8};
9
10pub fn resolve_repository_ref(
11    root_path: impl AsRef<Path>,
12    ref_selector: &str,
13) -> Result<String, CodeIndexError> {
14    resolve_repository_ref_with_path_filters(root_path, ref_selector, &[])
15}
16
17pub fn resolve_repository_ref_with_path_filters(
18    root_path: impl AsRef<Path>,
19    ref_selector: &str,
20    path_filters: &[String],
21) -> Result<String, CodeIndexError> {
22    resolve_repository_ref_with_filters(root_path, ref_selector, path_filters, &[])
23}
24
25pub fn resolve_repository_ref_with_filters(
26    root_path: impl AsRef<Path>,
27    ref_selector: &str,
28    path_filters: &[String],
29    language_filters: &[String],
30) -> Result<String, CodeIndexError> {
31    let root = root_path.as_ref();
32    if source_commit_is_filesystem(ref_selector) {
33        return Ok(ref_selector.to_owned());
34    }
35    if !source_kind(root)?.is_filesystem() {
36        return resolve_ref(root, ref_selector);
37    }
38
39    Ok(
40        scoped_source_snapshot_for_filters(root, ref_selector, path_filters, language_filters)?
41            .resolved_commit_sha,
42    )
43}
44
45pub fn resolve_repository_snapshot(
46    root_path: impl AsRef<Path>,
47    ref_selector: &str,
48) -> Result<(String, String), CodeIndexError> {
49    resolve_repository_snapshot_with_path_filters(root_path, ref_selector, &[])
50}
51
52pub fn resolve_repository_snapshot_with_path_filters(
53    root_path: impl AsRef<Path>,
54    ref_selector: &str,
55    path_filters: &[String],
56) -> Result<(String, String), CodeIndexError> {
57    resolve_repository_snapshot_with_filters(root_path, ref_selector, path_filters, &[])
58}
59
60pub fn resolve_repository_snapshot_with_filters(
61    root_path: impl AsRef<Path>,
62    ref_selector: &str,
63    path_filters: &[String],
64    language_filters: &[String],
65) -> Result<(String, String), CodeIndexError> {
66    let root = root_path.as_ref();
67    if source_commit_is_filesystem(ref_selector) {
68        return Ok((ref_selector.to_owned(), ref_selector.to_owned()));
69    }
70    if !source_kind(root)?.is_filesystem() {
71        let commit = resolve_ref(root, ref_selector)?;
72        let tree_hash = resolve_tree(root, &commit)?;
73
74        return Ok((commit, tree_hash));
75    }
76
77    let snapshot =
78        scoped_source_snapshot_for_filters(root, ref_selector, path_filters, language_filters)?;
79
80    Ok((snapshot.resolved_commit_sha, snapshot.tree_hash))
81}