Skip to main content

sley_remote/
ls_remote.rs

1//! Callable ls-remote advertisement listing for HTTP(S) and local remotes.
2//!
3//! [`ls_remote`] returns the advertised refs a `git ls-remote` would print for a
4//! resolved remote, as a [`LsRemoteRecord`] list, without sorting, printing, or
5//! exit-code mapping — those stay in the CLI (the `--sort`/`--symref` formatting
6//! and the `--exit-code` ⇒ exit-2 behavior are CLI concerns). Everything is taken
7//! as explicit parameters — the resolved [`LsRemoteSource`], the request
8//! [`ObjectFormat`], a [`LsRemoteFilter`], a ref-name match predicate, and a
9//! [`CredentialProvider`] — so it never reads process-global state, parses
10//! arguments, or prints.
11//!
12//! The ref-name glob/pattern matching (`refs/heads/*` style filters, peeled-tag
13//! `^{}` matching) is the CLI's larger ref-filter machinery, so it is injected as
14//! the `matches` predicate rather than moved; this module only applies the
15//! ref-class filters (`--heads`/`--tags`/`--refs`) and shapes the records.
16//!
17//! SSH ls-remote still lives in the CLI; only HTTP and local move here.
18
19#[cfg(feature = "http")]
20use std::collections::HashMap;
21use std::path::{Path, PathBuf};
22
23use sley_config::GitConfig;
24use sley_core::{GitError, ObjectFormat, ObjectId, Result};
25use sley_object::ObjectType;
26use sley_odb::{FileObjectDatabase, ObjectReader};
27use sley_refs::{FileRefStore, Ref, RefTarget};
28use sley_transport::RemoteUrl;
29
30use crate::CredentialProvider;
31
32/// How [`ls_remote`] obtains the ref advertisements.
33///
34/// The caller resolves the remote (URL rewriting, repository discovery — all
35/// process-state dependent) and hands `ls_remote` a concrete transport.
36pub enum LsRemoteSource {
37    /// A smart-HTTP(S) remote at the given already-resolved URL.
38    Http(RemoteUrl),
39    /// An SSH remote at the given already-resolved URL, listed by spawning `ssh`
40    /// (the credential seam is unused — the `ssh` program owns authentication).
41    Ssh(RemoteUrl),
42    /// A native anonymous `git://` remote at the given already-resolved URL.
43    Git(RemoteUrl),
44    /// A local repository read directly from `git_dir` (refs and the object
45    /// database used to peel annotated tags both resolve from this `$GIT_DIR`,
46    /// matching `git ls-remote` against a local path).
47    Local {
48        /// The remote repository's `$GIT_DIR`.
49        git_dir: PathBuf,
50    },
51}
52
53/// The ref-class filters that select which advertised refs to keep, mirroring the
54/// `git ls-remote` flags the CLI parses.
55#[derive(Debug, Clone, Copy, Default)]
56pub struct LsRemoteFilter {
57    /// Limit to branch refs (`--heads`/`--branches`).
58    pub heads: bool,
59    /// Limit to tag refs (`--tags`).
60    pub tags: bool,
61    /// Drop `HEAD` and peeled `^{}` entries (`--refs`).
62    pub refs_only: bool,
63}
64
65/// One advertised ref returned by [`ls_remote`] — what the CLI prints as a
66/// `<oid>\t<name>` line (with an optional preceding `ref: <symref>\t<name>` line
67/// when `--symref` is set and `symref` is present).
68#[derive(Debug, Clone)]
69pub struct LsRemoteRecord {
70    /// The object id the ref points at (peeled to the tag object for `^{}`
71    /// records).
72    pub oid: ObjectId,
73    /// The full ref name (e.g. `refs/heads/main`, `HEAD`, or `refs/tags/v1^{}`).
74    pub name: String,
75    /// The symref target, when the remote advertised this ref as a symbolic ref
76    /// (e.g. `HEAD` → `refs/heads/main`).
77    pub symref: Option<String>,
78}
79
80/// List the advertised refs for a resolved `source`.
81///
82/// Performs the work the CLI's `ls_remote_http_records` and inline local
83/// ls-remote path did: advertises the remote's refs (HTTP) or reads them directly
84/// (local), applies the `--heads`/`--tags`/`--refs` class filters and the
85/// caller-supplied `matches` ref-name predicate, and shapes the surviving refs
86/// into [`LsRemoteRecord`]s. For the local path it also emits peeled `^{}` records
87/// for annotated tags (unless `refs_only`).
88///
89/// `format` is the request/expected object format (SHA-1 for HTTP, the local
90/// repository's format for local); the returned [`ObjectFormat`] is the format
91/// actually in effect (HTTP resolves it from the advertisement). Returns the
92/// records and that format; never sorts, prints, or returns `GitError::Exit`. The
93/// caller applies `--sort`, `--symref` formatting, and the `--exit-code` mapping.
94pub fn ls_remote(
95    source: &LsRemoteSource,
96    format: ObjectFormat,
97    filter: &LsRemoteFilter,
98    matches: &dyn Fn(&str) -> bool,
99    config: Option<&GitConfig>,
100    #[cfg_attr(not(feature = "http"), allow(unused_variables))]
101    credentials: &mut dyn CredentialProvider,
102) -> Result<(Vec<LsRemoteRecord>, ObjectFormat)> {
103    crate::protocol::check_transport_allowed(scheme_for_ls_remote_source(source), config, None)
104        .map_err(crate::protocol::transport_policy_git_error)?;
105    match source {
106        #[cfg(feature = "http")]
107        LsRemoteSource::Http(remote) => {
108            ls_remote_http(remote, format, filter, matches, credentials)
109        }
110        #[cfg(not(feature = "http"))]
111        LsRemoteSource::Http(_) => Err(GitError::Unsupported(
112            "HTTP transport is not enabled in this build".into(),
113        )),
114        LsRemoteSource::Ssh(remote) => crate::ssh::ls_remote_ssh(remote, filter, matches),
115        LsRemoteSource::Git(remote) => crate::git::ls_remote_git(
116            remote,
117            filter,
118            matches,
119            config.and_then(|config| config.get("protocol", None, "version")) == Some("2"),
120        ),
121        LsRemoteSource::Local { git_dir } => {
122            ls_remote_local(git_dir, format, filter, matches, config)
123        }
124    }
125}
126
127fn scheme_for_ls_remote_source(source: &LsRemoteSource) -> &'static str {
128    match source {
129        LsRemoteSource::Http(remote) => crate::protocol::transport_scheme_for_remote(remote),
130        LsRemoteSource::Ssh(remote) => crate::protocol::transport_scheme_for_remote(remote),
131        LsRemoteSource::Git(remote) => crate::protocol::transport_scheme_for_remote(remote),
132        LsRemoteSource::Local { .. } => "file",
133    }
134}
135
136/// List advertised refs over smart HTTP(S): fetch the upload-pack advertisement,
137/// then apply the class filters and `matches` predicate, attaching the advertised
138/// `HEAD` symref where present.
139#[cfg(feature = "http")]
140fn ls_remote_http(
141    remote: &RemoteUrl,
142    format: ObjectFormat,
143    filter: &LsRemoteFilter,
144    matches: &dyn Fn(&str) -> bool,
145    credentials: &mut dyn CredentialProvider,
146) -> Result<(Vec<LsRemoteRecord>, ObjectFormat)> {
147    let client = crate::http::new_http_client();
148    let (refs, features) =
149        crate::http::http_upload_pack_advertisements(&client, remote, format, credentials)?;
150    let format = features.object_format.unwrap_or(ObjectFormat::Sha1);
151    if format != ObjectFormat::Sha1 {
152        return Err(GitError::Unsupported(format!(
153            "http ls-remote currently supports SHA-1 advertisements, got {}",
154            format.name()
155        )));
156    }
157    let symrefs = features
158        .symrefs
159        .iter()
160        .filter_map(|symref| symref.split_once(':'))
161        .map(|(name, target)| (name.to_string(), target.to_string()))
162        .collect::<HashMap<_, _>>();
163    let mut records = Vec::new();
164    for advertisement in refs {
165        if advertisement.oid.is_null() {
166            continue;
167        }
168        if filter.refs_only && (advertisement.name == "HEAD" || advertisement.name.ends_with("^{}"))
169        {
170            continue;
171        }
172        if !ref_class_selected(&advertisement.name, filter) {
173            continue;
174        }
175        if !matches(&advertisement.name) {
176            continue;
177        }
178        records.push(LsRemoteRecord {
179            oid: advertisement.oid,
180            symref: symrefs.get(&advertisement.name).cloned(),
181            name: advertisement.name,
182        });
183    }
184    Ok((records, format))
185}
186
187/// List advertised refs from a local repository at `git_dir`: `HEAD` (when no
188/// class filter is active), then every ref resolved to its object id, plus a
189/// peeled `^{}` record for each annotated tag (unless `refs_only`).
190fn ls_remote_local(
191    git_dir: &Path,
192    format: ObjectFormat,
193    filter: &LsRemoteFilter,
194    matches: &dyn Fn(&str) -> bool,
195    config: Option<&GitConfig>,
196) -> Result<(Vec<LsRemoteRecord>, ObjectFormat)> {
197    let store = FileRefStore::new(git_dir, format);
198    let db = FileObjectDatabase::from_git_dir(git_dir, format);
199    let config = ls_remote_local_config(git_dir, config);
200    let hidden_refs = upload_pack_hidden_ref_values(&config);
201    let include_non_head_symrefs =
202        !matches!(config.get("protocol", None, "version"), Some("0" | "1"));
203    let mut records = Vec::new();
204
205    if !filter.refs_only
206        && !filter.heads
207        && !filter.tags
208        && let Some(target) = store.read_ref("HEAD")?
209    {
210        let reference = Ref {
211            name: "HEAD".to_string(),
212            target,
213        };
214        if matches(&reference.name)
215            && let Some((oid, symref)) = resolve_for_each_ref_target(&store, &reference)?
216        {
217            records.push(LsRemoteRecord {
218                oid,
219                name: reference.name,
220                symref,
221            });
222        }
223    }
224
225    for reference in store.list_refs()? {
226        if ref_is_hidden_by_patterns(&reference.name, &hidden_refs) {
227            continue;
228        }
229        if !ref_class_selected(&reference.name, filter) {
230            continue;
231        }
232        if !matches(&reference.name) {
233            continue;
234        }
235        let Some((oid, symref)) = resolve_for_each_ref_target(&store, &reference)? else {
236            continue;
237        };
238        records.push(LsRemoteRecord {
239            oid,
240            name: reference.name.clone(),
241            symref: if include_non_head_symrefs {
242                symref
243            } else {
244                None
245            },
246        });
247        if !filter.refs_only
248            && let Some(record) = peeled_tag_record(&db, format, &oid, &reference.name, matches)?
249        {
250            records.push(record);
251        }
252    }
253
254    Ok((records, format))
255}
256
257fn ls_remote_local_config(git_dir: &Path, config: Option<&GitConfig>) -> GitConfig {
258    let mut local = sley_config::read_repo_config(git_dir, None).unwrap_or_default();
259    if let Some(config) = config {
260        local.sections.extend(config.sections.clone());
261    }
262    local
263}
264
265fn upload_pack_hidden_ref_values(config: &GitConfig) -> Vec<String> {
266    let mut out = Vec::new();
267    for section in &config.sections {
268        let applies = section.subsection.is_none()
269            && (section.name.eq_ignore_ascii_case("transfer")
270                || section.name.eq_ignore_ascii_case("uploadpack"));
271        if !applies {
272            continue;
273        }
274        for entry in &section.entries {
275            if entry.key.eq_ignore_ascii_case("hiderefs")
276                && let Some(value) = entry.value.as_deref()
277            {
278                out.push(trim_hidden_ref_pattern(value));
279            }
280        }
281    }
282    out
283}
284
285fn trim_hidden_ref_pattern(value: &str) -> String {
286    value.trim_end_matches('/').to_string()
287}
288
289fn ref_is_hidden_by_patterns(refname: &str, patterns: &[String]) -> bool {
290    for pattern in patterns.iter().rev() {
291        let mut pattern = pattern.as_str();
292        let negated = pattern.strip_prefix('!').is_some();
293        if negated {
294            pattern = &pattern[1..];
295        }
296        if let Some(rest) = pattern.strip_prefix('^') {
297            pattern = rest;
298        }
299        if hidden_ref_pattern_matches(refname, pattern) {
300            return !negated;
301        }
302    }
303    false
304}
305
306fn hidden_ref_pattern_matches(refname: &str, pattern: &str) -> bool {
307    refname
308        .strip_prefix(pattern)
309        .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
310}
311
312/// The peeled `^{}` record for `name` when `oid` is an annotated tag and the
313/// peeled name passes `matches`; `None` otherwise.
314fn peeled_tag_record(
315    db: &FileObjectDatabase,
316    format: ObjectFormat,
317    oid: &ObjectId,
318    name: &str,
319    matches: &dyn Fn(&str) -> bool,
320) -> Result<Option<LsRemoteRecord>> {
321    let object = db.read_object(oid)?;
322    if object.object_type != ObjectType::Tag {
323        return Ok(None);
324    }
325    let peeled_name = format!("{name}^{{}}");
326    if !matches(&peeled_name) {
327        return Ok(None);
328    }
329    let peeled = sley_rev::peel_tags(db, format, oid)?;
330    Ok(Some(LsRemoteRecord {
331        oid: peeled,
332        name: peeled_name,
333        symref: None,
334    }))
335}
336
337/// Whether `name` survives the `--heads`/`--tags` class filter (no class filter
338/// keeps everything; with one or both set, the ref must be in a selected class).
339fn ref_class_selected(name: &str, filter: &LsRemoteFilter) -> bool {
340    if !filter.heads && !filter.tags {
341        return true;
342    }
343    let is_head = name.starts_with("refs/heads/");
344    let is_tag = name.starts_with("refs/tags/");
345    (filter.heads && is_head) || (filter.tags && is_tag)
346}
347
348/// Resolve a (possibly symbolic) ref target to its object id, following up to
349/// five levels of symbolic indirection, returning the first symbolic name seen.
350fn resolve_for_each_ref_target(
351    store: &FileRefStore,
352    reference: &Ref,
353) -> Result<Option<(ObjectId, Option<String>)>> {
354    let mut target = reference.target.clone();
355    let mut symref = None;
356    for _ in 0..5 {
357        match target {
358            RefTarget::Direct(oid) => return Ok(Some((oid, symref))),
359            RefTarget::Symbolic(name) => {
360                symref.get_or_insert_with(|| name.clone());
361                let Some(next) = store.read_ref(&name)? else {
362                    return Ok(None);
363                };
364                target = next;
365            }
366        }
367    }
368    Ok(None)
369}