]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
repo_utils: Add current_branch()
authorZack Cerza <zack@redhat.com>
Thu, 21 Apr 2022 16:47:38 +0000 (10:47 -0600)
committerZack Cerza <zack@cerza.org>
Wed, 27 Apr 2022 20:51:10 +0000 (14:51 -0600)
Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/repo_utils.py
teuthology/test/test_repo_utils.py

index 4db15383e98f922be4c9aff5200a1318389e4b9a..432c3ea96f34972ebd835a823d2066ceb3df2016 100644 (file)
@@ -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
index b29567ba2d7dce655f53e20e37ce942fe630caa8..068a797a1688827e5d7e9507ca7f1d8291cc12f1 100644 (file)
@@ -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