diff options
author | 2023-05-10 04:32:07 +0800 | |
---|---|---|
committer | 2023-05-10 04:32:07 +0800 | |
commit | 747b87a6358c729e54148021d44c94e828603e01 (patch) | |
tree | dbe2d54ce393ce66bc9ada86655647ed2a586e00 /source/github-helpers/get-default-branch.ts | |
parent | 12532fc4c149f5922fffba89ee2624bf3982e975 (diff) | |
download | refined-github-747b87a6358c729e54148021d44c94e828603e01.tar.gz refined-github-747b87a6358c729e54148021d44c94e828603e01.tar.zst refined-github-747b87a6358c729e54148021d44c94e828603e01.zip |
Reliability fixes for `default-branch-button` (#6629)
Diffstat (limited to 'source/github-helpers/get-default-branch.ts')
-rw-r--r-- | source/github-helpers/get-default-branch.ts | 42 |
1 files changed, 11 insertions, 31 deletions
diff --git a/source/github-helpers/get-default-branch.ts b/source/github-helpers/get-default-branch.ts index 34f5bd11..efbbdd4c 100644 --- a/source/github-helpers/get-default-branch.ts +++ b/source/github-helpers/get-default-branch.ts @@ -1,44 +1,24 @@ import cache from 'webext-storage-cache'; -import select from 'select-dom'; import elementReady from 'element-ready'; import * as pageDetect from 'github-url-detection'; import * as api from './api.js'; -import {getRepo, getCurrentBranchFromFeed} from './index.js'; +import {getRepo} from './index.js'; import {branchSelector} from './selectors.js'; -// This regex should match all of these combinations: -// "This branch is even with master." -// "This branch is 1 commit behind master." -// "This branch is 1 commit ahead of master." -// "This branch is 1 commit ahead, 27 commits behind master." -const branchInfoRegex = /([^ ]+)\.$/; +const isCurrentRepo = ({nameWithOwner}: pageDetect.RepositoryInfo): boolean => Boolean(getRepo()?.nameWithOwner === nameWithOwner); // DO NOT use optional arguments/defaults in "cached functions" because they can't be memoized effectively // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1864 -const _getDefaultBranch = cache.function('default-branch', async function (repository: pageDetect.RepositoryInfo): Promise<string> { - if (arguments.length === 0 || JSON.stringify(repository) === JSON.stringify(getRepo())) { - if (pageDetect.isRepoHome()) { - const branchPicker = await elementReady(branchSelector); - if (branchPicker) { - return branchPicker.title === 'Switch branches or tags' - ? branchPicker.textContent!.trim() - : branchPicker.title; - } - } - - const defaultBranch = getCurrentBranchFromFeed(); - if (defaultBranch) { - return defaultBranch; - } - - if (!pageDetect.isForkedRepo()) { - // We can find the name in the infobar, available in folder views - const branchInfo = select('.branch-infobar')?.textContent!.trim(); - const defaultBranch = branchInfoRegex.exec(branchInfo!)?.[1]; - if (defaultBranch) { - return defaultBranch; - } +const _getDefaultBranch = cache.function('default-branch', async (repository: pageDetect.RepositoryInfo): Promise<string> => { + // TODO: extract from `ref-selector` if available https://github.com/refined-github/refined-github/issues/6557 + if (isCurrentRepo(repository) && ['', 'commits'].includes(repository.path)) { + // We're on the default branch, so we can extract it from the current page. This usually happens on the pages: + // @example /user/repo + // @example /user/repo/commits (without further path) + const branchPicker = await elementReady(branchSelector); + if (branchPicker) { + return branchPicker.textContent!.trim(); } } |