Minga.Git (Minga v0.1.0)

Copy Markdown View Source

Git operations, delegated to a configurable backend.

In production, uses Minga.Git.System which shells out to the git CLI. In tests, swap to Minga.Git.Stub to avoid spawning OS processes:

config :minga, git_module: Minga.Git.Stub

Summary

Types

Accepted diff options. Unknown keys, duplicate keys, and invalid values are rejected. :commit may be combined with :staged, but only when :staged is false.

A structured log entry.

A structured stash entry.

A structured status entry for one file.

Accepted status/2 options.

How much untracked-file detail git status should enumerate.

Functions

Returns ahead/behind counts relative to the upstream tracking branch.

Gets blame information for a specific line of a file.

Creates a new branch and checks it out.

Lists all branches (local and remote).

Switches to an existing branch.

Creates a commit with the given message.

Returns the parsed merge conflict count for a tracked buffer.

Returns parsed merge conflict regions for a tracked buffer.

Returns the current branch name for a git repository.

Returns the diff for a specific file or all changes.

Computes line-level diff hunks between two lists of lines.

Discards working tree changes for a file. Destructive and irreversible.

Fetches from all remotes.

Returns gutter sign indicators (line → hunk type) for a tracked buffer.

Returns the hunk at a specific line, or nil.

Returns all diff hunks for a tracked buffer.

Returns the message of the most recent commit.

Returns recent commits as structured entries.

Finds the Repo process for a git root path, or nil if not started.

Returns modeline-ready git info (branch, hunk counts) for a tracked buffer.

Pulls from the upstream remote (fetch + merge).

Pushes the current branch to its upstream remote.

Triggers a background refresh of the Repo's cached git state.

Returns the path of a file relative to the git root.

Returns cached status entries from a running Repo process.

Returns a summary map (branch, ahead/behind, file counts) from a Repo.

Reverts a single hunk, restoring the base content for those lines.

Finds the git repository root for a file path.

Reads the HEAD version of a file from git.

Reads the staged (index) version of a file from git.

Stages specific files (equivalent to git add).

Applies a unified diff patch to the git index (staging area).

Saves current worktree changes to a stash.

Drops a stash by index.

Lists stashes for a repository.

Pops the most recent stash.

Returns a structured list of changed files with their status.

Synchronizes a tracked buffer cache with the provided content.

Returns the git tracking process for a buffer, or nil if untracked.

Unstages specific files from the index (equivalent to git reset HEAD -- <paths>).

Unstages all staged files (equivalent to git reset HEAD).

Types

diff_opt()

@type diff_opt() :: {:path, String.t()} | {:staged, boolean()} | {:commit, String.t()}

Accepted diff options. Unknown keys, duplicate keys, and invalid values are rejected. :commit may be combined with :staged, but only when :staged is false.

diff_opts()

@type diff_opts() :: [diff_opt()]

log_entry()

@type log_entry() :: Minga.Git.LogEntry.t()

A structured log entry.

stash_entry()

@type stash_entry() :: Minga.Git.StashEntry.t()

A structured stash entry.

status_entry()

@type status_entry() :: Minga.Git.StatusEntry.t()

A structured status entry for one file.

status_opt()

@type status_opt() ::
  {:untracked_mode, untracked_mode()} | {:timeout_ms, pos_integer()}

Accepted status/2 options.

status_opts()

@type status_opts() :: [status_opt()]

untracked_mode()

@type untracked_mode() :: :no | :normal | :all

How much untracked-file detail git status should enumerate.

Functions

ahead_behind(git_root)

@spec ahead_behind(String.t()) :: {:ok, non_neg_integer(), non_neg_integer()} | :error

Returns ahead/behind counts relative to the upstream tracking branch.

blame_line(git_root, relative_path, line_number)

@spec blame_line(String.t(), String.t(), non_neg_integer()) ::
  {:ok, String.t()} | :error

Gets blame information for a specific line of a file.

Returns {:ok, blame_text} with a human-readable blame string, or :error if blame fails.

branch_create(git_root, name)

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

Creates a new branch and checks it out.

branch_delete(git_root, name, force \\ false)

@spec branch_delete(String.t(), String.t(), boolean()) :: :ok | {:error, String.t()}

Deletes a branch.

branch_list(git_root)

@spec branch_list(String.t()) ::
  {:ok, [Minga.Git.BranchInfo.t()]} | {:error, String.t()}

Lists all branches (local and remote).

branch_switch(git_root, name)

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

Switches to an existing branch.

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

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

Creates a commit with the given message.

Options: :amend (boolean, default false) to amend the previous commit.

conflict_count(git_buffer)

@spec conflict_count(GenServer.server()) :: non_neg_integer()

Returns the parsed merge conflict count for a tracked buffer.

conflicts(git_buffer)

Returns parsed merge conflict regions for a tracked buffer.

current_branch(git_root)

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

Returns the current branch name for a git repository.

