bgit 0.4.2

User-friendly Git wrapper for beginners, automating essential tasks like adding, committing, and pushing changes. It includes smart rules to avoid common pitfalls, such as accidentally adding sensitive files or directories and has exclusive support for portable hooks!
use git2::{CertificateCheckStatus, CredentialType, RemoteCallbacks};
use log::debug;
use std::sync::{Arc, Mutex};

use crate::{
    auth::{git_http::try_userpass_authentication, git_ssh::ssh_authenticate_git},
    config::global::BGitGlobalConfig,
};

pub fn setup_auth_callbacks<'a>(global_config: &'a BGitGlobalConfig) -> RemoteCallbacks<'a> {
    let mut callbacks = RemoteCallbacks::new();

    // Track attempt count across callback invocations
    let attempt_count: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));

    callbacks.credentials(move |url, username_from_url, allowed_types| {
        let mut count = attempt_count.lock().unwrap();
        *count += 1;
        let current_attempt = *count;
        drop(count);

        if allowed_types.contains(CredentialType::USER_PASS_PLAINTEXT) {
            try_userpass_authentication(username_from_url, global_config)
        } else {
            ssh_authenticate_git(
                url,
                username_from_url,
                allowed_types,
                current_attempt,
                global_config,
            )
        }
    });

    // Set up certificate check callback for HTTPS
    callbacks.certificate_check(|_cert, _host| {
        // TODO(rootCircle): make this configurable and secure. For now we accept all certs.
        debug!("Skipping certificate verification (INSECURE)");
        Ok(CertificateCheckStatus::CertificateOk)
    });

    callbacks
}