# `TerminusDB.Merge`
[🔗](https://github.com/thanos/terminusdb-client-elixir/blob/v0.3.3/lib/terminus_db/merge.ex#L1)

Branch merge API for TerminusDB.

Wraps the `/api/rebase` endpoint to merge (rebase) a source branch into a target
branch. TerminusDB uses a rebase model: the source branch's commits are
replayed on top of the target branch, creating a linear history.

## Quick start

    config =
      TerminusDB.Config.new(endpoint: "http://localhost:6363")
      |> TerminusDB.Config.with_database("mydb")

    # Merge `feature` into `main`
    {:ok, result} = TerminusDB.Merge.merge(config,
      source_branch: "feature",
      target_branch: "main"
    )

# `merge_opt`

```elixir
@type merge_opt() ::
  {:source_branch, String.t()}
  | {:target_branch, String.t()}
  | {:organization, String.t()}
  | {:repo, String.t()}
  | {:author, String.t()}
  | {:message, String.t()}
```

# `merge`

```elixir
@spec merge(TerminusDB.Config.t(), [merge_opt()]) ::
  {:ok, map()} | {:error, TerminusDB.Error.t()}
```

Merges (rebases) the `source_branch` into the `target_branch`.

TerminusDB replays the source branch's commits on top of the target branch.
If there are conflicts, the operation returns an error describing them.

## Options

- `:source_branch` (required) - the branch to merge from.
- `:target_branch` - the branch to merge into (default: `config.branch`).
- `:organization` - overrides `config.organization`.
- `:repo` - overrides `config.repo`.
- `:author` - commit author for the merge commit.
- `:message` - commit message for the merge commit.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> {:ok, result} = TerminusDB.Merge.merge(config,
    ...>   source_branch: "feature",
    ...>   target_branch: "main"
    ...> )
    iex> result["api:status"]
    "api:success"

# `merge!`

```elixir
@spec merge!(TerminusDB.Config.t(), [merge_opt()]) :: map()
```

Merges branches, or raises `TerminusDB.Error`.

## Examples

    iex> config = TerminusDB.Config.new(
    ...>   endpoint: "http://localhost:6363",
    ...>   adapter: fn req -> {req, Req.Response.new(status: 200, body: %{"api:status" => "api:success"})} end
    ...> ) |> TerminusDB.Config.with_database("mydb")
    iex> TerminusDB.Merge.merge!(config, source_branch: "feature", target_branch: "main")
    %{"api:status" => "api:success"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
