# `Git.Commands.MergeTree`
[🔗](https://github.com/joshrotenberg/git_wrapper_ex/blob/main/lib/git/commands/merge_tree.ex#L1)

Implements the `Git.Command` behaviour for `git merge-tree --write-tree`.

Performs a three-way merge of two commits entirely in the object database
and returns the resulting tree SHA plus the list of conflicted paths, with no
checkout, index, or working tree involvement. This answers "do these two
branches merge cleanly, and what conflicts?" for CI mergeability checks and
server-side merges.

Requires git 2.38 or newer (the `--write-tree` mode). Always passes
`--name-only`, so conflicts are reported as plain paths.

# `t`

```elixir
@type t() :: %Git.Commands.MergeTree{branch1: String.t(), branch2: String.t()}
```

# `args`

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

Returns the argument list for `git merge-tree`.

## Examples

    iex> Git.Commands.MergeTree.args(%Git.Commands.MergeTree{branch1: "main", branch2: "feature"})
    ["merge-tree", "--write-tree", "--name-only", "main", "feature"]

# `parse_output`

```elixir
@spec parse_output(String.t(), non_neg_integer()) ::
  {:ok, Git.MergeTreeResult.t()} | {:error, {String.t(), non_neg_integer()}}
```

Parses the output of `git merge-tree --write-tree --name-only`.

Exit code 0 is a clean merge and exit code 1 is a merge with conflicts (a
signal, not a failure); both return `{:ok, %Git.MergeTreeResult{}}`. Any other
case is a real error and returns `{:error, {stdout, exit_code}}`.

git also exits 1 for a bad ref ("not something we can merge"), which is not a
conflict, so exit 1 is only treated as a conflict when the first output line
is a valid object id; otherwise it is surfaced as an error.

The first output line is always the merged tree SHA. On a conflict, the lines
between it and the first blank line are the conflicted paths.

---

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