GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — claude-code-source-code/src/utils/git/gitConfigParser.ts at main · wangwang4git/claude-code-source-code Expand file tree/ Copy pathgitConfigParser.tsMore file actions 277 lines (243 loc) · 6.49 KB / Copy pathgitConfigParser.tsFile metadata and controls277 lines (243 loc) · 6.49 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 /** * Lightweight parser for .git/config files. * * Verified against git's config.c: * - Section names: case-insensitive, alphanumeric + hyphen * - Subsection names (quoted): case-sensitive, backslash escapes (\\ and \") * - Key names: case-insensitive, alphanumeric + hyphen * - Values: optional quoting, inline comments (# or ;), backslash escapes */ import { readFile } from 'fs/promises' import { join } from 'path' /** * Parse a single value from .git/config. * Finds the first matching key under the given section/subsection. */ export async function parseGitConfigValue( gitDir: string, section: string, subsection: string | null, key: string, ): Promise<string | null> { try { const config = await readFile(join(gitDir, 'config'), 'utf-8') return parseConfigString(config, section, subsection, key) } catch { return null } } /** * Parse a config value from an in-memory config string. * Exported for testing. */ export function parseConfigString( config: string, section: string, subsection: string | null, key: string, ): string | null { const lines = config.split('\n') const sectionLower = section.toLowerCase() const keyLower = key.toLowerCase() let inSection = false for (const line of lines) { const trimmed = line.trim() // Skip empty lines and comment-only lines if (trimmed.length === 0 || trimmed[0] === '#' || trimmed[0] === ';') { continue } // Section header if (trimmed[0] === '[') { inSection = matchesSectionHeader(trimmed, sectionLower, subsection) continue } if (!inSection) { continue } // Key-value line: find the key name const parsed = parseKeyValue(trimmed) if (parsed && parsed.key.toLowerCase() === keyLower) { return parsed.value } } return null } /** * Parse a key = value line. Returns null if the line doesn't contain a valid key. */ function parseKeyValue(line: string): { key: string; value: string } | null { // Read key: alphanumeric + hyphen, starting with alpha let i = 0 while (i < line.length && isKeyChar(line[i]!)) { i++ } if (i === 0) { return null } const key = line.slice(0, i) // Skip whitespace while (i < line.length && (line[i] === ' ' || line[i] === '\t')) { i++ } // Must have '=' if (i >= line.length || line[i] !== '=') { // Boolean key with no value — not relevant for our use cases return null } i++ // skip '=' // Skip whitespace after '=' while (i < line.length && (line[i] === ' ' || line[i] === '\t')) { i++ } const value = parseValue(line, i) return { key, value } } /** * Parse a config value starting at position i. * Handles quoted strings, escape sequences, and inline comments. */ function parseValue(line: string, start: number): string { let result = '' let inQuote = false let i = start while (i < line.length) { const ch = line[i]! // Inline comments outside quotes end the value if (!inQuote && (ch === '#' || ch === ';')) { break } if (ch === '"') { inQuote = !inQuote i++ continue } if (ch === '\\' && i + 1 < line.length) { const next = line[i + 1]! if (inQuote) { // Inside quotes: recognize escape sequences switch (next) { case 'n': result += '\n' break case 't': result += '\t' break case 'b': result += '\b' break case '"': result += '"' break case '\\': result += '\\' break default: // Git silently drops the backslash for unknown escapes result += next break } i += 2 continue } // Outside quotes: backslash at end of line = continuation (we don't // handle multi-line since we split on \n, but handle \\ and others) if (next === '\\') { result += '\\' i += 2 continue } // Fallthrough — treat backslash literally outside quotes } result += ch i++ } // Trim trailing whitespace from unquoted portions. // Git trims trailing whitespace that isn't inside quotes, but since we // process char-by-char and quotes toggle, the simplest correct approach // for single-line values is to trim the result when not ending in a quote. if (!inQuote) { result = trimTrailingWhitespace(result) } return result } function trimTrailingWhitespace(s: string): string { let end = s.length while (end > 0 && (s[end - 1] === ' ' || s[end - 1] === '\t')) { end-- } return s.slice(0, end) } /** * Check if a config line like `[remote "origin"]` matches the given section/subsection. * Section matching is case-insensitive; subsection matching is case-sensitive. */ function matchesSectionHeader( line: string, sectionLower: string, subsection: string | null, ): boolean { // line starts with '[' let i = 1 // Read section name while ( i < line.length && line[i] !== ']' && line[i] !== ' ' && line[i] !== '\t' && line[i] !== '"' ) { i++ } const foundSection = line.slice(1, i).toLowerCase() if (foundSection !== sectionLower) { return false } if (subsection === null) { // Simple section: must end with ']' return i < line.length && line[i] === ']' } // Skip whitespace before subsection quote while (i < line.length && (line[i] === ' ' || line[i] === '\t')) { i++ } // Must have opening quote if (i >= line.length || line[i] !== '"') { return false } i++ // skip opening quote // Read subsection — case-sensitive, handle \\ and \" escapes let foundSubsection = '' while (i < line.length && line[i] !== '"') { if (line[i] === '\\' && i + 1 < line.length) { const next = line[i + 1]! if (next === '\\' || next === '"') { foundSubsection += next i += 2 continue } // Git drops the backslash for other escapes in subsections foundSubsection += next i += 2 continue } foundSubsection += line[i] i++ } // Must have closing quote followed by ']' if (i >= line.length || line[i] !== '"') { return false } i++ // skip closing quote if (i >= line.length || line[i] !== ']') { return false } return foundSubsection === subsection } function isKeyChar(ch: string): boolean { return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch === '-' ) } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |