-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite codebase to be able to un-remove vendor matches
Fixes files that are by default marked vendored being removed even when explicitly marked as not vendored in gitattributes. See #26.
- Loading branch information
Showing
4 changed files
with
151 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import path from 'path'; | ||
|
||
import * as T from '../types'; | ||
|
||
export type FlagAttributes = { | ||
'vendored': boolean | null, | ||
'generated': boolean | null, | ||
'documentation': boolean | null, | ||
'binary': boolean | null, | ||
'language': T.LanguageResult; | ||
}; | ||
|
||
export type ParsedGitattributes = Array<{ | ||
glob: string, | ||
attrs: FlagAttributes, | ||
}>; | ||
|
||
/** | ||
* Parses a gitattributes file. | ||
*/ | ||
export default function parseAttributes(content: string, folderRoot: string = '.'): ParsedGitattributes { | ||
const output: ParsedGitattributes = []; | ||
|
||
for (const line of content.split('\n')) { | ||
if (!line) continue; | ||
|
||
const parts = line.split(/\s+/g); | ||
const fileGlob = parts[0]; | ||
const relFileGlob = path.join(folderRoot, fileGlob).replace(/\\/g, '/'); | ||
const attrParts = parts.slice(1); | ||
const isTrue = (str: string) => !str.startsWith('-') && !str.endsWith('=false'); | ||
const isFalse = (str: string) => str.startsWith('-') || str.endsWith('=false'); | ||
const trueParts = (str: string) => attrParts.filter(part => part.includes(str) && isTrue(part)); | ||
const falseParts = (str: string) => attrParts.filter(part => part.includes(str) && isFalse(part)); | ||
const hasTrueParts = (str: string) => trueParts(str).length > 0; | ||
const hasFalseParts = (str: string) => falseParts(str).length > 0; | ||
|
||
const attrs = { | ||
'generated': hasTrueParts('linguist-generated') ? true : hasFalseParts('linguist-generated') ? false : null, | ||
'vendored': hasTrueParts('linguist-vendored') ? true : hasFalseParts('linguist-vendored') ? false : null, | ||
'documentation': hasTrueParts('linguist-documentation') ? true : hasFalseParts('linguist-documentation') ? false : null, | ||
'binary': hasTrueParts('binary') || hasFalseParts('text') ? true : hasFalseParts('binary') || hasTrueParts('text') ? false : null, | ||
'language': trueParts('linguist-language').at(-1)?.split('=')[1] ?? null, | ||
} | ||
|
||
output.push({ glob: relFileGlob, attrs }); | ||
} | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters