Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"jquery": "3.7.1",
"katex": "0.16.22",
"mermaid": "11.11.0",
"minimatch": "10.0.3",
"monaco-editor": "0.53.0",
"monaco-editor-webpack-plugin": "7.1.0",
"online-3d-viewer": "0.16.0",
Expand Down
26 changes: 23 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions web_src/js/features/repo-settings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {minimatch} from 'minimatch';
import {createMonaco} from './codeeditor.ts';
import {onInputDebounce, queryElems, toggleElem} from '../utils/dom.ts';
import {POST} from '../modules/fetch.ts';
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
import {fomanticQuery} from '../modules/fomantic/base.ts';
import {globMatch} from '../utils/glob.ts';

const {appSubUrl, csrfToken} = window.config;

Expand Down Expand Up @@ -108,7 +108,7 @@ function initRepoSettingsBranches() {
let matched = false;
const statusCheck = el.getAttribute('data-status-check');
for (const pattern of validPatterns) {
if (minimatch(statusCheck, pattern, {noext: true})) { // https://github.com/go-gitea/gitea/issues/33121 disable extended glob syntax
if (globMatch(statusCheck, pattern, '/')) {
matched = true;
break;
}
Expand Down
129 changes: 129 additions & 0 deletions web_src/js/utils/glob.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {readFile} from 'node:fs/promises';
import * as path from 'node:path';
import {globCompile} from './glob.ts';

async function loadGlobTestData(): Promise<{caseNames: string[], caseDataMap: Record<string, string>}> {
const fileContent = await readFile(path.join(import.meta.dirname, 'glob.test.txt'), 'utf8');
const fileLines = fileContent.split('\n');
const caseDataMap: Record<string, string> = {};
const caseNameMap: Record<string, boolean> = {};
for (let line of fileLines) {
line = line.trim();
if (!line || line.startsWith('#')) continue;
const parts = line.split('=', 2);
if (parts.length !== 2) throw new Error(`Invalid test case line: ${line}`);

const key = parts[0].trim();
let value = parts[1].trim();
value = value.substring(1, value.length - 1); // remove quotes
value = value.replace(/\\\\/g, '\\').replaceAll(/\\\//g, '/');
caseDataMap[key] = value;
if (key.startsWith('pattern_')) caseNameMap[key.substring('pattern_'.length)] = true;
}
return {caseNames: Object.keys(caseNameMap), caseDataMap};
}

function loadGlobGolangCases() {
// https://github.com/gobwas/glob/blob/master/glob_test.go
function glob(matched: boolean, pattern: string, input: string, separators: string = '') {
return {matched, pattern, input, separators};
}
return [
glob(true, '* ?at * eyes', 'my cat has very bright eyes'),

glob(true, '', ''),
glob(false, '', 'b'),

glob(true, '*ä', 'åä'),
glob(true, 'abc', 'abc'),
glob(true, 'a*c', 'abc'),
glob(true, 'a*c', 'a12345c'),
glob(true, 'a?c', 'a1c'),
glob(true, 'a.b', 'a.b', '.'),
glob(true, 'a.*', 'a.b', '.'),
glob(true, 'a.**', 'a.b.c', '.'),
glob(true, 'a.?.c', 'a.b.c', '.'),
glob(true, 'a.?.?', 'a.b.c', '.'),
glob(true, '?at', 'cat'),
glob(true, '?at', 'fat'),
glob(true, '*', 'abc'),
glob(true, `\\*`, '*'),
glob(true, '**', 'a.b.c', '.'),

glob(false, '?at', 'at'),
glob(false, '?at', 'fat', 'f'),
glob(false, 'a.*', 'a.b.c', '.'),
glob(false, 'a.?.c', 'a.bb.c', '.'),
glob(false, '*', 'a.b.c', '.'),

glob(true, '*test', 'this is a test'),
glob(true, 'this*', 'this is a test'),
glob(true, '*is *', 'this is a test'),
glob(true, '*is*a*', 'this is a test'),
glob(true, '**test**', 'this is a test'),
glob(true, '**is**a***test*', 'this is a test'),

glob(false, '*is', 'this is a test'),
glob(false, '*no*', 'this is a test'),
glob(true, '[!a]*', 'this is a test3'),

glob(true, '*abc', 'abcabc'),
glob(true, '**abc', 'abcabc'),
glob(true, '???', 'abc'),
glob(true, '?*?', 'abc'),
glob(true, '?*?', 'ac'),
glob(false, 'sta', 'stagnation'),
glob(true, 'sta*', 'stagnation'),
glob(false, 'sta?', 'stagnation'),
glob(false, 'sta?n', 'stagnation'),

glob(true, '{abc,def}ghi', 'defghi'),
glob(true, '{abc,abcd}a', 'abcda'),
glob(true, '{a,ab}{bc,f}', 'abc'),
glob(true, '{*,**}{a,b}', 'ab'),
glob(false, '{*,**}{a,b}', 'ac'),

glob(true, '/{rate,[a-z][a-z][a-z]}*', '/rate'),
glob(true, '/{rate,[0-9][0-9][0-9]}*', '/rate'),
glob(true, '/{rate,[a-z][a-z][a-z]}*', '/usd'),

glob(true, '{*.google.*,*.yandex.*}', 'www.google.com', '.'),
glob(true, '{*.google.*,*.yandex.*}', 'www.yandex.com', '.'),
glob(false, '{*.google.*,*.yandex.*}', 'yandex.com', '.'),
glob(false, '{*.google.*,*.yandex.*}', 'google.com', '.'),

glob(true, '{*.google.*,yandex.*}', 'www.google.com', '.'),
glob(true, '{*.google.*,yandex.*}', 'yandex.com', '.'),
glob(false, '{*.google.*,yandex.*}', 'www.yandex.com', '.'),
glob(false, '{*.google.*,yandex.*}', 'google.com', '.'),

glob(true, '*//{,*.}example.com', 'https://www.example.com'),
glob(true, '*//{,*.}example.com', 'http://example.com'),
glob(false, '*//{,*.}example.com', 'http://example.com.net'),
];
}

test('GlobCompiler', async () => {
const {caseNames, caseDataMap} = await loadGlobTestData();
expect(caseNames.length).toBe(10); // should have 10 test cases
for (const caseName of caseNames) {
const pattern = caseDataMap[`pattern_${caseName}`];
const regexp = caseDataMap[`regexp_${caseName}`];
expect(globCompile(pattern).regexpPattern).toBe(regexp);
}

const golangCases = loadGlobGolangCases();
expect(golangCases.length).toBe(60);
for (const c of golangCases) {
const compiled = globCompile(c.pattern, c.separators);
const msg = `pattern: ${c.pattern}, input: ${c.input}, separators: ${c.separators || '(none)'}, compiled: ${compiled.regexpPattern}`;
// eslint-disable-next-line @vitest/valid-expect -- Unlike Jest, Vitest supports a message as the second argument
expect(compiled.regexp.test(c.input), msg).toBe(c.matched);
}

// then our cases
expect(globCompile('*/**/x').regexpPattern).toBe('^.*/.*/x$');
expect(globCompile('*/**/x', '/').regexpPattern).toBe('^[^/]*/.*/x$');
expect(globCompile('[a-b][^-\\]]', '/').regexpPattern).toBe('^[a-b][^-\\]]$');
expect(globCompile('.+^$()|', '/').regexpPattern).toBe('^\\.\\+\\^\\$\\(\\)\\|$');
});
44 changes: 44 additions & 0 deletions web_src/js/utils/glob.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# test cases are from https://github.com/gobwas/glob/blob/master/glob_test.go

pattern_all = "[a-z][!a-x]*cat*[h][!b]*eyes*"
regexp_all = `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$`
fixture_all_match = "my cat has very bright eyes"
fixture_all_mismatch = "my dog has very bright eyes"

pattern_plain = "google.com"
regexp_plain = `^google\.com$`
fixture_plain_match = "google.com"
fixture_plain_mismatch = "gobwas.com"

pattern_multiple = "https://*.google.*"
regexp_multiple = `^https:\/\/.*\.google\..*$`
fixture_multiple_match = "https://account.google.com"
fixture_multiple_mismatch = "https://google.com"

pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
regexp_alternatives = `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$`
fixture_alternatives_match = "http://yahoo.com"
fixture_alternatives_mismatch = "http://google.com"

pattern_alternatives_suffix = "{https://*gobwas.com,http://exclude.gobwas.com}"
regexp_alternatives_suffix = `^(https:\/\/.*gobwas\.com|http://exclude\.gobwas\.com)$`
fixture_alternatives_suffix_first_match = "https://safe.gobwas.com"
fixture_alternatives_suffix_first_mismatch = "http://safe.gobwas.com"
fixture_alternatives_suffix_second = "http://exclude.gobwas.com"

pattern_prefix = "abc*"
regexp_prefix = `^abc.*$`
pattern_suffix = "*def"
regexp_suffix = `^.*def$`
pattern_prefix_suffix = "ab*ef"
regexp_prefix_suffix = `^ab.*ef$`
fixture_prefix_suffix_match = "abcdef"
fixture_prefix_suffix_mismatch = "af"

pattern_alternatives_combine_lite = "{abc*def,abc?def,abc[zte]def}"
regexp_alternatives_combine_lite = `^(abc.*def|abc.def|abc[zte]def)$`
fixture_alternatives_combine_lite = "abczdef"

pattern_alternatives_combine_hard = "{abc*[a-c]def,abc?[d-g]def,abc[zte]?def}"
regexp_alternatives_combine_hard = `^(abc.*[a-c]def|abc.[d-g]def|abc[zte].def)$`
fixture_alternatives_combine_hard = "abczqdef"
Loading