Git.Commands.MergeBase (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git merge-base.

Finds the best common ancestor(s) between two or more commits for use in a three-way merge. Also supports checking ancestor relationships and finding fork points.

Summary

Functions

Builds the argument list for git merge-base.

Parses the output of git merge-base.

Types

t()

@type t() :: %Git.Commands.MergeBase{
  all: boolean(),
  commits: [String.t()],
  fork_point: boolean(),
  independent: boolean(),
  is_ancestor: boolean(),
  octopus: boolean()
}

Functions

args(command)

@spec args(t()) :: [String.t()]

Builds the argument list for git merge-base.

Examples

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["main", "feature"]})
["merge-base", "main", "feature"]

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["main", "feature"], is_ancestor: true})
["merge-base", "--is-ancestor", "main", "feature"]

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["main", "feature"], all: true})
["merge-base", "--all", "main", "feature"]

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["a", "b", "c"], octopus: true})
["merge-base", "--octopus", "a", "b", "c"]

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["a", "b", "c"], independent: true})
["merge-base", "--independent", "a", "b", "c"]

iex> Git.Commands.MergeBase.args(%Git.Commands.MergeBase{commits: ["main", "feature"], fork_point: true})
["merge-base", "--fork-point", "main", "feature"]

parse_output(stdout, exit_code)

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

Parses the output of git merge-base.

The output mode depends on the flags used:

  • Default: returns {:ok, String.t()} with the ancestor SHA
  • With is_ancestor: true: exit 0 = {:ok, true}, exit 1 = {:ok, false}
  • With all: true or independent: true: returns {:ok, [String.t()]}