Dulwich.io dulwich / 5db0caa
Add a active_branch() function. Jelmer Vernooij 8 days ago
3 changed file(s) with 32 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
55 still call check_wants manually. (Jelmer Vernooij)
66
77 * Switch over to Google-style docstrings.
8 (Jelmer Vernooij)
9
10 * Add a ``dulwich.porcelain.active_branch`` function.
811 (Jelmer Vernooij)
912
1013 0.19.13 2019-08-19
122122 )
123123 from dulwich.refs import (
124124 ANNOTATED_TAG_SUFFIX,
125 LOCAL_BRANCH_PREFIX,
125126 strip_peeled_refs,
126127 )
127128 from dulwich.repo import (BaseRepo, Repo)
11121113 def _make_branch_ref(name):
11131114 if getattr(name, 'encode', None):
11141115 name = name.encode(DEFAULT_ENCODING)
1115 return b"refs/heads/" + name
1116 return LOCAL_BRANCH_PREFIX + name
11161117
11171118
11181119 def _make_tag_ref(name):
11631164 :param repo: Path to the repository
11641165 """
11651166 with open_repo_closing(repo) as r:
1166 return r.refs.keys(base=b"refs/heads/")
1167 return r.refs.keys(base=LOCAL_BRANCH_PREFIX)
1168
1169
1170 def active_branch(repo):
1171 """Return the active branch in the repository, if any.
1172
1173 Args:
1174 repo: Repository to open
1175 Returns:
1176 branch name
1177 Raises:
1178 KeyError: if the repository does not have a working tree
1179 IndexError: if HEAD is floating
1180 """
1181 with open_repo_closing(repo) as r:
1182 active_ref = r.refs.follow(b'HEAD')[0][1]
1183 if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
1184 raise ValueError(active_ref)
1185 return active_ref[len(LOCAL_BRANCH_PREFIX):]
11671186
11681187
11691188 def fetch(repo, remote_location, remote_name=b'origin', outstream=sys.stdout,
11911210 depth=depth)
11921211 stripped_refs = strip_peeled_refs(fetch_result.refs)
11931212 branches = {
1194 n[len(b'refs/heads/'):]: v for (n, v) in stripped_refs.items()
1195 if n.startswith(b'refs/heads/')}
1213 n[len(LOCAL_BRANCH_PREFIX):]: v for (n, v) in stripped_refs.items()
1214 if n.startswith(LOCAL_BRANCH_PREFIX)}
11961215 r.refs.import_refs(
11971216 b'refs/remotes/' + remote_name, branches, message=message,
11981217 prune=prune)
17941794 self.assertEqual(
17951795 b'd2092c8a9f311f0311083bf8d177f2ca0ab5b241',
17961796 porcelain.write_tree(self.repo))
1797
1798
1799 class ActiveBranchTests(PorcelainTestCase):
1800
1801 def test_simple(self):
1802 self.assertEqual(b'master', porcelain.active_branch(self.repo))