Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

33.0 KB
Raw Blame
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use url::Url;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_name: Option<String>,
    pub branch: String,
    pub commit_id: String,
}

struct GitRepositoryPaths {
    git_dir: PathBuf,
    common_dir: PathBuf,
    worktree_dir: PathBuf,
}

pub fn get_git_info(dir: Option<PathBuf>) -> Result<Option<GitInfo>> {
    if let Some(info) = get_git_info_from_env(get_env_variable) {
        return Ok(Some(info));
    }

    let repository_paths = match find_git_repository_paths(dir) {
        Some(paths) => paths,
        None => return Ok(None),
    };

    let remote_url = get_remote_url_from_paths(&repository_paths);
    let repo_name = get_repo_name_from_paths(&repository_paths);
    let branch =
        get_branch_name(&repository_paths.git_dir).context("Failed to determine current branch")?;
    let commit =
        get_commit_sha(&repository_paths, &branch).context("Failed to determine commit sha")?;

    Ok(Some(GitInfo {
        remote_url,
        repo_name,
        branch,
        commit_id: commit,
    }))
}

#[doc(hidden)]
pub fn get_git_info_from_env(get_env: impl Fn(&str) -> Option<String>) -> Option<GitInfo> {
    if let Some(info) = get_git_info_from_github(&get_env) {
        return Some(info);
    }

    get_git_info_from_vercel(&get_env)
}

fn get_git_info_from_github(get_env: &impl Fn(&str) -> Option<String>) -> Option<GitInfo> {
    get_env("GITHUB_ACTIONS")?;

    let branch = get_env("GITHUB_REF_NAME")?;
    let commit_id = get_env("GITHUB_SHA")?;
    let repository = get_env("GITHUB_REPOSITORY")?;
    let server_url = get_env("GITHUB_SERVER_URL")?;

    let repo_name = repository.split('/').next_back().map(|s| s.to_string());
    let remote_url = Some(format!("{server_url}/{repository}.git"));

    Some(GitInfo {
        remote_url,
        repo_name,
        branch,
        commit_id,
    })
}

fn get_git_info_from_vercel(get_env: &impl Fn(&str) -> Option<String>) -> Option<GitInfo> {
    get_env("VERCEL")?;

    let branch = get_env("VERCEL_GIT_COMMIT_REF")?;
    let commit_id = get_env("VERCEL_GIT_COMMIT_SHA")?;
    let repo_slug = get_env("VERCEL_GIT_REPO_SLUG")?;

    let remote_url = build_vercel_remote_url(&repo_slug, get_env);

    Some(GitInfo {
        remote_url,
        repo_name: Some(repo_slug),
        branch,
        commit_id,
    })
}

fn build_vercel_remote_url(
    repo_slug: &str,
    get_env: &impl Fn(&str) -> Option<String>,
) -> Option<String> {
    let provider = get_env("VERCEL_GIT_PROVIDER")?;
    let owner = get_env("VERCEL_GIT_REPO_OWNER")?;

    let base_url = match provider.as_str() {
        "github" => "https://github.com",
        "gitlab" => "https://gitlab.com",
        "bitbucket" => "https://bitbucket.org",
        _ => return None,
    };

    Some(format!("{base_url}/{owner}/{repo_slug}.git"))
}

fn find_git_repository_paths(dir: Option<PathBuf>) -> Option<GitRepositoryPaths> {
    let mut current_dir = dir.unwrap_or(std::env::current_dir().ok()?);

    loop {
        let git_path = current_dir.join(".git");
        if git_path.is_dir() {
            let git_dir = normalize_existing_path(git_path);
            return Some(GitRepositoryPaths {
                common_dir: git_dir.clone(),
                git_dir,
                worktree_dir: current_dir,
            });
        }

        if git_path.is_file() {
            let git_dir = parse_git_dir_file(&git_path)?;
            let common_dir = get_common_dir(&git_dir);
            return Some(GitRepositoryPaths {
                git_dir,
                common_dir,
                worktree_dir: current_dir,
            });
        }

        if !current_dir.pop() {
            return None;
        }
    }
}

fn parse_git_dir_file(git_path: &Path) -> Option<PathBuf> {
    let content = fs::read_to_string(git_path).ok()?;
    let git_dir = content.trim().strip_prefix("gitdir:")?.trim();
    let git_dir = PathBuf::from(git_dir);

    let git_dir = if git_dir.is_absolute() {
        git_dir
    } else {
        git_path.parent()?.join(git_dir)
    };

    Some(normalize_existing_path(git_dir))
}