Returns {:ok, branch_name} or :error if it can't be determined (e.g., detached HEAD, not a git repo).

diff(git_root, opts \\ [])

@spec diff(String.t(), diff_opts()) :: {:ok, String.t()} | {:error, String.t()}

Returns the diff for a specific file or all changes.

Options: :path (file path), :staged (boolean, default false), :commit (commit hash). :commit may be combined with :staged, false; commit diffs can still be narrowed with :path.

diff_lines(base_lines, current_lines)

@spec diff_lines([String.t()], [String.t()]) :: [Minga.Core.Diff.hunk()]

Computes line-level diff hunks between two lists of lines.

discard(git_root, path)

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

Discards working tree changes for a file. Destructive and irreversible.

For tracked files, runs git checkout -- <path>. For untracked files, deletes the file.

fetch_remotes(git_root, opts \\ [])

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

Fetches from all remotes.

gutter_signs(git_buffer)

@spec gutter_signs(GenServer.server()) :: %{required(non_neg_integer()) => atom()}

Returns gutter sign indicators (line → hunk type) for a tracked buffer.

hunk_at(git_buffer, line)

@spec hunk_at(GenServer.server(), non_neg_integer()) :: Minga.Core.Diff.hunk() | nil

Returns the hunk at a specific line, or nil.

hunks(git_buffer)

@spec hunks(GenServer.server()) :: [Minga.Core.Diff.hunk()]

Returns all diff hunks for a tracked buffer.

last_commit_message(git_root)

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

Returns the message of the most recent commit.

log(git_root, opts \\ [])

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

Returns recent commits as structured entries.

Options: :count (default 10), :path (limit to file).

lookup_repo(git_root)

@spec lookup_repo(String.t()) :: pid() | nil

Finds the Repo process for a git root path, or nil if not started.

modeline_info(git_buffer)

@spec modeline_info(GenServer.server()) :: Minga.Git.Buffer.modeline_info()

Returns modeline-ready git info (branch, hunk counts) for a tracked buffer.

pull(git_root, opts \\ [])

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

Pulls from the upstream remote (fetch + merge).

push(git_root, opts \\ [])

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

Pushes the current branch to its upstream remote.

refresh_repo(repo_pid)

@spec refresh_repo(pid()) :: :ok

Triggers a background refresh of the Repo's cached git state.

relative_path(git_root, file_path)

@spec relative_path(String.t(), String.t()) :: String.t()

Returns the path of a file relative to the git root.

repo_status(repo_pid)

@spec repo_status(pid()) :: [status_entry()]

Returns cached status entries from a running Repo process.

repo_summary(repo_pid)

@spec repo_summary(pid()) :: map()

Returns a summary map (branch, ahead/behind, file counts) from a Repo.

revert_hunk(current_lines, hunk)

@spec revert_hunk([String.t()], Minga.Core.Diff.hunk()) :: [String.t()]

Reverts a single hunk, restoring the base content for those lines.

root_for(path)

@spec root_for(String.t()) :: {:ok, String.t()} | :not_git

Finds the git repository root for a file path.

Returns {:ok, root_path} if the file is inside a git repo, or :not_git if it isn't.

show_head(git_root, relative_path)

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

Reads the HEAD version of a file from git.

Returns {:ok, content} with the file content at HEAD, or :error if the file doesn't exist in HEAD (new file, not tracked, etc.).

show_staged(git_root, relative_path)

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

Reads the staged (index) version of a file from git.

Returns {:ok, content} with the file content in the index, or :error if the file is not staged.

stage(git_root, paths)

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

Stages specific files (equivalent to git add).

stage_patch(git_root, patch)

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

Applies a unified diff patch to the git index (staging area).

stash(git_root, opts \\ [])

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

Saves current worktree changes to a stash.

stash_drop(git_root, index)

@spec stash_drop(String.t(), non_neg_integer()) :: :ok | {:error, String.t()}

Drops a stash by index.

stash_list(git_root)

@spec stash_list(String.t()) :: {:ok, [stash_entry()]} | {:error, String.t()}

Lists stashes for a repository.

stash_pop(git_root)

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

Pops the most recent stash.

status(git_root, opts \\ [])

@spec status(String.t(), status_opts()) ::
  {:ok, [status_entry()]} | {:error, String.t()}

Returns a structured list of changed files with their status.

sync_tracked_buffer(buffer_pid, content)

@spec sync_tracked_buffer(pid(), String.t()) :: :ok

Synchronizes a tracked buffer cache with the provided content.

tracking_pid(buffer_pid)

@spec tracking_pid(pid()) :: pid() | nil

Returns the git tracking process for a buffer, or nil if untracked.

Composes the Tracker lookup so callers don't need to know about the Tracker → Buffer two-step.

unstage(git_root, paths)

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

Unstages specific files from the index (equivalent to git reset HEAD -- <paths>).

unstage_all(git_root)

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

Unstages all staged files (equivalent to git reset HEAD).