def _verify_ref(url, ref, enforce_sha): # Do a checkout in a temporary directory msg = 'Cloning "{0}" to verify ref "{1}"'.format(url, ref) tty.info(msg, stream=sys.stderr) git = executable.which('git', required=True) with fs.temporary_dir(): git('clone', '-q', url, '.') sha = git('rev-parse', '-q', ref + '^{commit}', output=str, error=os.devnull, fail_on_error=False) if git.returncode: msg = '"{0}" is not a valid reference for "{1}"' raise RuntimeError(msg.format(sha, url)) if enforce_sha: ref = sha.strip() return ref
def test_temporary_dir_context_manager(): previous_dir = os.path.realpath(os.getcwd()) with fs.temporary_dir() as tmp_dir: assert previous_dir != os.path.realpath(os.getcwd()) assert os.path.realpath(str(tmp_dir)) == os.path.realpath(os.getcwd())