Skip to main content

agent_hub_runtime/core/project_git/
remotes.rs

1//! Local fetch addresses for explicit clipboard actions; no network access.
2use super::actions::repository;
3use crate::core::git_credentials::split_credentials_from_url;
4use anyhow::{Context, Result};
5use serde::Serialize;
6use std::path::Path;
7
8#[derive(Serialize)]
9pub struct GitRemote {
10    pub name: String,
11    pub url: String,
12}
13
14pub fn remotes(root: &Path) -> Result<Vec<GitRemote>> {
15    let repo = repository(root)?;
16    let mut remotes = Vec::new();
17    for name in repo.remotes()?.iter() {
18        let name = name.context("Remote name is not valid UTF-8")?;
19        let remote = repo.find_remote(name)?;
20        let url = remote
21            .url()
22            .context("Remote URL is not valid UTF-8")?
23            .trim();
24        if url.is_empty() {
25            continue;
26        }
27        let url = split_credentials_from_url(url)
28            .map(|(_, sanitized)| sanitized)
29            .unwrap_or_else(|| url.to_owned());
30        remotes.push(GitRemote {
31            name: name.to_owned(),
32            url,
33        });
34    }
35    remotes.sort_by(|left, right| left.name.cmp(&right.name));
36    Ok(remotes)
37}
38
39#[cfg(test)]
40mod tests;