Git.Repo (git v0.4.0)

Copy Markdown View Source

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

t()

@type t() :: %Git.Repo{config: Git.Config.t(), path: String.t()}

Functions

add(repo, opts \\ [])

@spec add(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git add on the repository.

See Git.add/1 for available options.

am(repo, opts \\ [])

@spec am(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git am on the repository.

See Git.am/1 for available options.

apply_patch(repo, opts \\ [])

@spec apply_patch(
  t(),
  keyword()
) :: {:ok, :done} | {:ok, String.t()} | {:error, term()}

Runs git apply on the repository.

See Git.apply_patch/1 for available options.

archive(repo, opts \\ [])

@spec archive(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git archive on the repository.

See Git.archive/1 for available options.

bisect(repo, opts \\ [])

@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.

blame(repo, file, opts \\ [])

@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.

branch(repo, opts \\ [])

@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.

bundle(repo, opts \\ [])

@spec bundle(
  t(),
  keyword()
) :: {:ok, term()} | {:error, term()}

Runs git bundle on the repository.

See Git.bundle/1 for available options.

cat_file(repo, object, opts \\ [])

@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.

check_ignore(repo, opts \\ [])

@spec check_ignore(
  t(),
  keyword()
) :: {:ok, [String.t()] | [map()]} | {:error, term()}

Runs git check-ignore on the repository.

See Git.check_ignore/1 for available options.

checkout(repo, opts \\ [])

@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.

cherry(repo, opts \\ [])

@spec cherry(
  t(),
  keyword()
) :: {:ok, [Git.CherryEntry.t()]} | {:error, term()}

Runs git cherry on the repository.

See Git.cherry/1 for available options.

cherry_pick(repo, opts \\ [])

@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.

clean(repo, opts \\ [])

@spec clean(
  t(),
  keyword()
) :: {:ok, [String.t()]} | {:error, term()}

Runs git clean on the repository.

See Git.clean/1 for available options.

clone(url, path, opts \\ [])

@spec clone(String.t(), String.t(), keyword()) :: {:ok, t()} | {:error, term()}

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)

commit(repo, message, opts \\ [])

@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.

describe(repo, opts \\ [])

@spec describe(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git describe on the repository.

See Git.describe/1 for available options.

diff(repo, opts \\ [])

@spec diff(
  t(),
  keyword()
) :: {:ok, Git.Diff.t()} | {:error, term()}

Runs git diff on the repository.

See Git.diff/1 for available options.

fetch(repo, opts \\ [])

@spec fetch(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git fetch on the repository.

See Git.fetch/1 for available options.

for_each_ref(repo, opts \\ [])

@spec for_each_ref(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git for-each-ref on the repository.

See Git.for_each_ref/1 for available options.

format_patch(repo, opts \\ [])

@spec format_patch(
  t(),
  keyword()
) :: {:ok, [String.t()]} | {:ok, String.t()} | {:error, term()}

Runs git format-patch on the repository.

See Git.format_patch/1 for available options.

fsck(repo, opts \\ [])

@spec fsck(
  t(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Runs git fsck on the repository.

See Git.fsck/1 for available options.

gc(repo, opts \\ [])

@spec gc(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git gc on the repository.

See Git.gc/1 for available options.

git_config(repo, opts \\ [])

@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.

grep(repo, pattern, opts \\ [])

@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.

hash_object(repo, opts \\ [])

@spec hash_object(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git hash-object on the repository.

See Git.hash_object/1 for available options.

init(path, opts \\ [])

@spec init(
  String.t(),
  keyword()
) :: {:ok, t()} | {:error, term()}

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 - when true, initializes a bare repository (default false)

Examples

{:ok, repo} = Git.Repo.init("/tmp/new-repo")
{:ok, repo} = Git.Repo.init("/tmp/bare-repo", bare: true)

interpret_trailers(repo, opts \\ [])

@spec interpret_trailers(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git interpret-trailers on the repository.

See Git.interpret_trailers/1 for available options.

log(repo, opts \\ [])

@spec log(
  t(),
  keyword()
) :: {:ok, [Git.Commit.t()]} | {:error, term()}

Runs git log on the repository.

See Git.log/1 for available options.

ls_files(repo, opts \\ [])

@spec ls_files(
  t(),
  keyword()
) :: {:ok, [String.t()]} | {:error, term()}

Runs git ls-files on the repository.

See Git.ls_files/1 for available options.

ls_remote(repo, opts \\ [])

@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.

ls_tree(repo, opts \\ [])

@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.

maintenance(repo, opts \\ [])

@spec maintenance(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git maintenance on the repository.

See Git.maintenance/1 for available options.

merge(repo, branch_or_abort, opts \\ [])

@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.

merge_base(repo, opts \\ [])

@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.

mv(repo, source, destination, opts \\ [])

@spec mv(t(), String.t(), String.t(), keyword()) :: {:ok, :done} | {:error, term()}

Runs git mv on the repository.

See Git.mv/3 for available options.

notes(repo, opts \\ [])

@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.

ok(repo)

@spec ok(t()) :: {:ok, t()}

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)

open(path)

@spec open(String.t()) :: {:ok, t()} | {:error, term()}

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"

open!(path)

@spec open!(String.t()) :: t()

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")

pull(repo, opts \\ [])

@spec pull(
  t(),
  keyword()
) :: {:ok, Git.PullResult.t()} | {:error, term()}

Runs git pull on the repository.

See Git.pull/1 for available options.

push(repo, opts \\ [])

@spec push(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git push on the repository.

See Git.push/1 for available options.

range_diff(repo, opts \\ [])

@spec range_diff(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git range-diff on the repository.

See Git.range_diff/1 for available options.

rebase(repo, opts \\ [])

@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.

reflog(repo, opts \\ [])

@spec reflog(
  t(),
  keyword()
) :: {:ok, [Git.ReflogEntry.t()]} | {:error, term()}

Runs git reflog on the repository.

See Git.reflog/1 for available options.

remote(repo, opts \\ [])

@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.

rerere(repo, opts \\ [])

@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.

reset(repo, opts \\ [])

@spec reset(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git reset on the repository.

See Git.reset/1 for available options.

restore(repo, opts \\ [])

@spec restore(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git restore on the repository.

See Git.restore/1 for available options.

rev_list(repo, opts \\ [])

@spec rev_list(
  t(),
  keyword()
) :: {:ok, [String.t()] | integer() | map()} | {:error, term()}

Runs git rev-list on the repository.

See Git.rev_list/1 for available options.

rev_parse(repo, opts \\ [])

@spec rev_parse(
  t(),
  keyword()
) :: {:ok, String.t()} | {:error, term()}

Runs git rev-parse on the repository.

See Git.rev_parse/1 for available options.

revert(repo, opts \\ [])

@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.

rm(repo, opts \\ [])

@spec rm(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git rm on the repository.

See Git.rm/1 for available options.

run(error, fun)

@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)

shortlog(repo, opts \\ [])

@spec shortlog(
  t(),
  keyword()
) :: {:ok, [Git.ShortlogEntry.t()]} | {:error, term()}

Runs git shortlog on the repository.

See Git.shortlog/1 for available options.

show(repo, opts \\ [])

@spec show(
  t(),
  keyword()
) :: {:ok, Git.ShowResult.t()} | {:error, term()}

Runs git show on the repository.

See Git.show/1 for available options.

show_ref(repo, opts \\ [])

@spec show_ref(
  t(),
  keyword()
) :: {:ok, term()} | {:error, term()}

Runs git show-ref on the repository.

See Git.show_ref/1 for available options.

sparse_checkout(repo, opts \\ [])

@spec sparse_checkout(
  t(),
  keyword()
) :: {:ok, [String.t()]} | {:ok, :done} | {:error, term()}

Runs git sparse-checkout on the repository.

See Git.sparse_checkout/1 for available options.

stash(repo, opts \\ [])

@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.

status(repo, opts \\ [])

@spec status(
  t(),
  keyword()
) :: {:ok, Git.Status.t()} | {:error, term()}

Runs git status on the repository.

See Git.status/1 for available options.

submodule(repo, opts \\ [])

@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.

switch(repo, opts \\ [])

@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.

symbolic_ref(repo, opts \\ [])

@spec symbolic_ref(
  t(),
  keyword()
) :: {:ok, String.t() | :done} | {:error, term()}

Runs git symbolic-ref on the repository.

See Git.symbolic_ref/1 for available options.

tag(repo, opts \\ [])

@spec tag(
  t(),
  keyword()
) :: {:ok, [Git.Tag.t()]} | {:ok, :done} | {:error, term()}

Runs git tag on the repository.

See Git.tag/1 for available options.

update_ref(repo, opts \\ [])

@spec update_ref(
  t(),
  keyword()
) :: {:ok, :done} | {:error, term()}

Runs git update-ref on the repository.

See Git.update_ref/1 for available options.

verify_commit(repo, commit, opts \\ [])

@spec verify_commit(t(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}

Runs git verify-commit on the repository.

See Git.verify_commit/2 for available options.

verify_tag(repo, tag, opts \\ [])

@spec verify_tag(t(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}

Runs git verify-tag on the repository.

See Git.verify_tag/2 for available options.

worktree(repo, opts \\ [])

@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.