fn get_common_dir(git_dir: &Path) -> PathBuf {
    let commondir_path = git_dir.join("commondir");
    let Ok(commondir) = fs::read_to_string(commondir_path) else {
        return git_dir.to_path_buf();
    };

    let commondir = PathBuf::from(commondir.trim());
    let common_dir = if commondir.is_absolute() {
        commondir
    } else {
        git_dir.join(commondir)
    };

    normalize_existing_path(common_dir)
}

fn normalize_existing_path(path: PathBuf) -> PathBuf {
    fs::canonicalize(&path).unwrap_or(path)
}

fn config_paths(git_dir: &Path, common_dir: &Path) -> Vec<PathBuf> {
    let mut paths = vec![git_dir.join("config.worktree")];

    if common_dir != git_dir {
        paths.push(common_dir.join("config"));
    }

    paths.push(git_dir.join("config"));
    paths
}

pub fn get_remote_url(git_dir: &Path) -> Option<String> {
    get_remote_url_from_paths(&GitRepositoryPaths {
        git_dir: git_dir.to_path_buf(),
        common_dir: git_dir.to_path_buf(),
        worktree_dir: git_dir.parent().unwrap_or(git_dir).to_path_buf(),
    })
}

fn get_remote_url_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
    let url = find_remote_url(paths)?;
    let sanitized = strip_credentials(&url)?;

    if sanitized.ends_with(".git") {
        Some(sanitized)
    } else {
        Some(format!("{sanitized}.git"))
    }
}

/// Finds the URL of the repository's own remote, across the config files that apply to this
/// worktree. `origin` wins when it is present, because that is the remote the release belongs
/// to; otherwise the first remote in config precedence order is used.
fn find_remote_url(paths: &GitRepositoryPaths) -> Option<String> {
    // Ordered from the highest precedence config file to the lowest, so the first URL found
    // for a remote name is the one git itself would use.
    let mut remotes: Vec<(String, String)> = Vec::new();

    for config_path in config_paths(&paths.git_dir, &paths.common_dir) {
        let Ok(config_content) = fs::read_to_string(&config_path) else {
            continue;
        };

        for (name, url) in parse_remote_urls(&config_content) {
            if !remotes.iter().any(|(known, _)| *known == name) {
                remotes.push((name, url));
            }
        }
    }

    remotes
        .iter()
        .find(|(name, _)| name == "origin")
        .or_else(|| remotes.first())
        .map(|(_, url)| url.clone())
}

/// Reads every `[remote "<name>"] url = <url>` entry out of one git config file, in file order.
///
/// Git config is section based, so a scan that only looks for a `url = ` line also matches
/// sections that are not remotes. A superproject with submodules is the common case: `git
/// submodule add` writes a `[submodule "<path>"] url = ...` entry, and that entry sits above
/// the `[remote "origin"]` section whenever the repository got its remote after its
/// submodules. A section blind reader then reports the submodule's URL as the repository's
/// remote.
fn parse_remote_urls(config_content: &str) -> Vec<(String, String)> {
    let mut remotes = Vec::new();
    let mut remote_name: Option<String> = None;

    for line in config_content.lines() {
        let mut line = strip_config_comment(line.trim());

        if line.starts_with('[') {
            let Some(header_end) = find_section_header_end(line) else {
                // The section is unreadable, so every key below it is too.
                remote_name = None;
                continue;
            };
            remote_name = parse_remote_section_header(&line[1..header_end]);
            // Git allows a key on the same line as the section header.
            line = line[header_end + 1..].trim();
        }

        let Some(remote_name) = remote_name.as_deref() else {
            continue;
        };
        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        if !key.trim().eq_ignore_ascii_case("url") {
            continue;
        }

        let url = parse_config_value(value);
        if !url.is_empty() {
            remotes.push((remote_name.to_string(), url));
        }
    }

    remotes
}

/// Returns the index of the `]` that closes a section header. A subsection name is quoted and
/// can hold a `]` of its own, which git accepts: `git remote add 'deploy]prod' <url>` writes
/// `[remote "deploy]prod"]`.
fn find_section_header_end(line: &str) -> Option<usize> {
    let mut in_quotes = false;
    let mut escaped = false;

    for (index, character) in line.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }

        match character {
            '\\' => escaped = true,
            '"' => in_quotes = !in_quotes,
            ']' if !in_quotes => return Some(index),
            _ => {}
        }
    }

    None
}

