<commit_before>import select from 'select-dom';
import cache from 'webext-storage-cache';
import * as api from './api';
import {getOwnerAndRepo} from './utils';

// 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 = /([^ ]+)\.$/;

function parseBranchFromDom(): string | undefined {
	if (select.exists('.repohead h1 .octicon-repo-forked')) {
		return; // It's a fork, no "default branch" info available #1132
	}

	// We can find the name in the infobar, available in folder views
	const branchInfo = select('.branch-infobar');
	if (!branchInfo) {
		return;
	}

	// Parse the infobar
	const matches = branchInfoRegex.exec(branchInfo.textContent!.trim());
	return matches ? matches[0] : undefined;
}

async function fetchFromApi(user: string, repo: string): Promise<any> {
	const response = await api.v3(`repos/${user}/${repo}`);
	if (response.default_branch) {
		return response.default_branch;
	}
}

export default async function (): Promise<string> {
	const {ownerName, repoName} = getOwnerAndRepo();
	const cached = await cache.get<string>(`default-branch:${ownerName}/${repoName}`);
	if (cached) {
		return cached;
	}

	const branch = parseBranchFromDom() || await fetchFromApi(ownerName, repoName);
	await cache.set(`default-branch:${ownerName}/${repoName}`, branch, 1);
	return branch;
}
<commit_msg>Fix `branch-buttons` default branch name (#2241)<commit_after>import select from 'select-dom';
import cache from 'webext-storage-cache';
import * as api from './api';
import {getOwnerAndRepo} from './utils';

// 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 = /([^ ]+)\.$/;

function parseBranchFromDom(): string | undefined {
	if (select.exists('.repohead h1 .octicon-repo-forked')) {
		return; // It's a fork, no "default branch" info available #1132
	}

	// We can find the name in the infobar, available in folder views
	const branchInfo = select('.branch-infobar');
	if (!branchInfo) {
		return;
	}

	// Parse the infobar
	const matches = branchInfoRegex.exec(branchInfo.textContent!.trim());
	return matches ? matches[1] : undefined;
}

async function fetchFromApi(user: string, repo: string): Promise<any> {
	const response = await api.v3(`repos/${user}/${repo}`);
	if (response.default_branch) {
		return response.default_branch;
	}
}

export default async function (): Promise<string> {
	const {ownerName, repoName} = getOwnerAndRepo();
	const cached = await cache.get<string>(`default-branch:${ownerName}/${repoName}`);
	if (cached) {
		return cached;
	}

	const branch = parseBranchFromDom() || await fetchFromApi(ownerName, repoName);
	await cache.set(`default-branch:${ownerName}/${repoName}`, branch, 1);
	return branch;
}

{'fraction_non_alphanumeric': 0.14448421737715586, 'fraction_numerical': 0.009762447120078099, 'mean_word_length': 4.599271402550091, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5543669', 'n_tokens_mistral': 974, 'n_tokens_neox': 939, 'n_words': 301}