Skip to main content

git_remote_gittorrent/
git-remote-gittorrent.rs

1//! Git remote helper for gittorrent:// URLs
2//!
3//! This binary implements the Git remote helper protocol for gittorrent:// URLs,
4//! allowing standard Git commands to work with `GitTorrent` repositories.
5//!
6//! Git invokes this automatically when encountering gittorrent:// URLs.
7//! Configuration is read from environment variables and config files.
8
9use hyprstream_p2p::{
10    git::remote_helper::run_git_remote_helper, service::GitTorrentConfig, Result,
11};
12use std::io::Write;
13
14#[tokio::main]
15async fn main() -> Result<()> {
16    // Git passes the remote name and URL as command line arguments
17    let args: Vec<String> = std::env::args().collect();
18
19    if args.len() < 3 {
20        // Write errors to stderr (git remote helper protocol)
21        let _ = writeln!(
22            std::io::stderr(),
23            "Usage: git-remote-gittorrent <remote-name> <url>"
24        );
25        std::process::exit(1);
26    }
27
28    let _remote_name = &args[1];
29    let url = &args[2];
30
31    // Load configuration from file and environment variables
32    // Uses GitTorrentConfig::load() which handles both automatically
33    let mut config = GitTorrentConfig::load()?;
34
35    // For remote helpers, enable auto-discovery by default if no bootstrap nodes
36    if config.bootstrap_nodes.is_empty() {
37        config.auto_discovery = true;
38    }
39
40    // Run the Git remote helper with configuration and URL
41    match run_git_remote_helper(Some(config), Some(url)).await {
42        Ok(()) => {}
43        Err(e) => {
44            // Write errors to stderr (git remote helper protocol)
45            let _ = writeln!(std::io::stderr(), "error {e}");
46            std::process::exit(1);
47        }
48    }
49
50    Ok(())
51}