/// Returns the remote name of a `remote "<name>"` section header, and `None` for every other
/// section. Section names are case insensitive, subsection names are not.
fn parse_remote_section_header(header: &str) -> Option<String> {
    let (section, subsection) = header.trim().split_once('"')?;
    if !section.trim().eq_ignore_ascii_case("remote") {
        return None;
    }

    let name = unescape_config_subsection(subsection.strip_suffix('"')?);
    if name.is_empty() {
        return None;
    }

    Some(name)
}

/// Drops a trailing `#` or `;` comment. A `#` inside a quoted value is part of the value, and a
/// URL can hold one as a fragment.
fn strip_config_comment(line: &str) -> &str {
    let mut in_quotes = false;
    let mut escaped = false;

    for (index, character) in line.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }

        match character {
            '\\' => escaped = true,
            '"' => in_quotes = !in_quotes,
            '#' | ';' if !in_quotes => return line[..index].trim_end(),
            _ => {}
        }
    }

    line
}

/// Decodes a git config subsection name. Git keeps the escaped character of `\"` and `\\`, and
/// drops a backslash that precedes anything else: it reads `[remote "ori\gin"]` as the remote
/// `origin` and `[remote "tab\there"]` as `tabthere`. A value follows different rules, so it
/// has its own decoder.
fn unescape_config_subsection(name: &str) -> String {
    let mut parsed = String::with_capacity(name.len());
    let mut characters = name.chars();

    while let Some(character) = characters.next() {
        match character {
            '\\' => parsed.extend(characters.next()),
            _ => parsed.push(character),
        }
    }

    parsed
}

/// Unwraps a git config value: it can be quoted, and a quoted value can escape a quote or a
/// backslash.
fn parse_config_value(value: &str) -> String {
    unescape_config_string(value.trim())
}

/// Drops the quotes around a git config string, and resolves the escapes a quoted string can
/// hold.
fn unescape_config_string(value: &str) -> String {
    let mut parsed = String::with_capacity(value.len());
    let mut characters = value.chars();

    while let Some(character) = characters.next() {
        match character {
            '"' => {}
            '\\' => match characters.next() {
                Some(escaped @ ('"' | '\\')) => parsed.push(escaped),
                Some(other) => {
                    parsed.push('\\');
                    parsed.push(other);
                }
                None => parsed.push('\\'),
            },
            _ => parsed.push(character),
        }
    }

    parsed
}

/// Drops every part of a URL that can carry a credential before it is stored anywhere: the
/// userinfo component (`user[:pass]@`), the query, and the fragment. CI checkouts commonly
/// write a remote URL with an embedded credential (e.g. `actions/checkout`'s
/// `https://x-access-token:<token>@github.com/owner/repo.git`), and that credential must never
/// reach release metadata.
///
/// Returns `None` for a URL whose credential sits where no parser can identify it, rather than
/// store it. That covers a URL holding an `@` that does not parse, because userinfo must
/// percent-encode `/`, `?` and `#`, and a URL holding an `@` in its path.
fn strip_credentials(url: &str) -> Option<String> {
    // A query or a fragment can hold a token (`?token=`, `#access_token=`) and a git remote
    // needs neither, so both go before anything else looks at the URL.
    let trimmed = match url.find(['?', '#']) {
        Some(index) => &url[..index],
        None => url,
    };

    // Dropping the delimiter can drop an `@` with it, which leaves nothing to tell a malformed
    // credential (`https://user:token?x@host/owner/repo.git`) from a query that holds an `@`.
    // What remains can be the credential itself, so the input is refused rather than stored.
    if url.contains('@') && !trimmed.contains('@') {
        return None;
    }
    let url = trimmed;

    // SCP-like SSH remotes (`git@host:owner/repo.git`) have no `://` authority to parse, and
    // the leading `git` is a fixed SSH username rather than a stored secret. A second `@` sits
    // in the path, where the rule below applies.
    if !url.contains("://") {
        let path = url.split_once(':').map_or("", |(_, path)| path);
        return if path.contains('@') {
            None
        } else {
            Some(url.to_string())
        };
    }

    let Ok(mut parsed) = Url::parse(url) else {
        return if url.contains('@') {
            None
        } else {
            Some(url.to_string())
        };
    };

    // An `@` in the path is a credential written into the wrong position, most often
    // `https://host/${TOKEN}@host/owner/repo.git` from a CI script that meant to write
    // `https://${TOKEN}@host/owner/repo.git`. Nothing tells that token from a path segment.
    if parsed.path().contains('@') {
        return None;
    }

    // Return the input untouched when it holds no credential, so the parser never reshapes a
    // URL that this function does not need to change.
    if parsed.username().is_empty() && parsed.password().is_none() {
        return Some(url.to_string());
    }

    // Both setters fail only for a URL that cannot have an authority, such as `mailto:`. A URL
    // that parsed with userinfo always has one.
    parsed.set_username("").ok()?;
    parsed.set_password(None).ok()?;
    Some(parsed.to_string())
}

