Workspace.Git (Workspace v0.4.2)

View Source

Helper git related functions

Summary

Functions

Detects the changed files in the given directory.

Get the changed files of the head git reference since it diverged from base.

Returns the files of the repository

Get the relative path of the given path in its git repository

Get the git repo root of the given path

Returns the submodules of the repository

Returns a list of uncommitted files

Get list of untracked files

Types

change_type()

@type change_type() :: :uncommitted | :untracked | :modified

Functions

changed(opts \\ [])

@spec changed(opts :: keyword()) ::
  {:ok, [{binary(), change_type()}]} | {:error, binary()}

Detects the changed files in the given directory.

By default the following files are included:

  • Uncommitted files in the working directory
  • Untracked files in the working directory
  • If :base is provided it also includes the files changed on :head since it diverged from :base, e.g. git diff base...head. :head defaults to HEAD if not set.

A list of tuples of the form {"path/to/changed/file", change_type} is returned, where change_type can be one of the following:

  • :uncommitted - for changed files under version control that are not committed
  • :untracked - for new files that are not under version control
  • :modified - for changed committed files on :head since it diverged from :base

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.
  • :base (binary/0) - The base reference to use for comparing to the HEAD, can be a branch, a commit or any other git reference.
  • :head (binary/0) - The head to use for comparing to :base, if not set defaults to HEAD. Can be any git reference

changed_files(base, head, opts \\ [])

@spec changed_files(base :: binary(), head :: binary(), opts :: keyword()) ::
  {:ok, [binary()]} | {:error, binary()}

Get the changed files of the head git reference since it diverged from base.

The head is compared against the merge base of base and head, e.g. changes of base after head diverged from it are not included. This is equivalent to git diff base...head.

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.

files(opts \\ [])

@spec files(opts :: keyword()) :: {:ok, [binary()]} | {:error, binary()}

Returns the files of the repository

Both tracked files and untracked files which are not ignored are included. The paths are relative to the :cd path.

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.
  • :pathspec (list/0) - Only the files matching any of the given git pathspecs are returned, defaults to all files.

prefix(opts \\ [])

@spec prefix(opts :: keyword()) :: {:ok, binary()} | {:error, binary()}

Get the relative path of the given path in its git repository

Returns {:ok, prefix} in case of success, where prefix is an empty string for the repository root, or {:error, reason} in case of failure.

Options

  • :cd - the path to get the prefix of, if not set defaults to the current working directory.

root(opts \\ [])

@spec root(opts :: keyword()) :: {:ok, binary()} | {:error, binary()}

Get the git repo root of the given path

Returns {:ok, path} in case of success or {:error, reason} in case of failure.

If the given path is under a symlinked directory, the symlinked form of the root is returned, so that it can be safely compared with paths derived from the given path. If the path is itself a symlink pointing inside a repository there is no symlinked form of the root and the resolved root is returned.

Options

  • :cd - the path to use for getting the git root, if not set defaults to the current working directory.

submodules(opts \\ [])

@spec submodules(opts :: keyword()) :: {:ok, [binary()]} | {:error, binary()}

Returns the submodules of the repository

The paths of the submodules registered in the index are returned, relative to the :cd path, whether they are initialized or not. Nested submodules are not included.

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.

uncommitted_files(opts \\ [])

@spec uncommitted_files(opts :: keyword()) :: {:ok, [binary()]} | {:error, binary()}

Returns a list of uncommitted files

Uncommitted are considered the files that are staged but not committed yet.

If the repository has no commits yet, all staged files are considered uncommitted.

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.

untracked_files(opts \\ [])

@spec untracked_files(opts :: keyword()) :: {:ok, [binary()]} | {:error, binary()}

Get list of untracked files

Options

  • :cd (binary/0) - The git repo path, defaults to the current working directory.