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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use dialoguer::{Confirm, theme::ColorfulTheme};
use git2::{Error, ErrorClass, ErrorCode};
use log::debug;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use crate::config::global::BGitGlobalConfig;
use crate::constants::SSH_AGENT_SOCKET_BASENAME;

/// Get the count of identities in SSH agent with socket
/// ssh-add exit codes: 0=success, 1=command fails (includes no identities), 2=can't contact agent
pub fn agent_identities_count_with_auth(socket_path: Option<&str>) -> Result<usize, Error> {
    let mut cmd = Command::new("ssh-add");
    cmd.arg("-l");

    if let Some(socket) = socket_path {
        cmd.env("SSH_AUTH_SOCK", socket);
    }

    let output = cmd.output().map_err(|e| {
        Error::new(
            ErrorCode::Auth,
            ErrorClass::Net,
            format!("Failed to run ssh-add -l: {e}"),
        )
    })?;

    match output.status.code() {
        Some(0) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let count = stdout.lines().filter(|l| !l.trim().is_empty()).count();
            Ok(count)
        }
        Some(1) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);

            if stdout.contains("The agent has no identities") {
                Ok(0)
            } else {
                Err(Error::new(
                    ErrorCode::Auth,
                    ErrorClass::Net,
                    format!("ssh-add -l failed: {}", stderr.trim()),
                ))
            }
        }
        Some(2) => Err(Error::new(
            ErrorCode::Auth,
            ErrorClass::Net,
            "ssh-agent not reachable",
        )),
        Some(code) => {
            // Unexpected exit code
            let stderr = String::from_utf8_lossy(&output.stderr);
            Err(Error::new(
                ErrorCode::Auth,
                ErrorClass::Net,
                format!(
                    "ssh-add -l returned unexpected exit code {}: {}",
                    code,
                    stderr.trim()
                ),
            ))
        }
        None => Err(Error::new(
            ErrorCode::Auth,
            ErrorClass::Net,
            "ssh-add -l was terminated by signal",
        )),
    }
}

/// Interactively add a key to SSH agent with socket
pub fn add_key_interactive_with_auth(
    key_path: &Path,
    key_name: &str,
    socket_path: Option<&str>,
) -> Result<bool, Error> {
    debug!("Trying interactive ssh-add for key: {key_name}");

    // Ask user if they want to add this key interactively
    let should_add = Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt(format!(
            "Add SSH key '{key_name}' to ssh-agent? (you may be prompted for passphrase)"
        ))
        .default(true)
        .interact()
        .map_err(|e| {
            Error::new(
                ErrorCode::Auth,
                ErrorClass::Net,
                format!("Failed to get user confirmation: {e}"),
            )
        })?;

    if !should_add {
        debug!("User chose not to add key: {key_name}");
        return Ok(false);
    }

    println!("Adding SSH key: {key_name}");
    println!("If the key is passphrase-protected, you will be prompted to enter it.");

    let mut cmd = Command::new("ssh-add");
    cmd.arg(key_path);

    if let Some(socket) = socket_path {
        cmd.env("SSH_AUTH_SOCK", socket);
    }

    let status = cmd
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .map_err(|e| {
            Error::new(
                ErrorCode::Auth,
                ErrorClass::Net,
                format!("Failed to spawn ssh-add: {e}"),
            )
        })?;

    if status.success() {
        debug!("Successfully added key: {key_name}");
        println!("SSH key '{key_name}' added successfully!");
        Ok(true)
    } else {
        debug!("Interactive ssh-add failed for key: {key_name}");
        println!("Failed to add SSH key '{key_name}'");
        Ok(false)
    }
}

/// Try SSH key files directly without agent (platform-agnostic)
pub fn try_ssh_key_files_directly(username: &str) -> Result<git2::Cred, Error> {
    debug!("Trying SSH key files directly for user: {username}");

    let ssh_dir = home::home_dir()
        .map(|p| p.join(".ssh"))
        .unwrap_or_else(|| PathBuf::from(".ssh"));
    let key_files = ["id_ed25519", "id_rsa", "id_ecdsa", "id_dsa"];

    for key_name in &key_files {
        let private_key_path = ssh_dir.join(key_name);
        let public_key_path = ssh_dir.join(format!("{key_name}.pub"));

        if private_key_path.exists() && public_key_path.exists() {
            debug!("Trying SSH key pair: {key_name} / {key_name}.pub");

            match git2::Cred::ssh_key(
                username,
                Some(&public_key_path),
                &private_key_path,
                None, // No passphrase for now
            ) {
                Ok(cred) => {
                    debug!("SSH key authentication succeeded with {key_name}");
                    return Ok(cred);
                }
                Err(e) => {
                    debug!("SSH key authentication failed with {key_name}: {e}");
                }
            }
        }
    }

    Err(Error::new(
        ErrorCode::Auth,
        ErrorClass::Net,
        "No valid SSH key pairs found or all failed authentication",
    ))
}