pub fn get_repo_name(git_dir: &Path) -> Option<String> {
    get_repo_name_from_paths(&GitRepositoryPaths {
        git_dir: git_dir.to_path_buf(),
        common_dir: git_dir.to_path_buf(),
        worktree_dir: git_dir.parent().unwrap_or(git_dir).to_path_buf(),
    })
}

fn get_repo_name_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
    // Try grab it from the configured remote, otherwise just use the directory name.
    // A remote with no path puts the authority in the last segment, so the name is taken from
    // the sanitized URL. Fall back to the directory name when the URL cannot be sanitized,
    // rather than name the repository after a credential.
    if let Some(sanitized) = find_remote_url(paths).and_then(|url| strip_credentials(&url)) {
        if let Some(repo_name) = sanitized.split('/').next_back() {
            let clean_name = repo_name.trim_end_matches(".git");
            if !clean_name.is_empty() {
                return Some(clean_name.to_string());
            }
        }
    }

    if let Some(name) = paths.worktree_dir.file_name() {
        return Some(name.to_string_lossy().to_string());
    }

    None
}

fn get_branch_name(git_dir: &Path) -> Result<String> {
    // First try to read from HEAD file
    let head_path = git_dir.join("HEAD");
    let mut head_content = String::new();
    fs::File::open(&head_path)
        .with_context(|| format!("Failed to open HEAD file at {head_path:?}"))?
        .read_to_string(&mut head_content)
        .context("Failed to read HEAD file")?;

    // Parse HEAD content
    if head_content.starts_with("ref: refs/heads/") {
        Ok(head_content
            .trim_start_matches("ref: refs/heads/")
            .trim()
            .to_string())
    } else if head_content.trim().len() == 40 || head_content.trim().len() == 64 {
        Ok("HEAD-detached".to_string())
    } else {
        anyhow::bail!("Unrecognized HEAD format")
    }
}

fn get_commit_sha(paths: &GitRepositoryPaths, branch: &str) -> Result<String> {
    let git_dir = &paths.git_dir;

    if branch == "HEAD-detached" {
        // For detached HEAD, read directly from HEAD
        let head_path = git_dir.join("HEAD");
        let mut head_content = String::new();
        fs::File::open(&head_path)
            .with_context(|| format!("Failed to open HEAD file at {head_path:?}"))?
            .read_to_string(&mut head_content)
            .context("Failed to read HEAD file")?;

        return Ok(head_content.trim().to_string());
    }

    // Try to read the commit from the branch reference (loose ref)
    for ref_path in branch_ref_paths(paths, branch) {
        if !ref_path.exists() {
            continue;
        }

        let mut commit_id = String::new();
        fs::File::open(&ref_path)
            .with_context(|| format!("Failed to open branch reference at {ref_path:?}"))?
            .read_to_string(&mut commit_id)
            .context("Failed to read branch reference file")?;

        return Ok(commit_id.trim().to_string());
    }

    // Fall back to packed-refs — Git packs loose refs into this file during
    // garbage collection or clone, which is common on Windows and in CI.
    if let Some(commit_id) = get_packed_ref(paths, branch) {
        return Ok(commit_id);
    }

    anyhow::bail!("Could not determine commit ID")
}

fn branch_ref_paths(paths: &GitRepositoryPaths, branch: &str) -> Vec<PathBuf> {
    let mut ref_paths = vec![paths.git_dir.join("refs/heads").join(branch)];

    if paths.common_dir != paths.git_dir {
        ref_paths.push(paths.common_dir.join("refs/heads").join(branch));
    }

    ref_paths
}

