A stateful repository abstraction for the git CLI wrapper.
Git.Repo holds a Git.Config struct and the resolved repository path,
providing a cleaner API for working with a specific repository. Instead of
passing config: config to every Git function, you create a Repo and
call functions on it.
Opening an existing repository
{:ok, repo} = Git.Repo.open("/path/to/repo")
{:ok, status} = Git.Repo.status(repo)
{:ok, commits} = Git.Repo.log(repo, max_count: 5)Initializing a new repository
{:ok, repo} = Git.Repo.init("/tmp/new-repo")Cloning a repository
{:ok, repo} = Git.Repo.clone("https://github.com/owner/repo.git", "/tmp/clone")Pipeline pattern
Git.Repo.open("/path/to/repo")
|> Git.Repo.run(fn repo ->
Git.Repo.add(repo, all: true)
Git.Repo.commit(repo, "feat: new feature")
{:ok, repo}
end)
Summary
Functions
Runs git add on the repository.
Runs git am on the repository.
Runs git apply on the repository.
Runs git archive on the repository.
Runs git bisect on the repository.
Runs git blame on the repository.
Runs git branch on the repository.
Runs git bundle on the repository.
Runs git cat-file on the repository.
Runs git check-ignore on the repository.
Runs git checkout on the repository.
Runs git cherry on the repository.
Runs git cherry-pick on the repository.
Runs git clean on the repository.
Clones a repository from url into path.
Runs git commit on the repository.
Runs git describe on the repository.
Runs git diff on the repository.
Runs git fetch on the repository.
Runs git for-each-ref on the repository.
Runs git format-patch on the repository.
Runs git fsck on the repository.
Runs git gc on the repository.
Runs git config on the repository.
Runs git grep on the repository.
Runs git hash-object on the repository.
Initializes a new git repository at path.
Runs git interpret-trailers on the repository.
Runs git log on the repository.
Runs git ls-files on the repository.
Runs git ls-remote on the repository.
Runs git ls-tree on the repository.
Runs git maintenance on the repository.
Runs git merge on the repository.
Runs git merge-base on the repository.
Runs git mv on the repository.
Runs git notes on the repository.
Wraps a repo in an ok tuple for use as the start of a pipeline.
Opens an existing git repository at path.
Opens an existing git repository at path, raising on error.
Runs git pull on the repository.
Runs git push on the repository.
Runs git range-diff on the repository.
Runs git rebase on the repository.
Runs git reflog on the repository.
Runs git remote on the repository.
Runs git rerere on the repository.
Runs git reset on the repository.
Runs git restore on the repository.
Runs git rev-list on the repository.
Runs git rev-parse on the repository.
Runs git revert on the repository.
Runs git rm on the repository.
Runs a function in a pipeline, halting on error.
Runs git shortlog on the repository.
Runs git show on the repository.
Runs git show-ref on the repository.
Runs git sparse-checkout on the repository.
Runs git stash on the repository.
Runs git status on the repository.
Runs git submodule on the repository.
Runs git switch on the repository.
Runs git symbolic-ref on the repository.
Runs git tag on the repository.
Runs git update-ref on the repository.
Runs git verify-commit on the repository.
Runs git verify-tag on the repository.
Runs git worktree on the repository.
Types
@type t() :: %Git.Repo{config: Git.Config.t(), path: String.t()}
Functions
Runs git add on the repository.
See Git.add/1 for available options.
Runs git am on the repository.
See Git.am/1 for available options.
Runs git apply on the repository.
See Git.apply_patch/1 for available options.
Runs git archive on the repository.
See Git.archive/1 for available options.
@spec bisect( t(), keyword() ) :: {:ok, Git.BisectResult.t()} | {:ok, :done} | {:error, term()}
Runs git bisect on the repository.
See Git.bisect/1 for available options.
@spec blame(t(), String.t(), keyword()) :: {:ok, [Git.BlameEntry.t()]} | {:error, term()}
Runs git blame on the repository.
See Git.blame/2 for available options.
@spec branch( t(), keyword() ) :: {:ok, [Git.Branch.t()]} | {:ok, :done} | {:error, term()}
Runs git branch on the repository.
See Git.branch/1 for available options.
Runs git bundle on the repository.
See Git.bundle/1 for available options.
@spec cat_file(t(), String.t(), keyword()) :: {:ok, atom()} | {:ok, integer()} | {:ok, String.t()} | {:ok, boolean()} | {:error, term()}
Runs git cat-file on the repository.
See Git.cat_file/2 for available options.
Runs git check-ignore on the repository.
See Git.check_ignore/1 for available options.
@spec checkout( t(), keyword() ) :: {:ok, Git.Checkout.t()} | {:ok, :done} | {:error, term()}
Runs git checkout on the repository.
See Git.checkout/1 for available options.
@spec cherry( t(), keyword() ) :: {:ok, [Git.CherryEntry.t()]} | {:error, term()}
Runs git cherry on the repository.
See Git.cherry/1 for available options.
@spec cherry_pick( t(), keyword() ) :: {:ok, Git.CherryPickResult.t()} | {:ok, :done} | {:error, term()}
Runs git cherry-pick on the repository.
See Git.cherry_pick/1 for available options.
Runs git clean on the repository.
See Git.clean/1 for available options.
Clones a repository from url into path.
Runs git clone and returns a Git.Repo pointing at the cloned directory.
Options
:depth- create a shallow clone with the given number of commits:branch- check out the given branch after cloning:directory- name of the target directory (default: inferred from URL)
Examples
{:ok, repo} = Git.Repo.clone("https://github.com/owner/repo.git", "/tmp/clone")
{:ok, repo} = Git.Repo.clone("https://github.com/owner/repo.git", "/tmp/clone", depth: 1)
@spec commit(t(), String.t(), keyword()) :: {:ok, Git.CommitResult.t()} | {:error, term()}
Runs git commit on the repository.
See Git.commit/2 for available options.
Runs git describe on the repository.
See Git.describe/1 for available options.
@spec diff( t(), keyword() ) :: {:ok, Git.Diff.t()} | {:error, term()}
Runs git diff on the repository.
See Git.diff/1 for available options.
Runs git fetch on the repository.
See Git.fetch/1 for available options.
Runs git for-each-ref on the repository.
See Git.for_each_ref/1 for available options.
Runs git format-patch on the repository.
See Git.format_patch/1 for available options.
Runs git fsck on the repository.
See Git.fsck/1 for available options.
Runs git gc on the repository.
See Git.gc/1 for available options.
@spec git_config( t(), keyword() ) :: {:ok, String.t()} | {:ok, [{String.t(), String.t()}]} | {:ok, :done} | {:error, term()}
Runs git config on the repository.
See Git.git_config/1 for available options.
@spec grep(t(), String.t(), keyword()) :: {:ok, [Git.GrepResult.t()]} | {:error, term()}
Runs git grep on the repository.
See Git.grep/2 for available options.
Runs git hash-object on the repository.
See Git.hash_object/1 for available options.
Initializes a new git repository at path.
Creates the directory if it does not exist, runs git init, and returns
a Git.Repo pointing at the new repository.
Options
:bare- whentrue, initializes a bare repository (defaultfalse)
Examples
{:ok, repo} = Git.Repo.init("/tmp/new-repo")
{:ok, repo} = Git.Repo.init("/tmp/bare-repo", bare: true)
Runs git interpret-trailers on the repository.
See Git.interpret_trailers/1 for available options.
@spec log( t(), keyword() ) :: {:ok, [Git.Commit.t()]} | {:error, term()}
Runs git log on the repository.
See Git.log/1 for available options.
Runs git ls-files on the repository.
See Git.ls_files/1 for available options.
@spec ls_remote( t(), keyword() ) :: {:ok, [Git.LsRemoteEntry.t()]} | {:error, term()}
Runs git ls-remote on the repository.
See Git.ls_remote/1 for available options.
@spec ls_tree( t(), keyword() ) :: {:ok, [Git.TreeEntry.t()] | [String.t()]} | {:error, term()}
Runs git ls-tree on the repository.
See Git.ls_tree/1 for available options.
Runs git maintenance on the repository.
See Git.maintenance/1 for available options.
@spec merge(t(), String.t() | :abort, keyword()) :: {:ok, Git.MergeResult.t()} | {:ok, :done} | {:error, term()}
Runs git merge on the repository.
See Git.merge/2 for available options.
@spec merge_base( t(), keyword() ) :: {:ok, String.t() | boolean() | [String.t()]} | {:error, term()}
Runs git merge-base on the repository.
See Git.merge_base/1 for available options.
Runs git mv on the repository.
See Git.mv/3 for available options.
@spec notes( t(), keyword() ) :: {:ok, [map()]} | {:ok, String.t()} | {:ok, :done} | {:error, term()}
Runs git notes on the repository.
See Git.notes/1 for available options.
Wraps a repo in an ok tuple for use as the start of a pipeline.
Examples
Git.Repo.ok(repo)
|> Git.Repo.run(fn repo -> ... end)
Opens an existing git repository at path.
Validates that the path exists and is a git repository by calling
git rev-parse --show-toplevel. The resolved toplevel path is stored in
both the struct and the config's working_dir.
Returns {:ok, %Git.Repo{}} on success or {:error, reason} on failure.
Examples
{:ok, repo} = Git.Repo.open("/path/to/repo")
repo.path #=> "/path/to/repo"
Opens an existing git repository at path, raising on error.
Same as open/1 but raises on failure.
Examples
repo = Git.Repo.open!("/path/to/repo")
@spec pull( t(), keyword() ) :: {:ok, Git.PullResult.t()} | {:error, term()}
Runs git pull on the repository.
See Git.pull/1 for available options.
Runs git push on the repository.
See Git.push/1 for available options.
Runs git range-diff on the repository.
See Git.range_diff/1 for available options.
@spec rebase( t(), keyword() ) :: {:ok, Git.RebaseResult.t()} | {:ok, :done} | {:error, term()}
Runs git rebase on the repository.
See Git.rebase/1 for available options.
@spec reflog( t(), keyword() ) :: {:ok, [Git.ReflogEntry.t()]} | {:error, term()}
Runs git reflog on the repository.
See Git.reflog/1 for available options.
@spec remote( t(), keyword() ) :: {:ok, [Git.Remote.t()]} | {:ok, :done} | {:error, term()}
Runs git remote on the repository.
See Git.remote/1 for available options.
@spec rerere( t(), keyword() ) :: {:ok, [String.t()]} | {:ok, String.t()} | {:ok, :done} | {:error, term()}
Runs git rerere on the repository.
See Git.rerere/1 for available options.
Runs git reset on the repository.
See Git.reset/1 for available options.
Runs git restore on the repository.
See Git.restore/1 for available options.
Runs git rev-list on the repository.
See Git.rev_list/1 for available options.
Runs git rev-parse on the repository.
See Git.rev_parse/1 for available options.
@spec revert( t(), keyword() ) :: {:ok, Git.RevertResult.t()} | {:ok, :done} | {:error, term()}
Runs git revert on the repository.
See Git.revert/1 for available options.
Runs git rm on the repository.
See Git.rm/1 for available options.
@spec run({:ok, t()} | {:error, term()}, (t() -> {:ok, t()} | {:error, term()})) :: {:ok, t()} | {:error, term()}
Runs a function in a pipeline, halting on error.
If given {:ok, repo}, calls fun.(repo). If given {:error, _},
passes the error through unchanged.
Examples
Git.Repo.open("/path/to/repo")
|> Git.Repo.run(fn repo ->
Git.Repo.add(repo, all: true)
{:ok, repo}
end)
@spec shortlog( t(), keyword() ) :: {:ok, [Git.ShortlogEntry.t()]} | {:error, term()}
Runs git shortlog on the repository.
See Git.shortlog/1 for available options.
@spec show( t(), keyword() ) :: {:ok, Git.ShowResult.t()} | {:error, term()}
Runs git show on the repository.
See Git.show/1 for available options.
Runs git show-ref on the repository.
See Git.show_ref/1 for available options.
Runs git sparse-checkout on the repository.
See Git.sparse_checkout/1 for available options.
@spec stash( t(), keyword() ) :: {:ok, [Git.StashEntry.t()]} | {:ok, :done} | {:error, term()}
Runs git stash on the repository.
See Git.stash/1 for available options.
@spec status( t(), keyword() ) :: {:ok, Git.Status.t()} | {:error, term()}
Runs git status on the repository.
See Git.status/1 for available options.
@spec submodule( t(), keyword() ) :: {:ok, [Git.SubmoduleEntry.t()]} | {:ok, :done} | {:ok, String.t()} | {:error, term()}
Runs git submodule on the repository.
See Git.submodule/1 for available options.
@spec switch( t(), keyword() ) :: {:ok, Git.Checkout.t()} | {:ok, :done} | {:error, term()}
Runs git switch on the repository.
See Git.switch/1 for available options.
Runs git symbolic-ref on the repository.
See Git.symbolic_ref/1 for available options.
Runs git tag on the repository.
See Git.tag/1 for available options.
Runs git update-ref on the repository.
See Git.update_ref/1 for available options.
Runs git verify-commit on the repository.
See Git.verify_commit/2 for available options.
Runs git verify-tag on the repository.
See Git.verify_tag/2 for available options.
@spec worktree( t(), keyword() ) :: {:ok, [Git.Worktree.t()]} | {:ok, :done} | {:error, term()}
Runs git worktree on the repository.
See Git.worktree/1 for available options.