/// Add all available SSH keys to the agent with socket
pub fn add_all_ssh_keys_with_auth(
    cfg: &BGitGlobalConfig,
    socket_path: Option<&str>,
) -> Result<Option<PathBuf>, Error> {
    debug!("Adding all SSH keys from .ssh folder to ssh-agent");

    let ssh_dir = home::home_dir()
        .map(|p| p.join(".ssh"))
        .unwrap_or_else(|| PathBuf::from(".ssh"));

    if !ssh_dir.exists() {
        debug!("SSH directory {ssh_dir:?} does not exist");
        return Ok(None);
    }

    let key_files = ["id_ed25519", "id_rsa", "id_ecdsa", "id_dsa"];
    let mut added_count = 0;
    let mut first_added: Option<PathBuf> = None;

    let mut candidates: Vec<PathBuf> = Vec::new();
    let mut seen = std::collections::HashSet::new();

    if let Some(configured_key) = cfg.get_ssh_key_file()
        && seen.insert(configured_key.clone())
    {
        candidates.push(configured_key);
    }
    for name in &key_files {
        let path = ssh_dir.join(name);
        if seen.insert(path.clone()) {
            candidates.push(path);
        }
    }

    drop(seen);

    for key_path in candidates {
        let display_name = key_path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("ssh_key");

        if key_path.exists() {
            debug!("Found SSH key: {key_path:?}");
            // Skip if it's not a regular file or if a corresponding .pub is being considered
            if let Ok(md) = std::fs::metadata(&key_path)
                && !md.is_file()
            {
                continue;
            }
            // Also skip accidental public key files
            if key_path.extension().and_then(|s| s.to_str()) == Some("pub") {
                continue;
            }

            // First try a quick non-interactive add (for keys without passphrase)
            let mut cmd = Command::new("ssh-add");
            cmd.arg(&key_path);

            if let Some(socket) = socket_path {
                cmd.env("SSH_AUTH_SOCK", socket);
            }

            let quick_result = cmd
                .stdin(Stdio::null()) // No input for quick try
                .stdout(Stdio::null()) // Suppress output for quick try
                .stderr(Stdio::piped()) // Capture errors to check if passphrase is needed
                .output();

            match quick_result {
                Ok(output) if output.status.success() => {
                    debug!("Successfully added key without interaction: {display_name}");
                    added_count += 1;
                    if first_added.is_none() {
                        first_added = Some(key_path.clone());
                    }
                }
                Ok(output) => {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    debug!("Quick add failed for {display_name}: {stderr}");

                    debug!("Key {display_name} appears to need passphrase, trying interactive add");

                    match add_key_interactive_with_auth(&key_path, display_name, socket_path) {
                        Ok(true) => {
                            debug!("Successfully added key interactively: {display_name}");
                            added_count += 1;
                            if first_added.is_none() {
                                first_added = Some(key_path.clone());
                            }
                        }
                        Ok(false) => {
                            debug!("User skipped key: {display_name}");
                        }
                        Err(e) => {
                            debug!("Interactive add failed for {display_name}: {e}");
                        }
                    }
                }
                Err(e) => {
                    debug!("Error running ssh-add for {display_name}: {e}");
                }
            }
        } else {
            debug!("SSH key not found: {key_path:?}");
        }
    }

    debug!("Added {added_count} SSH keys to ssh-agent");

    if added_count == 0 {
        debug!("No SSH keys were added");
        println!("No SSH keys were added to ssh-agent.");
        println!("You may need to generate SSH keys or check your ~/.ssh directory.");
    } else {
        println!("Successfully added {added_count} SSH key(s) to ssh-agent.");
    }

    Ok(first_added)
}

/// SSH agent state management helpers
#[derive(Debug, Clone)]
pub struct BgitSshAgentState {
    pub socket_path: PathBuf,
}

/// Get the expected path for bgit SSH agent socket
pub fn get_bgit_agent_socket_path() -> PathBuf {
    let ssh_dir = home::home_dir()
        .map(|p| p.join(".ssh"))
        .unwrap_or_else(|| PathBuf::from(".ssh"));
    ssh_dir.join(SSH_AGENT_SOCKET_BASENAME)
}

