()
| 3 | |
| 4 | /** Detects if the current view is on the default branch. To be used on file/folder/commit lists */ |
| 5 | export default async function isDefaultBranch(): Promise<boolean> { |
| 6 | const repo = getRepo(); |
| 7 | if (!repo) { |
| 8 | // Like /settings/repositories |
| 9 | return false; |
| 10 | } |
| 11 | |
| 12 | const [type, ...parts] = repo.path.split('/'); |
| 13 | if (parts.length === 0) { |
| 14 | // Exactly /user/repo, which is on the default branch |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | if (!['tree', 'blob', 'commits'].includes(type)) { |
| 19 | // Like /user/repo/pulls |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | // Don't use `getCurrentGitRef` because it requires too much DOM. This is good enough, it only fails when: |
| 24 | // defaultBranch === 'a/b' && currentBranch === 'a' |
| 25 | const path = parts.join('/'); |
| 26 | const defaultBranch = await getDefaultBranch(); |
| 27 | return path === defaultBranch || path.startsWith(`${defaultBranch}/`); |
| 28 | } |
no test coverage detected