MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / parseGitAttributes

Function parseGitAttributes

packages/web/src/lib/gitattributes.ts:19–59  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

17// - "linguist-language=Go"
18// - etc.
19export function parseGitAttributes(content: string): GitAttributes {
20 const rules: GitAttributeRule[] = [];
21
22 for (const raw of content.split('\n')) {
23 const line = raw.trim();
24 if (line === '' || line.startsWith('#')) {
25 continue;
26 }
27
28 const fields = line.split(/\s+/);
29 if (fields.length < 2) {
30 continue;
31 }
32
33 const pattern = fields[0];
34 const attrs: Record<string, string> = {};
35
36 for (const field of fields.slice(1)) {
37 if (field.startsWith('!')) {
38 // !attr means unspecified (reset to default)
39 attrs[field.slice(1)] = 'unspecified';
40 } else if (field.startsWith('-')) {
41 // -attr means unset (false)
42 attrs[field.slice(1)] = 'false';
43 } else {
44 const eqIdx = field.indexOf('=');
45 if (eqIdx !== -1) {
46 // attr=value
47 attrs[field.slice(0, eqIdx)] = field.slice(eqIdx + 1);
48 } else {
49 // attr alone means set (true)
50 attrs[field] = 'true';
51 }
52 }
53 }
54
55 rules.push({ pattern, attrs });
56 }
57
58 return { rules };
59}
60
61// resolveLanguageFromGitAttributes returns the linguist-language override for
62// the given file path based on the parsed .gitattributes rules, or undefined

Callers 1

getFileSourceForRepoFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected