def githubpath(user, repo, path, name):
    """Build a valid URL to file on Github.

    Note: This is sensitive to changes in how Github URLs work.

    Arguments:
    user -- the Github user ID/name
    repo -- the Github user's repository name
    path -- specific path to file within the repo
    name -- specific file name or gist id

    Returns URL to Github file or gist.
    """
    if user != '' and repo != '':
        retval = 'https://github.com/{0}/{1}/blob/master/'.format(user, repo)
    else:
        retval = 'https://gist.github.com/'
    if path:
        retval += path
        if name not in path:
            retval += '/' + name
    else:
        retval += name
    return retval
