Skip to main content

anodizer_core/git/
mod.rs

1use anyhow::{Result, bail};
2use std::path::Path;
3use std::process::Command;
4
5mod commits;
6mod conventional;
7mod detect;
8mod github_api;
9mod github_probe;
10mod mailmap;
11mod remote;
12mod semver;
13mod slug;
14mod snapshot_sde;
15mod status;
16mod tags;
17pub mod worktree;
18
19#[cfg(test)]
20mod tests;
21
22pub use commits::{
23    Commit, CommitWithFiles, CommitterIdentity, RELEASE_COMMIT_PREFIX, SHORT_COMMIT_LEN,
24    add_path_in, branches_containing_sha_in, commit_in, commit_subject_in, commits_between_in,
25    commits_with_subjects_in, count_commits_since_last_tag_in, get_all_commits, get_all_commits_in,
26    get_all_commits_paths, get_all_commits_paths_in, get_all_commits_paths_with_files_in,
27    get_commit_messages_between, get_commit_messages_between_in, get_commit_messages_between_path,
28    get_commit_messages_between_path_in, get_commits_between, get_commits_between_in,
29    get_commits_between_paths, get_commits_between_paths_in,
30    get_commits_between_paths_with_files_in, get_commits_reachable_paths_in,
31    get_commits_reachable_paths_with_files_in, get_current_branch, get_current_branch_in,
32    get_current_branch_in_with_env, get_head_commit, get_head_commit_in, get_last_commit_messages,
33    get_last_commit_messages_in, get_last_commit_messages_path, get_last_commit_messages_path_in,
34    get_short_commit, get_short_commit_in, has_changes_since, has_changes_since_in,
35    has_commits_since_tag, has_commits_since_tag_in, head_commit_hash_in, head_commit_timestamp_in,
36    is_branchlike, log_subjects_for_range, parse_commit_output, parse_commit_output_with_files,
37    paths_changed_since_tag, paths_changed_since_tag_in, push_branch_in, release_bump_subject,
38    release_bump_subject_prefix, reset_hard_in, resolve_rollback_identity, rev_parse_in,
39    rev_verify_commit_in, revert_commit_in, short_commit_str, stage_and_commit,
40    stage_and_commit_in,
41};
42pub use conventional::{ConventionalLevel, classify_commit};
43pub use detect::{GitInfo, detect_git_info, detect_git_info_in};
44pub use github_api::{
45    GITHUB_TOKEN_ENV_LADDER, commit_author_login, commit_author_login_with_binary,
46    create_tag_via_github_api, create_tag_via_github_api_in, gh_api_get, gh_api_get_paginated,
47    gh_api_get_paginated_with_binary, gh_api_get_with_binary, github_token_env_hint,
48    github_token_hint, resolve_github_token, resolve_github_token_with_env,
49};
50pub use github_probe::{
51    RepoAccessOutcomes, RepoProbe, github_repo_probe, github_repo_push_check,
52    is_rate_limit_signature, is_secondary_rate_limit_signature, probe_to_push_check,
53    response_is_rate_limited,
54};
55pub use mailmap::canonical_author_email_in;
56// The owner/repo detectors are intentionally NOT re-exported publicly: the
57// only public path to a repository identity is a `slug::resolve_*` function,
58// which applies the config-override -> remote precedence in one place. See
59// `slug.rs`. `detect_remote_web_base_in` stays public — it is the dedicated
60// host-preserving web-base resolver (changelog compare footers) and has no
61// owner/name duplication to funnel.
62pub use remote::{detect_remote_web_base_in, has_remote_in, parse_remote_web_base};
63pub use semver::{SemVer, parse_semver, parse_semver_tag};
64pub use slug::{
65    RepoSlug, resolve_github_slug, resolve_github_slug_in, resolve_repo_slug, resolve_repo_slug_in,
66};
67pub use snapshot_sde::resolve_snapshot_sde;
68pub use status::{
69    check_git_available, git_status_porcelain, git_status_porcelain_in,
70    git_status_porcelain_result_in, is_git_dirty, is_git_dirty_in, is_git_repo, is_git_repo_in,
71    is_shallow_clone, is_shallow_clone_in, list_tracked_files_in, local_git_user_email,
72    local_git_user_email_in, local_git_user_name, local_git_user_name_in,
73};
74pub use tags::{
75    AtomicPushSpec, create_and_push_tag, create_and_push_tag_in, create_tag_local_only,
76    delete_local_tag_in, delete_remote_tag_in, extract_tag_prefix, find_latest_tag_matching,
77    find_latest_tag_matching_in, find_latest_tag_matching_with_prefix,
78    find_latest_tag_matching_with_prefix_in, find_previous_tag, find_previous_tag_in,
79    find_previous_tag_with_prefix, find_previous_tag_with_prefix_in, get_all_semver_tags,
80    get_all_semver_tags_in, get_branch_semver_tags, get_branch_semver_tags_in, get_first_commit,
81    get_first_commit_in, get_tags_at_head, get_tags_at_head_in, get_tags_at_sha_in,
82    has_version_placeholder, head_is_at_tag, list_tags_with_prefix, per_crate_tag_prefix,
83    push_branch_and_tags_atomic_in, render_ignore_patterns, strip_monorepo_prefix,
84    tag_points_at_head, tag_points_at_head_in,
85};
86pub use worktree::Worktree;
87
88/// Run `git` in `cwd` and return stdout, trimmed.
89///
90/// Shared low-level git invocation wrapper. Path-taking so callers
91/// that don't own the process cwd — notably tests against a `tempfile::tempdir()`
92/// fixture repo — can drive git without mutating the process-wide cwd
93/// (which would race every other parallel test). The no-arg public wrappers
94/// in sibling submodules delegate here with `std::env::current_dir()`.
95///
96/// On non-zero exit the stderr is passed through
97/// [`crate::redact::redact_process_env`] before interpolation, so any
98/// token-bearing remote URL git might echo (e.g.
99/// `https://ghp_xxx@github.com/...` produced by an `extraHeader` config
100/// leak) is scrubbed in the bail message.
101pub(crate) fn git_output_in(cwd: &Path, args: &[&str]) -> Result<String> {
102    // `GIT_TERMINAL_PROMPT=0` + `LC_ALL=C` pinned on every spawn so:
103    //   - a credential helper / nested config can't hang the wrapper
104    //     waiting for interactive input (unattended CI hosts),
105    //   - locale-sensitive stderr / stdout messages (e.g. "not found",
106    //     "remote ref does not exist") stay in English so substring
107    //     matching in caller code is stable.
108    let output = Command::new("git")
109        .current_dir(cwd)
110        .args(args)
111        .env("GIT_TERMINAL_PROMPT", "0")
112        .env("LC_ALL", "C")
113        .output()?;
114    if !output.status.success() {
115        let stderr_raw = String::from_utf8_lossy(&output.stderr);
116        let stderr_trim = stderr_raw.trim();
117        // Some git failure paths print only on stdout (notably `git commit`
118        // with "nothing to commit, working tree clean"). When stderr is
119        // empty, fall back to stdout so the bail message is diagnostic
120        // instead of `failed: ` with no further detail.
121        let detail = if stderr_trim.is_empty() {
122            String::from_utf8_lossy(&output.stdout).trim().to_string()
123        } else {
124            stderr_trim.to_string()
125        };
126        let raw = format!("git {} failed: {}", args.join(" "), detail);
127        bail!("{}", crate::redact::redact_process_env(&raw));
128    }
129    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
130}