From: Zack Cerza Date: Thu, 21 Apr 2022 16:47:38 +0000 (-0600) Subject: repo_utils: Add current_branch() X-Git-Tag: 1.2.0~167^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=11a27e6474ed16b2c720d9079ae3cf4dc53f8af9;p=teuthology.git repo_utils: Add current_branch() Signed-off-by: Zack Cerza --- diff --git a/teuthology/repo_utils.py b/teuthology/repo_utils.py index 4db15383e..432c3ea96 100644 --- a/teuthology/repo_utils.py +++ b/teuthology/repo_utils.py @@ -70,6 +70,27 @@ def ls_remote(url, ref): return sha1 +def current_branch(path: str) -> str: + """ + Return the current branch for a given on-disk repository. + + :returns: the current branch, or an empty string if none is found. + """ + # git branch --show-current was added in 2.22.0, and we can't assume + # our version is new enough. + cmd = "git rev-parse --abbrev-ref HEAD" + result = subprocess.Popen( + cmd, + shell=True, + cwd=path, + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + ).communicate()[0].strip().decode() + if result == "HEAD": + return "" + return result + + def enforce_repo_state(repo_url, dest_path, branch, commit=None, remove_on_error=True): """ Use git to either clone or update a given repo, forcing it to switch to the diff --git a/teuthology/test/test_repo_utils.py b/teuthology/test/test_repo_utils.py index b29567ba2..068a797a1 100644 --- a/teuthology/test/test_repo_utils.py +++ b/teuthology/test/test_repo_utils.py @@ -223,3 +223,9 @@ class TestRepoUtils(object): @mark.parametrize("input_, expected", URLS_AND_DIRNAMES) def test_url_to_dirname(self, input_, expected): assert repo_utils.url_to_dirname(input_) == expected + + def test_current_branch(self): + #repo_utils.enforce_repo_state(self.repo_url, self.dest_path, + # 'master', self.commit) + repo_utils.clone_repo(self.repo_url, self.dest_path, 'master', self.commit) + assert repo_utils.current_branch(self.dest_path) == "master" \ No newline at end of file