/// Load bgit SSH agent state - check if socket exists and is valid
pub fn load_bgit_agent_state() -> Option<BgitSshAgentState> {
    let socket_path = get_bgit_agent_socket_path();

    // Socket must exist to be considered valid
    if !socket_path.exists() {
        debug!("Bgit agent socket does not exist: {:?}", socket_path);
        return None;
    }

    // On Unix, ensure the socket path is actually a Unix domain socket
    #[cfg(unix)]
    {
        match std::fs::metadata(&socket_path) {
            Ok(md) => {
                use std::os::unix::fs::FileTypeExt;
                if !md.file_type().is_socket() {
                    debug!(
                        "Bgit agent socket path exists but is not a socket: {:?}",
                        socket_path
                    );
                    return None;
                }
            }
            Err(e) => {
                debug!("Failed to stat socket path {:?}: {}", socket_path, e);
                return None;
            }
        }
    }

    debug!("Loaded bgit agent state - socket: {:?}", socket_path);
    Some(BgitSshAgentState { socket_path })
}

/// Clean up bgit SSH agent socket
pub fn cleanup_bgit_agent_state() {
    let socket_path = get_bgit_agent_socket_path();

    if socket_path.exists() {
        if let Err(e) = std::fs::remove_file(&socket_path) {
            debug!("Failed to remove socket file {:?}: {}", socket_path, e);
        } else {
            debug!("Cleaned up socket file: {:?}", socket_path);
        }
    }
}

/// Direct agent verification without recursion - used internally to avoid infinite loops
fn verify_agent_socket_direct(socket_path: &str) -> bool {
    let mut cmd = Command::new("ssh-add");
    cmd.arg("-l").env("SSH_AUTH_SOCK", socket_path);

    match cmd.output() {
        Ok(output) => match output.status.code() {
            Some(0) => {
                debug!("Agent at {} is running with keys", socket_path);
                true
            }
            Some(1) => {
                let stdout = String::from_utf8_lossy(&output.stdout);
                let stderr = String::from_utf8_lossy(&output.stderr);

                if stdout.contains("The agent has no identities") {
                    debug!("Agent at {} is running but empty", socket_path);
                    true
                } else {
                    debug!("Agent at {} command failed: stderr={}", socket_path, stderr);
                    false
                }
            }
            Some(2) => {
                debug!("Agent at {} is not reachable (exit code 2)", socket_path);
                false
            }
            Some(code) => {
                debug!(
                    "Agent at {} returned unexpected exit code: {}",
                    socket_path, code
                );
                false
            }
            None => {
                debug!(
                    "Agent verification at {} was terminated by signal",
                    socket_path
                );
                false
            }
        },
        Err(e) => {
            // Command failed to run (ssh-add not found, permission denied, etc.)
            debug!("Failed to run ssh-add for {}: {}", socket_path, e);
            false
        }
    }
}

/// Set global SSH environment variables for libgit2 compatibility
/// This is needed because libgit2's Cred::ssh_key_from_agent() uses global environment
/// WARNING: This modifies global process state - use carefully!
pub fn set_global_ssh_env_for_libgit2(socket_path: Option<&str>) {
    if let Some(socket) = socket_path {
        debug!("Setting global SSH_AUTH_SOCK for libgit2: {}", socket);
        unsafe { std::env::set_var("SSH_AUTH_SOCK", socket) };
    } else {
        debug!("No SSH_AUTH_SOCK provided - libgit2 will use existing environment");
    }
}

/// Get the current effective SSH auth configuration
/// Returns socket_path - uses bgit state if available, otherwise current environment
pub fn get_effective_ssh_auth() -> Option<String> {
    // First try to load bgit agent state
    if let Some(state) = load_bgit_agent_state() {
        // Verify the socket is actually working - using direct verification to avoid recursion
        let socket_str = state.socket_path.to_string_lossy();
        if verify_agent_socket_direct(&socket_str) {
            debug!("Using bgit agent state: {:?}", state.socket_path);
            return Some(socket_str.to_string());
        } else {
            debug!("Bgit agent socket not working, cleaning up stale state");
            cleanup_bgit_agent_state();
            debug!("Returning None after cleanup to force bgit agent creation");
            return None;
        }
    }

    // Fallback to current environment only if no bgit state was found
    let current_sock = std::env::var("SSH_AUTH_SOCK").ok();

    // Validate environment-provided socket on Unix (must be a socket and working)
    if let Some(ref sock) = current_sock {
        #[cfg(unix)]
        {
            use std::os::unix::fs::FileTypeExt;
            let path = std::path::Path::new(sock);
            let is_socket = std::fs::metadata(path)
                .map(|m| m.file_type().is_socket())
                .unwrap_or(false);
            if !is_socket {
                debug!(
                    "Environment SSH_AUTH_SOCK is not a socket or missing: {:?}",
                    sock
                );
                return None;
            }
        }

        if verify_agent_socket_direct(sock) {
            debug!(
                "Using current environment auth - socket: {:?}",
                current_sock
            );
            return current_sock;
        } else {
            debug!(
                "Environment SSH agent not working for socket {:?}, ignoring",
                sock
            );
            return None;
        }
    }

    debug!("No SSH agent environment available");
    None
}