@contextlib.contextmanager
def _git_pkg_version(env, brew_cmd, pkg, version):
    git_cmd = _git_cmd_for_pkg_version(env, brew_cmd, pkg, version)
    git_fname = git_cmd.split()[-1]
    brew_prefix = env.safe_run_output("{brew_cmd} --prefix".format(**locals()))
    if git_fname.startswith("{brew_prefix}/Library/Taps/".format(**locals())):
        brew_prefix = os.path.dirname(git_fname)
    try:
        with cd(brew_prefix):
            if version != "HEAD":
                env.safe_run(git_cmd)
        yield
    finally:
        # reset Git back to latest
        with cd(brew_prefix):
            if version != "HEAD":
                cmd_parts = git_cmd.split()
                env.safe_run("%s reset HEAD %s" % (cmd_parts[0], cmd_parts[-1]))
                cmd_parts[2] = "--"
                env.safe_run(" ".join(cmd_parts))
Convert homebrew Git to previous revision to install a specific package version.
dataset/ETHPy150Open chapmanb/cloudbiolinux/cloudbio/package/brew.py/_git_pkg_version