瀏覽代碼

Add a active_branch() function.

Jelmer Vernooij 5 年之前
父節點
當前提交
5db0caac6e
共有 3 個文件被更改,包括 32 次插入4 次删除
  1. 3 0
      NEWS
  2. 23 4
      dulwich/porcelain.py
  3. 6 0
      dulwich/tests/test_porcelain.py

+ 3 - 0
NEWS

@@ -8,6 +8,9 @@
  * Switch over to Google-style docstrings.
    (Jelmer Vernooij)
 
+ * Add a ``dulwich.porcelain.active_branch`` function.
+   (Jelmer Vernooij)
+
 0.19.13	2019-08-19
 
  BUG FIXES

+ 23 - 4
dulwich/porcelain.py

@@ -123,6 +123,7 @@ from dulwich.protocol import (
     )
 from dulwich.refs import (
     ANNOTATED_TAG_SUFFIX,
+    LOCAL_BRANCH_PREFIX,
     strip_peeled_refs,
 )
 from dulwich.repo import (BaseRepo, Repo)
@@ -1113,7 +1114,7 @@ def receive_pack(path=".", inf=None, outf=None):
 def _make_branch_ref(name):
     if getattr(name, 'encode', None):
         name = name.encode(DEFAULT_ENCODING)
-    return b"refs/heads/" + name
+    return LOCAL_BRANCH_PREFIX + name
 
 
 def _make_tag_ref(name):
@@ -1164,7 +1165,25 @@ def branch_list(repo):
     :param repo: Path to the repository
     """
     with open_repo_closing(repo) as r:
-        return r.refs.keys(base=b"refs/heads/")
+        return r.refs.keys(base=LOCAL_BRANCH_PREFIX)
+
+
+def active_branch(repo):
+    """Return the active branch in the repository, if any.
+
+    Args:
+      repo: Repository to open
+    Returns:
+      branch name
+    Raises:
+      KeyError: if the repository does not have a working tree
+      IndexError: if HEAD is floating
+    """
+    with open_repo_closing(repo) as r:
+        active_ref = r.refs.follow(b'HEAD')[0][1]
+        if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
+            raise ValueError(active_ref)
+        return active_ref[len(LOCAL_BRANCH_PREFIX):]
 
 
 def fetch(repo, remote_location, remote_name=b'origin', outstream=sys.stdout,
@@ -1192,8 +1211,8 @@ def fetch(repo, remote_location, remote_name=b'origin', outstream=sys.stdout,
                                     depth=depth)
         stripped_refs = strip_peeled_refs(fetch_result.refs)
         branches = {
-            n[len(b'refs/heads/'):]: v for (n, v) in stripped_refs.items()
-            if n.startswith(b'refs/heads/')}
+            n[len(LOCAL_BRANCH_PREFIX):]: v for (n, v) in stripped_refs.items()
+            if n.startswith(LOCAL_BRANCH_PREFIX)}
         r.refs.import_refs(
             b'refs/remotes/' + remote_name, branches, message=message,
             prune=prune)

+ 6 - 0
dulwich/tests/test_porcelain.py

@@ -1795,3 +1795,9 @@ class WriteTreeTests(PorcelainTestCase):
         self.assertEqual(
             b'd2092c8a9f311f0311083bf8d177f2ca0ab5b241',
             porcelain.write_tree(self.repo))
+
+
+class ActiveBranchTests(PorcelainTestCase):
+
+    def test_simple(self):
+        self.assertEqual(b'master', porcelain.active_branch(self.repo))