.. versionadded:: 2015.8.0

    Recursively removes the worktree located at ``cwd``, returning ``True`` if
    successful. This function will attempt to determine if ``cwd`` is actually
    a worktree by invoking :py:func:`git.is_worktree
    <salt.modules.git.is_worktree>`. If the path does not correspond to a
    worktree, then an error will be raised and no action will be taken.

    .. warning::

        There is no undoing this action. Be **VERY** careful before running
        this function.

    cwd
        Path to the worktree to be removed

    user
        Used for path expansion when ``cwd`` is not an absolute path. By
        default, when ``cwd`` is not absolute, the path will be assumed to be
        relative to the home directory of the user under which the minion is
        running. Setting this option will change the home directory from which
        path expansion is performed.

    output_encoding
        Use this option to specify which encoding to use to decode the output
        from any git commands which are run. This should not be needed in most
        cases.

        .. note::
            This should only be needed if the files in the repository were
            created with filenames using an encoding other than UTF-8 to handle
            Unicode characters.

        .. versionadded:: 2018.3.1

    CLI Examples:

    .. code-block:: bash

        salt myminion git.worktree_rm /path/to/worktree

def worktree_rm(cwd, user=None, output_encoding=None):
    '''
    .. versionadded:: 2015.8.0

    Recursively removes the worktree located at ``cwd``, returning ``True`` if
    successful. This function will attempt to determine if ``cwd`` is actually
    a worktree by invoking :py:func:`git.is_worktree
    <salt.modules.git.is_worktree>`. If the path does not correspond to a
    worktree, then an error will be raised and no action will be taken.

    .. warning::

        There is no undoing this action. Be **VERY** careful before running
        this function.

    cwd
        Path to the worktree to be removed

    user
        Used for path expansion when ``cwd`` is not an absolute path. By
        default, when ``cwd`` is not absolute, the path will be assumed to be
        relative to the home directory of the user under which the minion is
        running. Setting this option will change the home directory from which
        path expansion is performed.

    output_encoding
        Use this option to specify which encoding to use to decode the output
        from any git commands which are run. This should not be needed in most
        cases.

        .. note::
            This should only be needed if the files in the repository were
            created with filenames using an encoding other than UTF-8 to handle
            Unicode characters.

        .. versionadded:: 2018.3.1

    CLI Examples:

    .. code-block:: bash

        salt myminion git.worktree_rm /path/to/worktree
    '''
    _check_worktree_support()
    cwd = _expand_path(cwd, user)
    if not os.path.exists(cwd):
        raise CommandExecutionError(cwd + ' does not exist')
    elif not is_worktree(cwd, output_encoding=output_encoding):
        raise CommandExecutionError(cwd + ' is not a git worktree')
    try:
        salt.utils.files.rm_rf(cwd)
    except Exception as exc:
        raise CommandExecutionError(
            'Unable to remove {0}: {1}'.format(cwd, exc)
        )
    return True