Tell agents about stored git credentials; give Claude Code the user's git config
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec95f9b8ad
commit
f0cd404f59
5 files changed
+71
-3
No files matched your search
@@ -53,6 +53,35 @@ pub fn identity_env(app: &App, user_id: &str) -> Vec<(String, String)> {
|
||||
env
|
||||
}
|
||||
|
||||
/// What an agent needs to know about the stored git access (no secrets): git already has the
|
||||
/// credentials, so it must not look for them. None when nothing is stored.
|
||||
pub fn prompt_section(app: &App, user_id: &str) -> Option<String> {
|
||||
render_prompt(&app.store.user_setting(user_id, "git").unwrap_or_default())
|
||||
}
|
||||
|
||||
fn render_prompt(s: &GitSettings) -> Option<String> {
|
||||
let creds: Vec<&GitCredential> = s.credentials.iter().filter(|c| !c.host.trim().is_empty() && !c.token.is_empty()).collect();
|
||||
if creds.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut t = String::from("\n# Git access\nThe user has stored git credentials for these hosts (HTTPS):\n");
|
||||
for c in &creds {
|
||||
t.push_str(&format!("- {} (user {})\n", c.host.trim(), c.username.trim()));
|
||||
}
|
||||
t.push_str(
|
||||
"git uses them automatically through its credential helper: `git clone`, `fetch`, `pull` and `push` with an https:// URL of these hosts just work, no login or token needed. \
|
||||
Do NOT ask the user for a token and do NOT search the environment, config files, `gh`, SSH keys or the keychain for credentials – they are not there, and the command line shows none because git keeps them in its credential store. \
|
||||
Use https:// remote URLs (SSH keys are not set up). \
|
||||
For a REST API of such a host (pull requests, issues, releases) take the token straight from git without printing it, e.g. \
|
||||
`curl -s -H \"Authorization: token $(printf 'protocol=https\\nhost=HOST\\n\\n' | git credential fill | sed -n 's/^password=//p')\" https://HOST/api/...` \
|
||||
(GitHub: `Authorization: Bearer`). Never echo the token or write it into files, commits or your answer.\n",
|
||||
);
|
||||
if !s.name.trim().is_empty() || !s.email.trim().is_empty() {
|
||||
t.push_str(&format!("Commits are made as {} <{}> automatically.\n", s.name.trim(), s.email.trim()));
|
||||
}
|
||||
Some(t)
|
||||
}
|
||||
|
||||
pub fn home_of(app: &App, user: &User) -> PathBuf {
|
||||
app.data_dir().join("users").join(&user.username).join("home")
|
||||
}
|
||||
@@ -159,6 +188,19 @@ pub fn merge(old: &GitSettings, mut new: GitSettings) -> GitSettings {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn prompt_names_hosts_but_never_tokens() {
|
||||
assert!(render_prompt(&GitSettings::default()).is_none());
|
||||
let settings = GitSettings { name: "Ann".into(), email: "ann@x.org".into(), credentials: vec![GitCredential { host: "git.x.org".into(), username: "ann".into(), token: "s3cret-token".into(), protocol: String::new() }] };
|
||||
let p = render_prompt(&settings).unwrap();
|
||||
println!("{p}");
|
||||
assert!(p.contains("git.x.org (user ann)") && p.contains("git credential fill") && p.contains("Ann <ann@x.org>"));
|
||||
assert!(!p.contains("s3cret-token"));
|
||||
// Entries without a token are not offered.
|
||||
let none = GitSettings { credentials: vec![GitCredential { host: "h".into(), username: "u".into(), ..Default::default() }], ..Default::default() };
|
||||
assert!(render_prompt(&none).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_masked_tokens() {
|
||||
let old = GitSettings { credentials: vec![GitCredential { host: "git.x".into(), username: "me".into(), token: "secret".into(), protocol: String::new() }], ..Default::default() };
|
||||
|
||||
Reference in new issue
Block a user