Skip to content

Commit

Permalink
Parse gitattributes language override file names as globs instead of …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
Nixinova committed Mar 1, 2024
1 parent 178e382 commit ecf5b6f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
// Setup main variables
const fileAssociations: Record<T.FilePath, T.LanguageResult[]> = {};
const extensions: Record<T.FilePath, string> = {};
const overrides: Record<T.FilePath, T.LanguageResult> = {};
const globOverrides: Record<T.FilePath, T.LanguageResult> = {};
const results: T.Results = {
files: { count: 0, bytes: 0, results: {}, alternatives: {} },
languages: { count: 0, bytes: 0, results: {} },
Expand All @@ -60,6 +60,8 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
const unRelPath = (file: string) => normPath(paths.resolve(commonRoot, file));
const localPath = (file: string) => localRoot(unRelPath(file));

const fileMatchesGlobs = (file: string, ...globs: string[]) => ignore().add(globs).ignores(relPath(file));

// Prepare list of ignored files
const ignored = ignore();
ignored.add('.git/');
Expand Down Expand Up @@ -172,7 +174,7 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
forcedLang = overrideLang[0];
}
}
overrides[unRelPath(path)] = forcedLang;
globOverrides[path] = forcedLang;
}

//*PARSE LANGUAGES*//
Expand All @@ -194,12 +196,18 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
// List all languages that could be associated with a given file
const definiteness: Record<T.FilePath, true | undefined> = {};
const fromShebang: Record<T.FilePath, true | undefined> = {};
fileLoop:
for (const file of files) {
// Check manual override
if (file in overrides) {
addResult(file, overrides[file]);
for (const globMatch in globOverrides) {
// TODO apply this logic (gitattributes lines being globs not relative file paths) to rest of code
if (!fileMatchesGlobs(file, globMatch)) continue;

// If the given file matches the glob, apply the override to the file
const forcedLang = globOverrides[globMatch];
addResult(file, forcedLang);
definiteness[file] = true;
continue;
continue fileLoop; // no need to check other heuristics, the classified language has been found
}

// Check first line for readability
Expand Down

0 comments on commit ecf5b6f

Please sign in to comment.