Skip to main content

codetether_agent/a2a/git_credentials/
repo_config.rs

1//! Repository-local Git credential configuration.
2//!
3//! This module wires repositories to the generated helper script and stores
4//! GitHub App metadata used by downstream Git operations.
5//!
6//! # Examples
7//!
8//! ```ignore
9//! let helper = configure_repo_git_auth(repo_path, "ws-1")?;
10//! ```
11
12use anyhow::{Result, anyhow};
13use std::path::{Path, PathBuf};
14
15use super::git_config::{run_git_command, set_local_config};
16use super::write_git_credential_helper_script;
17
18/// Configures a repository to use the CodeTether Git credential helper.
19///
20/// The helper path is returned so callers can inspect or clean it up later.
21///
22/// # Examples
23///
24/// ```ignore
25/// let helper = configure_repo_git_auth(repo_path, "ws-1")?;
26/// ```
27pub fn configure_repo_git_auth(repo_path: &Path, workspace_id: &str) -> Result<PathBuf> {
28    let helper_path = repo_path.join(".codetether-git-credential-helper");
29    write_git_credential_helper_script(&helper_path, workspace_id)?;
30    let helper_path_str = helper_path
31        .to_str()
32        .ok_or_else(|| anyhow!("Helper path is not valid UTF-8"))?;
33    run_git_command(
34        repo_path,
35        &["config", "--local", "credential.helper", helper_path_str],
36    )?;
37    run_git_command(
38        repo_path,
39        &["config", "--local", "credential.useHttpPath", "true"],
40    )?;
41    run_git_command(
42        repo_path,
43        &["config", "--local", "codetether.workspaceId", workspace_id],
44    )?;
45    Ok(helper_path)
46}
47
48/// Stores GitHub App identifiers in repository-local Git config.
49///
50/// Empty values are ignored so callers can pass optional server metadata.
51///
52/// # Examples
53///
54/// ```ignore
55/// configure_repo_git_github_app(repo_path, Some("1"), Some("2"))?;
56/// ```
57pub fn configure_repo_git_github_app(
58    repo_path: &Path,
59    installation_id: Option<&str>,
60    app_id: Option<&str>,
61) -> Result<()> {
62    set_local_config(
63        repo_path,
64        "codetether.githubInstallationId",
65        installation_id,
66    )?;
67    set_local_config(repo_path, "codetether.githubAppId", app_id)?;
68    Ok(())
69}