Skip to content

Commit 26a2977

Browse files
feat: report when forge refspecs are already installed
Assisted-by: Claude Code (Claude Sonnet 4.6)
1 parent c51de31 commit 26a2977

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

crates/git-forge/src/install.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@ pub fn run(remote: Option<&str>, global: bool) -> Result<(), Box<dyn std::error:
99
let push_key = format!("remote.{remote_name}.push");
1010

1111
if global {
12-
add_global_value(&fetch_key, FORGE_FETCH_REFSPEC)?;
13-
add_global_value(&push_key, FORGE_PUSH_REFSPEC)?;
14-
eprintln!(
15-
"Added forge refspecs for remote `{remote_name}` to global git config (~/.gitconfig)."
16-
);
12+
let fetch_added = add_global_value(&fetch_key, FORGE_FETCH_REFSPEC)?;
13+
let push_added = add_global_value(&push_key, FORGE_PUSH_REFSPEC)?;
14+
if fetch_added || push_added {
15+
eprintln!(
16+
"Added forge refspecs for remote `{remote_name}` to global git config (~/.gitconfig)."
17+
);
18+
} else {
19+
eprintln!(
20+
"Forge refspecs for remote `{remote_name}` already present in global git config (~/.gitconfig)."
21+
);
22+
}
1723
} else {
1824
let repo = git2::Repository::open_from_env()?;
1925
let mut config = repo.config()?.open_level(git2::ConfigLevel::Local)?;
20-
add_value_if_missing(&mut config, &fetch_key, FORGE_FETCH_REFSPEC)?;
21-
add_value_if_missing(&mut config, &push_key, FORGE_PUSH_REFSPEC)?;
22-
eprintln!("Added forge refspecs for remote `{remote_name}` to local git config.");
26+
let fetch_added = add_value_if_missing(&mut config, &fetch_key, FORGE_FETCH_REFSPEC)?;
27+
let push_added = add_value_if_missing(&mut config, &push_key, FORGE_PUSH_REFSPEC)?;
28+
if fetch_added || push_added {
29+
eprintln!("Added forge refspecs for remote `{remote_name}` to local git config.");
30+
} else {
31+
eprintln!(
32+
"Forge refspecs for remote `{remote_name}` already present in local git config."
33+
);
34+
}
2335
}
2436

2537
Ok(())
@@ -54,30 +66,32 @@ fn resolve_remote(
5466
}
5567

5668
/// Add `value` under `key` in the given config level, skipping if already present.
69+
/// Returns `true` if the value was added, `false` if it was already present.
5770
fn add_value_if_missing(
5871
config: &mut git2::Config,
5972
key: &str,
6073
value: &str,
61-
) -> Result<(), git2::Error> {
74+
) -> Result<bool, git2::Error> {
6275
// `multivar` lets us check all values for a multi-valued key.
6376
let already_set = config
6477
.multivar(key, Some(&regex_escape(value)))
6578
.map(|mut entries| entries.next().is_some())
6679
.unwrap_or(false);
6780

68-
if !already_set {
69-
config.set_multivar(key, "^$", value)?;
81+
if already_set {
82+
return Ok(false);
7083
}
7184

72-
Ok(())
85+
config.set_multivar(key, "^$", value)?;
86+
Ok(true)
7387
}
7488

7589
/// Add `value` under `key` in the global config, skipping if already present.
76-
fn add_global_value(key: &str, value: &str) -> Result<(), Box<dyn std::error::Error>> {
90+
/// Returns `true` if the value was added, `false` if it was already present.
91+
fn add_global_value(key: &str, value: &str) -> Result<bool, Box<dyn std::error::Error>> {
7792
let global_config = git2::Config::open_default()?;
7893
let mut global_level = global_config.open_level(git2::ConfigLevel::Global)?;
79-
add_value_if_missing(&mut global_level, key, value)?;
80-
Ok(())
94+
Ok(add_value_if_missing(&mut global_level, key, value)?)
8195
}
8296

8397
/// Escape a literal string for use as a regex in `Config::multivar`.

0 commit comments

Comments
 (0)