@matheusbsbs / matheus-bagliano / commits / d14f776402
Fork 0

git config syntax updates

This updates the hierarchical section name syntax to [section<space>+"<randomstring>"] where the only rule for "randomstring" is that it can't contain a newline, and if you really want to insert a double-quote, you do it with \". It turns that into the section name "secion.randomstring". The "section" part is still case insensitive, but the "randomstring" part is case sensitive. So you could use this for things like [email "torvalds@osdl.org"] name = Linus Torvalds if you wanted to do the "email->name" conversion as part of the config file format (I'm not claiming that is sensible, I'm just giving it as an insane example). That would show up as the association email.torvalds@osdl.org.name -> Linus Torvalds which is easy to parse (the "." in the email _looks_ ambiguous, but it isn't: you know that there will always be a single key-name, so you find the key name with "strrchr(name, '.')" and things are entirely unambiguous). Repo-config is updated to be able to parse the new format, and also write things out in the new format. [jc: rolled two patches from Linus and one fix-up from Sean into one, with additional adjustments for t/t1300 test to check the case insensitiveness of section base and variable and case sensitiveness of the extended section part. Then stripped some part off to make the result applicable to the stale 1.3.X series that does not have recent enhancements. ] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Sean Estabrooks <seanlkml@sympatico.ca> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 9, 2006 at 12:24 UTC d14f776402d9f7040cc71ff6e3b992b2e019526a
3 files changed +90 -20
config.c
+81 -13
@@ -134,6 +134,41 @@ static int get_value(config_fn_t fn, char *name, unsigned int len)
134 return fn(name, value);
135 }
136
137 +static int get_extended_base_var(char *name, int baselen, int c)
138 +{
139 + do {
140 + if (c == '\n')
141 + return -1;
142 + c = get_next_char();
143 + } while (isspace(c));
144 +
145 + /* We require the format to be '[base "extension"]' */
146 + if (c != '"')
147 + return -1;
148 + name[baselen++] = '.';
149 +
150 + for (;;) {
151 + int c = get_next_char();
152 + if (c == '\n')
153 + return -1;
154 + if (c == '"')
155 + break;
156 + if (c == '\\') {
157 + c = get_next_char();
158 + if (c == '\n')
159 + return -1;
160 + }
161 + name[baselen++] = c;
162 + if (baselen > MAXNAME / 2)
163 + return -1;
164 + }
165 +
166 + /* Final ']' */
167 + if (get_next_char() != ']')
168 + return -1;
169 + return baselen;
170 +}
171 +
172 static int get_base_var(char *name)
173 {
174 int baselen = 0;
@@ -144,6 +179,8 @@ static int get_base_var(char *name)
179 return -1;
180 if (c == ']')
181 return baselen;
182 + if (isspace(c))
183 + return get_extended_base_var(name, baselen, c);
184 if (!isalnum(c) && c != '.')
185 return -1;
186 if (baselen > MAXNAME / 2)
@@ -335,10 +372,12 @@ static int store_aux(const char* key, const char* value)
372 store.offset[store.seen] = ftell(config_file);
373 store.state = KEY_SEEN;
374 store.seen++;
338 - } else if (strrchr(key, '.') - key == store.baselen &&
375 + } else {
376 + if (strrchr(key, '.') - key == store.baselen &&
377 !strncmp(key, store.key, store.baselen)) {
378 store.state = SECTION_SEEN;
379 store.offset[store.seen] = ftell(config_file);
380 + }
381 }
382 }
383 return 0;
@@ -346,8 +385,30 @@ static int store_aux(const char* key, const char* value)
385
386 static void store_write_section(int fd, const char* key)
387 {
388 + const char *dot = strchr(key, '.');
389 + int len1 = store.baselen, len2 = -1;
390 +
391 + dot = strchr(key, '.');
392 + if (dot) {
393 + int dotlen = dot - key;
394 + if (dotlen < len1) {
395 + len2 = len1 - dotlen - 1;
396 + len1 = dotlen;
397 + }
398 + }
399 +
400 write(fd, "[", 1);
350 - write(fd, key, store.baselen);
401 + write(fd, key, len1);
402 + if (len2 >= 0) {
403 + write(fd, " \"", 2);
404 + while (--len2 >= 0) {
405 + unsigned char c = *++dot;
406 + if (c == '"')
407 + write(fd, "\\", 1);
408 + write(fd, &c, 1);
409 + }
410 + write(fd, "\"", 1);
411 + }
412 write(fd, "]\n", 2);
413 }
414
@@ -421,7 +482,7 @@ int git_config_set(const char* key, const char* value)
482 int git_config_set_multivar(const char* key, const char* value,
483 const char* value_regex, int multi_replace)
484 {
424 - int i;
485 + int i, dot;
486 int fd = -1, in_fd;
487 int ret;
488 char* config_filename = strdup(git_path("config"));
@@ -446,16 +507,23 @@ int git_config_set_multivar(const char* key, const char* value,
507 * Validate the key and while at it, lower case it for matching.
508 */
509 store.key = (char*)malloc(strlen(key)+1);
449 - for (i = 0; key[i]; i++)
450 - if (i != store.baselen &&
451 - ((!isalnum(key[i]) && key[i] != '.') ||
452 - (i == store.baselen+1 && !isalpha(key[i])))) {
453 - fprintf(stderr, "invalid key: %s\n", key);
454 - free(store.key);
455 - ret = 1;
456 - goto out_free;
457 - } else
458 - store.key[i] = tolower(key[i]);
510 + dot = 0;
511 + for (i = 0; key[i]; i++) {
512 + unsigned char c = key[i];
513 + if (c == '.')
514 + dot = 1;
515 + /* Leave the extended basename untouched.. */
516 + if (!dot || i > store.baselen) {
517 + if (!isalnum(c) || (i == store.baselen+1 && !isalpha(c))) {
518 + fprintf(stderr, "invalid key: %s\n", key);
519 + free(store.key);
520 + ret = 1;
521 + goto out_free;
522 + }
523 + c = tolower(c);
524 + }
525 + store.key[i] = c;
526 + }
527 store.key[i] = 0;
528
529 /*
repo-config.c
+6 -4
@@ -48,11 +48,13 @@ static int show_config(const char* key_, const char* value_)
48 static int get_value(const char* key_, const char* regex_)
49 {
50 int i;
51 + char *tl;
52
52 - key = malloc(strlen(key_)+1);
53 - for (i = 0; key_[i]; i++)
54 - key[i] = tolower(key_[i]);
55 - key[i] = 0;
53 + key = strdup(key_);
54 + for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
55 + *tl = tolower(*tl);
56 + for (tl=key; *tl && *tl != '.'; ++tl)
57 + *tl = tolower(*tl);
58
59 if (regex_) {
60 if (regex_[0] == '!') {
t/t1300-repo-config.sh
+3 -3
@@ -229,7 +229,7 @@ test_expect_failure 'invalid key' 'git-repo-config inval.2key blabla'
229 test_expect_success 'correct key' 'git-repo-config 123456.a123 987'
230
231 test_expect_success 'hierarchical section' \
232 - 'git-repo-config 1.2.3.alpha beta'
232 + 'git-repo-config Version.1.2.3eX.Alpha beta'
233
234 cat > expect << EOF
235 [beta] ; silly comment # another comment
@@ -241,8 +241,8 @@ noIndent= sillyValue ; 'nother silly comment
241 NoNewLine = wow2 for me
242 [123456]
243 a123 = 987
244 -[1.2.3]
245 - alpha = beta
244 +[Version "1.2.3eX"]
245 + Alpha = beta
246 EOF
247
248 test_expect_success 'hierarchical section value' 'cmp .git/config expect'