Git Operations¶
git
¶
Generic git operations for CI pipelines.
Host-side git operations: clone, push, branch creation, diff inspection. All operations use subprocess calls to git.
GitDiffError
¶
Bases: Exception
Raised when git diff fails (missing ref, not a repo, etc.).
extract_repo_url(text)
¶
Extract a repo URL from text, validating against forge APIs.
Filters out subpaths, file extensions, and placeholder URLs. Returns the first URL that resolves to a real project, or the first unvalidated candidate if no API tokens are available.
Source code in src/agentic_ci/git.py
extract_all_repo_urls(text)
¶
Extract all distinct repo root URLs from text.
Scans for both GitLab and GitHub URLs, filters out subpaths, file extensions, and placeholder URLs. GitLab URLs that are strict prefixes of other GitLab URLs are collapsed (nested group dedup).
Unlike :func:extract_repo_url, this does not validate URLs
against forge APIs -- it returns all plausible candidates.
Source code in src/agentic_ci/git.py
validate_repo_url(url)
¶
Check that a repo URL points to an allowed host with no path traversal.
Source code in src/agentic_ci/git.py
validate_branch_exists(repo_url, branch)
¶
Check if a branch exists on the remote repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
HTTPS URL of the git repository |
required |
branch
|
str
|
Branch name to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the branch exists on the remote, False otherwise |
Note
Returns False for any error condition (network issues, invalid refs, etc.) to allow graceful fallback in the resolution chain.
Source code in src/agentic_ci/git.py
clone_repo(url, dest, branch=None, depth=None)
¶
Clone a repository. Returns True on success.
Source code in src/agentic_ci/git.py
create_branch(repo_dir, branch_name)
¶
Create and checkout a new branch.
Source code in src/agentic_ci/git.py
checkout_branch(repo_dir, branch)
¶
Checkout an existing branch. Returns True on success.
Source code in src/agentic_ci/git.py
rebase_branch(repo_dir, onto)
¶
Rebase the current branch onto onto. Returns True on success.
On conflict the rebase is aborted so the worktree stays clean.
Source code in src/agentic_ci/git.py
get_default_branch(repo_dir)
¶
Detect the default branch of the remote origin.
Runs git rev-parse --abbrev-ref origin/HEAD and strips the
origin/ prefix. Falls back to "main" when the remote HEAD
cannot be determined.
Source code in src/agentic_ci/git.py
git_output(repo_dir, *args)
¶
Run a git command and return its stripped stdout, or None on error.
This is a thin wrapper around subprocess.run for cases where
the caller only needs the text output of a git command.
Source code in src/agentic_ci/git.py
push_branch(repo_dir, remote='origin', branch=None)
¶
Push the current branch to remote. Returns True on success.
Source code in src/agentic_ci/git.py
setup_git_config(repo_dir, name, email)
¶
Set local git user config.
Source code in src/agentic_ci/git.py
harden_git_config(repo_dir)
¶
Apply security hardening to git config (disable hooks, fsmonitor).
Source code in src/agentic_ci/git.py
get_commit_info(repo_dir)
¶
Get the latest commit info (committer, email, message, sha).
Uses committer identity (not author) so that rebased or cherry-picked commits always reflect the current git config.
Source code in src/agentic_ci/git.py
get_changed_files(repo_dir, base_ref='HEAD~1')
¶
Return files changed between base_ref and HEAD (committed state only).
Uses the two-ref form git diff --name-only <base_ref> HEAD so that
files still in HEAD after a failed git commit --amend are detected
even when git rm --cached already removed them from the index.
Raises GitDiffError if the git command fails.
Source code in src/agentic_ci/git.py
strip_committed_files(repo_dir, patterns, base_ref='origin/HEAD')
¶
Remove files matching patterns from the latest commit.
Agents can bypass .git/info/exclude by explicitly naming files in
git add. This function detects any committed files that match the
given fnmatch patterns and amends the commit to remove them, keeping
the working-tree copies intact.
Returns the list of file paths actually stripped (empty if none matched or all removals failed).
Source code in src/agentic_ci/git.py
setup_git_credentials(repo_url, *, github_token_resolver=None)
¶
Configure git url.insteadOf for the forge hosting repo_url.
Sets up transparent credential injection so that clone_repo() and
push_branch() (which use bare HTTPS URLs) can authenticate
without modification.
For GitLab, reads BOT_PAT from the environment.
For GitHub, calls github_token_resolver(repo_url) to obtain a
short-lived token. If no resolver is provided for GitHub URLs,
returns False.
Idempotent and safe to call multiple times. Returns True on success, False if credentials are unavailable.