Debian Codegraph @

utils.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package gitconfig
2
3 import (
4 "strings"
5
6 "github.com/gobwas/glob"
7 )
8
9 // globMatch matches a string against a glob pattern.
10 // It uses the gobwas/glob package and supports:
11 // - single-asterisk (*) patterns for matching within a path component
12 // - double-asterisk (**) patterns for matching across path components
13 // - question mark (?) for single character matching
14 // - character classes [abc] and ranges [a-z]
15 //
16 // The pattern uses '/' as a path separator.
17 //
18 // Example:
19 //
20 // globMatch("feat/*", "feat/test") // returns (true, nil)
21 // globMatch("feat/**", "feat/foo/bar") // returns (true, nil)
22 //
23 // Returns:
24 // - (true, nil) if the string matches the pattern.
25 // - (false, nil) if the string does not match.
26 // - (false, error) if the pattern is invalid.
27 func globMatch(pattern, s string) (bool, error) {
28 g, err := glob.Compile(pattern, '/')
29 if err != nil {
30 return false, err
31 }
32
33 return g.Match(s), nil
34 }
35
36 // splitKey splits a fully qualified gitconfig key into two or three parts.
37 // A valid key consists of either a section and a key separated by a dot
38 // or section, subsection and key, all separated by a dot. Note that
39 // the subsection might contain dots itself.
40 //
41 // Valid examples:
42 // - core.push
43 // - [email protected].
44 func splitKey(key string) (section, subsection, skey string) { //nolint:nonamedreturns
45 n := strings.Index(key, ".")
46 if n > 0 {
47 section = key[:n]
48 }
49
50 if m := strings.LastIndex(key, "."); n != m && m > 0 && len(key) > m+1 {
51 subsection = key[n+1 : m]
52 skey = key[m+1:]
53
54 return
55 }
56
57 skey = key[n+1:]
58
59 return
60 }
61
62 // canonicalizeKey normalizes a gitconfig key according to git rules.
63 //
64 // Canonicalization rules (per git-config):
65 // - Section names are converted to lowercase
66 // - Subsection names are kept as-is (case-sensitive per git spec)
67 // - Key names are converted to lowercase
68 //
69 // Returns an empty string if the key is invalid (missing section or key part).
70 //
71 // Examples:
72 //
73 // canonicalizeKey("Core.Push") returns "core.push"
74 // canonicalizeKey("remote.Origin.URL") returns "remote.Origin.url"
75 // canonicalizeKey("valid.key") returns "valid.key"
76 // canonicalizeKey("invalid") returns "" // missing key part
77 func canonicalizeKey(key string) string {
78 if key == "" {
79 // invalid key, return empty string
80 return ""
81 }
82
83 section, subsection, skey := splitKey(key)
84 // "Section names are case-insensitive.""
85 section = strings.ToLower(section)
86 // "Subsection names are case sensitive."
87 // "The variable names are case-insensitive."
88 skey = strings.ToLower(skey)
89
90 if section == "" || skey == "" {
91 // invalid key, return empty string
92 return ""
93 }
94
95 if subsection == "" {
96 return section + "." + skey
97 }
98
99 return section + "." + subsection + "." + skey
100 }
101
102 // trim removes leading and trailing whitespace from all strings in the slice.
103 // It modifies the slice in-place.
104 //
105 // This is a convenience function for cleaning up parsed lines.
106 func trim(s []string) {
107 for i, e := range s {
108 s[i] = strings.TrimSpace(e)
109 }
110 }
111
112 // parseLineForComment separates a line into content and comment parts.
113 //
114 // Parsing rules:
115 // - Searches for the first unquoted comment character (# or ;)
116 // - Ignores comment characters inside double-quoted strings
117 // - Trims whitespace from both content and comment
118 // - Removes surrounding double quotes from content
119 //
120 // The returned comment string does NOT include the delimiter character itself.
121 //
122 // Examples:
123 //
124 // parseLineForComment(`value # comment`) returns ("value", "comment")
125 // parseLineForComment(`"content # not-comment" # comment`) returns ("content # not-comment", "comment")
126 // parseLineForComment(`plain-value`) returns ("plain-value", "")
127 // parseLineForComment(`"quoted value"`) returns ("quoted value", "")
128 func parseLineForComment(line string) (string, string) {
129 line = strings.TrimSpace(line) // Trim whitespace from the line first
130 if !strings.HasPrefix(line, `"`) {
131 // no properly quoted value string, we shouldn't have ended up here.
132 if value, comment, found := strings.Cut(line, "#"); found {
133 return strings.TrimSpace(value), strings.TrimSpace(comment)
134 }
135 if value, comment, found := strings.Cut(line, ";"); found {
136 return strings.TrimSpace(value), strings.TrimSpace(comment)
137 }
138
139 // no comment found, return the line as is
140 return line, ""
141 }
142 commentStartIndex := -1 // Initialize to -1, indicating comment not found yet
143 inQuotes := false
144 foundComment := false // Flag to signal when to break the loop
145
146 // Iterate through the string to find the first unquoted comment character
147 for i, r := range line {
148 switch r {
149 case '"':
150 inQuotes = !inQuotes
151 case '#', ';':
152 if !inQuotes {
153 commentStartIndex = i
154 foundComment = true
155 }
156 }
157 if foundComment {
158 break // Exit the for loop
159 }
160 }
161
162 content := ""
163 comment := ""
164
165 // Determine initial content and comment parts based on index
166 var initialContent string
167 if commentStartIndex != -1 {
168 // Comment character was found
169 initialContent = line[:commentStartIndex]
170 // Extract comment text *after* the delimiter and trim whitespace
171 if commentStartIndex+1 <= len(line) { // Check index bounds
172 comment = strings.TrimSpace(line[commentStartIndex+1:])
173 } else {
174 comment = "" // Delimiter was the very last character
175 }
176 } else {
177 // No unquoted comment character found
178 initialContent = line
179 comment = ""
180 }
181
182 // Trim whitespace from the initial content part FIRST
183 trimmedContent := strings.TrimSpace(initialContent)
184 // Now, check for and remove surrounding quotes from the trimmed content
185 content = strings.Trim(trimmedContent, `"`)
186
187 // Return the processed content and the processed comment part
188 return content, comment
189 }