# Buggy code
def mod_run_check(cmd_kwargs, onlyif, unless):
    """
    Execute the onlyif and unless logic. Return a result dict if:

    * onlyif failed (onlyif != 0)
    * unless succeeded (unless == 0)

    Otherwise, returns ``True``
    """
    cmd_kwargs = copy.deepcopy(cmd_kwargs)
    cmd_kwargs["python_shell"] = True
    if onlyif:
        if __salt__["cmd.retcode"](onlyif, **cmd_kwargs) != 0:
            return {
                "comment": "onlyif condition is false",
                "skip_watch": True,
                "result": True,
            }

    if unless:
        if __salt__["cmd.retcode"](unless, **cmd_kwargs) == 0:
            return {
                "comment": "unless condition is true",
                "skip_watch": True,
                "result": True,
            }

    # No reason to stop, return True
    return True


# Traceback (TypeError)
----------
ID: salt-repo-git
Function: git.latest
Name: ssh://git@bitbucket-1.efinity.local:7999/sal/salt-apache-vhosts-test.git
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/state.py", line 1913, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/site-packages/salt/loader.py", line 1898, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/states/git.py", line 707, in latest
run_check_cmd_kwargs, onlyif, unless
File "/usr/lib/python2.7/site-packages/salt/states/git.py", line 3370, in mod_run_check
if __salt__['cmd.retcode'](unless, **cmd_kwargs) == 0:
File "/usr/lib/python2.7/site-packages/salt/modules/cmdmod.py", line 2088, in retcode
**kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/cmdmod.py", line 603, in _run
proc = salt.utils.timed_subprocess.TimedProc(cmd, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/utils/timed_subprocess.py", line 44, in __init__
args = salt.utils.stringutils.to_bytes(args)
File "/usr/lib/python2.7/site-packages/salt/utils/stringutils.py", line 63, in to_bytes
return to_str(s, encoding, errors)
File "/usr/lib/python2.7/site-packages/salt/utils/stringutils.py", line 118, in to_str
raise TypeError('expected str, bytearray, or unicode')
TypeError: expected str, bytearray, or unicode
Started: 05:36:17.256304
Duration: 54.61 ms
Changes:

# Fixed code
def mod_run_check(cmd_kwargs, onlyif, unless):
    """
    Execute the onlyif and unless logic. Return a result dict if:

    * onlyif failed (onlyif != 0)
    * unless succeeded (unless == 0)

    Otherwise, returns ``True``
    """
    cmd_kwargs = copy.deepcopy(cmd_kwargs)
    cmd_kwargs.update(
        {
            "use_vt": False,
            "bg": False,
            "ignore_retcode": True,
            "python_shell": True,
        }
    )

    if onlyif is not None:
        if not isinstance(onlyif, list):
            onlyif = [onlyif]

        for command in onlyif:
            if not isinstance(command, six.string_types) and command:
                # Boolean or some other non-string which resolves to True
                continue
            try:
                if __salt__["cmd.retcode"](command, **cmd_kwargs) == 0:
                    # Command exited with a zero retcode
                    continue
            except Exception as exc:
                log.exception(
                    "The following onlyif command raised an error: %s", command
                )
                return {
                    "comment": "onlyif raised error ({0}), see log for "
                    "more details".format(exc),
                    "result": False,
                }

            return {
                "comment": "onlyif condition is false",
                "skip_watch": True,
                "result": True,
            }

    if unless is not None:
        if not isinstance(unless, list):
            unless = [unless]

        for command in unless:
            if not isinstance(command, six.string_types) and not command:
                # Boolean or some other non-string which resolves to False
                break
            try:
                if __salt__["cmd.retcode"](command, **cmd_kwargs) != 0:
                    # Command exited with a non-zero retcode
                    break
            except Exception as exc:
                log.exception(
                    "The following unless command raised an error: %s", command
                )
                return {
                    "comment": "unless raised error ({0}), see log for "
                    "more details".format(exc),
                    "result": False,
                }
        else:
            return {
                "comment": "unless condition is true",
                "skip_watch": True,
                "result": True,
            }

    return True