Skip to main content

git2db/
gittorrent_integration.rs

1//! GitTorrent transport integration for git2db
2//!
3//! This module provides integration between git2db's transport system
4//! and the gittorrent-rs P2P git transport.
5//!
6//! Configuration is loaded automatically using `GitTorrentConfig::load()` with priority:
7//! 1. `GITTORRENT_*` environment variables
8//! 2. `~/.config/gittorrent/git-remote-gittorrent.*` config file
9//! 3. Default values
10
11use crate::transport::TransportFactory;
12use git2::transport::SmartSubtransport;
13use std::sync::Arc;
14
15/// Bridge between gittorrent's TransportFactory and git2db's TransportFactory
16pub struct GittorrentTransportBridge {
17    factory: hyprstream_p2p::GittorrentTransportFactory,
18}
19
20impl GittorrentTransportBridge {
21    /// Create a new bridge with the given gittorrent service
22    pub fn new(service: Arc<hyprstream_p2p::service::GitTorrentService>) -> Self {
23        Self {
24            factory: hyprstream_p2p::GittorrentTransportFactory::new(service),
25        }
26    }
27}
28
29impl TransportFactory for GittorrentTransportBridge {
30    fn create_transport(&self, url: &str) -> anyhow::Result<Box<dyn SmartSubtransport>> {
31        // Delegate to gittorrent's factory
32        hyprstream_p2p::git::transport::TransportFactory::create_transport(&self.factory, url)
33    }
34
35    fn supports_url(&self, url: &str) -> bool {
36        hyprstream_p2p::git::transport::TransportFactory::supports_url(&self.factory, url)
37    }
38}
39
40/// Register gittorrent transport with GitManager
41///
42/// Accepts a `GitTorrentConfig` which can come from:
43/// - `Git2DBConfig::gittorrent` (unified configuration)
44/// - Custom configuration programmatically created
45/// - Loaded directly via `GitTorrentConfig::load()`
46///
47/// Returns the service Arc for advanced usage if needed.
48///
49/// # Example
50///
51/// ```rust,ignore
52/// use git2db::{Git2DBConfig, gittorrent_integration};
53///
54/// // Option 1: Use git2db's unified config
55/// let config = Git2DBConfig::builder()
56///     .load_env()
57///     .gittorrent_p2p_port(4001)
58///     .build()?;
59///
60/// let service = gittorrent_integration::register_gittorrent(
61///     manager,
62///     config.gittorrent
63/// ).await?;
64///
65/// // Option 2: Load gittorrent config independently
66/// let gittorrent_config = hyprstream_p2p::service::GitTorrentConfig::load()?;
67/// let service = gittorrent_integration::register_gittorrent(
68///     manager,
69///     gittorrent_config
70/// ).await?;
71/// ```
72pub async fn register_gittorrent(
73    manager: &crate::manager::GitManager,
74    config: hyprstream_p2p::service::GitTorrentConfig,
75) -> anyhow::Result<Arc<hyprstream_p2p::service::GitTorrentService>> {
76    let service = Arc::new(hyprstream_p2p::service::GitTorrentService::new(config).await?);
77    let bridge = Arc::new(GittorrentTransportBridge::new(service.clone()));
78    manager.register_transport("gittorrent", bridge)?;
79
80    Ok(service)
81}