fn get_packed_ref(paths: &GitRepositoryPaths, branch: &str) -> Option<String> {
    let ref_name = format!("refs/heads/{branch}");
    let mut packed_ref_paths = vec![paths.git_dir.join("packed-refs")];

    if paths.common_dir != paths.git_dir {
        packed_ref_paths.push(paths.common_dir.join("packed-refs"));
    }

    for path in packed_ref_paths {
        let Ok(content) = fs::read_to_string(path) else {
            continue;
        };

        if let Some(commit_id) = parse_packed_refs(&content, &ref_name) {
            return Some(commit_id);
        }
    }

    None
}

fn parse_packed_refs(content: &str, ref_name: &str) -> Option<String> {
    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') || line.starts_with('^') {
            continue;
        }

        let mut parts = line.split_whitespace();
        let Some(commit_id) = parts.next() else {
            continue;
        };
        let Some(packed_ref) = parts.next() else {
            continue;
        };

        if packed_ref == ref_name {
            return Some(commit_id.to_string());
        }
    }

    None
}

fn get_env_variable(name: &str) -> Option<String> {
    let env_variable = std::env::var(name).ok()?.trim().to_string();
    match env_variable.as_ref() {
        "" => None,
        _ => Some(env_variable),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn strip_credentials_handles_remote_url_shapes() {
        // `None` means the URL is refused, so no credential can reach release metadata.
        let cases = [
            (
                "github actions checkout token",
                "https://x-access-token:[email protected]/owner/repo.git",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "generic user and password",
                "https://user:secret@host/owner/repo.git",
                Some("https://host/owner/repo.git"),
            ),
            (
                "token as the username",
                "https://[email protected]/owner/repo.git",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "host and port are kept",
                "https://user:[email protected]:8443/owner/repo.git",
                Some("https://git.example.com:8443/owner/repo.git"),
            ),
            (
                "the path survives the rewrite",
                "https://x-access-token:[email protected]/owner/repo.git",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "a query is dropped, because it can hold a token",
                "https://github.com/owner/repo.git?token=ghs_abc",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "a fragment is dropped, because it can hold a token",
                "https://github.com/owner/repo.git#access_token=ghs_abc",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "a query is dropped from an scp-like ssh remote too",
                "[email protected]:owner/repo.git?token=ghs_abc",
                Some("[email protected]:owner/repo.git"),
            ),
            (
                "url without a credential is unchanged",
                "https://github.com/owner/repo.git",
                Some("https://github.com/owner/repo.git"),
            ),
            (
                "scp-like ssh remote is unchanged, because `git` is a fixed ssh username",
                "[email protected]:owner/repo.git",
                Some("[email protected]:owner/repo.git"),
            ),
            (
                "a token in the path is refused, because nothing tells it from a path segment",
                "https://github.com/[email protected]/owner/repo.git",
                None,
            ),
            (
                "any other `@` in the path is refused for the same reason",
                "https://github.com/owner/[email protected]",
                None,
            ),
            (
                "a second `@` in an scp-like path is refused too",
                "[email protected]:ghs_abc123@owner/repo.git",
                None,
            ),
            (
                "a `?` before the `@` is refused, because dropping the query drops the `@`",
                "https://user:[email protected]/owner/repo.git",
                None,
            ),
            (
                "a `#` before the `@` is refused for the same reason",
                "https://user:ghp_abc123#[email protected]/owner/repo.git",
                None,
            ),
            (
                "the truncated prefix is refused even when it parses as a host on its own",
                "https://[email protected]/owner/repo.git",
                None,
            ),
            (
                "ssh url with a credential",
                "ssh://user:secret@host/owner/repo.git",
                Some("ssh://host/owner/repo.git"),
            ),
            (
                "git protocol url is unchanged",
                "git://github.com/owner/repo.git",
                Some("git://github.com/owner/repo.git"),
            ),
            (
                "unencoded `/` in a password hides the credential, so the url is refused",
                "https://user:ab/[email protected]/owner/repo.git",
                None,
            ),
        ];

        for (name, url, expected) in cases {
            assert_eq!(strip_credentials(url).as_deref(), expected, "case: {name}");
        }
    }

    fn write_config(url: &str) -> (tempfile::TempDir, GitRepositoryPaths) {
        write_config_content(&format!("[remote \"origin\"]\n\turl = {url}\n"))
    }

    fn write_config_content(content: &str) -> (tempfile::TempDir, GitRepositoryPaths) {
        let dir = tempfile::tempdir().unwrap();
        let git_dir = dir.path().join(".git");
        fs::create_dir_all(&git_dir).unwrap();
        fs::write(git_dir.join("config"), content).unwrap();
        let paths = GitRepositoryPaths {
            git_dir: git_dir.clone(),
            common_dir: git_dir,
            worktree_dir: dir.path().to_path_buf(),
        };
        (dir, paths)
    }

    #[test]
    fn parse_remote_urls_reads_only_remote_sections() {
        let config = r#"
[core]
    repositoryformatversion = 0
[submodule "vendor/lib"]
    url = https://github.com/PostHog/vendored.git
    active = true
[remote "upstream"]
    url = https://github.com/Other/upstream.git
[remote "origin"]
    url = https://github.com/PostHog/posthog.git
    pushurl = https://github.com/PostHog/fork.git
"#;

        assert_eq!(
            parse_remote_urls(config),
            vec![
                (
                    "upstream".to_string(),
                    "https://github.com/Other/upstream.git".to_string()
                ),
                (
                    "origin".to_string(),
                    "https://github.com/PostHog/posthog.git".to_string()
                ),
            ]
        );
    }

    #[test]
    fn parse_remote_urls_handles_config_syntax() {
        let cases = [
            (
                "no spaces around the separator",
                "[remote \"origin\"]\nurl=https://github.com/PostHog/posthog.git\n",
                vec!["https://github.com/PostHog/posthog.git"],
            ),
            (
                "a quoted value",
                "[remote \"origin\"]\n\turl = \"https://github.com/PostHog/posthog.git\"\n",
                vec!["https://github.com/PostHog/posthog.git"],
            ),
            (
                "a key on the section header line",
                "[remote \"origin\"] url = https://github.com/PostHog/posthog.git\n",
                vec!["https://github.com/PostHog/posthog.git"],
            ),
            (
                "a trailing comment",
                "[remote \"origin\"]\n\turl = https://github.com/PostHog/posthog.git ; the remote\n",
                vec!["https://github.com/PostHog/posthog.git"],
            ),
            (
                "a section name in a different case",
                "[REMOTE \"origin\"]\n\tURL = https://github.com/PostHog/posthog.git\n",
                vec!["https://github.com/PostHog/posthog.git"],
            ),
            (
                "a url section, which names the URL in the header rather than a value",
                "[url \"[email protected]:\"]\n\tinsteadOf = https://github.com/\n",
                vec![],
            ),
            (
                "a key that only ends in url",
                "[remote \"origin\"]\n\tpushurl = https://github.com/PostHog/fork.git\n",
                vec![],
            ),
            (
                "an unterminated section header",
                "[remote \"origin\"\n\turl = https://github.com/PostHog/posthog.git\n",
                vec![],
            ),
        ];

        for (name, config, expected) in cases {
            let urls: Vec<String> = parse_remote_urls(config)
                .into_iter()
                .map(|(_, url)| url)
                .collect();
            assert_eq!(urls, expected, "case: {name}");
        }
    }

    #[test]
    fn parse_remote_urls_reads_a_remote_name_that_holds_config_syntax() {
        // `git remote add 'deploy]prod' <url>` is accepted and writes this header, so the
        // closing bracket of the header is the one outside the quoted name.
        let cases = [
            (
                "a bracket in the name",
                "[remote \"deploy]prod\"]\n\turl = https://github.com/PostHog/posthog.git\n",
                "deploy]prod",
            ),
            (
                "a comment character in the name",
                "[remote \"deploy#1\"]\n\turl = https://github.com/PostHog/posthog.git\n",
                "deploy#1",
            ),
            (
                "an escaped quote in the name",
                "[remote \"deploy\\\"prod\"]\n\turl = https://github.com/PostHog/posthog.git\n",
                "deploy\"prod",
            ),
            (
                "an escaped backslash in the name",
                "[remote \"back\\\\slash\"]\n\turl = https://github.com/PostHog/posthog.git\n",
                "back\\slash",
            ),
            (
                // Git drops a backslash that precedes anything else, so this names `origin`.
                "a backslash before another character in the name",
                "[remote \"ori\\gin\"]\n\turl = https://github.com/PostHog/posthog.git\n",
                "origin",
            ),
        ];

        for (name, config, expected_remote) in cases {
            assert_eq!(
                parse_remote_urls(config),
                vec![(
                    expected_remote.to_string(),
                    "https://github.com/PostHog/posthog.git".to_string()
                )],
                "case: {name}"
            );
        }
    }

    #[test]
    fn get_repo_infos_prefer_origin_written_with_a_dropped_escape() {
        // Git reads `[remote "ori\gin"]` as the remote `origin`, so the release belongs to it
        // rather than to the remote above it.
        let (_dir, paths) = write_config_content(
            "[remote \"upstream\"]\n\turl = https://github.com/Other/upstream.git\n\
             [remote \"ori\\gin\"]\n\turl = https://github.com/PostHog/posthog.git\n",
        );

        assert_eq!(
            get_remote_url_from_paths(&paths),
            Some("https://github.com/PostHog/posthog.git".to_string())
        );
    }

    #[test]
    fn get_repo_infos_prefer_origin_over_another_remote() {
        let (_dir, paths) = write_config_content(
            r#"
[submodule "vendor/lib"]
    url = https://github.com/PostHog/vendored.git
[remote "upstream"]
    url = https://github.com/Other/upstream.git
[remote "origin"]
    url = https://github.com/PostHog/posthog.git
"#,
        );

        assert_eq!(
            get_remote_url_from_paths(&paths),
            Some("https://github.com/PostHog/posthog.git".to_string())
        );
        assert_eq!(
            get_repo_name_from_paths(&paths),
            Some("posthog".to_string())
        );
    }

    #[test]
    fn get_repo_infos_use_the_only_remote_when_there_is_no_origin() {
        let (_dir, paths) = write_config_content(
            r#"
[submodule "vendor/lib"]
    url = https://github.com/PostHog/vendored.git
[remote "upstream"]
    url = https://github.com/Other/upstream.git
"#,
        );

        assert_eq!(
            get_remote_url_from_paths(&paths),
            Some("https://github.com/Other/upstream.git".to_string())
        );
        assert_eq!(
            get_repo_name_from_paths(&paths),
            Some("upstream".to_string())
        );
    }

    #[test]
    fn get_repo_infos_ignore_a_submodule_url_and_fall_back_to_the_directory_name() {
        let (dir, paths) = write_config_content(
            r#"
[submodule "vendor/lib"]
    url = https://github.com/PostHog/vendored.git
"#,
        );

        assert_eq!(get_remote_url_from_paths(&paths), None);
        assert_eq!(
            get_repo_name_from_paths(&paths),
            Some(
                dir.path()
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .to_string()
            )
        );
    }

    #[test]
    fn get_remote_url_from_paths_strips_credential_from_config() {
        let (_dir, paths) =
            write_config("https://x-access-token:[email protected]/owner/repo.git");

        assert_eq!(
            get_remote_url_from_paths(&paths),
            Some("https://github.com/owner/repo.git".to_string())
        );

        // The `.git` suffix is normalized after the query goes, so the suffix check sees the
        // real end of the URL.
        let (_dir, paths) =
            write_config("https://x-access-token:[email protected]/owner/repo.git?ref=main");

        assert_eq!(
            get_remote_url_from_paths(&paths),
            Some("https://github.com/owner/repo.git".to_string())
        );
    }

    #[test]
    fn get_repo_name_from_paths_never_returns_a_credential() {
        let (_dir, paths) = write_config("https://user:[email protected]/owner/repo.git");
        assert_eq!(get_repo_name_from_paths(&paths), Some("repo".to_string()));

        // A truncated prefix parses as a host, which would otherwise name the repository
        // after the credential.
        let (dir, paths) = write_config("https://[email protected]/owner/repo.git");
        let name = get_repo_name_from_paths(&paths).unwrap();
        assert!(
            !name.contains("ghp_abc123"),
            "credential leaked into repo name: {name}"
        );
        assert_eq!(name, dir.path().file_name().unwrap().to_string_lossy());

        // A remote with no path would otherwise name the repository after the authority.
        let (dir, paths) = write_config("https://user:[email protected]");
        let name = get_repo_name_from_paths(&paths).unwrap();
        assert!(
            !name.contains("ghp_abc123"),
            "credential leaked into repo name: {name}"
        );
        assert_eq!(name, dir.path().file_name().unwrap().to_string_lossy());
    }
}