<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def update(cwd, rev, force=False, user=None):
    '''
    Update to a given revision

    cwd
        The path to the Mercurial repository

    rev
        The revision to update to

    force : False
        Force an update

    user : None
        Run hg as a user other than what the minion runs as

    CLI Example:

    .. code-block:: bash

        salt devserver1 hg.update /path/to/repo somebranch
    '''
    cmd = ['hg', 'update', '{0}'.format(rev)]
    if force:
        cmd.append('-C')

    ret = __salt__['cmd.run_all'](cmd, cwd=cwd, runas=user, python_shell=False)
    if ret['retcode'] != 0:
        raise CommandExecutionError(
            'Hg command failed: {0}'.format(ret.get('stderr', ret['stdout']))
        )

    return ret['stdout']