# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.GitRef do
  @moduledoc """
  A git ref (tag, branch, or commit).
  """

  use Dagger.Core.Base, kind: :object, name: "GitRef"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  The resolved commit id at this ref.
  """
  @spec commit(t()) :: {:ok, String.t()} | {:error, term()}
  def commit(%__MODULE__{} = git_ref) do
    query_builder =
      git_ref.query_builder |> QB.select("commit")

    Client.execute(git_ref.client, query_builder)
  end

  @doc """
  Find the best common ancestor between this ref and another ref.
  """
  @spec common_ancestor(t(), Dagger.GitRef.t()) :: Dagger.GitRef.t()
  def common_ancestor(%__MODULE__{} = git_ref, other) do
    query_builder =
      git_ref.query_builder
      |> QB.select("commonAncestor")
      |> QB.put_arg("other", Dagger.ID.id!(other))

    %Dagger.GitRef{
      query_builder: query_builder,
      client: git_ref.client
    }
  end

  @doc """
  A unique identifier for this GitRef.
  """
  @spec id(t()) :: {:ok, Dagger.GitRefID.t()} | {:error, term()}
  def id(%__MODULE__{} = git_ref) do
    query_builder =
      git_ref.query_builder |> QB.select("id")

    Client.execute(git_ref.client, query_builder)
  end

  @doc """
  The resolved ref name at this ref.
  """
  @spec ref(t()) :: {:ok, String.t()} | {:error, term()}
  def ref(%__MODULE__{} = git_ref) do
    query_builder =
      git_ref.query_builder |> QB.select("ref")

    Client.execute(git_ref.client, query_builder)
  end

  @doc """
  The filesystem tree at this ref.
  """
  @spec tree(t(), [{:discard_git_dir, boolean() | nil}, {:depth, integer() | nil}]) ::
          Dagger.Directory.t()
  def tree(%__MODULE__{} = git_ref, optional_args \\ []) do
    query_builder =
      git_ref.query_builder
      |> QB.select("tree")
      |> QB.maybe_put_arg("discardGitDir", optional_args[:discard_git_dir])
      |> QB.maybe_put_arg("depth", optional_args[:depth])

    %Dagger.Directory{
      query_builder: query_builder,
      client: git_ref.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.GitRef do
  def encode(git_ref, opts) do
    {:ok, id} = Dagger.GitRef.id(git_ref)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.GitRef do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_git_ref_from_id(Dagger.Global.dag(), id)}
  end
end
