summaryrefslogtreecommitdiff
path: root/source/github-helpers/index.ts
diff options
context:
space:
mode:
authorGravatar Federico Brigante <me@fregante.com> 2023-05-10 04:32:07 +0800
committerGravatar GitHub <noreply@github.com> 2023-05-10 04:32:07 +0800
commit747b87a6358c729e54148021d44c94e828603e01 (patch)
treedbe2d54ce393ce66bc9ada86655647ed2a586e00 /source/github-helpers/index.ts
parent12532fc4c149f5922fffba89ee2624bf3982e975 (diff)
downloadrefined-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/index.ts')
-rw-r--r--source/github-helpers/index.ts50
1 files changed, 5 insertions, 45 deletions
diff --git a/source/github-helpers/index.ts b/source/github-helpers/index.ts
index c38acc83..883b10df 100644
--- a/source/github-helpers/index.ts
+++ b/source/github-helpers/index.ts
@@ -20,13 +20,12 @@ export function getConversationNumber(): number | undefined {
return undefined;
}
-export function getCurrentBranchFromFeed(): string | void {
- // Not `isRepoCommitList` because this works exclusively on the default branch
- if (getRepo()!.path !== 'commits') {
- return;
+export function getCurrentBranchFromFeed(): string {
+ const feedLink = select('link[type="application/atom+xml"]');
+ if (!feedLink) {
+ throw new Error('getCurrentBranchFromFeed() is only available on commit lists');
}
- const feedLink = select('link[type="application/atom+xml"]')!;
return new URL(feedLink.href)
.pathname
.split('/')
@@ -35,45 +34,6 @@ export function getCurrentBranchFromFeed(): string | void {
.replace(/\.atom$/, '');
}
-const typesWithCommittish = new Set(['tree', 'blob', 'blame', 'edit', 'commit', 'commits', 'compare']);
-const titleWithCommittish = / at (?<branch>[.\w-/]+)( · [\w-]+\/[\w-]+)?$/i;
-export const getCurrentCommittish = (pathname = location.pathname, title = document.title): string | undefined => {
- if (!pathname.startsWith('/')) {
- throw new TypeError(`Expected pathname starting with /, got "${pathname}"`);
- }
-
- const [type, unslashedCommittish] = pathname.split('/').slice(3);
- if (!type || !typesWithCommittish.has(type)) {
- // Root; or piece of information not applicable to the page
- return;
- }
-
- // Handle slashed branches in commits pages
- if (type === 'commits') {
- if (!unslashedCommittish) {
- return getCurrentBranchFromFeed()!;
- }
-
- const branchAndFilepath = pathname.split('/').slice(4).join('/');
-
- // List of all commits of current branch (no filename)
- if (title.startsWith('Commits · ')) {
- return branchAndFilepath;
- }
-
- // List of commits touching a particular file ("History")
- const filepath = /^History for ([^ ]+) - /.exec(title)![1];
- return branchAndFilepath.slice(0, branchAndFilepath.lastIndexOf('/' + filepath));
- }
-
- const parsedTitle = titleWithCommittish.exec(title);
- if (parsedTitle) {
- return parsedTitle.groups!.branch;
- }
-
- return unslashedCommittish;
-};
-
export const isMac = navigator.userAgent.includes('Macintosh');
type Not<Yes, Not> = Yes extends Not ? never : Yes;
@@ -139,7 +99,7 @@ const cachePerPage = {
/** Is tag or commit, with elementReady */
export const isPermalink = mem(async () => {
- // No need for getCurrentCommittish(), it's a simple and exact check
+ // No need for getCurrentGitRef(), it's a simple and exact check
if (/^[\da-f]{40}$/.test(location.pathname.split('/')[4])) {
// It's a commit
return true;