Skip to content

Commit bfe9ff7

Browse files
committed
Delay git config setup until after home links
1 parent d89a830 commit bfe9ff7

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/home.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ pub fn run(opts: HomeOpts) -> Result<()> {
6060
}
6161
}
6262
let prefer_ssh = ssh::prefer_ssh();
63-
match ssh::ensure_git_ssh_command() {
64-
Ok(true) => println!("Configured git to use 1Password SSH agent."),
65-
Ok(false) => {}
66-
Err(err) => println!("warning: failed to configure git ssh: {}", err),
67-
}
68-
if !prefer_ssh {
69-
match ssh::ensure_git_https_insteadof() {
70-
Ok(true) => println!("Configured git to use HTTPS when SSH isn't available."),
71-
Ok(false) => {}
72-
Err(err) => println!("warning: failed to configure git https rewrites: {}", err),
73-
}
74-
}
7563
let home = dirs::home_dir().context("Could not find home directory")?;
7664
let config_dir = home.join("config");
7765
let repo = coerce_repo_scheme(parse_repo_input(&opts.repo)?, prefer_ssh);
@@ -101,6 +89,19 @@ pub fn run(opts: HomeOpts) -> Result<()> {
10189

10290
let archived = archive_existing_configs(&config_dir)?;
10391
apply_config(&config_dir)?;
92+
93+
match ssh::ensure_git_ssh_command() {
94+
Ok(true) => println!("Configured git to use 1Password SSH agent."),
95+
Ok(false) => {}
96+
Err(err) => println!("warning: failed to configure git ssh: {}", err),
97+
}
98+
if !prefer_ssh {
99+
match ssh::ensure_git_https_insteadof() {
100+
Ok(true) => println!("Configured git to use HTTPS when SSH isn't available."),
101+
Ok(false) => {}
102+
Err(err) => println!("warning: failed to configure git https rewrites: {}", err),
103+
}
104+
}
104105
ensure_kar_repo(&flow_bin, prefer_ssh)?;
105106

106107
if !archived.is_empty() {

src/ssh.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub fn ensure_ssh_env() {
8585
}
8686

8787
pub fn ensure_git_ssh_command() -> Result<bool> {
88+
if !git_config_writable() {
89+
return Ok(false);
90+
}
8891
let Some(sock) = preferred_agent_sock() else {
8992
return Ok(false);
9093
};
@@ -128,6 +131,9 @@ pub fn ensure_git_ssh_command_for_sock(sock: &Path, force: bool) -> Result<bool>
128131
}
129132

130133
pub fn ensure_git_https_insteadof() -> Result<bool> {
134+
if !git_config_writable() {
135+
return Ok(false);
136+
}
131137
let desired = ["git@github.com:", "ssh://git@github.com/"];
132138
let mut changed = false;
133139

@@ -142,6 +148,9 @@ pub fn ensure_git_https_insteadof() -> Result<bool> {
142148
}
143149

144150
pub fn clear_git_https_insteadof() -> Result<bool> {
151+
if !git_config_writable() {
152+
return Ok(false);
153+
}
145154
let desired = ["git@github.com:", "ssh://git@github.com/"];
146155
let mut changed = false;
147156

@@ -372,6 +381,33 @@ fn git_config_get(key: &str) -> Result<Option<String>> {
372381
Ok(Some(value))
373382
}
374383

384+
fn git_config_writable() -> bool {
385+
let home = match std::env::var_os("HOME") {
386+
Some(val) => PathBuf::from(val),
387+
None => return false,
388+
};
389+
if !home.is_dir() {
390+
return false;
391+
}
392+
393+
let path = home.join(".gitconfig");
394+
let meta = match fs::symlink_metadata(&path) {
395+
Ok(meta) => meta,
396+
Err(_) => return true,
397+
};
398+
if meta.is_dir() {
399+
return false;
400+
}
401+
if meta.file_type().is_symlink() {
402+
match fs::metadata(&path) {
403+
Ok(target_meta) => target_meta.is_file(),
404+
Err(_) => false,
405+
}
406+
} else {
407+
true
408+
}
409+
}
410+
375411
fn git_config_get_all(key: &str) -> Result<Vec<String>> {
376412
let output = Command::new("git")
377413
.args(["config", "--global", "--get-all", key])

0 commit comments

Comments
 (0)