MCPcopy Create free account
hub / github.com/libgit2/pygit2 / Branches

Class Branches

pygit2/branches.py:39–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37
38
39class Branches:
40 local: 'Branches'
41 remote: 'Branches'
42
43 def __init__(
44 self,
45 repository: BaseRepository,
46 flag: BranchType = BranchType.ALL,
47 commit: Commit | Oid | str | None = None,
48 ) -> None:
49 self._repository = repository
50 self._flag = flag
51 if commit is not None:
52 if isinstance(commit, Commit):
53 commit = commit.id
54 elif not isinstance(commit, Oid):
55 commit = self._repository.expand_id(commit)
56 self._commit = commit
57
58 if flag == BranchType.ALL:
59 self.local = Branches(repository, flag=BranchType.LOCAL, commit=commit)
60 self.remote = Branches(repository, flag=BranchType.REMOTE, commit=commit)
61
62 def __getitem__(self, name: str) -> Branch:
63 branch = None
64 if self._flag & BranchType.LOCAL:
65 branch = self._repository.lookup_branch(name, BranchType.LOCAL)
66
67 if branch is None and self._flag & BranchType.REMOTE:
68 branch = self._repository.lookup_branch(name, BranchType.REMOTE)
69
70 if branch is None or not self._valid(branch):
71 raise KeyError(f'Branch not found: {name}')
72
73 return branch
74
75 def get(self, key: str) -> Branch:
76 try:
77 return self[key]
78 except KeyError:
79 return None # type:ignore # next commit
80
81 def __iter__(self) -> Iterator[str]:
82 for branch_name in self._repository.listall_branches(self._flag):
83 if self._commit is None or self.get(branch_name) is not None:
84 yield branch_name
85
86 def create(self, name: str, commit: Commit, force: bool = False) -> Branch:
87 return self._repository.create_branch(name, commit, force)
88
89 def delete(self, name: str) -> None:
90 self[name].delete()
91
92 def _valid(self, branch: Branch) -> bool:
93 if branch.type == ReferenceType.SYMBOLIC:
94 branch_direct = branch.resolve()
95 else:
96 branch_direct = branch

Callers 3

__init__Method · 0.85
with_commitMethod · 0.85
_common_initMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…