blob: c5f885091975de0ef00ad9089838afff62c71965 (
plain)
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
|
use std::path::Path;
use anyhow::{Context, Result};
use directories::UserDirs;
use git2::opts::{set_server_connect_timeout_in_milliseconds, set_server_timeout_in_milliseconds};
pub fn check_ssh_keys() -> bool {
// Get the user's home directory using the directories crate
if let Some(user_dirs) = UserDirs::new() {
let ssh_dir = user_dirs.home_dir().join(".ssh");
let key_files = vec![
"id_rsa",
"id_ecdsa",
"id_ed25519",
"id_rsa.pub",
"id_ecdsa.pub",
"id_ed25519.pub",
];
for key in key_files {
if Path::new(&ssh_dir.join(key)).exists() {
return true; // At least one key exists
}
}
}
false // No keys found
}
pub fn set_git_timeout() -> Result<()> {
unsafe {
// Set a 3 000 ms timeout for establishing the TCP connection (default: 60 000
// ms).
set_server_connect_timeout_in_milliseconds(3_000)
.context("failed to set libgit2 connect timeout")?;
// The server timeout applies per socket send()/recv() call rather than
// to the entire fetch or push. libgit2 transfers data in ~16 KiB chunks,
// so each chunk’s transfer is subject to this timeout instead of the
// overall command.
//
// We set it to 15 000 ms (instead of the 300 000 ms default) to quickly
// abort any stalled ~16 KiB chunk transfer—enabling fast failover across
// redundant Git servers—while still accommodating transient hiccups.
set_server_timeout_in_milliseconds(15_000).context("failed to set libgit2 I/O timeout")?;
Ok(())
}
}
|