| 37 | |
| 38 | |
| 39 | class 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 |
no outgoing calls
no test coverage detected
searching dependent graphs…